commit 27c625b53d3e9361fa2edc77332f309e0dd9d6a2
parent 9f91e82ed3123671a556c9c7304e52794d6e23d5
Author: William Lindholm <william_lindholm@outlook.com>
Date: Wed, 27 Sep 2023 18:18:57 +0000
Receiving post from operations modal.
Diffstat:
| M | insertHandler.php | | | 68 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------- |
1 file changed, 58 insertions(+), 10 deletions(-)
diff --git a/insertHandler.php b/insertHandler.php
@@ -6,19 +6,52 @@
class InsertHandlerFactory {
public function createHandler($tableName) {
switch ($tableName) {
- case 'Agent':
- return new GenericInsertHandler($tableName);
- case 'Incident':
- return new IncidentInsertHandler();
+ case 'Operation':
+ return new OperationInsertHandler($tableName);
default:
- throw new Exception("No handler found for table: $tableName");
+ return new GenericInsertHandler($tableName);
}
}
}
- class OrdersInsertHandler implements InsertHandler {
+ class OperationInsertHandler implements InsertHandler {
+ private $tableName;
+ private $pdo;
+
+ public function __construct($tableName) {
+ $this->tableName = $tableName;
+ $db = dbconnection::getInstance();
+ $this->pdo = $db->getPdo();
+ }
+
public function handleInsert($data) {
- // Logic to insert data into the 'orders' table
+ $operationName = $data["OperationName"];
+ $startDate = $data["StartDate"];
+ $endDate = $data["EndDate"];
+ $successRate = (bool) $data["SuccessRate"] ? true : false;
+ $groupLeader = $data["GroupLeader"];
+ $incident = $data["Incident"];
+ list($incidentName, $incidentNumber) = explode(", ", $incident);
+
+ $query = "INSERT INTO {$this->tableName} (OperationName, StartDate, EndDate, SuccessRate, GroupLeader, IncidentName, IncidentNumber) VALUES (?, ?, ?, ?, ?, ?, ?)";
+
+ $stmt = $this->pdo->prepare($query);
+
+ $stmt->bindParam(1, $operationName, PDO::PARAM_STR);
+ $stmt->bindParam(2, $startDate, PDO::PARAM_STR);
+ $stmt->bindParam(3, $endDate, PDO::PARAM_STR);
+ $stmt->bindParam(4, $successRate, PDO::PARAM_BOOL);
+ $stmt->bindParam(5, $groupLeader, PDO::PARAM_STR);
+ $stmt->bindParam(6, $incidentName, PDO::PARAM_STR);
+ $stmt->bindParam(7, $incidentNumber, PDO::PARAM_INT);
+
+ logg("inserting into table {$this->tableName}");
+
+ if (!$stmt->execute()) {
+ throw new Exception("Failed to insert data into {$this->tableName}.");
+ }
+
+ RefreshTables();
}
}
@@ -35,29 +68,44 @@
public function handleInsert($data) {
unset($data['tableName']);
- $data = $this->convertBooleanStringsToInt($data);
+ $data = $this->convertStringsToAppropriateTypes($data);
$columns = array_keys($data);
$placeholders = array_fill(0, count($columns), '?');
- $sql = "INSERT INTO {$this->tableName} (" . implode(',', $columns) . ") VALUES (" . implode(',', $placeholders) . ")";
+ $sql = "INSERT INTO {$this->tableName} (" . implode(', ', $columns) . ") VALUES (" . implode(', ', $placeholders) . ")";
$stmt = $this->pdo->prepare($sql);
+ logg("inserting into table {$this->tableName}");
+
if (!$stmt->execute(array_values($data))) {
throw new Exception("Failed to insert data into {$this->tableName}.");
}
+
+ RefreshTables();
}
- private function convertBooleanStringsToInt(array $data): array {
+ private function convertStringsToAppropriateTypes(array $data): array {
foreach ($data as $key => $value) {
if (strtolower($value) === 'true') {
$data[$key] = 1;
} elseif (strtolower($value) === 'false') {
$data[$key] = 0;
+ } elseif (is_string($value) && ctype_digit($value)) {
+ $data[$key] = (int) $value;
}
}
return $data;
}
}
+
+ 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