PHP-databaskonstruktion

Log | Files | Refs

commit 8e0b88c90e29553889f1880efe1686c889ea39aa
parent 1c9b08f6835ce28d51b9e7775eb485c78d6a30cd
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Wed, 27 Sep 2023 09:38:40 +0000

Implemented InsertHandler.

Diffstat:
Mcomponents.php | 43++++++++++++++++++++++++-------------------
Mdbhelper.php | 18++++++++++++++++++
Mimports.php | 2++
Mincidents.php | 6+++---
Mindex.php | 11++++++++---
AinsertHandler.php | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MmodalBuilder.php | 12++++++++++++
Moperations.php | 4++--
MpageTemplate.php | 3+--
Dstylesheet.css | 7-------
AtableFactory.php | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mterrain.php | 11++++++++++-
12 files changed, 230 insertions(+), 37 deletions(-)

diff --git a/components.php b/components.php @@ -2,31 +2,36 @@ function displayTable($tableName) { $db = dbconnection::getInstance(); $pdo = $db->getPdo(); - + $output = "<div class='overflow-auto'><table class='table table-striped table-bordered'>"; - - $firstRow = true; - foreach($pdo->query('SELECT * FROM ' . $tableName . ';') AS $row) { - if($firstRow) { - $output .= "<thead class='thead-dark'><tr>"; + + try { + $firstRow = true; + foreach($pdo->query('SELECT * FROM ' . $tableName . ';') AS $row) { + if($firstRow) { + $output .= "<thead class='thead-dark'><tr>"; + foreach($row as $key => $value) { + if(!is_numeric($key)) { + $output .= "<th>" . $key . "</th>"; + } + } + $output .= "</tr></thead><tbody>"; + $firstRow = false; + } + $output .= "<tr>"; foreach($row as $key => $value) { if(!is_numeric($key)) { - $output .= "<th>" . $key . "</th>"; + $output .= "<td>" . $value . "</td>"; } } - $output .= "</tr></thead><tbody>"; - $firstRow = false; - } - $output .= "<tr>"; - foreach($row as $key => $value) { - if(!is_numeric($key)) { - $output .= "<td>" . $value . "</td>"; - } + $output .= "</tr>"; } - $output .= "</tr>"; + $output .= "</tbody></table></div>"; + + return $output; + } catch (PDOException $e) { + echo "Error: " . $e->getMessage(); + return ''; } - $output .= "</tbody></table></div>"; - - return $output; } ?> \ No newline at end of file diff --git a/dbhelper.php b/dbhelper.php @@ -18,4 +18,22 @@ return []; } } + + function GetCompositeKeyValues($table, $columns) { + $db = dbconnection::getInstance(); + $pdo = $db->getPdo(); + + try { + $query = "SELECT CONCAT_WS(', ', " . implode(", ", $columns) . ") AS composite_key FROM $table"; + $stmt = $pdo->prepare($query); + $stmt->execute(); + + $values = $stmt->fetchAll(PDO::FETCH_COLUMN); + + return $values; + } catch (PDOException $e) { + echo "Error: " . $e->getMessage(); + return []; + } + } ?> \ No newline at end of file diff --git a/imports.php b/imports.php @@ -4,4 +4,6 @@ include 'logging.php'; include 'components.php'; include 'modalBuilder.php'; + include 'tableFactory.php'; + include 'insertHandler.php'; ?> \ No newline at end of file diff --git a/incidents.php b/incidents.php @@ -4,15 +4,15 @@ $pageContent = ''; - $pageContent .= "<h3>FieldAgents</h3>" . displayTable("Incident"); + $pageContent .= "<h3>FieldAgents</h3>" . tableFactory::createTable("Incident"); $modalBuilder = (new ModalBuilder()) ->setModalId('insertModal') ->setTableName("Incident") ->addColumn("RegionName") ->addColumn("Location") - ->addDropdownColumn("Incident", ['True', 'False']) #add GetCompositeKeyValues(); - ->addDropdownColumn("Terrain", getColumnValues("Terrain", "TerrainCode")); #add GetColumnValues(table, column); + ->addDropdownColumn("Incident", GetCompositeKeyValues("Incident", ["IncidentName", "IncidentNumber"])) #add GetCompositeKeyValues(table, [column1, column2...]); separate using ", " + ->addDropdownColumn("Terrain", getColumnValues("Terrain", "TerrainCode")); $pageContent .= $modalBuilder->build(); $pageContent .= $modalBuilder->generateOpenButton("Create incident"); diff --git a/index.php b/index.php @@ -4,13 +4,17 @@ $pageContent = ''; - $pageContent .= "<h3>FieldAgents</h3>" . displayTable("FieldAgents"); - $pageContent .= "<h3>GroupLeaders</h3>" . displayTable("GroupLeaders"); - $pageContent .= "<h3>Managers</h3>" . displayTable("Managers"); + $pageContent .= "<h3>FieldAgents</h3>" . tableFactory::createTable("FieldAgents"); + $pageContent .= "<h3>GroupLeaders</h3>" . tableFactory::createTable("GroupLeaders"); + $pageContent .= "<h3>Managers</h3>" . tableFactory::createTable("Managers"); + + $handlerFactory = new InsertHandlerFactory(); $modalBuilder = (new ModalBuilder()) ->setModalId('insertModal') ->setTableName("Agent") + ->setInsertHandler($handlerFactory->createHandler('Agent')) + ->addColumn("CodeName") ->addColumn("FirstName") ->addColumn("LastName") ->addColumn("Salary", true) @@ -18,6 +22,7 @@ ->addDropdownColumn("IsGroupLeader", ['False', 'True']) ->addDropdownColumn("IsManager", ['False', 'True']); + $pageContent .= $modalBuilder->handleData(); $pageContent .= $modalBuilder->build(); $pageContent .= $modalBuilder->generateOpenButton("Hire agent"); diff --git a/insertHandler.php b/insertHandler.php @@ -0,0 +1,63 @@ +<?php + interface InsertHandler { + public function handleInsert($data); + } + + class InsertHandlerFactory { + public function createHandler($tableName) { + switch ($tableName) { + case 'Agent': + return new GenericInsertHandler($tableName); + case 'Incident': + return new IncidentInsertHandler(); + default: + throw new Exception("No handler found for table: $tableName"); + } + } + } + + class OrdersInsertHandler implements InsertHandler { + public function handleInsert($data) { + // Logic to insert data into the 'orders' table + } + } + + class GenericInsertHandler implements InsertHandler { + private $tableName; + private $pdo; + + public function __construct($tableName) { + $this->tableName = $tableName; + $db = dbconnection::getInstance(); + $this->pdo = $db->getPdo(); + } + + public function handleInsert($data) { + unset($data['tableName']); + + $data = $this->convertBooleanStringsToInt($data); + + $columns = array_keys($data); + $placeholders = array_fill(0, count($columns), '?'); + + $sql = "INSERT INTO {$this->tableName} (" . implode(',', $columns) . ") VALUES (" . implode(',', $placeholders) . ")"; + + $stmt = $this->pdo->prepare($sql); + + if (!$stmt->execute(array_values($data))) { + throw new Exception("Failed to insert data into {$this->tableName}."); + } + } + + private function convertBooleanStringsToInt(array $data): array { + foreach ($data as $key => $value) { + if (strtolower($value) === 'true') { + $data[$key] = 1; + } elseif (strtolower($value) === 'false') { + $data[$key] = 0; + } + } + return $data; + } + } +?> +\ No newline at end of file diff --git a/modalBuilder.php b/modalBuilder.php @@ -5,6 +5,7 @@ private $dropdownColumns = []; private $requiredColumns = []; private $modalId; + private $insertHandler; public function setTableName($tableName) { $this->tableName = $tableName; @@ -15,6 +16,11 @@ $this->modalId = $modalId; return $this; } + + public function setInsertHandler(InsertHandler $handler) { + $this->insertHandler = $handler; + return $this; + } public function addColumn($column, $optional = false) { $this->columns[] = $column; @@ -35,6 +41,12 @@ return "<button type='button' class='btn btn-primary' data-toggle='modal' data-target='#{$this->modalId}'>{$label}</button>"; } + public function handleData() { + if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['tableName']) && $_POST['tableName'] === $this->tableName) { + $this->insertHandler->handleInsert($_POST); + } + } + public function build() { $modalStart = "<form action='' method='POST'> <div class='modal fade' id='{$this->modalId}' tabindex='-1' role='dialog' aria-labelledby='insertModalLabel' aria-hidden='true'> diff --git a/operations.php b/operations.php @@ -4,7 +4,7 @@ $pageContent = ''; - $pageContent .= "<h3>Operations</h3>" . displayTable("Operation"); + $pageContent .= "<h3>Operations</h3>" . tableFactory::createTable("Operation"); $modalBuilder = (new ModalBuilder()) ->setModalId('insertModal') @@ -13,7 +13,7 @@ ->addColumn("StartDate") ->addColumn("EndDate", true) ->addColumn("SuccessRate", true) - ->addDropdownColumn("GroupLeader", ['True', 'False']) #add GetColumnValues(table, column); + ->addDropdownColumn("GroupLeader", getColumnValues("GroupLeaders", "CodeName")) ->addDropdownColumn("Incident", ['True', 'False']); #add GetCompositeKeyValues(); $pageContent .= $modalBuilder->build(); diff --git a/pageTemplate.php b/pageTemplate.php @@ -13,7 +13,6 @@ <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PUCKO-PORTAL</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> - <link rel="stylesheet" type="text/css" href="stylesheet.css"> <script defer src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script> <script defer src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js"></script> <script defer src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> @@ -48,7 +47,7 @@ <br><br><br><br><br><br> - <footer class="footer py-3 bg-dark text-white text-center"> + <footer class="footer py-3 bg-dark text-white text-center fixed-bottom"> <div class="container"> <p>&copy;<?php date('Y');?> PUCKO-PORTAL. All rights reserved.</p> </div> diff --git a/stylesheet.css b/stylesheet.css @@ -1,6 +0,0 @@ -.footer { - position: fixed; - left: 0; - bottom: 0; - width: 100%; -} -\ No newline at end of file diff --git a/tableFactory.php b/tableFactory.php @@ -0,0 +1,85 @@ +<?php + class tableFactory { + + //create table + public static function createTable($tableName) { + $db = dbconnection::getInstance(); + $pdo = $db->getPdo(); + + $output = "<div class='overflow-auto'><table class='table table-striped table-bordered'>"; + + try { + $firstRow = true; + foreach($pdo->query('SELECT * FROM ' . $tableName . ';') AS $row) { + if($firstRow) { + $output .= "<thead class='thead-dark'><tr>"; + foreach($row as $key => $value) { + if(!is_numeric($key)) { + $output .= "<th>" . $key . "</th>"; + } + } + $output .= "</tr></thead><tbody>"; + $firstRow = false; + } + $output .= "<tr>"; + foreach($row as $key => $value) { + if(!is_numeric($key)) { + $output .= "<td>" . $value . "</td>"; + } + } + } + $output .= "</tbody></table></div>"; + + return $output; + } catch (PDOException $e) { + echo "Error: " . $e->getMessage(); + return ''; + } + } + + /** + * Create table that redirects user to another address on click. + * $tableName: The table to display. + * $adress: The redirect adress. If user clicks a row, redirect to relative adress. + * $queryColumns: The columns that should be queried in the redirect. + * + * Ex: CreateTableWithRedirect("Operation", "operationdetails.php", ["OperationName"]), + * Clicking "Operation1" would result in a redirect to "./operationdetails.php/?OperationName=Operation1" + */ + // + public static function createTableWithRedirect($tableName, $adress, $queryColumns) { + $db = dbconnection::getInstance(); + $pdo = $db->getPdo(); + + $output = "<div class='overflow-auto'><table class='table table-striped table-bordered'>"; + + try { + $firstRow = true; + foreach($pdo->query('SELECT * FROM ' . $tableName . ';') AS $row) { + if($firstRow) { + $output .= "<thead class='thead-dark'><tr>"; + foreach($row as $key => $value) { + if(!is_numeric($key)) { + $output .= "<th>" . $key . "</th>"; + } + } + $output .= "</tr></thead><tbody>"; + $firstRow = false; + } + $output .= "<tr>"; + foreach($row as $key => $value) { + if(!is_numeric($key)) { + $output .= "<td>" . $value . "</td>"; + } + } + } + $output .= "</tbody></table></div>"; + + return $output; + } catch (PDOException $e) { + echo "Error: " . $e->getMessage(); + return ''; + } + } + } +?> +\ No newline at end of file diff --git a/terrain.php b/terrain.php @@ -4,7 +4,16 @@ $pageContent = ''; - $pageContent .= "<h3>Terrain</h3>" . displayTable("Terrain"); + $pageContent .= "<h3>Terrain</h3>" . tableFactory::createTable("Terrain"); + + $modalBuilder = (new ModalBuilder()) + ->setModalId('insertModal') + ->setTableName("Terrain") + ->addColumn("TerrainCode") + ->addColumn("TerrainName"); + + $pageContent .= $modalBuilder->build(); + $pageContent .= $modalBuilder->generateOpenButton("Create Terrain"); include 'pageTemplate.php'; ?> \ No newline at end of file