| Total Complexity | 59 |
| Total Lines | 420 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ResourcesPage 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.
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 ResourcesPage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | abstract class ResourcesPage extends AdministrationPage |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * This method is invoked from the `Sortable` class and it contains the |
||
| 19 | * logic for sorting (or unsorting) the resource index. It provides a basic |
||
| 20 | * wrapper to the `ResourceManager`'s `fetch()` method. |
||
| 21 | * |
||
| 22 | * @see toolkit.ResourceManager#getSortingField |
||
| 23 | * @see toolkit.ResourceManager#getSortingOrder |
||
| 24 | * @see toolkit.ResourceManager#fetch |
||
| 25 | * @param string $sort |
||
| 26 | * The field to sort on which should match one of the table's column names. |
||
| 27 | * If this is not provided the default will be determined by |
||
| 28 | * `ResourceManager::getSortingField` |
||
| 29 | * @param string $order |
||
| 30 | * The direction to sort in, either 'asc' or 'desc'. If this is not provided |
||
| 31 | * the value will be determined by `ResourceManager::getSortingOrder`. |
||
| 32 | * @param array $params |
||
| 33 | * An associative array of params (usually populated from the URL) that this |
||
| 34 | * function uses. The current implementation will use `type` and `unsort` keys |
||
| 35 | * @throws Exception |
||
| 36 | * @throws SymphonyErrorPage |
||
| 37 | * @return array |
||
| 38 | * An associative of the resource as determined by `ResourceManager::fetch` |
||
| 39 | */ |
||
| 40 | public function sort(&$sort, &$order, array $params) |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * This function creates an array of all page titles in the system. |
||
| 77 | * |
||
| 78 | * @return array |
||
| 79 | * An array of page titles |
||
| 80 | */ |
||
| 81 | public function pagesFlatView() |
||
| 82 | { |
||
| 83 | $pages = PageManager::fetch(false, array('id')); |
||
| 84 | |||
| 85 | foreach ($pages as &$p) { |
||
| 86 | $p['title'] = PageManager::resolvePageTitle($p['id']); |
||
| 87 | } |
||
| 88 | |||
| 89 | return $pages; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * This function contains the minimal amount of logic for generating the |
||
| 94 | * index table of a given `$resource_type`. The table has name, source, pages |
||
| 95 | * release date and author columns. The values for these columns are determined |
||
| 96 | * by the resource's `about()` method. |
||
| 97 | * |
||
| 98 | * As Datasources types can be installed using Providers, the Source column |
||
| 99 | * can be overridden with a Datasource's `getSourceColumn` method (if it exists). |
||
| 100 | * |
||
| 101 | * @param integer $resource_type |
||
| 102 | * Either `ResourceManager::RESOURCE_TYPE_EVENT` or `ResourceManager::RESOURCE_TYPE_DATASOURCE` |
||
| 103 | * @throws InvalidArgumentException |
||
| 104 | */ |
||
| 105 | public function __viewIndex($resource_type) |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * This function is called from the resources index when a user uses the |
||
| 305 | * With Selected, or Apply, menu. The type of resource is given by |
||
| 306 | * `$resource_type`. At this time the only two valid values, |
||
| 307 | * `ResourceManager::RESOURCE_TYPE_EVENT` or `ResourceManager::RESOURCE_TYPE_DATASOURCE`. |
||
| 308 | * |
||
| 309 | * The function handles 'delete', 'attach', 'detach', 'attach all', |
||
| 310 | * 'detach all' actions. |
||
| 311 | * |
||
| 312 | * @param integer $resource_type |
||
| 313 | * Either `ResourceManager::RESOURCE_TYPE_EVENT` or `ResourceManager::RESOURCE_TYPE_DATASOURCE` |
||
| 314 | * @throws Exception |
||
| 315 | */ |
||
| 316 | public function __actionIndex($resource_type) |
||
| 317 | { |
||
| 318 | $manager = ResourceManager::getManagerFromType($resource_type); |
||
| 319 | $checked = (is_array($_POST['items'])) ? array_keys($_POST['items']) : null; |
||
| 320 | $context = Administration::instance()->getPageCallback(); |
||
| 321 | |||
| 322 | if (isset($_POST['action']) && is_array($_POST['action'])) { |
||
| 323 | /** |
||
| 324 | * Extensions can listen for any custom actions that were added |
||
| 325 | * through `AddCustomPreferenceFieldsets` or `AddCustomActions` |
||
| 326 | * delegates. |
||
| 327 | * |
||
| 328 | * @delegate CustomActions |
||
| 329 | * @since Symphony 2.3.2 |
||
| 330 | * @param string $context |
||
| 331 | * '/blueprints/datasources/' or '/blueprints/events/' |
||
| 332 | * @param array $checked |
||
| 333 | * An array of the selected rows. The value is usually the ID of the |
||
| 334 | * the associated object. |
||
| 335 | */ |
||
| 336 | Symphony::ExtensionManager()->notifyMembers('CustomActions', $context['pageroot'], array( |
||
| 337 | 'checked' => $checked |
||
| 338 | )); |
||
| 339 | |||
| 340 | if (is_array($checked) && !empty($checked)) { |
||
| 341 | if ($_POST['with-selected'] == 'delete') { |
||
| 342 | $canProceed = true; |
||
| 343 | |||
| 344 | foreach ($checked as $handle) { |
||
| 345 | $path = call_user_func(array($manager, '__getDriverPath'), $handle); |
||
| 346 | |||
| 347 | // Don't allow Extension resources to be deleted. RE: #2027 |
||
| 348 | if (stripos($path, EXTENSIONS) === 0) { |
||
| 349 | continue; |
||
| 350 | } elseif (!General::deleteFile($path)) { |
||
| 351 | $folder = str_replace(DOCROOT, '', $path); |
||
| 352 | $folder = str_replace('/' . basename($path), '', $folder); |
||
| 353 | |||
| 354 | $this->pageAlert( |
||
| 355 | __('Failed to delete %s.', array('<code>' . basename($path) . '</code>')) |
||
| 356 | . ' ' . __('Please check permissions on %s', array('<code>' . $folder . '</code>')), |
||
| 357 | Alert::ERROR |
||
| 358 | ); |
||
| 359 | $canProceed = false; |
||
| 360 | } else { |
||
| 361 | $pages = ResourceManager::getAttachedPages($resource_type, $handle); |
||
| 362 | |||
| 363 | foreach ($pages as $page) { |
||
| 364 | ResourceManager::detach($resource_type, $handle, $page['id']); |
||
| 365 | } |
||
| 366 | } |
||
| 367 | } |
||
| 368 | |||
| 369 | if ($canProceed) { |
||
| 370 | redirect(Administration::instance()->getCurrentPageURL()); |
||
| 371 | } |
||
| 372 | } elseif (preg_match('/^(at|de)?tach-(to|from)-page-/', $_POST['with-selected'])) { |
||
| 373 | if (substr($_POST['with-selected'], 0, 6) == 'detach') { |
||
| 374 | $page = str_replace('detach-from-page-', '', $_POST['with-selected']); |
||
| 375 | |||
| 376 | foreach ($checked as $handle) { |
||
| 377 | ResourceManager::detach($resource_type, $handle, $page); |
||
| 378 | } |
||
| 379 | } else { |
||
| 380 | $page = str_replace('attach-to-page-', '', $_POST['with-selected']); |
||
| 381 | |||
| 382 | foreach ($checked as $handle) { |
||
| 383 | ResourceManager::attach($resource_type, $handle, $page); |
||
| 384 | } |
||
| 385 | } |
||
| 386 | |||
| 387 | if ($canProceed) { |
||
| 388 | redirect(Administration::instance()->getCurrentPageURL()); |
||
| 389 | } |
||
| 390 | } elseif (preg_match('/^(at|de)?tach-all-pages$/', $_POST['with-selected'])) { |
||
| 391 | $pages = PageManager::fetch(false, array('id')); |
||
| 392 | |||
| 393 | if (substr($_POST['with-selected'], 0, 6) == 'detach') { |
||
| 394 | foreach ($checked as $handle) { |
||
| 395 | foreach ($pages as $page) { |
||
| 396 | ResourceManager::detach($resource_type, $handle, $page['id']); |
||
| 397 | } |
||
| 398 | } |
||
| 399 | } else { |
||
| 400 | foreach ($checked as $handle) { |
||
| 401 | foreach ($pages as $page) { |
||
| 402 | ResourceManager::attach($resource_type, $handle, $page['id']); |
||
| 403 | } |
||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | redirect(Administration::instance()->getCurrentPageURL()); |
||
| 408 | } |
||
| 409 | } |
||
| 410 | } |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Returns the path to the component-template by looking at the |
||
| 415 | * `WORKSPACE/template/` directory, then at the `TEMPLATES` |
||
| 416 | * directory for the convention `*.tpl`. If the template |
||
| 417 | * is not found, false is returned |
||
| 418 | * |
||
| 419 | * @param string $name |
||
| 420 | * Name of the template |
||
| 421 | * @return mixed |
||
| 422 | * String, which is the path to the template if the template is found, |
||
| 423 | * false otherwise |
||
| 424 | */ |
||
| 425 | protected function getTemplate($name) |
||
| 435 | } |
||
| 436 | } |
||
| 438 |