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 |
||
| 12 | class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionProvider { |
||
| 13 | |||
| 14 | private static $url_segment = 'pages'; |
||
|
|
|||
| 15 | |||
| 16 | private static $url_rule = '/$Action/$ID/$OtherID'; |
||
| 17 | |||
| 18 | // Maintain a lower priority than other administration sections |
||
| 19 | // so that Director does not think they are actions of CMSMain |
||
| 20 | private static $url_priority = 39; |
||
| 21 | |||
| 22 | private static $menu_title = 'Edit Page'; |
||
| 23 | |||
| 24 | private static $menu_priority = 10; |
||
| 25 | |||
| 26 | private static $tree_class = "SiteTree"; |
||
| 27 | |||
| 28 | private static $subitem_class = "Member"; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Amount of results showing on a single page. |
||
| 32 | * |
||
| 33 | * @config |
||
| 34 | * @var int |
||
| 35 | */ |
||
| 36 | private static $page_length = 15; |
||
| 37 | |||
| 38 | private static $allowed_actions = array( |
||
| 39 | 'archive', |
||
| 40 | 'buildbrokenlinks', |
||
| 41 | 'deleteitems', |
||
| 42 | 'DeleteItemsForm', |
||
| 43 | 'dialog', |
||
| 44 | 'duplicate', |
||
| 45 | 'duplicatewithchildren', |
||
| 46 | 'publishall', |
||
| 47 | 'publishitems', |
||
| 48 | 'PublishItemsForm', |
||
| 49 | 'submit', |
||
| 50 | 'EditForm', |
||
| 51 | 'SearchForm', |
||
| 52 | 'SiteTreeAsUL', |
||
| 53 | 'getshowdeletedsubtree', |
||
| 54 | 'batchactions', |
||
| 55 | 'treeview', |
||
| 56 | 'listview', |
||
| 57 | 'ListViewForm', |
||
| 58 | 'childfilter', |
||
| 59 | ); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Enable legacy batch actions. |
||
| 63 | * @deprecated since version 4.0 |
||
| 64 | * @var array |
||
| 65 | * @config |
||
| 66 | */ |
||
| 67 | private static $enabled_legacy_actions = array(); |
||
| 68 | |||
| 69 | public function init() { |
||
| 117 | |||
| 118 | public function index($request) { |
||
| 125 | |||
| 126 | public function getResponseNegotiator() { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * If this is set to true, the "switchView" context in the |
||
| 137 | * template is shown, with links to the staging and publish site. |
||
| 138 | * |
||
| 139 | * @return boolean |
||
| 140 | */ |
||
| 141 | public function ShowSwitchView() { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Overloads the LeftAndMain::ShowView. Allows to pass a page as a parameter, so we are able |
||
| 147 | * to switch view also for archived versions. |
||
| 148 | */ |
||
| 149 | public function SwitchView($page = null) { |
||
| 159 | |||
| 160 | //------------------------------------------------------------------------------------------// |
||
| 161 | // Main controllers |
||
| 162 | |||
| 163 | //------------------------------------------------------------------------------------------// |
||
| 164 | // Main UI components |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Override {@link LeftAndMain} Link to allow blank URL segment for CMSMain. |
||
| 168 | * |
||
| 169 | * @param string|null $action Action to link to. |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | public function Link($action = null) { |
||
| 182 | |||
| 183 | public function LinkPages() { |
||
| 186 | |||
| 187 | public function LinkPagesWithSearch() { |
||
| 190 | |||
| 191 | public function LinkTreeView() { |
||
| 194 | |||
| 195 | public function LinkListView() { |
||
| 198 | |||
| 199 | public function LinkGalleryView() { |
||
| 202 | |||
| 203 | public function LinkPageEdit($id = null) { |
||
| 209 | |||
| 210 | View Code Duplication | public function LinkPageSettings() { |
|
| 217 | |||
| 218 | View Code Duplication | public function LinkPageHistory() { |
|
| 225 | |||
| 226 | public function LinkWithSearch($link) { |
||
| 239 | |||
| 240 | public function LinkPageAdd($extra = null, $placeholders = null) { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | public function LinkPreview() { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Return the entire site tree as a nested set of ULs |
||
| 275 | */ |
||
| 276 | public function SiteTreeAsUL() { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @return boolean |
||
| 289 | */ |
||
| 290 | public function TreeIsFiltered() { |
||
| 299 | |||
| 300 | public function ExtraTreeTools() { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Returns a Form for page searching for use in templates. |
||
| 308 | * |
||
| 309 | * Can be modified from a decorator by a 'updateSearchForm' method |
||
| 310 | * |
||
| 311 | * @return Form |
||
| 312 | */ |
||
| 313 | public function SearchForm() { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Returns a sorted array suitable for a dropdown with pagetypes and their translated name |
||
| 386 | * |
||
| 387 | * @return array |
||
| 388 | */ |
||
| 389 | protected function getPageTypes() { |
||
| 397 | |||
| 398 | public function doSearch($data, $form) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @param bool $unlinked |
||
| 404 | * @return ArrayList |
||
| 405 | */ |
||
| 406 | public function Breadcrumbs($unlinked = false) { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Create serialized JSON string with site tree hints data to be injected into |
||
| 420 | * 'data-hints' attribute of root node of jsTree. |
||
| 421 | * |
||
| 422 | * @return string Serialized JSON |
||
| 423 | */ |
||
| 424 | public function SiteTreeHints() { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Populates an array of classes in the CMS |
||
| 488 | * which allows the user to change the page type. |
||
| 489 | * |
||
| 490 | * @return SS_List |
||
| 491 | */ |
||
| 492 | public function PageTypes() { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Get a database record to be managed by the CMS. |
||
| 535 | * |
||
| 536 | * @param int $id Record ID |
||
| 537 | * @param int $versionID optional Version id of the given record |
||
| 538 | * @return SiteTree |
||
| 539 | */ |
||
| 540 | public function getRecord($id, $versionID = null) { |
||
| 589 | |||
| 590 | /** |
||
| 591 | * @param int $id |
||
| 592 | * @param FieldList $fields |
||
| 593 | * @return CMSForm |
||
| 594 | */ |
||
| 595 | public function getEditForm($id = null, $fields = null) { |
||
| 596 | if(!$id) $id = $this->currentPageID(); |
||
| 597 | $form = parent::getEditForm($id); |
||
| 598 | |||
| 599 | // TODO Duplicate record fetching (see parent implementation) |
||
| 600 | $record = $this->getRecord($id); |
||
| 601 | if($record && !$record->canView()) return Security::permissionFailure($this); |
||
| 602 | |||
| 603 | if(!$fields) $fields = $form->Fields(); |
||
| 604 | $actions = $form->Actions(); |
||
| 605 | |||
| 606 | if($record) { |
||
| 607 | $deletedFromStage = $record->getIsDeletedFromStage(); |
||
| 608 | |||
| 609 | $fields->push($idField = new HiddenField("ID", false, $id)); |
||
| 610 | // Necessary for different subsites |
||
| 611 | $fields->push($liveLinkField = new HiddenField("AbsoluteLink", false, $record->AbsoluteLink())); |
||
| 612 | $fields->push($liveLinkField = new HiddenField("LiveLink")); |
||
| 613 | $fields->push($stageLinkField = new HiddenField("StageLink")); |
||
| 614 | $fields->push(new HiddenField("TreeTitle", false, $record->TreeTitle)); |
||
| 615 | |||
| 616 | if($record->ID && is_numeric( $record->ID ) ) { |
||
| 617 | $liveLink = $record->getAbsoluteLiveLink(); |
||
| 618 | if($liveLink) $liveLinkField->setValue($liveLink); |
||
| 619 | if(!$deletedFromStage) { |
||
| 620 | $stageLink = Controller::join_links($record->AbsoluteLink(), '?stage=Stage'); |
||
| 621 | if($stageLink) $stageLinkField->setValue($stageLink); |
||
| 622 | } |
||
| 623 | } |
||
| 624 | |||
| 625 | // Added in-line to the form, but plucked into different view by LeftAndMain.Preview.js upon load |
||
| 626 | if(in_array('CMSPreviewable', class_implements($record)) && !$fields->fieldByName('SilverStripeNavigator')) { |
||
| 627 | $navField = new LiteralField('SilverStripeNavigator', $this->getSilverStripeNavigator()); |
||
| 628 | $navField->setAllowHTML(true); |
||
| 629 | $fields->push($navField); |
||
| 630 | } |
||
| 631 | |||
| 632 | // getAllCMSActions can be used to completely redefine the action list |
||
| 633 | if($record->hasMethod('getAllCMSActions')) { |
||
| 634 | $actions = $record->getAllCMSActions(); |
||
| 635 | } else { |
||
| 636 | $actions = $record->getCMSActions(); |
||
| 637 | |||
| 638 | // Find and remove action menus that have no actions. |
||
| 639 | if ($actions && $actions->Count()) { |
||
| 640 | $tabset = $actions->fieldByName('ActionMenus'); |
||
| 641 | if ($tabset) { |
||
| 642 | foreach ($tabset->getChildren() as $tab) { |
||
| 643 | if (!$tab->getChildren()->count()) { |
||
| 644 | $tabset->removeByName($tab->getName()); |
||
| 645 | } |
||
| 646 | } |
||
| 647 | } |
||
| 648 | } |
||
| 649 | } |
||
| 650 | |||
| 651 | // Use <button> to allow full jQuery UI styling |
||
| 652 | $actionsFlattened = $actions->dataFields(); |
||
| 653 | if($actionsFlattened) foreach($actionsFlattened as $action) $action->setUseButtonTag(true); |
||
| 654 | |||
| 655 | if($record->hasMethod('getCMSValidator')) { |
||
| 656 | $validator = $record->getCMSValidator(); |
||
| 657 | } else { |
||
| 658 | $validator = new RequiredFields(); |
||
| 659 | } |
||
| 660 | |||
| 661 | $form = CMSForm::create( |
||
| 662 | $this, "EditForm", $fields, $actions, $validator |
||
| 663 | )->setHTMLID('Form_EditForm'); |
||
| 664 | $form->setResponseNegotiator($this->getResponseNegotiator()); |
||
| 665 | $form->loadDataFrom($record); |
||
| 666 | $form->disableDefaultAction(); |
||
| 667 | $form->addExtraClass('cms-edit-form'); |
||
| 668 | $form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); |
||
| 669 | // TODO Can't merge $FormAttributes in template at the moment |
||
| 670 | $form->addExtraClass('center ' . $this->BaseCSSClasses()); |
||
| 671 | // if($form->Fields()->hasTabset()) $form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet'); |
||
| 672 | $form->setAttribute('data-pjax-fragment', 'CurrentForm'); |
||
| 673 | // Set validation exemptions for specific actions |
||
| 674 | $form->setValidationExemptActions(array('restore', 'revert', 'deletefromlive', 'delete', 'unpublish', 'rollback')); |
||
| 675 | |||
| 676 | // Announce the capability so the frontend can decide whether to allow preview or not. |
||
| 677 | if(in_array('CMSPreviewable', class_implements($record))) { |
||
| 678 | $form->addExtraClass('cms-previewable'); |
||
| 679 | } |
||
| 680 | |||
| 681 | if(!$record->canEdit() || $deletedFromStage) { |
||
| 682 | $readonlyFields = $form->Fields()->makeReadonly(); |
||
| 683 | $form->setFields($readonlyFields); |
||
| 684 | } |
||
| 685 | |||
| 686 | $this->extend('updateEditForm', $form); |
||
| 687 | return $form; |
||
| 688 | } else if($id) { |
||
| 689 | $form = CMSForm::create( $this, "EditForm", new FieldList( |
||
| 690 | new LabelField('PageDoesntExistLabel',_t('CMSMain.PAGENOTEXISTS',"This page doesn't exist"))), new FieldList() |
||
| 691 | )->setHTMLID('Form_EditForm'); |
||
| 692 | $form->setResponseNegotiator($this->getResponseNegotiator()); |
||
| 693 | return $form; |
||
| 694 | } |
||
| 695 | } |
||
| 696 | |||
| 697 | /** |
||
| 698 | * @param SS_HTTPRequest $request |
||
| 699 | * @return string HTML |
||
| 700 | */ |
||
| 701 | public function treeview($request) { |
||
| 702 | return $this->renderWith($this->getTemplatesWithSuffix('_TreeView')); |
||
| 703 | } |
||
| 704 | |||
| 705 | /** |
||
| 706 | * @param SS_HTTPRequest $request |
||
| 707 | * @return string HTML |
||
| 708 | */ |
||
| 709 | public function listview($request) { |
||
| 710 | return $this->renderWith($this->getTemplatesWithSuffix('_ListView')); |
||
| 711 | } |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Callback to request the list of page types allowed under a given page instance. |
||
| 715 | * Provides a slower but more precise response over SiteTreeHints |
||
| 716 | * |
||
| 717 | * @param SS_HTTPRequest $request |
||
| 718 | * @return SS_HTTPResponse |
||
| 719 | */ |
||
| 720 | public function childfilter($request) { |
||
| 721 | // Check valid parent specified |
||
| 722 | $parentID = $request->requestVar('ParentID'); |
||
| 723 | $parent = SiteTree::get()->byID($parentID); |
||
| 724 | if(!$parent || !$parent->exists()) return $this->httpError(404); |
||
| 725 | |||
| 726 | // Build hints specific to this class |
||
| 727 | // Identify disallows and set globals |
||
| 728 | $classes = SiteTree::page_type_classes(); |
||
| 729 | $disallowedChildren = array(); |
||
| 730 | foreach($classes as $class) { |
||
| 731 | $obj = singleton($class); |
||
| 732 | if($obj instanceof HiddenClass) continue; |
||
| 733 | |||
| 734 | if(!$obj->canCreate(null, array('Parent' => $parent))) { |
||
| 735 | $disallowedChildren[] = $class; |
||
| 736 | } |
||
| 737 | } |
||
| 738 | |||
| 739 | $this->extend('updateChildFilter', $disallowedChildren, $parentID); |
||
| 740 | return $this |
||
| 741 | ->getResponse() |
||
| 742 | ->addHeader('Content-Type', 'application/json; charset=utf-8') |
||
| 743 | ->setBody(Convert::raw2json($disallowedChildren)); |
||
| 744 | } |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Safely reconstruct a selected filter from a given set of query parameters |
||
| 748 | * |
||
| 749 | * @param array $params Query parameters to use |
||
| 750 | * @return CMSSiteTreeFilter The filter class, or null if none present |
||
| 751 | * @throws InvalidArgumentException if invalid filter class is passed. |
||
| 752 | */ |
||
| 753 | protected function getQueryFilter($params) { |
||
| 754 | if(empty($params['FilterClass'])) return null; |
||
| 755 | $filterClass = $params['FilterClass']; |
||
| 756 | if(!is_subclass_of($filterClass, 'CMSSiteTreeFilter')) { |
||
| 757 | throw new InvalidArgumentException("Invalid filter class passed: {$filterClass}"); |
||
| 758 | } |
||
| 759 | return $filterClass::create($params); |
||
| 760 | } |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Returns the pages meet a certain criteria as {@see CMSSiteTreeFilter} or the subpages of a parent page |
||
| 764 | * defaulting to no filter and show all pages in first level. |
||
| 765 | * Doubles as search results, if any search parameters are set through {@link SearchForm()}. |
||
| 766 | * |
||
| 767 | * @param array $params Search filter criteria |
||
| 768 | * @param int $parentID Optional parent node to filter on (can't be combined with other search criteria) |
||
| 769 | * @return SS_List |
||
| 770 | * @throws InvalidArgumentException if invalid filter class is passed. |
||
| 771 | */ |
||
| 772 | public function getList($params = array(), $parentID = 0) { |
||
| 773 | if($filter = $this->getQueryFilter($params)) { |
||
| 774 | return $filter->getFilteredPages(); |
||
| 775 | } else { |
||
| 776 | $list = DataList::create($this->stat('tree_class')); |
||
| 777 | $parentID = is_numeric($parentID) ? $parentID : 0; |
||
| 778 | return $list->filter("ParentID", $parentID); |
||
| 779 | } |
||
| 780 | } |
||
| 781 | |||
| 782 | public function ListViewForm() { |
||
| 783 | $params = $this->getRequest()->requestVar('q'); |
||
| 784 | $list = $this->getList($params, $parentID = $this->getRequest()->requestVar('ParentID')); |
||
| 785 | $gridFieldConfig = GridFieldConfig::create()->addComponents( |
||
| 786 | new GridFieldSortableHeader(), |
||
| 787 | new GridFieldDataColumns(), |
||
| 788 | new GridFieldPaginator(self::config()->page_length) |
||
| 789 | ); |
||
| 790 | if($parentID){ |
||
| 791 | $gridFieldConfig->addComponent( |
||
| 792 | GridFieldLevelup::create($parentID) |
||
| 793 | ->setLinkSpec('?ParentID=%d&view=list') |
||
| 794 | ->setAttributes(array('data-pjax' => 'ListViewForm,Breadcrumbs')) |
||
| 795 | ); |
||
| 796 | } |
||
| 797 | $gridField = new GridField('Page','Pages', $list, $gridFieldConfig); |
||
| 798 | $columns = $gridField->getConfig()->getComponentByType('GridFieldDataColumns'); |
||
| 799 | |||
| 800 | // Don't allow navigating into children nodes on filtered lists |
||
| 801 | $fields = array( |
||
| 802 | 'getTreeTitle' => _t('SiteTree.PAGETITLE', 'Page Title'), |
||
| 803 | 'singular_name' => _t('SiteTree.PAGETYPE'), |
||
| 804 | 'LastEdited' => _t('SiteTree.LASTUPDATED', 'Last Updated'), |
||
| 805 | ); |
||
| 806 | $gridField->getConfig()->getComponentByType('GridFieldSortableHeader')->setFieldSorting(array('getTreeTitle' => 'Title')); |
||
| 807 | $gridField->getState()->ParentID = $parentID; |
||
| 808 | |||
| 809 | if(!$params) { |
||
| 810 | $fields = array_merge(array('listChildrenLink' => ''), $fields); |
||
| 811 | } |
||
| 812 | |||
| 813 | $columns->setDisplayFields($fields); |
||
| 814 | $columns->setFieldCasting(array( |
||
| 815 | 'Created' => 'Datetime->Ago', |
||
| 816 | 'LastEdited' => 'Datetime->FormatFromSettings', |
||
| 817 | 'getTreeTitle' => 'HTMLText' |
||
| 818 | )); |
||
| 819 | |||
| 820 | $controller = $this; |
||
| 821 | $columns->setFieldFormatting(array( |
||
| 822 | 'listChildrenLink' => function($value, &$item) use($controller) { |
||
| 823 | $num = $item ? $item->numChildren() : null; |
||
| 824 | if($num) { |
||
| 825 | return sprintf( |
||
| 826 | '<a class="cms-panel-link list-children-link" data-pjax-target="ListViewForm,Breadcrumbs" href="%s">%s</a>', |
||
| 827 | Controller::join_links( |
||
| 828 | $controller->Link(), |
||
| 829 | sprintf("?ParentID=%d&view=list", (int)$item->ID) |
||
| 830 | ), |
||
| 831 | $num |
||
| 832 | ); |
||
| 833 | } |
||
| 834 | }, |
||
| 835 | 'getTreeTitle' => function($value, &$item) use($controller) { |
||
| 836 | return sprintf( |
||
| 837 | '<a class="action-detail" href="%s">%s</a>', |
||
| 838 | Controller::join_links( |
||
| 839 | singleton('CMSPageEditController')->Link('show'), |
||
| 840 | (int)$item->ID |
||
| 841 | ), |
||
| 842 | $item->TreeTitle // returns HTML, does its own escaping |
||
| 843 | ); |
||
| 844 | } |
||
| 845 | )); |
||
| 846 | |||
| 847 | $listview = CMSForm::create( |
||
| 848 | $this, |
||
| 849 | 'ListViewForm', |
||
| 850 | new FieldList($gridField), |
||
| 851 | new FieldList() |
||
| 852 | )->setHTMLID('Form_ListViewForm'); |
||
| 853 | $listview->setAttribute('data-pjax-fragment', 'ListViewForm'); |
||
| 854 | $listview->setResponseNegotiator($this->getResponseNegotiator()); |
||
| 855 | |||
| 856 | $this->extend('updateListView', $listview); |
||
| 857 | |||
| 858 | $listview->disableSecurityToken(); |
||
| 859 | return $listview; |
||
| 860 | } |
||
| 861 | |||
| 862 | public function currentPageID() { |
||
| 863 | $id = parent::currentPageID(); |
||
| 864 | |||
| 865 | $this->extend('updateCurrentPageID', $id); |
||
| 866 | |||
| 867 | return $id; |
||
| 868 | } |
||
| 869 | |||
| 870 | //------------------------------------------------------------------------------------------// |
||
| 871 | // Data saving handlers |
||
| 872 | |||
| 873 | /** |
||
| 874 | * Save and Publish page handler |
||
| 875 | */ |
||
| 876 | public function save($data, $form) { |
||
| 877 | $className = $this->stat('tree_class'); |
||
| 878 | |||
| 879 | // Existing or new record? |
||
| 880 | $id = $data['ID']; |
||
| 881 | if(substr($id,0,3) != 'new') { |
||
| 882 | $record = DataObject::get_by_id($className, $id); |
||
| 883 | if($record && !$record->canEdit()) return Security::permissionFailure($this); |
||
| 884 | if(!$record || !$record->ID) throw new SS_HTTPResponse_Exception("Bad record ID #$id", 404); |
||
| 885 | } else { |
||
| 886 | if(!singleton($this->stat('tree_class'))->canCreate()) return Security::permissionFailure($this); |
||
| 887 | $record = $this->getNewItem($id, false); |
||
| 888 | } |
||
| 889 | |||
| 890 | // TODO Coupling to SiteTree |
||
| 891 | $record->HasBrokenLink = 0; |
||
| 892 | $record->HasBrokenFile = 0; |
||
| 893 | |||
| 894 | if (!$record->ObsoleteClassName) $record->writeWithoutVersion(); |
||
| 895 | |||
| 896 | // Update the class instance if necessary |
||
| 897 | if(isset($data['ClassName']) && $data['ClassName'] != $record->ClassName) { |
||
| 898 | $newClassName = $record->ClassName; |
||
| 899 | // The records originally saved attribute was overwritten by $form->saveInto($record) before. |
||
| 900 | // This is necessary for newClassInstance() to work as expected, and trigger change detection |
||
| 901 | // on the ClassName attribute |
||
| 902 | $record->setClassName($data['ClassName']); |
||
| 903 | // Replace $record with a new instance |
||
| 904 | $record = $record->newClassInstance($newClassName); |
||
| 905 | } |
||
| 906 | |||
| 907 | // save form data into record |
||
| 908 | $form->saveInto($record); |
||
| 909 | $record->write(); |
||
| 910 | |||
| 911 | // If the 'Save & Publish' button was clicked, also publish the page |
||
| 912 | if (isset($data['publish']) && $data['publish'] == 1) { |
||
| 913 | $record->doPublish(); |
||
| 914 | } |
||
| 915 | |||
| 916 | return $this->getResponseNegotiator()->respond($this->getRequest()); |
||
| 917 | } |
||
| 918 | |||
| 919 | /** |
||
| 920 | * @uses LeftAndMainExtension->augmentNewSiteTreeItem() |
||
| 921 | */ |
||
| 922 | public function getNewItem($id, $setID = true) { |
||
| 923 | $parentClass = $this->stat('tree_class'); |
||
| 924 | list($dummy, $className, $parentID, $suffix) = array_pad(explode('-',$id),4,null); |
||
| 925 | |||
| 926 | if(!is_subclass_of($className, $parentClass) && strcasecmp($className, $parentClass) != 0) { |
||
| 927 | $response = Security::permissionFailure($this); |
||
| 928 | if (!$response) { |
||
| 929 | $response = $this->getResponse(); |
||
| 930 | } |
||
| 931 | throw new SS_HTTPResponse_Exception($response); |
||
| 932 | } |
||
| 933 | |||
| 934 | $newItem = new $className(); |
||
| 935 | |||
| 936 | if( !$suffix ) { |
||
| 937 | $sessionTag = "NewItems." . $parentID . "." . $className; |
||
| 938 | if(Session::get($sessionTag)) { |
||
| 939 | $suffix = '-' . Session::get($sessionTag); |
||
| 940 | Session::set($sessionTag, Session::get($sessionTag) + 1); |
||
| 941 | } |
||
| 942 | else |
||
| 943 | Session::set($sessionTag, 1); |
||
| 944 | |||
| 945 | $id = $id . $suffix; |
||
| 946 | } |
||
| 947 | |||
| 948 | $newItem->Title = _t( |
||
| 949 | 'CMSMain.NEWPAGE', |
||
| 950 | "New {pagetype}",'followed by a page type title', |
||
| 951 | array('pagetype' => singleton($className)->i18n_singular_name()) |
||
| 952 | ); |
||
| 953 | $newItem->ClassName = $className; |
||
| 954 | $newItem->ParentID = $parentID; |
||
| 955 | |||
| 956 | // DataObject::fieldExists only checks the current class, not the hierarchy |
||
| 957 | // This allows the CMS to set the correct sort value |
||
| 958 | if($newItem->castingHelper('Sort')) { |
||
| 959 | $newItem->Sort = DB::prepared_query('SELECT MAX("Sort") FROM "SiteTree" WHERE "ParentID" = ?', array($parentID))->value() + 1; |
||
| 960 | } |
||
| 961 | |||
| 962 | if($setID) $newItem->ID = $id; |
||
| 963 | |||
| 964 | # Some modules like subsites add extra fields that need to be set when the new item is created |
||
| 965 | $this->extend('augmentNewSiteTreeItem', $newItem); |
||
| 966 | |||
| 967 | return $newItem; |
||
| 968 | } |
||
| 969 | |||
| 970 | /** |
||
| 971 | * Delete the page from live. This means a page in draft mode might still exist. |
||
| 972 | * |
||
| 973 | * @see delete() |
||
| 974 | */ |
||
| 975 | public function deletefromlive($data, $form) { |
||
| 976 | Versioned::reading_stage('Live'); |
||
| 977 | |||
| 978 | /** @var SiteTree $record */ |
||
| 979 | $record = DataObject::get_by_id("SiteTree", $data['ID']); |
||
| 980 | if($record && !($record->canDelete() && $record->canUnpublish())) { |
||
| 981 | return Security::permissionFailure($this); |
||
| 982 | } |
||
| 983 | |||
| 984 | $descendantsRemoved = 0; |
||
| 985 | $recordTitle = $record->Title; |
||
| 986 | |||
| 987 | // before deleting the records, get the descendants of this tree |
||
| 988 | if($record) { |
||
| 989 | $descendantIDs = $record->getDescendantIDList(); |
||
| 990 | |||
| 991 | // then delete them from the live site too |
||
| 992 | $descendantsRemoved = 0; |
||
| 993 | foreach( $descendantIDs as $descID ) |
||
| 994 | /** @var SiteTree $descendant */ |
||
| 995 | if( $descendant = DataObject::get_by_id('SiteTree', $descID) ) { |
||
| 996 | $descendant->doUnpublish(); |
||
| 997 | $descendantsRemoved++; |
||
| 998 | } |
||
| 999 | |||
| 1000 | // delete the record |
||
| 1001 | $record->doUnpublish(); |
||
| 1002 | } |
||
| 1003 | |||
| 1004 | Versioned::reading_stage('Stage'); |
||
| 1005 | |||
| 1006 | if(isset($descendantsRemoved)) { |
||
| 1007 | $descRemoved = ' ' . _t( |
||
| 1008 | 'CMSMain.DESCREMOVED', |
||
| 1009 | 'and {count} descendants', |
||
| 1010 | array('count' => $descendantsRemoved) |
||
| 1011 | ); |
||
| 1012 | } else { |
||
| 1013 | $descRemoved = ''; |
||
| 1014 | } |
||
| 1015 | |||
| 1016 | $this->getResponse()->addHeader( |
||
| 1017 | 'X-Status', |
||
| 1018 | rawurlencode( |
||
| 1019 | _t( |
||
| 1020 | 'CMSMain.REMOVED', |
||
| 1021 | 'Deleted \'{title}\'{description} from live site', |
||
| 1022 | array('title' => $recordTitle, 'description' => $descRemoved) |
||
| 1023 | ) |
||
| 1024 | ) |
||
| 1025 | ); |
||
| 1026 | |||
| 1027 | // Even if the record has been deleted from stage and live, it can be viewed in "archive mode" |
||
| 1028 | return $this->getResponseNegotiator()->respond($this->getRequest()); |
||
| 1029 | } |
||
| 1030 | |||
| 1031 | /** |
||
| 1032 | * Actually perform the publication step |
||
| 1033 | */ |
||
| 1034 | public function performPublish($record) { |
||
| 1039 | |||
| 1040 | /** |
||
| 1041 | * Reverts a page by publishing it to live. |
||
| 1042 | * Use {@link restorepage()} if you want to restore a page |
||
| 1043 | * which was deleted from draft without publishing. |
||
| 1044 | * |
||
| 1045 | * @uses SiteTree->doRevertToLive() |
||
| 1046 | */ |
||
| 1047 | public function revert($data, $form) { |
||
| 1077 | |||
| 1078 | /** |
||
| 1079 | * Delete the current page from draft stage. |
||
| 1080 | * @see deletefromlive() |
||
| 1081 | */ |
||
| 1082 | public function delete($data, $form) { |
||
| 1100 | |||
| 1101 | /** |
||
| 1102 | * Delete this page from both live and stage |
||
| 1103 | * |
||
| 1104 | * @param array $data |
||
| 1105 | * @param Form $form |
||
| 1106 | */ |
||
| 1107 | public function archive($data, $form) { |
||
| 1128 | |||
| 1129 | public function publish($data, $form) { |
||
| 1134 | |||
| 1135 | public function unpublish($data, $form) { |
||
| 1136 | $className = $this->stat('tree_class'); |
||
| 1137 | /** @var SiteTree $record */ |
||
| 1138 | $record = DataObject::get_by_id($className, $data['ID']); |
||
| 1139 | |||
| 1140 | if($record && !$record->canUnpublish()) { |
||
| 1141 | return Security::permissionFailure($this); |
||
| 1142 | } |
||
| 1143 | View Code Duplication | if(!$record || !$record->ID) { |
|
| 1144 | throw new SS_HTTPResponse_Exception("Bad record ID #" . (int)$data['ID'], 404); |
||
| 1145 | } |
||
| 1156 | |||
| 1157 | /** |
||
| 1158 | * @return array |
||
| 1159 | */ |
||
| 1160 | public function rollback() { |
||
| 1166 | |||
| 1167 | /** |
||
| 1168 | * Rolls a site back to a given version ID |
||
| 1169 | * |
||
| 1170 | * @param array |
||
| 1171 | * @param Form |
||
| 1172 | * |
||
| 1173 | * @return html |
||
| 1174 | */ |
||
| 1175 | public function doRollback($data, $form) { |
||
| 1210 | |||
| 1211 | /** |
||
| 1212 | * Batch Actions Handler |
||
| 1213 | */ |
||
| 1214 | public function batchactions() { |
||
| 1217 | |||
| 1218 | public function BatchActionParameters() { |
||
| 1238 | /** |
||
| 1239 | * Returns a list of batch actions |
||
| 1240 | */ |
||
| 1241 | public function BatchActionList() { |
||
| 1244 | |||
| 1245 | public function buildbrokenlinks($request) { |
||
| 1280 | |||
| 1281 | public function publishall($request) { |
||
| 1334 | |||
| 1335 | /** |
||
| 1336 | * Restore a completely deleted page from the SiteTree_versions table. |
||
| 1337 | */ |
||
| 1338 | public function restore($data, $form) { |
||
| 1360 | |||
| 1361 | public function duplicate($request) { |
||
| 1398 | |||
| 1399 | public function duplicatewithchildren($request) { |
||
| 1430 | |||
| 1431 | public function providePermissions() { |
||
| 1445 | |||
| 1446 | } |
||
| 1447 |