PHP-databaskonstruktion

Log | Files | Refs

modalBuilder.php (5812B)


      1 <?php
      2     class ModalBuilder {
      3         private $tableName;
      4         private $columns = [];
      5         private $dropdownColumns = [];
      6         private $requiredColumns = [];
      7         private $hiddenColumns = [];
      8         private $dateColumns = [];
      9         private $procedure;
     10         private $modalId;
     11         private $postHandler;
     12             
     13         public function setTableName($tableName) {
     14             $this->tableName = $tableName;
     15             return $this;
     16         }
     17     
     18         public function setModalId($modalId = "modal") {
     19             $this->modalId = $modalId;
     20             return $this;
     21         }
     22 
     23         public function setPostHandler(PostHandler $handler) {
     24             $this->postHandler = $handler;
     25             return $this;
     26         }
     27 
     28         public function setProcedure($procedureName) {
     29             $this->procedure = $procedureName;
     30             return $this;
     31         }
     32     
     33         public function addColumn($column, $optional = false) {
     34             $this->columns[] = $column;
     35             
     36             if (!$optional) {
     37                 $this->requiredColumns[] = $column;
     38             }
     39 
     40             return $this;
     41         }
     42 
     43         public function addDateColumn($column) {
     44             $this->dateColumns[] = $column;
     45             return $this;
     46         }
     47 
     48         public function addHiddenColumn($column, $value) {
     49             $this->hiddenColumns[$column] = $value;
     50             return $this;
     51         }
     52 
     53         public function addDropdownColumn($column, $values) {
     54             $this->dropdownColumns[$column] = $values;
     55             return $this;
     56         }
     57     
     58         public function generateOpenButton($label = "Open Modal") {
     59             return "<button type='button' class='btn btn-primary' data-toggle='modal' data-target='#{$this->modalId}'>{$label}</button>";
     60         }
     61 
     62         public function handleData() {
     63             if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['tableName']) && $_POST['tableName'] === $this->tableName) {
     64                 $this->postHandler->handlePostData($_POST);
     65             }
     66             if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['operationType']) && isset($_POST['Function']) && $_POST['Function'] === $this->procedure) {
     67                 $this->postHandler->handlePostData($_POST);
     68             }
     69         }
     70 
     71         public function build() {
     72             $modalStart = "<form action='' method='POST'>
     73                             <div class='modal fade' id='{$this->modalId}' tabindex='-1' role='dialog' aria-labelledby='insertModalLabel' aria-hidden='true'>
     74                                 <div class='modal-dialog' role='document'>
     75                                     <div class='modal-content rounded'>
     76                                         <div class='modal-header'>
     77                                             <button type='button' class='close ml-0' data-dismiss='modal' aria-label='Close'>
     78                                                 <span aria-hidden='true'>&times;</span>
     79                                             </button>
     80                                             <h5 class='modal-title'>Insert into: {$this->tableName}</h5>
     81                                         </div>
     82                                         <div class='modal-body'>";
     83 
     84             $modalBody = '';
     85             foreach ($this->columns as $column) {
     86                 $required = in_array($column, $this->requiredColumns) ? 'required' : '';
     87                 $modalBody .= "<div class='form-group'>
     88                                     <label for='$column'>$column</label>
     89                                     <input type='text' class='form-control' id='$column' name='$column' placeholder='$column' $required>
     90                                 </div>";
     91             }
     92 
     93             foreach ($this->dateColumns as $column) {
     94                 $date = date('Y-m-d');
     95                 $modalBody .= "<div class='form-group'>
     96                                 <label for='$column'>$column</label>
     97                                 <input type='date' value='$date' class='form-control' id='$column' name='$column'>
     98                             </div>";
     99             }
    100 
    101             foreach ($this->dropdownColumns as $column => $values) {
    102                 $options = '';
    103                 foreach ($values as $value) {
    104                     $options .= "<option value='{$value}'>{$value}</option>";
    105                 }
    106                 $modalBody .= "<div class='form-group'>
    107                                     <label for='$column'>$column</label>
    108                                     <select class='form-control' id='$column' name='$column' required>
    109                                         $options
    110                                     </select>
    111                                 </div>";
    112             }
    113 
    114             foreach ($this->hiddenColumns as $column => $value) {
    115                 $modalBody .= "<input type=\"hidden\" name=\"$column\" value=\"$value\">";
    116             }
    117 
    118             $procedureOrTableName = isset($this->procedure) ? $this->procedure : $this->tableName;
    119             $procedureOrTable = isset($this->procedure) ? "Function" : "tableName";
    120             $submitMessage = isset($this->procedure) ? "Submit" : "Save";
    121 
    122             $modalEnd = "</div>
    123                             <div class='modal-footer'>
    124                                 <input type='hidden' name='$procedureOrTable' value='$procedureOrTableName'>
    125                                 <input type='hidden' name='operationType' value='{$this->postHandler->getOperationType()}'>
    126                                 <button type='submit' class='btn btn-primary'>$submitMessage</button>
    127                             </div>
    128                         </div>
    129                     </div>
    130                     </div>
    131                 </form>";
    132 
    133             return $modalStart . $modalBody . $modalEnd;
    134         }
    135     }
    136 ?>