commit b9dcb06772b3aa8c3e1af289b02eb29413671ab1
parent 25770d041173ef70a3aed750a8ab30dd8437dd2e
Author: William Lindholm <william_lindholm@outlook.com>
Date: Wed, 27 Sep 2023 21:20:44 +0000
Added insert functionality using InsertHandler.
Diffstat:
7 files changed, 189 insertions(+), 30 deletions(-)
diff --git a/db/dbhelper.php b/db/dbhelper.php
@@ -36,4 +36,13 @@
return [];
}
}
+
+ function RefreshTables() {
+ $location = $_SERVER['PHP_SELF'];
+ if (!empty($_SERVER['QUERY_STRING'])) {
+ $location .= "?" . $_SERVER['QUERY_STRING'];
+ }
+ header("Location: " . $location);
+ exit();
+ }
?>
\ No newline at end of file
diff --git a/operation.php b/operation.php
@@ -4,6 +4,7 @@
require 'utils/imports.php';
dbconnection::getInstance('mysql', 'a22willi', 'root', 'Safiren1');
$handlerFactory = new InsertHandlerFactory();
+ $updateFactory = new UpdateHandlerFactory();
$pageContent = '';
@@ -12,28 +13,51 @@
$incidentName = urldecode($_GET['IncidentName']);
$incidentNumber = urldecode($_GET['IncidentNumber']);
- $query = "SELECT CodeName FROM OperatesIn
- WHERE OperationName = '{$operationName}'
- AND StartDate = '{$startDate}'
- AND IncidentName = '{$incidentName}'
- AND IncidentNumber = {$incidentNumber}";
-
- $pageContent .= "<h3>Agents in $operationName</h3>" . tableFactory::createCustomTable($query);
-
- $modalBuilder = (new ModalBuilder())
- ->setModalId('insertModal')
- ->setTableName("OperatesIn")
- ->setInsertHandler($handlerFactory->createHandler('OperatesIn'))
- ->addHiddenColumn("OperationName", $operationName)
- ->addHiddenColumn("StartDate", $startDate)
- ->addHiddenColumn("IncidentName", $incidentName)
- ->addHiddenColumn("IncidentNumber", $incidentNumber)
- ->addDropdownColumn("CodeName", getColumnValues("FieldAgents", "CodeName"));
-
-
- $modalBuilder->handleData();
- $pageContent .= $modalBuilder->build();
- $pageContent .= $modalBuilder->generateOpenButton("Add agent");
+ $queryCodeName = "SELECT CodeName AS Agents FROM OperatesIn
+ WHERE OperationName = '{$operationName}'
+ AND StartDate = '{$startDate}'
+ AND IncidentName = '{$incidentName}'
+ AND IncidentNumber = {$incidentNumber}";
+
+ $pageContent .= "<h3>Agents in $operationName</h3>" . tableFactory::createCustomTable($queryCodeName);
+
+ $addAgentModalBuilder = (new ModalBuilder())
+ ->setModalId('insertModal')
+ ->setTableName("OperatesIn")
+ ->setInsertHandler($handlerFactory->createHandler('OperatesIn'))
+ ->addHiddenColumn("OperationName", $operationName)
+ ->addHiddenColumn("StartDate", $startDate)
+ ->addHiddenColumn("IncidentName", $incidentName)
+ ->addHiddenColumn("IncidentNumber", $incidentNumber)
+ ->addDropdownColumn("CodeName", getColumnValues("FieldAgents", "CodeName"));
+
+
+ $addAgentModalBuilder->handleData();
+ $pageContent .= $addAgentModalBuilder->build();
+ $pageContent .= $addAgentModalBuilder->generateOpenButton("Add agent");
+
+ $queryGroupLeader = "SELECT GroupLeader FROM Operation
+ WHERE OperationName = '{$operationName}'
+ AND StartDate = '{$startDate}'
+ AND IncidentName = '{$incidentName}'
+ AND IncidentNumber = {$incidentNumber}";
+
+ $pageContent .= "<h3>Groupleader for $operationName</h3>" . tableFactory::createCustomTable($queryGroupLeader);
+
+ $condition = "OperationName = '{$operationName}'
+ AND StartDate = '{$startDate}'
+ AND IncidentName = '{$incidentName}'
+ AND IncidentNumber = {$incidentNumber}";
+
+ $updateGroupLeaderModalBuilder = (new ModalBuilder())
+ ->setModalId('updateModal')
+ ->setTableName("OperatesIn")
+ ->setUpdateHandler($updateFactory->createHandler("Operation", $condition))
+ ->addDropdownColumn("GroupLeader", getColumnValues("GroupLeaders", "CodeName"));
+
+ $updateGroupLeaderModalBuilder->handleData();
+ $pageContent .= $updateGroupLeaderModalBuilder->build();
+ $pageContent .= $updateGroupLeaderModalBuilder->generateOpenButton("Change GroupLeader");
include 'utils/pageTemplate.php';
?>
\ No newline at end of file
diff --git a/utils/imports.php b/utils/imports.php
@@ -6,4 +6,5 @@
include 'modalBuilder.php';
include 'tableFactory.php';
include 'insertHandler.php';
+ include 'updateHandler.php';
?>
\ No newline at end of file
diff --git a/utils/insertHandler.php b/utils/insertHandler.php
@@ -25,6 +25,8 @@
}
public function handleInsert($data) {
+ if (!isInsert($data)) return;
+
logg("Inserting into: " . $this->tableName);
$operationName = $data["OperationName"];
@@ -66,9 +68,12 @@
}
public function handleInsert($data) {
+ if (!isInsert($data)) return;
+
logg("Inserting into: " . $this->tableName);
unset($data['tableName']);
+ unset($data['operationType']);
$data = $this->convertStringsToAppropriateTypes($data);
@@ -100,12 +105,7 @@
}
}
- function RefreshTables() {
- $location = $_SERVER['PHP_SELF'];
- if (!empty($_SERVER['QUERY_STRING'])) {
- $location .= "?" . $_SERVER['QUERY_STRING'];
- }
- header("Location: " . $location);
- exit();
+ function isInsert($postData) {
+ return $postData["operationType"] === "INSERT" ? true : false;
}
?>
\ No newline at end of file
diff --git a/utils/logs.txt b/utils/logs.txt
@@ -540,3 +540,56 @@
2023-09-27 19:11:34 - Inserting into: OperatesIn
2023-09-27 19:11:37 - Inserting into: OperatesIn
2023-09-27 19:11:53 - Inserting into: OperatesIn
+2023-09-27 20:38:14 - Inserting into: OperatesIn
+2023-09-27 20:38:22 - Inserting into: OperatesIn
+2023-09-27 20:39:17 - Inserting into: OperatesIn
+2023-09-27 20:39:25 - Inserting into: OperatesIn
+2023-09-27 20:49:33 - Updating records in: Operation
+2023-09-27 20:50:16 - Updating records in: Operation
+2023-09-27 20:50:42 - Updating records in: Operation
+2023-09-27 20:50:42 - UPDATE Operation SET CodeName = ?, tableName = ?, operationType = ? WHERE
+2023-09-27 20:52:05 - Inserting into: Agent
+2023-09-27 20:53:47 - Inserting into: Agent
+2023-09-27 20:54:31 - Updating records in: Operation
+2023-09-27 20:54:31 - UPDATE Operation SET CodeName = ? WHERE
+2023-09-27 21:12:39 - Updating records in: Operation
+2023-09-27 21:12:39 - UPDATE Operation SET CodeName = CodeName WHERE OperationName = 'Operation sol'
+ AND StartDate = '2021-03-23'
+ AND IncidentName = 'Theft'
+ AND IncidentNumber = 110
+2023-09-27 21:14:49 - Updating records in: Operation
+2023-09-27 21:14:49 - UPDATE Operation SET CodeName = 'CodeName' WHERE OperationName = 'Operation sol'
+ AND StartDate = '2021-03-23'
+ AND IncidentName = 'Theft'
+ AND IncidentNumber = 110
+2023-09-27 21:14:55 - Updating records in: Operation
+2023-09-27 21:14:55 - UPDATE Operation SET GroupLeader = 'GroupLeader' WHERE OperationName = 'Operation sol'
+ AND StartDate = '2021-03-23'
+ AND IncidentName = 'Theft'
+ AND IncidentNumber = 110
+2023-09-27 21:17:08 - Updating records in: Operation
+2023-09-27 21:17:08 - Array
+2023-09-27 21:17:08 - UPDATE Operation SET GroupLeader = 'GroupLeader' WHERE OperationName = 'Operation sol'
+ AND StartDate = '2021-03-23'
+ AND IncidentName = 'Theft'
+ AND IncidentNumber = 110
+2023-09-27 21:18:22 - Updating records in: Operation
+2023-09-27 21:18:22 - UPDATE Operation SET GroupLeader = 'A1' WHERE OperationName = 'Operation sol'
+ AND StartDate = '2021-03-23'
+ AND IncidentName = 'Theft'
+ AND IncidentNumber = 110
+2023-09-27 21:20:13 - Updating records in: Operation
+2023-09-27 21:20:13 - UPDATE Operation SET GroupLeader = 'A1' WHERE OperationName = 'Operation sol'
+ AND StartDate = '2021-03-23'
+ AND IncidentName = 'Theft'
+ AND IncidentNumber = 110
+2023-09-27 21:20:16 - Updating records in: Operation
+2023-09-27 21:20:16 - UPDATE Operation SET GroupLeader = 'L29' WHERE OperationName = 'Operation sol'
+ AND StartDate = '2021-03-23'
+ AND IncidentName = 'Theft'
+ AND IncidentNumber = 110
+2023-09-27 21:20:19 - Updating records in: Operation
+2023-09-27 21:20:19 - UPDATE Operation SET GroupLeader = 'D56' WHERE OperationName = 'Operation sol'
+ AND StartDate = '2021-03-23'
+ AND IncidentName = 'Theft'
+ AND IncidentNumber = 110
diff --git a/utils/modalBuilder.php b/utils/modalBuilder.php
@@ -7,6 +7,7 @@
private $hiddenColumns = [];
private $modalId;
private $insertHandler;
+ private $updateHandler;
public function setTableName($tableName) {
$this->tableName = $tableName;
@@ -22,6 +23,11 @@
$this->insertHandler = $handler;
return $this;
}
+
+ public function setUpdateHandler(UpdateHandler $handler) {
+ $this->updateHandler = $handler;
+ return $this;
+ }
public function addColumn($column, $optional = false) {
$this->columns[] = $column;
@@ -49,7 +55,11 @@
public function handleData() {
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['tableName']) && $_POST['tableName'] === $this->tableName) {
- $this->insertHandler->handleInsert($_POST);
+ if ($this->insertHandler !== null) {
+ $this->insertHandler->handleInsert($_POST);
+ } elseif ($this->updateHandler !== null) {
+ $this->updateHandler->handleUpdate($_POST);
+ }
}
}
@@ -92,9 +102,12 @@
$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}'>
<button type='submit' class='btn btn-primary'>Save</button>
</div>
</div>
diff --git a/utils/updateHandler.php b/utils/updateHandler.php
@@ -0,0 +1,58 @@
+<?php
+ interface UpdateHandler {
+ public function handleUpdate($data);
+ }
+
+ class UpdateHandlerFactory {
+ public function createHandler($tableName, $condition) {
+ switch ($tableName) {
+ default:
+ return new GenericUpdateHandler($tableName, $condition);
+ }
+ }
+ }
+
+ class GenericUpdateHandler implements UpdateHandler {
+ private $tableName;
+ private $pdo;
+ private $condition;
+
+ public function __construct($tableName, $condition) {
+ $this->condition = $condition;
+ $this->tableName = $tableName;
+ $db = dbconnection::getInstance();
+ $this->pdo = $db->getPdo();
+ }
+
+ public function handleUpdate($data) {
+ if (!isUpdate($data)) return;
+
+ logg("Updating records in: " . $this->tableName);
+
+ unset($data['tableName']);
+ unset($data['operationType']);
+
+ $setClause = [];
+ foreach ($data as $column => $value) {
+ $setClause[] = "$column = '{$value}'";
+ }
+ $setClause = implode(', ', $setClause);
+
+ $sql = "UPDATE {$this->tableName} SET {$setClause} WHERE {$this->condition}";
+
+ logg($sql);
+
+ $stmt = $this->pdo->prepare($sql);
+
+ if (!$stmt->execute()) {
+ throw new Exception("Failed to update data in {$this->tableName}.");
+ }
+
+ RefreshTables();
+ }
+ }
+
+ function isUpdate($postData) {
+ return $postData["operationType"] === "UPDATE" ? true : false;
+ }
+?>
+\ No newline at end of file