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 SiteTree 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 SiteTree, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvider,CMSPreviewable { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Indicates what kind of children this page type can have. |
||
| 40 | * This can be an array of allowed child classes, or the string "none" - |
||
| 41 | * indicating that this page type can't have children. |
||
| 42 | * If a classname is prefixed by "*", such as "*Page", then only that |
||
| 43 | * class is allowed - no subclasses. Otherwise, the class and all its |
||
| 44 | * subclasses are allowed. |
||
| 45 | * To control allowed children on root level (no parent), use {@link $can_be_root}. |
||
| 46 | * |
||
| 47 | * Note that this setting is cached when used in the CMS, use the "flush" query parameter to clear it. |
||
| 48 | * |
||
| 49 | * @config |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | private static $allowed_children = array("SiteTree"); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The default child class for this page. |
||
| 56 | * Note: Value might be cached, see {@link $allowed_chilren}. |
||
| 57 | * |
||
| 58 | * @config |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | private static $default_child = "Page"; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Default value for SiteTree.ClassName enum |
||
| 65 | * {@see DBClassName::getDefault} |
||
| 66 | * |
||
| 67 | * @config |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | private static $default_classname = "Page"; |
||
|
|
|||
| 71 | |||
| 72 | /** |
||
| 73 | * The default parent class for this page. |
||
| 74 | * Note: Value might be cached, see {@link $allowed_chilren}. |
||
| 75 | * |
||
| 76 | * @config |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | private static $default_parent = null; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Controls whether a page can be in the root of the site tree. |
||
| 83 | * Note: Value might be cached, see {@link $allowed_chilren}. |
||
| 84 | * |
||
| 85 | * @config |
||
| 86 | * @var bool |
||
| 87 | */ |
||
| 88 | private static $can_be_root = true; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * List of permission codes a user can have to allow a user to create a page of this type. |
||
| 92 | * Note: Value might be cached, see {@link $allowed_chilren}. |
||
| 93 | * |
||
| 94 | * @config |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | private static $need_permission = null; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * If you extend a class, and don't want to be able to select the old class |
||
| 101 | * in the cms, set this to the old class name. Eg, if you extended Product |
||
| 102 | * to make ImprovedProduct, then you would set $hide_ancestor to Product. |
||
| 103 | * |
||
| 104 | * @config |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | private static $hide_ancestor = null; |
||
| 108 | |||
| 109 | private static $db = array( |
||
| 110 | "URLSegment" => "Varchar(255)", |
||
| 111 | "Title" => "Varchar(255)", |
||
| 112 | "MenuTitle" => "Varchar(100)", |
||
| 113 | "Content" => "HTMLText", |
||
| 114 | "MetaDescription" => "Text", |
||
| 115 | "ExtraMeta" => "HTMLText('meta, link')", |
||
| 116 | "ShowInMenus" => "Boolean", |
||
| 117 | "ShowInSearch" => "Boolean", |
||
| 118 | "Sort" => "Int", |
||
| 119 | "HasBrokenFile" => "Boolean", |
||
| 120 | "HasBrokenLink" => "Boolean", |
||
| 121 | "ReportClass" => "Varchar", |
||
| 122 | "CanViewType" => "Enum('Anyone, LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit')", |
||
| 123 | "CanEditType" => "Enum('LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit')", |
||
| 124 | ); |
||
| 125 | |||
| 126 | private static $indexes = array( |
||
| 127 | "URLSegment" => true, |
||
| 128 | ); |
||
| 129 | |||
| 130 | private static $many_many = array( |
||
| 131 | "ViewerGroups" => "Group", |
||
| 132 | "EditorGroups" => "Group", |
||
| 133 | ); |
||
| 134 | |||
| 135 | private static $has_many = array( |
||
| 136 | "VirtualPages" => "VirtualPage.CopyContentFrom" |
||
| 137 | ); |
||
| 138 | |||
| 139 | private static $owned_by = array( |
||
| 140 | "VirtualPages" |
||
| 141 | ); |
||
| 142 | |||
| 143 | private static $casting = array( |
||
| 144 | "Breadcrumbs" => "HTMLText", |
||
| 145 | "LastEdited" => "SS_Datetime", |
||
| 146 | "Created" => "SS_Datetime", |
||
| 147 | 'Link' => 'Text', |
||
| 148 | 'RelativeLink' => 'Text', |
||
| 149 | 'AbsoluteLink' => 'Text', |
||
| 150 | 'TreeTitle' => 'HTMLText', |
||
| 151 | ); |
||
| 152 | |||
| 153 | private static $defaults = array( |
||
| 154 | "ShowInMenus" => 1, |
||
| 155 | "ShowInSearch" => 1, |
||
| 156 | "CanViewType" => "Inherit", |
||
| 157 | "CanEditType" => "Inherit" |
||
| 158 | ); |
||
| 159 | |||
| 160 | private static $versioning = array( |
||
| 161 | "Stage", "Live" |
||
| 162 | ); |
||
| 163 | |||
| 164 | private static $default_sort = "\"Sort\""; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * If this is false, the class cannot be created in the CMS by regular content authors, only by ADMINs. |
||
| 168 | * @var boolean |
||
| 169 | * @config |
||
| 170 | */ |
||
| 171 | private static $can_create = true; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Icon to use in the CMS page tree. This should be the full filename, relative to the webroot. |
||
| 175 | * Also supports custom CSS rule contents (applied to the correct selector for the tree UI implementation). |
||
| 176 | * |
||
| 177 | * @see CMSMain::generateTreeStylingCSS() |
||
| 178 | * @config |
||
| 179 | * @var string |
||
| 180 | */ |
||
| 181 | private static $icon = null; |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @config |
||
| 185 | * @var string Description of the class functionality, typically shown to a user |
||
| 186 | * when selecting which page type to create. Translated through {@link provideI18nEntities()}. |
||
| 187 | */ |
||
| 188 | private static $description = 'Generic content page'; |
||
| 189 | |||
| 190 | private static $extensions = array( |
||
| 191 | "Hierarchy", |
||
| 192 | "Versioned", |
||
| 193 | "SiteTreeLinkTracking" |
||
| 194 | ); |
||
| 195 | |||
| 196 | private static $searchable_fields = array( |
||
| 197 | 'Title', |
||
| 198 | 'Content', |
||
| 199 | ); |
||
| 200 | |||
| 201 | private static $field_labels = array( |
||
| 202 | 'URLSegment' => 'URL' |
||
| 203 | ); |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @config |
||
| 207 | */ |
||
| 208 | private static $nested_urls = true; |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @config |
||
| 212 | */ |
||
| 213 | private static $create_default_pages = true; |
||
| 214 | |||
| 215 | /** |
||
| 216 | * This controls whether of not extendCMSFields() is called by getCMSFields. |
||
| 217 | */ |
||
| 218 | private static $runCMSFieldsExtensions = true; |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Cache for canView/Edit/Publish/Delete permissions. |
||
| 222 | * Keyed by permission type (e.g. 'edit'), with an array |
||
| 223 | * of IDs mapped to their boolean permission ability (true=allow, false=deny). |
||
| 224 | * See {@link batch_permission_check()} for details. |
||
| 225 | */ |
||
| 226 | private static $cache_permissions = array(); |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @config |
||
| 230 | * @var boolean |
||
| 231 | */ |
||
| 232 | private static $enforce_strict_hierarchy = true; |
||
| 233 | |||
| 234 | /** |
||
| 235 | * The value used for the meta generator tag. Leave blank to omit the tag. |
||
| 236 | * |
||
| 237 | * @config |
||
| 238 | * @var string |
||
| 239 | */ |
||
| 240 | private static $meta_generator = 'SilverStripe - http://silverstripe.org'; |
||
| 241 | |||
| 242 | protected $_cache_statusFlags = null; |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Fetches the {@link SiteTree} object that maps to a link. |
||
| 246 | * |
||
| 247 | * If you have enabled {@link SiteTree::config()->nested_urls} on this site, then you can use a nested link such as |
||
| 248 | * "about-us/staff/", and this function will traverse down the URL chain and grab the appropriate link. |
||
| 249 | * |
||
| 250 | * Note that if no model can be found, this method will fall over to a extended alternateGetByLink method provided |
||
| 251 | * by a extension attached to {@link SiteTree} |
||
| 252 | * |
||
| 253 | * @param string $link The link of the page to search for |
||
| 254 | * @param bool $cache True (default) to use caching, false to force a fresh search from the database |
||
| 255 | * @return SiteTree |
||
| 256 | */ |
||
| 257 | static public function get_by_link($link, $cache = true) { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Return a subclass map of SiteTree that shouldn't be hidden through {@link SiteTree::$hide_ancestor} |
||
| 326 | * |
||
| 327 | * @return array |
||
| 328 | */ |
||
| 329 | public static function page_type_classes() { |
||
| 330 | $classes = ClassInfo::getValidSubClasses(); |
||
| 331 | |||
| 332 | $baseClassIndex = array_search('SiteTree', $classes); |
||
| 333 | if($baseClassIndex !== FALSE) unset($classes[$baseClassIndex]); |
||
| 334 | |||
| 335 | $kill_ancestors = array(); |
||
| 336 | |||
| 337 | // figure out if there are any classes we don't want to appear |
||
| 338 | foreach($classes as $class) { |
||
| 339 | $instance = singleton($class); |
||
| 340 | |||
| 341 | // do any of the progeny want to hide an ancestor? |
||
| 342 | if($ancestor_to_hide = $instance->stat('hide_ancestor')) { |
||
| 343 | // note for killing later |
||
| 344 | $kill_ancestors[] = $ancestor_to_hide; |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | // If any of the descendents don't want any of the elders to show up, cruelly render the elders surplus to |
||
| 349 | // requirements |
||
| 350 | if($kill_ancestors) { |
||
| 351 | $kill_ancestors = array_unique($kill_ancestors); |
||
| 352 | foreach($kill_ancestors as $mark) { |
||
| 353 | // unset from $classes |
||
| 354 | $idx = array_search($mark, $classes, true); |
||
| 355 | if ($idx !== false) { |
||
| 356 | unset($classes[$idx]); |
||
| 357 | } |
||
| 358 | } |
||
| 359 | } |
||
| 360 | |||
| 361 | return $classes; |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Replace a "[sitetree_link id=n]" shortcode with a link to the page with the corresponding ID. |
||
| 366 | * |
||
| 367 | * @param array $arguments |
||
| 368 | * @param string $content |
||
| 369 | * @param TextParser $parser |
||
| 370 | * @return string |
||
| 371 | */ |
||
| 372 | static public function link_shortcode_handler($arguments, $content = null, $parser = null) { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Return the link for this {@link SiteTree} object, with the {@link Director::baseURL()} included. |
||
| 393 | * |
||
| 394 | * @param string $action Optional controller action (method). |
||
| 395 | * Note: URI encoding of this parameter is applied automatically through template casting, |
||
| 396 | * don't encode the passed parameter. Please use {@link Controller::join_links()} instead to |
||
| 397 | * append GET parameters. |
||
| 398 | * @return string |
||
| 399 | */ |
||
| 400 | public function Link($action = null) { |
||
| 401 | return Controller::join_links(Director::baseURL(), $this->RelativeLink($action)); |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Get the absolute URL for this page, including protocol and host. |
||
| 406 | * |
||
| 407 | * @param string $action See {@link Link()} |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | public function AbsoluteLink($action = null) { |
||
| 411 | if($this->hasMethod('alternateAbsoluteLink')) { |
||
| 412 | return $this->alternateAbsoluteLink($action); |
||
| 413 | } else { |
||
| 414 | return Director::absoluteURL($this->Link($action)); |
||
| 415 | } |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Base link used for previewing. Defaults to absolute URL, in order to account for domain changes, e.g. on multi |
||
| 420 | * site setups. Does not contain hints about the stage, see {@link SilverStripeNavigator} for details. |
||
| 421 | * |
||
| 422 | * @param string $action See {@link Link()} |
||
| 423 | * @return string |
||
| 424 | */ |
||
| 425 | public function PreviewLink($action = null) { |
||
| 435 | |||
| 436 | public function getMimeType() { |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Return the link for this {@link SiteTree} object relative to the SilverStripe root. |
||
| 442 | * |
||
| 443 | * By default, if this page is the current home page, and there is no action specified then this will return a link |
||
| 444 | * to the root of the site. However, if you set the $action parameter to TRUE then the link will not be rewritten |
||
| 445 | * and returned in its full form. |
||
| 446 | * |
||
| 447 | * @uses RootURLController::get_homepage_link() |
||
| 448 | * |
||
| 449 | * @param string $action See {@link Link()} |
||
| 450 | * @return string |
||
| 451 | */ |
||
| 452 | public function RelativeLink($action = null) { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Get the absolute URL for this page on the Live site. |
||
| 480 | * |
||
| 481 | * @param bool $includeStageEqualsLive Whether to append the URL with ?stage=Live to force Live mode |
||
| 482 | * @return string |
||
| 483 | */ |
||
| 484 | public function getAbsoluteLiveLink($includeStageEqualsLive = true) { |
||
| 485 | $oldReadingMode = Versioned::get_reading_mode(); |
||
| 486 | Versioned::set_stage(Versioned::LIVE); |
||
| 487 | $live = Versioned::get_one_by_stage('SiteTree', Versioned::LIVE, array( |
||
| 488 | '"SiteTree"."ID"' => $this->ID |
||
| 489 | )); |
||
| 490 | if($live) { |
||
| 491 | $link = $live->AbsoluteLink(); |
||
| 492 | if($includeStageEqualsLive) $link .= '?stage=Live'; |
||
| 493 | } else { |
||
| 494 | $link = null; |
||
| 495 | } |
||
| 496 | |||
| 497 | Versioned::set_reading_mode($oldReadingMode); |
||
| 498 | return $link; |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Generates a link to edit this page in the CMS. |
||
| 503 | * |
||
| 504 | * @return string |
||
| 505 | */ |
||
| 506 | public function CMSEditLink() { |
||
| 513 | |||
| 514 | |||
| 515 | /** |
||
| 516 | * Return a CSS identifier generated from this page's link. |
||
| 517 | * |
||
| 518 | * @return string The URL segment |
||
| 519 | */ |
||
| 520 | public function ElementName() { |
||
| 521 | return str_replace('/', '-', trim($this->RelativeLink(true), '/')); |
||
| 522 | } |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Returns true if this is the currently active page being used to handle this request. |
||
| 526 | * |
||
| 527 | * @return bool |
||
| 528 | */ |
||
| 529 | public function isCurrent() { |
||
| 530 | return $this->ID ? $this->ID == Director::get_current_page()->ID : $this === Director::get_current_page(); |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Check if this page is in the currently active section (e.g. it is either current or one of its children is |
||
| 535 | * currently being viewed). |
||
| 536 | * |
||
| 537 | * @return bool |
||
| 538 | */ |
||
| 539 | public function isSection() { |
||
| 540 | return $this->isCurrent() || ( |
||
| 541 | Director::get_current_page() instanceof SiteTree && in_array($this->ID, Director::get_current_page()->getAncestors()->column()) |
||
| 542 | ); |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Check if the parent of this page has been removed (or made otherwise unavailable), and is still referenced by |
||
| 547 | * this child. Any such orphaned page may still require access via the CMS, but should not be shown as accessible |
||
| 548 | * to external users. |
||
| 549 | * |
||
| 550 | * @return bool |
||
| 551 | */ |
||
| 552 | public function isOrphaned() { |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Return "link" or "current" depending on if this is the {@link SiteTree::isCurrent()} current page. |
||
| 563 | * |
||
| 564 | * @return string |
||
| 565 | */ |
||
| 566 | public function LinkOrCurrent() { |
||
| 567 | return $this->isCurrent() ? 'current' : 'link'; |
||
| 568 | } |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Return "link" or "section" depending on if this is the {@link SiteTree::isSeciton()} current section. |
||
| 572 | * |
||
| 573 | * @return string |
||
| 574 | */ |
||
| 575 | public function LinkOrSection() { |
||
| 576 | return $this->isSection() ? 'section' : 'link'; |
||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Return "link", "current" or "section" depending on if this page is the current page, or not on the current page |
||
| 581 | * but in the current section. |
||
| 582 | * |
||
| 583 | * @return string |
||
| 584 | */ |
||
| 585 | public function LinkingMode() { |
||
| 586 | if($this->isCurrent()) { |
||
| 587 | return 'current'; |
||
| 588 | } elseif($this->isSection()) { |
||
| 589 | return 'section'; |
||
| 590 | } else { |
||
| 591 | return 'link'; |
||
| 592 | } |
||
| 593 | } |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Check if this page is in the given current section. |
||
| 597 | * |
||
| 598 | * @param string $sectionName Name of the section to check |
||
| 599 | * @return bool True if we are in the given section |
||
| 600 | */ |
||
| 601 | public function InSection($sectionName) { |
||
| 602 | $page = Director::get_current_page(); |
||
| 603 | while($page) { |
||
| 604 | if($sectionName == $page->URLSegment) |
||
| 605 | return true; |
||
| 606 | $page = $page->Parent; |
||
| 607 | } |
||
| 608 | return false; |
||
| 609 | } |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Create a duplicate of this node. Doesn't affect joined data - create a custom overloading of this if you need |
||
| 613 | * such behaviour. |
||
| 614 | * |
||
| 615 | * @param bool $doWrite Whether to write the new object before returning it |
||
| 616 | * @return self The duplicated object |
||
| 617 | */ |
||
| 618 | public function duplicate($doWrite = true) { |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Duplicates each child of this node recursively and returns the top-level duplicate node. |
||
| 636 | * |
||
| 637 | * @return self The duplicated object |
||
| 638 | */ |
||
| 639 | public function duplicateWithChildren() { |
||
| 640 | $clone = $this->duplicate(); |
||
| 641 | $children = $this->AllChildren(); |
||
| 642 | |||
| 643 | if($children) { |
||
| 644 | foreach($children as $child) { |
||
| 645 | $childClone = method_exists($child, 'duplicateWithChildren') |
||
| 646 | ? $child->duplicateWithChildren() |
||
| 647 | : $child->duplicate(); |
||
| 648 | $childClone->ParentID = $clone->ID; |
||
| 649 | $childClone->write(); |
||
| 650 | } |
||
| 651 | } |
||
| 652 | |||
| 653 | return $clone; |
||
| 654 | } |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Duplicate this node and its children as a child of the node with the given ID |
||
| 658 | * |
||
| 659 | * @param int $id ID of the new node's new parent |
||
| 660 | */ |
||
| 661 | public function duplicateAsChild($id) { |
||
| 662 | $newSiteTree = $this->duplicate(); |
||
| 663 | $newSiteTree->ParentID = $id; |
||
| 664 | $newSiteTree->Sort = 0; |
||
| 665 | $newSiteTree->write(); |
||
| 666 | } |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Return a breadcrumb trail to this page. Excludes "hidden" pages (with ShowInMenus=0) by default. |
||
| 670 | * |
||
| 671 | * @param int $maxDepth The maximum depth to traverse. |
||
| 672 | * @param boolean $unlinked Whether to link page titles. |
||
| 673 | * @param boolean|string $stopAtPageType ClassName of a page to stop the upwards traversal. |
||
| 674 | * @param boolean $showHidden Include pages marked with the attribute ShowInMenus = 0 |
||
| 675 | * @return HTMLText The breadcrumb trail. |
||
| 676 | */ |
||
| 677 | public function Breadcrumbs($maxDepth = 20, $unlinked = false, $stopAtPageType = false, $showHidden = false) { |
||
| 678 | $pages = $this->getBreadcrumbItems($maxDepth, $stopAtPageType, $showHidden); |
||
| 679 | $template = new SSViewer('BreadcrumbsTemplate'); |
||
| 680 | return $template->process($this->customise(new ArrayData(array( |
||
| 681 | "Pages" => $pages, |
||
| 682 | "Unlinked" => $unlinked |
||
| 683 | )))); |
||
| 684 | } |
||
| 685 | |||
| 686 | |||
| 687 | /** |
||
| 688 | * Returns a list of breadcrumbs for the current page. |
||
| 689 | * |
||
| 690 | * @param int $maxDepth The maximum depth to traverse. |
||
| 691 | * @param boolean|string $stopAtPageType ClassName of a page to stop the upwards traversal. |
||
| 692 | * @param boolean $showHidden Include pages marked with the attribute ShowInMenus = 0 |
||
| 693 | * |
||
| 694 | * @return ArrayList |
||
| 695 | */ |
||
| 696 | public function getBreadcrumbItems($maxDepth = 20, $stopAtPageType = false, $showHidden = false) { |
||
| 714 | |||
| 715 | |||
| 716 | /** |
||
| 717 | * Make this page a child of another page. |
||
| 718 | * |
||
| 719 | * If the parent page does not exist, resolve it to a valid ID before updating this page's reference. |
||
| 720 | * |
||
| 721 | * @param SiteTree|int $item Either the parent object, or the parent ID |
||
| 722 | */ |
||
| 723 | public function setParent($item) { |
||
| 724 | if(is_object($item)) { |
||
| 725 | if (!$item->exists()) $item->write(); |
||
| 726 | $this->setField("ParentID", $item->ID); |
||
| 727 | } else { |
||
| 728 | $this->setField("ParentID", $item); |
||
| 729 | } |
||
| 730 | } |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Get the parent of this page. |
||
| 734 | * |
||
| 735 | * @return SiteTree Parent of this page |
||
| 736 | */ |
||
| 737 | public function getParent() { |
||
| 738 | if ($parentID = $this->getField("ParentID")) { |
||
| 739 | return DataObject::get_by_id("SiteTree", $parentID); |
||
| 740 | } |
||
| 741 | } |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Return a string of the form "parent - page" or "grandparent - parent - page" using page titles |
||
| 745 | * |
||
| 746 | * @param int $level The maximum amount of levels to traverse. |
||
| 747 | * @param string $separator Seperating string |
||
| 748 | * @return string The resulting string |
||
| 749 | */ |
||
| 750 | public function NestedTitle($level = 2, $separator = " - ") { |
||
| 751 | $item = $this; |
||
| 752 | while($item && $level > 0) { |
||
| 753 | $parts[] = $item->Title; |
||
| 754 | $item = $item->Parent; |
||
| 755 | $level--; |
||
| 756 | } |
||
| 757 | return implode($separator, array_reverse($parts)); |
||
| 758 | } |
||
| 759 | |||
| 760 | /** |
||
| 761 | * This function should return true if the current user can execute this action. It can be overloaded to customise |
||
| 762 | * the security model for an application. |
||
| 763 | * |
||
| 764 | * Slightly altered from parent behaviour in {@link DataObject->can()}: |
||
| 765 | * - Checks for existence of a method named "can<$perm>()" on the object |
||
| 766 | * - Calls decorators and only returns for FALSE "vetoes" |
||
| 767 | * - Falls back to {@link Permission::check()} |
||
| 768 | * - Does NOT check for many-many relations named "Can<$perm>" |
||
| 769 | * |
||
| 770 | * @uses DataObjectDecorator->can() |
||
| 771 | * |
||
| 772 | * @param string $perm The permission to be checked, such as 'View' |
||
| 773 | * @param Member $member The member whose permissions need checking. Defaults to the currently logged in user. |
||
| 774 | * @param array $context Context argument for canCreate() |
||
| 775 | * @return bool True if the the member is allowed to do the given action |
||
| 776 | */ |
||
| 777 | public function can($perm, $member = null, $context = array()) { |
||
| 794 | |||
| 795 | /** |
||
| 796 | * This function should return true if the current user can add children to this page. It can be overloaded to |
||
| 797 | * customise the security model for an application. |
||
| 798 | * |
||
| 799 | * Denies permission if any of the following conditions is true: |
||
| 800 | * - alternateCanAddChildren() on a extension returns false |
||
| 801 | * - canEdit() is not granted |
||
| 802 | * - There are no classes defined in {@link $allowed_children} |
||
| 803 | * |
||
| 804 | * @uses SiteTreeExtension->canAddChildren() |
||
| 805 | * @uses canEdit() |
||
| 806 | * @uses $allowed_children |
||
| 807 | * |
||
| 808 | * @param Member|int $member |
||
| 809 | * @return bool True if the current user can add children |
||
| 810 | */ |
||
| 811 | public function canAddChildren($member = null) { |
||
| 834 | |||
| 835 | /** |
||
| 836 | * This function should return true if the current user can view this page. It can be overloaded to customise the |
||
| 837 | * security model for an application. |
||
| 838 | * |
||
| 839 | * Denies permission if any of the following conditions is true: |
||
| 840 | * - canView() on any extension returns false |
||
| 841 | * - "CanViewType" directive is set to "Inherit" and any parent page return false for canView() |
||
| 842 | * - "CanViewType" directive is set to "LoggedInUsers" and no user is logged in |
||
| 843 | * - "CanViewType" directive is set to "OnlyTheseUsers" and user is not in the given groups |
||
| 844 | * |
||
| 845 | * @uses DataExtension->canView() |
||
| 846 | * @uses ViewerGroups() |
||
| 847 | * |
||
| 848 | * @param Member|int $member |
||
| 849 | * @return bool True if the current user can view this page |
||
| 850 | */ |
||
| 851 | public function canView($member = null) { |
||
| 900 | |||
| 901 | /** |
||
| 902 | * Check if this page can be published |
||
| 903 | * |
||
| 904 | * @param Member $member |
||
| 905 | * @return bool |
||
| 906 | */ |
||
| 907 | public function canPublish($member = null) { |
||
| 908 | if(!$member) { |
||
| 909 | $member = Member::currentUser(); |
||
| 910 | } |
||
| 911 | |||
| 912 | // Check extension |
||
| 913 | $extended = $this->extendedCan('canPublish', $member); |
||
| 914 | if($extended !== null) { |
||
| 915 | return $extended; |
||
| 916 | } |
||
| 917 | |||
| 918 | if(Permission::checkMember($member, "ADMIN")) { |
||
| 919 | return true; |
||
| 920 | } |
||
| 921 | |||
| 922 | // Default to relying on edit permission |
||
| 923 | return $this->canEdit($member); |
||
| 924 | } |
||
| 925 | |||
| 926 | /** |
||
| 927 | * This function should return true if the current user can delete this page. It can be overloaded to customise the |
||
| 928 | * security model for an application. |
||
| 929 | * |
||
| 930 | * Denies permission if any of the following conditions is true: |
||
| 931 | * - canDelete() returns false on any extension |
||
| 932 | * - canEdit() returns false |
||
| 933 | * - any descendant page returns false for canDelete() |
||
| 934 | * |
||
| 935 | * @uses canDelete() |
||
| 936 | * @uses SiteTreeExtension->canDelete() |
||
| 937 | * @uses canEdit() |
||
| 938 | * |
||
| 939 | * @param Member $member |
||
| 940 | * @return bool True if the current user can delete this page |
||
| 941 | */ |
||
| 942 | public function canDelete($member = null) { |
||
| 943 | View Code Duplication | if($member instanceof Member) $memberID = $member->ID; |
|
| 944 | else if(is_numeric($member)) $memberID = $member; |
||
| 945 | else $memberID = Member::currentUserID(); |
||
| 946 | |||
| 947 | // Standard mechanism for accepting permission changes from extensions |
||
| 948 | $extended = $this->extendedCan('canDelete', $memberID); |
||
| 949 | if($extended !== null) { |
||
| 950 | return $extended; |
||
| 951 | } |
||
| 952 | |||
| 953 | // Default permission check |
||
| 954 | if($memberID && Permission::checkMember($memberID, array("ADMIN", "SITETREE_EDIT_ALL"))) { |
||
| 955 | return true; |
||
| 956 | } |
||
| 957 | |||
| 958 | // Regular canEdit logic is handled by can_edit_multiple |
||
| 959 | $results = self::can_delete_multiple(array($this->ID), $memberID); |
||
| 960 | |||
| 961 | // If this page no longer exists in stage/live results won't contain the page. |
||
| 962 | // Fail-over to false |
||
| 963 | return isset($results[$this->ID]) ? $results[$this->ID] : false; |
||
| 964 | } |
||
| 965 | |||
| 966 | /** |
||
| 967 | * This function should return true if the current user can create new pages of this class, regardless of class. It |
||
| 968 | * can be overloaded to customise the security model for an application. |
||
| 969 | * |
||
| 970 | * By default, permission to create at the root level is based on the SiteConfig configuration, and permission to |
||
| 971 | * create beneath a parent is based on the ability to edit that parent page. |
||
| 972 | * |
||
| 973 | * Use {@link canAddChildren()} to control behaviour of creating children under this page. |
||
| 974 | * |
||
| 975 | * @uses $can_create |
||
| 976 | * @uses DataExtension->canCreate() |
||
| 977 | * |
||
| 978 | * @param Member $member |
||
| 979 | * @param array $context Optional array which may contain array('Parent' => $parentObj) |
||
| 980 | * If a parent page is known, it will be checked for validity. |
||
| 981 | * If omitted, it will be assumed this is to be created as a top level page. |
||
| 982 | * @return bool True if the current user can create pages on this class. |
||
| 983 | */ |
||
| 984 | public function canCreate($member = null, $context = array()) { |
||
| 985 | View Code Duplication | if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) { |
|
| 986 | $member = Member::currentUserID(); |
||
| 987 | } |
||
| 988 | |||
| 989 | // Check parent (custom canCreate option for SiteTree) |
||
| 990 | // Block children not allowed for this parent type |
||
| 991 | $parent = isset($context['Parent']) ? $context['Parent'] : null; |
||
| 992 | if($parent && !in_array(get_class($this), $parent->allowedChildren())) { |
||
| 993 | return false; |
||
| 994 | } |
||
| 995 | |||
| 996 | // Standard mechanism for accepting permission changes from extensions |
||
| 997 | $extended = $this->extendedCan(__FUNCTION__, $member, $context); |
||
| 998 | if($extended !== null) { |
||
| 999 | return $extended; |
||
| 1000 | } |
||
| 1001 | |||
| 1002 | // Check permission |
||
| 1003 | if($member && Permission::checkMember($member, "ADMIN")) { |
||
| 1004 | return true; |
||
| 1005 | } |
||
| 1006 | |||
| 1007 | // Fall over to inherited permissions |
||
| 1008 | if($parent) { |
||
| 1009 | return $parent->canAddChildren($member); |
||
| 1010 | } else { |
||
| 1011 | // This doesn't necessarily mean we are creating a root page, but that |
||
| 1012 | // we don't know if there is a parent, so default to this permission |
||
| 1013 | return SiteConfig::current_site_config()->canCreateTopLevel($member); |
||
| 1014 | } |
||
| 1015 | } |
||
| 1016 | |||
| 1017 | /** |
||
| 1018 | * This function should return true if the current user can edit this page. It can be overloaded to customise the |
||
| 1019 | * security model for an application. |
||
| 1020 | * |
||
| 1021 | * Denies permission if any of the following conditions is true: |
||
| 1022 | * - canEdit() on any extension returns false |
||
| 1023 | * - canView() return false |
||
| 1024 | * - "CanEditType" directive is set to "Inherit" and any parent page return false for canEdit() |
||
| 1025 | * - "CanEditType" directive is set to "LoggedInUsers" and no user is logged in or doesn't have the |
||
| 1026 | * CMS_Access_CMSMAIN permission code |
||
| 1027 | * - "CanEditType" directive is set to "OnlyTheseUsers" and user is not in the given groups |
||
| 1028 | * |
||
| 1029 | * @uses canView() |
||
| 1030 | * @uses EditorGroups() |
||
| 1031 | * @uses DataExtension->canEdit() |
||
| 1032 | * |
||
| 1033 | * @param Member $member Set to false if you want to explicitly test permissions without a valid user (useful for |
||
| 1034 | * unit tests) |
||
| 1035 | * @return bool True if the current user can edit this page |
||
| 1036 | */ |
||
| 1037 | public function canEdit($member = null) { |
||
| 1038 | View Code Duplication | if($member instanceof Member) $memberID = $member->ID; |
|
| 1039 | else if(is_numeric($member)) $memberID = $member; |
||
| 1040 | else $memberID = Member::currentUserID(); |
||
| 1041 | |||
| 1042 | // Standard mechanism for accepting permission changes from extensions |
||
| 1043 | $extended = $this->extendedCan('canEdit', $memberID); |
||
| 1044 | if($extended !== null) { |
||
| 1045 | return $extended; |
||
| 1046 | } |
||
| 1047 | |||
| 1048 | // Default permissions |
||
| 1049 | if($memberID && Permission::checkMember($memberID, array("ADMIN", "SITETREE_EDIT_ALL"))) { |
||
| 1050 | return true; |
||
| 1051 | } |
||
| 1052 | |||
| 1053 | if($this->ID) { |
||
| 1054 | // Regular canEdit logic is handled by can_edit_multiple |
||
| 1055 | $results = self::can_edit_multiple(array($this->ID), $memberID); |
||
| 1056 | |||
| 1057 | // If this page no longer exists in stage/live results won't contain the page. |
||
| 1058 | // Fail-over to false |
||
| 1059 | return isset($results[$this->ID]) ? $results[$this->ID] : false; |
||
| 1060 | |||
| 1061 | // Default for unsaved pages |
||
| 1062 | } else { |
||
| 1063 | return $this->getSiteConfig()->canEditPages($member); |
||
| 1064 | } |
||
| 1065 | } |
||
| 1066 | |||
| 1067 | /** |
||
| 1068 | * Stub method to get the site config, unless the current class can provide an alternate. |
||
| 1069 | * |
||
| 1070 | * @return SiteConfig |
||
| 1071 | */ |
||
| 1072 | public function getSiteConfig() { |
||
| 1073 | |||
| 1074 | if($this->hasMethod('alternateSiteConfig')) { |
||
| 1075 | $altConfig = $this->alternateSiteConfig(); |
||
| 1076 | if($altConfig) return $altConfig; |
||
| 1077 | } |
||
| 1078 | |||
| 1079 | return SiteConfig::current_site_config(); |
||
| 1080 | } |
||
| 1081 | |||
| 1082 | /** |
||
| 1083 | * Pre-populate the cache of canEdit, canView, canDelete, canPublish permissions. This method will use the static |
||
| 1084 | * can_(perm)_multiple method for efficiency. |
||
| 1085 | * |
||
| 1086 | * @param string $permission The permission: edit, view, publish, approve, etc. |
||
| 1087 | * @param array $ids An array of page IDs |
||
| 1088 | * @param callable|string $batchCallback The function/static method to call to calculate permissions. Defaults |
||
| 1089 | * to 'SiteTree::can_(permission)_multiple' |
||
| 1090 | */ |
||
| 1091 | static public function prepopulate_permission_cache($permission = 'CanEditType', $ids, $batchCallback = null) { |
||
| 1092 | if(!$batchCallback) $batchCallback = "SiteTree::can_{$permission}_multiple"; |
||
| 1093 | |||
| 1094 | if(is_callable($batchCallback)) { |
||
| 1095 | call_user_func($batchCallback, $ids, Member::currentUserID(), false); |
||
| 1096 | } else { |
||
| 1097 | user_error("SiteTree::prepopulate_permission_cache can't calculate '$permission' " |
||
| 1098 | . "with callback '$batchCallback'", E_USER_WARNING); |
||
| 1099 | } |
||
| 1100 | } |
||
| 1101 | |||
| 1102 | /** |
||
| 1103 | * This method is NOT a full replacement for the individual can*() methods, e.g. {@link canEdit()}. Rather than |
||
| 1104 | * checking (potentially slow) PHP logic, it relies on the database group associations, e.g. the "CanEditType" field |
||
| 1105 | * plus the "SiteTree_EditorGroups" many-many table. By batch checking multiple records, we can combine the queries |
||
| 1106 | * efficiently. |
||
| 1107 | * |
||
| 1108 | * Caches based on $typeField data. To invalidate the cache, use {@link SiteTree::reset()} or set the $useCached |
||
| 1109 | * property to FALSE. |
||
| 1110 | * |
||
| 1111 | * @param array $ids Of {@link SiteTree} IDs |
||
| 1112 | * @param int $memberID Member ID |
||
| 1113 | * @param string $typeField A property on the data record, e.g. "CanEditType". |
||
| 1114 | * @param string $groupJoinTable A many-many table name on this record, e.g. "SiteTree_EditorGroups" |
||
| 1115 | * @param string $siteConfigMethod Method to call on {@link SiteConfig} for toplevel items, e.g. "canEdit" |
||
| 1116 | * @param string $globalPermission If the member doesn't have this permission code, don't bother iterating deeper |
||
| 1117 | * @param bool $useCached |
||
| 1118 | * @return array An map of {@link SiteTree} ID keys to boolean values |
||
| 1119 | */ |
||
| 1120 | public static function batch_permission_check($ids, $memberID, $typeField, $groupJoinTable, $siteConfigMethod, |
||
| 1121 | $globalPermission = null, $useCached = true) { |
||
| 1122 | if($globalPermission === NULL) $globalPermission = array('CMS_ACCESS_LeftAndMain', 'CMS_ACCESS_CMSMain'); |
||
| 1123 | |||
| 1124 | // Sanitise the IDs |
||
| 1125 | $ids = array_filter($ids, 'is_numeric'); |
||
| 1126 | |||
| 1127 | // This is the name used on the permission cache |
||
| 1128 | // converts something like 'CanEditType' to 'edit'. |
||
| 1129 | $cacheKey = strtolower(substr($typeField, 3, -4)) . "-$memberID"; |
||
| 1130 | |||
| 1131 | // Default result: nothing editable |
||
| 1132 | $result = array_fill_keys($ids, false); |
||
| 1133 | if($ids) { |
||
| 1134 | |||
| 1135 | // Look in the cache for values |
||
| 1136 | if($useCached && isset(self::$cache_permissions[$cacheKey])) { |
||
| 1137 | $cachedValues = array_intersect_key(self::$cache_permissions[$cacheKey], $result); |
||
| 1138 | |||
| 1139 | // If we can't find everything in the cache, then look up the remainder separately |
||
| 1140 | $uncachedValues = array_diff_key($result, self::$cache_permissions[$cacheKey]); |
||
| 1141 | if($uncachedValues) { |
||
| 1142 | $cachedValues = self::batch_permission_check(array_keys($uncachedValues), $memberID, $typeField, $groupJoinTable, $siteConfigMethod, $globalPermission, false) + $cachedValues; |
||
| 1143 | } |
||
| 1144 | return $cachedValues; |
||
| 1145 | } |
||
| 1146 | |||
| 1147 | // If a member doesn't have a certain permission then they can't edit anything |
||
| 1148 | if(!$memberID || ($globalPermission && !Permission::checkMember($memberID, $globalPermission))) { |
||
| 1149 | return $result; |
||
| 1150 | } |
||
| 1151 | |||
| 1152 | // Placeholder for parameterised ID list |
||
| 1153 | $idPlaceholders = DB::placeholders($ids); |
||
| 1154 | |||
| 1155 | // If page can't be viewed, don't grant edit permissions to do - implement can_view_multiple(), so this can |
||
| 1156 | // be enabled |
||
| 1157 | //$ids = array_keys(array_filter(self::can_view_multiple($ids, $memberID))); |
||
| 1158 | |||
| 1159 | // Get the groups that the given member belongs to |
||
| 1160 | $groupIDs = DataObject::get_by_id('Member', $memberID)->Groups()->column("ID"); |
||
| 1161 | $SQL_groupList = implode(", ", $groupIDs); |
||
| 1162 | if (!$SQL_groupList) $SQL_groupList = '0'; |
||
| 1163 | |||
| 1164 | $combinedStageResult = array(); |
||
| 1165 | |||
| 1166 | foreach(array(Versioned::DRAFT, Versioned::LIVE) as $stage) { |
||
| 1167 | // Start by filling the array with the pages that actually exist |
||
| 1168 | $table = ($stage=='Stage') ? "SiteTree" : "SiteTree_$stage"; |
||
| 1169 | |||
| 1170 | if($ids) { |
||
| 1171 | $idQuery = "SELECT \"ID\" FROM \"$table\" WHERE \"ID\" IN ($idPlaceholders)"; |
||
| 1172 | $stageIds = DB::prepared_query($idQuery, $ids)->column(); |
||
| 1173 | } else { |
||
| 1174 | $stageIds = array(); |
||
| 1175 | } |
||
| 1176 | $result = array_fill_keys($stageIds, false); |
||
| 1177 | |||
| 1178 | // Get the uninherited permissions |
||
| 1179 | $uninheritedPermissions = Versioned::get_by_stage("SiteTree", $stage) |
||
| 1180 | ->where(array( |
||
| 1181 | "(\"$typeField\" = 'LoggedInUsers' OR |
||
| 1182 | (\"$typeField\" = 'OnlyTheseUsers' AND \"$groupJoinTable\".\"SiteTreeID\" IS NOT NULL)) |
||
| 1183 | AND \"SiteTree\".\"ID\" IN ($idPlaceholders)" |
||
| 1184 | => $ids |
||
| 1185 | )) |
||
| 1186 | ->leftJoin($groupJoinTable, "\"$groupJoinTable\".\"SiteTreeID\" = \"SiteTree\".\"ID\" AND \"$groupJoinTable\".\"GroupID\" IN ($SQL_groupList)"); |
||
| 1187 | |||
| 1188 | if($uninheritedPermissions) { |
||
| 1189 | // Set all the relevant items in $result to true |
||
| 1190 | $result = array_fill_keys($uninheritedPermissions->column('ID'), true) + $result; |
||
| 1191 | } |
||
| 1192 | |||
| 1193 | // Get permissions that are inherited |
||
| 1194 | $potentiallyInherited = Versioned::get_by_stage( |
||
| 1195 | "SiteTree", |
||
| 1196 | $stage, |
||
| 1197 | array("\"$typeField\" = 'Inherit' AND \"SiteTree\".\"ID\" IN ($idPlaceholders)" => $ids) |
||
| 1198 | ); |
||
| 1199 | |||
| 1200 | if($potentiallyInherited) { |
||
| 1201 | // Group $potentiallyInherited by ParentID; we'll look at the permission of all those parents and |
||
| 1202 | // then see which ones the user has permission on |
||
| 1203 | $groupedByParent = array(); |
||
| 1204 | foreach($potentiallyInherited as $item) { |
||
| 1205 | if($item->ParentID) { |
||
| 1206 | if(!isset($groupedByParent[$item->ParentID])) $groupedByParent[$item->ParentID] = array(); |
||
| 1207 | $groupedByParent[$item->ParentID][] = $item->ID; |
||
| 1208 | } else { |
||
| 1209 | // Might return different site config based on record context, e.g. when subsites module |
||
| 1210 | // is used |
||
| 1211 | $siteConfig = $item->getSiteConfig(); |
||
| 1212 | $result[$item->ID] = $siteConfig->{$siteConfigMethod}($memberID); |
||
| 1213 | } |
||
| 1214 | } |
||
| 1215 | |||
| 1216 | if($groupedByParent) { |
||
| 1217 | $actuallyInherited = self::batch_permission_check(array_keys($groupedByParent), $memberID, $typeField, $groupJoinTable, $siteConfigMethod); |
||
| 1218 | if($actuallyInherited) { |
||
| 1219 | $parentIDs = array_keys(array_filter($actuallyInherited)); |
||
| 1220 | foreach($parentIDs as $parentID) { |
||
| 1221 | // Set all the relevant items in $result to true |
||
| 1222 | $result = array_fill_keys($groupedByParent[$parentID], true) + $result; |
||
| 1223 | } |
||
| 1224 | } |
||
| 1225 | } |
||
| 1226 | } |
||
| 1227 | |||
| 1228 | $combinedStageResult = $combinedStageResult + $result; |
||
| 1229 | |||
| 1230 | } |
||
| 1231 | } |
||
| 1232 | |||
| 1233 | if(isset($combinedStageResult)) { |
||
| 1234 | // Cache the results |
||
| 1235 | if(empty(self::$cache_permissions[$cacheKey])) self::$cache_permissions[$cacheKey] = array(); |
||
| 1236 | self::$cache_permissions[$cacheKey] = $combinedStageResult + self::$cache_permissions[$cacheKey]; |
||
| 1237 | |||
| 1238 | return $combinedStageResult; |
||
| 1239 | } else { |
||
| 1240 | return array(); |
||
| 1241 | } |
||
| 1242 | } |
||
| 1243 | |||
| 1244 | /** |
||
| 1245 | * Get the 'can edit' information for a number of SiteTree pages. |
||
| 1246 | * |
||
| 1247 | * @param array $ids An array of IDs of the SiteTree pages to look up |
||
| 1248 | * @param int $memberID ID of member |
||
| 1249 | * @param bool $useCached Return values from the permission cache if they exist |
||
| 1250 | * @return array A map where the IDs are keys and the values are booleans stating whether the given page can be |
||
| 1251 | * edited |
||
| 1252 | */ |
||
| 1253 | static public function can_edit_multiple($ids, $memberID, $useCached = true) { |
||
| 1254 | return self::batch_permission_check($ids, $memberID, 'CanEditType', 'SiteTree_EditorGroups', 'canEditPages', null, $useCached); |
||
| 1255 | } |
||
| 1256 | |||
| 1257 | /** |
||
| 1258 | * Get the 'can edit' information for a number of SiteTree pages. |
||
| 1259 | * |
||
| 1260 | * @param array $ids An array of IDs of the SiteTree pages to look up |
||
| 1261 | * @param int $memberID ID of member |
||
| 1262 | * @param bool $useCached Return values from the permission cache if they exist |
||
| 1263 | * @return array |
||
| 1264 | */ |
||
| 1265 | static public function can_delete_multiple($ids, $memberID, $useCached = true) { |
||
| 1266 | $deletable = array(); |
||
| 1267 | $result = array_fill_keys($ids, false); |
||
| 1268 | $cacheKey = "delete-$memberID"; |
||
| 1269 | |||
| 1270 | // Look in the cache for values |
||
| 1271 | if($useCached && isset(self::$cache_permissions[$cacheKey])) { |
||
| 1272 | $cachedValues = array_intersect_key(self::$cache_permissions[$cacheKey], $result); |
||
| 1273 | |||
| 1274 | // If we can't find everything in the cache, then look up the remainder separately |
||
| 1275 | $uncachedValues = array_diff_key($result, self::$cache_permissions[$cacheKey]); |
||
| 1276 | if($uncachedValues) { |
||
| 1277 | $cachedValues = self::can_delete_multiple(array_keys($uncachedValues), $memberID, false) |
||
| 1278 | + $cachedValues; |
||
| 1279 | } |
||
| 1280 | return $cachedValues; |
||
| 1281 | } |
||
| 1282 | |||
| 1283 | // You can only delete pages that you can edit |
||
| 1284 | $editableIDs = array_keys(array_filter(self::can_edit_multiple($ids, $memberID))); |
||
| 1285 | if($editableIDs) { |
||
| 1286 | |||
| 1287 | // You can only delete pages whose children you can delete |
||
| 1288 | $editablePlaceholders = DB::placeholders($editableIDs); |
||
| 1289 | $childRecords = SiteTree::get()->where(array( |
||
| 1290 | "\"SiteTree\".\"ParentID\" IN ($editablePlaceholders)" => $editableIDs |
||
| 1291 | )); |
||
| 1292 | if($childRecords) { |
||
| 1293 | $children = $childRecords->map("ID", "ParentID"); |
||
| 1294 | |||
| 1295 | // Find out the children that can be deleted |
||
| 1296 | $deletableChildren = self::can_delete_multiple($children->keys(), $memberID); |
||
| 1297 | |||
| 1298 | // Get a list of all the parents that have no undeletable children |
||
| 1299 | $deletableParents = array_fill_keys($editableIDs, true); |
||
| 1300 | foreach($deletableChildren as $id => $canDelete) { |
||
| 1301 | if(!$canDelete) unset($deletableParents[$children[$id]]); |
||
| 1302 | } |
||
| 1303 | |||
| 1304 | // Use that to filter the list of deletable parents that have children |
||
| 1305 | $deletableParents = array_keys($deletableParents); |
||
| 1306 | |||
| 1307 | // Also get the $ids that don't have children |
||
| 1308 | $parents = array_unique($children->values()); |
||
| 1309 | $deletableLeafNodes = array_diff($editableIDs, $parents); |
||
| 1310 | |||
| 1311 | // Combine the two |
||
| 1312 | $deletable = array_merge($deletableParents, $deletableLeafNodes); |
||
| 1313 | |||
| 1314 | } else { |
||
| 1315 | $deletable = $editableIDs; |
||
| 1316 | } |
||
| 1317 | } else { |
||
| 1318 | $deletable = array(); |
||
| 1319 | } |
||
| 1320 | |||
| 1321 | // Convert the array of deletable IDs into a map of the original IDs with true/false as the value |
||
| 1322 | return array_fill_keys($deletable, true) + array_fill_keys($ids, false); |
||
| 1323 | } |
||
| 1324 | |||
| 1325 | /** |
||
| 1326 | * Collate selected descendants of this page. |
||
| 1327 | * |
||
| 1328 | * {@link $condition} will be evaluated on each descendant, and if it is succeeds, that item will be added to the |
||
| 1329 | * $collator array. |
||
| 1330 | * |
||
| 1331 | * @param string $condition The PHP condition to be evaluated. The page will be called $item |
||
| 1332 | * @param array $collator An array, passed by reference, to collect all of the matching descendants. |
||
| 1333 | * @return bool |
||
| 1334 | */ |
||
| 1335 | public function collateDescendants($condition, &$collator) { |
||
| 1336 | if($children = $this->Children()) { |
||
| 1337 | foreach($children as $item) { |
||
| 1338 | if(eval("return $condition;")) $collator[] = $item; |
||
| 1339 | $item->collateDescendants($condition, $collator); |
||
| 1340 | } |
||
| 1341 | return true; |
||
| 1342 | } |
||
| 1343 | } |
||
| 1344 | |||
| 1345 | /** |
||
| 1346 | * Return the title, description, keywords and language metatags. |
||
| 1347 | * |
||
| 1348 | * @todo Move <title> tag in separate getter for easier customization and more obvious usage |
||
| 1349 | * |
||
| 1350 | * @param bool $includeTitle Show default <title>-tag, set to false for custom templating |
||
| 1351 | * @return string The XHTML metatags |
||
| 1352 | */ |
||
| 1353 | public function MetaTags($includeTitle = true) { |
||
| 1354 | $tags = ""; |
||
| 1355 | if($includeTitle === true || $includeTitle == 'true') { |
||
| 1356 | $tags .= "<title>" . Convert::raw2xml($this->Title) . "</title>\n"; |
||
| 1357 | } |
||
| 1358 | |||
| 1359 | $generator = trim(Config::inst()->get('SiteTree', 'meta_generator')); |
||
| 1360 | if (!empty($generator)) { |
||
| 1361 | $tags .= "<meta name=\"generator\" content=\"" . Convert::raw2att($generator) . "\" />\n"; |
||
| 1362 | } |
||
| 1363 | |||
| 1364 | $charset = Config::inst()->get('ContentNegotiator', 'encoding'); |
||
| 1365 | $tags .= "<meta http-equiv=\"Content-type\" content=\"text/html; charset=$charset\" />\n"; |
||
| 1366 | if($this->MetaDescription) { |
||
| 1367 | $tags .= "<meta name=\"description\" content=\"" . Convert::raw2att($this->MetaDescription) . "\" />\n"; |
||
| 1368 | } |
||
| 1369 | if($this->ExtraMeta) { |
||
| 1370 | $tags .= $this->ExtraMeta . "\n"; |
||
| 1371 | } |
||
| 1372 | |||
| 1373 | if(Permission::check('CMS_ACCESS_CMSMain') |
||
| 1374 | && in_array('CMSPreviewable', class_implements($this)) |
||
| 1375 | && !$this instanceof ErrorPage |
||
| 1376 | && $this->ID > 0 |
||
| 1377 | ) { |
||
| 1378 | $tags .= "<meta name=\"x-page-id\" content=\"{$this->ID}\" />\n"; |
||
| 1379 | $tags .= "<meta name=\"x-cms-edit-link\" content=\"" . $this->CMSEditLink() . "\" />\n"; |
||
| 1380 | } |
||
| 1381 | |||
| 1382 | $this->extend('MetaTags', $tags); |
||
| 1383 | |||
| 1384 | return $tags; |
||
| 1385 | } |
||
| 1386 | |||
| 1387 | /** |
||
| 1388 | * Returns the object that contains the content that a user would associate with this page. |
||
| 1389 | * |
||
| 1390 | * Ordinarily, this is just the page itself, but for example on RedirectorPages or VirtualPages ContentSource() will |
||
| 1391 | * return the page that is linked to. |
||
| 1392 | * |
||
| 1393 | * @return $this |
||
| 1394 | */ |
||
| 1395 | public function ContentSource() { |
||
| 1398 | |||
| 1399 | /** |
||
| 1400 | * Add default records to database. |
||
| 1401 | * |
||
| 1402 | * This function is called whenever the database is built, after the database tables have all been created. Overload |
||
| 1403 | * this to add default records when the database is built, but make sure you call parent::requireDefaultRecords(). |
||
| 1404 | */ |
||
| 1405 | public function requireDefaultRecords() { |
||
| 1454 | |||
| 1455 | protected function onBeforeWrite() { |
||
| 1505 | |||
| 1506 | /** |
||
| 1507 | * Trigger synchronisation of link tracking |
||
| 1508 | * |
||
| 1509 | * {@see SiteTreeLinkTracking::augmentSyncLinkTracking} |
||
| 1510 | */ |
||
| 1511 | public function syncLinkTracking() { |
||
| 1514 | |||
| 1515 | public function onBeforeDelete() { |
||
| 1525 | |||
| 1526 | public function onAfterDelete() { |
||
| 1539 | |||
| 1540 | public function flushCache($persistent = true) { |
||
| 1544 | |||
| 1545 | public function validate() { |
||
| 1582 | |||
| 1583 | /** |
||
| 1584 | * Returns true if this object has a URLSegment value that does not conflict with any other objects. This method |
||
| 1585 | * checks for: |
||
| 1586 | * - A page with the same URLSegment that has a conflict |
||
| 1587 | * - Conflicts with actions on the parent page |
||
| 1588 | * - A conflict caused by a root page having the same URLSegment as a class name |
||
| 1589 | * |
||
| 1590 | * @return bool |
||
| 1591 | */ |
||
| 1592 | public function validURLSegment() { |
||
| 1626 | |||
| 1627 | /** |
||
| 1628 | * Generate a URL segment based on the title provided. |
||
| 1629 | * |
||
| 1630 | * If {@link Extension}s wish to alter URL segment generation, they can do so by defining |
||
| 1631 | * updateURLSegment(&$url, $title). $url will be passed by reference and should be modified. $title will contain |
||
| 1632 | * the title that was originally used as the source of this generated URL. This lets extensions either start from |
||
| 1633 | * scratch, or incrementally modify the generated URL. |
||
| 1634 | * |
||
| 1635 | * @param string $title Page title |
||
| 1636 | * @return string Generated url segment |
||
| 1637 | */ |
||
| 1638 | public function generateURLSegment($title){ |
||
| 1650 | |||
| 1651 | /** |
||
| 1652 | * Gets the URL segment for the latest draft version of this page. |
||
| 1653 | * |
||
| 1654 | * @return string |
||
| 1655 | */ |
||
| 1656 | public function getStageURLSegment() { |
||
| 1662 | |||
| 1663 | /** |
||
| 1664 | * Gets the URL segment for the currently published version of this page. |
||
| 1665 | * |
||
| 1666 | * @return string |
||
| 1667 | */ |
||
| 1668 | public function getLiveURLSegment() { |
||
| 1674 | |||
| 1675 | /** |
||
| 1676 | * Returns the pages that depend on this page. This includes virtual pages, pages that link to it, etc. |
||
| 1677 | * |
||
| 1678 | * @param bool $includeVirtuals Set to false to exlcude virtual pages. |
||
| 1679 | * @return ArrayList |
||
| 1680 | */ |
||
| 1681 | public function DependentPages($includeVirtuals = true) { |
||
| 1731 | |||
| 1732 | /** |
||
| 1733 | * Return all virtual pages that link to this page. |
||
| 1734 | * |
||
| 1735 | * @return DataList |
||
| 1736 | */ |
||
| 1737 | public function VirtualPages() { |
||
| 1747 | |||
| 1748 | /** |
||
| 1749 | * Returns a FieldList with which to create the main editing form. |
||
| 1750 | * |
||
| 1751 | * You can override this in your child classes to add extra fields - first get the parent fields using |
||
| 1752 | * parent::getCMSFields(), then use addFieldToTab() on the FieldList. |
||
| 1753 | * |
||
| 1754 | * See {@link getSettingsFields()} for a different set of fields concerned with configuration aspects on the record, |
||
| 1755 | * e.g. access control. |
||
| 1756 | * |
||
| 1757 | * @return FieldList The fields to be displayed in the CMS |
||
| 1758 | */ |
||
| 1759 | public function getCMSFields() { |
||
| 1939 | |||
| 1940 | |||
| 1941 | /** |
||
| 1942 | * Returns fields related to configuration aspects on this record, e.g. access control. See {@link getCMSFields()} |
||
| 1943 | * for content-related fields. |
||
| 1944 | * |
||
| 1945 | * @return FieldList |
||
| 1946 | */ |
||
| 1947 | public function getSettingsFields() { |
||
| 2053 | |||
| 2054 | /** |
||
| 2055 | * @param bool $includerelations A boolean value to indicate if the labels returned should include relation fields |
||
| 2056 | * @return array |
||
| 2057 | */ |
||
| 2058 | public function fieldLabels($includerelations = true) { |
||
| 2096 | |||
| 2097 | /** |
||
| 2098 | * Get the actions available in the CMS for this page - eg Save, Publish. |
||
| 2099 | * |
||
| 2100 | * Frontend scripts and styles know how to handle the following FormFields: |
||
| 2101 | * - top-level FormActions appear as standalone buttons |
||
| 2102 | * - top-level CompositeField with FormActions within appear as grouped buttons |
||
| 2103 | * - TabSet & Tabs appear as a drop ups |
||
| 2104 | * - FormActions within the Tab are restyled as links |
||
| 2105 | * - major actions can provide alternate states for richer presentation (see ssui.button widget extension) |
||
| 2106 | * |
||
| 2107 | * @return FieldList The available actions for this page. |
||
| 2108 | */ |
||
| 2109 | public function getCMSActions() { |
||
| 2110 | $existsOnLive = $this->isPublished(); |
||
| 2111 | |||
| 2112 | // Major actions appear as buttons immediately visible as page actions. |
||
| 2113 | $majorActions = CompositeField::create()->setName('MajorActions')->setTag('fieldset')->addExtraClass('btn-group ss-ui-buttonset noborder'); |
||
| 2114 | |||
| 2115 | // Minor options are hidden behind a drop-up and appear as links (although they are still FormActions). |
||
| 2116 | $rootTabSet = new TabSet('ActionMenus'); |
||
| 2117 | $moreOptions = new Tab( |
||
| 2118 | 'MoreOptions', |
||
| 2119 | _t('SiteTree.MoreOptions', 'More options', 'Expands a view for more buttons') |
||
| 2120 | ); |
||
| 2121 | $rootTabSet->push($moreOptions); |
||
| 2122 | $rootTabSet->addExtraClass('ss-ui-action-tabset action-menus noborder'); |
||
| 2123 | |||
| 2124 | // Render page information into the "more-options" drop-up, on the top. |
||
| 2125 | $live = Versioned::get_one_by_stage('SiteTree', Versioned::LIVE, array( |
||
| 2126 | '"SiteTree"."ID"' => $this->ID |
||
| 2127 | )); |
||
| 2128 | $moreOptions->push( |
||
| 2129 | new LiteralField('Information', |
||
| 2130 | $this->customise(array( |
||
| 2131 | 'Live' => $live, |
||
| 2132 | 'ExistsOnLive' => $existsOnLive |
||
| 2133 | ))->renderWith('SiteTree_Information') |
||
| 2134 | ) |
||
| 2135 | ); |
||
| 2136 | |||
| 2137 | $moreOptions->push(AddToCampaignHandler_FormAction::create()); |
||
| 2138 | |||
| 2139 | // "readonly"/viewing version that isn't the current version of the record |
||
| 2140 | $stageOrLiveRecord = Versioned::get_one_by_stage($this->class, Versioned::get_stage(), array( |
||
| 2141 | '"SiteTree"."ID"' => $this->ID |
||
| 2142 | )); |
||
| 2143 | if($stageOrLiveRecord && $stageOrLiveRecord->Version != $this->Version) { |
||
| 2144 | $moreOptions->push(FormAction::create('email', _t('CMSMain.EMAIL', 'Email'))); |
||
| 2145 | $moreOptions->push(FormAction::create('rollback', _t('CMSMain.ROLLBACK', 'Roll back to this version'))); |
||
| 2146 | |||
| 2147 | $actions = new FieldList(array($majorActions, $rootTabSet)); |
||
| 2148 | |||
| 2149 | // getCMSActions() can be extended with updateCMSActions() on a extension |
||
| 2150 | $this->extend('updateCMSActions', $actions); |
||
| 2151 | |||
| 2152 | return $actions; |
||
| 2153 | } |
||
| 2154 | |||
| 2155 | if($this->isPublished() && $this->canPublish() && !$this->getIsDeletedFromStage() && $this->canUnpublish()) { |
||
| 2156 | // "unpublish" |
||
| 2157 | $moreOptions->push( |
||
| 2158 | FormAction::create('unpublish', _t('SiteTree.BUTTONUNPUBLISH', 'Unpublish'), 'delete') |
||
| 2159 | ->setDescription(_t('SiteTree.BUTTONUNPUBLISHDESC', 'Remove this page from the published site')) |
||
| 2160 | ->addExtraClass('ss-ui-action-destructive') |
||
| 2161 | ); |
||
| 2162 | } |
||
| 2163 | |||
| 2164 | if($this->stagesDiffer(Versioned::DRAFT, Versioned::LIVE) && !$this->getIsDeletedFromStage()) { |
||
| 2165 | if($this->isPublished() && $this->canEdit()) { |
||
| 2166 | // "rollback" |
||
| 2167 | $moreOptions->push( |
||
| 2168 | FormAction::create('rollback', _t('SiteTree.BUTTONCANCELDRAFT', 'Cancel draft changes'), 'delete') |
||
| 2169 | ->setDescription(_t('SiteTree.BUTTONCANCELDRAFTDESC', 'Delete your draft and revert to the currently published page')) |
||
| 2170 | ); |
||
| 2171 | } |
||
| 2172 | } |
||
| 2173 | |||
| 2174 | if($this->canEdit()) { |
||
| 2175 | if($this->getIsDeletedFromStage()) { |
||
| 2176 | // The usual major actions are not available, so we provide alternatives here. |
||
| 2177 | if($existsOnLive) { |
||
| 2178 | // "restore" |
||
| 2179 | $majorActions->push(FormAction::create('revert',_t('CMSMain.RESTORE','Restore'))); |
||
| 2180 | if($this->canDelete() && $this->canUnpublish()) { |
||
| 2181 | // "delete from live" |
||
| 2182 | $majorActions->push( |
||
| 2183 | FormAction::create('deletefromlive',_t('CMSMain.DELETEFP','Delete')) |
||
| 2184 | ->addExtraClass('ss-ui-action-destructive') |
||
| 2185 | ); |
||
| 2186 | } |
||
| 2187 | } else { |
||
| 2188 | // Determine if we should force a restore to root (where once it was a subpage) |
||
| 2189 | $restoreToRoot = $this->isParentArchived(); |
||
| 2190 | |||
| 2191 | // "restore" |
||
| 2192 | $title = $restoreToRoot |
||
| 2193 | ? _t('CMSMain.RESTORE_TO_ROOT','Restore draft at top level') |
||
| 2194 | : _t('CMSMain.RESTORE','Restore draft'); |
||
| 2195 | $description = $restoreToRoot |
||
| 2196 | ? _t('CMSMain.RESTORE_TO_ROOT_DESC','Restore the archived version to draft as a top level page') |
||
| 2197 | : _t('CMSMain.RESTORE_DESC', 'Restore the archived version to draft'); |
||
| 2198 | $majorActions->push( |
||
| 2199 | FormAction::create('restore', $title) |
||
| 2200 | ->setDescription($description) |
||
| 2201 | ->setAttribute('data-to-root', $restoreToRoot) |
||
| 2202 | ->setAttribute('data-icon', 'decline') |
||
| 2203 | ); |
||
| 2204 | } |
||
| 2205 | } else { |
||
| 2206 | if($this->canDelete()) { |
||
| 2207 | // delete |
||
| 2208 | $moreOptions->push( |
||
| 2209 | FormAction::create('delete',_t('CMSMain.DELETE','Delete draft')) |
||
| 2210 | ->addExtraClass('delete ss-ui-action-destructive') |
||
| 2211 | ); |
||
| 2212 | } |
||
| 2213 | if($this->canArchive()) { |
||
| 2214 | // "archive" |
||
| 2215 | $moreOptions->push( |
||
| 2216 | FormAction::create('archive',_t('CMSMain.ARCHIVE','Archive')) |
||
| 2217 | ->setDescription(_t( |
||
| 2218 | 'SiteTree.BUTTONARCHIVEDESC', |
||
| 2219 | 'Unpublish and send to archive' |
||
| 2220 | )) |
||
| 2221 | ->addExtraClass('delete ss-ui-action-destructive') |
||
| 2222 | ); |
||
| 2223 | } |
||
| 2224 | |||
| 2225 | // "save", supports an alternate state that is still clickable, but notifies the user that the action is not needed. |
||
| 2226 | $majorActions->push( |
||
| 2227 | FormAction::create('save', _t('SiteTree.BUTTONSAVED', 'Saved')) |
||
| 2228 | ->setAttribute('data-icon', 'accept') |
||
| 2229 | ->setAttribute('data-icon-alternate', 'addpage') |
||
| 2230 | ->setAttribute('data-text-alternate', _t('CMSMain.SAVEDRAFT','Save draft')) |
||
| 2231 | ); |
||
| 2232 | } |
||
| 2233 | } |
||
| 2234 | |||
| 2235 | if($this->canPublish() && !$this->getIsDeletedFromStage()) { |
||
| 2236 | // "publish", as with "save", it supports an alternate state to show when action is needed. |
||
| 2237 | $majorActions->push( |
||
| 2238 | $publish = FormAction::create('publish', _t('SiteTree.BUTTONPUBLISHED', 'Published')) |
||
| 2239 | ->setAttribute('data-icon', 'accept') |
||
| 2240 | ->setAttribute('data-icon-alternate', 'disk') |
||
| 2241 | ->setAttribute('data-text-alternate', _t('SiteTree.BUTTONSAVEPUBLISH', 'Save & publish')) |
||
| 2242 | ); |
||
| 2243 | |||
| 2244 | // Set up the initial state of the button to reflect the state of the underlying SiteTree object. |
||
| 2245 | if($this->stagesDiffer(Versioned::DRAFT, Versioned::LIVE)) { |
||
| 2246 | $publish->addExtraClass('ss-ui-alternate'); |
||
| 2247 | } |
||
| 2248 | } |
||
| 2249 | |||
| 2250 | $actions = new FieldList(array($majorActions, $rootTabSet)); |
||
| 2251 | |||
| 2252 | // Hook for extensions to add/remove actions. |
||
| 2253 | $this->extend('updateCMSActions', $actions); |
||
| 2254 | |||
| 2255 | return $actions; |
||
| 2256 | } |
||
| 2257 | |||
| 2258 | public function onAfterPublish() { |
||
| 2266 | |||
| 2267 | /** |
||
| 2268 | * Update draft dependant pages |
||
| 2269 | */ |
||
| 2270 | public function onAfterRevertToLive() { |
||
| 2282 | |||
| 2283 | /** |
||
| 2284 | * Determine if this page references a parent which is archived, and not available in stage |
||
| 2285 | * |
||
| 2286 | * @return bool True if there is an archived parent |
||
| 2287 | */ |
||
| 2288 | protected function isParentArchived() { |
||
| 2297 | |||
| 2298 | /** |
||
| 2299 | * Restore the content in the active copy of this SiteTree page to the stage site. |
||
| 2300 | * |
||
| 2301 | * @return self |
||
| 2302 | */ |
||
| 2303 | public function doRestoreToStage() { |
||
| 2304 | $this->invokeWithExtensions('onBeforeRestoreToStage', $this); |
||
| 2305 | |||
| 2306 | // Ensure that the parent page is restored, otherwise restore to root |
||
| 2307 | if($this->isParentArchived()) { |
||
| 2308 | $this->ParentID = 0; |
||
| 2309 | } |
||
| 2310 | |||
| 2311 | // if no record can be found on draft stage (meaning it has been "deleted from draft" before), |
||
| 2312 | // create an empty record |
||
| 2313 | if(!DB::prepared_query("SELECT \"ID\" FROM \"SiteTree\" WHERE \"ID\" = ?", array($this->ID))->value()) { |
||
| 2314 | $conn = DB::get_conn(); |
||
| 2315 | if(method_exists($conn, 'allowPrimaryKeyEditing')) $conn->allowPrimaryKeyEditing('SiteTree', true); |
||
| 2316 | DB::prepared_query("INSERT INTO \"SiteTree\" (\"ID\") VALUES (?)", array($this->ID)); |
||
| 2317 | if(method_exists($conn, 'allowPrimaryKeyEditing')) $conn->allowPrimaryKeyEditing('SiteTree', false); |
||
| 2318 | } |
||
| 2319 | |||
| 2320 | $oldReadingMode = Versioned::get_reading_mode(); |
||
| 2321 | Versioned::set_stage(Versioned::DRAFT); |
||
| 2322 | $this->forceChange(); |
||
| 2323 | $this->write(); |
||
| 2324 | |||
| 2325 | $result = DataObject::get_by_id($this->class, $this->ID); |
||
| 2326 | |||
| 2327 | // Need to update pages linking to this one as no longer broken |
||
| 2328 | foreach($result->DependentPages(false) as $page) { |
||
| 2329 | // $page->write() calls syncLinkTracking, which does all the hard work for us. |
||
| 2330 | $page->write(); |
||
| 2331 | } |
||
| 2332 | |||
| 2333 | Versioned::set_reading_mode($oldReadingMode); |
||
| 2334 | |||
| 2335 | $this->invokeWithExtensions('onAfterRestoreToStage', $this); |
||
| 2336 | |||
| 2337 | return $result; |
||
| 2338 | } |
||
| 2339 | |||
| 2340 | /** |
||
| 2341 | * Check if this page is new - that is, if it has yet to have been written to the database. |
||
| 2342 | * |
||
| 2343 | * @return bool |
||
| 2344 | */ |
||
| 2345 | public function isNew() { |
||
| 2356 | |||
| 2357 | /** |
||
| 2358 | * Get the class dropdown used in the CMS to change the class of a page. This returns the list of options in the |
||
| 2359 | * dropdown as a Map from class name to singular name. Filters by {@link SiteTree->canCreate()}, as well as |
||
| 2360 | * {@link SiteTree::$needs_permission}. |
||
| 2361 | * |
||
| 2362 | * @return array |
||
| 2363 | */ |
||
| 2364 | protected function getClassDropdown() { |
||
| 2408 | |||
| 2409 | /** |
||
| 2410 | * Returns an array of the class names of classes that are allowed to be children of this class. |
||
| 2411 | * |
||
| 2412 | * @return string[] |
||
| 2413 | */ |
||
| 2414 | public function allowedChildren() { |
||
| 2434 | |||
| 2435 | /** |
||
| 2436 | * Returns the class name of the default class for children of this page. |
||
| 2437 | * |
||
| 2438 | * @return string |
||
| 2439 | */ |
||
| 2440 | public function defaultChild() { |
||
| 2449 | |||
| 2450 | /** |
||
| 2451 | * Returns the class name of the default class for the parent of this page. |
||
| 2452 | * |
||
| 2453 | * @return string |
||
| 2454 | */ |
||
| 2455 | public function defaultParent() { |
||
| 2458 | |||
| 2459 | /** |
||
| 2460 | * Get the title for use in menus for this page. If the MenuTitle field is set it returns that, else it returns the |
||
| 2461 | * Title field. |
||
| 2462 | * |
||
| 2463 | * @return string |
||
| 2464 | */ |
||
| 2465 | public function getMenuTitle(){ |
||
| 2466 | if($value = $this->getField("MenuTitle")) { |
||
| 2467 | return $value; |
||
| 2468 | } else { |
||
| 2469 | return $this->getField("Title"); |
||
| 2470 | } |
||
| 2471 | } |
||
| 2472 | |||
| 2473 | |||
| 2474 | /** |
||
| 2475 | * Set the menu title for this page. |
||
| 2476 | * |
||
| 2477 | * @param string $value |
||
| 2478 | */ |
||
| 2479 | public function setMenuTitle($value) { |
||
| 2486 | |||
| 2487 | /** |
||
| 2488 | * A flag provides the user with additional data about the current page status, for example a "removed from draft" |
||
| 2489 | * status. Each page can have more than one status flag. Returns a map of a unique key to a (localized) title for |
||
| 2490 | * the flag. The unique key can be reused as a CSS class. Use the 'updateStatusFlags' extension point to customize |
||
| 2491 | * the flags. |
||
| 2492 | * |
||
| 2493 | * Example (simple): |
||
| 2494 | * "deletedonlive" => "Deleted" |
||
| 2495 | * |
||
| 2496 | * Example (with optional title attribute): |
||
| 2497 | * "deletedonlive" => array('text' => "Deleted", 'title' => 'This page has been deleted') |
||
| 2498 | * |
||
| 2499 | * @param bool $cached Whether to serve the fields from cache; false regenerate them |
||
| 2500 | * @return array |
||
| 2501 | */ |
||
| 2502 | public function getStatusFlags($cached = true) { |
||
| 2536 | |||
| 2537 | /** |
||
| 2538 | * getTreeTitle will return three <span> html DOM elements, an empty <span> with the class 'jstree-pageicon' in |
||
| 2539 | * front, following by a <span> wrapping around its MenutTitle, then following by a <span> indicating its |
||
| 2540 | * publication status. |
||
| 2541 | * |
||
| 2542 | * @return string An HTML string ready to be directly used in a template |
||
| 2543 | */ |
||
| 2544 | public function getTreeTitle() { |
||
| 2573 | |||
| 2574 | /** |
||
| 2575 | * Returns the page in the current page stack of the given level. Level(1) will return the main menu item that |
||
| 2576 | * we're currently inside, etc. |
||
| 2577 | * |
||
| 2578 | * @param int $level |
||
| 2579 | * @return SiteTree |
||
| 2580 | */ |
||
| 2581 | public function Level($level) { |
||
| 2590 | |||
| 2591 | /** |
||
| 2592 | * Gets the depth of this page in the sitetree, where 1 is the root level |
||
| 2593 | * |
||
| 2594 | * @return int |
||
| 2595 | */ |
||
| 2596 | public function getPageLevel() { |
||
| 2602 | |||
| 2603 | /** |
||
| 2604 | * Return the CSS classes to apply to this node in the CMS tree. |
||
| 2605 | * |
||
| 2606 | * @param string $numChildrenMethod |
||
| 2607 | * @return string |
||
| 2608 | */ |
||
| 2609 | public function CMSTreeClasses($numChildrenMethod="numChildren") { |
||
| 2640 | |||
| 2641 | /** |
||
| 2642 | * Compares current draft with live version, and returns true if no draft version of this page exists but the page |
||
| 2643 | * is still published (eg, after triggering "Delete from draft site" in the CMS). |
||
| 2644 | * |
||
| 2645 | * @return bool |
||
| 2646 | */ |
||
| 2647 | public function getIsDeletedFromStage() { |
||
| 2656 | |||
| 2657 | /** |
||
| 2658 | * Return true if this page exists on the live site |
||
| 2659 | * |
||
| 2660 | * @return bool |
||
| 2661 | */ |
||
| 2662 | public function getExistsOnLive() { |
||
| 2665 | |||
| 2666 | /** |
||
| 2667 | * Compares current draft with live version, and returns true if these versions differ, meaning there have been |
||
| 2668 | * unpublished changes to the draft site. |
||
| 2669 | * |
||
| 2670 | * @return bool |
||
| 2671 | */ |
||
| 2672 | public function getIsModifiedOnStage() { |
||
| 2684 | |||
| 2685 | /** |
||
| 2686 | * Compares current draft with live version, and returns true if no live version exists, meaning the page was never |
||
| 2687 | * published. |
||
| 2688 | * |
||
| 2689 | * @return bool |
||
| 2690 | */ |
||
| 2691 | public function getIsAddedToStage() { |
||
| 2700 | |||
| 2701 | /** |
||
| 2702 | * Stops extendCMSFields() being called on getCMSFields(). This is useful when you need access to fields added by |
||
| 2703 | * subclasses of SiteTree in a extension. Call before calling parent::getCMSFields(), and reenable afterwards. |
||
| 2704 | */ |
||
| 2705 | static public function disableCMSFieldsExtensions() { |
||
| 2708 | |||
| 2709 | /** |
||
| 2710 | * Reenables extendCMSFields() being called on getCMSFields() after it has been disabled by |
||
| 2711 | * disableCMSFieldsExtensions(). |
||
| 2712 | */ |
||
| 2713 | static public function enableCMSFieldsExtensions() { |
||
| 2716 | |||
| 2717 | public function providePermissions() { |
||
| 2751 | |||
| 2752 | /** |
||
| 2753 | * Return the translated Singular name. |
||
| 2754 | * |
||
| 2755 | * @return string |
||
| 2756 | */ |
||
| 2757 | public function i18n_singular_name() { |
||
| 2762 | |||
| 2763 | /** |
||
| 2764 | * Overloaded to also provide entities for 'Page' class which is usually located in custom code, hence textcollector |
||
| 2765 | * picks it up for the wrong folder. |
||
| 2766 | * |
||
| 2767 | * @return array |
||
| 2768 | */ |
||
| 2769 | public function provideI18nEntities() { |
||
| 2785 | |||
| 2786 | /** |
||
| 2787 | * Returns 'root' if the current page has no parent, or 'subpage' otherwise |
||
| 2788 | * |
||
| 2789 | * @return string |
||
| 2790 | */ |
||
| 2791 | public function getParentType() { |
||
| 2794 | |||
| 2795 | /** |
||
| 2796 | * Clear the permissions cache for SiteTree |
||
| 2797 | */ |
||
| 2798 | public static function reset() { |
||
| 2801 | |||
| 2802 | static public function on_db_reset() { |
||
| 2805 | |||
| 2806 | } |
||
| 2807 |