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 |
||
| 14 | class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionProvider { |
||
| 15 | |||
| 16 | private static $url_segment = 'pages'; |
||
|
|
|||
| 17 | |||
| 18 | private static $url_rule = '/$Action/$ID/$OtherID'; |
||
| 19 | |||
| 20 | // Maintain a lower priority than other administration sections |
||
| 21 | // so that Director does not think they are actions of CMSMain |
||
| 22 | private static $url_priority = 39; |
||
| 23 | |||
| 24 | private static $menu_title = 'Edit Page'; |
||
| 25 | |||
| 26 | private static $menu_priority = 10; |
||
| 27 | |||
| 28 | private static $tree_class = "SiteTree"; |
||
| 29 | |||
| 30 | private static $subitem_class = "Member"; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Amount of results showing on a single page. |
||
| 34 | * |
||
| 35 | * @config |
||
| 36 | * @var int |
||
| 37 | */ |
||
| 38 | private static $page_length = 15; |
||
| 39 | |||
| 40 | private static $allowed_actions = array( |
||
| 41 | 'archive', |
||
| 42 | 'buildbrokenlinks', |
||
| 43 | 'deleteitems', |
||
| 44 | 'DeleteItemsForm', |
||
| 45 | 'dialog', |
||
| 46 | 'duplicate', |
||
| 47 | 'duplicatewithchildren', |
||
| 48 | 'publishall', |
||
| 49 | 'publishitems', |
||
| 50 | 'PublishItemsForm', |
||
| 51 | 'submit', |
||
| 52 | 'EditForm', |
||
| 53 | 'SearchForm', |
||
| 54 | 'SiteTreeAsUL', |
||
| 55 | 'getshowdeletedsubtree', |
||
| 56 | 'batchactions', |
||
| 57 | 'treeview', |
||
| 58 | 'listview', |
||
| 59 | 'ListViewForm', |
||
| 60 | 'childfilter', |
||
| 61 | ); |
||
| 62 | |||
| 63 | public function init() { |
||
| 64 | // set reading lang |
||
| 65 | if(SiteTree::has_extension('Translatable') && !$this->getRequest()->isAjax()) { |
||
| 66 | Translatable::choose_site_locale(array_keys(Translatable::get_existing_content_languages('SiteTree'))); |
||
| 67 | } |
||
| 68 | |||
| 69 | parent::init(); |
||
| 70 | |||
| 71 | Requirements::css(CMS_DIR . '/client/dist/styles/bundle.css'); |
||
| 72 | Requirements::customCSS($this->generatePageIconsCss()); |
||
| 73 | Requirements::add_i18n_javascript(CMS_DIR . '/client/src/lang', false, true); |
||
| 74 | Requirements::javascript(CMS_DIR . '/client/dist/js/bundle-legacy.js', [ |
||
| 75 | 'provides' => [ |
||
| 76 | CMS_DIR . '/client/dist/js/CMSMain.AddForm.js', |
||
| 77 | CMS_DIR . '/client/dist/js/CMSMain.EditForm.js', |
||
| 78 | CMS_DIR . '/client/dist/js/CMSMain.js', |
||
| 79 | CMS_DIR . '/client/dist/js/CMSMain.Tree.js', |
||
| 80 | CMS_DIR . '/client/dist/js/CMSPageHistoryController.js', |
||
| 81 | CMS_DIR . '/client/dist/js/RedirectorPage.js', |
||
| 82 | CMS_DIR . '/client/dist/js/SilverStripeNavigator.js', |
||
| 83 | CMS_DIR . '/client/dist/js/SiteTreeURLSegmentField.js' |
||
| 84 | ] |
||
| 85 | ]); |
||
| 86 | |||
| 87 | CMSBatchActionHandler::register('publish', 'CMSBatchAction_Publish'); |
||
| 88 | CMSBatchActionHandler::register('unpublish', 'CMSBatchAction_Unpublish'); |
||
| 89 | CMSBatchActionHandler::register('delete', 'CMSBatchAction_Delete'); |
||
| 90 | CMSBatchActionHandler::register('archive', 'CMSBatchAction_Archive'); |
||
| 91 | CMSBatchActionHandler::register('restore', 'CMSBatchAction_Restore'); |
||
| 92 | } |
||
| 93 | |||
| 94 | public function index($request) { |
||
| 95 | // In case we're not showing a specific record, explicitly remove any session state, |
||
| 96 | // to avoid it being highlighted in the tree, and causing an edit form to show. |
||
| 97 | if(!$request->param('Action')) $this->setCurrentPageId(null); |
||
| 98 | |||
| 99 | return parent::index($request); |
||
| 100 | } |
||
| 101 | |||
| 102 | public function getResponseNegotiator() { |
||
| 103 | $negotiator = parent::getResponseNegotiator(); |
||
| 104 | $controller = $this; |
||
| 105 | $negotiator->setCallback('ListViewForm', function() use(&$controller) { |
||
| 106 | return $controller->ListViewForm()->forTemplate(); |
||
| 107 | }); |
||
| 108 | return $negotiator; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * If this is set to true, the "switchView" context in the |
||
| 113 | * template is shown, with links to the staging and publish site. |
||
| 114 | * |
||
| 115 | * @return boolean |
||
| 116 | */ |
||
| 117 | public function ShowSwitchView() { |
||
| 118 | return true; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Overloads the LeftAndMain::ShowView. Allows to pass a page as a parameter, so we are able |
||
| 123 | * to switch view also for archived versions. |
||
| 124 | */ |
||
| 125 | public function SwitchView($page = null) { |
||
| 126 | if(!$page) { |
||
| 127 | $page = $this->currentPage(); |
||
| 128 | } |
||
| 129 | |||
| 130 | if($page) { |
||
| 131 | $nav = SilverStripeNavigator::get_for_record($page); |
||
| 132 | return $nav['items']; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | //------------------------------------------------------------------------------------------// |
||
| 137 | // Main controllers |
||
| 138 | |||
| 139 | //------------------------------------------------------------------------------------------// |
||
| 140 | // Main UI components |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Override {@link LeftAndMain} Link to allow blank URL segment for CMSMain. |
||
| 144 | * |
||
| 145 | * @param string|null $action Action to link to. |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | public function Link($action = null) { |
||
| 149 | $link = Controller::join_links( |
||
| 150 | $this->stat('url_base', true), |
||
| 151 | $this->stat('url_segment', true), // in case we want to change the segment |
||
| 152 | '/', // trailing slash needed if $action is null! |
||
| 153 | "$action" |
||
| 154 | ); |
||
| 155 | $this->extend('updateLink', $link); |
||
| 156 | return $link; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function LinkPages() { |
||
| 160 | return singleton('CMSPagesController')->Link(); |
||
| 161 | } |
||
| 162 | |||
| 163 | public function LinkPagesWithSearch() { |
||
| 164 | return $this->LinkWithSearch($this->LinkPages()); |
||
| 165 | } |
||
| 166 | |||
| 167 | public function LinkTreeView() { |
||
| 168 | return $this->LinkWithSearch(singleton('CMSMain')->Link('treeview')); |
||
| 169 | } |
||
| 170 | |||
| 171 | public function LinkListView() { |
||
| 172 | return $this->LinkWithSearch(singleton('CMSMain')->Link('listview')); |
||
| 173 | } |
||
| 174 | |||
| 175 | public function LinkGalleryView() { |
||
| 176 | return $this->LinkWithSearch(singleton('CMSMain')->Link('galleryview')); |
||
| 177 | } |
||
| 178 | |||
| 179 | public function LinkPageEdit($id = null) { |
||
| 180 | if(!$id) $id = $this->currentPageID(); |
||
| 181 | return $this->LinkWithSearch( |
||
| 182 | Controller::join_links(singleton('CMSPageEditController')->Link('show'), $id) |
||
| 183 | ); |
||
| 184 | } |
||
| 185 | |||
| 186 | View Code Duplication | public function LinkPageSettings() { |
|
| 187 | if($id = $this->currentPageID()) { |
||
| 188 | return $this->LinkWithSearch( |
||
| 189 | Controller::join_links(singleton('CMSPageSettingsController')->Link('show'), $id) |
||
| 190 | ); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | View Code Duplication | public function LinkPageHistory() { |
|
| 195 | if($id = $this->currentPageID()) { |
||
| 196 | return $this->LinkWithSearch( |
||
| 197 | Controller::join_links(singleton('CMSPageHistoryController')->Link('show'), $id) |
||
| 198 | ); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | public function LinkWithSearch($link) { |
||
| 203 | // Whitelist to avoid side effects |
||
| 204 | $params = array( |
||
| 205 | 'q' => (array)$this->getRequest()->getVar('q'), |
||
| 206 | 'ParentID' => $this->getRequest()->getVar('ParentID') |
||
| 207 | ); |
||
| 208 | $link = Controller::join_links( |
||
| 209 | $link, |
||
| 210 | array_filter(array_values($params)) ? '?' . http_build_query($params) : null |
||
| 211 | ); |
||
| 212 | $this->extend('updateLinkWithSearch', $link); |
||
| 213 | return $link; |
||
| 214 | } |
||
| 215 | |||
| 216 | public function LinkPageAdd($extra = null, $placeholders = null) { |
||
| 217 | $link = singleton("CMSPageAddController")->Link(); |
||
| 218 | $this->extend('updateLinkPageAdd', $link); |
||
| 219 | |||
| 220 | if($extra) { |
||
| 221 | $link = Controller::join_links ($link, $extra); |
||
| 222 | } |
||
| 223 | |||
| 224 | if($placeholders) { |
||
| 225 | $link .= (strpos($link, '?') === false ? "?$placeholders" : "&$placeholders"); |
||
| 226 | } |
||
| 227 | |||
| 228 | return $link; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | public function LinkPreview() { |
||
| 235 | $record = $this->getRecord($this->currentPageID()); |
||
| 236 | $baseLink = Director::absoluteBaseURL(); |
||
| 237 | if ($record && $record instanceof Page) { |
||
| 238 | // if we are an external redirector don't show a link |
||
| 239 | if ($record instanceof RedirectorPage && $record->RedirectionType == 'External') { |
||
| 240 | $baseLink = false; |
||
| 241 | } |
||
| 242 | else { |
||
| 243 | $baseLink = $record->Link('?stage=Stage'); |
||
| 244 | } |
||
| 245 | } |
||
| 246 | return $baseLink; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Return the entire site tree as a nested set of ULs |
||
| 251 | */ |
||
| 252 | public function SiteTreeAsUL() { |
||
| 253 | // Pre-cache sitetree version numbers for querying efficiency |
||
| 254 | Versioned::prepopulate_versionnumber_cache("SiteTree", "Stage"); |
||
| 255 | Versioned::prepopulate_versionnumber_cache("SiteTree", "Live"); |
||
| 256 | $html = $this->getSiteTreeFor($this->stat('tree_class')); |
||
| 257 | |||
| 258 | $this->extend('updateSiteTreeAsUL', $html); |
||
| 259 | |||
| 260 | return $html; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @return boolean |
||
| 265 | */ |
||
| 266 | public function TreeIsFiltered() { |
||
| 267 | $query = $this->getRequest()->getVar('q'); |
||
| 268 | |||
| 269 | if (!$query || (count($query) === 1 && isset($query['FilterClass']) && $query['FilterClass'] === 'CMSSiteTreeFilter_Search')) { |
||
| 270 | return false; |
||
| 271 | } |
||
| 272 | |||
| 273 | return true; |
||
| 274 | } |
||
| 275 | |||
| 276 | public function ExtraTreeTools() { |
||
| 277 | $html = ''; |
||
| 278 | $this->extend('updateExtraTreeTools', $html); |
||
| 279 | return $html; |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Returns a Form for page searching for use in templates. |
||
| 284 | * |
||
| 285 | * Can be modified from a decorator by a 'updateSearchForm' method |
||
| 286 | * |
||
| 287 | * @return Form |
||
| 288 | */ |
||
| 289 | public function SearchForm() { |
||
| 290 | // Create the fields |
||
| 291 | $content = new TextField('q[Term]', _t('CMSSearch.FILTERLABELTEXT', 'Search')); |
||
| 292 | $dateHeader = new HeaderField('q[Date]', _t('CMSSearch.PAGEFILTERDATEHEADING', 'Last edited'), 4); |
||
| 293 | $dateFrom = new DateField( |
||
| 294 | 'q[LastEditedFrom]', |
||
| 295 | _t('CMSSearch.FILTERDATEFROM', 'From') |
||
| 296 | ); |
||
| 297 | $dateFrom->setConfig('showcalendar', true); |
||
| 298 | $dateTo = new DateField( |
||
| 299 | 'q[LastEditedTo]', |
||
| 300 | _t('CMSSearch.FILTERDATETO', 'To') |
||
| 301 | ); |
||
| 302 | $dateTo->setConfig('showcalendar', true); |
||
| 303 | $pageFilter = new DropdownField( |
||
| 304 | 'q[FilterClass]', |
||
| 305 | _t('CMSMain.PAGES', 'Page status'), |
||
| 306 | CMSSiteTreeFilter::get_all_filters() |
||
| 307 | ); |
||
| 308 | $pageClasses = new DropdownField( |
||
| 309 | 'q[ClassName]', |
||
| 310 | _t('CMSMain.PAGETYPEOPT', 'Page type', 'Dropdown for limiting search to a page type'), |
||
| 311 | $this->getPageTypes() |
||
| 312 | ); |
||
| 313 | $pageClasses->setEmptyString(_t('CMSMain.PAGETYPEANYOPT','Any')); |
||
| 314 | |||
| 315 | // Group the Datefields |
||
| 316 | $dateGroup = new FieldGroup( |
||
| 317 | $dateHeader, |
||
| 318 | $dateFrom, |
||
| 319 | $dateTo |
||
| 320 | ); |
||
| 321 | $dateGroup->setFieldHolderTemplate('FieldGroup_DefaultFieldHolder')->addExtraClass('stacked'); |
||
| 322 | |||
| 323 | // Create the Field list |
||
| 324 | $fields = new FieldList( |
||
| 325 | $content, |
||
| 326 | $dateGroup, |
||
| 327 | $pageFilter, |
||
| 328 | $pageClasses |
||
| 329 | ); |
||
| 330 | |||
| 331 | // Create the Search and Reset action |
||
| 332 | $actions = new FieldList( |
||
| 333 | FormAction::create('doSearch', _t('CMSMain_left_ss.APPLY_FILTER', 'Search')) |
||
| 334 | ->addExtraClass('ss-ui-action-constructive'), |
||
| 335 | Object::create('ResetFormAction', 'clear', _t('CMSMain_left_ss.CLEAR_FILTER', 'Clear')) |
||
| 336 | ); |
||
| 337 | |||
| 338 | // Use <button> to allow full jQuery UI styling on the all of the Actions |
||
| 339 | foreach($actions->dataFields() as $action) { |
||
| 340 | $action->setUseButtonTag(true); |
||
| 341 | } |
||
| 342 | |||
| 343 | // Create the form |
||
| 344 | $form = Form::create($this, 'SearchForm', $fields, $actions) |
||
| 345 | ->addExtraClass('cms-search-form') |
||
| 346 | ->setFormMethod('GET') |
||
| 347 | ->setFormAction($this->Link()) |
||
| 348 | ->disableSecurityToken() |
||
| 349 | ->unsetValidator(); |
||
| 350 | |||
| 351 | // Load the form with previously sent search data |
||
| 352 | $form->loadDataFrom($this->getRequest()->getVars()); |
||
| 353 | |||
| 354 | // Allow decorators to modify the form |
||
| 355 | $this->extend('updateSearchForm', $form); |
||
| 356 | |||
| 357 | return $form; |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Returns a sorted array suitable for a dropdown with pagetypes and their translated name |
||
| 362 | * |
||
| 363 | * @return array |
||
| 364 | */ |
||
| 365 | protected function getPageTypes() { |
||
| 366 | $pageTypes = array(); |
||
| 367 | foreach(SiteTree::page_type_classes() as $pageTypeClass) { |
||
| 368 | $pageTypes[$pageTypeClass] = _t($pageTypeClass.'.SINGULARNAME', $pageTypeClass); |
||
| 369 | } |
||
| 370 | asort($pageTypes); |
||
| 371 | return $pageTypes; |
||
| 372 | } |
||
| 373 | |||
| 374 | public function doSearch($data, $form) { |
||
| 375 | return $this->getsubtree($this->getRequest()); |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @param bool $unlinked |
||
| 380 | * @return ArrayList |
||
| 381 | */ |
||
| 382 | public function Breadcrumbs($unlinked = false) { |
||
| 383 | $items = parent::Breadcrumbs($unlinked); |
||
| 384 | |||
| 385 | if($items->count() > 1) { |
||
| 386 | // Specific to the SiteTree admin section, we never show the cms section and current |
||
| 387 | // page in the same breadcrumbs block. |
||
| 388 | $items->shift(); |
||
| 389 | } |
||
| 390 | |||
| 391 | return $items; |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Create serialized JSON string with site tree hints data to be injected into |
||
| 396 | * 'data-hints' attribute of root node of jsTree. |
||
| 397 | * |
||
| 398 | * @return string Serialized JSON |
||
| 399 | */ |
||
| 400 | public function SiteTreeHints() { |
||
| 401 | $json = ''; |
||
| 402 | $classes = SiteTree::page_type_classes(); |
||
| 403 | |||
| 404 | $cacheCanCreate = array(); |
||
| 405 | foreach($classes as $class) $cacheCanCreate[$class] = singleton($class)->canCreate(); |
||
| 406 | |||
| 407 | // Generate basic cache key. Too complex to encompass all variations |
||
| 408 | $cache = SS_Cache::factory('CMSMain_SiteTreeHints'); |
||
| 409 | $cacheKey = md5(implode('_', array(Member::currentUserID(), implode(',', $cacheCanCreate), implode(',', $classes)))); |
||
| 410 | if($this->getRequest()->getVar('flush')) $cache->clean(Zend_Cache::CLEANING_MODE_ALL); |
||
| 411 | $json = $cache->load($cacheKey); |
||
| 412 | if(!$json) { |
||
| 413 | $def['Root'] = array(); |
||
| 414 | $def['Root']['disallowedChildren'] = array(); |
||
| 415 | |||
| 416 | // Contains all possible classes to support UI controls listing them all, |
||
| 417 | // such as the "add page here" context menu. |
||
| 418 | $def['All'] = array(); |
||
| 419 | |||
| 420 | // Identify disallows and set globals |
||
| 421 | foreach($classes as $class) { |
||
| 422 | $obj = singleton($class); |
||
| 423 | if($obj instanceof HiddenClass) continue; |
||
| 424 | |||
| 425 | // Name item |
||
| 426 | $def['All'][$class] = array( |
||
| 427 | 'title' => $obj->i18n_singular_name() |
||
| 428 | ); |
||
| 429 | |||
| 430 | // Check if can be created at the root |
||
| 431 | $needsPerm = $obj->stat('need_permission'); |
||
| 432 | if( |
||
| 433 | !$obj->stat('can_be_root') |
||
| 434 | || (!array_key_exists($class, $cacheCanCreate) || !$cacheCanCreate[$class]) |
||
| 435 | || ($needsPerm && !$this->can($needsPerm)) |
||
| 436 | ) { |
||
| 437 | $def['Root']['disallowedChildren'][] = $class; |
||
| 438 | } |
||
| 439 | |||
| 440 | // Hint data specific to the class |
||
| 441 | $def[$class] = array(); |
||
| 442 | |||
| 443 | $defaultChild = $obj->defaultChild(); |
||
| 444 | if($defaultChild !== 'Page' && $defaultChild !== null) { |
||
| 445 | $def[$class]['defaultChild'] = $defaultChild; |
||
| 446 | } |
||
| 447 | |||
| 448 | $defaultParent = $obj->defaultParent(); |
||
| 449 | if ($defaultParent !== 1 && $defaultParent !== null) { |
||
| 450 | $def[$class]['defaultParent'] = $defaultParent; |
||
| 451 | } |
||
| 452 | } |
||
| 453 | |||
| 454 | $this->extend('updateSiteTreeHints', $def); |
||
| 455 | |||
| 456 | $json = Convert::raw2json($def); |
||
| 457 | $cache->save($json, $cacheKey); |
||
| 458 | } |
||
| 459 | return $json; |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Populates an array of classes in the CMS |
||
| 464 | * which allows the user to change the page type. |
||
| 465 | * |
||
| 466 | * @return SS_List |
||
| 467 | */ |
||
| 468 | public function PageTypes() { |
||
| 469 | $classes = SiteTree::page_type_classes(); |
||
| 470 | |||
| 471 | $result = new ArrayList(); |
||
| 472 | |||
| 473 | foreach($classes as $class) { |
||
| 474 | $instance = singleton($class); |
||
| 475 | |||
| 476 | if($instance instanceof HiddenClass) continue; |
||
| 477 | |||
| 478 | // skip this type if it is restricted |
||
| 479 | if($instance->stat('need_permission') && !$this->can(singleton($class)->stat('need_permission'))) continue; |
||
| 480 | |||
| 481 | $addAction = $instance->i18n_singular_name(); |
||
| 482 | |||
| 483 | // Get description (convert 'Page' to 'SiteTree' for correct localization lookups) |
||
| 484 | $description = _t((($class == 'Page') ? 'SiteTree' : $class) . '.DESCRIPTION'); |
||
| 485 | |||
| 486 | if(!$description) { |
||
| 487 | $description = $instance->uninherited('description'); |
||
| 488 | } |
||
| 489 | |||
| 490 | if($class == 'Page' && !$description) { |
||
| 491 | $description = singleton('SiteTree')->uninherited('description'); |
||
| 492 | } |
||
| 493 | |||
| 494 | $result->push(new ArrayData(array( |
||
| 495 | 'ClassName' => $class, |
||
| 496 | 'AddAction' => $addAction, |
||
| 497 | 'Description' => $description, |
||
| 498 | // TODO Sprite support |
||
| 499 | 'IconURL' => $instance->stat('icon'), |
||
| 500 | 'Title' => singleton($class)->i18n_singular_name(), |
||
| 501 | ))); |
||
| 502 | } |
||
| 503 | |||
| 504 | $result = $result->sort('AddAction'); |
||
| 505 | |||
| 506 | return $result; |
||
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Get a database record to be managed by the CMS. |
||
| 511 | * |
||
| 512 | * @param int $id Record ID |
||
| 513 | * @param int $versionID optional Version id of the given record |
||
| 514 | * @return SiteTree |
||
| 515 | */ |
||
| 516 | public function getRecord($id, $versionID = null) { |
||
| 517 | $treeClass = $this->stat('tree_class'); |
||
| 518 | |||
| 519 | if($id instanceof $treeClass) { |
||
| 520 | return $id; |
||
| 521 | } |
||
| 522 | else if($id && is_numeric($id)) { |
||
| 523 | if($this->getRequest()->getVar('Version')) { |
||
| 524 | $versionID = (int) $this->getRequest()->getVar('Version'); |
||
| 525 | } |
||
| 526 | |||
| 527 | if($versionID) { |
||
| 528 | $record = Versioned::get_version($treeClass, $id, $versionID); |
||
| 529 | } else { |
||
| 530 | $record = DataObject::get_by_id($treeClass, $id); |
||
| 531 | } |
||
| 532 | |||
| 533 | // Then, try getting a record from the live site |
||
| 534 | if(!$record) { |
||
| 535 | // $record = Versioned::get_one_by_stage($treeClass, "Live", "\"$treeClass\".\"ID\" = $id"); |
||
| 536 | Versioned::set_stage(Versioned::LIVE); |
||
| 537 | singleton($treeClass)->flushCache(); |
||
| 538 | |||
| 539 | $record = DataObject::get_by_id($treeClass, $id); |
||
| 540 | if($record) Versioned::set_reading_mode(''); |
||
| 541 | } |
||
| 542 | |||
| 543 | // Then, try getting a deleted record |
||
| 544 | if(!$record) { |
||
| 545 | $record = Versioned::get_latest_version($treeClass, $id); |
||
| 546 | } |
||
| 547 | |||
| 548 | // Don't open a page from a different locale |
||
| 549 | /** The record's Locale is saved in database in 2.4, and not related with Session, |
||
| 550 | * we should not check their locale matches the Translatable::get_current_locale, |
||
| 551 | * here as long as we all the HTTPRequest is init with right locale. |
||
| 552 | * This bit breaks the all FileIFrameField functions if the field is used in CMS |
||
| 553 | * and its relevent ajax calles, like loading the tree dropdown for TreeSelectorField. |
||
| 554 | */ |
||
| 555 | /* if($record && SiteTree::has_extension('Translatable') && $record->Locale && $record->Locale != Translatable::get_current_locale()) { |
||
| 556 | $record = null; |
||
| 557 | }*/ |
||
| 558 | |||
| 559 | return $record; |
||
| 560 | |||
| 561 | } else if(substr($id,0,3) == 'new') { |
||
| 562 | return $this->getNewItem($id); |
||
| 563 | } |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @param int $id |
||
| 568 | * @param FieldList $fields |
||
| 569 | * @return Form |
||
| 570 | */ |
||
| 571 | public function getEditForm($id = null, $fields = null) { |
||
| 572 | if(!$id) $id = $this->currentPageID(); |
||
| 573 | $form = parent::getEditForm($id, $fields); |
||
| 574 | |||
| 575 | // TODO Duplicate record fetching (see parent implementation) |
||
| 576 | $record = $this->getRecord($id); |
||
| 577 | if($record && !$record->canView()) return Security::permissionFailure($this); |
||
| 578 | |||
| 579 | if(!$fields) $fields = $form->Fields(); |
||
| 580 | $actions = $form->Actions(); |
||
| 581 | |||
| 582 | if($record) { |
||
| 583 | $deletedFromStage = $record->getIsDeletedFromStage(); |
||
| 584 | |||
| 585 | $fields->push($idField = new HiddenField("ID", false, $id)); |
||
| 586 | // Necessary for different subsites |
||
| 587 | $fields->push($liveLinkField = new HiddenField("AbsoluteLink", false, $record->AbsoluteLink())); |
||
| 588 | $fields->push($liveLinkField = new HiddenField("LiveLink")); |
||
| 589 | $fields->push($stageLinkField = new HiddenField("StageLink")); |
||
| 590 | $fields->push(new HiddenField("TreeTitle", false, $record->TreeTitle)); |
||
| 591 | |||
| 592 | if($record->ID && is_numeric( $record->ID ) ) { |
||
| 593 | $liveLink = $record->getAbsoluteLiveLink(); |
||
| 594 | if($liveLink) $liveLinkField->setValue($liveLink); |
||
| 595 | if(!$deletedFromStage) { |
||
| 596 | $stageLink = Controller::join_links($record->AbsoluteLink(), '?stage=Stage'); |
||
| 597 | if($stageLink) $stageLinkField->setValue($stageLink); |
||
| 598 | } |
||
| 599 | } |
||
| 600 | |||
| 601 | // Added in-line to the form, but plucked into different view by LeftAndMain.Preview.js upon load |
||
| 602 | if(in_array('CMSPreviewable', class_implements($record)) && !$fields->fieldByName('SilverStripeNavigator')) { |
||
| 603 | $navField = new LiteralField('SilverStripeNavigator', $this->getSilverStripeNavigator()); |
||
| 604 | $navField->setAllowHTML(true); |
||
| 605 | $fields->push($navField); |
||
| 606 | } |
||
| 607 | |||
| 608 | // getAllCMSActions can be used to completely redefine the action list |
||
| 609 | if($record->hasMethod('getAllCMSActions')) { |
||
| 610 | $actions = $record->getAllCMSActions(); |
||
| 611 | } else { |
||
| 612 | $actions = $record->getCMSActions(); |
||
| 613 | |||
| 614 | // Find and remove action menus that have no actions. |
||
| 615 | if ($actions && $actions->Count()) { |
||
| 616 | $tabset = $actions->fieldByName('ActionMenus'); |
||
| 617 | if ($tabset) { |
||
| 618 | foreach ($tabset->getChildren() as $tab) { |
||
| 619 | if (!$tab->getChildren()->count()) { |
||
| 620 | $tabset->removeByName($tab->getName()); |
||
| 621 | } |
||
| 622 | } |
||
| 623 | } |
||
| 624 | } |
||
| 625 | } |
||
| 626 | |||
| 627 | // Use <button> to allow full jQuery UI styling |
||
| 628 | $actionsFlattened = $actions->dataFields(); |
||
| 629 | if($actionsFlattened) foreach($actionsFlattened as $action) $action->setUseButtonTag(true); |
||
| 630 | |||
| 631 | if($record->hasMethod('getCMSValidator')) { |
||
| 632 | $validator = $record->getCMSValidator(); |
||
| 633 | } else { |
||
| 634 | $validator = new RequiredFields(); |
||
| 635 | } |
||
| 636 | |||
| 637 | // TODO Can't merge $FormAttributes in template at the moment |
||
| 638 | $form->addExtraClass('center ' . $this->BaseCSSClasses()); |
||
| 639 | // Set validation exemptions for specific actions |
||
| 640 | $form->setValidationExemptActions(array('restore', 'revert', 'deletefromlive', 'delete', 'unpublish', 'rollback', 'doRollback')); |
||
| 641 | |||
| 642 | // Announce the capability so the frontend can decide whether to allow preview or not. |
||
| 643 | if(in_array('CMSPreviewable', class_implements($record))) { |
||
| 644 | $form->addExtraClass('cms-previewable'); |
||
| 645 | } |
||
| 646 | |||
| 647 | if(!$record->canEdit() || $deletedFromStage) { |
||
| 648 | $readonlyFields = $form->Fields()->makeReadonly(); |
||
| 649 | $form->setFields($readonlyFields); |
||
| 650 | } |
||
| 651 | |||
| 652 | $form->Fields()->setForm($form); |
||
| 653 | |||
| 654 | $this->extend('updateEditForm', $form); |
||
| 655 | return $form; |
||
| 656 | } else if($id) { |
||
| 657 | $form = Form::create( $this, "EditForm", new FieldList( |
||
| 658 | new LabelField('PageDoesntExistLabel',_t('CMSMain.PAGENOTEXISTS',"This page doesn't exist"))), new FieldList() |
||
| 659 | )->setHTMLID('Form_EditForm'); |
||
| 660 | return $form; |
||
| 661 | } |
||
| 662 | } |
||
| 663 | |||
| 664 | /** |
||
| 665 | * @param SS_HTTPRequest $request |
||
| 666 | * @return string HTML |
||
| 667 | */ |
||
| 668 | public function treeview($request) { |
||
| 669 | return $this->renderWith($this->getTemplatesWithSuffix('_TreeView')); |
||
| 670 | } |
||
| 671 | |||
| 672 | /** |
||
| 673 | * @param SS_HTTPRequest $request |
||
| 674 | * @return string HTML |
||
| 675 | */ |
||
| 676 | public function listview($request) { |
||
| 677 | return $this->renderWith($this->getTemplatesWithSuffix('_ListView')); |
||
| 678 | } |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Callback to request the list of page types allowed under a given page instance. |
||
| 682 | * Provides a slower but more precise response over SiteTreeHints |
||
| 683 | * |
||
| 684 | * @param SS_HTTPRequest $request |
||
| 685 | * @return SS_HTTPResponse |
||
| 686 | */ |
||
| 687 | public function childfilter($request) { |
||
| 688 | // Check valid parent specified |
||
| 689 | $parentID = $request->requestVar('ParentID'); |
||
| 690 | $parent = SiteTree::get()->byID($parentID); |
||
| 691 | if(!$parent || !$parent->exists()) return $this->httpError(404); |
||
| 692 | |||
| 693 | // Build hints specific to this class |
||
| 694 | // Identify disallows and set globals |
||
| 695 | $classes = SiteTree::page_type_classes(); |
||
| 696 | $disallowedChildren = array(); |
||
| 697 | foreach($classes as $class) { |
||
| 698 | $obj = singleton($class); |
||
| 699 | if($obj instanceof HiddenClass) continue; |
||
| 700 | |||
| 701 | if(!$obj->canCreate(null, array('Parent' => $parent))) { |
||
| 702 | $disallowedChildren[] = $class; |
||
| 703 | } |
||
| 704 | } |
||
| 705 | |||
| 706 | $this->extend('updateChildFilter', $disallowedChildren, $parentID); |
||
| 707 | return $this |
||
| 708 | ->getResponse() |
||
| 709 | ->addHeader('Content-Type', 'application/json; charset=utf-8') |
||
| 710 | ->setBody(Convert::raw2json($disallowedChildren)); |
||
| 711 | } |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Safely reconstruct a selected filter from a given set of query parameters |
||
| 715 | * |
||
| 716 | * @param array $params Query parameters to use |
||
| 717 | * @return CMSSiteTreeFilter The filter class, or null if none present |
||
| 718 | * @throws InvalidArgumentException if invalid filter class is passed. |
||
| 719 | */ |
||
| 720 | protected function getQueryFilter($params) { |
||
| 721 | if(empty($params['FilterClass'])) return null; |
||
| 722 | $filterClass = $params['FilterClass']; |
||
| 723 | if(!is_subclass_of($filterClass, 'CMSSiteTreeFilter')) { |
||
| 724 | throw new InvalidArgumentException("Invalid filter class passed: {$filterClass}"); |
||
| 725 | } |
||
| 726 | return $filterClass::create($params); |
||
| 727 | } |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Returns the pages meet a certain criteria as {@see CMSSiteTreeFilter} or the subpages of a parent page |
||
| 731 | * defaulting to no filter and show all pages in first level. |
||
| 732 | * Doubles as search results, if any search parameters are set through {@link SearchForm()}. |
||
| 733 | * |
||
| 734 | * @param array $params Search filter criteria |
||
| 735 | * @param int $parentID Optional parent node to filter on (can't be combined with other search criteria) |
||
| 736 | * @return SS_List |
||
| 737 | * @throws InvalidArgumentException if invalid filter class is passed. |
||
| 738 | */ |
||
| 739 | public function getList($params = array(), $parentID = 0) { |
||
| 740 | if($filter = $this->getQueryFilter($params)) { |
||
| 741 | return $filter->getFilteredPages(); |
||
| 742 | } else { |
||
| 743 | $list = DataList::create($this->stat('tree_class')); |
||
| 744 | $parentID = is_numeric($parentID) ? $parentID : 0; |
||
| 745 | return $list->filter("ParentID", $parentID); |
||
| 746 | } |
||
| 747 | } |
||
| 748 | |||
| 749 | public function ListViewForm() { |
||
| 750 | $params = $this->getRequest()->requestVar('q'); |
||
| 751 | $list = $this->getList($params, $parentID = $this->getRequest()->requestVar('ParentID')); |
||
| 752 | $gridFieldConfig = GridFieldConfig::create()->addComponents( |
||
| 753 | new GridFieldSortableHeader(), |
||
| 754 | new GridFieldDataColumns(), |
||
| 755 | new GridFieldPaginator(self::config()->page_length) |
||
| 756 | ); |
||
| 757 | if($parentID){ |
||
| 758 | $gridFieldConfig->addComponent( |
||
| 759 | GridFieldLevelup::create($parentID) |
||
| 760 | ->setLinkSpec('?ParentID=%d&view=list') |
||
| 761 | ->setAttributes(array('data-pjax' => 'ListViewForm,Breadcrumbs')) |
||
| 762 | ); |
||
| 763 | } |
||
| 764 | $gridField = new GridField('Page','Pages', $list, $gridFieldConfig); |
||
| 765 | $columns = $gridField->getConfig()->getComponentByType('GridFieldDataColumns'); |
||
| 766 | |||
| 767 | // Don't allow navigating into children nodes on filtered lists |
||
| 768 | $fields = array( |
||
| 769 | 'getTreeTitle' => _t('SiteTree.PAGETITLE', 'Page Title'), |
||
| 770 | 'singular_name' => _t('SiteTree.PAGETYPE'), |
||
| 771 | 'LastEdited' => _t('SiteTree.LASTUPDATED', 'Last Updated'), |
||
| 772 | ); |
||
| 773 | $gridField->getConfig()->getComponentByType('GridFieldSortableHeader')->setFieldSorting(array('getTreeTitle' => 'Title')); |
||
| 774 | $gridField->getState()->ParentID = $parentID; |
||
| 775 | |||
| 776 | if(!$params) { |
||
| 777 | $fields = array_merge(array('listChildrenLink' => ''), $fields); |
||
| 778 | } |
||
| 779 | |||
| 780 | $columns->setDisplayFields($fields); |
||
| 781 | $columns->setFieldCasting(array( |
||
| 782 | 'Created' => 'SS_Datetime->Ago', |
||
| 783 | 'LastEdited' => 'SS_Datetime->FormatFromSettings', |
||
| 784 | 'getTreeTitle' => 'HTMLText' |
||
| 785 | )); |
||
| 786 | |||
| 787 | $controller = $this; |
||
| 788 | $columns->setFieldFormatting(array( |
||
| 789 | 'listChildrenLink' => function($value, &$item) use($controller) { |
||
| 790 | $num = $item ? $item->numChildren() : null; |
||
| 791 | if($num) { |
||
| 792 | return sprintf( |
||
| 793 | '<a class="cms-panel-link list-children-link" data-pjax-target="ListViewForm,Breadcrumbs" href="%s">%s</a>', |
||
| 794 | Controller::join_links( |
||
| 795 | $controller->Link(), |
||
| 796 | sprintf("?ParentID=%d&view=list", (int)$item->ID) |
||
| 797 | ), |
||
| 798 | $num |
||
| 799 | ); |
||
| 800 | } |
||
| 801 | }, |
||
| 802 | 'getTreeTitle' => function($value, &$item) use($controller) { |
||
| 803 | return sprintf( |
||
| 804 | '<a class="action-detail" href="%s">%s</a>', |
||
| 805 | Controller::join_links( |
||
| 806 | singleton('CMSPageEditController')->Link('show'), |
||
| 807 | (int)$item->ID |
||
| 808 | ), |
||
| 809 | $item->TreeTitle // returns HTML, does its own escaping |
||
| 810 | ); |
||
| 811 | } |
||
| 812 | )); |
||
| 813 | |||
| 814 | $negotiator = $this->getResponseNegotiator(); |
||
| 815 | $listview = Form::create( |
||
| 816 | $this, |
||
| 817 | 'ListViewForm', |
||
| 818 | new FieldList($gridField), |
||
| 819 | new FieldList() |
||
| 820 | )->setHTMLID('Form_ListViewForm'); |
||
| 821 | $listview->setAttribute('data-pjax-fragment', 'ListViewForm'); |
||
| 822 | View Code Duplication | $listview->setValidationResponseCallback(function() use ($negotiator, $listview) { |
|
| 823 | $request = $this->getRequest(); |
||
| 824 | if($request->isAjax() && $negotiator) { |
||
| 825 | $listview->setupFormErrors(); |
||
| 826 | $result = $listview->forTemplate(); |
||
| 827 | |||
| 828 | return $negotiator->respond($request, array( |
||
| 829 | 'CurrentForm' => function() use($result) { |
||
| 830 | return $result; |
||
| 831 | } |
||
| 832 | )); |
||
| 833 | } |
||
| 834 | }); |
||
| 835 | |||
| 836 | $this->extend('updateListView', $listview); |
||
| 837 | |||
| 838 | $listview->disableSecurityToken(); |
||
| 839 | return $listview; |
||
| 840 | } |
||
| 841 | |||
| 842 | public function currentPageID() { |
||
| 843 | $id = parent::currentPageID(); |
||
| 844 | |||
| 845 | $this->extend('updateCurrentPageID', $id); |
||
| 846 | |||
| 847 | return $id; |
||
| 848 | } |
||
| 849 | |||
| 850 | //------------------------------------------------------------------------------------------// |
||
| 851 | // Data saving handlers |
||
| 852 | |||
| 853 | /** |
||
| 854 | * Save and Publish page handler |
||
| 855 | * |
||
| 856 | * @param array $data |
||
| 857 | * @param Form $form |
||
| 858 | * @return SS_HTTPResponse |
||
| 859 | * @throws SS_HTTPResponse_Exception |
||
| 860 | */ |
||
| 861 | public function save($data, $form) { |
||
| 862 | $className = $this->stat('tree_class'); |
||
| 863 | |||
| 864 | // Existing or new record? |
||
| 865 | $id = $data['ID']; |
||
| 866 | if(substr($id,0,3) != 'new') { |
||
| 867 | /** @var SiteTree $record */ |
||
| 868 | $record = DataObject::get_by_id($className, $id); |
||
| 869 | // Check edit permissions |
||
| 870 | if($record && !$record->canEdit()) { |
||
| 871 | return Security::permissionFailure($this); |
||
| 872 | } |
||
| 873 | if(!$record || !$record->ID) { |
||
| 874 | throw new SS_HTTPResponse_Exception("Bad record ID #$id", 404); |
||
| 875 | } |
||
| 876 | } else { |
||
| 877 | if(!$className::singleton()->canCreate()) { |
||
| 878 | return Security::permissionFailure($this); |
||
| 879 | } |
||
| 880 | $record = $this->getNewItem($id, false); |
||
| 881 | } |
||
| 882 | |||
| 883 | // Check publishing permissions |
||
| 884 | $doPublish = !empty($data['publish']); |
||
| 885 | if($record && $doPublish && !$record->canPublish()) { |
||
| 886 | return Security::permissionFailure($this); |
||
| 887 | } |
||
| 888 | |||
| 889 | // TODO Coupling to SiteTree |
||
| 890 | $record->HasBrokenLink = 0; |
||
| 891 | $record->HasBrokenFile = 0; |
||
| 892 | |||
| 893 | if (!$record->ObsoleteClassName) { |
||
| 894 | $record->writeWithoutVersion(); |
||
| 895 | } |
||
| 896 | |||
| 897 | // Update the class instance if necessary |
||
| 898 | if(isset($data['ClassName']) && $data['ClassName'] != $record->ClassName) { |
||
| 899 | $newClassName = $record->ClassName; |
||
| 900 | // The records originally saved attribute was overwritten by $form->saveInto($record) before. |
||
| 901 | // This is necessary for newClassInstance() to work as expected, and trigger change detection |
||
| 902 | // on the ClassName attribute |
||
| 903 | $record->setClassName($data['ClassName']); |
||
| 904 | // Replace $record with a new instance |
||
| 905 | $record = $record->newClassInstance($newClassName); |
||
| 906 | } |
||
| 907 | |||
| 908 | // save form data into record |
||
| 909 | $form->saveInto($record); |
||
| 910 | $record->write(); |
||
| 911 | |||
| 912 | // If the 'Save & Publish' button was clicked, also publish the page |
||
| 913 | if ($doPublish) { |
||
| 914 | $record->publishRecursive(); |
||
| 915 | $message = _t( |
||
| 916 | 'CMSMain.PUBLISHED', |
||
| 917 | "Published '{title}' successfully.", |
||
| 918 | ['title' => $record->Title] |
||
| 919 | ); |
||
| 920 | } else { |
||
| 921 | $message = _t( |
||
| 922 | 'CMSMain.SAVED', |
||
| 923 | "Saved '{title}' successfully.", |
||
| 924 | ['title' => $record->Title] |
||
| 925 | ); |
||
| 926 | } |
||
| 927 | |||
| 928 | $this->getResponse()->addHeader('X-Status', rawurlencode($message)); |
||
| 929 | return $this->getResponseNegotiator()->respond($this->getRequest()); |
||
| 930 | } |
||
| 931 | |||
| 932 | /** |
||
| 933 | * @uses LeftAndMainExtension->augmentNewSiteTreeItem() |
||
| 934 | */ |
||
| 935 | public function getNewItem($id, $setID = true) { |
||
| 936 | $parentClass = $this->stat('tree_class'); |
||
| 937 | list($dummy, $className, $parentID, $suffix) = array_pad(explode('-',$id),4,null); |
||
| 938 | |||
| 939 | if(!is_subclass_of($className, $parentClass) && strcasecmp($className, $parentClass) != 0) { |
||
| 940 | $response = Security::permissionFailure($this); |
||
| 941 | if (!$response) { |
||
| 942 | $response = $this->getResponse(); |
||
| 943 | } |
||
| 944 | throw new SS_HTTPResponse_Exception($response); |
||
| 945 | } |
||
| 946 | |||
| 947 | $newItem = new $className(); |
||
| 948 | |||
| 949 | if( !$suffix ) { |
||
| 950 | $sessionTag = "NewItems." . $parentID . "." . $className; |
||
| 951 | if(Session::get($sessionTag)) { |
||
| 952 | $suffix = '-' . Session::get($sessionTag); |
||
| 953 | Session::set($sessionTag, Session::get($sessionTag) + 1); |
||
| 954 | } |
||
| 955 | else |
||
| 956 | Session::set($sessionTag, 1); |
||
| 957 | |||
| 958 | $id = $id . $suffix; |
||
| 959 | } |
||
| 960 | |||
| 961 | $newItem->Title = _t( |
||
| 962 | 'CMSMain.NEWPAGE', |
||
| 963 | "New {pagetype}",'followed by a page type title', |
||
| 964 | array('pagetype' => singleton($className)->i18n_singular_name()) |
||
| 965 | ); |
||
| 966 | $newItem->ClassName = $className; |
||
| 967 | $newItem->ParentID = $parentID; |
||
| 968 | |||
| 969 | // DataObject::fieldExists only checks the current class, not the hierarchy |
||
| 970 | // This allows the CMS to set the correct sort value |
||
| 971 | if($newItem->castingHelper('Sort')) { |
||
| 972 | $newItem->Sort = DB::prepared_query('SELECT MAX("Sort") FROM "SiteTree" WHERE "ParentID" = ?', array($parentID))->value() + 1; |
||
| 973 | } |
||
| 974 | |||
| 975 | if($setID) $newItem->ID = $id; |
||
| 976 | |||
| 977 | # Some modules like subsites add extra fields that need to be set when the new item is created |
||
| 978 | $this->extend('augmentNewSiteTreeItem', $newItem); |
||
| 979 | |||
| 980 | return $newItem; |
||
| 981 | } |
||
| 982 | |||
| 983 | /** |
||
| 984 | * Actually perform the publication step |
||
| 985 | * |
||
| 986 | * @param Versioned|DataObject $record |
||
| 987 | * @return mixed |
||
| 988 | */ |
||
| 989 | public function performPublish($record) { |
||
| 990 | if($record && !$record->canPublish()) { |
||
| 991 | return Security::permissionFailure($this); |
||
| 992 | } |
||
| 993 | |||
| 994 | $record->publishRecursive(); |
||
| 995 | } |
||
| 996 | |||
| 997 | /** |
||
| 998 | * Reverts a page by publishing it to live. |
||
| 999 | * Use {@link restorepage()} if you want to restore a page |
||
| 1000 | * which was deleted from draft without publishing. |
||
| 1001 | * |
||
| 1002 | * @uses SiteTree->doRevertToLive() |
||
| 1003 | * |
||
| 1004 | * @param array $data |
||
| 1005 | * @param Form $form |
||
| 1006 | * @return SS_HTTPResponse |
||
| 1007 | * @throws SS_HTTPResponse_Exception |
||
| 1008 | */ |
||
| 1009 | public function revert($data, $form) { |
||
| 1010 | if(!isset($data['ID'])) { |
||
| 1011 | throw new SS_HTTPResponse_Exception("Please pass an ID in the form content", 400); |
||
| 1012 | } |
||
| 1013 | |||
| 1014 | $id = (int) $data['ID']; |
||
| 1015 | $restoredPage = Versioned::get_latest_version("SiteTree", $id); |
||
| 1016 | if(!$restoredPage) { |
||
| 1017 | throw new SS_HTTPResponse_Exception("SiteTree #$id not found", 400); |
||
| 1018 | } |
||
| 1019 | |||
| 1020 | /** @var SiteTree $record */ |
||
| 1021 | $record = Versioned::get_one_by_stage('SiteTree', 'Live', array( |
||
| 1022 | '"SiteTree_Live"."ID"' => $id |
||
| 1023 | )); |
||
| 1024 | |||
| 1025 | // a user can restore a page without publication rights, as it just adds a new draft state |
||
| 1026 | // (this action should just be available when page has been "deleted from draft") |
||
| 1027 | if($record && !$record->canEdit()) { |
||
| 1028 | return Security::permissionFailure($this); |
||
| 1029 | } |
||
| 1030 | if(!$record || !$record->ID) { |
||
| 1031 | throw new SS_HTTPResponse_Exception("Bad record ID #$id", 404); |
||
| 1032 | } |
||
| 1033 | |||
| 1034 | $record->doRevertToLive(); |
||
| 1035 | |||
| 1036 | $this->getResponse()->addHeader( |
||
| 1037 | 'X-Status', |
||
| 1038 | rawurlencode(_t( |
||
| 1039 | 'CMSMain.RESTORED', |
||
| 1040 | "Restored '{title}' successfully", |
||
| 1041 | 'Param %s is a title', |
||
| 1042 | array('title' => $record->Title) |
||
| 1043 | )) |
||
| 1044 | ); |
||
| 1045 | |||
| 1046 | return $this->getResponseNegotiator()->respond($this->getRequest()); |
||
| 1047 | } |
||
| 1048 | |||
| 1049 | /** |
||
| 1050 | * Delete the current page from draft stage. |
||
| 1051 | * |
||
| 1052 | * @see deletefromlive() |
||
| 1053 | * |
||
| 1054 | * @param array $data |
||
| 1055 | * @param Form $form |
||
| 1056 | * @return SS_HTTPResponse |
||
| 1057 | * @throws SS_HTTPResponse_Exception |
||
| 1058 | */ |
||
| 1059 | View Code Duplication | public function delete($data, $form) { |
|
| 1060 | $id = $data['ID']; |
||
| 1061 | $record = DataObject::get_by_id("SiteTree", $id); |
||
| 1062 | if($record && !$record->canDelete()) { |
||
| 1063 | return Security::permissionFailure(); |
||
| 1064 | } |
||
| 1065 | if(!$record || !$record->ID) { |
||
| 1066 | throw new SS_HTTPResponse_Exception("Bad record ID #$id", 404); |
||
| 1067 | } |
||
| 1068 | |||
| 1069 | // Delete record |
||
| 1070 | $record->delete(); |
||
| 1071 | |||
| 1072 | $this->getResponse()->addHeader( |
||
| 1073 | 'X-Status', |
||
| 1074 | rawurlencode(sprintf(_t('CMSMain.REMOVEDPAGEFROMDRAFT',"Removed '%s' from the draft site"), $record->Title)) |
||
| 1075 | ); |
||
| 1076 | |||
| 1077 | // Even if the record has been deleted from stage and live, it can be viewed in "archive mode" |
||
| 1078 | return $this->getResponseNegotiator()->respond($this->getRequest()); |
||
| 1079 | } |
||
| 1080 | |||
| 1081 | /** |
||
| 1082 | * Delete this page from both live and stage |
||
| 1083 | * |
||
| 1084 | * @param array $data |
||
| 1085 | * @param Form $form |
||
| 1086 | * @return SS_HTTPResponse |
||
| 1087 | * @throws SS_HTTPResponse_Exception |
||
| 1088 | */ |
||
| 1089 | View Code Duplication | public function archive($data, $form) { |
|
| 1090 | $id = $data['ID']; |
||
| 1091 | /** @var SiteTree $record */ |
||
| 1092 | $record = DataObject::get_by_id("SiteTree", $id); |
||
| 1093 | if(!$record || !$record->exists()) { |
||
| 1094 | throw new SS_HTTPResponse_Exception("Bad record ID #$id", 404); |
||
| 1095 | } |
||
| 1096 | if(!$record->canArchive()) { |
||
| 1097 | return Security::permissionFailure(); |
||
| 1098 | } |
||
| 1099 | |||
| 1100 | // Archive record |
||
| 1101 | $record->doArchive(); |
||
| 1102 | |||
| 1103 | $this->getResponse()->addHeader( |
||
| 1104 | 'X-Status', |
||
| 1105 | rawurlencode(sprintf(_t('CMSMain.ARCHIVEDPAGE',"Archived page '%s'"), $record->Title)) |
||
| 1106 | ); |
||
| 1107 | |||
| 1108 | // Even if the record has been deleted from stage and live, it can be viewed in "archive mode" |
||
| 1109 | return $this->getResponseNegotiator()->respond($this->getRequest()); |
||
| 1110 | } |
||
| 1111 | |||
| 1112 | public function publish($data, $form) { |
||
| 1113 | $data['publish'] = '1'; |
||
| 1114 | |||
| 1115 | return $this->save($data, $form); |
||
| 1116 | } |
||
| 1117 | |||
| 1118 | public function unpublish($data, $form) { |
||
| 1119 | $className = $this->stat('tree_class'); |
||
| 1120 | /** @var SiteTree $record */ |
||
| 1121 | $record = DataObject::get_by_id($className, $data['ID']); |
||
| 1122 | |||
| 1123 | if($record && !$record->canUnpublish()) { |
||
| 1124 | return Security::permissionFailure($this); |
||
| 1125 | } |
||
| 1126 | View Code Duplication | if(!$record || !$record->ID) { |
|
| 1127 | throw new SS_HTTPResponse_Exception("Bad record ID #" . (int)$data['ID'], 404); |
||
| 1128 | } |
||
| 1129 | |||
| 1130 | $record->doUnpublish(); |
||
| 1131 | |||
| 1132 | $this->getResponse()->addHeader( |
||
| 1133 | 'X-Status', |
||
| 1134 | rawurlencode(_t('CMSMain.REMOVEDPAGE',"Removed '{title}' from the published site", array('title' => $record->Title))) |
||
| 1135 | ); |
||
| 1136 | |||
| 1137 | return $this->getResponseNegotiator()->respond($this->getRequest()); |
||
| 1138 | } |
||
| 1139 | |||
| 1140 | /** |
||
| 1141 | * @return array |
||
| 1142 | */ |
||
| 1143 | public function rollback() { |
||
| 1144 | return $this->doRollback(array( |
||
| 1145 | 'ID' => $this->currentPageID(), |
||
| 1146 | 'Version' => $this->getRequest()->param('VersionID') |
||
| 1147 | ), null); |
||
| 1148 | } |
||
| 1149 | |||
| 1150 | /** |
||
| 1151 | * Rolls a site back to a given version ID |
||
| 1152 | * |
||
| 1153 | * @param array $data |
||
| 1154 | * @param Form $form |
||
| 1155 | * @return SS_HTTPResponse |
||
| 1156 | */ |
||
| 1157 | public function doRollback($data, $form) { |
||
| 1158 | $this->extend('onBeforeRollback', $data['ID']); |
||
| 1159 | |||
| 1160 | $id = (isset($data['ID'])) ? (int) $data['ID'] : null; |
||
| 1161 | $version = (isset($data['Version'])) ? (int) $data['Version'] : null; |
||
| 1162 | |||
| 1163 | /** @var DataObject|Versioned $record */ |
||
| 1164 | $record = DataObject::get_by_id($this->stat('tree_class'), $id); |
||
| 1165 | if($record && !$record->canEdit()) { |
||
| 1166 | return Security::permissionFailure($this); |
||
| 1167 | } |
||
| 1168 | |||
| 1169 | if($version) { |
||
| 1170 | $record->doRollbackTo($version); |
||
| 1171 | $message = _t( |
||
| 1172 | 'CMSMain.ROLLEDBACKVERSIONv2', |
||
| 1173 | "Rolled back to version #%d.", |
||
| 1174 | array('version' => $data['Version']) |
||
| 1175 | ); |
||
| 1176 | } else { |
||
| 1177 | $record->doRollbackTo('Live'); |
||
| 1178 | $message = _t( |
||
| 1179 | 'CMSMain.ROLLEDBACKPUBv2',"Rolled back to published version." |
||
| 1180 | ); |
||
| 1181 | } |
||
| 1182 | |||
| 1183 | $this->getResponse()->addHeader('X-Status', rawurlencode($message)); |
||
| 1184 | |||
| 1185 | // Can be used in different contexts: In normal page edit view, in which case the redirect won't have any effect. |
||
| 1186 | // Or in history view, in which case a revert causes the CMS to re-load the edit view. |
||
| 1187 | // The X-Pjax header forces a "full" content refresh on redirect. |
||
| 1188 | $url = Controller::join_links(singleton('CMSPageEditController')->Link('show'), $record->ID); |
||
| 1189 | $this->getResponse()->addHeader('X-ControllerURL', $url); |
||
| 1190 | $this->getRequest()->addHeader('X-Pjax', 'Content'); |
||
| 1191 | $this->getResponse()->addHeader('X-Pjax', 'Content'); |
||
| 1192 | |||
| 1193 | return $this->getResponseNegotiator()->respond($this->getRequest()); |
||
| 1194 | } |
||
| 1195 | |||
| 1196 | /** |
||
| 1197 | * Action handler for adding pages to a campaign |
||
| 1198 | * |
||
| 1199 | * @param array $data |
||
| 1200 | * @param Form $form |
||
| 1201 | * @return DBHTMLText|SS_HTTPResponse |
||
| 1202 | */ |
||
| 1203 | public function addtocampaign($data, $form) { |
||
| 1204 | $handler = AddToCampaignHandler::create($form, $data); |
||
| 1205 | return $handler->handle(); |
||
| 1206 | } |
||
| 1207 | |||
| 1208 | /** |
||
| 1209 | * Batch Actions Handler |
||
| 1210 | */ |
||
| 1211 | public function batchactions() { |
||
| 1214 | |||
| 1215 | public function BatchActionParameters() { |
||
| 1235 | /** |
||
| 1236 | * Returns a list of batch actions |
||
| 1237 | */ |
||
| 1238 | public function BatchActionList() { |
||
| 1241 | |||
| 1242 | public function buildbrokenlinks($request) { |
||
| 1277 | |||
| 1278 | public function publishall($request) { |
||
| 1279 | if(!Permission::check('ADMIN')) return Security::permissionFailure($this); |
||
| 1280 | |||
| 1281 | increase_time_limit_to(); |
||
| 1282 | increase_memory_limit_to(); |
||
| 1283 | |||
| 1284 | $response = ""; |
||
| 1285 | |||
| 1286 | if(isset($this->requestParams['confirm'])) { |
||
| 1287 | // Protect against CSRF on destructive action |
||
| 1288 | if(!SecurityToken::inst()->checkRequest($request)) return $this->httpError(400); |
||
| 1289 | |||
| 1290 | $start = 0; |
||
| 1291 | $pages = DataObject::get("SiteTree", "", "", "", "$start,30"); |
||
| 1292 | $count = 0; |
||
| 1293 | while($pages) { |
||
| 1294 | foreach($pages as $page) { |
||
| 1295 | if($page && !$page->canPublish()) { |
||
| 1296 | return Security::permissionFailure($this); |
||
| 1297 | } |
||
| 1298 | |||
| 1299 | $page->publishRecursive(); |
||
| 1300 | $page->destroy(); |
||
| 1301 | unset($page); |
||
| 1302 | $count++; |
||
| 1303 | $response .= "<li>$count</li>"; |
||
| 1304 | } |
||
| 1305 | if($pages->Count() > 29) { |
||
| 1306 | $start += 30; |
||
| 1307 | $pages = DataObject::get("SiteTree", "", "", "", "$start,30"); |
||
| 1308 | } else { |
||
| 1309 | break; |
||
| 1310 | } |
||
| 1311 | } |
||
| 1312 | $response .= _t('CMSMain.PUBPAGES',"Done: Published {count} pages", array('count' => $count)); |
||
| 1313 | |||
| 1314 | } else { |
||
| 1315 | $token = SecurityToken::inst(); |
||
| 1316 | $fields = new FieldList(); |
||
| 1317 | $token->updateFieldSet($fields); |
||
| 1318 | $tokenField = $fields->First(); |
||
| 1319 | $tokenHtml = ($tokenField) ? $tokenField->FieldHolder() : ''; |
||
| 1320 | $response .= '<h1>' . _t('CMSMain.PUBALLFUN','"Publish All" functionality') . '</h1> |
||
| 1321 | <p>' . _t('CMSMain.PUBALLFUN2', 'Pressing this button will do the equivalent of going to every page and pressing "publish". It\'s |
||
| 1322 | intended to be used after there have been massive edits of the content, such as when the site was |
||
| 1323 | first built.') . '</p> |
||
| 1333 | |||
| 1334 | /** |
||
| 1335 | * Restore a completely deleted page from the SiteTree_versions table. |
||
| 1336 | */ |
||
| 1337 | public function restore($data, $form) { |
||
| 1359 | |||
| 1360 | public function duplicate($request) { |
||
| 1397 | |||
| 1398 | public function duplicatewithchildren($request) { |
||
| 1429 | |||
| 1430 | public function providePermissions() { |
||
| 1444 | |||
| 1445 | } |
||
| 1446 |