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 |
||
| 92 | class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvider,CMSPreviewable { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Indicates what kind of children this page type can have. |
||
| 96 | * This can be an array of allowed child classes, or the string "none" - |
||
| 97 | * indicating that this page type can't have children. |
||
| 98 | * If a classname is prefixed by "*", such as "*Page", then only that |
||
| 99 | * class is allowed - no subclasses. Otherwise, the class and all its |
||
| 100 | * subclasses are allowed. |
||
| 101 | * To control allowed children on root level (no parent), use {@link $can_be_root}. |
||
| 102 | * |
||
| 103 | * Note that this setting is cached when used in the CMS, use the "flush" query parameter to clear it. |
||
| 104 | * |
||
| 105 | * @config |
||
| 106 | * @var array |
||
| 107 | */ |
||
| 108 | private static $allowed_children = array("SilverStripe\\CMS\\Model\\SiteTree"); |
||
| 109 | |||
| 110 | /** |
||
| 111 | * The default child class for this page. |
||
| 112 | * Note: Value might be cached, see {@link $allowed_chilren}. |
||
| 113 | * |
||
| 114 | * @config |
||
| 115 | * @var string |
||
| 116 | */ |
||
| 117 | private static $default_child = "Page"; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Default value for SiteTree.ClassName enum |
||
| 121 | * {@see DBClassName::getDefault} |
||
| 122 | * |
||
| 123 | * @config |
||
| 124 | * @var string |
||
| 125 | */ |
||
| 126 | private static $default_classname = "Page"; |
||
|
|
|||
| 127 | |||
| 128 | /** |
||
| 129 | * The default parent class for this page. |
||
| 130 | * Note: Value might be cached, see {@link $allowed_chilren}. |
||
| 131 | * |
||
| 132 | * @config |
||
| 133 | * @var string |
||
| 134 | */ |
||
| 135 | private static $default_parent = null; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Controls whether a page can be in the root of the site tree. |
||
| 139 | * Note: Value might be cached, see {@link $allowed_chilren}. |
||
| 140 | * |
||
| 141 | * @config |
||
| 142 | * @var bool |
||
| 143 | */ |
||
| 144 | private static $can_be_root = true; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * List of permission codes a user can have to allow a user to create a page of this type. |
||
| 148 | * Note: Value might be cached, see {@link $allowed_chilren}. |
||
| 149 | * |
||
| 150 | * @config |
||
| 151 | * @var array |
||
| 152 | */ |
||
| 153 | private static $need_permission = null; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * If you extend a class, and don't want to be able to select the old class |
||
| 157 | * in the cms, set this to the old class name. Eg, if you extended Product |
||
| 158 | * to make ImprovedProduct, then you would set $hide_ancestor to Product. |
||
| 159 | * |
||
| 160 | * @config |
||
| 161 | * @var string |
||
| 162 | */ |
||
| 163 | private static $hide_ancestor = null; |
||
| 164 | |||
| 165 | private static $db = array( |
||
| 166 | "URLSegment" => "Varchar(255)", |
||
| 167 | "Title" => "Varchar(255)", |
||
| 168 | "MenuTitle" => "Varchar(100)", |
||
| 169 | "Content" => "HTMLText", |
||
| 170 | "MetaDescription" => "Text", |
||
| 171 | "ExtraMeta" => "HTMLFragment(['whitelist' => ['meta', 'link']])", |
||
| 172 | "ShowInMenus" => "Boolean", |
||
| 173 | "ShowInSearch" => "Boolean", |
||
| 174 | "Sort" => "Int", |
||
| 175 | "HasBrokenFile" => "Boolean", |
||
| 176 | "HasBrokenLink" => "Boolean", |
||
| 177 | "ReportClass" => "Varchar", |
||
| 178 | "CanViewType" => "Enum('Anyone, LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit')", |
||
| 179 | "CanEditType" => "Enum('LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit')", |
||
| 180 | ); |
||
| 181 | |||
| 182 | private static $indexes = array( |
||
| 183 | "URLSegment" => true, |
||
| 184 | ); |
||
| 185 | |||
| 186 | private static $many_many = array( |
||
| 187 | "ViewerGroups" => "SilverStripe\\Security\\Group", |
||
| 188 | "EditorGroups" => "SilverStripe\\Security\\Group", |
||
| 189 | ); |
||
| 190 | |||
| 191 | private static $has_many = array( |
||
| 192 | "VirtualPages" => "SilverStripe\\CMS\\Model\\VirtualPage.CopyContentFrom" |
||
| 193 | ); |
||
| 194 | |||
| 195 | private static $owned_by = array( |
||
| 196 | "VirtualPages" |
||
| 197 | ); |
||
| 198 | |||
| 199 | private static $casting = array( |
||
| 200 | "Breadcrumbs" => "HTMLFragment", |
||
| 201 | "LastEdited" => "Datetime", |
||
| 202 | "Created" => "Datetime", |
||
| 203 | 'Link' => 'Text', |
||
| 204 | 'RelativeLink' => 'Text', |
||
| 205 | 'AbsoluteLink' => 'Text', |
||
| 206 | 'CMSEditLink' => 'Text', |
||
| 207 | 'TreeTitle' => 'HTMLFragment', |
||
| 208 | 'MetaTags' => 'HTMLFragment', |
||
| 209 | ); |
||
| 210 | |||
| 211 | private static $defaults = array( |
||
| 212 | "ShowInMenus" => 1, |
||
| 213 | "ShowInSearch" => 1, |
||
| 214 | "CanViewType" => "Inherit", |
||
| 215 | "CanEditType" => "Inherit" |
||
| 216 | ); |
||
| 217 | |||
| 218 | private static $table_name = 'SiteTree'; |
||
| 219 | |||
| 220 | private static $versioning = array( |
||
| 221 | "Stage", "Live" |
||
| 222 | ); |
||
| 223 | |||
| 224 | private static $default_sort = "\"Sort\""; |
||
| 225 | |||
| 226 | /** |
||
| 227 | * If this is false, the class cannot be created in the CMS by regular content authors, only by ADMINs. |
||
| 228 | * @var boolean |
||
| 229 | * @config |
||
| 230 | */ |
||
| 231 | private static $can_create = true; |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Icon to use in the CMS page tree. This should be the full filename, relative to the webroot. |
||
| 235 | * Also supports custom CSS rule contents (applied to the correct selector for the tree UI implementation). |
||
| 236 | * |
||
| 237 | * @see CMSMain::generateTreeStylingCSS() |
||
| 238 | * @config |
||
| 239 | * @var string |
||
| 240 | */ |
||
| 241 | private static $icon = null; |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @config |
||
| 245 | * @var string Description of the class functionality, typically shown to a user |
||
| 246 | * when selecting which page type to create. Translated through {@link provideI18nEntities()}. |
||
| 247 | */ |
||
| 248 | private static $description = 'Generic content page'; |
||
| 249 | |||
| 250 | private static $extensions = array( |
||
| 251 | 'SilverStripe\\ORM\\Hierarchy\\Hierarchy', |
||
| 252 | 'SilverStripe\\ORM\\Versioning\\Versioned', |
||
| 253 | "SilverStripe\\CMS\\Model\\SiteTreeLinkTracking" |
||
| 254 | ); |
||
| 255 | |||
| 256 | private static $searchable_fields = array( |
||
| 257 | 'Title', |
||
| 258 | 'Content', |
||
| 259 | ); |
||
| 260 | |||
| 261 | private static $field_labels = array( |
||
| 262 | 'URLSegment' => 'URL' |
||
| 263 | ); |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @config |
||
| 267 | */ |
||
| 268 | private static $nested_urls = true; |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @config |
||
| 272 | */ |
||
| 273 | private static $create_default_pages = true; |
||
| 274 | |||
| 275 | /** |
||
| 276 | * This controls whether of not extendCMSFields() is called by getCMSFields. |
||
| 277 | */ |
||
| 278 | private static $runCMSFieldsExtensions = true; |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Cache for canView/Edit/Publish/Delete permissions. |
||
| 282 | * Keyed by permission type (e.g. 'edit'), with an array |
||
| 283 | * of IDs mapped to their boolean permission ability (true=allow, false=deny). |
||
| 284 | * See {@link batch_permission_check()} for details. |
||
| 285 | */ |
||
| 286 | private static $cache_permissions = array(); |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @config |
||
| 290 | * @var boolean |
||
| 291 | */ |
||
| 292 | private static $enforce_strict_hierarchy = true; |
||
| 293 | |||
| 294 | /** |
||
| 295 | * The value used for the meta generator tag. Leave blank to omit the tag. |
||
| 296 | * |
||
| 297 | * @config |
||
| 298 | * @var string |
||
| 299 | */ |
||
| 300 | private static $meta_generator = 'SilverStripe - http://silverstripe.org'; |
||
| 301 | |||
| 302 | protected $_cache_statusFlags = null; |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Fetches the {@link SiteTree} object that maps to a link. |
||
| 306 | * |
||
| 307 | * If you have enabled {@link SiteTree::config()->nested_urls} on this site, then you can use a nested link such as |
||
| 308 | * "about-us/staff/", and this function will traverse down the URL chain and grab the appropriate link. |
||
| 309 | * |
||
| 310 | * Note that if no model can be found, this method will fall over to a extended alternateGetByLink method provided |
||
| 311 | * by a extension attached to {@link SiteTree} |
||
| 312 | * |
||
| 313 | * @param string $link The link of the page to search for |
||
| 314 | * @param bool $cache True (default) to use caching, false to force a fresh search from the database |
||
| 315 | * @return SiteTree |
||
| 316 | */ |
||
| 317 | static public function get_by_link($link, $cache = true) { |
||
| 318 | if(trim($link, '/')) { |
||
| 319 | $link = trim(Director::makeRelative($link), '/'); |
||
| 320 | } else { |
||
| 321 | $link = RootURLController::get_homepage_link(); |
||
| 322 | } |
||
| 323 | |||
| 324 | $parts = preg_split('|/+|', $link); |
||
| 325 | |||
| 326 | // Grab the initial root level page to traverse down from. |
||
| 327 | $URLSegment = array_shift($parts); |
||
| 328 | $conditions = array('"SiteTree"."URLSegment"' => rawurlencode($URLSegment)); |
||
| 329 | if(self::config()->nested_urls) { |
||
| 330 | $conditions[] = array('"SiteTree"."ParentID"' => 0); |
||
| 331 | } |
||
| 332 | /** @var SiteTree $sitetree */ |
||
| 333 | $sitetree = DataObject::get_one(__CLASS__, $conditions, $cache); |
||
| 334 | |||
| 335 | /// Fall back on a unique URLSegment for b/c. |
||
| 336 | if( !$sitetree |
||
| 337 | && self::config()->nested_urls |
||
| 338 | && $sitetree = DataObject::get_one(__CLASS__, array( |
||
| 339 | '"SiteTree"."URLSegment"' => $URLSegment |
||
| 340 | ), $cache) |
||
| 341 | ) { |
||
| 342 | return $sitetree; |
||
| 343 | } |
||
| 344 | |||
| 345 | // Attempt to grab an alternative page from extensions. |
||
| 346 | if(!$sitetree) { |
||
| 347 | $parentID = self::config()->nested_urls ? 0 : null; |
||
| 348 | |||
| 349 | View Code Duplication | if($alternatives = static::singleton()->extend('alternateGetByLink', $URLSegment, $parentID)) { |
|
| 350 | foreach($alternatives as $alternative) { |
||
| 351 | if($alternative) { |
||
| 352 | $sitetree = $alternative; |
||
| 353 | } |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | if(!$sitetree) { |
||
| 358 | return null; |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | // Check if we have any more URL parts to parse. |
||
| 363 | if(!self::config()->nested_urls || !count($parts)) { |
||
| 364 | return $sitetree; |
||
| 365 | } |
||
| 366 | |||
| 367 | // Traverse down the remaining URL segments and grab the relevant SiteTree objects. |
||
| 368 | foreach($parts as $segment) { |
||
| 369 | $next = DataObject::get_one('SilverStripe\\CMS\\Model\\SiteTree', array( |
||
| 370 | '"SiteTree"."URLSegment"' => $segment, |
||
| 371 | '"SiteTree"."ParentID"' => $sitetree->ID |
||
| 372 | ), |
||
| 373 | $cache |
||
| 374 | ); |
||
| 375 | |||
| 376 | if(!$next) { |
||
| 377 | $parentID = (int) $sitetree->ID; |
||
| 378 | |||
| 379 | View Code Duplication | if($alternatives = static::singleton()->extend('alternateGetByLink', $segment, $parentID)) { |
|
| 380 | foreach($alternatives as $alternative) if($alternative) $next = $alternative; |
||
| 381 | } |
||
| 382 | |||
| 383 | if(!$next) { |
||
| 384 | return null; |
||
| 385 | } |
||
| 386 | } |
||
| 387 | |||
| 388 | $sitetree->destroy(); |
||
| 389 | $sitetree = $next; |
||
| 390 | } |
||
| 391 | |||
| 392 | return $sitetree; |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Return a subclass map of SiteTree that shouldn't be hidden through {@link SiteTree::$hide_ancestor} |
||
| 397 | * |
||
| 398 | * @return array |
||
| 399 | */ |
||
| 400 | public static function page_type_classes() { |
||
| 401 | $classes = ClassInfo::getValidSubClasses(); |
||
| 402 | |||
| 403 | $baseClassIndex = array_search(__CLASS__, $classes); |
||
| 404 | if($baseClassIndex !== false) { |
||
| 405 | unset($classes[$baseClassIndex]); |
||
| 406 | } |
||
| 407 | |||
| 408 | $kill_ancestors = array(); |
||
| 409 | |||
| 410 | // figure out if there are any classes we don't want to appear |
||
| 411 | foreach($classes as $class) { |
||
| 412 | $instance = singleton($class); |
||
| 413 | |||
| 414 | // do any of the progeny want to hide an ancestor? |
||
| 415 | if($ancestor_to_hide = $instance->stat('hide_ancestor')) { |
||
| 416 | // note for killing later |
||
| 417 | $kill_ancestors[] = $ancestor_to_hide; |
||
| 418 | } |
||
| 419 | } |
||
| 420 | |||
| 421 | // If any of the descendents don't want any of the elders to show up, cruelly render the elders surplus to |
||
| 422 | // requirements |
||
| 423 | if($kill_ancestors) { |
||
| 424 | $kill_ancestors = array_unique($kill_ancestors); |
||
| 425 | foreach($kill_ancestors as $mark) { |
||
| 426 | // unset from $classes |
||
| 427 | $idx = array_search($mark, $classes, true); |
||
| 428 | if ($idx !== false) { |
||
| 429 | unset($classes[$idx]); |
||
| 430 | } |
||
| 431 | } |
||
| 432 | } |
||
| 433 | |||
| 434 | return $classes; |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Replace a "[sitetree_link id=n]" shortcode with a link to the page with the corresponding ID. |
||
| 439 | * |
||
| 440 | * @param array $arguments |
||
| 441 | * @param string $content |
||
| 442 | * @param ShortcodeParser $parser |
||
| 443 | * @return string |
||
| 444 | */ |
||
| 445 | static public function link_shortcode_handler($arguments, $content = null, $parser = null) { |
||
| 446 | if(!isset($arguments['id']) || !is_numeric($arguments['id'])) { |
||
| 447 | return null; |
||
| 448 | } |
||
| 449 | |||
| 450 | /** @var SiteTree $page */ |
||
| 451 | if ( |
||
| 452 | !($page = DataObject::get_by_id(__CLASS__, $arguments['id'])) // Get the current page by ID. |
||
| 453 | && !($page = Versioned::get_latest_version(__CLASS__, $arguments['id'])) // Attempt link to old version. |
||
| 454 | ) { |
||
| 455 | return null; // There were no suitable matches at all. |
||
| 456 | } |
||
| 457 | |||
| 458 | /** @var SiteTree $page */ |
||
| 459 | $link = Convert::raw2att($page->Link()); |
||
| 460 | |||
| 461 | if($content) { |
||
| 462 | return sprintf('<a href="%s">%s</a>', $link, $parser->parse($content)); |
||
| 463 | } else { |
||
| 464 | return $link; |
||
| 465 | } |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Return the link for this {@link SiteTree} object, with the {@link Director::baseURL()} included. |
||
| 470 | * |
||
| 471 | * @param string $action Optional controller action (method). |
||
| 472 | * Note: URI encoding of this parameter is applied automatically through template casting, |
||
| 473 | * don't encode the passed parameter. Please use {@link Controller::join_links()} instead to |
||
| 474 | * append GET parameters. |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | public function Link($action = null) { |
||
| 478 | return Controller::join_links(Director::baseURL(), $this->RelativeLink($action)); |
||
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Get the absolute URL for this page, including protocol and host. |
||
| 483 | * |
||
| 484 | * @param string $action See {@link Link()} |
||
| 485 | * @return string |
||
| 486 | */ |
||
| 487 | public function AbsoluteLink($action = null) { |
||
| 488 | if($this->hasMethod('alternateAbsoluteLink')) { |
||
| 489 | return $this->alternateAbsoluteLink($action); |
||
| 490 | } else { |
||
| 491 | return Director::absoluteURL($this->Link($action)); |
||
| 492 | } |
||
| 493 | } |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Base link used for previewing. Defaults to absolute URL, in order to account for domain changes, e.g. on multi |
||
| 497 | * site setups. Does not contain hints about the stage, see {@link SilverStripeNavigator} for details. |
||
| 498 | * |
||
| 499 | * @param string $action See {@link Link()} |
||
| 500 | * @return string |
||
| 501 | */ |
||
| 502 | public function PreviewLink($action = null) { |
||
| 503 | if($this->hasMethod('alternatePreviewLink')) { |
||
| 504 | Deprecation::notice('5.0', 'Use updatePreviewLink or override PreviewLink method'); |
||
| 505 | return $this->alternatePreviewLink($action); |
||
| 506 | } |
||
| 507 | |||
| 508 | $link = $this->AbsoluteLink($action); |
||
| 509 | $this->extend('updatePreviewLink', $link, $action); |
||
| 510 | return $link; |
||
| 511 | } |
||
| 512 | |||
| 513 | public function getMimeType() { |
||
| 514 | return 'text/html'; |
||
| 515 | } |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Return the link for this {@link SiteTree} object relative to the SilverStripe root. |
||
| 519 | * |
||
| 520 | * By default, if this page is the current home page, and there is no action specified then this will return a link |
||
| 521 | * to the root of the site. However, if you set the $action parameter to TRUE then the link will not be rewritten |
||
| 522 | * and returned in its full form. |
||
| 523 | * |
||
| 524 | * @uses RootURLController::get_homepage_link() |
||
| 525 | * |
||
| 526 | * @param string $action See {@link Link()} |
||
| 527 | * @return string |
||
| 528 | */ |
||
| 529 | public function RelativeLink($action = null) { |
||
| 530 | if($this->ParentID && self::config()->nested_urls) { |
||
| 531 | $parent = $this->Parent(); |
||
| 532 | // If page is removed select parent from version history (for archive page view) |
||
| 533 | if((!$parent || !$parent->exists()) && $this->getIsDeletedFromStage()) { |
||
| 534 | $parent = Versioned::get_latest_version(__CLASS__, $this->ParentID); |
||
| 535 | } |
||
| 536 | $base = $parent->RelativeLink($this->URLSegment); |
||
| 537 | } elseif(!$action && $this->URLSegment == RootURLController::get_homepage_link()) { |
||
| 538 | // Unset base for root-level homepages. |
||
| 539 | // Note: Homepages with action parameters (or $action === true) |
||
| 540 | // need to retain their URLSegment. |
||
| 541 | $base = null; |
||
| 542 | } else { |
||
| 543 | $base = $this->URLSegment; |
||
| 544 | } |
||
| 545 | |||
| 546 | $this->extend('updateRelativeLink', $base, $action); |
||
| 547 | |||
| 548 | // Legacy support: If $action === true, retain URLSegment for homepages, |
||
| 549 | // but don't append any action |
||
| 550 | if($action === true) $action = null; |
||
| 551 | |||
| 552 | return Controller::join_links($base, '/', $action); |
||
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Get the absolute URL for this page on the Live site. |
||
| 557 | * |
||
| 558 | * @param bool $includeStageEqualsLive Whether to append the URL with ?stage=Live to force Live mode |
||
| 559 | * @return string |
||
| 560 | */ |
||
| 561 | public function getAbsoluteLiveLink($includeStageEqualsLive = true) { |
||
| 562 | $oldReadingMode = Versioned::get_reading_mode(); |
||
| 563 | Versioned::set_stage(Versioned::LIVE); |
||
| 564 | /** @var SiteTree $live */ |
||
| 565 | $live = Versioned::get_one_by_stage(__CLASS__, Versioned::LIVE, array( |
||
| 566 | '"SiteTree"."ID"' => $this->ID |
||
| 567 | )); |
||
| 568 | if($live) { |
||
| 569 | $link = $live->AbsoluteLink(); |
||
| 570 | if($includeStageEqualsLive) { |
||
| 571 | $link = Controller::join_links($link, '?stage=Live'); |
||
| 572 | } |
||
| 573 | } else { |
||
| 574 | $link = null; |
||
| 575 | } |
||
| 576 | |||
| 577 | Versioned::set_reading_mode($oldReadingMode); |
||
| 578 | return $link; |
||
| 579 | } |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Generates a link to edit this page in the CMS. |
||
| 583 | * |
||
| 584 | * @return string |
||
| 585 | */ |
||
| 586 | public function CMSEditLink() { |
||
| 587 | $link = Controller::join_links( |
||
| 588 | CMSPageEditController::singleton()->Link('show'), |
||
| 589 | $this->ID |
||
| 590 | ); |
||
| 591 | return Director::absoluteURL($link); |
||
| 592 | } |
||
| 593 | |||
| 594 | |||
| 595 | /** |
||
| 596 | * Return a CSS identifier generated from this page's link. |
||
| 597 | * |
||
| 598 | * @return string The URL segment |
||
| 599 | */ |
||
| 600 | public function ElementName() { |
||
| 601 | return str_replace('/', '-', trim($this->RelativeLink(true), '/')); |
||
| 602 | } |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Returns true if this is the currently active page being used to handle this request. |
||
| 606 | * |
||
| 607 | * @return bool |
||
| 608 | */ |
||
| 609 | public function isCurrent() { |
||
| 610 | $currentPage = Director::get_current_page(); |
||
| 611 | if ($currentPage instanceof ContentController) { |
||
| 612 | $currentPage = $currentPage->data(); |
||
| 613 | } |
||
| 614 | if($currentPage instanceof SiteTree) { |
||
| 615 | return $currentPage === $this || $currentPage->ID === $this->ID; |
||
| 616 | } |
||
| 617 | return false; |
||
| 618 | } |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Check if this page is in the currently active section (e.g. it is either current or one of its children is |
||
| 622 | * currently being viewed). |
||
| 623 | * |
||
| 624 | * @return bool |
||
| 625 | */ |
||
| 626 | public function isSection() { |
||
| 627 | return $this->isCurrent() || ( |
||
| 628 | Director::get_current_page() instanceof SiteTree && in_array($this->ID, Director::get_current_page()->getAncestors()->column()) |
||
| 629 | ); |
||
| 630 | } |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Check if the parent of this page has been removed (or made otherwise unavailable), and is still referenced by |
||
| 634 | * this child. Any such orphaned page may still require access via the CMS, but should not be shown as accessible |
||
| 635 | * to external users. |
||
| 636 | * |
||
| 637 | * @return bool |
||
| 638 | */ |
||
| 639 | public function isOrphaned() { |
||
| 640 | // Always false for root pages |
||
| 641 | if(empty($this->ParentID)) { |
||
| 642 | return false; |
||
| 643 | } |
||
| 644 | |||
| 645 | // Parent must exist and not be an orphan itself |
||
| 646 | $parent = $this->Parent(); |
||
| 647 | return !$parent || !$parent->exists() || $parent->isOrphaned(); |
||
| 648 | } |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Return "link" or "current" depending on if this is the {@link SiteTree::isCurrent()} current page. |
||
| 652 | * |
||
| 653 | * @return string |
||
| 654 | */ |
||
| 655 | public function LinkOrCurrent() { |
||
| 656 | return $this->isCurrent() ? 'current' : 'link'; |
||
| 657 | } |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Return "link" or "section" depending on if this is the {@link SiteTree::isSeciton()} current section. |
||
| 661 | * |
||
| 662 | * @return string |
||
| 663 | */ |
||
| 664 | public function LinkOrSection() { |
||
| 665 | return $this->isSection() ? 'section' : 'link'; |
||
| 666 | } |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Return "link", "current" or "section" depending on if this page is the current page, or not on the current page |
||
| 670 | * but in the current section. |
||
| 671 | * |
||
| 672 | * @return string |
||
| 673 | */ |
||
| 674 | public function LinkingMode() { |
||
| 675 | if($this->isCurrent()) { |
||
| 676 | return 'current'; |
||
| 677 | } elseif($this->isSection()) { |
||
| 678 | return 'section'; |
||
| 679 | } else { |
||
| 680 | return 'link'; |
||
| 681 | } |
||
| 682 | } |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Check if this page is in the given current section. |
||
| 686 | * |
||
| 687 | * @param string $sectionName Name of the section to check |
||
| 688 | * @return bool True if we are in the given section |
||
| 689 | */ |
||
| 690 | public function InSection($sectionName) { |
||
| 691 | $page = Director::get_current_page(); |
||
| 692 | while($page && $page->exists()) { |
||
| 693 | if($sectionName == $page->URLSegment) { |
||
| 694 | return true; |
||
| 695 | } |
||
| 696 | $page = $page->Parent(); |
||
| 697 | } |
||
| 698 | return false; |
||
| 699 | } |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Reset Sort on duped page |
||
| 703 | * |
||
| 704 | * @param SiteTree $original |
||
| 705 | * @param bool $doWrite |
||
| 706 | */ |
||
| 707 | public function onBeforeDuplicate($original, $doWrite) { |
||
| 708 | $this->Sort = 0; |
||
| 709 | } |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Duplicates each child of this node recursively and returns the top-level duplicate node. |
||
| 713 | * |
||
| 714 | * @return static The duplicated object |
||
| 715 | */ |
||
| 716 | public function duplicateWithChildren() { |
||
| 717 | /** @var SiteTree $clone */ |
||
| 718 | $clone = $this->duplicate(); |
||
| 719 | $children = $this->AllChildren(); |
||
| 720 | |||
| 721 | if($children) { |
||
| 722 | /** @var SiteTree $child */ |
||
| 723 | foreach($children as $child) { |
||
| 724 | $childClone = $child->duplicateWithChildren(); |
||
| 725 | $childClone->ParentID = $clone->ID; |
||
| 726 | $childClone->write(); |
||
| 727 | } |
||
| 728 | } |
||
| 729 | |||
| 730 | return $clone; |
||
| 731 | } |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Duplicate this node and its children as a child of the node with the given ID |
||
| 735 | * |
||
| 736 | * @param int $id ID of the new node's new parent |
||
| 737 | */ |
||
| 738 | public function duplicateAsChild($id) { |
||
| 739 | /** @var SiteTree $newSiteTree */ |
||
| 740 | $newSiteTree = $this->duplicate(); |
||
| 741 | $newSiteTree->ParentID = $id; |
||
| 742 | $newSiteTree->Sort = 0; |
||
| 743 | $newSiteTree->write(); |
||
| 744 | } |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Return a breadcrumb trail to this page. Excludes "hidden" pages (with ShowInMenus=0) by default. |
||
| 748 | * |
||
| 749 | * @param int $maxDepth The maximum depth to traverse. |
||
| 750 | * @param boolean $unlinked Whether to link page titles. |
||
| 751 | * @param boolean|string $stopAtPageType ClassName of a page to stop the upwards traversal. |
||
| 752 | * @param boolean $showHidden Include pages marked with the attribute ShowInMenus = 0 |
||
| 753 | * @return string The breadcrumb trail. |
||
| 754 | */ |
||
| 755 | public function Breadcrumbs($maxDepth = 20, $unlinked = false, $stopAtPageType = false, $showHidden = false) { |
||
| 756 | $pages = $this->getBreadcrumbItems($maxDepth, $stopAtPageType, $showHidden); |
||
| 757 | $template = new SSViewer('BreadcrumbsTemplate'); |
||
| 758 | return $template->process($this->customise(new ArrayData(array( |
||
| 759 | "Pages" => $pages, |
||
| 760 | "Unlinked" => $unlinked |
||
| 761 | )))); |
||
| 762 | } |
||
| 763 | |||
| 764 | |||
| 765 | /** |
||
| 766 | * Returns a list of breadcrumbs for the current page. |
||
| 767 | * |
||
| 768 | * @param int $maxDepth The maximum depth to traverse. |
||
| 769 | * @param boolean|string $stopAtPageType ClassName of a page to stop the upwards traversal. |
||
| 770 | * @param boolean $showHidden Include pages marked with the attribute ShowInMenus = 0 |
||
| 771 | * |
||
| 772 | * @return ArrayList |
||
| 773 | */ |
||
| 774 | public function getBreadcrumbItems($maxDepth = 20, $stopAtPageType = false, $showHidden = false) { |
||
| 775 | $page = $this; |
||
| 776 | $pages = array(); |
||
| 777 | |||
| 778 | while( |
||
| 779 | $page |
||
| 780 | && $page->exists() |
||
| 781 | && (!$maxDepth || count($pages) < $maxDepth) |
||
| 782 | && (!$stopAtPageType || $page->ClassName != $stopAtPageType) |
||
| 783 | ) { |
||
| 784 | if($showHidden || $page->ShowInMenus || ($page->ID == $this->ID)) { |
||
| 785 | $pages[] = $page; |
||
| 786 | } |
||
| 787 | |||
| 788 | $page = $page->Parent(); |
||
| 789 | } |
||
| 790 | |||
| 791 | return new ArrayList(array_reverse($pages)); |
||
| 792 | } |
||
| 793 | |||
| 794 | |||
| 795 | /** |
||
| 796 | * Make this page a child of another page. |
||
| 797 | * |
||
| 798 | * If the parent page does not exist, resolve it to a valid ID before updating this page's reference. |
||
| 799 | * |
||
| 800 | * @param SiteTree|int $item Either the parent object, or the parent ID |
||
| 801 | */ |
||
| 802 | public function setParent($item) { |
||
| 803 | if(is_object($item)) { |
||
| 804 | if (!$item->exists()) $item->write(); |
||
| 805 | $this->setField("ParentID", $item->ID); |
||
| 806 | } else { |
||
| 807 | $this->setField("ParentID", $item); |
||
| 808 | } |
||
| 809 | } |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Get the parent of this page. |
||
| 813 | * |
||
| 814 | * @return SiteTree Parent of this page |
||
| 815 | */ |
||
| 816 | public function getParent() { |
||
| 817 | if ($parentID = $this->getField("ParentID")) { |
||
| 818 | return DataObject::get_by_id("SilverStripe\\CMS\\Model\\SiteTree", $parentID); |
||
| 819 | } |
||
| 820 | return null; |
||
| 821 | } |
||
| 822 | |||
| 823 | /** |
||
| 824 | * Return a string of the form "parent - page" or "grandparent - parent - page" using page titles |
||
| 825 | * |
||
| 826 | * @param int $level The maximum amount of levels to traverse. |
||
| 827 | * @param string $separator Seperating string |
||
| 828 | * @return string The resulting string |
||
| 829 | */ |
||
| 830 | public function NestedTitle($level = 2, $separator = " - ") { |
||
| 831 | $item = $this; |
||
| 832 | $parts = []; |
||
| 833 | while($item && $level > 0) { |
||
| 834 | $parts[] = $item->Title; |
||
| 835 | $item = $item->getParent(); |
||
| 836 | $level--; |
||
| 837 | } |
||
| 838 | return implode($separator, array_reverse($parts)); |
||
| 839 | } |
||
| 840 | |||
| 841 | /** |
||
| 842 | * This function should return true if the current user can execute this action. It can be overloaded to customise |
||
| 843 | * the security model for an application. |
||
| 844 | * |
||
| 845 | * Slightly altered from parent behaviour in {@link DataObject->can()}: |
||
| 846 | * - Checks for existence of a method named "can<$perm>()" on the object |
||
| 847 | * - Calls decorators and only returns for FALSE "vetoes" |
||
| 848 | * - Falls back to {@link Permission::check()} |
||
| 849 | * - Does NOT check for many-many relations named "Can<$perm>" |
||
| 850 | * |
||
| 851 | * @uses DataObjectDecorator->can() |
||
| 852 | * |
||
| 853 | * @param string $perm The permission to be checked, such as 'View' |
||
| 854 | * @param Member $member The member whose permissions need checking. Defaults to the currently logged in user. |
||
| 855 | * @param array $context Context argument for canCreate() |
||
| 856 | * @return bool True if the the member is allowed to do the given action |
||
| 857 | */ |
||
| 858 | public function can($perm, $member = null, $context = array()) { |
||
| 859 | View Code Duplication | if(!$member || !($member instanceof Member) || is_numeric($member)) { |
|
| 860 | $member = Member::currentUserID(); |
||
| 861 | } |
||
| 862 | |||
| 863 | if($member && Permission::checkMember($member, "ADMIN")) return true; |
||
| 864 | |||
| 865 | if(is_string($perm) && method_exists($this, 'can' . ucfirst($perm))) { |
||
| 866 | $method = 'can' . ucfirst($perm); |
||
| 867 | return $this->$method($member); |
||
| 868 | } |
||
| 869 | |||
| 870 | $results = $this->extend('can', $member); |
||
| 871 | if($results && is_array($results)) if(!min($results)) return false; |
||
| 872 | |||
| 873 | return ($member && Permission::checkMember($member, $perm)); |
||
| 874 | } |
||
| 875 | |||
| 876 | /** |
||
| 877 | * This function should return true if the current user can add children to this page. It can be overloaded to |
||
| 878 | * customise the security model for an application. |
||
| 879 | * |
||
| 880 | * Denies permission if any of the following conditions is true: |
||
| 881 | * - alternateCanAddChildren() on a extension returns false |
||
| 882 | * - canEdit() is not granted |
||
| 883 | * - There are no classes defined in {@link $allowed_children} |
||
| 884 | * |
||
| 885 | * @uses SiteTreeExtension->canAddChildren() |
||
| 886 | * @uses canEdit() |
||
| 887 | * @uses $allowed_children |
||
| 888 | * |
||
| 889 | * @param Member|int $member |
||
| 890 | * @return bool True if the current user can add children |
||
| 891 | */ |
||
| 892 | public function canAddChildren($member = null) { |
||
| 893 | // Disable adding children to archived pages |
||
| 894 | if($this->getIsDeletedFromStage()) { |
||
| 895 | return false; |
||
| 896 | } |
||
| 897 | |||
| 898 | View Code Duplication | if(!$member || !($member instanceof Member) || is_numeric($member)) { |
|
| 899 | $member = Member::currentUserID(); |
||
| 900 | } |
||
| 901 | |||
| 902 | // Standard mechanism for accepting permission changes from extensions |
||
| 903 | $extended = $this->extendedCan('canAddChildren', $member); |
||
| 904 | if($extended !== null) { |
||
| 905 | return $extended; |
||
| 906 | } |
||
| 907 | |||
| 908 | // Default permissions |
||
| 909 | if($member && Permission::checkMember($member, "ADMIN")) { |
||
| 910 | return true; |
||
| 911 | } |
||
| 912 | |||
| 913 | return $this->canEdit($member) && $this->stat('allowed_children') != 'none'; |
||
| 914 | } |
||
| 915 | |||
| 916 | /** |
||
| 917 | * This function should return true if the current user can view this page. It can be overloaded to customise the |
||
| 918 | * security model for an application. |
||
| 919 | * |
||
| 920 | * Denies permission if any of the following conditions is true: |
||
| 921 | * - canView() on any extension returns false |
||
| 922 | * - "CanViewType" directive is set to "Inherit" and any parent page return false for canView() |
||
| 923 | * - "CanViewType" directive is set to "LoggedInUsers" and no user is logged in |
||
| 924 | * - "CanViewType" directive is set to "OnlyTheseUsers" and user is not in the given groups |
||
| 925 | * |
||
| 926 | * @uses DataExtension->canView() |
||
| 927 | * @uses ViewerGroups() |
||
| 928 | * |
||
| 929 | * @param Member|int $member |
||
| 930 | * @return bool True if the current user can view this page |
||
| 931 | */ |
||
| 932 | public function canView($member = null) { |
||
| 933 | View Code Duplication | if(!$member || !($member instanceof Member) || is_numeric($member)) { |
|
| 934 | $member = Member::currentUserID(); |
||
| 935 | } |
||
| 936 | |||
| 937 | // Standard mechanism for accepting permission changes from extensions |
||
| 938 | $extended = $this->extendedCan('canView', $member); |
||
| 939 | if($extended !== null) { |
||
| 940 | return $extended; |
||
| 941 | } |
||
| 942 | |||
| 943 | // admin override |
||
| 944 | if($member && Permission::checkMember($member, array("ADMIN", "SITETREE_VIEW_ALL"))) { |
||
| 945 | return true; |
||
| 946 | } |
||
| 947 | |||
| 948 | // Orphaned pages (in the current stage) are unavailable, except for admins via the CMS |
||
| 949 | if($this->isOrphaned()) { |
||
| 950 | return false; |
||
| 951 | } |
||
| 952 | |||
| 953 | // check for empty spec |
||
| 954 | if(!$this->CanViewType || $this->CanViewType == 'Anyone') { |
||
| 955 | return true; |
||
| 956 | } |
||
| 957 | |||
| 958 | // check for inherit |
||
| 959 | if($this->CanViewType == 'Inherit') { |
||
| 960 | if($this->ParentID) return $this->Parent()->canView($member); |
||
| 961 | else return $this->getSiteConfig()->canViewPages($member); |
||
| 962 | } |
||
| 963 | |||
| 964 | // check for any logged-in users |
||
| 965 | if($this->CanViewType == 'LoggedInUsers' && $member) { |
||
| 966 | return true; |
||
| 967 | } |
||
| 968 | |||
| 969 | // check for specific groups |
||
| 970 | if($member && is_numeric($member)) { |
||
| 971 | $member = DataObject::get_by_id('SilverStripe\\Security\\Member', $member); |
||
| 972 | } |
||
| 973 | if( |
||
| 974 | $this->CanViewType == 'OnlyTheseUsers' |
||
| 975 | && $member |
||
| 976 | && $member->inGroups($this->ViewerGroups()) |
||
| 977 | ) return true; |
||
| 978 | |||
| 979 | return false; |
||
| 980 | } |
||
| 981 | |||
| 982 | /** |
||
| 983 | * Check if this page can be published |
||
| 984 | * |
||
| 985 | * @param Member $member |
||
| 986 | * @return bool |
||
| 987 | */ |
||
| 988 | public function canPublish($member = null) { |
||
| 989 | if(!$member) { |
||
| 990 | $member = Member::currentUser(); |
||
| 991 | } |
||
| 992 | |||
| 993 | // Check extension |
||
| 994 | $extended = $this->extendedCan('canPublish', $member); |
||
| 995 | if($extended !== null) { |
||
| 996 | return $extended; |
||
| 997 | } |
||
| 998 | |||
| 999 | if(Permission::checkMember($member, "ADMIN")) { |
||
| 1000 | return true; |
||
| 1001 | } |
||
| 1002 | |||
| 1003 | // Default to relying on edit permission |
||
| 1004 | return $this->canEdit($member); |
||
| 1005 | } |
||
| 1006 | |||
| 1007 | /** |
||
| 1008 | * This function should return true if the current user can delete this page. It can be overloaded to customise the |
||
| 1009 | * security model for an application. |
||
| 1010 | * |
||
| 1011 | * Denies permission if any of the following conditions is true: |
||
| 1012 | * - canDelete() returns false on any extension |
||
| 1013 | * - canEdit() returns false |
||
| 1014 | * - any descendant page returns false for canDelete() |
||
| 1015 | * |
||
| 1016 | * @uses canDelete() |
||
| 1017 | * @uses SiteTreeExtension->canDelete() |
||
| 1018 | * @uses canEdit() |
||
| 1019 | * |
||
| 1020 | * @param Member $member |
||
| 1021 | * @return bool True if the current user can delete this page |
||
| 1022 | */ |
||
| 1023 | public function canDelete($member = null) { |
||
| 1024 | View Code Duplication | if($member instanceof Member) $memberID = $member->ID; |
|
| 1025 | else if(is_numeric($member)) $memberID = $member; |
||
| 1026 | else $memberID = Member::currentUserID(); |
||
| 1027 | |||
| 1028 | // Standard mechanism for accepting permission changes from extensions |
||
| 1029 | $extended = $this->extendedCan('canDelete', $memberID); |
||
| 1030 | if($extended !== null) { |
||
| 1031 | return $extended; |
||
| 1032 | } |
||
| 1033 | |||
| 1034 | // Default permission check |
||
| 1035 | if($memberID && Permission::checkMember($memberID, array("ADMIN", "SITETREE_EDIT_ALL"))) { |
||
| 1036 | return true; |
||
| 1037 | } |
||
| 1038 | |||
| 1039 | // Regular canEdit logic is handled by can_edit_multiple |
||
| 1040 | $results = self::can_delete_multiple(array($this->ID), $memberID); |
||
| 1041 | |||
| 1042 | // If this page no longer exists in stage/live results won't contain the page. |
||
| 1043 | // Fail-over to false |
||
| 1044 | return isset($results[$this->ID]) ? $results[$this->ID] : false; |
||
| 1045 | } |
||
| 1046 | |||
| 1047 | /** |
||
| 1048 | * This function should return true if the current user can create new pages of this class, regardless of class. It |
||
| 1049 | * can be overloaded to customise the security model for an application. |
||
| 1050 | * |
||
| 1051 | * By default, permission to create at the root level is based on the SiteConfig configuration, and permission to |
||
| 1052 | * create beneath a parent is based on the ability to edit that parent page. |
||
| 1053 | * |
||
| 1054 | * Use {@link canAddChildren()} to control behaviour of creating children under this page. |
||
| 1055 | * |
||
| 1056 | * @uses $can_create |
||
| 1057 | * @uses DataExtension->canCreate() |
||
| 1058 | * |
||
| 1059 | * @param Member $member |
||
| 1060 | * @param array $context Optional array which may contain array('Parent' => $parentObj) |
||
| 1061 | * If a parent page is known, it will be checked for validity. |
||
| 1062 | * If omitted, it will be assumed this is to be created as a top level page. |
||
| 1063 | * @return bool True if the current user can create pages on this class. |
||
| 1064 | */ |
||
| 1065 | public function canCreate($member = null, $context = array()) { |
||
| 1066 | View Code Duplication | if(!$member || !(is_a($member, 'SilverStripe\\Security\\Member')) || is_numeric($member)) { |
|
| 1067 | $member = Member::currentUserID(); |
||
| 1068 | } |
||
| 1069 | |||
| 1070 | // Check parent (custom canCreate option for SiteTree) |
||
| 1071 | // Block children not allowed for this parent type |
||
| 1072 | $parent = isset($context['Parent']) ? $context['Parent'] : null; |
||
| 1073 | if($parent && !in_array(get_class($this), $parent->allowedChildren())) { |
||
| 1074 | return false; |
||
| 1075 | } |
||
| 1076 | |||
| 1077 | // Standard mechanism for accepting permission changes from extensions |
||
| 1078 | $extended = $this->extendedCan(__FUNCTION__, $member, $context); |
||
| 1079 | if($extended !== null) { |
||
| 1080 | return $extended; |
||
| 1081 | } |
||
| 1082 | |||
| 1083 | // Check permission |
||
| 1084 | if($member && Permission::checkMember($member, "ADMIN")) { |
||
| 1085 | return true; |
||
| 1086 | } |
||
| 1087 | |||
| 1088 | // Fall over to inherited permissions |
||
| 1089 | if($parent) { |
||
| 1090 | return $parent->canAddChildren($member); |
||
| 1091 | } else { |
||
| 1092 | // This doesn't necessarily mean we are creating a root page, but that |
||
| 1093 | // we don't know if there is a parent, so default to this permission |
||
| 1094 | return SiteConfig::current_site_config()->canCreateTopLevel($member); |
||
| 1095 | } |
||
| 1096 | } |
||
| 1097 | |||
| 1098 | /** |
||
| 1099 | * This function should return true if the current user can edit this page. It can be overloaded to customise the |
||
| 1100 | * security model for an application. |
||
| 1101 | * |
||
| 1102 | * Denies permission if any of the following conditions is true: |
||
| 1103 | * - canEdit() on any extension returns false |
||
| 1104 | * - canView() return false |
||
| 1105 | * - "CanEditType" directive is set to "Inherit" and any parent page return false for canEdit() |
||
| 1106 | * - "CanEditType" directive is set to "LoggedInUsers" and no user is logged in or doesn't have the |
||
| 1107 | * CMS_Access_CMSMAIN permission code |
||
| 1108 | * - "CanEditType" directive is set to "OnlyTheseUsers" and user is not in the given groups |
||
| 1109 | * |
||
| 1110 | * @uses canView() |
||
| 1111 | * @uses EditorGroups() |
||
| 1112 | * @uses DataExtension->canEdit() |
||
| 1113 | * |
||
| 1114 | * @param Member $member Set to false if you want to explicitly test permissions without a valid user (useful for |
||
| 1115 | * unit tests) |
||
| 1116 | * @return bool True if the current user can edit this page |
||
| 1117 | */ |
||
| 1118 | public function canEdit($member = null) { |
||
| 1119 | View Code Duplication | if($member instanceof Member) $memberID = $member->ID; |
|
| 1120 | else if(is_numeric($member)) $memberID = $member; |
||
| 1121 | else $memberID = Member::currentUserID(); |
||
| 1122 | |||
| 1123 | // Standard mechanism for accepting permission changes from extensions |
||
| 1124 | $extended = $this->extendedCan('canEdit', $memberID); |
||
| 1125 | if($extended !== null) { |
||
| 1126 | return $extended; |
||
| 1127 | } |
||
| 1128 | |||
| 1129 | // Default permissions |
||
| 1130 | if($memberID && Permission::checkMember($memberID, array("ADMIN", "SITETREE_EDIT_ALL"))) { |
||
| 1131 | return true; |
||
| 1132 | } |
||
| 1133 | |||
| 1134 | if($this->ID) { |
||
| 1135 | // Regular canEdit logic is handled by can_edit_multiple |
||
| 1136 | $results = self::can_edit_multiple(array($this->ID), $memberID); |
||
| 1137 | |||
| 1138 | // If this page no longer exists in stage/live results won't contain the page. |
||
| 1139 | // Fail-over to false |
||
| 1140 | return isset($results[$this->ID]) ? $results[$this->ID] : false; |
||
| 1141 | |||
| 1142 | // Default for unsaved pages |
||
| 1143 | } else { |
||
| 1144 | return $this->getSiteConfig()->canEditPages($member); |
||
| 1145 | } |
||
| 1146 | } |
||
| 1147 | |||
| 1148 | /** |
||
| 1149 | * Stub method to get the site config, unless the current class can provide an alternate. |
||
| 1150 | * |
||
| 1151 | * @return SiteConfig |
||
| 1152 | */ |
||
| 1153 | public function getSiteConfig() { |
||
| 1154 | $configs = $this->invokeWithExtensions('alternateSiteConfig'); |
||
| 1155 | foreach(array_filter($configs) as $config) { |
||
| 1156 | return $config; |
||
| 1157 | } |
||
| 1158 | |||
| 1159 | return SiteConfig::current_site_config(); |
||
| 1160 | } |
||
| 1161 | |||
| 1162 | /** |
||
| 1163 | * Pre-populate the cache of canEdit, canView, canDelete, canPublish permissions. This method will use the static |
||
| 1164 | * can_(perm)_multiple method for efficiency. |
||
| 1165 | * |
||
| 1166 | * @param string $permission The permission: edit, view, publish, approve, etc. |
||
| 1167 | * @param array $ids An array of page IDs |
||
| 1168 | * @param callable|string $batchCallback The function/static method to call to calculate permissions. Defaults |
||
| 1169 | * to 'SiteTree::can_(permission)_multiple' |
||
| 1170 | */ |
||
| 1171 | static public function prepopulate_permission_cache($permission = 'CanEditType', $ids, $batchCallback = null) { |
||
| 1172 | if(!$batchCallback) { |
||
| 1173 | $batchCallback = __CLASS__ . "::can_{$permission}_multiple"; |
||
| 1174 | } |
||
| 1175 | |||
| 1176 | if(is_callable($batchCallback)) { |
||
| 1177 | call_user_func($batchCallback, $ids, Member::currentUserID(), false); |
||
| 1178 | } else { |
||
| 1179 | user_error("SiteTree::prepopulate_permission_cache can't calculate '$permission' " |
||
| 1180 | . "with callback '$batchCallback'", E_USER_WARNING); |
||
| 1181 | } |
||
| 1182 | } |
||
| 1183 | |||
| 1184 | /** |
||
| 1185 | * This method is NOT a full replacement for the individual can*() methods, e.g. {@link canEdit()}. Rather than |
||
| 1186 | * checking (potentially slow) PHP logic, it relies on the database group associations, e.g. the "CanEditType" field |
||
| 1187 | * plus the "SiteTree_EditorGroups" many-many table. By batch checking multiple records, we can combine the queries |
||
| 1188 | * efficiently. |
||
| 1189 | * |
||
| 1190 | * Caches based on $typeField data. To invalidate the cache, use {@link SiteTree::reset()} or set the $useCached |
||
| 1191 | * property to FALSE. |
||
| 1192 | * |
||
| 1193 | * @param array $ids Of {@link SiteTree} IDs |
||
| 1194 | * @param int $memberID Member ID |
||
| 1195 | * @param string $typeField A property on the data record, e.g. "CanEditType". |
||
| 1196 | * @param string $groupJoinTable A many-many table name on this record, e.g. "SiteTree_EditorGroups" |
||
| 1197 | * @param string $siteConfigMethod Method to call on {@link SiteConfig} for toplevel items, e.g. "canEdit" |
||
| 1198 | * @param string $globalPermission If the member doesn't have this permission code, don't bother iterating deeper |
||
| 1199 | * @param bool $useCached |
||
| 1200 | * @return array An map of {@link SiteTree} ID keys to boolean values |
||
| 1201 | */ |
||
| 1202 | public static function batch_permission_check($ids, $memberID, $typeField, $groupJoinTable, $siteConfigMethod, |
||
| 1203 | $globalPermission = null, $useCached = true) { |
||
| 1204 | if($globalPermission === NULL) $globalPermission = array('CMS_ACCESS_LeftAndMain', 'CMS_ACCESS_CMSMain'); |
||
| 1205 | |||
| 1206 | // Sanitise the IDs |
||
| 1207 | $ids = array_filter($ids, 'is_numeric'); |
||
| 1208 | |||
| 1209 | // This is the name used on the permission cache |
||
| 1210 | // converts something like 'CanEditType' to 'edit'. |
||
| 1211 | $cacheKey = strtolower(substr($typeField, 3, -4)) . "-$memberID"; |
||
| 1212 | |||
| 1213 | // Default result: nothing editable |
||
| 1214 | $result = array_fill_keys($ids, false); |
||
| 1215 | if($ids) { |
||
| 1216 | |||
| 1217 | // Look in the cache for values |
||
| 1218 | if($useCached && isset(self::$cache_permissions[$cacheKey])) { |
||
| 1219 | $cachedValues = array_intersect_key(self::$cache_permissions[$cacheKey], $result); |
||
| 1220 | |||
| 1221 | // If we can't find everything in the cache, then look up the remainder separately |
||
| 1222 | $uncachedValues = array_diff_key($result, self::$cache_permissions[$cacheKey]); |
||
| 1223 | if($uncachedValues) { |
||
| 1224 | $cachedValues = self::batch_permission_check(array_keys($uncachedValues), $memberID, $typeField, $groupJoinTable, $siteConfigMethod, $globalPermission, false) + $cachedValues; |
||
| 1225 | } |
||
| 1226 | return $cachedValues; |
||
| 1227 | } |
||
| 1228 | |||
| 1229 | // If a member doesn't have a certain permission then they can't edit anything |
||
| 1230 | if(!$memberID || ($globalPermission && !Permission::checkMember($memberID, $globalPermission))) { |
||
| 1231 | return $result; |
||
| 1232 | } |
||
| 1233 | |||
| 1234 | // Placeholder for parameterised ID list |
||
| 1235 | $idPlaceholders = DB::placeholders($ids); |
||
| 1236 | |||
| 1237 | // If page can't be viewed, don't grant edit permissions to do - implement can_view_multiple(), so this can |
||
| 1238 | // be enabled |
||
| 1239 | //$ids = array_keys(array_filter(self::can_view_multiple($ids, $memberID))); |
||
| 1240 | |||
| 1241 | // Get the groups that the given member belongs to |
||
| 1242 | /** @var Member $member */ |
||
| 1243 | $member = DataObject::get_by_id('SilverStripe\\Security\\Member', $memberID); |
||
| 1244 | $groupIDs = $member->Groups()->column("ID"); |
||
| 1245 | $SQL_groupList = implode(", ", $groupIDs); |
||
| 1246 | if (!$SQL_groupList) { |
||
| 1247 | $SQL_groupList = '0'; |
||
| 1248 | } |
||
| 1249 | |||
| 1250 | $combinedStageResult = array(); |
||
| 1251 | |||
| 1252 | foreach(array(Versioned::DRAFT, Versioned::LIVE) as $stage) { |
||
| 1253 | // Start by filling the array with the pages that actually exist |
||
| 1254 | /** @skipUpgrade */ |
||
| 1255 | $table = ($stage=='Stage') ? "SiteTree" : "SiteTree_$stage"; |
||
| 1256 | |||
| 1257 | if($ids) { |
||
| 1258 | $idQuery = "SELECT \"ID\" FROM \"$table\" WHERE \"ID\" IN ($idPlaceholders)"; |
||
| 1259 | $stageIds = DB::prepared_query($idQuery, $ids)->column(); |
||
| 1260 | } else { |
||
| 1261 | $stageIds = array(); |
||
| 1262 | } |
||
| 1263 | $result = array_fill_keys($stageIds, false); |
||
| 1264 | |||
| 1265 | // Get the uninherited permissions |
||
| 1266 | $uninheritedPermissions = Versioned::get_by_stage("SilverStripe\\CMS\\Model\\SiteTree", $stage) |
||
| 1267 | ->where(array( |
||
| 1268 | "(\"$typeField\" = 'LoggedInUsers' OR |
||
| 1269 | (\"$typeField\" = 'OnlyTheseUsers' AND \"$groupJoinTable\".\"SiteTreeID\" IS NOT NULL)) |
||
| 1270 | AND \"SiteTree\".\"ID\" IN ($idPlaceholders)" |
||
| 1271 | => $ids |
||
| 1272 | )) |
||
| 1273 | ->leftJoin($groupJoinTable, "\"$groupJoinTable\".\"SiteTreeID\" = \"SiteTree\".\"ID\" AND \"$groupJoinTable\".\"GroupID\" IN ($SQL_groupList)"); |
||
| 1274 | |||
| 1275 | if($uninheritedPermissions) { |
||
| 1276 | // Set all the relevant items in $result to true |
||
| 1277 | $result = array_fill_keys($uninheritedPermissions->column('ID'), true) + $result; |
||
| 1278 | } |
||
| 1279 | |||
| 1280 | // Get permissions that are inherited |
||
| 1281 | $potentiallyInherited = Versioned::get_by_stage( |
||
| 1282 | "SilverStripe\\CMS\\Model\\SiteTree", |
||
| 1283 | $stage, |
||
| 1284 | array("\"$typeField\" = 'Inherit' AND \"SiteTree\".\"ID\" IN ($idPlaceholders)" => $ids) |
||
| 1285 | ); |
||
| 1286 | |||
| 1287 | if($potentiallyInherited) { |
||
| 1288 | // Group $potentiallyInherited by ParentID; we'll look at the permission of all those parents and |
||
| 1289 | // then see which ones the user has permission on |
||
| 1290 | $groupedByParent = array(); |
||
| 1291 | foreach($potentiallyInherited as $item) { |
||
| 1292 | /** @var SiteTree $item */ |
||
| 1293 | if($item->ParentID) { |
||
| 1294 | if(!isset($groupedByParent[$item->ParentID])) $groupedByParent[$item->ParentID] = array(); |
||
| 1295 | $groupedByParent[$item->ParentID][] = $item->ID; |
||
| 1296 | } else { |
||
| 1297 | // Might return different site config based on record context, e.g. when subsites module |
||
| 1298 | // is used |
||
| 1299 | $siteConfig = $item->getSiteConfig(); |
||
| 1300 | $result[$item->ID] = $siteConfig->{$siteConfigMethod}($memberID); |
||
| 1301 | } |
||
| 1302 | } |
||
| 1303 | |||
| 1304 | if($groupedByParent) { |
||
| 1305 | $actuallyInherited = self::batch_permission_check(array_keys($groupedByParent), $memberID, $typeField, $groupJoinTable, $siteConfigMethod); |
||
| 1306 | if($actuallyInherited) { |
||
| 1307 | $parentIDs = array_keys(array_filter($actuallyInherited)); |
||
| 1308 | foreach($parentIDs as $parentID) { |
||
| 1309 | // Set all the relevant items in $result to true |
||
| 1310 | $result = array_fill_keys($groupedByParent[$parentID], true) + $result; |
||
| 1311 | } |
||
| 1312 | } |
||
| 1313 | } |
||
| 1314 | } |
||
| 1315 | |||
| 1316 | $combinedStageResult = $combinedStageResult + $result; |
||
| 1317 | |||
| 1318 | } |
||
| 1319 | } |
||
| 1320 | |||
| 1321 | if(isset($combinedStageResult)) { |
||
| 1322 | // Cache the results |
||
| 1323 | if(empty(self::$cache_permissions[$cacheKey])) self::$cache_permissions[$cacheKey] = array(); |
||
| 1324 | self::$cache_permissions[$cacheKey] = $combinedStageResult + self::$cache_permissions[$cacheKey]; |
||
| 1325 | return $combinedStageResult; |
||
| 1326 | } else { |
||
| 1327 | return array(); |
||
| 1328 | } |
||
| 1329 | } |
||
| 1330 | |||
| 1331 | /** |
||
| 1332 | * Get the 'can edit' information for a number of SiteTree pages. |
||
| 1333 | * |
||
| 1334 | * @param array $ids An array of IDs of the SiteTree pages to look up |
||
| 1335 | * @param int $memberID ID of member |
||
| 1336 | * @param bool $useCached Return values from the permission cache if they exist |
||
| 1337 | * @return array A map where the IDs are keys and the values are booleans stating whether the given page can be |
||
| 1338 | * edited |
||
| 1339 | */ |
||
| 1340 | static public function can_edit_multiple($ids, $memberID, $useCached = true) { |
||
| 1341 | return self::batch_permission_check($ids, $memberID, 'CanEditType', 'SiteTree_EditorGroups', 'canEditPages', null, $useCached); |
||
| 1342 | } |
||
| 1343 | |||
| 1344 | /** |
||
| 1345 | * Get the 'can edit' information for a number of SiteTree pages. |
||
| 1346 | * |
||
| 1347 | * @param array $ids An array of IDs of the SiteTree pages to look up |
||
| 1348 | * @param int $memberID ID of member |
||
| 1349 | * @param bool $useCached Return values from the permission cache if they exist |
||
| 1350 | * @return array |
||
| 1351 | */ |
||
| 1352 | static public function can_delete_multiple($ids, $memberID, $useCached = true) { |
||
| 1353 | $deletable = array(); |
||
| 1354 | $result = array_fill_keys($ids, false); |
||
| 1355 | $cacheKey = "delete-$memberID"; |
||
| 1356 | |||
| 1357 | // Look in the cache for values |
||
| 1358 | if($useCached && isset(self::$cache_permissions[$cacheKey])) { |
||
| 1359 | $cachedValues = array_intersect_key(self::$cache_permissions[$cacheKey], $result); |
||
| 1360 | |||
| 1361 | // If we can't find everything in the cache, then look up the remainder separately |
||
| 1362 | $uncachedValues = array_diff_key($result, self::$cache_permissions[$cacheKey]); |
||
| 1363 | if($uncachedValues) { |
||
| 1364 | $cachedValues = self::can_delete_multiple(array_keys($uncachedValues), $memberID, false) |
||
| 1365 | + $cachedValues; |
||
| 1366 | } |
||
| 1367 | return $cachedValues; |
||
| 1368 | } |
||
| 1369 | |||
| 1370 | // You can only delete pages that you can edit |
||
| 1371 | $editableIDs = array_keys(array_filter(self::can_edit_multiple($ids, $memberID))); |
||
| 1372 | if($editableIDs) { |
||
| 1373 | |||
| 1374 | // You can only delete pages whose children you can delete |
||
| 1375 | $editablePlaceholders = DB::placeholders($editableIDs); |
||
| 1376 | $childRecords = SiteTree::get()->where(array( |
||
| 1377 | "\"SiteTree\".\"ParentID\" IN ($editablePlaceholders)" => $editableIDs |
||
| 1378 | )); |
||
| 1379 | if($childRecords) { |
||
| 1380 | $children = $childRecords->map("ID", "ParentID"); |
||
| 1381 | |||
| 1382 | // Find out the children that can be deleted |
||
| 1383 | $deletableChildren = self::can_delete_multiple($children->keys(), $memberID); |
||
| 1384 | |||
| 1385 | // Get a list of all the parents that have no undeletable children |
||
| 1386 | $deletableParents = array_fill_keys($editableIDs, true); |
||
| 1387 | foreach($deletableChildren as $id => $canDelete) { |
||
| 1388 | if(!$canDelete) unset($deletableParents[$children[$id]]); |
||
| 1389 | } |
||
| 1390 | |||
| 1391 | // Use that to filter the list of deletable parents that have children |
||
| 1392 | $deletableParents = array_keys($deletableParents); |
||
| 1393 | |||
| 1394 | // Also get the $ids that don't have children |
||
| 1395 | $parents = array_unique($children->values()); |
||
| 1396 | $deletableLeafNodes = array_diff($editableIDs, $parents); |
||
| 1397 | |||
| 1398 | // Combine the two |
||
| 1399 | $deletable = array_merge($deletableParents, $deletableLeafNodes); |
||
| 1400 | |||
| 1401 | } else { |
||
| 1402 | $deletable = $editableIDs; |
||
| 1403 | } |
||
| 1404 | } |
||
| 1405 | |||
| 1406 | // Convert the array of deletable IDs into a map of the original IDs with true/false as the value |
||
| 1407 | return array_fill_keys($deletable, true) + array_fill_keys($ids, false); |
||
| 1408 | } |
||
| 1409 | |||
| 1410 | /** |
||
| 1411 | * Collate selected descendants of this page. |
||
| 1412 | * |
||
| 1413 | * {@link $condition} will be evaluated on each descendant, and if it is succeeds, that item will be added to the |
||
| 1414 | * $collator array. |
||
| 1415 | * |
||
| 1416 | * @param string $condition The PHP condition to be evaluated. The page will be called $item |
||
| 1417 | * @param array $collator An array, passed by reference, to collect all of the matching descendants. |
||
| 1418 | * @return bool |
||
| 1419 | */ |
||
| 1420 | public function collateDescendants($condition, &$collator) { |
||
| 1421 | $children = $this->Children(); |
||
| 1422 | if($children) { |
||
| 1423 | foreach($children as $item) { |
||
| 1424 | |||
| 1425 | if(eval("return $condition;")) { |
||
| 1426 | $collator[] = $item; |
||
| 1427 | } |
||
| 1428 | /** @var SiteTree $item */ |
||
| 1429 | $item->collateDescendants($condition, $collator); |
||
| 1430 | } |
||
| 1431 | return true; |
||
| 1432 | } |
||
| 1433 | return false; |
||
| 1434 | } |
||
| 1435 | |||
| 1436 | /** |
||
| 1437 | * Return the title, description, keywords and language metatags. |
||
| 1438 | * |
||
| 1439 | * @todo Move <title> tag in separate getter for easier customization and more obvious usage |
||
| 1440 | * |
||
| 1441 | * @param bool $includeTitle Show default <title>-tag, set to false for custom templating |
||
| 1442 | * @return string The XHTML metatags |
||
| 1443 | */ |
||
| 1444 | public function MetaTags($includeTitle = true) { |
||
| 1445 | $tags = array(); |
||
| 1446 | if($includeTitle && strtolower($includeTitle) != 'false') { |
||
| 1447 | $tags[] = FormField::create_tag('title', array(), $this->obj('Title')->forTemplate()); |
||
| 1448 | } |
||
| 1449 | |||
| 1450 | $generator = trim(Config::inst()->get(__CLASS__, 'meta_generator')); |
||
| 1451 | if (!empty($generator)) { |
||
| 1452 | $tags[] = FormField::create_tag('meta', array( |
||
| 1453 | 'name' => 'generator', |
||
| 1454 | 'content' => $generator, |
||
| 1455 | )); |
||
| 1456 | } |
||
| 1457 | |||
| 1458 | $charset = Config::inst()->get('SilverStripe\\Control\\ContentNegotiator', 'encoding'); |
||
| 1459 | $tags[] = FormField::create_tag('meta', array( |
||
| 1460 | 'http-equiv' => 'Content-Type', |
||
| 1461 | 'content' => 'text/html; charset=' . $charset, |
||
| 1462 | )); |
||
| 1463 | if($this->MetaDescription) { |
||
| 1464 | $tags[] = FormField::create_tag('meta', array( |
||
| 1465 | 'name' => 'description', |
||
| 1466 | 'content' => $this->MetaDescription, |
||
| 1467 | )); |
||
| 1468 | } |
||
| 1469 | |||
| 1470 | if(Permission::check('CMS_ACCESS_CMSMain') |
||
| 1471 | && !$this instanceof ErrorPage |
||
| 1472 | && $this->ID > 0 |
||
| 1473 | ) { |
||
| 1474 | $tags[] = FormField::create_tag('meta', array( |
||
| 1475 | 'name' => 'x-page-id', |
||
| 1476 | 'content' => $this->obj('ID')->forTemplate(), |
||
| 1477 | )); |
||
| 1478 | $tags[] = FormField::create_tag('meta', array( |
||
| 1479 | 'name' => 'x-cms-edit-link', |
||
| 1480 | 'content' => $this->obj('CMSEditLink')->forTemplate(), |
||
| 1481 | )); |
||
| 1482 | } |
||
| 1483 | |||
| 1484 | $tags = implode("\n", $tags); |
||
| 1485 | if($this->ExtraMeta) { |
||
| 1486 | $tags .= $this->obj('ExtraMeta')->forTemplate(); |
||
| 1487 | } |
||
| 1488 | |||
| 1489 | $this->extend('MetaTags', $tags); |
||
| 1490 | |||
| 1491 | return $tags; |
||
| 1492 | } |
||
| 1493 | |||
| 1494 | /** |
||
| 1495 | * Returns the object that contains the content that a user would associate with this page. |
||
| 1496 | * |
||
| 1497 | * Ordinarily, this is just the page itself, but for example on RedirectorPages or VirtualPages ContentSource() will |
||
| 1498 | * return the page that is linked to. |
||
| 1499 | * |
||
| 1500 | * @return $this |
||
| 1501 | */ |
||
| 1502 | public function ContentSource() { |
||
| 1505 | |||
| 1506 | /** |
||
| 1507 | * Add default records to database. |
||
| 1508 | * |
||
| 1509 | * This function is called whenever the database is built, after the database tables have all been created. Overload |
||
| 1510 | * this to add default records when the database is built, but make sure you call parent::requireDefaultRecords(). |
||
| 1511 | */ |
||
| 1512 | public function requireDefaultRecords() { |
||
| 1513 | parent::requireDefaultRecords(); |
||
| 1514 | |||
| 1515 | // default pages |
||
| 1516 | if($this->class == __CLASS__ && $this->config()->create_default_pages) { |
||
| 1517 | if(!SiteTree::get_by_link(RootURLController::config()->default_homepage_link)) { |
||
| 1518 | $homepage = new Page(); |
||
| 1519 | $homepage->Title = _t('SiteTree.DEFAULTHOMETITLE', 'Home'); |
||
| 1520 | $homepage->Content = _t('SiteTree.DEFAULTHOMECONTENT', '<p>Welcome to SilverStripe! This is the default homepage. You can edit this page by opening <a href="admin/">the CMS</a>.</p><p>You can now access the <a href="http://docs.silverstripe.org">developer documentation</a>, or begin the <a href="http://www.silverstripe.org/learn/lessons">SilverStripe lessons</a>.</p>'); |
||
| 1521 | $homepage->URLSegment = RootURLController::config()->default_homepage_link; |
||
| 1522 | $homepage->Sort = 1; |
||
| 1523 | $homepage->write(); |
||
| 1524 | $homepage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
||
| 1525 | $homepage->flushCache(); |
||
| 1526 | DB::alteration_message('Home page created', 'created'); |
||
| 1527 | } |
||
| 1528 | |||
| 1529 | if(DB::query("SELECT COUNT(*) FROM \"SiteTree\"")->value() == 1) { |
||
| 1530 | $aboutus = new Page(); |
||
| 1531 | $aboutus->Title = _t('SiteTree.DEFAULTABOUTTITLE', 'About Us'); |
||
| 1532 | $aboutus->Content = _t( |
||
| 1533 | 'SiteTree.DEFAULTABOUTCONTENT', |
||
| 1534 | '<p>You can fill this page out with your own content, or delete it and create your own pages.</p>' |
||
| 1535 | ); |
||
| 1536 | $aboutus->Sort = 2; |
||
| 1537 | $aboutus->write(); |
||
| 1538 | $aboutus->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
||
| 1539 | $aboutus->flushCache(); |
||
| 1540 | DB::alteration_message('About Us page created', 'created'); |
||
| 1541 | |||
| 1542 | $contactus = new Page(); |
||
| 1543 | $contactus->Title = _t('SiteTree.DEFAULTCONTACTTITLE', 'Contact Us'); |
||
| 1544 | $contactus->Content = _t( |
||
| 1545 | 'SiteTree.DEFAULTCONTACTCONTENT', |
||
| 1546 | '<p>You can fill this page out with your own content, or delete it and create your own pages.</p>' |
||
| 1547 | ); |
||
| 1548 | $contactus->Sort = 3; |
||
| 1549 | $contactus->write(); |
||
| 1550 | $contactus->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
||
| 1551 | $contactus->flushCache(); |
||
| 1552 | DB::alteration_message('Contact Us page created', 'created'); |
||
| 1553 | } |
||
| 1554 | } |
||
| 1555 | } |
||
| 1556 | |||
| 1557 | protected function onBeforeWrite() { |
||
| 1607 | |||
| 1608 | /** |
||
| 1609 | * Trigger synchronisation of link tracking |
||
| 1610 | * |
||
| 1611 | * {@see SiteTreeLinkTracking::augmentSyncLinkTracking} |
||
| 1612 | */ |
||
| 1613 | public function syncLinkTracking() { |
||
| 1616 | |||
| 1617 | public function onBeforeDelete() { |
||
| 1618 | parent::onBeforeDelete(); |
||
| 1619 | |||
| 1620 | // If deleting this page, delete all its children. |
||
| 1621 | if(SiteTree::config()->enforce_strict_hierarchy && $children = $this->AllChildren()) { |
||
| 1622 | foreach($children as $child) { |
||
| 1623 | /** @var SiteTree $child */ |
||
| 1624 | $child->delete(); |
||
| 1625 | } |
||
| 1626 | } |
||
| 1627 | } |
||
| 1628 | |||
| 1629 | public function onAfterDelete() { |
||
| 1642 | |||
| 1643 | public function flushCache($persistent = true) { |
||
| 1647 | |||
| 1648 | public function validate() { |
||
| 1649 | $result = parent::validate(); |
||
| 1650 | |||
| 1651 | // Allowed children validation |
||
| 1652 | $parent = $this->getParent(); |
||
| 1653 | if($parent && $parent->exists()) { |
||
| 1654 | // No need to check for subclasses or instanceof, as allowedChildren() already |
||
| 1655 | // deconstructs any inheritance trees already. |
||
| 1656 | $allowed = $parent->allowedChildren(); |
||
| 1657 | $subject = ($this instanceof VirtualPage && $this->CopyContentFromID) |
||
| 1658 | ? $this->CopyContentFrom() |
||
| 1659 | : $this; |
||
| 1660 | if(!in_array($subject->ClassName, $allowed)) { |
||
| 1661 | $result->error( |
||
| 1662 | _t( |
||
| 1663 | 'SiteTree.PageTypeNotAllowed', |
||
| 1664 | 'Page type "{type}" not allowed as child of this parent page', |
||
| 1665 | array('type' => $subject->i18n_singular_name()) |
||
| 1666 | ), |
||
| 1667 | 'ALLOWED_CHILDREN' |
||
| 1668 | ); |
||
| 1669 | } |
||
| 1670 | } |
||
| 1671 | |||
| 1672 | // "Can be root" validation |
||
| 1673 | if(!$this->stat('can_be_root') && !$this->ParentID) { |
||
| 1674 | $result->error( |
||
| 1675 | _t( |
||
| 1676 | 'SiteTree.PageTypNotAllowedOnRoot', |
||
| 1677 | 'Page type "{type}" is not allowed on the root level', |
||
| 1678 | array('type' => $this->i18n_singular_name()) |
||
| 1679 | ), |
||
| 1680 | 'CAN_BE_ROOT' |
||
| 1681 | ); |
||
| 1682 | } |
||
| 1683 | |||
| 1684 | return $result; |
||
| 1685 | } |
||
| 1686 | |||
| 1687 | /** |
||
| 1688 | * Returns true if this object has a URLSegment value that does not conflict with any other objects. This method |
||
| 1689 | * checks for: |
||
| 1690 | * - A page with the same URLSegment that has a conflict |
||
| 1691 | * - Conflicts with actions on the parent page |
||
| 1692 | * - A conflict caused by a root page having the same URLSegment as a class name |
||
| 1693 | * |
||
| 1694 | * @return bool |
||
| 1695 | */ |
||
| 1696 | public function validURLSegment() { |
||
| 1730 | |||
| 1731 | /** |
||
| 1732 | * Generate a URL segment based on the title provided. |
||
| 1733 | * |
||
| 1734 | * If {@link Extension}s wish to alter URL segment generation, they can do so by defining |
||
| 1735 | * updateURLSegment(&$url, $title). $url will be passed by reference and should be modified. $title will contain |
||
| 1736 | * the title that was originally used as the source of this generated URL. This lets extensions either start from |
||
| 1737 | * scratch, or incrementally modify the generated URL. |
||
| 1738 | * |
||
| 1739 | * @param string $title Page title |
||
| 1740 | * @return string Generated url segment |
||
| 1741 | */ |
||
| 1742 | public function generateURLSegment($title){ |
||
| 1754 | |||
| 1755 | /** |
||
| 1756 | * Gets the URL segment for the latest draft version of this page. |
||
| 1757 | * |
||
| 1758 | * @return string |
||
| 1759 | */ |
||
| 1760 | public function getStageURLSegment() { |
||
| 1761 | $stageRecord = Versioned::get_one_by_stage('SilverStripe\\CMS\\Model\\SiteTree', Versioned::DRAFT, array( |
||
| 1762 | '"SiteTree"."ID"' => $this->ID |
||
| 1763 | )); |
||
| 1764 | return ($stageRecord) ? $stageRecord->URLSegment : null; |
||
| 1765 | } |
||
| 1766 | |||
| 1767 | /** |
||
| 1768 | * Gets the URL segment for the currently published version of this page. |
||
| 1769 | * |
||
| 1770 | * @return string |
||
| 1771 | */ |
||
| 1772 | public function getLiveURLSegment() { |
||
| 1773 | $liveRecord = Versioned::get_one_by_stage('SilverStripe\\CMS\\Model\\SiteTree', Versioned::LIVE, array( |
||
| 1774 | '"SiteTree"."ID"' => $this->ID |
||
| 1775 | )); |
||
| 1776 | return ($liveRecord) ? $liveRecord->URLSegment : null; |
||
| 1777 | } |
||
| 1778 | |||
| 1779 | /** |
||
| 1780 | * Returns the pages that depend on this page. This includes virtual pages, pages that link to it, etc. |
||
| 1781 | * |
||
| 1782 | * @param bool $includeVirtuals Set to false to exlcude virtual pages. |
||
| 1783 | * @return ArrayList |
||
| 1784 | */ |
||
| 1785 | public function DependentPages($includeVirtuals = true) { |
||
| 1786 | if(class_exists('Subsite')) { |
||
| 1787 | $origDisableSubsiteFilter = Subsite::$disable_subsite_filter; |
||
| 1788 | Subsite::disable_subsite_filter(true); |
||
| 1789 | } |
||
| 1790 | |||
| 1791 | // Content links |
||
| 1792 | $items = new ArrayList(); |
||
| 1793 | |||
| 1794 | // We merge all into a regular SS_List, because DataList doesn't support merge |
||
| 1795 | if($contentLinks = $this->BackLinkTracking()) { |
||
| 1796 | $linkList = new ArrayList(); |
||
| 1797 | foreach($contentLinks as $item) { |
||
| 1798 | $item->DependentLinkType = 'Content link'; |
||
| 1799 | $linkList->push($item); |
||
| 1800 | } |
||
| 1801 | $items->merge($linkList); |
||
| 1802 | } |
||
| 1803 | |||
| 1804 | // Virtual pages |
||
| 1805 | if($includeVirtuals) { |
||
| 1806 | $virtuals = $this->VirtualPages(); |
||
| 1807 | if($virtuals) { |
||
| 1808 | $virtualList = new ArrayList(); |
||
| 1809 | foreach($virtuals as $item) { |
||
| 1810 | $item->DependentLinkType = 'Virtual page'; |
||
| 1811 | $virtualList->push($item); |
||
| 1812 | } |
||
| 1813 | $items->merge($virtualList); |
||
| 1814 | } |
||
| 1815 | } |
||
| 1816 | |||
| 1817 | // Redirector pages |
||
| 1818 | $redirectors = RedirectorPage::get()->where(array( |
||
| 1819 | '"RedirectorPage"."RedirectionType"' => 'Internal', |
||
| 1820 | '"RedirectorPage"."LinkToID"' => $this->ID |
||
| 1821 | )); |
||
| 1822 | if($redirectors) { |
||
| 1823 | $redirectorList = new ArrayList(); |
||
| 1824 | foreach($redirectors as $item) { |
||
| 1825 | $item->DependentLinkType = 'Redirector page'; |
||
| 1826 | $redirectorList->push($item); |
||
| 1827 | } |
||
| 1828 | $items->merge($redirectorList); |
||
| 1829 | } |
||
| 1830 | |||
| 1831 | if(class_exists('Subsite')) { |
||
| 1832 | Subsite::disable_subsite_filter($origDisableSubsiteFilter); |
||
| 1833 | } |
||
| 1834 | |||
| 1835 | return $items; |
||
| 1836 | } |
||
| 1837 | |||
| 1838 | /** |
||
| 1839 | * Return all virtual pages that link to this page. |
||
| 1840 | * |
||
| 1841 | * @return DataList |
||
| 1842 | */ |
||
| 1843 | public function VirtualPages() { |
||
| 1853 | |||
| 1854 | /** |
||
| 1855 | * Returns a FieldList with which to create the main editing form. |
||
| 1856 | * |
||
| 1857 | * You can override this in your child classes to add extra fields - first get the parent fields using |
||
| 1858 | * parent::getCMSFields(), then use addFieldToTab() on the FieldList. |
||
| 1859 | * |
||
| 1860 | * See {@link getSettingsFields()} for a different set of fields concerned with configuration aspects on the record, |
||
| 1861 | * e.g. access control. |
||
| 1862 | * |
||
| 1863 | * @return FieldList The fields to be displayed in the CMS |
||
| 1864 | */ |
||
| 1865 | public function getCMSFields() { |
||
| 1866 | // Status / message |
||
| 1867 | // Create a status message for multiple parents |
||
| 1868 | if($this->ID && is_numeric($this->ID)) { |
||
| 1869 | $linkedPages = $this->VirtualPages(); |
||
| 1870 | |||
| 1871 | $parentPageLinks = array(); |
||
| 1872 | |||
| 1873 | if($linkedPages->count() > 0) { |
||
| 1874 | /** @var VirtualPage $linkedPage */ |
||
| 1875 | foreach($linkedPages as $linkedPage) { |
||
| 1876 | $parentPage = $linkedPage->Parent(); |
||
| 1877 | if($parentPage && $parentPage->exists()) { |
||
| 1878 | $link = Convert::raw2att($parentPage->CMSEditLink()); |
||
| 1879 | $title = Convert::raw2xml($parentPage->Title); |
||
| 1880 | } else { |
||
| 1881 | $link = CMSPageEditController::singleton()->Link('show'); |
||
| 1882 | $title = _t('SiteTree.TOPLEVEL', 'Site Content (Top Level)'); |
||
| 1883 | } |
||
| 1884 | $parentPageLinks[] = "<a class=\"cmsEditlink\" href=\"{$link}\">{$title}</a>"; |
||
| 1885 | } |
||
| 1886 | |||
| 1887 | $lastParent = array_pop($parentPageLinks); |
||
| 1888 | $parentList = "'$lastParent'"; |
||
| 1889 | |||
| 1890 | if(count($parentPageLinks)) { |
||
| 1891 | $parentList = "'" . implode("', '", $parentPageLinks) . "' and " |
||
| 1892 | . $parentList; |
||
| 1893 | } |
||
| 1894 | |||
| 1895 | $statusMessage[] = _t( |
||
| 1896 | 'SiteTree.APPEARSVIRTUALPAGES', |
||
| 1897 | "This content also appears on the virtual pages in the {title} sections.", |
||
| 1898 | array('title' => $parentList) |
||
| 1899 | ); |
||
| 2042 | |||
| 2043 | |||
| 2044 | /** |
||
| 2045 | * Returns fields related to configuration aspects on this record, e.g. access control. See {@link getCMSFields()} |
||
| 2046 | * for content-related fields. |
||
| 2047 | * |
||
| 2048 | * @return FieldList |
||
| 2049 | */ |
||
| 2050 | public function getSettingsFields() { |
||
| 2157 | |||
| 2158 | /** |
||
| 2159 | * @param bool $includerelations A boolean value to indicate if the labels returned should include relation fields |
||
| 2160 | * @return array |
||
| 2161 | */ |
||
| 2162 | public function fieldLabels($includerelations = true) { |
||
| 2200 | |||
| 2201 | /** |
||
| 2202 | * Get the actions available in the CMS for this page - eg Save, Publish. |
||
| 2203 | * |
||
| 2204 | * Frontend scripts and styles know how to handle the following FormFields: |
||
| 2205 | * - top-level FormActions appear as standalone buttons |
||
| 2206 | * - top-level CompositeField with FormActions within appear as grouped buttons |
||
| 2207 | * - TabSet & Tabs appear as a drop ups |
||
| 2208 | * - FormActions within the Tab are restyled as links |
||
| 2209 | * - major actions can provide alternate states for richer presentation (see ssui.button widget extension) |
||
| 2210 | * |
||
| 2211 | * @return FieldList The available actions for this page. |
||
| 2212 | */ |
||
| 2213 | public function getCMSActions() { |
||
| 2364 | |||
| 2365 | public function onAfterPublish() { |
||
| 2373 | |||
| 2374 | /** |
||
| 2375 | * Update draft dependant pages |
||
| 2376 | */ |
||
| 2377 | public function onAfterRevertToLive() { |
||
| 2389 | |||
| 2390 | /** |
||
| 2391 | * Determine if this page references a parent which is archived, and not available in stage |
||
| 2392 | * |
||
| 2393 | * @return bool True if there is an archived parent |
||
| 2394 | */ |
||
| 2395 | protected function isParentArchived() { |
||
| 2404 | |||
| 2405 | /** |
||
| 2406 | * Restore the content in the active copy of this SiteTree page to the stage site. |
||
| 2407 | * |
||
| 2408 | * @return self |
||
| 2409 | */ |
||
| 2410 | public function doRestoreToStage() { |
||
| 2447 | |||
| 2448 | /** |
||
| 2449 | * Check if this page is new - that is, if it has yet to have been written to the database. |
||
| 2450 | * |
||
| 2451 | * @return bool |
||
| 2452 | */ |
||
| 2453 | public function isNew() { |
||
| 2464 | |||
| 2465 | /** |
||
| 2466 | * Get the class dropdown used in the CMS to change the class of a page. This returns the list of options in the |
||
| 2467 | * dropdown as a Map from class name to singular name. Filters by {@link SiteTree->canCreate()}, as well as |
||
| 2468 | * {@link SiteTree::$needs_permission}. |
||
| 2469 | * |
||
| 2470 | * @return array |
||
| 2471 | */ |
||
| 2472 | protected function getClassDropdown() { |
||
| 2515 | |||
| 2516 | /** |
||
| 2517 | * Returns an array of the class names of classes that are allowed to be children of this class. |
||
| 2518 | * |
||
| 2519 | * @return string[] |
||
| 2520 | */ |
||
| 2521 | public function allowedChildren() { |
||
| 2544 | |||
| 2545 | /** |
||
| 2546 | * Returns the class name of the default class for children of this page. |
||
| 2547 | * |
||
| 2548 | * @return string |
||
| 2549 | */ |
||
| 2550 | public function defaultChild() { |
||
| 2561 | |||
| 2562 | /** |
||
| 2563 | * Returns the class name of the default class for the parent of this page. |
||
| 2564 | * |
||
| 2565 | * @return string |
||
| 2566 | */ |
||
| 2567 | public function defaultParent() { |
||
| 2570 | |||
| 2571 | /** |
||
| 2572 | * Get the title for use in menus for this page. If the MenuTitle field is set it returns that, else it returns the |
||
| 2573 | * Title field. |
||
| 2574 | * |
||
| 2575 | * @return string |
||
| 2576 | */ |
||
| 2577 | public function getMenuTitle(){ |
||
| 2584 | |||
| 2585 | |||
| 2586 | /** |
||
| 2587 | * Set the menu title for this page. |
||
| 2588 | * |
||
| 2589 | * @param string $value |
||
| 2590 | */ |
||
| 2591 | public function setMenuTitle($value) { |
||
| 2598 | |||
| 2599 | /** |
||
| 2600 | * A flag provides the user with additional data about the current page status, for example a "removed from draft" |
||
| 2601 | * status. Each page can have more than one status flag. Returns a map of a unique key to a (localized) title for |
||
| 2602 | * the flag. The unique key can be reused as a CSS class. Use the 'updateStatusFlags' extension point to customize |
||
| 2603 | * the flags. |
||
| 2604 | * |
||
| 2605 | * Example (simple): |
||
| 2606 | * "deletedonlive" => "Deleted" |
||
| 2607 | * |
||
| 2608 | * Example (with optional title attribute): |
||
| 2609 | * "deletedonlive" => array('text' => "Deleted", 'title' => 'This page has been deleted') |
||
| 2610 | * |
||
| 2611 | * @param bool $cached Whether to serve the fields from cache; false regenerate them |
||
| 2612 | * @return array |
||
| 2613 | */ |
||
| 2614 | public function getStatusFlags($cached = true) { |
||
| 2648 | |||
| 2649 | /** |
||
| 2650 | * getTreeTitle will return three <span> html DOM elements, an empty <span> with the class 'jstree-pageicon' in |
||
| 2651 | * front, following by a <span> wrapping around its MenutTitle, then following by a <span> indicating its |
||
| 2652 | * publication status. |
||
| 2653 | * |
||
| 2654 | * @return string An HTML string ready to be directly used in a template |
||
| 2655 | */ |
||
| 2656 | public function getTreeTitle() { |
||
| 2685 | |||
| 2686 | /** |
||
| 2687 | * Returns the page in the current page stack of the given level. Level(1) will return the main menu item that |
||
| 2688 | * we're currently inside, etc. |
||
| 2689 | * |
||
| 2690 | * @param int $level |
||
| 2691 | * @return SiteTree |
||
| 2692 | */ |
||
| 2693 | public function Level($level) { |
||
| 2702 | |||
| 2703 | /** |
||
| 2704 | * Gets the depth of this page in the sitetree, where 1 is the root level |
||
| 2705 | * |
||
| 2706 | * @return int |
||
| 2707 | */ |
||
| 2708 | public function getPageLevel() { |
||
| 2714 | |||
| 2715 | /** |
||
| 2716 | * @return string |
||
| 2717 | */ |
||
| 2718 | public function getControllerName() { |
||
| 2734 | |||
| 2735 | /** |
||
| 2736 | * Return the CSS classes to apply to this node in the CMS tree. |
||
| 2737 | * |
||
| 2738 | * @param string $numChildrenMethod |
||
| 2739 | * @return string |
||
| 2740 | */ |
||
| 2741 | public function CMSTreeClasses($numChildrenMethod="numChildren") { |
||
| 2772 | |||
| 2773 | /** |
||
| 2774 | * Compares current draft with live version, and returns true if no draft version of this page exists but the page |
||
| 2775 | * is still published (eg, after triggering "Delete from draft site" in the CMS). |
||
| 2776 | * |
||
| 2777 | * @return bool |
||
| 2778 | */ |
||
| 2779 | public function getIsDeletedFromStage() { |
||
| 2788 | |||
| 2789 | /** |
||
| 2790 | * Return true if this page exists on the live site |
||
| 2791 | * |
||
| 2792 | * @return bool |
||
| 2793 | */ |
||
| 2794 | public function getExistsOnLive() { |
||
| 2797 | |||
| 2798 | /** |
||
| 2799 | * Compares current draft with live version, and returns true if these versions differ, meaning there have been |
||
| 2800 | * unpublished changes to the draft site. |
||
| 2801 | * |
||
| 2802 | * @return bool |
||
| 2803 | */ |
||
| 2804 | public function getIsModifiedOnStage() { |
||
| 2816 | |||
| 2817 | /** |
||
| 2818 | * Compares current draft with live version, and returns true if no live version exists, meaning the page was never |
||
| 2819 | * published. |
||
| 2820 | * |
||
| 2821 | * @return bool |
||
| 2822 | */ |
||
| 2823 | public function getIsAddedToStage() { |
||
| 2832 | |||
| 2833 | /** |
||
| 2834 | * Stops extendCMSFields() being called on getCMSFields(). This is useful when you need access to fields added by |
||
| 2835 | * subclasses of SiteTree in a extension. Call before calling parent::getCMSFields(), and reenable afterwards. |
||
| 2836 | */ |
||
| 2837 | static public function disableCMSFieldsExtensions() { |
||
| 2840 | |||
| 2841 | /** |
||
| 2842 | * Reenables extendCMSFields() being called on getCMSFields() after it has been disabled by |
||
| 2843 | * disableCMSFieldsExtensions(). |
||
| 2844 | */ |
||
| 2845 | static public function enableCMSFieldsExtensions() { |
||
| 2848 | |||
| 2849 | public function providePermissions() { |
||
| 2883 | |||
| 2884 | /** |
||
| 2885 | * Return the translated Singular name. |
||
| 2886 | * |
||
| 2887 | * @return string |
||
| 2888 | */ |
||
| 2889 | public function i18n_singular_name() { |
||
| 2898 | |||
| 2899 | /** |
||
| 2900 | * Overloaded to also provide entities for 'Page' class which is usually located in custom code, hence textcollector |
||
| 2901 | * picks it up for the wrong folder. |
||
| 2902 | * |
||
| 2903 | * @return array |
||
| 2904 | */ |
||
| 2905 | public function provideI18nEntities() { |
||
| 2921 | |||
| 2922 | /** |
||
| 2923 | * Returns 'root' if the current page has no parent, or 'subpage' otherwise |
||
| 2924 | * |
||
| 2925 | * @return string |
||
| 2926 | */ |
||
| 2927 | public function getParentType() { |
||
| 2930 | |||
| 2931 | /** |
||
| 2932 | * Clear the permissions cache for SiteTree |
||
| 2933 | */ |
||
| 2934 | public static function reset() { |
||
| 2937 | |||
| 2938 | static public function on_db_reset() { |
||
| 2941 | |||
| 2942 | } |
||
| 2943 |