Passed
Push — main ( a6f261...9b9a2d )
by Sammy
01:33
created
Controllers/ORMController.class.php 1 patch
Spacing   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -27,19 +27,19 @@  discard block
 block discarded – undo
27 27
     $custom_template = null;
28 28
     $method = $this->router()->target_method();
29 29
 
30
-    foreach(['prepare', "before_$method", $method, "after_$method"] as $step => $chainling)
30
+    foreach (['prepare', "before_$method", $method, "after_$method"] as $step => $chainling)
31 31
     {
32 32
       $this->search_and_execute_trait_methods($chainling);
33
-    	if(method_exists($this, $chainling) && empty($this->errors()))
33
+    	if (method_exists($this, $chainling) && empty($this->errors()))
34 34
       {
35 35
         $res = $this->$chainling();
36 36
 
37
-        if($this->logger()->has_halting_messages()) // logger handled a critical error during the chailing execution
37
+        if ($this->logger()->has_halting_messages()) // logger handled a critical error during the chailing execution
38 38
         {
39 39
           break; // dont go on with other
40 40
         }
41 41
 
42
-        if($chainling === $method)
42
+        if ($chainling === $method)
43 43
         {
44 44
 
45 45
           $custom_template = $res;
@@ -47,10 +47,10 @@  discard block
 block discarded – undo
47 47
       }
48 48
     }
49 49
 
50
-    if(method_exists($this, 'conclude')) // conclude always executed, even with has_halting_messages
50
+    if (method_exists($this, 'conclude')) // conclude always executed, even with has_halting_messages
51 51
     	$this->conclude();
52 52
 
53
-    if(method_exists($this, 'display'))
53
+    if (method_exists($this, 'display'))
54 54
     	$template = $this->display($custom_template);
55 55
   }
56 56
 
@@ -58,7 +58,7 @@  discard block
 block discarded – undo
58 58
   {
59 59
     parent::prepare();
60 60
 
61
-    if(!class_exists($this->model_class_name = $this->class_name()))
61
+    if (!class_exists($this->model_class_name = $this->class_name()))
62 62
       throw new \Exception("!class_exists($this->model_class_name)");
63 63
 
64 64
     $this->model_type = $this->model_class_name::model_type();
@@ -68,22 +68,22 @@  discard block
 block discarded – undo
68 68
 
69 69
     $pk_values = [];
70 70
 
71
-    if($this->router()->submits())
71
+    if ($this->router()->submits())
72 72
     {
73 73
       $this->form_model->import($this->sanitize_post_data($this->router()->submitted()));
74 74
       $pk_values = $this->model_class_name::table()->primary_keys_match($this->router()->submitted());
75 75
 
76 76
       $this->load_model = $this->model_class_name::exists($pk_values);
77 77
     }
78
-    elseif($this->router()->requests())
78
+    elseif ($this->router()->requests())
79 79
     {
80 80
       $pk_values = $this->model_class_name::table()->primary_keys_match($this->router()->params());
81 81
 
82
-      if(!is_null($this->load_model = $this->model_class_name::exists($pk_values)))
82
+      if (!is_null($this->load_model = $this->model_class_name::exists($pk_values)))
83 83
         $this->form_model = clone $this->load_model;
84 84
     }
85 85
 
86
-    if(!is_null($this->load_model) && is_subclass_of($this->load_model, '\HexMakina\Crudites\Interfaces\TraceableInterface') && $this->load_model->traceable())
86
+    if (!is_null($this->load_model) && is_subclass_of($this->load_model, '\HexMakina\Crudites\Interfaces\TraceableInterface') && $this->load_model->traceable())
87 87
     {
88 88
       // $traces = $this->tracer()->traces_by_model($this->load_model);
89 89
       $traces = $this->tracer()->history_by_model($this->load_model);
@@ -99,7 +99,7 @@  discard block
 block discarded – undo
99 99
   // ----------- META -----------
100 100
   public function class_name() : string
101 101
   {
102
-    if(is_null($this->model_class_name))
102
+    if (is_null($this->model_class_name))
103 103
     {
104 104
       $this->model_class_name = get_called_class();
105 105
       $this->model_class_name = str_replace('\Controllers\\', '\Models\\', $this->model_class_name);
@@ -112,12 +112,12 @@  discard block
 block discarded – undo
112 112
   public function persist_model($model) : ?ModelInterface
113 113
   {
114 114
     $this->errors = $model->save($this->operator()->operator_id(), $this->tracer()); // returns [errors]
115
-    if(empty($this->errors()))
115
+    if (empty($this->errors()))
116 116
     {
117 117
       $this->logger()->nice(L('CRUDITES_INSTANCE_ALTERED', [L('MODEL_'.get_class($model)::model_type().'_INSTANCE')]));
118 118
       return $model;
119 119
     }
120
-    foreach($this->errors() as $field => $error_msg)
120
+    foreach ($this->errors() as $field => $error_msg)
121 121
       $this->logger()->warning(L($error_msg, [$field]));
122 122
 
123 123
     return null;
@@ -128,12 +128,12 @@  discard block
 block discarded – undo
128 128
     return $this->listing(); //default dashboard is a listing
129 129
   }
130 130
 
131
-  public function listing($model=null,$filters=[],$options=[])
131
+  public function listing($model = null, $filters = [], $options = [])
132 132
   {
133 133
     $class_name = is_null($model) ? $this->model_class_name : get_class($model);
134 134
 
135
-    if(!isset($filters['date_start']))  $filters['date_start'] = $this->box('StateAgent')->filters('date_start');
136
-    if(!isset($filters['date_stop']))   $filters['date_stop'] = $this->box('StateAgent')->filters('date_stop');
135
+    if (!isset($filters['date_start']))  $filters['date_start'] = $this->box('StateAgent')->filters('date_start');
136
+    if (!isset($filters['date_stop']))   $filters['date_stop'] = $this->box('StateAgent')->filters('date_stop');
137 137
 
138 138
     // dd($filters);
139 139
     $listing = $class_name::filter($filters);
@@ -144,24 +144,24 @@  discard block
 block discarded – undo
144 144
   public function viewport_listing($class_name, $listing, $listing_template)
145 145
   {
146 146
     $listing_fields = [];
147
-    if(empty($listing))
147
+    if (empty($listing))
148 148
     {
149
-      foreach($class_name::table()->columns() as $column)
149
+      foreach ($class_name::table()->columns() as $column)
150 150
       {
151 151
 
152
-        if(!$column->is_auto_incremented() && !$column->is_hidden())
153
-          $listing_fields[$column->name()]=L(sprintf('MODEL_%s_FIELD_%s', $class_name::model_type(), $column->name()));
152
+        if (!$column->is_auto_incremented() && !$column->is_hidden())
153
+          $listing_fields[$column->name()] = L(sprintf('MODEL_%s_FIELD_%s', $class_name::model_type(), $column->name()));
154 154
       }
155 155
     }
156 156
     else
157 157
     {
158 158
       $current = current($listing);
159
-      if(is_object($current))
159
+      if (is_object($current))
160 160
         $current = get_object_vars($current);
161 161
 
162
-      foreach(array_keys($current) as $field)
162
+      foreach (array_keys($current) as $field)
163 163
       {
164
-        $listing_fields[$field]=L(sprintf('MODEL_%s_FIELD_%s', $class_name::model_type(), $field));
164
+        $listing_fields[$field] = L(sprintf('MODEL_%s_FIELD_%s', $class_name::model_type(), $field));
165 165
       }
166 166
 
167 167
     }
@@ -183,13 +183,13 @@  discard block
 block discarded – undo
183 183
     $this->edit();
184 184
   }
185 185
 
186
-  public function edit(){}
186
+  public function edit() {}
187 187
 
188 188
   public function save()
189 189
   {
190 190
     $model = $this->persist_model($this->form_model);
191 191
 
192
-    if(empty($this->errors()))
192
+    if (empty($this->errors()))
193 193
       $this->route_back($model);
194 194
     else
195 195
     {
@@ -200,21 +200,21 @@  discard block
 block discarded – undo
200 200
 
201 201
   public function before_edit()
202 202
   {
203
-    if(!is_null($this->router()->params('id')) && is_null($this->load_model))
203
+    if (!is_null($this->router()->params('id')) && is_null($this->load_model))
204 204
     {
205 205
       $this->logger()->warning(L('CRUDITES_ERR_INSTANCE_NOT_FOUND', [L('MODEL_'.$this->model_class_name::model_type().'_INSTANCE')]));
206 206
       $this->router()->hop($this->model_class_name::model_type());
207 207
     }
208 208
   }
209 209
 
210
-  public function before_save() : array { return [];}
210
+  public function before_save() : array { return []; }
211 211
 
212 212
   // default: hop to altered object
213
-  public function after_save() {$this->router()->hop($this->route_back());}
213
+  public function after_save() {$this->router()->hop($this->route_back()); }
214 214
 
215 215
   public function destroy_confirm()
216 216
   {
217
-    if(is_null($this->load_model))
217
+    if (is_null($this->load_model))
218 218
     {
219 219
       $this->logger()->warning(L('CRUDITES_ERR_INSTANCE_NOT_FOUND', [L('MODEL_'.$this->model_type.'_INSTANCE')]));
220 220
       $this->router()->hop($this->model_type);
@@ -227,12 +227,12 @@  discard block
 block discarded – undo
227 227
 
228 228
   public function before_destroy() // default: checks for load_model and immortality, hops back to object on failure
229 229
   {
230
-    if(is_null($this->load_model))
230
+    if (is_null($this->load_model))
231 231
     {
232 232
       $this->logger()->warning(L('CRUDITES_ERR_INSTANCE_NOT_FOUND', [L('MODEL_'.$this->model_type.'_INSTANCE')]));
233 233
       $this->router()->hop($this->model_type);
234 234
     }
235
-    elseif($this->load_model->immortal())
235
+    elseif ($this->load_model->immortal())
236 236
     {
237 237
 
238 238
       $this->logger()->warning(L('CRUDITES_ERR_INSTANCE_IS_IMMORTAL', [L('MODEL_'.$this->model_type.'_INSTANCE')]));
@@ -242,10 +242,10 @@  discard block
 block discarded – undo
242 242
 
243 243
   public function destroy()
244 244
   {
245
-    if(!$this->router()->submits())
245
+    if (!$this->router()->submits())
246 246
       throw new \Exception('KADRO_ROUTER_MUST_SUBMIT');
247 247
 
248
-    if($this->load_model->destroy($this->operator()->operator_id(), $this->tracer()) === false)
248
+    if ($this->load_model->destroy($this->operator()->operator_id(), $this->tracer()) === false)
249 249
     {
250 250
       $this->logger()->info(L('CRUDITES_ERR_INSTANCE_IS_UNDELETABLE', [''.$this->load_model]));
251 251
       $this->route_back($this->load_model);
@@ -268,10 +268,10 @@  discard block
 block discarded – undo
268 268
 
269 269
     $this->viewport('form_model_type', $this->model_type);
270 270
 
271
-    if(isset($this->load_model))
271
+    if (isset($this->load_model))
272 272
       $this->viewport('load_model', $this->load_model);
273 273
 
274
-    if(isset($this->form_model))
274
+    if (isset($this->form_model))
275 275
       $this->viewport('form_model', $this->form_model);
276 276
   }
277 277
 
@@ -283,15 +283,15 @@  discard block
 block discarded – undo
283 283
 
284 284
     $header = false;
285 285
 
286
-    foreach($collection as $line)
286
+    foreach ($collection as $line)
287 287
     {
288 288
       $line = get_object_vars($line);
289
-      if($header === false)
289
+      if ($header === false)
290 290
       {
291
-        fputcsv($fp,array_keys($line));
291
+        fputcsv($fp, array_keys($line));
292 292
         $header = true;
293 293
       }
294
-      fputcsv($fp,$line);
294
+      fputcsv($fp, $line);
295 295
     }
296 296
     fclose($fp);
297 297
 
@@ -301,7 +301,7 @@  discard block
 block discarded – undo
301 301
   public function export()
302 302
   {
303 303
     $format = $this->router()->params('format');
304
-    switch($format)
304
+    switch ($format)
305 305
     {
306 306
       case null:
307 307
         $filename = $this->model_type;
@@ -330,33 +330,33 @@  discard block
 block discarded – undo
330 330
     $route_params = [];
331 331
 
332 332
     $route_name = get_class($model)::model_type().'_';
333
-    if($model->is_new())
334
-      $route_name.= 'new';
333
+    if ($model->is_new())
334
+      $route_name .= 'new';
335 335
     else
336 336
     {
337
-      $route_name.= 'default';
337
+      $route_name .= 'default';
338 338
       $route_params = ['id' => $model->get_id()];
339 339
     }
340 340
     $res = $this->router()->prehop($route_name, $route_params);
341 341
     return $res;
342 342
   }
343 343
 
344
-  public function route_factory($route=null, $route_params=[]) : string
344
+  public function route_factory($route = null, $route_params = []) : string
345 345
   {
346
-    if(is_null($route) && $this->router()->submits())
346
+    if (is_null($route) && $this->router()->submits())
347 347
       $route = $this->form_model;
348 348
 
349
-    if(!is_null($route) && is_subclass_of($route, '\HexMakina\Crudites\Interfaces\ModelInterface'))
349
+    if (!is_null($route) && is_subclass_of($route, '\HexMakina\Crudites\Interfaces\ModelInterface'))
350 350
       $route = $this->route_model($route);
351 351
 
352 352
     return parent::route_factory($route, $route_params);
353 353
   }
354 354
 
355
-  private function sanitize_post_data($post_data=[])
355
+  private function sanitize_post_data($post_data = [])
356 356
   {
357
-    foreach($this->model_class_name::table()->columns() as $col)
357
+    foreach ($this->model_class_name::table()->columns() as $col)
358 358
     {
359
-      if($col->type()->is_boolean())
359
+      if ($col->type()->is_boolean())
360 360
       {
361 361
           $post_data[$col->name()] = !empty($post_data[$col->name()]);
362 362
       }
Please login to merge, or discard this patch.