PHP-databaskonstruktion

Log | Files | Refs

tableFactory.php (5238B)


      1 <?php
      2     class tableFactory {
      3 
      4         //create table
      5         public static function createTable($query, $table = null) {
      6             $sql = "SELECT * FROM $query;";
      7 
      8             $tableBuilder = (new TableBuilder($sql));
      9 
     10             if (isset($table)) {
     11                 $tableBuilder->addActionColumn(function ($row) use ($table) {
     12                     return self::addDeleteButton($row, $table);
     13                 });
     14             }
     15 
     16             return $tableBuilder->buildTable();
     17         }
     18 
     19         /**
     20          * Create table that redirects user to another address on click.
     21          * $query: The query to be displayed.
     22          * $adress: The redirect adress. If user clicks a row, redirect to relative adress.
     23          * $queryColumns: The columns that should be queried in the redirect. 
     24          * 
     25          * Ex: CreateTableWithRedirect("Operation", "operationdetails.php", ["OperationName"]),
     26          * Clicking "Operation1" would result in a redirect to "./operationdetails.php/?OperationName=Operation1"
     27          */
     28         public static function createTableWithRedirect($query, $address, $queryColumns, $table = null) {
     29             $tableBuilder = (new TableBuilder($query));
     30             $tableBuilder->setRedirect($address, $queryColumns);
     31         
     32             if (isset($table)) {
     33                 $tableBuilder->addActionColumn(function ($row) use ($table) {
     34                     return self::addDeleteButton($row, $table);
     35                 });
     36             }
     37             
     38             return $tableBuilder->buildTable();
     39         }
     40 
     41         /**
     42          * Create a custom table, that displays any table generated from an sql query.
     43          */
     44         public static function createCustomTable($query, $table = null) {
     45             $tableBuilder = (new TableBuilder($query));
     46             
     47             if (isset($table)) {
     48                 $tableBuilder->addActionColumn(function ($row) use ($table) {
     49                     return self::addDeleteButton($row, $table);
     50                 });
     51             }
     52 
     53             return $tableBuilder->buildTable();
     54         }
     55 
     56         /**
     57          * Generate a card layout instead of a table layout. 
     58          * Only suitable for smaller datatables.
     59          */
     60         public static function createCardTable($tableName) {
     61             $db = dbconnection::getInstance();
     62             $pdo = $db->getPdo();
     63         
     64             $output = "<div class='card-columns'>";
     65         
     66             try {
     67                 $headers = [];
     68                 $firstRow = true;
     69                 foreach($pdo->query('SELECT * FROM ' . $tableName . ';') AS $row) {
     70                     if($firstRow) {
     71                         foreach($row as $key => $value) {
     72                             if(!is_numeric($key)) { 
     73                                 $headers[] = $key;
     74                             }
     75                         }
     76                         $firstRow = false;
     77                     }
     78         
     79                     $output .= "<div class='card'>";
     80                     $output .= "<div class='card-header'>$tableName</div>";
     81                     $output .= "<div class='card-body'>";
     82                     
     83                     for($i = 0; $i < count($headers); $i++) {
     84                         if(isset($row[$headers[$i]]) && !is_numeric($headers[$i])) {
     85                             $output .= "<p class='card-text'>" . $headers[$i] . ": " . $row[$headers[$i]] . "</p>";
     86                         }
     87                     }
     88                     
     89                     $output .= "</div>";
     90                     $output .= "</div>";
     91                 }
     92                 $output .= "</div>";
     93         
     94                 return $output;
     95             } catch (PDOException $e) {
     96                 echo "Error: " . $e->getMessage();
     97                 return '';
     98             }
     99         }
    100 
    101         /**
    102          * Specify a callback function which will be called when a user presses a row.
    103          * @param $function = the callback function (function foo($row))
    104          */
    105         public static function createTableWithCallbackColumn($query, $function, $table = null) {
    106             $tableBuilder = (new TableBuilder($query));
    107 
    108             if (isset($table)) {
    109                 $tableBuilder->addActionColumn(function ($row) use ($table) {
    110                     return self::addDeleteButton($row, $table);
    111                 });
    112             }
    113 
    114             return $tableBuilder->addActionColumn($function)->buildTable();
    115         }
    116 
    117         /**
    118          * Generate a delete button that redirects to delete.php 
    119          * with all of the necessary information embedded in the url to delete a row.
    120          */
    121         private static function addDeleteButton($row, $table) {
    122         
    123             $params = [];
    124             foreach ($row as $key => $value) {
    125                 if (!is_numeric($key)) {
    126                     if (isset($value)) {
    127                         $params[] = "{$key}=" . urlencode($value);
    128                     }
    129                 }
    130             }
    131             $queryString = implode('&', $params);
    132         
    133             $tableQuery = "table=$table&";
    134             
    135             $deleteUrl = "./delete.php?" . $tableQuery . $queryString;
    136             
    137             return "<a href='$deleteUrl' class='btn btn-block btn-danger m-0'>Delete</a>";
    138         }
    139     }
    140 ?>