PHP-databaskonstruktion

Log | Files | Refs

commit 0cfaa738c46f844153eab2ba05a7542059e2f4ca
parent e9dd2556bcc606fda3c73e6eec79e3f560f3c256
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Thu, 28 Sep 2023 15:35:53 +0000

Refactoring and bugfixes.

Diffstat:
Mincident.php | 12+++++++++---
Mincidents.php | 2+-
Mindex.php | 4++--
Moperation.php | 4++--
Moperations.php | 2+-
Mterrain.php | 2+-
Mutils/imports.php | 1+
Mutils/insertHandler.php | 26++++++++++++++++++--------
Mutils/logs.txt | 110+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mutils/modalBuilder.php | 24++++++------------------
Autils/postHandlerInterface.php | 7+++++++
Mutils/updateHandler.php | 12++++++------
12 files changed, 164 insertions(+), 42 deletions(-)

diff --git a/incident.php b/incident.php @@ -12,16 +12,18 @@ $pageContent .= "<h3>Operations in $incidentName</h3>" . tableFactory::createTableWithRedirect($queryOperations, "operation.php", ["OperationName", "StartDate", "IncidentName", "IncidentNumber"]); + logg("Incident: " . "'{$incidentName}', '{$incidentNumber}'"); + $operationModalBuilder = (new ModalBuilder()) ->setModalId('insertOperation') ->setTableName("Operation") - ->setInsertHandler($handlerFactory->createHandler('Operation')) + ->setPostHandler($handlerFactory->createHandler('Operation')) ->addColumn("OperationName") ->addColumn("StartDate") ->addColumn("EndDate", true) ->addColumn("SuccessRate", true) ->addDropdownColumn("GroupLeader", getColumnValues("GroupLeaders", "CodeName")) - ->addHiddenColumn("Incident", "'{$incidentName}', '{$incidentNumber}'"); + ->addHiddenColumn("Incident", "{$incidentName}, {$incidentNumber}"); $operationModalBuilder->handleData(); $pageContent .= $operationModalBuilder->build(); @@ -52,7 +54,7 @@ $WriteReportModal = (new ModalBuilder()) ->setModalId('insertReport') ->setTableName("Report") - ->setInsertHandler($handlerFactory->createHandler('Report')) + ->setPostHandler($handlerFactory->createHandler('Report')) ->addColumn("Title") ->addColumn("DateCreated") ->addDropdownColumn("Author", getColumnValues("Agent", "CodeName")) @@ -64,5 +66,9 @@ $pageContent .= $WriteReportModal->build(); $pageContent .= $WriteReportModal->generateOpenButton("Write Report"); + $pageContent .= "<hr>"; + + + include 'utils/pageTemplate.php'; ?> \ No newline at end of file diff --git a/incidents.php b/incidents.php @@ -12,7 +12,7 @@ $modalBuilder = (new ModalBuilder()) ->setModalId('insertModal') ->setTableName("Incident") - ->setInsertHandler($handlerFactory->createHandler('Incident')) + ->setPostHandler($handlerFactory->createHandler('Incident')) ->addColumn("RegionName") ->addColumn("Location") ->addColumn("IncidentName") diff --git a/index.php b/index.php @@ -10,7 +10,7 @@ $attributeModalBuilder = (new ModalBuilder()) ->setModalId('insertAttributeModal') ->setTableName("FieldAgentAttributes") - ->setInsertHandler($handlerFactory->createHandler('FieldAgentAttributes')) + ->setPostHandler($handlerFactory->createHandler('FieldAgentAttributes')) ->addDropdownColumn("AgentCodeName", getColumnValues("FieldAgents", "CodeName")) ->addColumn("Specialty") ->addColumn("Competence"); @@ -30,7 +30,7 @@ $agentModalBuilder = (new ModalBuilder()) ->setModalId('insertAgentModal') ->setTableName("Agent") - ->setInsertHandler($handlerFactory->createHandler('Agent')) + ->setPostHandler($handlerFactory->createHandler('Agent')) ->addColumn("CodeName") ->addColumn("FirstName") ->addColumn("LastName") diff --git a/operation.php b/operation.php @@ -24,7 +24,7 @@ $addAgentModalBuilder = (new ModalBuilder()) ->setModalId('insertModal') ->setTableName("OperatesIn") - ->setInsertHandler($handlerFactory->createHandler('OperatesIn')) + ->setPostHandler($handlerFactory->createHandler('OperatesIn')) ->addHiddenColumn("OperationName", $operationName) ->addHiddenColumn("StartDate", $startDate) ->addHiddenColumn("IncidentName", $incidentName) @@ -54,7 +54,7 @@ $updateGroupLeaderModalBuilder = (new ModalBuilder()) ->setModalId('updateModal') ->setTableName("OperatesIn") - ->setUpdateHandler($updateFactory->createHandler("Operation", $condition)) + ->setPostHandler($updateFactory->createHandler("Operation", $condition)) ->addDropdownColumn("GroupLeader", getColumnValues("GroupLeaders", "CodeName")); $updateGroupLeaderModalBuilder->handleData(); diff --git a/operations.php b/operations.php @@ -12,7 +12,7 @@ $modalBuilder = (new ModalBuilder()) ->setModalId('insertModal') ->setTableName("Operation") - ->setInsertHandler($handlerFactory->createHandler('Operation')) + ->setPostHandler($handlerFactory->createHandler('Operation')) ->addColumn("OperationName") ->addColumn("StartDate") ->addColumn("EndDate", true) diff --git a/terrain.php b/terrain.php @@ -10,7 +10,7 @@ $modalBuilder = (new ModalBuilder()) ->setModalId('insertModal') ->setTableName("Terrain") - ->setInsertHandler($handlerFactory->createHandler('Terrain')) + ->setPostHandler($handlerFactory->createHandler('Terrain')) ->addHiddenColumn("TerrainCode", getTableCount('Terrain') + 1) ->addColumn("TerrainName"); diff --git a/utils/imports.php b/utils/imports.php @@ -5,6 +5,7 @@ include 'components.php'; include 'modalBuilder.php'; include 'tableFactory.php'; + include 'postHandlerInterface.php'; include 'insertHandler.php'; include 'updateHandler.php'; ?> \ No newline at end of file diff --git a/utils/insertHandler.php b/utils/insertHandler.php @@ -1,8 +1,4 @@ <?php - interface InsertHandler { - public function handleInsert($data); - } - class InsertHandlerFactory { public function createHandler($tableName) { switch ($tableName) { @@ -14,7 +10,7 @@ } } - class OperationInsertHandler implements InsertHandler { + class OperationInsertHandler implements PostHandler { private $tableName; private $pdo; @@ -24,7 +20,7 @@ $this->pdo = $db->getPdo(); } - public function handleInsert($data) { + public function handlePostData($data) { if (!isInsert($data)) return; logg("Inserting into: " . $this->tableName); @@ -35,8 +31,14 @@ $successRate = (bool) $data["SuccessRate"] ? true : false; $groupLeader = $data["GroupLeader"]; $incident = $data["Incident"]; + + logg("calling from OperationInsertHandler: \$incident = " . $incident); + list($incidentName, $incidentNumber) = explode(", ", $incident); + logg("IncidentName= " . $incidentName); + logg("IncidentNumber= " . $incidentNumber); + $query = "INSERT INTO {$this->tableName} (OperationName, StartDate, EndDate, SuccessRate, GroupLeader, IncidentName, IncidentNumber) VALUES (?, ?, ?, ?, ?, ?, ?)"; $stmt = $this->pdo->prepare($query); @@ -55,9 +57,13 @@ RefreshTables(); } + + public function getOperationType() { + return "INSERT"; + } } - class GenericInsertHandler implements InsertHandler { + class GenericInsertHandler implements PostHandler { private $tableName; private $pdo; @@ -67,7 +73,7 @@ $this->pdo = $db->getPdo(); } - public function handleInsert($data) { + public function handlePostData($data) { if (!isInsert($data)) return; logg("Inserting into: " . $this->tableName); @@ -103,6 +109,10 @@ } return $data; } + + public function getOperationType() { + return "INSERT"; + } } function isInsert($postData) { diff --git a/utils/logs.txt b/utils/logs.txt @@ -634,3 +634,113 @@ 2023-09-27 23:36:01 - Inserting into: Report 2023-09-27 23:36:30 - Inserting into: Report 2023-09-27 23:37:26 - Inserting into: Report +2023-09-27 23:45:11 - Inserting into: OperatesIn +2023-09-27 23:45:19 - Inserting into: OperatesIn +2023-09-27 23:45:58 - Inserting into: Agent +2023-09-27 23:46:59 - Inserting into: OperatesIn +2023-09-27 23:47:04 - Inserting into: OperatesIn +2023-09-27 23:47:25 - Inserting into: OperatesIn +2023-09-27 23:47:45 - Inserting into: FieldAgentAttributes +2023-09-27 23:48:16 - Inserting into: Report +2023-09-27 23:48:27 - Inserting into: Operation +2023-09-27 23:48:48 - Inserting into: Operation +2023-09-27 23:50:08 - Inserting into: Operation +2023-09-27 23:51:13 - Inserting into: Operation +2023-09-27 23:53:14 - Incident +2023-09-27 23:53:15 - Incident +2023-09-27 23:53:52 - Incident: 'Sabotage', '103 +2023-09-27 23:54:00 - Incident: 'Sabotage', '103 +2023-09-27 23:54:01 - Incident: 'Sabotage', '103 +2023-09-27 23:54:01 - Incident: 'Sabotage', '103 +2023-09-27 23:54:01 - Incident: 'Sabotage', '103 +2023-09-28 08:19:04 - Incident: 'Arson', '108 +2023-09-28 08:19:11 - Inserting into: OperatesIn +2023-09-28 08:19:19 - Inserting into: OperatesIn +2023-09-28 08:19:58 - Incident: 'Arson', '108 +2023-09-28 08:19:58 - Inserting into: Operation +2023-09-28 10:14:34 - Incident: 'Arson', '108 +2023-09-28 10:14:37 - Incident: 'Arson', '108 +2023-09-28 10:15:09 - Incident: 'asdasd', '123123 +2023-09-28 10:15:13 - Incident: 'asdasd', '123123 +2023-09-28 10:15:14 - Incident: 'asdasd', '123123 +2023-09-28 14:54:11 - Incident: 'asdasd', '123123 +2023-09-28 14:54:15 - Incident: 'inringning', '901 +2023-09-28 14:54:37 - Incident: 'inringning', '901 +2023-09-28 14:54:37 - Inserting into: Report +2023-09-28 14:54:37 - Incident: 'inringning', '901 +2023-09-28 14:54:46 - Incident: 'inringning', '901 +2023-09-28 14:54:46 - Inserting into: Operation +2023-09-28 15:10:59 - Incident: 'inringning', '901 +2023-09-28 15:11:01 - Incident: 'inringning', '901 +2023-09-28 15:12:00 - Incident: 'inringning', '901 +2023-09-28 15:12:00 - Incident: 'inringning', '901 +2023-09-28 15:13:02 - Incident: 'inringning', '901 +2023-09-28 15:13:03 - Incident: 'inringning', '901 +2023-09-28 15:13:06 - Incident: 'inringning', '901 +2023-09-28 15:13:24 - Incident: 'inringning', '901 +2023-09-28 15:13:40 - Incident: 'inringning', '901 +2023-09-28 15:13:51 - Incident: 'inringning', '901 +2023-09-28 15:13:51 - Inserting into: Operation +2023-09-28 15:15:42 - Incident: 'inringning', '901 +2023-09-28 15:15:42 - Inserting into: Operation +2023-09-28 15:15:42 - +2023-09-28 15:17:19 - Incident: 'inringning', '901 +2023-09-28 15:17:20 - Incident: 'inringning', '901 +2023-09-28 15:17:20 - Incident: 'inringning', '901 +2023-09-28 15:17:34 - Incident: 'inringning', '901 +2023-09-28 15:17:34 - Inserting into: Operation +2023-09-28 15:17:34 - +2023-09-28 15:19:23 - Incident: 'inringning', '901' +2023-09-28 15:19:23 - Inserting into: Operation +2023-09-28 15:19:23 - calling from OperationInsertHandler: $incident = +2023-09-28 15:20:06 - Incident: 'inringning', '901' +2023-09-28 15:20:07 - Incident: 'inringning', '901' +2023-09-28 15:22:30 - Inserting into: OperatesIn +2023-09-28 15:22:33 - Updating records in: Operation +2023-09-28 15:22:36 - Updating records in: Operation +2023-09-28 15:24:54 - Incident: 'Arson', '108' +2023-09-28 15:26:35 - Incident: 'Arson', '108' +2023-09-28 15:26:43 - Incident: 'Arson', '108' +2023-09-28 15:27:02 - Incident: 'Arson', '108' +2023-09-28 15:27:23 - Incident: 'Arson', '108' +2023-09-28 15:27:23 - Inserting into: Operation +2023-09-28 15:27:23 - calling from OperationInsertHandler: $incident = 'Arson', '108' +2023-09-28 15:27:36 - Incident: 'Arson', '108' +2023-09-28 15:27:57 - Incident: 'Arson', '108' +2023-09-28 15:28:09 - Incident: 'Arson', '108' +2023-09-28 15:28:09 - Inserting into: Operation +2023-09-28 15:28:09 - calling from OperationInsertHandler: $incident = 'Arson', '108' +2023-09-28 15:28:29 - Incident: 'Arson', '108' +2023-09-28 15:28:47 - Incident: '213123asd', '234234' +2023-09-28 15:28:56 - Incident: '213123asd', '234234' +2023-09-28 15:28:56 - Inserting into: Operation +2023-09-28 15:28:56 - calling from OperationInsertHandler: $incident = '213123asd', '234234' +2023-09-28 15:30:09 - Incident: '213123asd', '234234' +2023-09-28 15:30:09 - Inserting into: Operation +2023-09-28 15:30:09 - calling from OperationInsertHandler: $incident = '213123asd', '234234' +2023-09-28 15:30:09 - IncidentName= '213123asd' +2023-09-28 15:30:09 - IncidentNumber= '234234' +2023-09-28 15:30:26 - Incident: '213123asd', '234234' +2023-09-28 15:30:52 - Incident: 'Hijacking', '107' +2023-09-28 15:31:01 - Incident: 'Hijacking', '107' +2023-09-28 15:31:01 - Inserting into: Operation +2023-09-28 15:31:01 - calling from OperationInsertHandler: $incident = 'Hijacking', '107' +2023-09-28 15:31:01 - IncidentName= 'Hijacking' +2023-09-28 15:31:01 - IncidentNumber= '107' +2023-09-28 15:31:48 - Incident: 'Hijacking', '107' +2023-09-28 15:31:48 - Inserting into: Operation +2023-09-28 15:31:48 - calling from OperationInsertHandler: $incident = 'Hijacking', '107' +2023-09-28 15:31:48 - IncidentName= 'Hijacking' +2023-09-28 15:31:48 - IncidentNumber= '107' +2023-09-28 15:32:00 - Incident: 'Hijacking', '107' +2023-09-28 15:32:05 - Incident: 'Hijacking', '107' +2023-09-28 15:32:05 - Inserting into: Operation +2023-09-28 15:32:05 - calling from OperationInsertHandler: $incident = Hijacking, 107 +2023-09-28 15:32:05 - IncidentName= Hijacking +2023-09-28 15:32:05 - IncidentNumber= 107 +2023-09-28 15:32:05 - Incident: 'Hijacking', '107' +2023-09-28 15:33:06 - Inserting into: OperatesIn +2023-09-28 15:33:34 - Incident: 'Kidnapping', '102' +2023-09-28 15:34:06 - Inserting into: Agent +2023-09-28 15:34:30 - Inserting into: FieldAgentAttributes +2023-09-28 15:34:50 - Inserting into: OperatesIn diff --git a/utils/modalBuilder.php b/utils/modalBuilder.php @@ -6,8 +6,7 @@ private $requiredColumns = []; private $hiddenColumns = []; private $modalId; - private $insertHandler; - private $updateHandler; + private $postHandler; public function setTableName($tableName) { $this->tableName = $tableName; @@ -19,13 +18,8 @@ return $this; } - public function setInsertHandler(InsertHandler $handler) { - $this->insertHandler = $handler; - return $this; - } - - public function setUpdateHandler(UpdateHandler $handler) { - $this->updateHandler = $handler; + public function setPostHandler(PostHandler $handler) { + $this->postHandler = $handler; return $this; } @@ -55,11 +49,7 @@ public function handleData() { if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['tableName']) && $_POST['tableName'] === $this->tableName) { - if ($this->insertHandler !== null) { - $this->insertHandler->handleInsert($_POST); - } elseif ($this->updateHandler !== null) { - $this->updateHandler->handleUpdate($_POST); - } + $this->postHandler->handlePostData($_POST); } } @@ -99,15 +89,13 @@ } foreach ($this->hiddenColumns as $column => $value) { - $modalBody .= "<input type='hidden' name='$column' value='$value'>"; + $modalBody .= "<input type=\"hidden\" name=\"$column\" value=\"$value\">"; } - $operationType = $this->insertHandler !== null ? "INSERT" : "UPDATE"; - $modalEnd = "</div> <div class='modal-footer'> <input type='hidden' name='tableName' value='{$this->tableName}'> - <input type='hidden' name='operationType' value='{$operationType}'> + <input type='hidden' name='operationType' value='{$this->postHandler->getOperationType()}'> <button type='submit' class='btn btn-primary'>Save</button> </div> </div> diff --git a/utils/postHandlerInterface.php b/utils/postHandlerInterface.php @@ -0,0 +1,6 @@ +<?php + interface PostHandler { + public function handlePostData($data); + public function getOperationType(); + } +?> +\ No newline at end of file diff --git a/utils/updateHandler.php b/utils/updateHandler.php @@ -1,8 +1,4 @@ <?php - interface UpdateHandler { - public function handleUpdate($data); - } - class UpdateHandlerFactory { public function createHandler($tableName, $condition) { switch ($tableName) { @@ -12,7 +8,7 @@ } } - class GenericUpdateHandler implements UpdateHandler { + class GenericUpdateHandler implements PostHandler { private $tableName; private $pdo; private $condition; @@ -24,7 +20,7 @@ $this->pdo = $db->getPdo(); } - public function handleUpdate($data) { + public function handlePostData($data) { if (!isUpdate($data)) return; logg("Updating records in: " . $this->tableName); @@ -47,6 +43,10 @@ RefreshTables(); } + + public function getOperationType() { + return "UPDATE"; + } } function isUpdate($postData) {