Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 15 | class Reports extends Admin |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * For admin listing and search results |
||
| 19 | */ |
||
| 20 | use Traits\SearchController; |
||
| 21 | |||
| 22 | protected $template_path = 'cms/admin/reports/'; |
||
| 23 | |||
| 24 | |||
| 25 | /** |
||
| 26 | * |
||
| 27 | * |
||
| 28 | * @param \Base $f3 |
||
| 29 | * @return void |
||
| 30 | */ |
||
| 31 | View Code Duplication | public function listing(\Base $f3) |
|
| 32 | { |
||
| 33 | $view = strtolower(trim(strip_tags($f3->get('REQUEST.view')))); |
||
| 34 | $view = empty($view) ? 'list.phtml' : $view . '.phtml'; |
||
| 35 | $f3->set('REQUEST.view', $view); |
||
| 36 | |||
| 37 | $f3->set('results', $this->getListingResults($f3, new Mappers\Reports)); |
||
| 38 | |||
| 39 | $f3->set('breadcrumbs', [ |
||
| 40 | _('Admin') => 'admin', |
||
| 41 | _('Reports') => 'admin_reports_list', |
||
| 42 | ]); |
||
| 43 | |||
| 44 | $f3->set('form', $f3->get('REQUEST')); |
||
| 45 | echo \View::instance()->render($this->template_path . $view); |
||
| 46 | } |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * |
||
| 51 | * |
||
| 52 | * @param \Base $f3 |
||
| 53 | * @return void |
||
| 54 | */ |
||
| 55 | View Code Duplication | public function search(\Base $f3) |
|
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * |
||
| 76 | * |
||
| 77 | * @param \Base $f3 |
||
| 78 | * @return void |
||
| 79 | */ |
||
| 80 | public function edit(\Base $f3) |
||
| 81 | { |
||
| 82 | $this->redirectLoggedOutUser(); |
||
| 83 | $this->csrf(); |
||
| 84 | |||
| 85 | if (false == $f3->get('isRoot')) { |
||
| 86 | $this->notify(_('You do not have (root) permission!'), 'error'); |
||
| 87 | return $f3->reroute('@admin'); |
||
| 88 | } |
||
| 89 | |||
| 90 | $uuid = $f3->get('REQUEST.uuid'); |
||
| 91 | |||
| 92 | $mapper = new Mappers\Reports; |
||
| 93 | $mapper->load(['uuid = ?', $uuid]); |
||
| 94 | |||
| 95 | if (null == $mapper->id) { |
||
| 96 | $this->notify(_('The entry no longer exists!'), 'error'); |
||
| 97 | return $f3->reroute('@admin_reports_lists'); |
||
| 98 | } |
||
| 99 | |||
| 100 | $f3->set('breadcrumbs', [ |
||
| 101 | _('Admin') => 'admin', |
||
| 102 | _('Users') => $this->url('@admin_reports_search', [ |
||
| 103 | 'search' => $mapper->users_uuid, |
||
| 104 | 'search_fields' => 'uuid', |
||
| 105 | 'type' => 'exact', |
||
| 106 | ]), |
||
| 107 | _('Reports') => $this->url('@admin_reports_search', [ |
||
| 108 | 'search' => $mapper->users_uuid, |
||
| 109 | 'search_fields' => 'users_uuid', |
||
| 110 | 'order' => 'key', |
||
| 111 | 'type' => 'exact', |
||
| 112 | ]), |
||
| 113 | _('Edit') => '', |
||
| 114 | ]); |
||
| 115 | |||
| 116 | $f3->set('form', $mapper->cast()); |
||
| 117 | echo \View::instance()->render($this->template_path . 'edit.phtml'); |
||
| 118 | } |
||
| 119 | |||
| 120 | |||
| 121 | /** |
||
| 122 | * |
||
| 123 | * |
||
| 124 | * @param \Base $f3 |
||
| 125 | * @return void |
||
| 126 | */ |
||
| 127 | public function editPost(\Base $f3) |
||
| 128 | { |
||
| 129 | $this->csrf('@admin_reports_list'); |
||
| 130 | $this->redirectLoggedOutUser(); |
||
| 131 | |||
| 132 | if (false == $f3->get('isRoot')) { |
||
| 133 | $this->notify(_('You do not have (root) permission!'), 'error'); |
||
| 134 | return $f3->reroute('@admin'); |
||
| 135 | } |
||
| 136 | |||
| 137 | $view = $this->template_path . 'edit.phtml'; |
||
| 138 | |||
| 139 | // get current user details |
||
| 140 | $uuid = $f3->get('REQUEST.uuid'); |
||
| 141 | |||
| 142 | $mapper = new Mappers\Reports; |
||
| 143 | $mapper->load(['uuid = ?', $uuid]); |
||
| 144 | |||
| 145 | if (null == $mapper->id) { |
||
| 146 | $this->notify(_('The entry no longer exists!'), 'error'); |
||
| 147 | return $f3->reroute('@admin_reports_list'); |
||
| 148 | } |
||
| 149 | |||
| 150 | $f3->set('breadcrumbs', [ |
||
| 151 | _('Admin') => 'admin', |
||
| 152 | _('Users') => $this->url('@admin_reports_search', [ |
||
| 153 | 'search' => $mapper->users_uuid, |
||
| 154 | 'search_fields' => 'uuid', |
||
| 155 | 'type' => 'exact', |
||
| 156 | ]), |
||
| 157 | _('Reports') => $this->url('@admin_reports_search', [ |
||
| 158 | 'search' => $mapper->users_uuid, |
||
| 159 | 'search_fields' => 'users_uuid', |
||
| 160 | 'order' => 'key', |
||
| 161 | 'type' => 'exact', |
||
| 162 | ]), |
||
| 163 | _('Edit') => '', |
||
| 164 | ]); |
||
| 165 | |||
| 166 | // only allow updating of these fields |
||
| 167 | $data = $f3->get('REQUEST'); |
||
| 168 | $fields = [ |
||
| 169 | 'users_uuid', |
||
| 170 | 'scopes', |
||
| 171 | 'key', |
||
| 172 | 'name', |
||
| 173 | 'description', |
||
| 174 | 'query', |
||
| 175 | ]; |
||
| 176 | |||
| 177 | // check input data has values set for the above fields |
||
| 178 | foreach ($fields as $k => $field) { |
||
| 179 | if (!array_key_exists($field, $data)) { |
||
| 180 | $data[$field] = null; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | // then remove any input data fields that aren't in the above fields |
||
| 184 | foreach ($data as $field => $v) { |
||
| 185 | if (!in_array($field, $fields)) { |
||
| 186 | unset($data[$field]); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | // update required fields to check from ones which changed |
||
| 191 | // validate the entered data |
||
| 192 | $data['uuid'] = $f3->get('REQUEST.uuid'); |
||
| 193 | $data['users_uuid'] = $f3->get('uuid'); |
||
| 194 | $mapper->copyfrom($data); |
||
| 195 | $mapper->validationRequired($fields); |
||
| 196 | $errors = $mapper->validate(false); |
||
| 197 | View Code Duplication | if (is_array($errors)) { |
|
| 198 | $this->notify(['warning' => $mapper->validationErrors($errors)]); |
||
| 199 | $f3->set('form', $f3->get('REQUEST')); |
||
| 200 | echo \View::instance()->render($view); |
||
| 201 | return; |
||
| 202 | } |
||
| 203 | |||
| 204 | // no change, do nothing |
||
| 205 | if (!$mapper->changed()) { |
||
| 206 | $this->notify(_('There was nothing to change!'), 'info'); |
||
| 207 | return $f3->reroute('@admin_reports_list'); |
||
| 208 | } |
||
| 209 | |||
| 210 | // reset usermapper and copy in valid data |
||
| 211 | $mapper->load(['uuid = ?', $data['uuid']]); |
||
| 212 | $mapper->copyfrom($data); |
||
| 213 | View Code Duplication | if ($mapper->save()) { |
|
| 214 | $this->notify(_('The report data was updated!'), 'success'); |
||
| 215 | } else { |
||
| 216 | $this->notify(_('Unable to update report data!'), 'error'); |
||
| 217 | $f3->set('form', $f3->get('REQUEST')); |
||
| 218 | echo \View::instance()->render($view); |
||
| 219 | return; |
||
| 220 | } |
||
| 221 | |||
| 222 | $f3->reroute('@admin_reports_search' . '?search=' . $mapper->uuid); |
||
| 223 | } |
||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * |
||
| 228 | * |
||
| 229 | * @param \Base $f3 |
||
| 230 | * @return void |
||
| 231 | */ |
||
| 232 | public function add(\Base $f3) |
||
| 263 | |||
| 264 | |||
| 265 | /** |
||
| 266 | * |
||
| 267 | * |
||
| 268 | * @param \Base $f3 |
||
| 269 | * @return void |
||
| 270 | */ |
||
| 271 | public function addPost(\Base $f3) |
||
| 272 | { |
||
| 273 | $this->csrf('@admin_reports_list'); |
||
| 274 | $this->redirectLoggedOutUser(); |
||
| 275 | |||
| 276 | if (false == $f3->get('isRoot')) { |
||
| 277 | $this->notify(_('You do not have (root) permission!'), 'error'); |
||
| 278 | return $f3->reroute('@admin'); |
||
| 279 | } |
||
| 280 | |||
| 281 | $view = $this->template_path . 'add.phtml'; |
||
| 282 | |||
| 283 | $uuid = $f3->get('REQUEST.uuid'); |
||
| 284 | |||
| 285 | $mapper = new Mappers\Reports; |
||
| 286 | |||
| 287 | $f3->set('breadcrumbs', [ |
||
| 288 | _('Admin') => 'admin', |
||
| 289 | _('Users') => $this->url('@admin_reports_search', [ |
||
| 290 | 'search' => $uuid, |
||
| 291 | 'search_fields' => 'uuid', |
||
| 292 | 'type' => 'exact', |
||
| 293 | ]), |
||
| 294 | _('Reports') => $this->url('@admin_reports_search', [ |
||
| 295 | 'search' => $uuid, |
||
| 296 | 'search_fields' => 'users_uuid', |
||
| 297 | 'order' => 'key', |
||
| 298 | 'type' => 'exact', |
||
| 299 | ]), |
||
| 300 | _('Add') => '', |
||
| 301 | ]); |
||
| 302 | |||
| 303 | // only allow updating of these fields |
||
| 304 | $data = $f3->get('REQUEST'); |
||
| 305 | $fields = [ |
||
| 306 | 'scopes', |
||
| 307 | 'key', |
||
| 308 | 'name', |
||
| 309 | 'description', |
||
| 310 | 'query', |
||
| 311 | ]; |
||
| 312 | |||
| 313 | // check input data has values set for the above fields |
||
| 314 | foreach ($fields as $k => $field) { |
||
| 315 | if (!array_key_exists($field, $data) || empty($data[$field])) { |
||
| 316 | $data[$field] = null; |
||
| 317 | } |
||
| 318 | } |
||
| 319 | // then remove any input data fields that aren't in the above fields |
||
| 320 | foreach ($data as $field => $v) { |
||
| 321 | if (!in_array($field, $fields)) { |
||
| 322 | unset($data[$field]); |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | // update required fields to check from ones which changed |
||
| 327 | // validate the entered data |
||
| 328 | $data['users_uuid'] = $f3->get('uuid'); |
||
| 329 | $mapper->copyfrom($data); |
||
| 330 | $mapper->validationRequired($fields); |
||
| 331 | $errors = $mapper->validate(false); |
||
| 332 | View Code Duplication | if (is_array($errors)) { |
|
| 333 | $this->notify(['warning' => $mapper->validationErrors($errors)]); |
||
| 334 | $f3->set('form', $f3->get('REQUEST')); |
||
| 335 | echo \View::instance()->render($view); |
||
| 336 | return; |
||
| 337 | } |
||
| 338 | |||
| 339 | // no change, do nothing |
||
| 340 | if (!$mapper->changed()) { |
||
| 341 | $this->notify(_('There was nothing to change!'), 'info'); |
||
| 342 | return $f3->reroute('@admin_reports_list'); |
||
| 343 | } |
||
| 344 | |||
| 345 | // reset usermapper and copy in valid data |
||
| 346 | $mapper->load(['uuid = ?', $mapper->uuid]); |
||
| 347 | $mapper->copyfrom($data); |
||
| 348 | View Code Duplication | if ($mapper->save()) { |
|
| 349 | $this->notify(_('The report data was updated!'), 'success'); |
||
| 350 | } else { |
||
| 351 | $this->notify(_('Unable to update report data!'), 'error'); |
||
| 352 | $f3->set('form', $f3->get('REQUEST')); |
||
| 353 | echo \View::instance()->render($view); |
||
| 354 | return; |
||
| 355 | } |
||
| 356 | |||
| 357 | $f3->reroute('@admin_reports_search' . '?search=' . $mapper->uuid); |
||
| 358 | } |
||
| 359 | |||
| 360 | |||
| 361 | /** |
||
| 362 | * |
||
| 363 | * |
||
| 364 | * @param \Base $f3 |
||
| 365 | * @return void |
||
| 366 | */ |
||
| 367 | public function view(\Base $f3) |
||
| 430 | |||
| 431 | |||
| 432 | /** |
||
| 433 | * |
||
| 434 | * |
||
| 435 | * @param \Base $f3 |
||
| 436 | * @return void |
||
| 437 | */ |
||
| 438 | View Code Duplication | public function delete(\Base $f3) |
|
| 463 | |||
| 464 | |||
| 465 | } |
||
| 466 |