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 | |||
| 79 | private static $url_segment = 'pages'; |
||
|
|
|||
| 80 | |||
| 81 | private static $url_rule = '/$Action/$ID/$OtherID'; |
||
| 82 | |||
| 83 | // Maintain a lower priority than other administration sections |
||
| 84 | // so that Director does not think they are actions of CMSMain |
||
| 85 | private static $url_priority = 39; |
||
| 86 | |||
| 87 | private static $menu_title = 'Edit Page'; |
||
| 88 | |||
| 89 | private static $menu_icon_class = 'font-icon-sitemap'; |
||
| 90 | |||
| 91 | private static $menu_priority = 10; |
||
| 92 | |||
| 93 | private static $tree_class = SiteTree::class; |
||
| 94 | |||
| 95 | private static $subitem_class = Member::class; |
||
| 96 | |||
| 97 | private static $session_namespace = 'SilverStripe\\CMS\\Controllers\\CMSMain'; |
||
| 98 | |||
| 99 | private static $required_permission_codes = 'CMS_ACCESS_CMSMain'; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Amount of results showing on a single page. |
||
| 103 | * |
||
| 104 | * @config |
||
| 105 | * @var int |
||
| 106 | */ |
||
| 107 | private static $page_length = 15; |
||
| 108 | |||
| 109 | private static $allowed_actions = array( |
||
| 110 | 'archive', |
||
| 111 | 'deleteitems', |
||
| 112 | 'DeleteItemsForm', |
||
| 113 | 'dialog', |
||
| 114 | 'duplicate', |
||
| 115 | 'duplicatewithchildren', |
||
| 116 | 'publishall', |
||
| 117 | 'publishitems', |
||
| 118 | 'PublishItemsForm', |
||
| 119 | 'submit', |
||
| 120 | 'EditForm', |
||
| 121 | 'SearchForm', |
||
| 122 | 'SiteTreeAsUL', |
||
| 123 | 'getshowdeletedsubtree', |
||
| 124 | 'batchactions', |
||
| 125 | 'treeview', |
||
| 126 | 'listview', |
||
| 127 | 'ListViewForm', |
||
| 128 | 'childfilter', |
||
| 129 | ); |
||
| 130 | |||
| 131 | private static $casting = array( |
||
| 132 | 'TreeIsFiltered' => 'Boolean', |
||
| 133 | 'AddForm' => 'HTMLFragment', |
||
| 134 | 'LinkPages' => 'Text', |
||
| 135 | 'Link' => 'Text', |
||
| 136 | 'ListViewForm' => 'HTMLFragment', |
||
| 137 | 'ExtraTreeTools' => 'HTMLFragment', |
||
| 138 | 'PageList' => 'HTMLFragment', |
||
| 139 | 'PageListSidebar' => 'HTMLFragment', |
||
| 140 | 'SiteTreeHints' => 'HTMLFragment', |
||
| 141 | 'SecurityID' => 'Text', |
||
| 142 | 'SiteTreeAsUL' => 'HTMLFragment', |
||
| 143 | ); |
||
| 144 | |||
| 145 | protected function init() |
||
| 146 | { |
||
| 147 | // set reading lang |
||
| 148 | if (SiteTree::has_extension('Translatable') && !$this->getRequest()->isAjax()) { |
||
| 149 | Translatable::choose_site_locale(array_keys(Translatable::get_existing_content_languages('SilverStripe\\CMS\\Model\\SiteTree'))); |
||
| 150 | } |
||
| 151 | |||
| 152 | parent::init(); |
||
| 153 | |||
| 154 | Requirements::javascript(CMS_DIR . '/client/dist/js/bundle.js'); |
||
| 155 | Requirements::javascript(CMS_DIR . '/client/dist/js/SilverStripeNavigator.js'); |
||
| 156 | Requirements::css(CMS_DIR . '/client/dist/styles/bundle.css'); |
||
| 157 | Requirements::customCSS($this->generatePageIconsCss()); |
||
| 158 | Requirements::add_i18n_javascript(CMS_DIR . '/client/lang', false, true); |
||
| 159 | |||
| 160 | CMSBatchActionHandler::register('restore', CMSBatchAction_Restore::class); |
||
| 161 | CMSBatchActionHandler::register('archive', CMSBatchAction_Archive::class); |
||
| 162 | CMSBatchActionHandler::register('unpublish', CMSBatchAction_Unpublish::class); |
||
| 163 | CMSBatchActionHandler::register('publish', CMSBatchAction_Publish::class); |
||
| 164 | } |
||
| 165 | |||
| 166 | public function index($request) |
||
| 167 | { |
||
| 168 | // In case we're not showing a specific record, explicitly remove any session state, |
||
| 169 | // to avoid it being highlighted in the tree, and causing an edit form to show. |
||
| 170 | if (!$request->param('Action')) { |
||
| 171 | $this->setCurrentPageID(null); |
||
| 172 | } |
||
| 173 | |||
| 174 | return parent::index($request); |
||
| 175 | } |
||
| 176 | |||
| 177 | public function getResponseNegotiator() |
||
| 178 | { |
||
| 179 | $negotiator = parent::getResponseNegotiator(); |
||
| 180 | |||
| 181 | // ListViewForm |
||
| 182 | $negotiator->setCallback('ListViewForm', function () { |
||
| 183 | return $this->ListViewForm()->forTemplate(); |
||
| 184 | }); |
||
| 185 | |||
| 186 | // PageList view |
||
| 187 | $negotiator->setCallback('Content-PageList', function () { |
||
| 188 | return $this->PageList()->forTemplate(); |
||
| 189 | }); |
||
| 190 | |||
| 191 | // PageList view for edit controller |
||
| 192 | $negotiator->setCallback('Content-PageList-Sidebar', function () { |
||
| 193 | return $this->PageListSidebar()->forTemplate(); |
||
| 194 | }); |
||
| 195 | |||
| 196 | return $negotiator; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get pages listing area |
||
| 201 | * |
||
| 202 | * @return DBHTMLText |
||
| 203 | */ |
||
| 204 | public function PageList() |
||
| 205 | { |
||
| 206 | return $this->renderWith($this->getTemplatesWithSuffix('_PageList')); |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Page list view for edit-form |
||
| 211 | * |
||
| 212 | * @return DBHTMLText |
||
| 213 | */ |
||
| 214 | public function PageListSidebar() |
||
| 215 | { |
||
| 216 | return $this->renderWith($this->getTemplatesWithSuffix('_PageList_Sidebar')); |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * If this is set to true, the "switchView" context in the |
||
| 221 | * template is shown, with links to the staging and publish site. |
||
| 222 | * |
||
| 223 | * @return boolean |
||
| 224 | */ |
||
| 225 | public function ShowSwitchView() |
||
| 226 | { |
||
| 227 | return true; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Overloads the LeftAndMain::ShowView. Allows to pass a page as a parameter, so we are able |
||
| 232 | * to switch view also for archived versions. |
||
| 233 | * |
||
| 234 | * @param SiteTree $page |
||
| 235 | * @return array |
||
| 236 | */ |
||
| 237 | public function SwitchView($page = null) |
||
| 238 | { |
||
| 239 | if (!$page) { |
||
| 240 | $page = $this->currentPage(); |
||
| 241 | } |
||
| 242 | |||
| 243 | if ($page) { |
||
| 244 | $nav = SilverStripeNavigator::get_for_record($page); |
||
| 245 | return $nav['items']; |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | //------------------------------------------------------------------------------------------// |
||
| 250 | // Main controllers |
||
| 251 | |||
| 252 | //------------------------------------------------------------------------------------------// |
||
| 253 | // Main UI components |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Override {@link LeftAndMain} Link to allow blank URL segment for CMSMain. |
||
| 257 | * |
||
| 258 | * @param string|null $action Action to link to. |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | public function Link($action = null) |
||
| 262 | { |
||
| 263 | $link = Controller::join_links( |
||
| 264 | AdminRootController::admin_url(), |
||
| 265 | $this->stat('url_segment'), // in case we want to change the segment |
||
| 266 | '/', // trailing slash needed if $action is null! |
||
| 267 | "$action" |
||
| 268 | ); |
||
| 269 | $this->extend('updateLink', $link); |
||
| 270 | return $link; |
||
| 271 | } |
||
| 272 | |||
| 273 | public function LinkPages() |
||
| 274 | { |
||
| 275 | return CMSPagesController::singleton()->Link(); |
||
| 276 | } |
||
| 277 | |||
| 278 | public function LinkPagesWithSearch() |
||
| 279 | { |
||
| 280 | return $this->LinkWithSearch($this->LinkPages()); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Get link to tree view |
||
| 285 | * |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function LinkTreeView() |
||
| 289 | { |
||
| 290 | // Tree view is just default link to main pages section (no /treeview suffix) |
||
| 291 | return $this->LinkWithSearch(CMSMain::singleton()->Link()); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get link to list view |
||
| 296 | * |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | public function LinkListView() |
||
| 300 | { |
||
| 301 | // Note : Force redirect to top level page controller |
||
| 302 | return $this->LinkWithSearch(CMSMain::singleton()->Link('listview')); |
||
| 303 | } |
||
| 304 | |||
| 305 | View Code Duplication | public function LinkPageEdit($id = null) |
|
| 306 | { |
||
| 307 | if (!$id) { |
||
| 308 | $id = $this->currentPageID(); |
||
| 309 | } |
||
| 310 | return $this->LinkWithSearch( |
||
| 311 | Controller::join_links(CMSPageEditController::singleton()->Link('show'), $id) |
||
| 312 | ); |
||
| 313 | } |
||
| 314 | |||
| 315 | View Code Duplication | public function LinkPageSettings() |
|
| 316 | { |
||
| 317 | if ($id = $this->currentPageID()) { |
||
| 318 | return $this->LinkWithSearch( |
||
| 319 | Controller::join_links(CMSPageSettingsController::singleton()->Link('show'), $id) |
||
| 320 | ); |
||
| 321 | } else { |
||
| 322 | return null; |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | View Code Duplication | public function LinkPageHistory() |
|
| 327 | { |
||
| 328 | if ($id = $this->currentPageID()) { |
||
| 329 | return $this->LinkWithSearch( |
||
| 330 | Controller::join_links(CMSPageHistoryController::singleton()->Link('show'), $id) |
||
| 331 | ); |
||
| 332 | } else { |
||
| 333 | return null; |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | public function LinkWithSearch($link) |
||
| 338 | { |
||
| 339 | // Whitelist to avoid side effects |
||
| 340 | $params = array( |
||
| 341 | 'q' => (array)$this->getRequest()->getVar('q'), |
||
| 342 | 'ParentID' => $this->getRequest()->getVar('ParentID') |
||
| 343 | ); |
||
| 344 | $link = Controller::join_links( |
||
| 345 | $link, |
||
| 346 | array_filter(array_values($params)) ? '?' . http_build_query($params) : null |
||
| 347 | ); |
||
| 348 | $this->extend('updateLinkWithSearch', $link); |
||
| 349 | return $link; |
||
| 350 | } |
||
| 351 | |||
| 352 | public function LinkPageAdd($extra = null, $placeholders = null) |
||
| 353 | { |
||
| 354 | $link = CMSPageAddController::singleton()->Link(); |
||
| 355 | $this->extend('updateLinkPageAdd', $link); |
||
| 356 | |||
| 357 | if ($extra) { |
||
| 358 | $link = Controller::join_links($link, $extra); |
||
| 359 | } |
||
| 360 | |||
| 361 | if ($placeholders) { |
||
| 362 | $link .= (strpos($link, '?') === false ? "?$placeholders" : "&$placeholders"); |
||
| 363 | } |
||
| 364 | |||
| 365 | return $link; |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | public function LinkPreview() |
||
| 372 | { |
||
| 373 | $record = $this->getRecord($this->currentPageID()); |
||
| 374 | $baseLink = Director::absoluteBaseURL(); |
||
| 375 | if ($record && $record instanceof SiteTree) { |
||
| 376 | // if we are an external redirector don't show a link |
||
| 377 | if ($record instanceof RedirectorPage && $record->RedirectionType == 'External') { |
||
| 378 | $baseLink = false; |
||
| 379 | } else { |
||
| 380 | $baseLink = $record->Link('?stage=Stage'); |
||
| 381 | } |
||
| 382 | } |
||
| 383 | return $baseLink; |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Return the entire site tree as a nested set of ULs |
||
| 388 | */ |
||
| 389 | public function SiteTreeAsUL() |
||
| 390 | { |
||
| 391 | // Pre-cache sitetree version numbers for querying efficiency |
||
| 392 | Versioned::prepopulate_versionnumber_cache(SiteTree::class, "Stage"); |
||
| 393 | Versioned::prepopulate_versionnumber_cache(SiteTree::class, "Live"); |
||
| 394 | $html = $this->getSiteTreeFor($this->stat('tree_class')); |
||
| 395 | |||
| 396 | $this->extend('updateSiteTreeAsUL', $html); |
||
| 397 | |||
| 398 | return $html; |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @return boolean |
||
| 403 | */ |
||
| 404 | public function TreeIsFiltered() |
||
| 405 | { |
||
| 406 | $query = $this->getRequest()->getVar('q'); |
||
| 407 | |||
| 408 | if (!$query || (count($query) === 1 && isset($query['FilterClass']) && $query['FilterClass'] === 'SilverStripe\\CMS\\Controllers\\CMSSiteTreeFilter_Search')) { |
||
| 409 | return false; |
||
| 410 | } |
||
| 411 | |||
| 412 | return true; |
||
| 413 | } |
||
| 414 | |||
| 415 | public function ExtraTreeTools() |
||
| 416 | { |
||
| 417 | $html = ''; |
||
| 418 | $this->extend('updateExtraTreeTools', $html); |
||
| 419 | return $html; |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Returns a Form for page searching for use in templates. |
||
| 424 | * |
||
| 425 | * Can be modified from a decorator by a 'updateSearchForm' method |
||
| 426 | * |
||
| 427 | * @return Form |
||
| 428 | */ |
||
| 429 | public function SearchForm() |
||
| 430 | { |
||
| 431 | // Create the fields |
||
| 432 | $content = new TextField('q[Term]', _t('CMSSearch.FILTERLABELTEXT', 'Search')); |
||
| 433 | $dateFrom = new DateField( |
||
| 434 | 'q[LastEditedFrom]', |
||
| 435 | _t('CMSSearch.FILTERDATEFROM', 'From') |
||
| 436 | ); |
||
| 437 | $dateTo = new DateField( |
||
| 438 | 'q[LastEditedTo]', |
||
| 439 | _t('CMSSearch.FILTERDATETO', 'To') |
||
| 440 | ); |
||
| 441 | $pageFilter = new DropdownField( |
||
| 442 | 'q[FilterClass]', |
||
| 443 | _t('CMSMain.PAGES', 'Page status'), |
||
| 444 | CMSSiteTreeFilter::get_all_filters() |
||
| 445 | ); |
||
| 446 | $pageClasses = new DropdownField( |
||
| 447 | 'q[ClassName]', |
||
| 448 | _t('CMSMain.PAGETYPEOPT', 'Page type', 'Dropdown for limiting search to a page type'), |
||
| 449 | $this->getPageTypes() |
||
| 450 | ); |
||
| 451 | $pageClasses->setEmptyString(_t('CMSMain.PAGETYPEANYOPT', 'Any')); |
||
| 452 | |||
| 453 | // Group the Datefields |
||
| 454 | $dateGroup = new FieldGroup( |
||
| 455 | $dateFrom, |
||
| 456 | $dateTo |
||
| 457 | ); |
||
| 458 | $dateGroup->setTitle(_t('CMSSearch.PAGEFILTERDATEHEADING', 'Last edited')); |
||
| 459 | |||
| 460 | // view mode |
||
| 461 | $viewMode = HiddenField::create('view', false, $this->ViewState()); |
||
| 462 | |||
| 463 | // Create the Field list |
||
| 464 | $fields = new FieldList( |
||
| 465 | $content, |
||
| 466 | $pageFilter, |
||
| 467 | $pageClasses, |
||
| 468 | $dateGroup, |
||
| 469 | $viewMode |
||
| 470 | ); |
||
| 471 | |||
| 472 | // Create the Search and Reset action |
||
| 473 | $actions = new FieldList( |
||
| 474 | FormAction::create('doSearch', _t('CMSMain_left_ss.APPLY_FILTER', 'Search')) |
||
| 475 | ->addExtraClass('btn btn-primary'), |
||
| 476 | ResetFormAction::create('clear', _t('CMSMain_left_ss.CLEAR_FILTER', 'Clear')) |
||
| 477 | ->addExtraClass('btn btn-secondary') |
||
| 478 | ); |
||
| 479 | |||
| 480 | // Use <button> to allow full jQuery UI styling on the all of the Actions |
||
| 481 | /** @var FormAction $action */ |
||
| 482 | foreach ($actions->dataFields() as $action) { |
||
| 483 | /** @var FormAction $action */ |
||
| 484 | $action->setUseButtonTag(true); |
||
| 485 | } |
||
| 486 | |||
| 487 | // Create the form |
||
| 488 | /** @skipUpgrade */ |
||
| 489 | $form = Form::create($this, 'SearchForm', $fields, $actions) |
||
| 490 | ->addExtraClass('cms-search-form') |
||
| 491 | ->setFormMethod('GET') |
||
| 492 | ->setFormAction($this->Link()) |
||
| 493 | ->disableSecurityToken() |
||
| 494 | ->unsetValidator(); |
||
| 495 | |||
| 496 | // Load the form with previously sent search data |
||
| 497 | $form->loadDataFrom($this->getRequest()->getVars()); |
||
| 498 | |||
| 499 | // Allow decorators to modify the form |
||
| 500 | $this->extend('updateSearchForm', $form); |
||
| 501 | |||
| 502 | return $form; |
||
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Returns a sorted array suitable for a dropdown with pagetypes and their translated name |
||
| 507 | * |
||
| 508 | * @return array |
||
| 509 | */ |
||
| 510 | protected function getPageTypes() |
||
| 519 | |||
| 520 | public function doSearch($data, $form) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @param bool $unlinked |
||
| 527 | * @return ArrayList |
||
| 528 | */ |
||
| 529 | public function Breadcrumbs($unlinked = false) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Create serialized JSON string with site tree hints data to be injected into |
||
| 544 | * 'data-hints' attribute of root node of jsTree. |
||
| 545 | * |
||
| 546 | * @return string Serialized JSON |
||
| 547 | */ |
||
| 548 | public function SiteTreeHints() |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Populates an array of classes in the CMS |
||
| 617 | * which allows the user to change the page type. |
||
| 618 | * |
||
| 619 | * @return SS_List |
||
| 620 | */ |
||
| 621 | public function PageTypes() |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Get a database record to be managed by the CMS. |
||
| 658 | * |
||
| 659 | * @param int $id Record ID |
||
| 660 | * @param int $versionID optional Version id of the given record |
||
| 661 | * @return SiteTree |
||
| 662 | */ |
||
| 663 | public function getRecord($id, $versionID = null) |
||
| 715 | |||
| 716 | /** |
||
| 717 | * @param int $id |
||
| 718 | * @param FieldList $fields |
||
| 719 | * @return Form |
||
| 720 | */ |
||
| 721 | public function getEditForm($id = null, $fields = null) |
||
| 722 | { |
||
| 723 | if (!$id) { |
||
| 724 | $id = $this->currentPageID(); |
||
| 725 | } |
||
| 726 | $form = parent::getEditForm($id, $fields); |
||
| 727 | |||
| 728 | // TODO Duplicate record fetching (see parent implementation) |
||
| 729 | $record = $this->getRecord($id); |
||
| 730 | if ($record && !$record->canView()) { |
||
| 731 | return Security::permissionFailure($this); |
||
| 732 | } |
||
| 733 | |||
| 734 | if (!$fields) { |
||
| 735 | $fields = $form->Fields(); |
||
| 736 | } |
||
| 737 | $actions = $form->Actions(); |
||
| 738 | |||
| 739 | if ($record) { |
||
| 740 | $deletedFromStage = !$record->isOnDraft(); |
||
| 741 | |||
| 742 | $fields->push($idField = new HiddenField("ID", false, $id)); |
||
| 743 | // Necessary for different subsites |
||
| 744 | $fields->push($liveLinkField = new HiddenField("AbsoluteLink", false, $record->AbsoluteLink())); |
||
| 745 | $fields->push($liveLinkField = new HiddenField("LiveLink")); |
||
| 746 | $fields->push($stageLinkField = new HiddenField("StageLink")); |
||
| 747 | $fields->push($archiveWarningMsgField = new HiddenField("ArchiveWarningMessage")); |
||
| 748 | $fields->push(new HiddenField("TreeTitle", false, $record->TreeTitle)); |
||
| 749 | |||
| 750 | $archiveWarningMsgField->setValue($this->getArchiveWarningMessage($record)); |
||
| 751 | |||
| 752 | if ($record->ID && is_numeric($record->ID)) { |
||
| 753 | $liveLink = $record->getAbsoluteLiveLink(); |
||
| 754 | if ($liveLink) { |
||
| 755 | $liveLinkField->setValue($liveLink); |
||
| 756 | } |
||
| 757 | if (!$deletedFromStage) { |
||
| 758 | $stageLink = Controller::join_links($record->AbsoluteLink(), '?stage=Stage'); |
||
| 759 | if ($stageLink) { |
||
| 760 | $stageLinkField->setValue($stageLink); |
||
| 761 | } |
||
| 762 | } |
||
| 763 | } |
||
| 764 | |||
| 765 | // Added in-line to the form, but plucked into different view by LeftAndMain.Preview.js upon load |
||
| 766 | /** @skipUpgrade */ |
||
| 767 | if ($record instanceof CMSPreviewable && !$fields->fieldByName('SilverStripeNavigator')) { |
||
| 768 | $navField = new LiteralField('SilverStripeNavigator', $this->getSilverStripeNavigator()); |
||
| 769 | $navField->setAllowHTML(true); |
||
| 770 | $fields->push($navField); |
||
| 771 | } |
||
| 772 | |||
| 773 | // getAllCMSActions can be used to completely redefine the action list |
||
| 774 | if ($record->hasMethod('getAllCMSActions')) { |
||
| 775 | $actions = $record->getAllCMSActions(); |
||
| 776 | } else { |
||
| 777 | $actions = $record->getCMSActions(); |
||
| 778 | |||
| 779 | // Find and remove action menus that have no actions. |
||
| 780 | if ($actions && $actions->count()) { |
||
| 781 | /** @var TabSet $tabset */ |
||
| 782 | $tabset = $actions->fieldByName('ActionMenus'); |
||
| 783 | if ($tabset) { |
||
| 784 | foreach ($tabset->getChildren() as $tab) { |
||
| 785 | if (!$tab->getChildren()->count()) { |
||
| 786 | $tabset->removeByName($tab->getName()); |
||
| 787 | } |
||
| 788 | } |
||
| 789 | } |
||
| 790 | } |
||
| 791 | } |
||
| 792 | |||
| 793 | // Use <button> to allow full jQuery UI styling |
||
| 794 | $actionsFlattened = $actions->dataFields(); |
||
| 795 | if ($actionsFlattened) { |
||
| 796 | /** @var FormAction $action */ |
||
| 797 | foreach ($actionsFlattened as $action) { |
||
| 798 | $action->setUseButtonTag(true); |
||
| 799 | } |
||
| 800 | } |
||
| 801 | |||
| 802 | if ($record->hasMethod('getCMSValidator')) { |
||
| 803 | $validator = $record->getCMSValidator(); |
||
| 804 | } else { |
||
| 805 | $validator = new RequiredFields(); |
||
| 806 | } |
||
| 807 | |||
| 808 | // TODO Can't merge $FormAttributes in template at the moment |
||
| 809 | $form->addExtraClass('center ' . $this->BaseCSSClasses()); |
||
| 810 | // Set validation exemptions for specific actions |
||
| 811 | $form->setValidationExemptActions(array('restore', 'revert', 'deletefromlive', 'delete', 'unpublish', 'rollback', 'doRollback')); |
||
| 812 | |||
| 813 | // Announce the capability so the frontend can decide whether to allow preview or not. |
||
| 814 | if ($record instanceof CMSPreviewable) { |
||
| 815 | $form->addExtraClass('cms-previewable'); |
||
| 816 | } |
||
| 817 | $form->addExtraClass('fill-height flexbox-area-grow'); |
||
| 818 | |||
| 819 | if (!$record->canEdit() || $deletedFromStage) { |
||
| 820 | $readonlyFields = $form->Fields()->makeReadonly(); |
||
| 821 | $form->setFields($readonlyFields); |
||
| 822 | } |
||
| 823 | |||
| 824 | $form->Fields()->setForm($form); |
||
| 825 | |||
| 826 | $this->extend('updateEditForm', $form); |
||
| 827 | return $form; |
||
| 828 | } elseif ($id) { |
||
| 829 | $form = Form::create($this, "EditForm", new FieldList( |
||
| 830 | new LabelField('PageDoesntExistLabel', _t('CMSMain.PAGENOTEXISTS', "This page doesn't exist")) |
||
| 831 | ), new FieldList())->setHTMLID('Form_EditForm'); |
||
| 832 | return $form; |
||
| 833 | } |
||
| 834 | } |
||
| 835 | |||
| 836 | protected function getArchiveWarningMessage($record) |
||
| 837 | { |
||
| 838 | // Get all page's descendants |
||
| 839 | $record->collateDescendants(true, $descendants); |
||
| 840 | if (!$descendants) { |
||
| 841 | $descendants = []; |
||
| 842 | } |
||
| 843 | |||
| 844 | // Get all campaigns that the page and its descendants belong to |
||
| 845 | $inChangeSetIDs = ChangeSetItem::get_for_object($record)->column('ChangeSetID'); |
||
| 846 | |||
| 847 | foreach ($descendants as $page) { |
||
| 848 | $inChangeSetIDs = array_merge($inChangeSetIDs, ChangeSetItem::get_for_object($page)->column('ChangeSetID')); |
||
| 849 | } |
||
| 850 | |||
| 851 | if (count($inChangeSetIDs) > 0) { |
||
| 852 | $inChangeSets = ChangeSet::get()->filter(['ID' => $inChangeSetIDs, 'State' => ChangeSet::STATE_OPEN]); |
||
| 853 | } else { |
||
| 854 | $inChangeSets = new ArrayList(); |
||
| 855 | } |
||
| 856 | |||
| 857 | $numCampaigns = ChangeSet::singleton()->i18n_pluralise($inChangeSets->count()); |
||
| 858 | $numCampaigns = mb_strtolower($numCampaigns); |
||
| 859 | |||
| 860 | if (count($descendants) > 0 && $inChangeSets->count() > 0) { |
||
| 861 | $archiveWarningMsg = _t('CMSMain.ArchiveWarningWithChildrenAndCampaigns', 'Warning: This page and all of its child pages will be unpublished and automatically removed from their associated {NumCampaigns} before being sent to the archive.\n\nAre you sure you want to proceed?', [ 'NumCampaigns' => $numCampaigns ]); |
||
| 862 | } elseif (count($descendants) > 0) { |
||
| 863 | $archiveWarningMsg = _t('CMSMain.ArchiveWarningWithChildren', 'Warning: This page and all of its child pages will be unpublished before being sent to the archive.\n\nAre you sure you want to proceed?'); |
||
| 864 | } elseif ($inChangeSets->count() > 0) { |
||
| 865 | $archiveWarningMsg = _t('CMSMain.ArchiveWarningWithCampaigns', 'Warning: This page will be unpublished and automatically removed from their associated {NumCampaigns} before being sent to the archive.\n\nAre you sure you want to proceed?', [ 'NumCampaigns' => $numCampaigns ]); |
||
| 866 | } else { |
||
| 867 | $archiveWarningMsg = _t('CMSMain.ArchiveWarning', 'Warning: This page will be unpublished before being sent to the archive.\n\nAre you sure you want to proceed?'); |
||
| 868 | } |
||
| 869 | |||
| 870 | return $archiveWarningMsg; |
||
| 871 | } |
||
| 872 | |||
| 873 | /** |
||
| 874 | * @param HTTPRequest $request |
||
| 875 | * @return string HTML |
||
| 876 | */ |
||
| 877 | public function treeview($request) |
||
| 881 | |||
| 882 | /** |
||
| 883 | * @param HTTPRequest $request |
||
| 884 | * @return string HTML |
||
| 885 | */ |
||
| 886 | public function listview($request) |
||
| 890 | |||
| 891 | /** |
||
| 892 | * @return string |
||
| 893 | */ |
||
| 894 | public function ViewState() |
||
| 906 | |||
| 907 | /** |
||
| 908 | * Callback to request the list of page types allowed under a given page instance. |
||
| 909 | * Provides a slower but more precise response over SiteTreeHints |
||
| 910 | * |
||
| 911 | * @param HTTPRequest $request |
||
| 912 | * @return HTTPResponse |
||
| 913 | */ |
||
| 914 | public function childfilter($request) |
||
| 944 | |||
| 945 | /** |
||
| 946 | * Safely reconstruct a selected filter from a given set of query parameters |
||
| 947 | * |
||
| 948 | * @param array $params Query parameters to use |
||
| 949 | * @return CMSSiteTreeFilter The filter class, or null if none present |
||
| 950 | * @throws InvalidArgumentException if invalid filter class is passed. |
||
| 951 | */ |
||
| 952 | protected function getQueryFilter($params) |
||
| 963 | |||
| 964 | /** |
||
| 965 | * Returns the pages meet a certain criteria as {@see CMSSiteTreeFilter} or the subpages of a parent page |
||
| 966 | * defaulting to no filter and show all pages in first level. |
||
| 967 | * Doubles as search results, if any search parameters are set through {@link SearchForm()}. |
||
| 968 | * |
||
| 969 | * @param array $params Search filter criteria |
||
| 970 | * @param int $parentID Optional parent node to filter on (can't be combined with other search criteria) |
||
| 971 | * @return SS_List |
||
| 972 | * @throws InvalidArgumentException if invalid filter class is passed. |
||
| 973 | */ |
||
| 974 | public function getList($params = array(), $parentID = 0) |
||
| 984 | |||
| 985 | /** |
||
| 986 | * @return Form |
||
| 987 | */ |
||
| 988 | public function ListViewForm() |
||
| 1085 | |||
| 1086 | public function currentPageID() |
||
| 1094 | |||
| 1095 | //------------------------------------------------------------------------------------------// |
||
| 1096 | // Data saving handlers |
||
| 1097 | |||
| 1098 | /** |
||
| 1099 | * Save and Publish page handler |
||
| 1100 | * |
||
| 1101 | * @param array $data |
||
| 1102 | * @param Form $form |
||
| 1103 | * @return HTTPResponse |
||
| 1104 | * @throws HTTPResponse_Exception |
||
| 1105 | */ |
||
| 1106 | public function save($data, $form) |
||
| 1173 | |||
| 1174 | /** |
||
| 1175 | * @uses LeftAndMainExtension->augmentNewSiteTreeItem() |
||
| 1176 | * |
||
| 1177 | * @param int|string $id |
||
| 1178 | * @param bool $setID |
||
| 1179 | * @return mixed|DataObject |
||
| 1180 | * @throws HTTPResponse_Exception |
||
| 1181 | */ |
||
| 1182 | public function getNewItem($id, $setID = true) |
||
| 1233 | |||
| 1234 | /** |
||
| 1235 | * Actually perform the publication step |
||
| 1236 | * |
||
| 1237 | * @param Versioned|DataObject $record |
||
| 1238 | * @return mixed |
||
| 1239 | */ |
||
| 1240 | public function performPublish($record) |
||
| 1248 | |||
| 1249 | /** |
||
| 1250 | * Reverts a page by publishing it to live. |
||
| 1251 | * Use {@link restorepage()} if you want to restore a page |
||
| 1252 | * which was deleted from draft without publishing. |
||
| 1253 | * |
||
| 1254 | * @uses SiteTree->doRevertToLive() |
||
| 1255 | * |
||
| 1256 | * @param array $data |
||
| 1257 | * @param Form $form |
||
| 1258 | * @return HTTPResponse |
||
| 1259 | * @throws HTTPResponse_Exception |
||
| 1260 | */ |
||
| 1261 | public function revert($data, $form) |
||
| 1301 | |||
| 1302 | /** |
||
| 1303 | * Delete the current page from draft stage. |
||
| 1304 | * |
||
| 1305 | * @see deletefromlive() |
||
| 1306 | * |
||
| 1307 | * @param array $data |
||
| 1308 | * @param Form $form |
||
| 1309 | * @return HTTPResponse |
||
| 1310 | * @throws HTTPResponse_Exception |
||
| 1311 | */ |
||
| 1312 | View Code Duplication | public function delete($data, $form) |
|
| 1334 | |||
| 1335 | /** |
||
| 1336 | * Delete this page from both live and stage |
||
| 1337 | * |
||
| 1338 | * @param array $data |
||
| 1339 | * @param Form $form |
||
| 1340 | * @return HTTPResponse |
||
| 1341 | * @throws HTTPResponse_Exception |
||
| 1342 | */ |
||
| 1343 | View Code Duplication | public function archive($data, $form) |
|
| 1366 | |||
| 1367 | public function publish($data, $form) |
||
| 1373 | |||
| 1374 | public function unpublish($data, $form) |
||
| 1396 | |||
| 1397 | /** |
||
| 1398 | * @return HTTPResponse |
||
| 1399 | */ |
||
| 1400 | public function rollback() |
||
| 1407 | |||
| 1408 | /** |
||
| 1409 | * Rolls a site back to a given version ID |
||
| 1410 | * |
||
| 1411 | * @param array $data |
||
| 1412 | * @param Form $form |
||
| 1413 | * @return HTTPResponse |
||
| 1414 | */ |
||
| 1415 | public function doRollback($data, $form) |
||
| 1455 | |||
| 1456 | /** |
||
| 1457 | * Batch Actions Handler |
||
| 1458 | */ |
||
| 1459 | public function batchactions() |
||
| 1463 | |||
| 1464 | public function BatchActionParameters() |
||
| 1486 | /** |
||
| 1487 | * Returns a list of batch actions |
||
| 1488 | */ |
||
| 1489 | public function BatchActionList() |
||
| 1493 | |||
| 1494 | public function publishall($request) |
||
| 1558 | |||
| 1559 | /** |
||
| 1560 | * Restore a completely deleted page from the SiteTree_versions table. |
||
| 1561 | * |
||
| 1562 | * @param array $data |
||
| 1563 | * @param Form $form |
||
| 1564 | * @return HTTPResponse |
||
| 1565 | */ |
||
| 1566 | public function restore($data, $form) |
||
| 1592 | |||
| 1593 | public function duplicate($request) |
||
| 1636 | |||
| 1637 | public function duplicatewithchildren($request) |
||
| 1674 | |||
| 1675 | public function providePermissions() |
||
| 1690 | } |
||
| 1691 |