hanze/iwa-panda2

Controller/Templates.php in main
Repositories | Summary | Log | Files | README.md

Templates.php (6871B) download


  1<?php
  2
  3namespace Controller {
  4
  5    /// Controller\Templates is a utility class for creating HTML-forms and other constructs
  6    class Templates
  7    {
  8        private \Lollipop\SQLDatabase $db;
  9        private \Lollipop\DatabaseObject $table;
 10        private string $schema = 'panda';
 11        private string $table_name;
 12
 13        public function __construct(\Lollipop\SQLDatabase $db, \Lollipop\DatabaseObject $table)
 14        {
 15            $this->db = $db;
 16            $this->table = $table;
 17            $this->table_name = $table::class;
 18        }
 19
 20        public function form(string $action, array $data = [], array $response = []): string
 21        {
 22            /*auto-increment fields are automatically hidden*/
 23            $form_type = "Add";
 24            $form = '<form method="POST" action="'. $action . '">';
 25            foreach($this->table->get_col_names_ai() as $col) {
 26                if($data == []) {
 27                    $value = '-1';
 28                } else {
 29                    if(in_array($col, array_keys($data))) {
 30                        $value = $data[$col];
 31                    }
 32                    $form_type = "Update";
 33                }
 34                $form .= '<input type="hidden" name="' . $col . '" value="' . $value . '">';
 35            }
 36            $form .= '<input type="hidden" name="form_type" value="' . $form_type . '">';
 37            foreach($this->table->get_col_names_no_ai() as $col) {
 38                if($data == []) {
 39                    $value = '';
 40                } else {
 41                    if(in_array($col, array_keys($data))) {
 42                        $value = $data[$col];
 43                    }
 44                }
 45                $form .= '<input type="text" name="' . $col . '" placeholder="' . $col . '" value="' . $value . '">';
 46                $miss_key = 'missing_'.$col;
 47                if(array_key_exists($miss_key, $response)) {
 48                    $form .= '<div class="form-response"><p style="color:red;"> col: '. $col . ' cannot be empty</p></div>';
 49                }
 50            }
 51            $form .='
 52                <input type="submit" value="'. $form_type .'"> 
 53            </form>';
 54
 55            return $form;
 56        }
 57
 58        public function form_v2(string $action, array $values = [], array $extra = [], array $response = []): string
 59        {
 60            /*auto-increment fields are automatically hidden*/
 61            if(sizeof($values) == 0) {
 62                $form_type = "Add";
 63            } else {
 64                $form_type = "Update";
 65            }
 66            $form = '<h1>'. $form_type .' '. $this->table->get_table() .'</h1>
 67                    <a href="/'. $this->table->get_table() .'">New</a>';
 68            $form .= '<form method="POST" action="'. $action . '">';
 69            foreach($this->table->get_col_info() as $col => $info) {
 70                if(isset($info["extra"]) && $info["extra"] == "auto_increment") {
 71                    $form .= '<input type="hidden" name="' . $col . '" placeholder="' . $col . '" value="';
 72                    if(isset($values[$col])) {
 73                        $form .= $values[$col];
 74                    }
 75                    $form .= '">';
 76                } elseif(isset($info["extra"]) && $info["extra"] == "password") {
 77                    $form .= '<input type="password" name="' . $col . '" placeholder="' . $col . '">';
 78                } elseif(isset($info["input_type"])) {
 79                    $form .= '<input type="'. $info["input_type"] .'" name="' . $col . '" placeholder="' . $col . '" value="';
 80                    if(isset($values[$col])) {
 81                        $form .= $values[$col];
 82                    }
 83                    $form .= '">';
 84                }
 85                $miss_key = 'missing_'.$col;
 86                if(array_key_exists($miss_key, $response)) {
 87                    $form .= '<div class="form-response"><p style="color:red;"> col: '. $col . ' cannot be empty</p></div>';
 88                }
 89            }
 90            foreach($extra as $html) {
 91                $form.= $html;
 92            }
 93            $form .= '<input type="hidden" name="form_type" " value="' . $form_type . '">';
 94            $form .='
 95                <input type="submit" value="'. $form_type .'"> 
 96            </form>';
 97            return $form;
 98        }
 99
100        public function search_form(string $action): string
101        {
102            return '
103                    <form method="POST" action="'. $action . '">
104                        <input type="text" name="search" placeholder="Search...">
105                        <input type="submit" value="Search"> 
106                    </form>';
107        }
108
109        public function crud_table(string $action, string $search = "", string $search_key = "", \Model\PermissionUser $permissionUser = null): string
110        {
111            if($search == "") {
112                $search = "%";
113            } else {
114                $search = "%$search%";
115            }
116            $table = "<table> <thead> <tr>";
117            foreach($this->table->get_column_names() as $column) {
118                $table .= "<th>$column</th>";
119            }
120            $table .= "<th>Alter</th> <th>Delete</th>";
121            if($permissionUser != null) {
122                $table .= "<th>user permissions</th>";
123            }
124            $table .= "</tr> </thead>";
125
126            $objs = $this->db->all_where($this->table_name, [$search_key => $search]);
127            $table .= "<tbody>";
128            foreach($objs as $obj) {
129                $table .= "<tr>";
130                $col_names = $obj->get_column_names();
131                foreach($col_names as $col) {
132                    $table .= '<td>';
133                    if($col == "email" || $col == "name" || $col == "course" || $col == "exam") {
134                        $table .= '<a href="/'.$this->table->get_table().'/' .$obj->{$col}.'">'. $obj->{$col} . '</a></td>';
135                    } else {
136                        $table .= $obj->{$col} . '</td>';
137                    }
138                }
139                $table .= '
140                    <td>
141                        <a class="edit" href="' . $action . '/'. $obj->{$this->table->get_primary()} .'/edit/">Edit</a>
142                    </td>
143                    <td>
144                        <a class="delete" href="' . $action . '/'. $obj->{$this->table->get_primary()} .'/delete/">Delete</a>
145                    </td>
146                    <td>';
147                if($permissionUser != null) {
148                    foreach($this->db->all_where(\Model\PermissionUser::class, [$permissionUser->get_primary() => $obj->{$this->table->get_primary()}]) as $perm) {
149                        $table .= $perm->id_permission . ' ';
150                    }
151                }
152                $table .= '</td> </tr>';
153            }
154
155            $table .= "
156                </tbody>
157            </table>";
158            return $table;
159        }
160
161    }
162}