Complex classes like ActiveRecordController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ActiveRecordController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 64 | class ActiveRecordController extends Controller implements ControllerInterface |
||
| 65 | { |
||
| 66 | /** |
||
| 67 | * The start number for list pageination. |
||
| 68 | * |
||
| 69 | * @var int |
||
| 70 | * |
||
| 71 | * @since 2.0 |
||
| 72 | */ |
||
| 73 | protected $start = 0; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The amount of records to return during pageination. |
||
| 77 | * |
||
| 78 | * @var int |
||
| 79 | * |
||
| 80 | * @since 2.0 |
||
| 81 | */ |
||
| 82 | protected $limit; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The count of the records of this type in the database (used during pagination). |
||
| 86 | * |
||
| 87 | * @var int |
||
| 88 | * |
||
| 89 | * @since 2.0 |
||
| 90 | */ |
||
| 91 | protected $recordCount = 0; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * The field name to sort the list by (optional, default is OID). |
||
| 95 | * |
||
| 96 | * @var string |
||
| 97 | * |
||
| 98 | * @since 2.0 |
||
| 99 | */ |
||
| 100 | protected $sort; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * The order to sort the list by (optional, should be ASC or DESC, default is ASC). |
||
| 104 | * |
||
| 105 | * @var string |
||
| 106 | * |
||
| 107 | * @since 2.0 |
||
| 108 | */ |
||
| 109 | protected $order; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * The name of the BO field to filter the list by (optional). |
||
| 113 | * |
||
| 114 | * @var string |
||
| 115 | * |
||
| 116 | * @since 2.0 |
||
| 117 | */ |
||
| 118 | protected $filterField; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * The value of the filterField to filter by (optional). |
||
| 122 | * |
||
| 123 | * @var string |
||
| 124 | * |
||
| 125 | * @since 2.0 |
||
| 126 | */ |
||
| 127 | protected $filterValue; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Trace logger. |
||
| 131 | * |
||
| 132 | * @var \Alpha\Util\Logging\Logger |
||
| 133 | * |
||
| 134 | * @since 2.0 |
||
| 135 | */ |
||
| 136 | private static $logger = null; |
||
|
|
|||
| 137 | |||
| 138 | /** |
||
| 139 | * Constructor to set up the object. |
||
| 140 | * |
||
| 141 | * @param string $visibility The name of the rights group that can access this controller. |
||
| 142 | * |
||
| 143 | * @since 1.0 |
||
| 144 | */ |
||
| 145 | public function __construct($visibility = 'Admin') |
||
| 146 | { |
||
| 147 | self::$logger = new Logger('ActiveRecordController'); |
||
| 148 | self::$logger->debug('>>__construct()'); |
||
| 149 | |||
| 150 | // ensure that the super class constructor is called, indicating the rights group |
||
| 151 | parent::__construct($visibility); |
||
| 152 | |||
| 153 | self::$logger->debug('<<__construct'); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Handle GET requests. |
||
| 158 | * |
||
| 159 | * @param \Alpha\Util\Http\Request $request |
||
| 160 | * |
||
| 161 | * @throws \Alpha\Exception\ResourceNotFoundException |
||
| 162 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 163 | * |
||
| 164 | * @return \Alpha\Util\Http\Response |
||
| 165 | * |
||
| 166 | * @since 2.0 |
||
| 167 | */ |
||
| 168 | public function doGET($request) |
||
| 169 | { |
||
| 170 | self::$logger->debug('>>doGET(request=['.var_export($request, true).'])'); |
||
| 171 | |||
| 172 | $params = $request->getParams(); |
||
| 173 | $accept = $request->getAccept(); |
||
| 174 | |||
| 175 | $body = ''; |
||
| 176 | |||
| 177 | try { |
||
| 178 | // get a single record |
||
| 179 | if (isset($params['ActiveRecordType']) && isset($params['ActiveRecordOID'])) { |
||
| 180 | if (!Validator::isInteger($params['ActiveRecordOID'])) { |
||
| 181 | throw new IllegalArguementException('Invalid oid ['.$params['ActiveRecordOID'].'] provided on the request!'); |
||
| 182 | } |
||
| 183 | |||
| 184 | $ActiveRecordType = urldecode($params['ActiveRecordType']); |
||
| 185 | |||
| 186 | if (class_exists($ActiveRecordType)) { |
||
| 187 | $record = new $ActiveRecordType(); |
||
| 188 | } else { |
||
| 189 | throw new IllegalArguementException('No ActiveRecord available to view!'); |
||
| 190 | } |
||
| 191 | |||
| 192 | // set up the title and meta details |
||
| 193 | if (isset($params['view']) && $params['view'] == 'edit') { |
||
| 194 | if (!isset($this->title)) { |
||
| 195 | $this->setTitle('Editing a '.$record->getFriendlyClassName()); |
||
| 196 | } |
||
| 197 | if (!isset($this->description)) { |
||
| 198 | $this->setDescription('Page to edit a '.$record->getFriendlyClassName().'.'); |
||
| 199 | } |
||
| 200 | if (!isset($this->keywords)) { |
||
| 201 | $this->setKeywords('edit,'.$record->getFriendlyClassName()); |
||
| 202 | } |
||
| 203 | } else { |
||
| 204 | if (!isset($this->title)) { |
||
| 205 | $this->setTitle('Viewing a '.$record->getFriendlyClassName()); |
||
| 206 | } |
||
| 207 | if (!isset($this->description)) { |
||
| 208 | $this->setDescription('Page to view a '.$record->getFriendlyClassName().'.'); |
||
| 209 | } |
||
| 210 | if (!isset($this->keywords)) { |
||
| 211 | $this->setKeywords('view,'.$record->getFriendlyClassName()); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | $record->load($params['ActiveRecordOID']); |
||
| 216 | ActiveRecord::disconnect(); |
||
| 217 | |||
| 218 | $view = View::getInstance($record, false, $accept); |
||
| 219 | |||
| 220 | $body .= View::displayPageHead($this); |
||
| 221 | |||
| 222 | $message = $this->getStatusMessage(); |
||
| 223 | if (!empty($message)) { |
||
| 224 | $body .= $message; |
||
| 225 | } |
||
| 226 | |||
| 227 | $body .= View::renderDeleteForm($request->getURI()); |
||
| 228 | |||
| 229 | if (isset($params['view']) && $params['view'] == 'edit') { |
||
| 230 | $fields = array('formAction' => $this->request->getURI()); |
||
| 231 | $body .= $view->editView($fields); |
||
| 232 | } else { |
||
| 233 | $body .= $view->detailedView(); |
||
| 234 | } |
||
| 235 | } elseif (isset($params['ActiveRecordType']) && isset($params['start'])) { |
||
| 236 | // list all records of this type |
||
| 237 | $ActiveRecordType = urldecode($params['ActiveRecordType']); |
||
| 238 | |||
| 239 | if (class_exists($ActiveRecordType)) { |
||
| 240 | $record = new $ActiveRecordType(); |
||
| 241 | } else { |
||
| 242 | throw new IllegalArguementException('No ActiveRecord available to view!'); |
||
| 243 | } |
||
| 244 | |||
| 245 | // set up the title and meta details |
||
| 246 | if (!isset($this->title)) { |
||
| 247 | $this->setTitle('Listing all '.$record->getFriendlyClassName()); |
||
| 248 | } |
||
| 249 | if (!isset($this->description)) { |
||
| 250 | $this->setDescription('Listing all '.$record->getFriendlyClassName()); |
||
| 251 | } |
||
| 252 | if (!isset($this->keywords)) { |
||
| 253 | $this->setKeywords('list,all,'.$record->getFriendlyClassName()); |
||
| 254 | } |
||
| 255 | |||
| 256 | if (isset($this->filterField) && isset($this->filterValue)) { |
||
| 257 | if (isset($this->sort) && isset($this->order)) { |
||
| 258 | $records = $record->loadAllByAttribute($this->filterField, $this->filterValue, $params['start'], $params['limit'], |
||
| 259 | $this->sort, $this->order); |
||
| 260 | } else { |
||
| 261 | $records = $record->loadAllByAttribute($this->filterField, $this->filterValue, $params['start'], $params['limit']); |
||
| 262 | } |
||
| 263 | |||
| 264 | $this->recordCount = $record->getCount(array($this->filterField), array($this->filterValue)); |
||
| 265 | } else { |
||
| 266 | if (isset($this->sort) && isset($this->order)) { |
||
| 267 | $records = $record->loadAll($params['start'], $params['limit'], $this->sort, $this->order); |
||
| 268 | } else { |
||
| 269 | $records = $record->loadAll($params['start'], $params['limit']); |
||
| 270 | } |
||
| 271 | |||
| 272 | $this->recordCount = $record->getCount(); |
||
| 273 | } |
||
| 274 | |||
| 275 | ActiveRecord::disconnect(); |
||
| 276 | |||
| 277 | $view = View::getInstance($record, false, $accept); |
||
| 278 | |||
| 279 | $body .= View::displayPageHead($this); |
||
| 280 | |||
| 281 | $message = $this->getStatusMessage(); |
||
| 282 | if (!empty($message)) { |
||
| 283 | $body .= $message; |
||
| 284 | } |
||
| 285 | |||
| 286 | $body .= View::renderDeleteForm($this->request->getURI()); |
||
| 287 | |||
| 288 | foreach ($records as $record) { |
||
| 289 | $view = View::getInstance($record, false, $accept); |
||
| 290 | $fields = array('formAction' => $this->request->getURI()); |
||
| 291 | $body .= $view->listView($fields); |
||
| 292 | } |
||
| 293 | |||
| 294 | if ($accept == 'application/json') { |
||
| 295 | $body = rtrim($body, ','); |
||
| 296 | } |
||
| 297 | } elseif (isset($params['ActiveRecordType'])) { |
||
| 298 | // create a new record of this type |
||
| 299 | $ActiveRecordType = urldecode($params['ActiveRecordType']); |
||
| 300 | |||
| 301 | if (class_exists($ActiveRecordType)) { |
||
| 302 | $record = new $ActiveRecordType(); |
||
| 303 | } else { |
||
| 304 | throw new IllegalArguementException('No ActiveRecord available to create!'); |
||
| 305 | } |
||
| 306 | |||
| 307 | // set up the title and meta details |
||
| 308 | if (!isset($this->title)) { |
||
| 309 | $this->setTitle('Create a new '.$record->getFriendlyClassName()); |
||
| 310 | } |
||
| 311 | if (!isset($this->description)) { |
||
| 312 | $this->setDescription('Create a new '.$record->getFriendlyClassName().'.'); |
||
| 313 | } |
||
| 314 | if (!isset($this->keywords)) { |
||
| 315 | $this->setKeywords('create,new,'.$record->getFriendlyClassName()); |
||
| 316 | } |
||
| 317 | |||
| 318 | $view = View::getInstance($record, false, $accept); |
||
| 319 | |||
| 320 | $body .= View::displayPageHead($this); |
||
| 321 | $fields = array('formAction' => $this->request->getURI()); |
||
| 322 | $body .= $view->createView($fields); |
||
| 323 | } else { |
||
| 324 | throw new IllegalArguementException('No ActiveRecord available to display!'); |
||
| 325 | } |
||
| 326 | } catch (IllegalArguementException $e) { |
||
| 327 | self::$logger->warn($e->getMessage()); |
||
| 328 | throw new ResourceNotFoundException('The record that you have requested cannot be found!'); |
||
| 329 | } catch (RecordNotFoundException $e) { |
||
| 330 | self::$logger->warn($e->getMessage()); |
||
| 331 | throw new ResourceNotFoundException('The record that you have requested cannot be found!'); |
||
| 332 | } |
||
| 333 | |||
| 334 | $body .= View::displayPageFoot($this); |
||
| 335 | |||
| 336 | self::$logger->debug('<<doGET'); |
||
| 337 | |||
| 338 | return new Response(200, $body, array('Content-Type' => ($accept == 'application/json' ? 'application/json' : 'text/html'))); |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Method to handle POST requests. |
||
| 343 | * |
||
| 344 | * @param \Alpha\Util\Http\Request $request |
||
| 345 | * |
||
| 346 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 347 | * @throws \Alpha\Exception\SecurityException |
||
| 348 | * |
||
| 349 | * @return \Alpha\Util\Http\Response |
||
| 350 | * |
||
| 351 | * @since 2.0 |
||
| 352 | */ |
||
| 353 | public function doPOST($request) |
||
| 354 | { |
||
| 355 | self::$logger->debug('>>doDPOST(request=['.var_export($request, true).'])'); |
||
| 356 | |||
| 357 | $config = ConfigProvider::getInstance(); |
||
| 358 | |||
| 359 | $params = $request->getParams(); |
||
| 360 | $accept = $request->getAccept(); |
||
| 361 | |||
| 362 | try { |
||
| 363 | if (isset($params['ActiveRecordType'])) { |
||
| 364 | $ActiveRecordType = urldecode($params['ActiveRecordType']); |
||
| 365 | } else { |
||
| 366 | throw new IllegalArguementException('No ActiveRecord available to create!'); |
||
| 367 | } |
||
| 368 | |||
| 369 | if (class_exists($ActiveRecordType)) { |
||
| 370 | $record = new $ActiveRecordType(); |
||
| 371 | } else { |
||
| 372 | throw new IllegalArguementException('No ActiveRecord ['.$ActiveRecordType.'] available to create!'); |
||
| 373 | } |
||
| 374 | |||
| 375 | // check the hidden security fields before accepting the form POST data |
||
| 376 | if (!$this->checkSecurityFields()) { |
||
| 377 | throw new SecurityException('This page cannot accept post data from remote servers!'); |
||
| 378 | } |
||
| 379 | |||
| 380 | $record->populateFromArray($params); |
||
| 381 | $record->save(); |
||
| 382 | |||
| 383 | self::$logger->action('Created new '.$ActiveRecordType.' instance with OID '.$record->getOID()); |
||
| 384 | |||
| 385 | if (isset($params['statusMessage'])) { |
||
| 386 | $this->setStatusMessage(View::displayUpdateMessage($params['statusMessage'])); |
||
| 387 | } else { |
||
| 388 | $this->setStatusMessage(View::displayUpdateMessage('Created')); |
||
| 389 | } |
||
| 390 | |||
| 391 | ActiveRecord::disconnect(); |
||
| 392 | } catch (SecurityException $e) { |
||
| 393 | self::$logger->warn($e->getMessage()); |
||
| 394 | throw new ResourceNotAllowedException($e->getMessage()); |
||
| 395 | } catch (IllegalArguementException $e) { |
||
| 396 | self::$logger->warn($e->getMessage()); |
||
| 397 | throw new ResourceNotFoundException('The record that you have requested cannot be found!'); |
||
| 398 | } catch (ValidationException $e) { |
||
| 399 | self::$logger->warn($e->getMessage()); |
||
| 400 | $this->setStatusMessage(View::displayErrorMessage($e->getMessage())); |
||
| 401 | } |
||
| 402 | |||
| 403 | if ($accept == 'application/json') { |
||
| 404 | $view = View::getInstance($record, false, $accept); |
||
| 405 | $body = $view->detailedView(); |
||
| 406 | $response = new Response(201); |
||
| 407 | $response->setHeader('Content-Type', 'application/json'); |
||
| 408 | $response->setHeader('Location', $config->get('app.url').'/record/'.$params['ActiveRecordType'].'/'.$record->getOID()); |
||
| 409 | $response->setBody($body); |
||
| 410 | } else { |
||
| 411 | $response = new Response(301); |
||
| 412 | |||
| 413 | if ($this->getNextJob() != '') { |
||
| 414 | $response->redirect($this->getNextJob()); |
||
| 415 | } else { |
||
| 416 | if ($this->request->isSecureURI()) { |
||
| 417 | $response->redirect(FrontController::generateSecureURL('act=Alpha\\Controller\\ActiveRecordController&ActiveRecordType='.$ActiveRecordType.'&ActiveRecordOID='.$record->getOID())); |
||
| 418 | } else { |
||
| 419 | $response->redirect($config->get('app.url').'/record/'.$params['ActiveRecordType'].'/'.$record->getOID()); |
||
| 420 | } |
||
| 421 | } |
||
| 422 | } |
||
| 423 | |||
| 424 | self::$logger->debug('<<doPOST'); |
||
| 425 | |||
| 426 | return $response; |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Method to handle PUT requests. |
||
| 431 | * |
||
| 432 | * @param \Alpha\Util\Http\Request $request |
||
| 433 | * |
||
| 434 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 435 | * @throws \Alpha\Exception\SecurityException |
||
| 436 | * |
||
| 437 | * @return \Alpha\Util\Http\Response |
||
| 438 | * |
||
| 439 | * @since 2.0 |
||
| 440 | */ |
||
| 441 | public function doPUT($request) |
||
| 442 | { |
||
| 443 | self::$logger->debug('>>doPUT(request=['.var_export($request, true).'])'); |
||
| 444 | |||
| 445 | $config = ConfigProvider::getInstance(); |
||
| 446 | |||
| 447 | $params = $request->getParams(); |
||
| 448 | $accept = $request->getAccept(); |
||
| 449 | |||
| 450 | try { |
||
| 451 | if (isset($params['ActiveRecordType'])) { |
||
| 452 | $ActiveRecordType = urldecode($params['ActiveRecordType']); |
||
| 453 | } else { |
||
| 454 | throw new IllegalArguementException('No ActiveRecord available to edit!'); |
||
| 455 | } |
||
| 456 | |||
| 457 | if (class_exists($ActiveRecordType)) { |
||
| 458 | $record = new $ActiveRecordType(); |
||
| 459 | } else { |
||
| 460 | throw new IllegalArguementException('No ActiveRecord ['.$ActiveRecordType.'] available to edit!'); |
||
| 461 | } |
||
| 462 | |||
| 463 | // check the hidden security fields before accepting the form POST data |
||
| 464 | if (!$this->checkSecurityFields()) { |
||
| 465 | throw new SecurityException('This page cannot accept post data from remote servers!'); |
||
| 466 | } |
||
| 467 | |||
| 468 | $record->load($params['ActiveRecordOID']); |
||
| 469 | $record->populateFromArray($params); |
||
| 470 | $record->save(); |
||
| 471 | |||
| 472 | self::$logger->action('Saved '.$ActiveRecordType.' instance with OID '.$record->getOID()); |
||
| 473 | |||
| 474 | if (isset($params['statusMessage'])) { |
||
| 475 | $this->setStatusMessage(View::displayUpdateMessage($params['statusMessage'])); |
||
| 476 | } else { |
||
| 477 | $this->setStatusMessage(View::displayUpdateMessage('Saved')); |
||
| 478 | } |
||
| 479 | |||
| 480 | ActiveRecord::disconnect(); |
||
| 481 | } catch (SecurityException $e) { |
||
| 482 | self::$logger->warn($e->getMessage()); |
||
| 483 | throw new ResourceNotAllowedException($e->getMessage()); |
||
| 484 | } catch (IllegalArguementException $e) { |
||
| 485 | self::$logger->warn($e->getMessage()); |
||
| 486 | throw new ResourceNotFoundException('The record that you have requested cannot be found!'); |
||
| 487 | } catch (RecordNotFoundException $e) { |
||
| 488 | self::$logger->warn($e->getMessage()); |
||
| 489 | throw new ResourceNotFoundException('The record that you have requested cannot be found!'); |
||
| 490 | } catch (ValidationException $e) { |
||
| 491 | self::$logger->warn($e->getMessage()); |
||
| 492 | $this->setStatusMessage(View::displayErrorMessage($e->getMessage())); |
||
| 493 | } |
||
| 494 | |||
| 495 | if ($accept == 'application/json') { |
||
| 496 | $view = View::getInstance($record, false, $accept); |
||
| 497 | $body = $view->detailedView(); |
||
| 498 | $response = new Response(200); |
||
| 499 | $response->setHeader('Content-Type', 'application/json'); |
||
| 500 | $response->setHeader('Location', $config->get('app.url').'/record/'.$params['ActiveRecordType'].'/'.$record->getOID()); |
||
| 501 | $response->setBody($body); |
||
| 502 | } else { |
||
| 503 | $response = new Response(301); |
||
| 504 | |||
| 505 | if ($this->getNextJob() != '') { |
||
| 506 | $response->redirect($this->getNextJob()); |
||
| 507 | } else { |
||
| 508 | if ($this->request->isSecureURI()) { |
||
| 509 | $response->redirect(FrontController::generateSecureURL('act=Alpha\\Controller\\ActiveRecordController&ActiveRecordType='.urldecode($params['ActiveRecordType']).'&ActiveRecordOID='.$record->getOID().'&view=edit')); |
||
| 510 | } else { |
||
| 511 | $response->redirect($config->get('app.url').'/record/'.$params['ActiveRecordType'].'/'.$record->getOID().'/edit'); |
||
| 512 | } |
||
| 513 | } |
||
| 514 | } |
||
| 515 | |||
| 516 | self::$logger->debug('<<doPUT'); |
||
| 517 | |||
| 518 | return $response; |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Method to handle DELETE requests. |
||
| 523 | * |
||
| 524 | * @param \Alpha\Util\Http\Request $request |
||
| 525 | * |
||
| 526 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 527 | * @throws \Alpha\Exception\SecurityException |
||
| 528 | * @throws \Alpha\Exception\ResourceNotAllowedException |
||
| 529 | * |
||
| 530 | * @return \Alpha\Util\Http\Response |
||
| 531 | * |
||
| 532 | * @since 2.0 |
||
| 533 | */ |
||
| 534 | public function doDELETE($request) |
||
| 535 | { |
||
| 536 | self::$logger->debug('>>doDELETE(request=['.var_export($request, true).'])'); |
||
| 537 | |||
| 538 | $config = ConfigProvider::getInstance(); |
||
| 539 | |||
| 540 | $params = $request->getParams(); |
||
| 541 | $accept = $request->getAccept(); |
||
| 542 | |||
| 543 | try { |
||
| 544 | // check the hidden security fields before accepting the form data |
||
| 545 | if (!$this->checkSecurityFields()) { |
||
| 546 | throw new SecurityException('This page cannot accept data from remote servers!'); |
||
| 547 | } |
||
| 548 | |||
| 549 | if (isset($params['ActiveRecordType'])) { |
||
| 550 | $ActiveRecordType = urldecode($params['ActiveRecordType']); |
||
| 551 | } else { |
||
| 552 | throw new IllegalArguementException('No ActiveRecord available to edit!'); |
||
| 553 | } |
||
| 554 | |||
| 555 | if (class_exists($ActiveRecordType)) { |
||
| 556 | $record = new $ActiveRecordType(); |
||
| 557 | } else { |
||
| 558 | throw new IllegalArguementException('No ActiveRecord ['.$ActiveRecordType.'] available to edit!'); |
||
| 559 | } |
||
| 560 | |||
| 561 | // check the hidden security fields before accepting the form POST data |
||
| 562 | if (!$this->checkSecurityFields()) { |
||
| 563 | throw new SecurityException('This page cannot accept post data from remote servers!'); |
||
| 564 | } |
||
| 565 | |||
| 566 | $record->load($params['ActiveRecordOID']); |
||
| 567 | |||
| 568 | ActiveRecord::begin(); |
||
| 569 | $record->delete(); |
||
| 570 | ActiveRecord::commit(); |
||
| 571 | ActiveRecord::disconnect(); |
||
| 572 | |||
| 573 | self::$logger->action('Deleted '.$ActiveRecordType.' instance with OID '.$params['ActiveRecordOID']); |
||
| 574 | |||
| 575 | if ($accept == 'application/json') { |
||
| 576 | $response = new Response(200); |
||
| 577 | $response->setHeader('Content-Type', 'application/json'); |
||
| 578 | $response->setBody(json_encode(array('message' => 'deleted'))); |
||
| 579 | } else { |
||
| 580 | $response = new Response(301); |
||
| 581 | |||
| 582 | if (isset($params['statusMessage'])) { |
||
| 583 | $this->setStatusMessage(View::displayUpdateMessage($params['statusMessage'])); |
||
| 584 | } else { |
||
| 585 | $this->setStatusMessage(View::displayUpdateMessage('Deleted')); |
||
| 586 | } |
||
| 587 | |||
| 588 | if ($this->getNextJob() != '') { |
||
| 589 | $response->redirect($this->getNextJob()); |
||
| 590 | } else { |
||
| 591 | if ($this->request->isSecureURI()) { |
||
| 592 | $response->redirect(FrontController::generateSecureURL('act=Alpha\\Controller\\ActiveRecordController&ActiveRecordType='.$ActiveRecordType.'&start=0&limit='.$config->get('app.list.page.amount'))); |
||
| 593 | } else { |
||
| 594 | $response->redirect($config->get('app.url').'/records/'.$params['ActiveRecordType']); |
||
| 595 | } |
||
| 596 | } |
||
| 597 | } |
||
| 598 | |||
| 599 | self::$logger->debug('<<doDELETE'); |
||
| 600 | |||
| 601 | return $response; |
||
| 602 | } catch (SecurityException $e) { |
||
| 603 | self::$logger->warn($e->getMessage()); |
||
| 604 | throw new ResourceNotAllowedException($e->getMessage()); |
||
| 605 | } catch (RecordNotFoundException $e) { |
||
| 606 | self::$logger->warn($e->getMessage()); |
||
| 607 | throw new ResourceNotFoundException('The item that you have requested cannot be found!'); |
||
| 608 | } catch (AlphaException $e) { |
||
| 609 | self::$logger->error($e->getMessage()); |
||
| 610 | ActiveRecord::rollback(); |
||
| 611 | throw new ResourceNotAllowedException($e->getMessage()); |
||
| 612 | } |
||
| 613 | } |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Sets up the pagination start point and limit. |
||
| 617 | * |
||
| 618 | * @since 2.0 |
||
| 619 | */ |
||
| 620 | public function after_displayPageHead_callback() |
||
| 621 | { |
||
| 622 | $body = parent::after_displayPageHead_callback(); |
||
| 623 | |||
| 624 | // set the start point for the list pagination |
||
| 625 | if ($this->request->getParam('start') != null) { |
||
| 626 | $this->start = $this->request->getParam('start'); |
||
| 627 | |||
| 628 | $viewState = ViewState::getInstance(); |
||
| 629 | $viewState->set('selectedStart', $this->start); |
||
| 630 | |||
| 631 | if ($this->request->getParam('limit') != null) { |
||
| 632 | $this->limit = $this->request->getParam('limit'); |
||
| 633 | } else { |
||
| 634 | $config = ConfigProvider::getInstance(); |
||
| 635 | $this->limit = $config->get('app.list.page.amount'); |
||
| 636 | } |
||
| 637 | |||
| 638 | $accept = $this->request->getAccept(); |
||
| 639 | |||
| 640 | if ($accept == 'application/json') { |
||
| 641 | $body .= '['; |
||
| 642 | } |
||
| 643 | } |
||
| 644 | |||
| 645 | return $body; |
||
| 646 | } |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Method to display the page footer with pageination links. |
||
| 650 | * |
||
| 651 | * @return string |
||
| 652 | * |
||
| 653 | * @since 2.0 |
||
| 654 | */ |
||
| 655 | public function before_displayPageFoot_callback() |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Method for rendering the pagination links. |
||
| 675 | * |
||
| 676 | * @return string |
||
| 677 | * |
||
| 678 | * @since 2.0 |
||
| 679 | */ |
||
| 680 | protected function renderPageLinks() |
||
| 752 | } |
||
| 753 |