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 |
||
100 | class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvider, CMSPreviewable, Resettable |
||
101 | { |
||
102 | |||
103 | /** |
||
104 | * Indicates what kind of children this page type can have. |
||
105 | * This can be an array of allowed child classes, or the string "none" - |
||
106 | * indicating that this page type can't have children. |
||
107 | * If a classname is prefixed by "*", such as "*Page", then only that |
||
108 | * class is allowed - no subclasses. Otherwise, the class and all its |
||
109 | * subclasses are allowed. |
||
110 | * To control allowed children on root level (no parent), use {@link $can_be_root}. |
||
111 | * |
||
112 | * Note that this setting is cached when used in the CMS, use the "flush" query parameter to clear it. |
||
113 | * |
||
114 | * @config |
||
115 | * @var array |
||
116 | */ |
||
117 | private static $allowed_children = [ |
||
118 | self::class |
||
119 | ]; |
||
120 | |||
121 | /** |
||
122 | * The default child class for this page. |
||
123 | * Note: Value might be cached, see {@link $allowed_chilren}. |
||
124 | * |
||
125 | * @config |
||
126 | * @var string |
||
127 | */ |
||
128 | private static $default_child = "Page"; |
||
129 | |||
130 | /** |
||
131 | * Default value for SiteTree.ClassName enum |
||
132 | * {@see DBClassName::getDefault} |
||
133 | * |
||
134 | * @config |
||
135 | * @var string |
||
136 | */ |
||
137 | private static $default_classname = "Page"; |
||
|
|||
138 | |||
139 | /** |
||
140 | * The default parent class for this page. |
||
141 | * Note: Value might be cached, see {@link $allowed_chilren}. |
||
142 | * |
||
143 | * @config |
||
144 | * @var string |
||
145 | */ |
||
146 | private static $default_parent = null; |
||
147 | |||
148 | /** |
||
149 | * Controls whether a page can be in the root of the site tree. |
||
150 | * Note: Value might be cached, see {@link $allowed_chilren}. |
||
151 | * |
||
152 | * @config |
||
153 | * @var bool |
||
154 | */ |
||
155 | private static $can_be_root = true; |
||
156 | |||
157 | /** |
||
158 | * List of permission codes a user can have to allow a user to create a page of this type. |
||
159 | * Note: Value might be cached, see {@link $allowed_chilren}. |
||
160 | * |
||
161 | * @config |
||
162 | * @var array |
||
163 | */ |
||
164 | private static $need_permission = null; |
||
165 | |||
166 | /** |
||
167 | * If you extend a class, and don't want to be able to select the old class |
||
168 | * in the cms, set this to the old class name. Eg, if you extended Product |
||
169 | * to make ImprovedProduct, then you would set $hide_ancestor to Product. |
||
170 | * |
||
171 | * @config |
||
172 | * @var string |
||
173 | */ |
||
174 | private static $hide_ancestor = null; |
||
175 | |||
176 | private static $db = array( |
||
177 | "URLSegment" => "Varchar(255)", |
||
178 | "Title" => "Varchar(255)", |
||
179 | "MenuTitle" => "Varchar(100)", |
||
180 | "Content" => "HTMLText", |
||
181 | "MetaDescription" => "Text", |
||
182 | "ExtraMeta" => "HTMLFragment(['whitelist' => ['meta', 'link']])", |
||
183 | "ShowInMenus" => "Boolean", |
||
184 | "ShowInSearch" => "Boolean", |
||
185 | "Sort" => "Int", |
||
186 | "HasBrokenFile" => "Boolean", |
||
187 | "HasBrokenLink" => "Boolean", |
||
188 | "ReportClass" => "Varchar", |
||
189 | ); |
||
190 | |||
191 | private static $indexes = array( |
||
192 | "URLSegment" => true, |
||
193 | ); |
||
194 | |||
195 | private static $has_many = array( |
||
196 | "VirtualPages" => "SilverStripe\\CMS\\Model\\VirtualPage.CopyContentFrom" |
||
197 | ); |
||
198 | |||
199 | private static $owned_by = array( |
||
200 | "VirtualPages" |
||
201 | ); |
||
202 | |||
203 | private static $casting = array( |
||
204 | "Breadcrumbs" => "HTMLFragment", |
||
205 | "LastEdited" => "Datetime", |
||
206 | "Created" => "Datetime", |
||
207 | 'Link' => 'Text', |
||
208 | 'RelativeLink' => 'Text', |
||
209 | 'AbsoluteLink' => 'Text', |
||
210 | 'CMSEditLink' => 'Text', |
||
211 | 'TreeTitle' => 'HTMLFragment', |
||
212 | 'MetaTags' => 'HTMLFragment', |
||
213 | ); |
||
214 | |||
215 | private static $defaults = array( |
||
216 | "ShowInMenus" => 1, |
||
217 | "ShowInSearch" => 1, |
||
218 | ); |
||
219 | |||
220 | private static $table_name = 'SiteTree'; |
||
221 | |||
222 | private static $versioning = array( |
||
223 | "Stage", "Live" |
||
224 | ); |
||
225 | |||
226 | private static $default_sort = "\"Sort\""; |
||
227 | |||
228 | /** |
||
229 | * If this is false, the class cannot be created in the CMS by regular content authors, only by ADMINs. |
||
230 | * @var boolean |
||
231 | * @config |
||
232 | */ |
||
233 | private static $can_create = true; |
||
234 | |||
235 | /** |
||
236 | * Icon to use in the CMS page tree. This should be the full filename, relative to the webroot. |
||
237 | * Also supports custom CSS rule contents (applied to the correct selector for the tree UI implementation). |
||
238 | * |
||
239 | * @see CMSMain::generateTreeStylingCSS() |
||
240 | * @config |
||
241 | * @var string |
||
242 | */ |
||
243 | private static $icon = null; |
||
244 | |||
245 | private static $extensions = [ |
||
246 | Hierarchy::class, |
||
247 | Versioned::class, |
||
248 | SiteTreeLinkTracking::class, |
||
249 | InheritedPermissionsExtension::class, |
||
250 | ]; |
||
251 | |||
252 | private static $searchable_fields = array( |
||
253 | 'Title', |
||
254 | 'Content', |
||
255 | ); |
||
256 | |||
257 | private static $field_labels = array( |
||
258 | 'URLSegment' => 'URL' |
||
259 | ); |
||
260 | |||
261 | /** |
||
262 | * @config |
||
263 | */ |
||
264 | private static $nested_urls = true; |
||
265 | |||
266 | /** |
||
267 | * @config |
||
268 | */ |
||
269 | private static $create_default_pages = true; |
||
270 | |||
271 | /** |
||
272 | * This controls whether of not extendCMSFields() is called by getCMSFields. |
||
273 | */ |
||
274 | private static $runCMSFieldsExtensions = true; |
||
275 | |||
276 | /** |
||
277 | * @config |
||
278 | * @var boolean |
||
279 | */ |
||
280 | private static $enforce_strict_hierarchy = true; |
||
281 | |||
282 | /** |
||
283 | * The value used for the meta generator tag. Leave blank to omit the tag. |
||
284 | * |
||
285 | * @config |
||
286 | * @var string |
||
287 | */ |
||
288 | private static $meta_generator = 'SilverStripe - http://silverstripe.org'; |
||
289 | |||
290 | protected $_cache_statusFlags = null; |
||
291 | |||
292 | /** |
||
293 | * Plural form for SiteTree / Page classes. Not inherited by subclasses. |
||
294 | * |
||
295 | * @config |
||
296 | * @var string |
||
297 | */ |
||
298 | private static $base_plural_name = 'Pages'; |
||
299 | |||
300 | /** |
||
301 | * Plural form for SiteTree / Page classes. Not inherited by subclasses. |
||
302 | * |
||
303 | * @config |
||
304 | * @var string |
||
305 | */ |
||
306 | private static $base_singular_name = 'Page'; |
||
307 | |||
308 | /** |
||
309 | * Description of the class functionality, typically shown to a user |
||
310 | * when selecting which page type to create. Translated through {@link provideI18nEntities()}. |
||
311 | * |
||
312 | * @see SiteTree::classDescription() |
||
313 | * @see SiteTree::i18n_classDescription() |
||
314 | * |
||
315 | * @config |
||
316 | * @var string |
||
317 | */ |
||
318 | private static $description = null; |
||
319 | |||
320 | /** |
||
321 | * Description for Page and SiteTree classes, but not inherited by subclasses. |
||
322 | * override SiteTree::$description in subclasses instead. |
||
323 | * |
||
324 | * @see SiteTree::classDescription() |
||
325 | * @see SiteTree::i18n_classDescription() |
||
326 | * |
||
327 | * @config |
||
328 | * @var string |
||
329 | */ |
||
330 | private static $base_description = 'Generic content page'; |
||
331 | |||
332 | /** |
||
333 | * Fetches the {@link SiteTree} object that maps to a link. |
||
334 | * |
||
335 | * If you have enabled {@link SiteTree::config()->nested_urls} on this site, then you can use a nested link such as |
||
336 | * "about-us/staff/", and this function will traverse down the URL chain and grab the appropriate link. |
||
337 | * |
||
338 | * Note that if no model can be found, this method will fall over to a extended alternateGetByLink method provided |
||
339 | * by a extension attached to {@link SiteTree} |
||
340 | * |
||
341 | * @param string $link The link of the page to search for |
||
342 | * @param bool $cache True (default) to use caching, false to force a fresh search from the database |
||
343 | * @return SiteTree |
||
344 | */ |
||
345 | public static function get_by_link($link, $cache = true) |
||
346 | { |
||
347 | if (trim($link, '/')) { |
||
348 | $link = trim(Director::makeRelative($link), '/'); |
||
349 | } else { |
||
350 | $link = RootURLController::get_homepage_link(); |
||
351 | } |
||
352 | |||
353 | $parts = preg_split('|/+|', $link); |
||
354 | |||
355 | // Grab the initial root level page to traverse down from. |
||
356 | $URLSegment = array_shift($parts); |
||
357 | $conditions = array('"SiteTree"."URLSegment"' => rawurlencode($URLSegment)); |
||
358 | if (self::config()->nested_urls) { |
||
359 | $conditions[] = array('"SiteTree"."ParentID"' => 0); |
||
360 | } |
||
361 | /** @var SiteTree $sitetree */ |
||
362 | $sitetree = DataObject::get_one(self::class, $conditions, $cache); |
||
363 | |||
364 | /// Fall back on a unique URLSegment for b/c. |
||
365 | if (!$sitetree |
||
366 | && self::config()->nested_urls |
||
367 | && $sitetree = DataObject::get_one(self::class, array( |
||
368 | '"SiteTree"."URLSegment"' => $URLSegment |
||
369 | ), $cache) |
||
370 | ) { |
||
371 | return $sitetree; |
||
372 | } |
||
373 | |||
374 | // Attempt to grab an alternative page from extensions. |
||
375 | if (!$sitetree) { |
||
376 | $parentID = self::config()->nested_urls ? 0 : null; |
||
377 | |||
378 | View Code Duplication | if ($alternatives = static::singleton()->extend('alternateGetByLink', $URLSegment, $parentID)) { |
|
379 | foreach ($alternatives as $alternative) { |
||
380 | if ($alternative) { |
||
381 | $sitetree = $alternative; |
||
382 | } |
||
383 | } |
||
384 | } |
||
385 | |||
386 | if (!$sitetree) { |
||
387 | return null; |
||
388 | } |
||
389 | } |
||
390 | |||
391 | // Check if we have any more URL parts to parse. |
||
392 | if (!self::config()->nested_urls || !count($parts)) { |
||
393 | return $sitetree; |
||
394 | } |
||
395 | |||
396 | // Traverse down the remaining URL segments and grab the relevant SiteTree objects. |
||
397 | foreach ($parts as $segment) { |
||
398 | $next = DataObject::get_one( |
||
399 | self::class, |
||
400 | array( |
||
401 | '"SiteTree"."URLSegment"' => $segment, |
||
402 | '"SiteTree"."ParentID"' => $sitetree->ID |
||
403 | ), |
||
404 | $cache |
||
405 | ); |
||
406 | |||
407 | if (!$next) { |
||
408 | $parentID = (int) $sitetree->ID; |
||
409 | |||
410 | View Code Duplication | if ($alternatives = static::singleton()->extend('alternateGetByLink', $segment, $parentID)) { |
|
411 | foreach ($alternatives as $alternative) { |
||
412 | if ($alternative) { |
||
413 | $next = $alternative; |
||
414 | } |
||
415 | } |
||
416 | } |
||
417 | |||
418 | if (!$next) { |
||
419 | return null; |
||
420 | } |
||
421 | } |
||
422 | |||
423 | $sitetree->destroy(); |
||
424 | $sitetree = $next; |
||
425 | } |
||
426 | |||
427 | return $sitetree; |
||
428 | } |
||
429 | |||
430 | /** |
||
431 | * Return a subclass map of SiteTree that shouldn't be hidden through {@link SiteTree::$hide_ancestor} |
||
432 | * |
||
433 | * @return array |
||
434 | */ |
||
435 | public static function page_type_classes() |
||
436 | { |
||
437 | $classes = ClassInfo::getValidSubClasses(); |
||
438 | |||
439 | $baseClassIndex = array_search(self::class, $classes); |
||
440 | if ($baseClassIndex !== false) { |
||
441 | unset($classes[$baseClassIndex]); |
||
442 | } |
||
443 | |||
444 | $kill_ancestors = array(); |
||
445 | |||
446 | // figure out if there are any classes we don't want to appear |
||
447 | foreach ($classes as $class) { |
||
448 | $instance = singleton($class); |
||
449 | |||
450 | // do any of the progeny want to hide an ancestor? |
||
451 | if ($ancestor_to_hide = $instance->stat('hide_ancestor')) { |
||
452 | // note for killing later |
||
453 | $kill_ancestors[] = $ancestor_to_hide; |
||
454 | } |
||
455 | } |
||
456 | |||
457 | // If any of the descendents don't want any of the elders to show up, cruelly render the elders surplus to |
||
458 | // requirements |
||
459 | if ($kill_ancestors) { |
||
460 | $kill_ancestors = array_unique($kill_ancestors); |
||
461 | foreach ($kill_ancestors as $mark) { |
||
462 | // unset from $classes |
||
463 | $idx = array_search($mark, $classes, true); |
||
464 | if ($idx !== false) { |
||
465 | unset($classes[$idx]); |
||
466 | } |
||
467 | } |
||
468 | } |
||
469 | |||
470 | return $classes; |
||
471 | } |
||
472 | |||
473 | /** |
||
474 | * Replace a "[sitetree_link id=n]" shortcode with a link to the page with the corresponding ID. |
||
475 | * |
||
476 | * @param array $arguments |
||
477 | * @param string $content |
||
478 | * @param ShortcodeParser $parser |
||
479 | * @return string |
||
480 | */ |
||
481 | public static function link_shortcode_handler($arguments, $content = null, $parser = null) |
||
482 | { |
||
483 | if (!isset($arguments['id']) || !is_numeric($arguments['id'])) { |
||
484 | return null; |
||
485 | } |
||
486 | |||
487 | /** @var SiteTree $page */ |
||
488 | if (!($page = DataObject::get_by_id(self::class, $arguments['id'])) // Get the current page by ID. |
||
489 | && !($page = Versioned::get_latest_version(self::class, $arguments['id'])) // Attempt link to old version. |
||
490 | ) { |
||
491 | return null; // There were no suitable matches at all. |
||
492 | } |
||
493 | |||
494 | /** @var SiteTree $page */ |
||
495 | $link = Convert::raw2att($page->Link()); |
||
496 | |||
497 | if ($content) { |
||
498 | return sprintf('<a href="%s">%s</a>', $link, $parser->parse($content)); |
||
499 | } else { |
||
500 | return $link; |
||
501 | } |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * Return the link for this {@link SiteTree} object, with the {@link Director::baseURL()} included. |
||
506 | * |
||
507 | * @param string $action Optional controller action (method). |
||
508 | * Note: URI encoding of this parameter is applied automatically through template casting, |
||
509 | * don't encode the passed parameter. Please use {@link Controller::join_links()} instead to |
||
510 | * append GET parameters. |
||
511 | * @return string |
||
512 | */ |
||
513 | public function Link($action = null) |
||
514 | { |
||
515 | return Controller::join_links(Director::baseURL(), $this->RelativeLink($action)); |
||
516 | } |
||
517 | |||
518 | /** |
||
519 | * Get the absolute URL for this page, including protocol and host. |
||
520 | * |
||
521 | * @param string $action See {@link Link()} |
||
522 | * @return string |
||
523 | */ |
||
524 | public function AbsoluteLink($action = null) |
||
525 | { |
||
526 | if ($this->hasMethod('alternateAbsoluteLink')) { |
||
527 | return $this->alternateAbsoluteLink($action); |
||
528 | } else { |
||
529 | return Director::absoluteURL($this->Link($action)); |
||
530 | } |
||
531 | } |
||
532 | |||
533 | /** |
||
534 | * Base link used for previewing. Defaults to absolute URL, in order to account for domain changes, e.g. on multi |
||
535 | * site setups. Does not contain hints about the stage, see {@link SilverStripeNavigator} for details. |
||
536 | * |
||
537 | * @param string $action See {@link Link()} |
||
538 | * @return string |
||
539 | */ |
||
540 | public function PreviewLink($action = null) |
||
541 | { |
||
542 | if ($this->hasMethod('alternatePreviewLink')) { |
||
543 | Deprecation::notice('5.0', 'Use updatePreviewLink or override PreviewLink method'); |
||
544 | return $this->alternatePreviewLink($action); |
||
545 | } |
||
546 | |||
547 | $link = $this->AbsoluteLink($action); |
||
548 | $this->extend('updatePreviewLink', $link, $action); |
||
549 | return $link; |
||
550 | } |
||
551 | |||
552 | public function getMimeType() |
||
553 | { |
||
554 | return 'text/html'; |
||
555 | } |
||
556 | |||
557 | /** |
||
558 | * Return the link for this {@link SiteTree} object relative to the SilverStripe root. |
||
559 | * |
||
560 | * By default, if this page is the current home page, and there is no action specified then this will return a link |
||
561 | * to the root of the site. However, if you set the $action parameter to TRUE then the link will not be rewritten |
||
562 | * and returned in its full form. |
||
563 | * |
||
564 | * @uses RootURLController::get_homepage_link() |
||
565 | * |
||
566 | * @param string $action See {@link Link()} |
||
567 | * @return string |
||
568 | */ |
||
569 | public function RelativeLink($action = null) |
||
570 | { |
||
571 | if ($this->ParentID && self::config()->nested_urls) { |
||
572 | $parent = $this->Parent(); |
||
573 | // If page is removed select parent from version history (for archive page view) |
||
574 | if ((!$parent || !$parent->exists()) && !$this->isOnDraft()) { |
||
575 | $parent = Versioned::get_latest_version(self::class, $this->ParentID); |
||
576 | } |
||
577 | $base = $parent->RelativeLink($this->URLSegment); |
||
578 | } elseif (!$action && $this->URLSegment == RootURLController::get_homepage_link()) { |
||
579 | // Unset base for root-level homepages. |
||
580 | // Note: Homepages with action parameters (or $action === true) |
||
581 | // need to retain their URLSegment. |
||
582 | $base = null; |
||
583 | } else { |
||
584 | $base = $this->URLSegment; |
||
585 | } |
||
586 | |||
587 | $this->extend('updateRelativeLink', $base, $action); |
||
588 | |||
589 | // Legacy support: If $action === true, retain URLSegment for homepages, |
||
590 | // but don't append any action |
||
591 | if ($action === true) { |
||
592 | $action = null; |
||
593 | } |
||
594 | |||
595 | return Controller::join_links($base, '/', $action); |
||
596 | } |
||
597 | |||
598 | /** |
||
599 | * Get the absolute URL for this page on the Live site. |
||
600 | * |
||
601 | * @param bool $includeStageEqualsLive Whether to append the URL with ?stage=Live to force Live mode |
||
602 | * @return string |
||
603 | */ |
||
604 | public function getAbsoluteLiveLink($includeStageEqualsLive = true) |
||
605 | { |
||
606 | $oldReadingMode = Versioned::get_reading_mode(); |
||
607 | Versioned::set_stage(Versioned::LIVE); |
||
608 | /** @var SiteTree $live */ |
||
609 | $live = Versioned::get_one_by_stage(self::class, Versioned::LIVE, array( |
||
610 | '"SiteTree"."ID"' => $this->ID |
||
611 | )); |
||
612 | if ($live) { |
||
613 | $link = $live->AbsoluteLink(); |
||
614 | if ($includeStageEqualsLive) { |
||
615 | $link = Controller::join_links($link, '?stage=Live'); |
||
616 | } |
||
617 | } else { |
||
618 | $link = null; |
||
619 | } |
||
620 | |||
621 | Versioned::set_reading_mode($oldReadingMode); |
||
622 | return $link; |
||
623 | } |
||
624 | |||
625 | /** |
||
626 | * Generates a link to edit this page in the CMS. |
||
627 | * |
||
628 | * @return string |
||
629 | */ |
||
630 | public function CMSEditLink() |
||
631 | { |
||
632 | $link = Controller::join_links( |
||
633 | CMSPageEditController::singleton()->Link('show'), |
||
634 | $this->ID |
||
635 | ); |
||
636 | return Director::absoluteURL($link); |
||
637 | } |
||
638 | |||
639 | |||
640 | /** |
||
641 | * Return a CSS identifier generated from this page's link. |
||
642 | * |
||
643 | * @return string The URL segment |
||
644 | */ |
||
645 | public function ElementName() |
||
646 | { |
||
647 | return str_replace('/', '-', trim($this->RelativeLink(true), '/')); |
||
648 | } |
||
649 | |||
650 | /** |
||
651 | * Returns true if this is the currently active page being used to handle this request. |
||
652 | * |
||
653 | * @return bool |
||
654 | */ |
||
655 | public function isCurrent() |
||
656 | { |
||
657 | $currentPage = Director::get_current_page(); |
||
658 | if ($currentPage instanceof ContentController) { |
||
659 | $currentPage = $currentPage->data(); |
||
660 | } |
||
661 | if ($currentPage instanceof SiteTree) { |
||
662 | return $currentPage === $this || $currentPage->ID === $this->ID; |
||
663 | } |
||
664 | return false; |
||
665 | } |
||
666 | |||
667 | /** |
||
668 | * Check if this page is in the currently active section (e.g. it is either current or one of its children is |
||
669 | * currently being viewed). |
||
670 | * |
||
671 | * @return bool |
||
672 | */ |
||
673 | public function isSection() |
||
674 | { |
||
675 | return $this->isCurrent() || ( |
||
676 | Director::get_current_page() instanceof SiteTree && in_array($this->ID, Director::get_current_page()->getAncestors()->column()) |
||
677 | ); |
||
678 | } |
||
679 | |||
680 | /** |
||
681 | * Check if the parent of this page has been removed (or made otherwise unavailable), and is still referenced by |
||
682 | * this child. Any such orphaned page may still require access via the CMS, but should not be shown as accessible |
||
683 | * to external users. |
||
684 | * |
||
685 | * @return bool |
||
686 | */ |
||
687 | public function isOrphaned() |
||
688 | { |
||
689 | // Always false for root pages |
||
690 | if (empty($this->ParentID)) { |
||
691 | return false; |
||
692 | } |
||
693 | |||
694 | // Parent must exist and not be an orphan itself |
||
695 | $parent = $this->Parent(); |
||
696 | return !$parent || !$parent->exists() || $parent->isOrphaned(); |
||
697 | } |
||
698 | |||
699 | /** |
||
700 | * Return "link" or "current" depending on if this is the {@link SiteTree::isCurrent()} current page. |
||
701 | * |
||
702 | * @return string |
||
703 | */ |
||
704 | public function LinkOrCurrent() |
||
705 | { |
||
706 | return $this->isCurrent() ? 'current' : 'link'; |
||
707 | } |
||
708 | |||
709 | /** |
||
710 | * Return "link" or "section" depending on if this is the {@link SiteTree::isSeciton()} current section. |
||
711 | * |
||
712 | * @return string |
||
713 | */ |
||
714 | public function LinkOrSection() |
||
715 | { |
||
716 | return $this->isSection() ? 'section' : 'link'; |
||
717 | } |
||
718 | |||
719 | /** |
||
720 | * Return "link", "current" or "section" depending on if this page is the current page, or not on the current page |
||
721 | * but in the current section. |
||
722 | * |
||
723 | * @return string |
||
724 | */ |
||
725 | public function LinkingMode() |
||
726 | { |
||
727 | if ($this->isCurrent()) { |
||
728 | return 'current'; |
||
729 | } elseif ($this->isSection()) { |
||
730 | return 'section'; |
||
731 | } else { |
||
732 | return 'link'; |
||
733 | } |
||
734 | } |
||
735 | |||
736 | /** |
||
737 | * Check if this page is in the given current section. |
||
738 | * |
||
739 | * @param string $sectionName Name of the section to check |
||
740 | * @return bool True if we are in the given section |
||
741 | */ |
||
742 | public function InSection($sectionName) |
||
743 | { |
||
744 | $page = Director::get_current_page(); |
||
745 | while ($page && $page->exists()) { |
||
746 | if ($sectionName == $page->URLSegment) { |
||
747 | return true; |
||
748 | } |
||
749 | $page = $page->Parent(); |
||
750 | } |
||
751 | return false; |
||
752 | } |
||
753 | |||
754 | /** |
||
755 | * Reset Sort on duped page |
||
756 | * |
||
757 | * @param SiteTree $original |
||
758 | * @param bool $doWrite |
||
759 | */ |
||
760 | public function onBeforeDuplicate($original, $doWrite) |
||
761 | { |
||
762 | $this->Sort = 0; |
||
763 | } |
||
764 | |||
765 | /** |
||
766 | * Duplicates each child of this node recursively and returns the top-level duplicate node. |
||
767 | * |
||
768 | * @return static The duplicated object |
||
769 | */ |
||
770 | public function duplicateWithChildren() |
||
771 | { |
||
772 | /** @var SiteTree $clone */ |
||
773 | $clone = $this->duplicate(); |
||
774 | $children = $this->AllChildren(); |
||
775 | |||
776 | if ($children) { |
||
777 | /** @var SiteTree $child */ |
||
778 | $sort = 0; |
||
779 | foreach ($children as $child) { |
||
780 | $childClone = $child->duplicateWithChildren(); |
||
781 | $childClone->ParentID = $clone->ID; |
||
782 | //retain sort order by manually setting sort values |
||
783 | $childClone->Sort = ++$sort; |
||
784 | $childClone->write(); |
||
785 | } |
||
786 | } |
||
787 | |||
788 | return $clone; |
||
789 | } |
||
790 | |||
791 | /** |
||
792 | * Duplicate this node and its children as a child of the node with the given ID |
||
793 | * |
||
794 | * @param int $id ID of the new node's new parent |
||
795 | */ |
||
796 | public function duplicateAsChild($id) |
||
797 | { |
||
798 | /** @var SiteTree $newSiteTree */ |
||
799 | $newSiteTree = $this->duplicate(); |
||
800 | $newSiteTree->ParentID = $id; |
||
801 | $newSiteTree->Sort = 0; |
||
802 | $newSiteTree->write(); |
||
803 | } |
||
804 | |||
805 | /** |
||
806 | * Return a breadcrumb trail to this page. Excludes "hidden" pages (with ShowInMenus=0) by default. |
||
807 | * |
||
808 | * @param int $maxDepth The maximum depth to traverse. |
||
809 | * @param boolean $unlinked Whether to link page titles. |
||
810 | * @param boolean|string $stopAtPageType ClassName of a page to stop the upwards traversal. |
||
811 | * @param boolean $showHidden Include pages marked with the attribute ShowInMenus = 0 |
||
812 | * @return string The breadcrumb trail. |
||
813 | */ |
||
814 | public function Breadcrumbs($maxDepth = 20, $unlinked = false, $stopAtPageType = false, $showHidden = false) |
||
815 | { |
||
816 | $pages = $this->getBreadcrumbItems($maxDepth, $stopAtPageType, $showHidden); |
||
817 | $template = new SSViewer('BreadcrumbsTemplate'); |
||
818 | return $template->process($this->customise(new ArrayData(array( |
||
819 | "Pages" => $pages, |
||
820 | "Unlinked" => $unlinked |
||
821 | )))); |
||
822 | } |
||
823 | |||
824 | |||
825 | /** |
||
826 | * Returns a list of breadcrumbs for the current page. |
||
827 | * |
||
828 | * @param int $maxDepth The maximum depth to traverse. |
||
829 | * @param boolean|string $stopAtPageType ClassName of a page to stop the upwards traversal. |
||
830 | * @param boolean $showHidden Include pages marked with the attribute ShowInMenus = 0 |
||
831 | * |
||
832 | * @return ArrayList |
||
833 | */ |
||
834 | public function getBreadcrumbItems($maxDepth = 20, $stopAtPageType = false, $showHidden = false) |
||
835 | { |
||
836 | $page = $this; |
||
837 | $pages = array(); |
||
838 | |||
839 | while ($page |
||
840 | && $page->exists() |
||
841 | && (!$maxDepth || count($pages) < $maxDepth) |
||
842 | && (!$stopAtPageType || $page->ClassName != $stopAtPageType) |
||
843 | ) { |
||
844 | if ($showHidden || $page->ShowInMenus || ($page->ID == $this->ID)) { |
||
845 | $pages[] = $page; |
||
846 | } |
||
847 | |||
848 | $page = $page->Parent(); |
||
849 | } |
||
850 | |||
851 | return new ArrayList(array_reverse($pages)); |
||
852 | } |
||
853 | |||
854 | |||
855 | /** |
||
856 | * Make this page a child of another page. |
||
857 | * |
||
858 | * If the parent page does not exist, resolve it to a valid ID before updating this page's reference. |
||
859 | * |
||
860 | * @param SiteTree|int $item Either the parent object, or the parent ID |
||
861 | */ |
||
862 | public function setParent($item) |
||
863 | { |
||
864 | if (is_object($item)) { |
||
865 | if (!$item->exists()) { |
||
866 | $item->write(); |
||
867 | } |
||
868 | $this->setField("ParentID", $item->ID); |
||
869 | } else { |
||
870 | $this->setField("ParentID", $item); |
||
871 | } |
||
872 | } |
||
873 | |||
874 | /** |
||
875 | * Get the parent of this page. |
||
876 | * |
||
877 | * @return SiteTree Parent of this page |
||
878 | */ |
||
879 | public function getParent() |
||
880 | { |
||
881 | if ($parentID = $this->getField("ParentID")) { |
||
882 | return DataObject::get_by_id(self::class, $parentID); |
||
883 | } |
||
884 | return null; |
||
885 | } |
||
886 | |||
887 | /** |
||
888 | * Return a string of the form "parent - page" or "grandparent - parent - page" using page titles |
||
889 | * |
||
890 | * @param int $level The maximum amount of levels to traverse. |
||
891 | * @param string $separator Seperating string |
||
892 | * @return string The resulting string |
||
893 | */ |
||
894 | public function NestedTitle($level = 2, $separator = " - ") |
||
895 | { |
||
896 | $item = $this; |
||
897 | $parts = []; |
||
898 | while ($item && $level > 0) { |
||
899 | $parts[] = $item->Title; |
||
900 | $item = $item->getParent(); |
||
901 | $level--; |
||
902 | } |
||
903 | return implode($separator, array_reverse($parts)); |
||
904 | } |
||
905 | |||
906 | /** |
||
907 | * This function should return true if the current user can execute this action. It can be overloaded to customise |
||
908 | * the security model for an application. |
||
909 | * |
||
910 | * Slightly altered from parent behaviour in {@link DataObject->can()}: |
||
911 | * - Checks for existence of a method named "can<$perm>()" on the object |
||
912 | * - Calls decorators and only returns for FALSE "vetoes" |
||
913 | * - Falls back to {@link Permission::check()} |
||
914 | * - Does NOT check for many-many relations named "Can<$perm>" |
||
915 | * |
||
916 | * @uses DataObjectDecorator->can() |
||
917 | * |
||
918 | * @param string $perm The permission to be checked, such as 'View' |
||
919 | * @param Member $member The member whose permissions need checking. Defaults to the currently logged in user. |
||
920 | * @param array $context Context argument for canCreate() |
||
921 | * @return bool True if the the member is allowed to do the given action |
||
922 | */ |
||
923 | public function can($perm, $member = null, $context = array()) |
||
924 | { |
||
925 | if (!$member) { |
||
926 | $member = Security::getCurrentUser(); |
||
927 | } |
||
928 | |||
929 | if ($member && Permission::checkMember($member, "ADMIN")) { |
||
930 | return true; |
||
931 | } |
||
932 | |||
933 | if (is_string($perm) && method_exists($this, 'can' . ucfirst($perm))) { |
||
934 | $method = 'can' . ucfirst($perm); |
||
935 | return $this->$method($member); |
||
936 | } |
||
937 | |||
938 | $results = $this->extend('can', $member); |
||
939 | if ($results && is_array($results)) { |
||
940 | if (!min($results)) { |
||
941 | return false; |
||
942 | } |
||
943 | } |
||
944 | |||
945 | return ($member && Permission::checkMember($member, $perm)); |
||
946 | } |
||
947 | |||
948 | /** |
||
949 | * This function should return true if the current user can add children to this page. It can be overloaded to |
||
950 | * customise the security model for an application. |
||
951 | * |
||
952 | * Denies permission if any of the following conditions is true: |
||
953 | * - alternateCanAddChildren() on a extension returns false |
||
954 | * - canEdit() is not granted |
||
955 | * - There are no classes defined in {@link $allowed_children} |
||
956 | * |
||
957 | * @uses SiteTreeExtension->canAddChildren() |
||
958 | * @uses canEdit() |
||
959 | * @uses $allowed_children |
||
960 | * |
||
961 | * @param Member|int $member |
||
962 | * @return bool True if the current user can add children |
||
963 | */ |
||
964 | public function canAddChildren($member = null) |
||
965 | { |
||
966 | // Disable adding children to archived pages |
||
967 | if (!$this->isOnDraft()) { |
||
968 | return false; |
||
969 | } |
||
970 | |||
971 | if (!$member) { |
||
972 | $member = Security::getCurrentUser(); |
||
973 | } |
||
974 | |||
975 | // Standard mechanism for accepting permission changes from extensions |
||
976 | $extended = $this->extendedCan('canAddChildren', $member); |
||
977 | if ($extended !== null) { |
||
978 | return $extended; |
||
979 | } |
||
980 | |||
981 | // Default permissions |
||
982 | if ($member && Permission::checkMember($member, "ADMIN")) { |
||
983 | return true; |
||
984 | } |
||
985 | |||
986 | return $this->canEdit($member) && $this->stat('allowed_children') !== 'none'; |
||
987 | } |
||
988 | |||
989 | /** |
||
990 | * This function should return true if the current user can view this page. It can be overloaded to customise the |
||
991 | * security model for an application. |
||
992 | * |
||
993 | * Denies permission if any of the following conditions is true: |
||
994 | * - canView() on any extension returns false |
||
995 | * - "CanViewType" directive is set to "Inherit" and any parent page return false for canView() |
||
996 | * - "CanViewType" directive is set to "LoggedInUsers" and no user is logged in |
||
997 | * - "CanViewType" directive is set to "OnlyTheseUsers" and user is not in the given groups |
||
998 | * |
||
999 | * @uses DataExtension->canView() |
||
1000 | * @uses ViewerGroups() |
||
1001 | * |
||
1002 | * @param Member $member |
||
1003 | * @return bool True if the current user can view this page |
||
1004 | */ |
||
1005 | public function canView($member = null) |
||
1006 | { |
||
1007 | if (!$member) { |
||
1008 | $member = Security::getCurrentUser(); |
||
1009 | } |
||
1010 | |||
1011 | // Standard mechanism for accepting permission changes from extensions |
||
1012 | $extended = $this->extendedCan('canView', $member); |
||
1013 | if ($extended !== null) { |
||
1014 | return $extended; |
||
1015 | } |
||
1016 | |||
1017 | // admin override |
||
1018 | if ($member && Permission::checkMember($member, array("ADMIN", "SITETREE_VIEW_ALL"))) { |
||
1019 | return true; |
||
1020 | } |
||
1021 | |||
1022 | // Orphaned pages (in the current stage) are unavailable, except for admins via the CMS |
||
1023 | if ($this->isOrphaned()) { |
||
1024 | return false; |
||
1025 | } |
||
1026 | |||
1027 | // Note: getInheritedPermissions() is disused in this instance |
||
1028 | // to allow parent canView extensions to influence subpage canView() |
||
1029 | |||
1030 | // check for empty spec |
||
1031 | if (!$this->CanViewType || $this->CanViewType === InheritedPermissions::ANYONE) { |
||
1032 | return true; |
||
1033 | } |
||
1034 | |||
1035 | // check for inherit |
||
1036 | if ($this->CanViewType === InheritedPermissions::INHERIT) { |
||
1037 | if ($this->ParentID) { |
||
1038 | return $this->Parent()->canView($member); |
||
1039 | } else { |
||
1040 | return $this->getSiteConfig()->canViewPages($member); |
||
1041 | } |
||
1042 | } |
||
1043 | |||
1044 | // check for any logged-in users |
||
1045 | if ($this->CanViewType === InheritedPermissions::LOGGED_IN_USERS && $member && $member->ID) { |
||
1046 | return true; |
||
1047 | } |
||
1048 | |||
1049 | // check for specific groups |
||
1050 | if ($this->CanViewType === InheritedPermissions::ONLY_THESE_USERS |
||
1051 | && $member |
||
1052 | && $member->inGroups($this->ViewerGroups()) |
||
1053 | ) { |
||
1054 | return true; |
||
1055 | } |
||
1056 | |||
1057 | return false; |
||
1058 | } |
||
1059 | |||
1060 | /** |
||
1061 | * Check if this page can be published |
||
1062 | * |
||
1063 | * @param Member $member |
||
1064 | * @return bool |
||
1065 | */ |
||
1066 | View Code Duplication | public function canPublish($member = null) |
|
1067 | { |
||
1068 | if (!$member) { |
||
1069 | $member = Security::getCurrentUser(); |
||
1070 | } |
||
1071 | |||
1072 | // Check extension |
||
1073 | $extended = $this->extendedCan('canPublish', $member); |
||
1074 | if ($extended !== null) { |
||
1075 | return $extended; |
||
1076 | } |
||
1077 | |||
1078 | if (Permission::checkMember($member, "ADMIN")) { |
||
1079 | return true; |
||
1080 | } |
||
1081 | |||
1082 | // Default to relying on edit permission |
||
1083 | return $this->canEdit($member); |
||
1084 | } |
||
1085 | |||
1086 | /** |
||
1087 | * This function should return true if the current user can delete this page. It can be overloaded to customise the |
||
1088 | * security model for an application. |
||
1089 | * |
||
1090 | * Denies permission if any of the following conditions is true: |
||
1091 | * - canDelete() returns false on any extension |
||
1092 | * - canEdit() returns false |
||
1093 | * - any descendant page returns false for canDelete() |
||
1094 | * |
||
1095 | * @uses canDelete() |
||
1096 | * @uses SiteTreeExtension->canDelete() |
||
1097 | * @uses canEdit() |
||
1098 | * |
||
1099 | * @param Member $member |
||
1100 | * @return bool True if the current user can delete this page |
||
1101 | */ |
||
1102 | public function canDelete($member = null) |
||
1103 | { |
||
1104 | if (!$member) { |
||
1105 | $member = Security::getCurrentUser(); |
||
1106 | } |
||
1107 | |||
1108 | // Standard mechanism for accepting permission changes from extensions |
||
1109 | $extended = $this->extendedCan('canDelete', $member); |
||
1110 | if ($extended !== null) { |
||
1111 | return $extended; |
||
1112 | } |
||
1113 | |||
1114 | if (!$member) { |
||
1115 | return false; |
||
1116 | } |
||
1117 | |||
1118 | // Default permission check |
||
1119 | if (Permission::checkMember($member, array("ADMIN", "SITETREE_EDIT_ALL"))) { |
||
1120 | return true; |
||
1121 | } |
||
1122 | |||
1123 | // Check inherited permissions |
||
1124 | return static::getPermissionChecker() |
||
1125 | ->canDelete($this->ID, $member); |
||
1126 | } |
||
1127 | |||
1128 | /** |
||
1129 | * This function should return true if the current user can create new pages of this class, regardless of class. It |
||
1130 | * can be overloaded to customise the security model for an application. |
||
1131 | * |
||
1132 | * By default, permission to create at the root level is based on the SiteConfig configuration, and permission to |
||
1133 | * create beneath a parent is based on the ability to edit that parent page. |
||
1134 | * |
||
1135 | * Use {@link canAddChildren()} to control behaviour of creating children under this page. |
||
1136 | * |
||
1137 | * @uses $can_create |
||
1138 | * @uses DataExtension->canCreate() |
||
1139 | * |
||
1140 | * @param Member $member |
||
1141 | * @param array $context Optional array which may contain array('Parent' => $parentObj) |
||
1142 | * If a parent page is known, it will be checked for validity. |
||
1143 | * If omitted, it will be assumed this is to be created as a top level page. |
||
1144 | * @return bool True if the current user can create pages on this class. |
||
1145 | */ |
||
1146 | public function canCreate($member = null, $context = array()) |
||
1147 | { |
||
1148 | if (!$member) { |
||
1149 | $member = Security::getCurrentUser(); |
||
1150 | } |
||
1151 | |||
1152 | // Check parent (custom canCreate option for SiteTree) |
||
1153 | // Block children not allowed for this parent type |
||
1154 | $parent = isset($context['Parent']) ? $context['Parent'] : null; |
||
1155 | if ($parent && !in_array(static::class, $parent->allowedChildren())) { |
||
1156 | return false; |
||
1157 | } |
||
1158 | |||
1159 | // Standard mechanism for accepting permission changes from extensions |
||
1160 | $extended = $this->extendedCan(__FUNCTION__, $member, $context); |
||
1161 | if ($extended !== null) { |
||
1162 | return $extended; |
||
1163 | } |
||
1164 | |||
1165 | // Check permission |
||
1166 | if ($member && Permission::checkMember($member, "ADMIN")) { |
||
1167 | return true; |
||
1168 | } |
||
1169 | |||
1170 | // Fall over to inherited permissions |
||
1171 | if ($parent && $parent->exists()) { |
||
1172 | return $parent->canAddChildren($member); |
||
1173 | } else { |
||
1174 | // This doesn't necessarily mean we are creating a root page, but that |
||
1175 | // we don't know if there is a parent, so default to this permission |
||
1176 | return SiteConfig::current_site_config()->canCreateTopLevel($member); |
||
1177 | } |
||
1178 | } |
||
1179 | |||
1180 | /** |
||
1181 | * This function should return true if the current user can edit this page. It can be overloaded to customise the |
||
1182 | * security model for an application. |
||
1183 | * |
||
1184 | * Denies permission if any of the following conditions is true: |
||
1185 | * - canEdit() on any extension returns false |
||
1186 | * - canView() return false |
||
1187 | * - "CanEditType" directive is set to "Inherit" and any parent page return false for canEdit() |
||
1188 | * - "CanEditType" directive is set to "LoggedInUsers" and no user is logged in or doesn't have the |
||
1189 | * CMS_Access_CMSMAIN permission code |
||
1190 | * - "CanEditType" directive is set to "OnlyTheseUsers" and user is not in the given groups |
||
1191 | * |
||
1192 | * @uses canView() |
||
1193 | * @uses EditorGroups() |
||
1194 | * @uses DataExtension->canEdit() |
||
1195 | * |
||
1196 | * @param Member $member Set to false if you want to explicitly test permissions without a valid user (useful for |
||
1197 | * unit tests) |
||
1198 | * @return bool True if the current user can edit this page |
||
1199 | */ |
||
1200 | View Code Duplication | public function canEdit($member = null) |
|
1201 | { |
||
1202 | if (!$member) { |
||
1203 | $member = Security::getCurrentUser(); |
||
1204 | } |
||
1205 | |||
1206 | // Standard mechanism for accepting permission changes from extensions |
||
1207 | $extended = $this->extendedCan('canEdit', $member); |
||
1208 | if ($extended !== null) { |
||
1209 | return $extended; |
||
1210 | } |
||
1211 | |||
1212 | // Default permissions |
||
1213 | if (Permission::checkMember($member, "SITETREE_EDIT_ALL")) { |
||
1214 | return true; |
||
1215 | } |
||
1216 | |||
1217 | // Check inherited permissions |
||
1218 | return static::getPermissionChecker() |
||
1219 | ->canEdit($this->ID, $member); |
||
1220 | } |
||
1221 | |||
1222 | /** |
||
1223 | * Stub method to get the site config, unless the current class can provide an alternate. |
||
1224 | * |
||
1225 | * @return SiteConfig |
||
1226 | */ |
||
1227 | public function getSiteConfig() |
||
1228 | { |
||
1229 | $configs = $this->invokeWithExtensions('alternateSiteConfig'); |
||
1230 | foreach (array_filter($configs) as $config) { |
||
1231 | return $config; |
||
1232 | } |
||
1233 | |||
1234 | return SiteConfig::current_site_config(); |
||
1235 | } |
||
1236 | |||
1237 | /** |
||
1238 | * @return PermissionChecker |
||
1239 | */ |
||
1240 | public static function getPermissionChecker() |
||
1241 | { |
||
1242 | return Injector::inst()->get(PermissionChecker::class.'.sitetree'); |
||
1243 | } |
||
1244 | |||
1245 | /** |
||
1246 | * Collate selected descendants of this page. |
||
1247 | * |
||
1248 | * {@link $condition} will be evaluated on each descendant, and if it is succeeds, that item will be added to the |
||
1249 | * $collator array. |
||
1250 | * |
||
1251 | * @param string $condition The PHP condition to be evaluated. The page will be called $item |
||
1252 | * @param array $collator An array, passed by reference, to collect all of the matching descendants. |
||
1253 | * @return bool |
||
1254 | */ |
||
1255 | public function collateDescendants($condition, &$collator) |
||
1256 | { |
||
1257 | $children = $this->Children(); |
||
1258 | if ($children) { |
||
1259 | foreach ($children as $item) { |
||
1260 | if (eval("return $condition;")) { |
||
1261 | $collator[] = $item; |
||
1262 | } |
||
1263 | /** @var SiteTree $item */ |
||
1264 | $item->collateDescendants($condition, $collator); |
||
1265 | } |
||
1266 | return true; |
||
1267 | } |
||
1268 | return false; |
||
1269 | } |
||
1270 | |||
1271 | /** |
||
1272 | * Return the title, description, keywords and language metatags. |
||
1273 | * |
||
1274 | * @todo Move <title> tag in separate getter for easier customization and more obvious usage |
||
1275 | * |
||
1276 | * @param bool $includeTitle Show default <title>-tag, set to false for custom templating |
||
1277 | * @return string The XHTML metatags |
||
1278 | */ |
||
1279 | public function MetaTags($includeTitle = true) |
||
1280 | { |
||
1281 | $tags = array(); |
||
1282 | if ($includeTitle && strtolower($includeTitle) != 'false') { |
||
1283 | $tags[] = HTML::createTag('title', array(), $this->obj('Title')->forTemplate()); |
||
1284 | } |
||
1285 | |||
1286 | $generator = trim(Config::inst()->get(self::class, 'meta_generator')); |
||
1287 | if (!empty($generator)) { |
||
1288 | $tags[] = HTML::createTag('meta', array( |
||
1289 | 'name' => 'generator', |
||
1290 | 'content' => $generator, |
||
1291 | )); |
||
1292 | } |
||
1293 | |||
1294 | $charset = ContentNegotiator::config()->uninherited('encoding'); |
||
1295 | $tags[] = HTML::createTag('meta', array( |
||
1296 | 'http-equiv' => 'Content-Type', |
||
1297 | 'content' => 'text/html; charset=' . $charset, |
||
1298 | )); |
||
1299 | if ($this->MetaDescription) { |
||
1300 | $tags[] = HTML::createTag('meta', array( |
||
1301 | 'name' => 'description', |
||
1302 | 'content' => $this->MetaDescription, |
||
1303 | )); |
||
1304 | } |
||
1305 | |||
1306 | if (Permission::check('CMS_ACCESS_CMSMain') |
||
1307 | && $this->ID > 0 |
||
1308 | ) { |
||
1309 | $tags[] = HTML::createTag('meta', array( |
||
1310 | 'name' => 'x-page-id', |
||
1311 | 'content' => $this->obj('ID')->forTemplate(), |
||
1312 | )); |
||
1313 | $tags[] = HTML::createTag('meta', array( |
||
1314 | 'name' => 'x-cms-edit-link', |
||
1315 | 'content' => $this->obj('CMSEditLink')->forTemplate(), |
||
1316 | )); |
||
1317 | } |
||
1318 | |||
1319 | $tags = implode("\n", $tags); |
||
1320 | if ($this->ExtraMeta) { |
||
1321 | $tags .= $this->obj('ExtraMeta')->forTemplate(); |
||
1322 | } |
||
1323 | |||
1324 | $this->extend('MetaTags', $tags); |
||
1325 | |||
1326 | return $tags; |
||
1327 | } |
||
1328 | |||
1329 | /** |
||
1330 | * Returns the object that contains the content that a user would associate with this page. |
||
1331 | * |
||
1332 | * Ordinarily, this is just the page itself, but for example on RedirectorPages or VirtualPages ContentSource() will |
||
1333 | * return the page that is linked to. |
||
1334 | * |
||
1335 | * @return $this |
||
1336 | */ |
||
1337 | public function ContentSource() |
||
1341 | |||
1342 | /** |
||
1343 | * Add default records to database. |
||
1344 | * |
||
1345 | * This function is called whenever the database is built, after the database tables have all been created. Overload |
||
1346 | * this to add default records when the database is built, but make sure you call parent::requireDefaultRecords(). |
||
1347 | */ |
||
1348 | public function requireDefaultRecords() |
||
1393 | |||
1394 | protected function onBeforeWrite() |
||
1448 | |||
1449 | /** |
||
1450 | * Trigger synchronisation of link tracking |
||
1451 | * |
||
1452 | * {@see SiteTreeLinkTracking::augmentSyncLinkTracking} |
||
1453 | */ |
||
1454 | public function syncLinkTracking() |
||
1458 | |||
1459 | public function onBeforeDelete() |
||
1471 | |||
1472 | public function onAfterDelete() |
||
1473 | { |
||
1474 | $this->updateDependentPages(); |
||
1475 | parent::onAfterDelete(); |
||
1476 | } |
||
1477 | |||
1478 | public function flushCache($persistent = true) |
||
1479 | { |
||
1480 | parent::flushCache($persistent); |
||
1481 | $this->_cache_statusFlags = null; |
||
1482 | } |
||
1483 | |||
1484 | public function validate() |
||
1485 | { |
||
1486 | $result = parent::validate(); |
||
1487 | |||
1488 | // Allowed children validation |
||
1489 | $parent = $this->getParent(); |
||
1490 | if ($parent && $parent->exists()) { |
||
1491 | // No need to check for subclasses or instanceof, as allowedChildren() already |
||
1492 | // deconstructs any inheritance trees already. |
||
1493 | $allowed = $parent->allowedChildren(); |
||
1494 | $subject = ($this instanceof VirtualPage && $this->CopyContentFromID) |
||
1495 | ? $this->CopyContentFrom() |
||
1496 | : $this; |
||
1497 | if (!in_array($subject->ClassName, $allowed)) { |
||
1498 | $result->addError( |
||
1499 | _t( |
||
1500 | 'SilverStripe\\CMS\\Model\\SiteTree.PageTypeNotAllowed', |
||
1501 | 'Page type "{type}" not allowed as child of this parent page', |
||
1502 | array('type' => $subject->i18n_singular_name()) |
||
1503 | ), |
||
1504 | ValidationResult::TYPE_ERROR, |
||
1505 | 'ALLOWED_CHILDREN' |
||
1506 | ); |
||
1507 | } |
||
1508 | } |
||
1509 | |||
1510 | // "Can be root" validation |
||
1525 | |||
1526 | /** |
||
1527 | * Returns true if this object has a URLSegment value that does not conflict with any other objects. This method |
||
1528 | * checks for: |
||
1529 | * - A page with the same URLSegment that has a conflict |
||
1530 | * - Conflicts with actions on the parent page |
||
1531 | * - A conflict caused by a root page having the same URLSegment as a class name |
||
1532 | * |
||
1533 | * @return bool |
||
1534 | */ |
||
1535 | public function validURLSegment() |
||
1574 | |||
1575 | /** |
||
1576 | * Generate a URL segment based on the title provided. |
||
1577 | * |
||
1578 | * If {@link Extension}s wish to alter URL segment generation, they can do so by defining |
||
1579 | * updateURLSegment(&$url, $title). $url will be passed by reference and should be modified. $title will contain |
||
1580 | * the title that was originally used as the source of this generated URL. This lets extensions either start from |
||
1581 | * scratch, or incrementally modify the generated URL. |
||
1582 | * |
||
1583 | * @param string $title Page title |
||
1584 | * @return string Generated url segment |
||
1585 | */ |
||
1586 | public function generateURLSegment($title) |
||
1601 | |||
1602 | /** |
||
1603 | * Gets the URL segment for the latest draft version of this page. |
||
1604 | * |
||
1605 | * @return string |
||
1606 | */ |
||
1607 | public function getStageURLSegment() |
||
1614 | |||
1615 | /** |
||
1616 | * Gets the URL segment for the currently published version of this page. |
||
1617 | * |
||
1618 | * @return string |
||
1619 | */ |
||
1620 | public function getLiveURLSegment() |
||
1627 | |||
1628 | /** |
||
1629 | * Returns the pages that depend on this page. This includes virtual pages, pages that link to it, etc. |
||
1630 | * |
||
1631 | * @param bool $includeVirtuals Set to false to exlcude virtual pages. |
||
1632 | * @return ArrayList |
||
1633 | */ |
||
1634 | public function DependentPages($includeVirtuals = true) |
||
1687 | |||
1688 | /** |
||
1689 | * Return all virtual pages that link to this page. |
||
1690 | * |
||
1691 | * @return DataList |
||
1692 | */ |
||
1693 | public function VirtualPages() |
||
1704 | |||
1705 | /** |
||
1706 | * Returns a FieldList with which to create the main editing form. |
||
1707 | * |
||
1708 | * You can override this in your child classes to add extra fields - first get the parent fields using |
||
1709 | * parent::getCMSFields(), then use addFieldToTab() on the FieldList. |
||
1710 | * |
||
1711 | * See {@link getSettingsFields()} for a different set of fields concerned with configuration aspects on the record, |
||
1712 | * e.g. access control. |
||
1713 | * |
||
1714 | * @return FieldList The fields to be displayed in the CMS |
||
1715 | */ |
||
1716 | public function getCMSFields() |
||
1908 | |||
1909 | |||
1910 | /** |
||
1911 | * Returns fields related to configuration aspects on this record, e.g. access control. See {@link getCMSFields()} |
||
1912 | * for content-related fields. |
||
1913 | * |
||
1914 | * @return FieldList |
||
1915 | */ |
||
1916 | 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) |
||
2097 | |||
2098 | /** |
||
2099 | * Get the actions available in the CMS for this page - eg Save, Publish. |
||
2100 | * |
||
2101 | * Frontend scripts and styles know how to handle the following FormFields: |
||
2102 | * - top-level FormActions appear as standalone buttons |
||
2103 | * - top-level CompositeField with FormActions within appear as grouped buttons |
||
2104 | * - TabSet & Tabs appear as a drop ups |
||
2105 | * - FormActions within the Tab are restyled as links |
||
2106 | * - major actions can provide alternate states for richer presentation (see ssui.button widget extension) |
||
2107 | * |
||
2108 | * @return FieldList The available actions for this page. |
||
2109 | */ |
||
2110 | public function getCMSActions() |
||
2269 | |||
2270 | public function onAfterPublish() |
||
2280 | |||
2281 | /** |
||
2282 | * Update draft dependant pages |
||
2283 | */ |
||
2284 | public function onAfterRevertToLive() |
||
2297 | |||
2298 | /** |
||
2299 | * Determine if this page references a parent which is archived, and not available in stage |
||
2300 | * |
||
2301 | * @return bool True if there is an archived parent |
||
2302 | */ |
||
2303 | protected function isParentArchived() |
||
2314 | |||
2315 | /** |
||
2316 | * Restore the content in the active copy of this SiteTree page to the stage site. |
||
2317 | * |
||
2318 | * @return self |
||
2319 | */ |
||
2320 | public function doRestoreToStage() |
||
2359 | |||
2360 | /** |
||
2361 | * Check if this page is new - that is, if it has yet to have been written to the database. |
||
2362 | * |
||
2363 | * @return bool |
||
2364 | */ |
||
2365 | public function isNew() |
||
2381 | |||
2382 | /** |
||
2383 | * Get the class dropdown used in the CMS to change the class of a page. This returns the list of options in the |
||
2384 | * dropdown as a Map from class name to singular name. Filters by {@link SiteTree->canCreate()}, as well as |
||
2385 | * {@link SiteTree::$needs_permission}. |
||
2386 | * |
||
2387 | * @return array |
||
2388 | */ |
||
2389 | protected function getClassDropdown() |
||
2439 | |||
2440 | /** |
||
2441 | * Returns an array of the class names of classes that are allowed to be children of this class. |
||
2442 | * |
||
2443 | * @return string[] |
||
2444 | */ |
||
2445 | public function allowedChildren() |
||
2479 | |||
2480 | /** |
||
2481 | * Returns the class name of the default class for children of this page. |
||
2482 | * |
||
2483 | * @return string |
||
2484 | */ |
||
2485 | public function defaultChild() |
||
2497 | |||
2498 | /** |
||
2499 | * Returns the class name of the default class for the parent of this page. |
||
2500 | * |
||
2501 | * @return string |
||
2502 | */ |
||
2503 | public function defaultParent() |
||
2507 | |||
2508 | /** |
||
2509 | * Get the title for use in menus for this page. If the MenuTitle field is set it returns that, else it returns the |
||
2510 | * Title field. |
||
2511 | * |
||
2512 | * @return string |
||
2513 | */ |
||
2514 | public function getMenuTitle() |
||
2522 | |||
2523 | |||
2524 | /** |
||
2525 | * Set the menu title for this page. |
||
2526 | * |
||
2527 | * @param string $value |
||
2528 | */ |
||
2529 | public function setMenuTitle($value) |
||
2537 | |||
2538 | /** |
||
2539 | * A flag provides the user with additional data about the current page status, for example a "removed from draft" |
||
2540 | * status. Each page can have more than one status flag. Returns a map of a unique key to a (localized) title for |
||
2541 | * the flag. The unique key can be reused as a CSS class. Use the 'updateStatusFlags' extension point to customize |
||
2542 | * the flags. |
||
2543 | * |
||
2544 | * Example (simple): |
||
2545 | * "deletedonlive" => "Deleted" |
||
2546 | * |
||
2547 | * Example (with optional title attribute): |
||
2548 | * "deletedonlive" => array('text' => "Deleted", 'title' => 'This page has been deleted') |
||
2549 | * |
||
2550 | * @param bool $cached Whether to serve the fields from cache; false regenerate them |
||
2551 | * @return array |
||
2552 | */ |
||
2553 | public function getStatusFlags($cached = true) |
||
2586 | |||
2587 | /** |
||
2588 | * getTreeTitle will return three <span> html DOM elements, an empty <span> with the class 'jstree-pageicon' in |
||
2589 | * front, following by a <span> wrapping around its MenutTitle, then following by a <span> indicating its |
||
2590 | * publication status. |
||
2591 | * |
||
2592 | * @return string An HTML string ready to be directly used in a template |
||
2593 | */ |
||
2594 | public function getTreeTitle() |
||
2628 | |||
2629 | /** |
||
2630 | * Returns the page in the current page stack of the given level. Level(1) will return the main menu item that |
||
2631 | * we're currently inside, etc. |
||
2632 | * |
||
2633 | * @param int $level |
||
2634 | * @return SiteTree |
||
2635 | */ |
||
2636 | public function Level($level) |
||
2646 | |||
2647 | /** |
||
2648 | * Gets the depth of this page in the sitetree, where 1 is the root level |
||
2649 | * |
||
2650 | * @return int |
||
2651 | */ |
||
2652 | public function getPageLevel() |
||
2659 | |||
2660 | /** |
||
2661 | * Find the controller name by our convention of {$ModelClass}Controller |
||
2662 | * |
||
2663 | * @return string |
||
2664 | */ |
||
2665 | public function getControllerName() |
||
2696 | |||
2697 | /** |
||
2698 | * Return the CSS classes to apply to this node in the CMS tree. |
||
2699 | * |
||
2700 | * @return string |
||
2701 | */ |
||
2702 | public function CMSTreeClasses() |
||
2727 | |||
2728 | /** |
||
2729 | * Stops extendCMSFields() being called on getCMSFields(). This is useful when you need access to fields added by |
||
2730 | * subclasses of SiteTree in a extension. Call before calling parent::getCMSFields(), and reenable afterwards. |
||
2731 | */ |
||
2732 | public static function disableCMSFieldsExtensions() |
||
2736 | |||
2737 | /** |
||
2738 | * Reenables extendCMSFields() being called on getCMSFields() after it has been disabled by |
||
2739 | * disableCMSFieldsExtensions(). |
||
2740 | */ |
||
2741 | public static function enableCMSFieldsExtensions() |
||
2745 | |||
2746 | public function providePermissions() |
||
2781 | |||
2782 | /** |
||
2783 | * Default singular name for page / sitetree |
||
2784 | * |
||
2785 | * @return string |
||
2786 | */ |
||
2787 | View Code Duplication | public function singular_name() |
|
2795 | |||
2796 | /** |
||
2797 | * Default plural name for page / sitetree |
||
2798 | * |
||
2799 | * @return string |
||
2800 | */ |
||
2801 | View Code Duplication | public function plural_name() |
|
2809 | |||
2810 | /** |
||
2811 | * Get description for this page type |
||
2812 | * |
||
2813 | * @return string|null |
||
2814 | */ |
||
2815 | public function classDescription() |
||
2823 | |||
2824 | /** |
||
2825 | * Get localised description for this page |
||
2826 | * |
||
2827 | * @return string|null |
||
2828 | */ |
||
2829 | public function i18n_classDescription() |
||
2837 | |||
2838 | /** |
||
2839 | * Overloaded to also provide entities for 'Page' class which is usually located in custom code, hence textcollector |
||
2840 | * picks it up for the wrong folder. |
||
2841 | * |
||
2842 | * @return array |
||
2843 | */ |
||
2844 | public function provideI18nEntities() |
||
2855 | |||
2856 | /** |
||
2857 | * Returns 'root' if the current page has no parent, or 'subpage' otherwise |
||
2858 | * |
||
2859 | * @return string |
||
2860 | */ |
||
2861 | public function getParentType() |
||
2865 | |||
2866 | /** |
||
2867 | * Clear the permissions cache for SiteTree |
||
2868 | */ |
||
2869 | public static function reset() |
||
2876 | |||
2877 | /** |
||
2878 | * Update dependant pages |
||
2879 | */ |
||
2880 | protected function updateDependentPages() |
||
2894 | } |
||
2895 |