@@ -10,71 +10,71 @@ discard block |
||
| 10 | 10 | { |
| 11 | 11 | use \Anax\DI\TInjectable; |
| 12 | 12 | |
| 13 | - //private $id; |
|
| 13 | + //private $id; |
|
| 14 | 14 | |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Get the table name. |
|
| 18 | - * |
|
| 19 | - * @return string with the table name. |
|
| 20 | - */ |
|
| 21 | - public function getSource() |
|
| 22 | - { |
|
| 23 | - return strtolower(implode('', array_slice(explode('\\', get_class($this)), -1))); |
|
| 24 | - } |
|
| 16 | + /** |
|
| 17 | + * Get the table name. |
|
| 18 | + * |
|
| 19 | + * @return string with the table name. |
|
| 20 | + */ |
|
| 21 | + public function getSource() |
|
| 22 | + { |
|
| 23 | + return strtolower(implode('', array_slice(explode('\\', get_class($this)), -1))); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Find and return all. |
|
| 28 | - * |
|
| 29 | - * @return array |
|
| 30 | - */ |
|
| 31 | - public function findAll() |
|
| 32 | - { |
|
| 33 | - $this->db->select() |
|
| 34 | - ->from($this->getSource()); |
|
| 26 | + /** |
|
| 27 | + * Find and return all. |
|
| 28 | + * |
|
| 29 | + * @return array |
|
| 30 | + */ |
|
| 31 | + public function findAll() |
|
| 32 | + { |
|
| 33 | + $this->db->select() |
|
| 34 | + ->from($this->getSource()); |
|
| 35 | 35 | |
| 36 | - $this->db->execute(); |
|
| 37 | - $this->db->setFetchModeClass(__CLASS__); |
|
| 38 | - return $this->db->fetchAll(); |
|
| 39 | - } |
|
| 36 | + $this->db->execute(); |
|
| 37 | + $this->db->setFetchModeClass(__CLASS__); |
|
| 38 | + return $this->db->fetchAll(); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Get object properties. |
|
| 43 | - * |
|
| 44 | - * @return array with object properties. |
|
| 45 | - */ |
|
| 46 | - public function getProperties() |
|
| 47 | - { |
|
| 48 | - $properties = get_object_vars($this); |
|
| 49 | - unset($properties['di']); |
|
| 50 | - unset($properties['db']); |
|
| 41 | + /** |
|
| 42 | + * Get object properties. |
|
| 43 | + * |
|
| 44 | + * @return array with object properties. |
|
| 45 | + */ |
|
| 46 | + public function getProperties() |
|
| 47 | + { |
|
| 48 | + $properties = get_object_vars($this); |
|
| 49 | + unset($properties['di']); |
|
| 50 | + unset($properties['db']); |
|
| 51 | 51 | |
| 52 | - return $properties; |
|
| 53 | - } |
|
| 52 | + return $properties; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Find and return specific. |
|
| 58 | - * |
|
| 59 | - * @return this |
|
| 60 | - */ |
|
| 61 | - public function find($id) |
|
| 62 | - { |
|
| 56 | + /** |
|
| 57 | + * Find and return specific. |
|
| 58 | + * |
|
| 59 | + * @return this |
|
| 60 | + */ |
|
| 61 | + public function find($id) |
|
| 62 | + { |
|
| 63 | 63 | $this->db->select() |
| 64 | - ->from($this->getSource()) |
|
| 65 | - ->where("id = ?"); |
|
| 64 | + ->from($this->getSource()) |
|
| 65 | + ->where("id = ?"); |
|
| 66 | 66 | |
| 67 | 67 | $this->db->execute([$id]); |
| 68 | 68 | return $this->db->fetchInto($this); |
| 69 | - } |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Save current object/row. |
|
| 73 | - * |
|
| 74 | - * @param array $values key/values to save or empty to use object properties. |
|
| 75 | - * |
|
| 76 | - * @return boolean true or false if saving went okey. |
|
| 77 | - */ |
|
| 71 | + /** |
|
| 72 | + * Save current object/row. |
|
| 73 | + * |
|
| 74 | + * @param array $values key/values to save or empty to use object properties. |
|
| 75 | + * |
|
| 76 | + * @return boolean true or false if saving went okey. |
|
| 77 | + */ |
|
| 78 | 78 | public function save($values = []) |
| 79 | 79 | { |
| 80 | 80 | |
@@ -87,149 +87,149 @@ discard block |
||
| 87 | 87 | } else { |
| 88 | 88 | return $this->create($values); |
| 89 | 89 | } |
| 90 | - } |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Set object properties. |
|
| 94 | - * |
|
| 95 | - * @param array $properties with properties to set. |
|
| 96 | - * |
|
| 97 | - * @return void |
|
| 98 | - */ |
|
| 99 | - public function setProperties($properties) |
|
| 100 | - { |
|
| 101 | - // Update object with incoming values, if any |
|
| 102 | - if (!empty($properties)) { |
|
| 103 | - foreach ($properties as $key => $val) { |
|
| 104 | - $this->$key = $val; |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - } |
|
| 92 | + /** |
|
| 93 | + * Set object properties. |
|
| 94 | + * |
|
| 95 | + * @param array $properties with properties to set. |
|
| 96 | + * |
|
| 97 | + * @return void |
|
| 98 | + */ |
|
| 99 | + public function setProperties($properties) |
|
| 100 | + { |
|
| 101 | + // Update object with incoming values, if any |
|
| 102 | + if (!empty($properties)) { |
|
| 103 | + foreach ($properties as $key => $val) { |
|
| 104 | + $this->$key = $val; |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * Create new row. |
|
| 111 | - * |
|
| 112 | - * @param array $values key/values to save. |
|
| 113 | - * |
|
| 114 | - * @return boolean true or false if saving went okey. |
|
| 115 | - */ |
|
| 116 | - public function create($values) |
|
| 117 | - { |
|
| 118 | - $keys = array_keys($values); |
|
| 119 | - $values = array_values($values); |
|
| 109 | + /** |
|
| 110 | + * Create new row. |
|
| 111 | + * |
|
| 112 | + * @param array $values key/values to save. |
|
| 113 | + * |
|
| 114 | + * @return boolean true or false if saving went okey. |
|
| 115 | + */ |
|
| 116 | + public function create($values) |
|
| 117 | + { |
|
| 118 | + $keys = array_keys($values); |
|
| 119 | + $values = array_values($values); |
|
| 120 | 120 | |
| 121 | - $this->db->insert( |
|
| 122 | - $this->getSource(), |
|
| 123 | - $keys |
|
| 124 | - ); |
|
| 121 | + $this->db->insert( |
|
| 122 | + $this->getSource(), |
|
| 123 | + $keys |
|
| 124 | + ); |
|
| 125 | 125 | |
| 126 | - $res = $this->db->execute($values); |
|
| 126 | + $res = $this->db->execute($values); |
|
| 127 | 127 | |
| 128 | - $this->id = $this->db->lastInsertId(); |
|
| 128 | + $this->id = $this->db->lastInsertId(); |
|
| 129 | 129 | |
| 130 | - return $res; |
|
| 131 | - } |
|
| 130 | + return $res; |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | 133 | |
| 134 | - /** |
|
| 135 | - * Update row. |
|
| 136 | - * |
|
| 137 | - * @param array $values key/values to save. |
|
| 138 | - * |
|
| 139 | - * @return boolean true or false if saving went okey. |
|
| 140 | - */ |
|
| 141 | - public function update($values) |
|
| 142 | - { |
|
| 143 | - $keys = array_keys($values); |
|
| 144 | - $values = array_values($values); |
|
| 134 | + /** |
|
| 135 | + * Update row. |
|
| 136 | + * |
|
| 137 | + * @param array $values key/values to save. |
|
| 138 | + * |
|
| 139 | + * @return boolean true or false if saving went okey. |
|
| 140 | + */ |
|
| 141 | + public function update($values) |
|
| 142 | + { |
|
| 143 | + $keys = array_keys($values); |
|
| 144 | + $values = array_values($values); |
|
| 145 | 145 | |
| 146 | - // Its update, remove id and use as where-clause |
|
| 147 | - unset($keys['id']); |
|
| 148 | - $values[] = $this->id; |
|
| 146 | + // Its update, remove id and use as where-clause |
|
| 147 | + unset($keys['id']); |
|
| 148 | + $values[] = $this->id; |
|
| 149 | 149 | |
| 150 | - $this->db->update( |
|
| 151 | - $this->getSource(), |
|
| 152 | - $keys, |
|
| 153 | - "id = ?" |
|
| 154 | - ); |
|
| 150 | + $this->db->update( |
|
| 151 | + $this->getSource(), |
|
| 152 | + $keys, |
|
| 153 | + "id = ?" |
|
| 154 | + ); |
|
| 155 | 155 | |
| 156 | - return $this->db->execute($values); |
|
| 157 | - } |
|
| 156 | + return $this->db->execute($values); |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - /** |
|
| 160 | - * Delete row. |
|
| 161 | - * |
|
| 162 | - * @param integer $id to delete. |
|
| 163 | - * |
|
| 164 | - * @return boolean true or false if deleting went okey. |
|
| 165 | - */ |
|
| 166 | - public function delete($id) |
|
| 167 | - { |
|
| 168 | - $this->db->delete( |
|
| 169 | - $this->getSource(), |
|
| 170 | - 'id = ?' |
|
| 171 | - ); |
|
| 159 | + /** |
|
| 160 | + * Delete row. |
|
| 161 | + * |
|
| 162 | + * @param integer $id to delete. |
|
| 163 | + * |
|
| 164 | + * @return boolean true or false if deleting went okey. |
|
| 165 | + */ |
|
| 166 | + public function delete($id) |
|
| 167 | + { |
|
| 168 | + $this->db->delete( |
|
| 169 | + $this->getSource(), |
|
| 170 | + 'id = ?' |
|
| 171 | + ); |
|
| 172 | 172 | |
| 173 | - return $this->db->execute([$id]); |
|
| 174 | - } |
|
| 173 | + return $this->db->execute([$id]); |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | 176 | |
| 177 | - /** |
|
| 178 | - * Build a select-query. |
|
| 179 | - * |
|
| 180 | - * @param string $columns which columns to select. |
|
| 181 | - * |
|
| 182 | - * @return $this |
|
| 183 | - */ |
|
| 184 | - public function query($columns = '*') |
|
| 185 | - { |
|
| 186 | - $this->db->select($columns) |
|
| 187 | - ->from($this->getSource()); |
|
| 177 | + /** |
|
| 178 | + * Build a select-query. |
|
| 179 | + * |
|
| 180 | + * @param string $columns which columns to select. |
|
| 181 | + * |
|
| 182 | + * @return $this |
|
| 183 | + */ |
|
| 184 | + public function query($columns = '*') |
|
| 185 | + { |
|
| 186 | + $this->db->select($columns) |
|
| 187 | + ->from($this->getSource()); |
|
| 188 | 188 | |
| 189 | - return $this; |
|
| 190 | - } |
|
| 189 | + return $this; |
|
| 190 | + } |
|
| 191 | 191 | |
| 192 | - /** |
|
| 193 | - * Build the where part. |
|
| 194 | - * |
|
| 195 | - * @param string $condition for building the where part of the query. |
|
| 196 | - * |
|
| 197 | - * @return $this |
|
| 198 | - */ |
|
| 199 | - public function where($condition) |
|
| 200 | - { |
|
| 201 | - $this->db->where($condition); |
|
| 192 | + /** |
|
| 193 | + * Build the where part. |
|
| 194 | + * |
|
| 195 | + * @param string $condition for building the where part of the query. |
|
| 196 | + * |
|
| 197 | + * @return $this |
|
| 198 | + */ |
|
| 199 | + public function where($condition) |
|
| 200 | + { |
|
| 201 | + $this->db->where($condition); |
|
| 202 | 202 | |
| 203 | - return $this; |
|
| 204 | - } |
|
| 203 | + return $this; |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | - /** |
|
| 207 | - * Build the where part. |
|
| 208 | - * |
|
| 209 | - * @param string $condition for building the where part of the query. |
|
| 210 | - * |
|
| 211 | - * @return $this |
|
| 212 | - */ |
|
| 213 | - public function andWhere($condition) |
|
| 214 | - { |
|
| 215 | - $this->db->andWhere($condition); |
|
| 206 | + /** |
|
| 207 | + * Build the where part. |
|
| 208 | + * |
|
| 209 | + * @param string $condition for building the where part of the query. |
|
| 210 | + * |
|
| 211 | + * @return $this |
|
| 212 | + */ |
|
| 213 | + public function andWhere($condition) |
|
| 214 | + { |
|
| 215 | + $this->db->andWhere($condition); |
|
| 216 | 216 | |
| 217 | - return $this; |
|
| 218 | - } |
|
| 217 | + return $this; |
|
| 218 | + } |
|
| 219 | 219 | |
| 220 | - /** |
|
| 221 | - * Execute the query built. |
|
| 222 | - * |
|
| 223 | - * @param string $query custom query. |
|
| 224 | - * |
|
| 225 | - * @return $this |
|
| 226 | - */ |
|
| 227 | - public function execute($params = []) |
|
| 228 | - { |
|
| 229 | - $this->db->execute($this->db->getSQL(), $params); |
|
| 230 | - $this->db->setFetchModeClass(__CLASS__); |
|
| 220 | + /** |
|
| 221 | + * Execute the query built. |
|
| 222 | + * |
|
| 223 | + * @param string $query custom query. |
|
| 224 | + * |
|
| 225 | + * @return $this |
|
| 226 | + */ |
|
| 227 | + public function execute($params = []) |
|
| 228 | + { |
|
| 229 | + $this->db->execute($this->db->getSQL(), $params); |
|
| 230 | + $this->db->setFetchModeClass(__CLASS__); |
|
| 231 | 231 | |
| 232 | - return $this->db->fetchAll(); |
|
| 233 | - } |
|
| 232 | + return $this->db->fetchAll(); |
|
| 233 | + } |
|
| 234 | 234 | |
| 235 | 235 | } |
@@ -140,7 +140,7 @@ |
||
| 140 | 140 | */ |
| 141 | 141 | public function update($values) |
| 142 | 142 | { |
| 143 | - $keys = array_keys($values); |
|
| 143 | + $keys = array_keys($values); |
|
| 144 | 144 | $values = array_values($values); |
| 145 | 145 | |
| 146 | 146 | // Its update, remove id and use as where-clause |
@@ -8,43 +8,43 @@ |
||
| 8 | 8 | |
| 9 | 9 | class CHTMLTable{ |
| 10 | 10 | |
| 11 | - private $data; |
|
| 12 | - private $headline; |
|
| 11 | + private $data; |
|
| 12 | + private $headline; |
|
| 13 | 13 | |
| 14 | - //tar emot en headline som enkel array och data som tvådimensionell array |
|
| 15 | - public function __construct($headline, $data){ |
|
| 14 | + //tar emot en headline som enkel array och data som tvådimensionell array |
|
| 15 | + public function __construct($headline, $data){ |
|
| 16 | 16 | $this->data = $data; |
| 17 | 17 | $this->headline = $headline; |
| 18 | - } |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * skapar en tabell (sträng) med rubriker från den endimensionella |
|
| 22 | - * arrayen $headline och innehåll från |
|
| 23 | - * den tvådimensionella arrayen $data array |
|
| 24 | - * |
|
| 25 | - * @return string $html, sträng innehållande en tabell |
|
| 26 | - */ |
|
| 20 | + /** |
|
| 21 | + * skapar en tabell (sträng) med rubriker från den endimensionella |
|
| 22 | + * arrayen $headline och innehåll från |
|
| 23 | + * den tvådimensionella arrayen $data array |
|
| 24 | + * |
|
| 25 | + * @return string $html, sträng innehållande en tabell |
|
| 26 | + */ |
|
| 27 | 27 | |
| 28 | - public function getTable(){ |
|
| 28 | + public function getTable(){ |
|
| 29 | 29 | |
| 30 | 30 | $table = "<div class='dbtable'> |
| 31 | 31 | <table class> |
| 32 | 32 | <tr class='rows'>"; |
| 33 | 33 | |
| 34 | - foreach($this->headline as $h) { |
|
| 35 | - $table.= "<th>" . $h ."</th>"; |
|
| 36 | - } |
|
| 34 | + foreach($this->headline as $h) { |
|
| 35 | + $table.= "<th>" . $h ."</th>"; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - $table .= "</tr>"; |
|
| 39 | - foreach($this->data as $d){ |
|
| 40 | - $table .= "<tr>"; |
|
| 41 | - foreach ($d as $v) { |
|
| 42 | - $table .= "<td>$v</td>"; |
|
| 43 | - } |
|
| 44 | - $table .= "</tr>"; |
|
| 45 | - } |
|
| 46 | - $table .="</table></div>"; |
|
| 38 | + $table .= "</tr>"; |
|
| 39 | + foreach($this->data as $d){ |
|
| 40 | + $table .= "<tr>"; |
|
| 41 | + foreach ($d as $v) { |
|
| 42 | + $table .= "<td>$v</td>"; |
|
| 43 | + } |
|
| 44 | + $table .= "</tr>"; |
|
| 45 | + } |
|
| 46 | + $table .="</table></div>"; |
|
| 47 | 47 | |
| 48 | - return $table; |
|
| 49 | - } |
|
| 48 | + return $table; |
|
| 49 | + } |
|
| 50 | 50 | } |
@@ -12,59 +12,59 @@ |
||
| 12 | 12 | |
| 13 | 13 | private $model; |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Lists all instances of the model, (databasetable) except for noListing. |
|
| 17 | - * |
|
| 18 | - * @param $noListing params not to be listed |
|
| 19 | - * @param $model model object to list (create an html table from) |
|
| 20 | - * |
|
| 21 | - * @return void |
|
| 22 | - */ |
|
| 23 | - public function listAction($noListing = NULL, $model) |
|
| 24 | - { |
|
| 15 | + /** |
|
| 16 | + * Lists all instances of the model, (databasetable) except for noListing. |
|
| 17 | + * |
|
| 18 | + * @param $noListing params not to be listed |
|
| 19 | + * @param $model model object to list (create an html table from) |
|
| 20 | + * |
|
| 21 | + * @return void |
|
| 22 | + */ |
|
| 23 | + public function listAction($noListing = NULL, $model) |
|
| 24 | + { |
|
| 25 | 25 | |
| 26 | - $this->model = $model; |
|
| 27 | - $this->model->setDI($this->di); |
|
| 26 | + $this->model = $model; |
|
| 27 | + $this->model->setDI($this->di); |
|
| 28 | 28 | |
| 29 | - $all = $this->model->findAll(); |
|
| 29 | + $all = $this->model->findAll(); |
|
| 30 | 30 | |
| 31 | - //gör om arrayen av objekt till en array av arrayer |
|
| 32 | - foreach ($all as $key1=>$value) { |
|
| 31 | + //gör om arrayen av objekt till en array av arrayer |
|
| 32 | + foreach ($all as $key1=>$value) { |
|
| 33 | 33 | foreach ($value as $key2=>$v){ |
| 34 | - if (!in_array($key2, $noListing)) { //$noListing, parametrar som inte ska vara med |
|
| 35 | - $aContent[$key1][$key2] = $v; |
|
| 36 | - } |
|
| 34 | + if (!in_array($key2, $noListing)) { //$noListing, parametrar som inte ska vara med |
|
| 35 | + $aContent[$key1][$key2] = $v; |
|
| 36 | + } |
|
| 37 | + } |
|
| 37 | 38 | } |
| 38 | - } |
|
| 39 | 39 | |
| 40 | 40 | |
| 41 | - $aHeading = []; |
|
| 42 | - //hittar objektens parametrar/tabellens kolumnnamn och skapar |
|
| 43 | - //en ny array av rubriker |
|
| 44 | - foreach ($all as $key1=>$value) { |
|
| 41 | + $aHeading = []; |
|
| 42 | + //hittar objektens parametrar/tabellens kolumnnamn och skapar |
|
| 43 | + //en ny array av rubriker |
|
| 44 | + foreach ($all as $key1=>$value) { |
|
| 45 | 45 | foreach ($value as $key2=>$v){ |
| 46 | - if (!in_array($key2, $noListing)) { //$noListing, parametrar som inte ska vara med |
|
| 47 | - $aHeading[] = $key2; |
|
| 48 | - } |
|
| 46 | + if (!in_array($key2, $noListing)) { //$noListing, parametrar som inte ska vara med |
|
| 47 | + $aHeading[] = $key2; |
|
| 48 | + } |
|
| 49 | 49 | } |
| 50 | 50 | break; |
| 51 | - } |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - $chtml = new \Jovis\HTMLTable\CHTMLTable($aHeading, $aContent); |
|
| 53 | + $chtml = new \Jovis\HTMLTable\CHTMLTable($aHeading, $aContent); |
|
| 54 | 54 | |
| 55 | - $htmltable = $chtml->getTable(); |
|
| 56 | - $source = $this->model->getSource(); |
|
| 55 | + $htmltable = $chtml->getTable(); |
|
| 56 | + $source = $this->model->getSource(); |
|
| 57 | 57 | |
| 58 | 58 | |
| 59 | - $this->theme->setTitle("Visa alla användare"); |
|
| 60 | - $this->views->add('table/list-all', [ |
|
| 61 | - 'title' => $source, |
|
| 62 | - 'htmltable' => $htmltable, |
|
| 63 | - ]); |
|
| 64 | - } |
|
| 59 | + $this->theme->setTitle("Visa alla användare"); |
|
| 60 | + $this->views->add('table/list-all', [ |
|
| 61 | + 'title' => $source, |
|
| 62 | + 'htmltable' => $htmltable, |
|
| 63 | + ]); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - public function initAction($model){ //används för att befolka databasen, bara för att testa |
|
| 66 | + public function initAction($model){ //används för att befolka databasen, bara för att testa |
|
| 67 | 67 | $model->setDI($this->di); |
| 68 | 68 | $model->init(); |
| 69 | - } |
|
| 69 | + } |
|
| 70 | 70 | } |