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:
Complex classes like CMSMain 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 CMSMain, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 76 | class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionProvider { | ||
| 77 | |||
| 78 | private static $url_segment = 'pages'; | ||
|  | |||
| 79 | |||
| 80 | private static $url_rule = '/$Action/$ID/$OtherID'; | ||
| 81 | |||
| 82 | // Maintain a lower priority than other administration sections | ||
| 83 | // so that Director does not think they are actions of CMSMain | ||
| 84 | private static $url_priority = 39; | ||
| 85 | |||
| 86 | private static $menu_title = 'Edit Page'; | ||
| 87 | |||
| 88 | private static $menu_priority = 10; | ||
| 89 | |||
| 90 | private static $tree_class = SiteTree::class; | ||
| 91 | |||
| 92 | private static $subitem_class = Member::class; | ||
| 93 | |||
| 94 | private static $session_namespace = 'SilverStripe\\CMS\\Controllers\\CMSMain'; | ||
| 95 | |||
| 96 | private static $required_permission_codes = 'CMS_ACCESS_CMSMain'; | ||
| 97 | |||
| 98 | /** | ||
| 99 | * Amount of results showing on a single page. | ||
| 100 | * | ||
| 101 | * @config | ||
| 102 | * @var int | ||
| 103 | */ | ||
| 104 | private static $page_length = 15; | ||
| 105 | |||
| 106 | private static $allowed_actions = array( | ||
| 107 | 'archive', | ||
| 108 | 'deleteitems', | ||
| 109 | 'DeleteItemsForm', | ||
| 110 | 'dialog', | ||
| 111 | 'duplicate', | ||
| 112 | 'duplicatewithchildren', | ||
| 113 | 'publishall', | ||
| 114 | 'publishitems', | ||
| 115 | 'PublishItemsForm', | ||
| 116 | 'submit', | ||
| 117 | 'EditForm', | ||
| 118 | 'SearchForm', | ||
| 119 | 'SiteTreeAsUL', | ||
| 120 | 'getshowdeletedsubtree', | ||
| 121 | 'batchactions', | ||
| 122 | 'treeview', | ||
| 123 | 'listview', | ||
| 124 | 'ListViewForm', | ||
| 125 | 'childfilter', | ||
| 126 | ); | ||
| 127 | |||
| 128 | private static $casting = array( | ||
| 129 | 'TreeIsFiltered' => 'Boolean', | ||
| 130 | 'AddForm' => 'HTMLFragment', | ||
| 131 | 'LinkPages' => 'Text', | ||
| 132 | 'Link' => 'Text', | ||
| 133 | 'ListViewForm' => 'HTMLFragment', | ||
| 134 | 'ExtraTreeTools' => 'HTMLFragment', | ||
| 135 | 'PageList' => 'HTMLFragment', | ||
| 136 | 'PageListSidebar' => 'HTMLFragment', | ||
| 137 | 'SiteTreeHints' => 'HTMLFragment', | ||
| 138 | 'SecurityID' => 'Text', | ||
| 139 | 'SiteTreeAsUL' => 'HTMLFragment', | ||
| 140 | ); | ||
| 141 | |||
| 142 | 	public function init() { | ||
| 161 | |||
| 162 | 	public function index($request) { | ||
| 171 | |||
| 172 | 	public function getResponseNegotiator() { | ||
| 192 | |||
| 193 | /** | ||
| 194 | * Get pages listing area | ||
| 195 | * | ||
| 196 | * @return DBHTMLText | ||
| 197 | */ | ||
| 198 | 	public function PageList() { | ||
| 201 | |||
| 202 | /** | ||
| 203 | * Page list view for edit-form | ||
| 204 | * | ||
| 205 | * @return DBHTMLText | ||
| 206 | */ | ||
| 207 | 	public function PageListSidebar() { | ||
| 210 | |||
| 211 | /** | ||
| 212 | * If this is set to true, the "switchView" context in the | ||
| 213 | * template is shown, with links to the staging and publish site. | ||
| 214 | * | ||
| 215 | * @return boolean | ||
| 216 | */ | ||
| 217 | 	public function ShowSwitchView() { | ||
| 220 | |||
| 221 | /** | ||
| 222 | * Overloads the LeftAndMain::ShowView. Allows to pass a page as a parameter, so we are able | ||
| 223 | * to switch view also for archived versions. | ||
| 224 | * | ||
| 225 | * @param SiteTree $page | ||
| 226 | * @return array | ||
| 227 | */ | ||
| 228 | 	public function SwitchView($page = null) { | ||
| 238 | |||
| 239 | //------------------------------------------------------------------------------------------// | ||
| 240 | // Main controllers | ||
| 241 | |||
| 242 | //------------------------------------------------------------------------------------------// | ||
| 243 | // Main UI components | ||
| 244 | |||
| 245 | /** | ||
| 246 | 	 * Override {@link LeftAndMain} Link to allow blank URL segment for CMSMain. | ||
| 247 | * | ||
| 248 | * @param string|null $action Action to link to. | ||
| 249 | * @return string | ||
| 250 | */ | ||
| 251 | 	public function Link($action = null) { | ||
| 261 | |||
| 262 | 	public function LinkPages() { | ||
| 265 | |||
| 266 | 	public function LinkPagesWithSearch() { | ||
| 269 | |||
| 270 | /** | ||
| 271 | * Get link to tree view | ||
| 272 | * | ||
| 273 | * @return string | ||
| 274 | */ | ||
| 275 | 	public function LinkTreeView() { | ||
| 279 | |||
| 280 | /** | ||
| 281 | * Get link to list view | ||
| 282 | * | ||
| 283 | * @return string | ||
| 284 | */ | ||
| 285 | 	public function LinkListView() { | ||
| 289 | |||
| 290 | View Code Duplication | 	public function LinkPageEdit($id = null) { | |
| 298 | |||
| 299 | View Code Duplication | 	public function LinkPageSettings() { | |
| 308 | |||
| 309 | View Code Duplication | 	public function LinkPageHistory() { | |
| 318 | |||
| 319 | 	public function LinkWithSearch($link) { | ||
| 332 | |||
| 333 | 	public function LinkPageAdd($extra = null, $placeholders = null) { | ||
| 347 | |||
| 348 | /** | ||
| 349 | * @return string | ||
| 350 | */ | ||
| 351 | 	public function LinkPreview() { | ||
| 365 | |||
| 366 | /** | ||
| 367 | * Return the entire site tree as a nested set of ULs | ||
| 368 | */ | ||
| 369 | 	public function SiteTreeAsUL() { | ||
| 379 | |||
| 380 | /** | ||
| 381 | * @return boolean | ||
| 382 | */ | ||
| 383 | 	public function TreeIsFiltered() { | ||
| 392 | |||
| 393 | 	public function ExtraTreeTools() { | ||
| 398 | |||
| 399 | /** | ||
| 400 | * Returns a Form for page searching for use in templates. | ||
| 401 | * | ||
| 402 | * Can be modified from a decorator by a 'updateSearchForm' method | ||
| 403 | * | ||
| 404 | * @return Form | ||
| 405 | */ | ||
| 406 | 	public function SearchForm() { | ||
| 407 | // Create the fields | ||
| 408 | 		$content = new TextField('q[Term]', _t('CMSSearch.FILTERLABELTEXT', 'Search')); | ||
| 409 | $dateFrom = new DateField( | ||
| 410 | 'q[LastEditedFrom]', | ||
| 411 | 			_t('CMSSearch.FILTERDATEFROM', 'From') | ||
| 412 | ); | ||
| 413 | 		$dateFrom->setConfig('showcalendar', true); | ||
| 414 | $dateTo = new DateField( | ||
| 415 | 'q[LastEditedTo]', | ||
| 416 | 			_t('CMSSearch.FILTERDATETO', 'To') | ||
| 417 | ); | ||
| 418 | 		$dateTo->setConfig('showcalendar', true); | ||
| 419 | $pageFilter = new DropdownField( | ||
| 420 | 'q[FilterClass]', | ||
| 421 | 			_t('CMSMain.PAGES', 'Page status'), | ||
| 422 | CMSSiteTreeFilter::get_all_filters() | ||
| 423 | ); | ||
| 424 | $pageClasses = new DropdownField( | ||
| 425 | 'q[ClassName]', | ||
| 426 | 			_t('CMSMain.PAGETYPEOPT', 'Page type', 'Dropdown for limiting search to a page type'), | ||
| 427 | $this->getPageTypes() | ||
| 428 | ); | ||
| 429 | 		$pageClasses->setEmptyString(_t('CMSMain.PAGETYPEANYOPT','Any')); | ||
| 430 | |||
| 431 | // Group the Datefields | ||
| 432 | $dateGroup = new FieldGroup( | ||
| 433 | $dateFrom, | ||
| 434 | $dateTo | ||
| 435 | ); | ||
| 436 | 		$dateGroup->setTitle(_t('CMSSearch.PAGEFILTERDATEHEADING', 'Last edited')); | ||
| 437 | |||
| 438 | // view mode | ||
| 439 | 		$viewMode = HiddenField::create('view', false, $this->ViewState()); | ||
| 440 | |||
| 441 | // Create the Field list | ||
| 442 | $fields = new FieldList( | ||
| 443 | $content, | ||
| 444 | $pageFilter, | ||
| 445 | $pageClasses, | ||
| 446 | $dateGroup, | ||
| 447 | $viewMode | ||
| 448 | ); | ||
| 449 | |||
| 450 | // Create the Search and Reset action | ||
| 451 | $actions = new FieldList( | ||
| 452 | 			FormAction::create('doSearch',  _t('CMSMain_left_ss.APPLY_FILTER', 'Search')) | ||
| 453 | 				->addExtraClass('ss-ui-action-constructive'), | ||
| 454 | 			ResetFormAction::create('clear', _t('CMSMain_left_ss.CLEAR_FILTER', 'Clear')) | ||
| 455 | ); | ||
| 456 | |||
| 457 | // Use <button> to allow full jQuery UI styling on the all of the Actions | ||
| 458 | /** @var FormAction $action */ | ||
| 459 | 		foreach($actions->dataFields() as $action) { | ||
| 460 | /** @var FormAction $action */ | ||
| 461 | $action->setUseButtonTag(true); | ||
| 462 | } | ||
| 463 | |||
| 464 | // Create the form | ||
| 465 | /** @skipUpgrade */ | ||
| 466 | $form = Form::create($this, 'SearchForm', $fields, $actions) | ||
| 467 | 			->addExtraClass('cms-search-form') | ||
| 468 | 			->setFormMethod('GET') | ||
| 469 | ->setFormAction($this->Link()) | ||
| 470 | ->disableSecurityToken() | ||
| 471 | ->unsetValidator(); | ||
| 472 | |||
| 473 | // Load the form with previously sent search data | ||
| 474 | $form->loadDataFrom($this->getRequest()->getVars()); | ||
| 475 | |||
| 476 | // Allow decorators to modify the form | ||
| 477 | 		$this->extend('updateSearchForm', $form); | ||
| 478 | |||
| 479 | return $form; | ||
| 480 | } | ||
| 481 | |||
| 482 | /** | ||
| 483 | * Returns a sorted array suitable for a dropdown with pagetypes and their translated name | ||
| 484 | * | ||
| 485 | * @return array | ||
| 486 | */ | ||
| 487 | 	protected function getPageTypes() { | ||
| 488 | $pageTypes = array(); | ||
| 489 | 		foreach(SiteTree::page_type_classes() as $pageTypeClass) { | ||
| 490 | $pageTypes[$pageTypeClass] = SiteTree::singleton($pageTypeClass)->i18n_singular_name(); | ||
| 491 | } | ||
| 492 | asort($pageTypes); | ||
| 493 | return $pageTypes; | ||
| 494 | } | ||
| 495 | |||
| 496 | 	public function doSearch($data, $form) { | ||
| 497 | return $this->getsubtree($this->getRequest()); | ||
| 498 | } | ||
| 499 | |||
| 500 | /** | ||
| 501 | * @param bool $unlinked | ||
| 502 | * @return ArrayList | ||
| 503 | */ | ||
| 504 | 	public function Breadcrumbs($unlinked = false) { | ||
| 505 | $items = parent::Breadcrumbs($unlinked); | ||
| 506 | |||
| 507 | 		if($items->count() > 1) { | ||
| 508 | // Specific to the SiteTree admin section, we never show the cms section and current | ||
| 509 | // page in the same breadcrumbs block. | ||
| 510 | $items->shift(); | ||
| 511 | } | ||
| 512 | |||
| 513 | return $items; | ||
| 514 | } | ||
| 515 | |||
| 516 | /** | ||
| 517 | * Create serialized JSON string with site tree hints data to be injected into | ||
| 518 | * 'data-hints' attribute of root node of jsTree. | ||
| 519 | * | ||
| 520 | * @return string Serialized JSON | ||
| 521 | */ | ||
| 522 | 	public function SiteTreeHints() { | ||
| 523 | $classes = SiteTree::page_type_classes(); | ||
| 524 | |||
| 525 | $cacheCanCreate = array(); | ||
| 526 | foreach($classes as $class) $cacheCanCreate[$class] = singleton($class)->canCreate(); | ||
| 527 | |||
| 528 | // Generate basic cache key. Too complex to encompass all variations | ||
| 529 | 	 	$cache = Cache::factory('CMSMain_SiteTreeHints'); | ||
| 530 | 	 	$cacheKey = md5(implode('_', array(Member::currentUserID(), implode(',', $cacheCanCreate), implode(',', $classes)))); | ||
| 531 | 	 	if($this->getRequest()->getVar('flush')) { | ||
| 532 | $cache->clean(Zend_Cache::CLEANING_MODE_ALL); | ||
| 533 | } | ||
| 534 | $json = $cache->load($cacheKey); | ||
| 535 | 	 	if(!$json) { | ||
| 536 | $def['Root'] = array(); | ||
| 537 | $def['Root']['disallowedChildren'] = array(); | ||
| 538 | |||
| 539 | // Contains all possible classes to support UI controls listing them all, | ||
| 540 | // such as the "add page here" context menu. | ||
| 541 | $def['All'] = array(); | ||
| 542 | |||
| 543 | // Identify disallows and set globals | ||
| 544 | 			foreach($classes as $class) { | ||
| 545 | $obj = singleton($class); | ||
| 546 | if($obj instanceof HiddenClass) continue; | ||
| 547 | |||
| 548 | // Name item | ||
| 549 | $def['All'][$class] = array( | ||
| 550 | 'title' => $obj->i18n_singular_name() | ||
| 551 | ); | ||
| 552 | |||
| 553 | // Check if can be created at the root | ||
| 554 | 				$needsPerm = $obj->stat('need_permission'); | ||
| 555 | if( | ||
| 556 | 					!$obj->stat('can_be_root') | ||
| 557 | || (!array_key_exists($class, $cacheCanCreate) || !$cacheCanCreate[$class]) | ||
| 558 | || ($needsPerm && !$this->can($needsPerm)) | ||
| 559 | 				) { | ||
| 560 | $def['Root']['disallowedChildren'][] = $class; | ||
| 561 | } | ||
| 562 | |||
| 563 | // Hint data specific to the class | ||
| 564 | $def[$class] = array(); | ||
| 565 | |||
| 566 | $defaultChild = $obj->defaultChild(); | ||
| 567 | 				if($defaultChild !== 'Page' && $defaultChild !== null) { | ||
| 568 | $def[$class]['defaultChild'] = $defaultChild; | ||
| 569 | } | ||
| 570 | |||
| 571 | $defaultParent = $obj->defaultParent(); | ||
| 572 | 				if ($defaultParent !== 1 && $defaultParent !== null) { | ||
| 573 | $def[$class]['defaultParent'] = $defaultParent; | ||
| 574 | } | ||
| 575 | } | ||
| 576 | |||
| 577 | 			$this->extend('updateSiteTreeHints', $def); | ||
| 578 | |||
| 579 | $json = Convert::raw2json($def); | ||
| 580 | $cache->save($json, $cacheKey); | ||
| 581 | } | ||
| 582 | return $json; | ||
| 583 | } | ||
| 584 | |||
| 585 | /** | ||
| 586 | * Populates an array of classes in the CMS | ||
| 587 | * which allows the user to change the page type. | ||
| 588 | * | ||
| 589 | * @return SS_List | ||
| 590 | */ | ||
| 591 | 	public function PageTypes() { | ||
| 592 | $classes = SiteTree::page_type_classes(); | ||
| 593 | |||
| 594 | $result = new ArrayList(); | ||
| 595 | |||
| 596 | 		foreach($classes as $class) { | ||
| 597 | $instance = singleton($class); | ||
| 598 | |||
| 599 | 			if($instance instanceof HiddenClass) { | ||
| 600 | continue; | ||
| 601 | } | ||
| 602 | |||
| 603 | // skip this type if it is restricted | ||
| 604 | 			if($instance->stat('need_permission') && !$this->can(singleton($class)->stat('need_permission'))) { | ||
| 605 | continue; | ||
| 606 | } | ||
| 607 | |||
| 608 | $addAction = $instance->i18n_singular_name(); | ||
| 609 | |||
| 610 | // Get description (convert 'Page' to 'SiteTree' for correct localization lookups) | ||
| 611 | $i18nClass = ($class == 'Page') ? 'SilverStripe\\CMS\\Model\\SiteTree' : $class; | ||
| 612 | $description = _t($i18nClass . '.DESCRIPTION'); | ||
| 613 | |||
| 614 | 			if(!$description) { | ||
| 615 | 				$description = $instance->uninherited('description'); | ||
| 616 | } | ||
| 617 | |||
| 618 | 			if($class == 'Page' && !$description) { | ||
| 619 | 				$description = SiteTree::singleton()->uninherited('description'); | ||
| 620 | } | ||
| 621 | |||
| 622 | $result->push(new ArrayData(array( | ||
| 623 | 'ClassName' => $class, | ||
| 624 | 'AddAction' => $addAction, | ||
| 625 | 'Description' => $description, | ||
| 626 | // TODO Sprite support | ||
| 627 | 				'IconURL' => $instance->stat('icon'), | ||
| 628 | 'Title' => singleton($class)->i18n_singular_name(), | ||
| 629 | ))); | ||
| 630 | } | ||
| 631 | |||
| 632 | 		$result = $result->sort('AddAction'); | ||
| 633 | |||
| 634 | return $result; | ||
| 635 | } | ||
| 636 | |||
| 637 | /** | ||
| 638 | * Get a database record to be managed by the CMS. | ||
| 639 | * | ||
| 640 | * @param int $id Record ID | ||
| 641 | * @param int $versionID optional Version id of the given record | ||
| 642 | * @return SiteTree | ||
| 643 | */ | ||
| 644 |  	public function getRecord($id, $versionID = null) { | ||
| 645 | 		$treeClass = $this->stat('tree_class'); | ||
| 646 | |||
| 647 | 		if($id instanceof $treeClass) { | ||
| 648 | return $id; | ||
| 649 | } | ||
| 650 | 		else if($id && is_numeric($id)) { | ||
| 651 | $currentStage = Versioned::get_reading_mode(); | ||
| 652 | |||
| 653 | 			if($this->getRequest()->getVar('Version')) { | ||
| 654 | 				$versionID = (int) $this->getRequest()->getVar('Version'); | ||
| 655 | } | ||
| 656 | |||
| 657 | 			if($versionID) { | ||
| 658 | $record = Versioned::get_version($treeClass, $id, $versionID); | ||
| 659 | 			} else { | ||
| 660 | $record = DataObject::get_by_id($treeClass, $id); | ||
| 661 | } | ||
| 662 | |||
| 663 | // Then, try getting a record from the live site | ||
| 664 | 			if(!$record) { | ||
| 665 | // $record = Versioned::get_one_by_stage($treeClass, "Live", "\"$treeClass\".\"ID\" = $id"); | ||
| 666 | Versioned::set_stage(Versioned::LIVE); | ||
| 667 | singleton($treeClass)->flushCache(); | ||
| 668 | |||
| 669 | $record = DataObject::get_by_id($treeClass, $id); | ||
| 670 | } | ||
| 671 | |||
| 672 | // Then, try getting a deleted record | ||
| 673 | 			if(!$record) { | ||
| 674 | $record = Versioned::get_latest_version($treeClass, $id); | ||
| 675 | } | ||
| 676 | |||
| 677 | // Don't open a page from a different locale | ||
| 678 | /** The record's Locale is saved in database in 2.4, and not related with Session, | ||
| 679 | * we should not check their locale matches the Translatable::get_current_locale, | ||
| 680 | * here as long as we all the HTTPRequest is init with right locale. | ||
| 681 | * This bit breaks the all FileIFrameField functions if the field is used in CMS | ||
| 682 | * and its relevent ajax calles, like loading the tree dropdown for TreeSelectorField. | ||
| 683 | */ | ||
| 684 | 			/* if($record && SiteTree::has_extension('Translatable') && $record->Locale && $record->Locale != Translatable::get_current_locale()) { | ||
| 685 | $record = null; | ||
| 686 | }*/ | ||
| 687 | |||
| 688 | // Set the reading mode back to what it was. | ||
| 689 | Versioned::set_reading_mode($currentStage); | ||
| 690 | |||
| 691 | return $record; | ||
| 692 | |||
| 693 | 		} else if(substr($id,0,3) == 'new') { | ||
| 694 | return $this->getNewItem($id); | ||
| 695 | } | ||
| 696 | } | ||
| 697 | |||
| 698 | /** | ||
| 699 | * @param int $id | ||
| 700 | * @param FieldList $fields | ||
| 701 | * @return Form | ||
| 702 | */ | ||
| 703 | 	public function getEditForm($id = null, $fields = null) { | ||
| 704 | if(!$id) $id = $this->currentPageID(); | ||
| 705 | $form = parent::getEditForm($id, $fields); | ||
| 706 | |||
| 707 | // TODO Duplicate record fetching (see parent implementation) | ||
| 708 | $record = $this->getRecord($id); | ||
| 709 | if($record && !$record->canView()) return Security::permissionFailure($this); | ||
| 710 | |||
| 711 | if(!$fields) $fields = $form->Fields(); | ||
| 712 | $actions = $form->Actions(); | ||
| 713 | |||
| 714 | 		if($record) { | ||
| 715 | $deletedFromStage = !$record->isOnDraft(); | ||
| 716 | |||
| 717 | 			$fields->push($idField = new HiddenField("ID", false, $id)); | ||
| 718 | // Necessary for different subsites | ||
| 719 | 			$fields->push($liveLinkField = new HiddenField("AbsoluteLink", false, $record->AbsoluteLink())); | ||
| 720 | 			$fields->push($liveLinkField = new HiddenField("LiveLink")); | ||
| 721 | 			$fields->push($stageLinkField = new HiddenField("StageLink")); | ||
| 722 | 			$fields->push(new HiddenField("TreeTitle", false, $record->TreeTitle)); | ||
| 723 | |||
| 724 | 			if($record->ID && is_numeric( $record->ID ) ) { | ||
| 725 | $liveLink = $record->getAbsoluteLiveLink(); | ||
| 726 | if($liveLink) $liveLinkField->setValue($liveLink); | ||
| 727 | 				if(!$deletedFromStage) { | ||
| 728 | $stageLink = Controller::join_links($record->AbsoluteLink(), '?stage=Stage'); | ||
| 729 | if($stageLink) $stageLinkField->setValue($stageLink); | ||
| 730 | } | ||
| 731 | } | ||
| 732 | |||
| 733 | // Added in-line to the form, but plucked into different view by LeftAndMain.Preview.js upon load | ||
| 734 | /** @skipUpgrade */ | ||
| 735 | 			if($record instanceof CMSPreviewable && !$fields->fieldByName('SilverStripeNavigator')) { | ||
| 736 | 				$navField = new LiteralField('SilverStripeNavigator', $this->getSilverStripeNavigator()); | ||
| 737 | $navField->setAllowHTML(true); | ||
| 738 | $fields->push($navField); | ||
| 739 | } | ||
| 740 | |||
| 741 | // getAllCMSActions can be used to completely redefine the action list | ||
| 742 | 			if($record->hasMethod('getAllCMSActions')) { | ||
| 743 | $actions = $record->getAllCMSActions(); | ||
| 744 | 			} else { | ||
| 745 | $actions = $record->getCMSActions(); | ||
| 746 | |||
| 747 | // Find and remove action menus that have no actions. | ||
| 748 | 				if ($actions && $actions->count()) { | ||
| 749 | /** @var TabSet $tabset */ | ||
| 750 | 					$tabset = $actions->fieldByName('ActionMenus'); | ||
| 751 | 					if ($tabset) { | ||
| 752 | 						foreach ($tabset->getChildren() as $tab) { | ||
| 753 | 							if (!$tab->getChildren()->count()) { | ||
| 754 | $tabset->removeByName($tab->getName()); | ||
| 755 | } | ||
| 756 | } | ||
| 757 | } | ||
| 758 | } | ||
| 759 | } | ||
| 760 | |||
| 761 | // Use <button> to allow full jQuery UI styling | ||
| 762 | $actionsFlattened = $actions->dataFields(); | ||
| 763 | 			if($actionsFlattened) { | ||
| 764 | /** @var FormAction $action */ | ||
| 765 | 				foreach($actionsFlattened as $action) { | ||
| 766 | $action->setUseButtonTag(true); | ||
| 767 | } | ||
| 768 | } | ||
| 769 | |||
| 770 | 			if($record->hasMethod('getCMSValidator')) { | ||
| 771 | $validator = $record->getCMSValidator(); | ||
| 772 | 			} else { | ||
| 773 | $validator = new RequiredFields(); | ||
| 774 | } | ||
| 775 | |||
| 776 | // TODO Can't merge $FormAttributes in template at the moment | ||
| 777 | 			$form->addExtraClass('center ' . $this->BaseCSSClasses()); | ||
| 778 | // Set validation exemptions for specific actions | ||
| 779 | 			$form->setValidationExemptActions(array('restore', 'revert', 'deletefromlive', 'delete', 'unpublish', 'rollback', 'doRollback')); | ||
| 780 | |||
| 781 | // Announce the capability so the frontend can decide whether to allow preview or not. | ||
| 782 | 			if ($record instanceof CMSPreviewable) { | ||
| 783 | 				$form->addExtraClass('cms-previewable'); | ||
| 784 | } | ||
| 785 | 			$form->addExtraClass('fill-height flexbox-area-grow'); | ||
| 786 | |||
| 787 | 			if(!$record->canEdit() || $deletedFromStage) { | ||
| 788 | $readonlyFields = $form->Fields()->makeReadonly(); | ||
| 789 | $form->setFields($readonlyFields); | ||
| 790 | } | ||
| 791 | |||
| 792 | $form->Fields()->setForm($form); | ||
| 793 | |||
| 794 | 			$this->extend('updateEditForm', $form); | ||
| 795 | return $form; | ||
| 796 | 		} else if($id) { | ||
| 797 | $form = Form::create( $this, "EditForm", new FieldList( | ||
| 798 | 				new LabelField('PageDoesntExistLabel',_t('CMSMain.PAGENOTEXISTS',"This page doesn't exist"))), new FieldList() | ||
| 799 | 			)->setHTMLID('Form_EditForm'); | ||
| 800 | return $form; | ||
| 801 | } | ||
| 802 | } | ||
| 803 | |||
| 804 | /** | ||
| 805 | * @param HTTPRequest $request | ||
| 806 | * @return string HTML | ||
| 807 | */ | ||
| 808 | 	public function treeview($request) { | ||
| 809 | return $this->getResponseNegotiator()->respond($request); | ||
| 810 | } | ||
| 811 | |||
| 812 | /** | ||
| 813 | * @param HTTPRequest $request | ||
| 814 | * @return string HTML | ||
| 815 | */ | ||
| 816 | 	public function listview($request) { | ||
| 817 | return $this->getResponseNegotiator()->respond($request); | ||
| 818 | } | ||
| 819 | |||
| 820 | /** | ||
| 821 | * @return string | ||
| 822 | */ | ||
| 823 | 	public function ViewState() { | ||
| 824 | 		$mode = $this->getRequest()->requestVar('view') | ||
| 825 | 			?: $this->getRequest()->param('Action'); | ||
| 826 | 		switch($mode) { | ||
| 827 | case 'listview': | ||
| 828 | case 'treeview': | ||
| 829 | return $mode; | ||
| 830 | default: | ||
| 831 | return 'treeview'; | ||
| 832 | } | ||
| 833 | } | ||
| 834 | |||
| 835 | /** | ||
| 836 | * Callback to request the list of page types allowed under a given page instance. | ||
| 837 | * Provides a slower but more precise response over SiteTreeHints | ||
| 838 | * | ||
| 839 | * @param HTTPRequest $request | ||
| 840 | * @return HTTPResponse | ||
| 841 | */ | ||
| 842 | 	public function childfilter($request) { | ||
| 867 | |||
| 868 | /** | ||
| 869 | * Safely reconstruct a selected filter from a given set of query parameters | ||
| 870 | * | ||
| 871 | * @param array $params Query parameters to use | ||
| 872 | * @return CMSSiteTreeFilter The filter class, or null if none present | ||
| 873 | * @throws InvalidArgumentException if invalid filter class is passed. | ||
| 874 | */ | ||
| 875 | 	protected function getQueryFilter($params) { | ||
| 883 | |||
| 884 | /** | ||
| 885 | 	 * Returns the pages meet a certain criteria as {@see CMSSiteTreeFilter} or the subpages of a parent page | ||
| 886 | * defaulting to no filter and show all pages in first level. | ||
| 887 | 	 * Doubles as search results, if any search parameters are set through {@link SearchForm()}. | ||
| 888 | * | ||
| 889 | * @param array $params Search filter criteria | ||
| 890 | * @param int $parentID Optional parent node to filter on (can't be combined with other search criteria) | ||
| 891 | * @return SS_List | ||
| 892 | * @throws InvalidArgumentException if invalid filter class is passed. | ||
| 893 | */ | ||
| 894 | 	public function getList($params = array(), $parentID = 0) { | ||
| 903 | |||
| 904 | /** | ||
| 905 | * @return Form | ||
| 906 | */ | ||
| 907 | 	public function ListViewForm() { | ||
| 1005 | |||
| 1006 | 	public function currentPageID() { | ||
| 1013 | |||
| 1014 | //------------------------------------------------------------------------------------------// | ||
| 1015 | // Data saving handlers | ||
| 1016 | |||
| 1017 | /** | ||
| 1018 | * Save and Publish page handler | ||
| 1019 | * | ||
| 1020 | * @param array $data | ||
| 1021 | * @param Form $form | ||
| 1022 | * @return HTTPResponse | ||
| 1023 | * @throws HTTPResponse_Exception | ||
| 1024 | */ | ||
| 1025 | 	public function save($data, $form) { | ||
| 1091 | |||
| 1092 | /** | ||
| 1093 | * @uses LeftAndMainExtension->augmentNewSiteTreeItem() | ||
| 1094 | * | ||
| 1095 | * @param int|string $id | ||
| 1096 | * @param bool $setID | ||
| 1097 | * @return mixed|DataObject | ||
| 1098 | * @throws HTTPResponse_Exception | ||
| 1099 | */ | ||
| 1100 | 	public function getNewItem($id, $setID = true) { | ||
| 1147 | |||
| 1148 | /** | ||
| 1149 | * Actually perform the publication step | ||
| 1150 | * | ||
| 1151 | * @param Versioned|DataObject $record | ||
| 1152 | * @return mixed | ||
| 1153 | */ | ||
| 1154 | 	public function performPublish($record) { | ||
| 1161 | |||
| 1162 | /** | ||
| 1163 | * Reverts a page by publishing it to live. | ||
| 1164 | 	 * Use {@link restorepage()} if you want to restore a page | ||
| 1165 | * which was deleted from draft without publishing. | ||
| 1166 | * | ||
| 1167 | * @uses SiteTree->doRevertToLive() | ||
| 1168 | * | ||
| 1169 | * @param array $data | ||
| 1170 | * @param Form $form | ||
| 1171 | * @return HTTPResponse | ||
| 1172 | * @throws HTTPResponse_Exception | ||
| 1173 | */ | ||
| 1174 | 	public function revert($data, $form) { | ||
| 1213 | |||
| 1214 | /** | ||
| 1215 | * Delete the current page from draft stage. | ||
| 1216 | * | ||
| 1217 | * @see deletefromlive() | ||
| 1218 | * | ||
| 1219 | * @param array $data | ||
| 1220 | * @param Form $form | ||
| 1221 | * @return HTTPResponse | ||
| 1222 | * @throws HTTPResponse_Exception | ||
| 1223 | */ | ||
| 1224 | View Code Duplication | 	public function delete($data, $form) { | |
| 1245 | |||
| 1246 | /** | ||
| 1247 | * Delete this page from both live and stage | ||
| 1248 | * | ||
| 1249 | * @param array $data | ||
| 1250 | * @param Form $form | ||
| 1251 | * @return HTTPResponse | ||
| 1252 | * @throws HTTPResponse_Exception | ||
| 1253 | */ | ||
| 1254 | View Code Duplication | 	public function archive($data, $form) { | |
| 1276 | |||
| 1277 | 	public function publish($data, $form) { | ||
| 1282 | |||
| 1283 | 	public function unpublish($data, $form) { | ||
| 1304 | |||
| 1305 | /** | ||
| 1306 | * @return HTTPResponse | ||
| 1307 | */ | ||
| 1308 | 	public function rollback() { | ||
| 1314 | |||
| 1315 | /** | ||
| 1316 | * Rolls a site back to a given version ID | ||
| 1317 | * | ||
| 1318 | * @param array $data | ||
| 1319 | * @param Form $form | ||
| 1320 | * @return HTTPResponse | ||
| 1321 | */ | ||
| 1322 | 	public function doRollback($data, $form) { | ||
| 1360 | |||
| 1361 | /** | ||
| 1362 | * Batch Actions Handler | ||
| 1363 | */ | ||
| 1364 | 	public function batchactions() { | ||
| 1367 | |||
| 1368 | 	public function BatchActionParameters() { | ||
| 1389 | /** | ||
| 1390 | * Returns a list of batch actions | ||
| 1391 | */ | ||
| 1392 | 	public function BatchActionList() { | ||
| 1395 | |||
| 1396 | 	public function publishall($request) { | ||
| 1452 | |||
| 1453 | /** | ||
| 1454 | * Restore a completely deleted page from the SiteTree_versions table. | ||
| 1455 | * | ||
| 1456 | * @param array $data | ||
| 1457 | * @param Form $form | ||
| 1458 | * @return HTTPResponse | ||
| 1459 | */ | ||
| 1460 | 	public function restore($data, $form) { | ||
| 1485 | |||
| 1486 | 	public function duplicate($request) { | ||
| 1524 | |||
| 1525 | 	public function duplicatewithchildren($request) { | ||
| 1557 | |||
| 1558 | 	public function providePermissions() { | ||
| 1572 | |||
| 1573 | } | ||
| 1574 |