PHP-databaskonstruktion

Log | Files | Refs

tableBuilder.php (2794B)


      1 <?php
      2     class TableBuilder {
      3 
      4         private $db;
      5         private $query;
      6         private $redirectAddress;
      7         private $queryColumns = [];
      8         private $actionCallbacks = [];
      9 
     10         public function __construct($query) {
     11             $this->db = dbconnection::getInstance();
     12             $this->query = $query;
     13         }
     14 
     15         public function setRedirect($address, $queryColumns = []) {
     16             $this->redirectAddress = $address;
     17             $this->queryColumns = $queryColumns;
     18             return $this;
     19         }
     20 
     21         public function addActionColumn($function, $headerTitle = "Action") {
     22             $this->actionCallbacks[] = ['callback' => $function, 'header' => $headerTitle];
     23             return $this;
     24         }
     25 
     26         private function buildRow($row) {
     27             $rowData = "<tr";
     28 
     29             if ($this->redirectAddress) {
     30                 $queryParams = array_map(function($col) use ($row) {
     31                     return $col . "=" . urlencode($row[$col]);
     32                 }, $this->queryColumns);
     33                 $redirectUrl = $this->redirectAddress . "?" . implode("&", $queryParams);
     34                 $rowData .= " onclick=\"window.location.href = '" . $redirectUrl . "'\" style='cursor: pointer;'";
     35             }
     36 
     37             $rowData .= ">";
     38 
     39             foreach ($row as $key => $value) {
     40                 if (!is_numeric($key)) {
     41                     $rowData .= "<td>" . $value . "</td>";
     42                 }
     43             }
     44 
     45             foreach ($this->actionCallbacks as $action) {
     46                 $rowData .= "<td style='width: 80px; white-space: nowrap;'>" . call_user_func($action['callback'], $row) . "</td>";
     47             }
     48             $rowData .= "</tr>";
     49             
     50             return $rowData;
     51         }
     52 
     53         public function buildTable() {
     54             $hover = isset($this->redirectAddress) ? "table-hover" : "";
     55             $output = "<div class='overflow-auto mt-1 mb-1'><table class='table table-striped table-bordered $hover'><thead class='thead-dark'><tr>";
     56 
     57             foreach ($this->db->getPdo()->query($this->query) as $row) {
     58                 foreach ($row as $key => $value) {
     59                     if (!is_numeric($key)) {
     60                         $output .= "<th>" . $key . "</th>";
     61                     }
     62                 }
     63                 foreach ($this->actionCallbacks as $action) {
     64                     $output .= "<th style='width: 80px; white-space: nowrap;'>" . $action['header'] . "</th>";
     65                 }
     66                 $output .= "</tr></thead><tbody>";
     67                 break; 
     68             }
     69 
     70             foreach ($this->db->getPdo()->query($this->query) as $row) {
     71                 $output .= $this->buildRow($row);
     72             }
     73 
     74             $output .= "</tbody></table></div>";
     75             return $output;
     76         }
     77     }
     78 ?>