These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace SilverStripe\CMS\Model; |
||
4 | |||
5 | use Page; |
||
6 | use SilverStripe\CampaignAdmin\AddToCampaignHandler_FormAction; |
||
7 | use SilverStripe\CMS\Controllers\CMSPageEditController; |
||
8 | use SilverStripe\CMS\Controllers\ContentController; |
||
9 | use SilverStripe\CMS\Controllers\ModelAsController; |
||
10 | use SilverStripe\CMS\Controllers\RootURLController; |
||
11 | use SilverStripe\CMS\Forms\SiteTreeURLSegmentField; |
||
12 | use SilverStripe\Control\ContentNegotiator; |
||
13 | use SilverStripe\Control\Controller; |
||
14 | use SilverStripe\Control\Director; |
||
15 | use SilverStripe\Control\RequestHandler; |
||
16 | use SilverStripe\Core\ClassInfo; |
||
17 | use SilverStripe\Core\Config\Config; |
||
18 | use SilverStripe\Core\Convert; |
||
19 | use SilverStripe\Core\Injector\Injector; |
||
20 | use SilverStripe\Core\Resettable; |
||
21 | use SilverStripe\Dev\Deprecation; |
||
22 | use SilverStripe\Forms\CheckboxField; |
||
23 | use SilverStripe\Forms\CompositeField; |
||
24 | use SilverStripe\Forms\DropdownField; |
||
25 | use SilverStripe\Forms\FieldGroup; |
||
26 | use SilverStripe\Forms\FieldList; |
||
27 | use SilverStripe\Forms\FormAction; |
||
28 | use SilverStripe\Forms\GridField\GridField; |
||
29 | use SilverStripe\Forms\GridField\GridFieldDataColumns; |
||
30 | use SilverStripe\Forms\HTMLEditor\HTMLEditorField; |
||
31 | use SilverStripe\Forms\ListboxField; |
||
32 | use SilverStripe\Forms\LiteralField; |
||
33 | use SilverStripe\Forms\OptionsetField; |
||
34 | use SilverStripe\Forms\Tab; |
||
35 | use SilverStripe\Forms\TabSet; |
||
36 | use SilverStripe\Forms\TextareaField; |
||
37 | use SilverStripe\Forms\TextField; |
||
38 | use SilverStripe\Forms\ToggleCompositeField; |
||
39 | use SilverStripe\Forms\TreeDropdownField; |
||
40 | use SilverStripe\i18n\i18n; |
||
41 | use SilverStripe\i18n\i18nEntityProvider; |
||
42 | use SilverStripe\ORM\ArrayList; |
||
43 | use SilverStripe\ORM\CMSPreviewable; |
||
44 | use SilverStripe\ORM\DataList; |
||
45 | use SilverStripe\ORM\DataObject; |
||
46 | use SilverStripe\ORM\DB; |
||
47 | use SilverStripe\ORM\HiddenClass; |
||
48 | use SilverStripe\ORM\Hierarchy\Hierarchy; |
||
49 | use SilverStripe\ORM\ManyManyList; |
||
50 | use SilverStripe\ORM\ValidationResult; |
||
51 | use SilverStripe\Security\Group; |
||
52 | use SilverStripe\Security\InheritedPermissions; |
||
53 | use SilverStripe\Security\InheritedPermissionsExtension; |
||
54 | use SilverStripe\Security\Member; |
||
55 | use SilverStripe\Security\Permission; |
||
56 | use SilverStripe\Security\PermissionChecker; |
||
57 | use SilverStripe\Security\PermissionProvider; |
||
58 | use SilverStripe\Security\Security; |
||
59 | use SilverStripe\SiteConfig\SiteConfig; |
||
60 | use SilverStripe\Versioned\Versioned; |
||
61 | use SilverStripe\View\ArrayData; |
||
62 | use SilverStripe\View\HTML; |
||
63 | use SilverStripe\View\Parsers\ShortcodeParser; |
||
64 | use SilverStripe\View\Parsers\URLSegmentFilter; |
||
65 | use SilverStripe\View\SSViewer; |
||
66 | use Subsite; |
||
67 | |||
68 | /** |
||
69 | * Basic data-object representing all pages within the site tree. All page types that live within the hierarchy should |
||
70 | * inherit from this. In addition, it contains a number of static methods for querying the site tree and working with |
||
71 | * draft and published states. |
||
72 | * |
||
73 | * <h2>URLs</h2> |
||
74 | * A page is identified during request handling via its "URLSegment" database column. As pages can be nested, the full |
||
75 | * path of a URL might contain multiple segments. Each segment is stored in its filtered representation (through |
||
76 | * {@link URLSegmentFilter}). The full path is constructed via {@link Link()}, {@link RelativeLink()} and |
||
77 | * {@link AbsoluteLink()}. You can allow these segments to contain multibyte characters through |
||
78 | * {@link URLSegmentFilter::$default_allow_multibyte}. |
||
79 | * |
||
80 | * @property string URLSegment |
||
81 | * @property string Title |
||
82 | * @property string MenuTitle |
||
83 | * @property string Content HTML content of the page. |
||
84 | * @property string MetaDescription |
||
85 | * @property string ExtraMeta |
||
86 | * @property string ShowInMenus |
||
87 | * @property string ShowInSearch |
||
88 | * @property string Sort Integer value denoting the sort order. |
||
89 | * @property string ReportClass |
||
90 | * |
||
91 | * @method ManyManyList ViewerGroups() List of groups that can view this object. |
||
92 | * @method ManyManyList EditorGroups() List of groups that can edit this object. |
||
93 | * @method SiteTree Parent() |
||
94 | * |
||
95 | * @mixin Hierarchy |
||
96 | * @mixin Versioned |
||
97 | * @mixin SiteTreeLinkTracking |
||
98 | * @mixin InheritedPermissionsExtension |
||
99 | */ |
||
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 = method_exists($child, 'duplicateWithChildren') |
||
781 | ? $child->duplicateWithChildren() |
||
782 | : $child->duplicate(); |
||
783 | $childClone->ParentID = $clone->ID; |
||
784 | //retain sort order by manually setting sort values |
||
785 | $childClone->Sort = ++$sort; |
||
786 | $childClone->write(); |
||
787 | } |
||
788 | } |
||
789 | |||
790 | return $clone; |
||
791 | } |
||
792 | |||
793 | /** |
||
794 | * Duplicate this node and its children as a child of the node with the given ID |
||
795 | * |
||
796 | * @param int $id ID of the new node's new parent |
||
797 | */ |
||
798 | public function duplicateAsChild($id) |
||
799 | { |
||
800 | /** @var SiteTree $newSiteTree */ |
||
801 | $newSiteTree = $this->duplicate(); |
||
802 | $newSiteTree->ParentID = $id; |
||
803 | $newSiteTree->Sort = 0; |
||
804 | $newSiteTree->write(); |
||
805 | } |
||
806 | |||
807 | /** |
||
808 | * Return a breadcrumb trail to this page. Excludes "hidden" pages (with ShowInMenus=0) by default. |
||
809 | * |
||
810 | * @param int $maxDepth The maximum depth to traverse. |
||
811 | * @param boolean $unlinked Whether to link page titles. |
||
812 | * @param boolean|string $stopAtPageType ClassName of a page to stop the upwards traversal. |
||
813 | * @param boolean $showHidden Include pages marked with the attribute ShowInMenus = 0 |
||
814 | * @return string The breadcrumb trail. |
||
815 | */ |
||
816 | public function Breadcrumbs($maxDepth = 20, $unlinked = false, $stopAtPageType = false, $showHidden = false) |
||
817 | { |
||
818 | $pages = $this->getBreadcrumbItems($maxDepth, $stopAtPageType, $showHidden); |
||
819 | $template = new SSViewer('BreadcrumbsTemplate'); |
||
820 | return $template->process($this->customise(new ArrayData(array( |
||
821 | "Pages" => $pages, |
||
822 | "Unlinked" => $unlinked |
||
823 | )))); |
||
824 | } |
||
825 | |||
826 | |||
827 | /** |
||
828 | * Returns a list of breadcrumbs for the current page. |
||
829 | * |
||
830 | * @param int $maxDepth The maximum depth to traverse. |
||
831 | * @param boolean|string $stopAtPageType ClassName of a page to stop the upwards traversal. |
||
832 | * @param boolean $showHidden Include pages marked with the attribute ShowInMenus = 0 |
||
833 | * |
||
834 | * @return ArrayList |
||
835 | */ |
||
836 | public function getBreadcrumbItems($maxDepth = 20, $stopAtPageType = false, $showHidden = false) |
||
837 | { |
||
838 | $page = $this; |
||
839 | $pages = array(); |
||
840 | |||
841 | while ($page |
||
842 | && $page->exists() |
||
843 | && (!$maxDepth || count($pages) < $maxDepth) |
||
844 | && (!$stopAtPageType || $page->ClassName != $stopAtPageType) |
||
845 | ) { |
||
846 | if ($showHidden || $page->ShowInMenus || ($page->ID == $this->ID)) { |
||
847 | $pages[] = $page; |
||
848 | } |
||
849 | |||
850 | $page = $page->Parent(); |
||
851 | } |
||
852 | |||
853 | return new ArrayList(array_reverse($pages)); |
||
854 | } |
||
855 | |||
856 | |||
857 | /** |
||
858 | * Make this page a child of another page. |
||
859 | * |
||
860 | * If the parent page does not exist, resolve it to a valid ID before updating this page's reference. |
||
861 | * |
||
862 | * @param SiteTree|int $item Either the parent object, or the parent ID |
||
863 | */ |
||
864 | public function setParent($item) |
||
865 | { |
||
866 | if (is_object($item)) { |
||
867 | if (!$item->exists()) { |
||
868 | $item->write(); |
||
869 | } |
||
870 | $this->setField("ParentID", $item->ID); |
||
871 | } else { |
||
872 | $this->setField("ParentID", $item); |
||
873 | } |
||
874 | } |
||
875 | |||
876 | /** |
||
877 | * Get the parent of this page. |
||
878 | * |
||
879 | * @return SiteTree Parent of this page |
||
880 | */ |
||
881 | public function getParent() |
||
882 | { |
||
883 | if ($parentID = $this->getField("ParentID")) { |
||
884 | return DataObject::get_by_id(self::class, $parentID); |
||
885 | } |
||
886 | return null; |
||
887 | } |
||
888 | |||
889 | /** |
||
890 | * Return a string of the form "parent - page" or "grandparent - parent - page" using page titles |
||
891 | * |
||
892 | * @param int $level The maximum amount of levels to traverse. |
||
893 | * @param string $separator Seperating string |
||
894 | * @return string The resulting string |
||
895 | */ |
||
896 | public function NestedTitle($level = 2, $separator = " - ") |
||
897 | { |
||
898 | $item = $this; |
||
899 | $parts = []; |
||
900 | while ($item && $level > 0) { |
||
901 | $parts[] = $item->Title; |
||
902 | $item = $item->getParent(); |
||
903 | $level--; |
||
904 | } |
||
905 | return implode($separator, array_reverse($parts)); |
||
906 | } |
||
907 | |||
908 | /** |
||
909 | * This function should return true if the current user can execute this action. It can be overloaded to customise |
||
910 | * the security model for an application. |
||
911 | * |
||
912 | * Slightly altered from parent behaviour in {@link DataObject->can()}: |
||
913 | * - Checks for existence of a method named "can<$perm>()" on the object |
||
914 | * - Calls decorators and only returns for FALSE "vetoes" |
||
915 | * - Falls back to {@link Permission::check()} |
||
916 | * - Does NOT check for many-many relations named "Can<$perm>" |
||
917 | * |
||
918 | * @uses DataObjectDecorator->can() |
||
919 | * |
||
920 | * @param string $perm The permission to be checked, such as 'View' |
||
921 | * @param Member $member The member whose permissions need checking. Defaults to the currently logged in user. |
||
922 | * @param array $context Context argument for canCreate() |
||
923 | * @return bool True if the the member is allowed to do the given action |
||
924 | */ |
||
925 | public function can($perm, $member = null, $context = array()) |
||
926 | { |
||
927 | if (!$member) { |
||
928 | $member = Security::getCurrentUser(); |
||
929 | } |
||
930 | |||
931 | if ($member && Permission::checkMember($member, "ADMIN")) { |
||
932 | return true; |
||
933 | } |
||
934 | |||
935 | if (is_string($perm) && method_exists($this, 'can' . ucfirst($perm))) { |
||
936 | $method = 'can' . ucfirst($perm); |
||
937 | return $this->$method($member); |
||
938 | } |
||
939 | |||
940 | $results = $this->extend('can', $member); |
||
941 | if ($results && is_array($results)) { |
||
942 | if (!min($results)) { |
||
943 | return false; |
||
944 | } |
||
945 | } |
||
946 | |||
947 | return ($member && Permission::checkMember($member, $perm)); |
||
948 | } |
||
949 | |||
950 | /** |
||
951 | * This function should return true if the current user can add children to this page. It can be overloaded to |
||
952 | * customise the security model for an application. |
||
953 | * |
||
954 | * Denies permission if any of the following conditions is true: |
||
955 | * - alternateCanAddChildren() on a extension returns false |
||
956 | * - canEdit() is not granted |
||
957 | * - There are no classes defined in {@link $allowed_children} |
||
958 | * |
||
959 | * @uses SiteTreeExtension->canAddChildren() |
||
960 | * @uses canEdit() |
||
961 | * @uses $allowed_children |
||
962 | * |
||
963 | * @param Member|int $member |
||
964 | * @return bool True if the current user can add children |
||
965 | */ |
||
966 | public function canAddChildren($member = null) |
||
967 | { |
||
968 | // Disable adding children to archived pages |
||
969 | if (!$this->isOnDraft()) { |
||
970 | return false; |
||
971 | } |
||
972 | |||
973 | if (!$member) { |
||
974 | $member = Security::getCurrentUser(); |
||
975 | } |
||
976 | |||
977 | // Standard mechanism for accepting permission changes from extensions |
||
978 | $extended = $this->extendedCan('canAddChildren', $member); |
||
979 | if ($extended !== null) { |
||
980 | return $extended; |
||
981 | } |
||
982 | |||
983 | // Default permissions |
||
984 | if ($member && Permission::checkMember($member, "ADMIN")) { |
||
985 | return true; |
||
986 | } |
||
987 | |||
988 | return $this->canEdit($member) && $this->stat('allowed_children') !== 'none'; |
||
989 | } |
||
990 | |||
991 | /** |
||
992 | * This function should return true if the current user can view this page. It can be overloaded to customise the |
||
993 | * security model for an application. |
||
994 | * |
||
995 | * Denies permission if any of the following conditions is true: |
||
996 | * - canView() on any extension returns false |
||
997 | * - "CanViewType" directive is set to "Inherit" and any parent page return false for canView() |
||
998 | * - "CanViewType" directive is set to "LoggedInUsers" and no user is logged in |
||
999 | * - "CanViewType" directive is set to "OnlyTheseUsers" and user is not in the given groups |
||
1000 | * |
||
1001 | * @uses DataExtension->canView() |
||
1002 | * @uses ViewerGroups() |
||
1003 | * |
||
1004 | * @param Member $member |
||
1005 | * @return bool True if the current user can view this page |
||
1006 | */ |
||
1007 | public function canView($member = null) |
||
1008 | { |
||
1009 | if (!$member) { |
||
1010 | $member = Security::getCurrentUser(); |
||
1011 | } |
||
1012 | |||
1013 | // Standard mechanism for accepting permission changes from extensions |
||
1014 | $extended = $this->extendedCan('canView', $member); |
||
1015 | if ($extended !== null) { |
||
1016 | return $extended; |
||
1017 | } |
||
1018 | |||
1019 | // admin override |
||
1020 | if ($member && Permission::checkMember($member, array("ADMIN", "SITETREE_VIEW_ALL"))) { |
||
1021 | return true; |
||
1022 | } |
||
1023 | |||
1024 | // Orphaned pages (in the current stage) are unavailable, except for admins via the CMS |
||
1025 | if ($this->isOrphaned()) { |
||
1026 | return false; |
||
1027 | } |
||
1028 | |||
1029 | // Note: getInheritedPermissions() is disused in this instance |
||
1030 | // to allow parent canView extensions to influence subpage canView() |
||
1031 | |||
1032 | // check for empty spec |
||
1033 | if (!$this->CanViewType || $this->CanViewType === InheritedPermissions::ANYONE) { |
||
1034 | return true; |
||
1035 | } |
||
1036 | |||
1037 | // check for inherit |
||
1038 | if ($this->CanViewType === InheritedPermissions::INHERIT) { |
||
1039 | if ($this->ParentID) { |
||
1040 | return $this->Parent()->canView($member); |
||
1041 | } else { |
||
1042 | return $this->getSiteConfig()->canViewPages($member); |
||
1043 | } |
||
1044 | } |
||
1045 | |||
1046 | // check for any logged-in users |
||
1047 | if ($this->CanViewType === InheritedPermissions::LOGGED_IN_USERS && $member && $member->ID) { |
||
1048 | return true; |
||
1049 | } |
||
1050 | |||
1051 | // check for specific groups |
||
1052 | if ($this->CanViewType === InheritedPermissions::ONLY_THESE_USERS |
||
1053 | && $member |
||
1054 | && $member->inGroups($this->ViewerGroups()) |
||
1055 | ) { |
||
1056 | return true; |
||
1057 | } |
||
1058 | |||
1059 | return false; |
||
1060 | } |
||
1061 | |||
1062 | /** |
||
1063 | * Check if this page can be published |
||
1064 | * |
||
1065 | * @param Member $member |
||
1066 | * @return bool |
||
1067 | */ |
||
1068 | View Code Duplication | public function canPublish($member = null) |
|
1069 | { |
||
1070 | if (!$member) { |
||
1071 | $member = Security::getCurrentUser(); |
||
1072 | } |
||
1073 | |||
1074 | // Check extension |
||
1075 | $extended = $this->extendedCan('canPublish', $member); |
||
1076 | if ($extended !== null) { |
||
1077 | return $extended; |
||
1078 | } |
||
1079 | |||
1080 | if (Permission::checkMember($member, "ADMIN")) { |
||
1081 | return true; |
||
1082 | } |
||
1083 | |||
1084 | // Default to relying on edit permission |
||
1085 | return $this->canEdit($member); |
||
1086 | } |
||
1087 | |||
1088 | /** |
||
1089 | * This function should return true if the current user can delete this page. It can be overloaded to customise the |
||
1090 | * security model for an application. |
||
1091 | * |
||
1092 | * Denies permission if any of the following conditions is true: |
||
1093 | * - canDelete() returns false on any extension |
||
1094 | * - canEdit() returns false |
||
1095 | * - any descendant page returns false for canDelete() |
||
1096 | * |
||
1097 | * @uses canDelete() |
||
1098 | * @uses SiteTreeExtension->canDelete() |
||
1099 | * @uses canEdit() |
||
1100 | * |
||
1101 | * @param Member $member |
||
1102 | * @return bool True if the current user can delete this page |
||
1103 | */ |
||
1104 | public function canDelete($member = null) |
||
1105 | { |
||
1106 | if (!$member) { |
||
1107 | $member = Security::getCurrentUser(); |
||
1108 | } |
||
1109 | |||
1110 | // Standard mechanism for accepting permission changes from extensions |
||
1111 | $extended = $this->extendedCan('canDelete', $member); |
||
1112 | if ($extended !== null) { |
||
1113 | return $extended; |
||
1114 | } |
||
1115 | |||
1116 | if (!$member) { |
||
1117 | return false; |
||
1118 | } |
||
1119 | |||
1120 | // Default permission check |
||
1121 | if (Permission::checkMember($member, array("ADMIN", "SITETREE_EDIT_ALL"))) { |
||
1122 | return true; |
||
1123 | } |
||
1124 | |||
1125 | // Check inherited permissions |
||
1126 | return static::getPermissionChecker() |
||
1127 | ->canDelete($this->ID, $member); |
||
1128 | } |
||
1129 | |||
1130 | /** |
||
1131 | * This function should return true if the current user can create new pages of this class, regardless of class. It |
||
1132 | * can be overloaded to customise the security model for an application. |
||
1133 | * |
||
1134 | * By default, permission to create at the root level is based on the SiteConfig configuration, and permission to |
||
1135 | * create beneath a parent is based on the ability to edit that parent page. |
||
1136 | * |
||
1137 | * Use {@link canAddChildren()} to control behaviour of creating children under this page. |
||
1138 | * |
||
1139 | * @uses $can_create |
||
1140 | * @uses DataExtension->canCreate() |
||
1141 | * |
||
1142 | * @param Member $member |
||
1143 | * @param array $context Optional array which may contain array('Parent' => $parentObj) |
||
1144 | * If a parent page is known, it will be checked for validity. |
||
1145 | * If omitted, it will be assumed this is to be created as a top level page. |
||
1146 | * @return bool True if the current user can create pages on this class. |
||
1147 | */ |
||
1148 | public function canCreate($member = null, $context = array()) |
||
1149 | { |
||
1150 | if (!$member) { |
||
1151 | $member = Security::getCurrentUser(); |
||
1152 | } |
||
1153 | |||
1154 | // Check parent (custom canCreate option for SiteTree) |
||
1155 | // Block children not allowed for this parent type |
||
1156 | $parent = isset($context['Parent']) ? $context['Parent'] : null; |
||
1157 | if ($parent && !in_array(static::class, $parent->allowedChildren())) { |
||
1158 | return false; |
||
1159 | } |
||
1160 | |||
1161 | // Standard mechanism for accepting permission changes from extensions |
||
1162 | $extended = $this->extendedCan(__FUNCTION__, $member, $context); |
||
1163 | if ($extended !== null) { |
||
1164 | return $extended; |
||
1165 | } |
||
1166 | |||
1167 | // Check permission |
||
1168 | if ($member && Permission::checkMember($member, "ADMIN")) { |
||
1169 | return true; |
||
1170 | } |
||
1171 | |||
1172 | // Fall over to inherited permissions |
||
1173 | if ($parent && $parent->exists()) { |
||
1174 | return $parent->canAddChildren($member); |
||
1175 | } else { |
||
1176 | // This doesn't necessarily mean we are creating a root page, but that |
||
1177 | // we don't know if there is a parent, so default to this permission |
||
1178 | return SiteConfig::current_site_config()->canCreateTopLevel($member); |
||
1179 | } |
||
1180 | } |
||
1181 | |||
1182 | /** |
||
1183 | * This function should return true if the current user can edit this page. It can be overloaded to customise the |
||
1184 | * security model for an application. |
||
1185 | * |
||
1186 | * Denies permission if any of the following conditions is true: |
||
1187 | * - canEdit() on any extension returns false |
||
1188 | * - canView() return false |
||
1189 | * - "CanEditType" directive is set to "Inherit" and any parent page return false for canEdit() |
||
1190 | * - "CanEditType" directive is set to "LoggedInUsers" and no user is logged in or doesn't have the |
||
1191 | * CMS_Access_CMSMAIN permission code |
||
1192 | * - "CanEditType" directive is set to "OnlyTheseUsers" and user is not in the given groups |
||
1193 | * |
||
1194 | * @uses canView() |
||
1195 | * @uses EditorGroups() |
||
1196 | * @uses DataExtension->canEdit() |
||
1197 | * |
||
1198 | * @param Member $member Set to false if you want to explicitly test permissions without a valid user (useful for |
||
1199 | * unit tests) |
||
1200 | * @return bool True if the current user can edit this page |
||
1201 | */ |
||
1202 | View Code Duplication | public function canEdit($member = null) |
|
1203 | { |
||
1204 | if (!$member) { |
||
1205 | $member = Security::getCurrentUser(); |
||
1206 | } |
||
1207 | |||
1208 | // Standard mechanism for accepting permission changes from extensions |
||
1209 | $extended = $this->extendedCan('canEdit', $member); |
||
1210 | if ($extended !== null) { |
||
1211 | return $extended; |
||
1212 | } |
||
1213 | |||
1214 | // Default permissions |
||
1215 | if (Permission::checkMember($member, "SITETREE_EDIT_ALL")) { |
||
1216 | return true; |
||
1217 | } |
||
1218 | |||
1219 | // Check inherited permissions |
||
1220 | return static::getPermissionChecker() |
||
1221 | ->canEdit($this->ID, $member); |
||
1222 | } |
||
1223 | |||
1224 | /** |
||
1225 | * Stub method to get the site config, unless the current class can provide an alternate. |
||
1226 | * |
||
1227 | * @return SiteConfig |
||
1228 | */ |
||
1229 | public function getSiteConfig() |
||
1230 | { |
||
1231 | $configs = $this->invokeWithExtensions('alternateSiteConfig'); |
||
1232 | foreach (array_filter($configs) as $config) { |
||
1233 | return $config; |
||
1234 | } |
||
1235 | |||
1236 | return SiteConfig::current_site_config(); |
||
1237 | } |
||
1238 | |||
1239 | /** |
||
1240 | * @return PermissionChecker |
||
1241 | */ |
||
1242 | public static function getPermissionChecker() |
||
1243 | { |
||
1244 | return Injector::inst()->get(PermissionChecker::class.'.sitetree'); |
||
1245 | } |
||
1246 | |||
1247 | /** |
||
1248 | * Collate selected descendants of this page. |
||
1249 | * |
||
1250 | * {@link $condition} will be evaluated on each descendant, and if it is succeeds, that item will be added to the |
||
1251 | * $collator array. |
||
1252 | * |
||
1253 | * @param string $condition The PHP condition to be evaluated. The page will be called $item |
||
1254 | * @param array $collator An array, passed by reference, to collect all of the matching descendants. |
||
1255 | * @return bool |
||
1256 | */ |
||
1257 | public function collateDescendants($condition, &$collator) |
||
1258 | { |
||
1259 | $children = $this->Children(); |
||
1260 | if ($children) { |
||
1261 | foreach ($children as $item) { |
||
1262 | if (eval("return $condition;")) { |
||
1263 | $collator[] = $item; |
||
1264 | } |
||
1265 | /** @var SiteTree $item */ |
||
1266 | $item->collateDescendants($condition, $collator); |
||
1267 | } |
||
1268 | return true; |
||
1269 | } |
||
1270 | return false; |
||
1271 | } |
||
1272 | |||
1273 | /** |
||
1274 | * Return the title, description, keywords and language metatags. |
||
1275 | * |
||
1276 | * @todo Move <title> tag in separate getter for easier customization and more obvious usage |
||
1277 | * |
||
1278 | * @param bool $includeTitle Show default <title>-tag, set to false for custom templating |
||
1279 | * @return string The XHTML metatags |
||
1280 | */ |
||
1281 | public function MetaTags($includeTitle = true) |
||
1282 | { |
||
1283 | $tags = array(); |
||
1284 | if ($includeTitle && strtolower($includeTitle) != 'false') { |
||
1285 | $tags[] = HTML::createTag('title', array(), $this->obj('Title')->forTemplate()); |
||
1286 | } |
||
1287 | |||
1288 | $generator = trim(Config::inst()->get(self::class, 'meta_generator')); |
||
1289 | if (!empty($generator)) { |
||
1290 | $tags[] = HTML::createTag('meta', array( |
||
1291 | 'name' => 'generator', |
||
1292 | 'content' => $generator, |
||
1293 | )); |
||
1294 | } |
||
1295 | |||
1296 | $charset = ContentNegotiator::config()->uninherited('encoding'); |
||
1297 | $tags[] = HTML::createTag('meta', array( |
||
1298 | 'http-equiv' => 'Content-Type', |
||
1299 | 'content' => 'text/html; charset=' . $charset, |
||
1300 | )); |
||
1301 | if ($this->MetaDescription) { |
||
1302 | $tags[] = HTML::createTag('meta', array( |
||
1303 | 'name' => 'description', |
||
1304 | 'content' => $this->MetaDescription, |
||
1305 | )); |
||
1306 | } |
||
1307 | |||
1308 | if (Permission::check('CMS_ACCESS_CMSMain') |
||
1309 | && $this->ID > 0 |
||
1310 | ) { |
||
1311 | $tags[] = HTML::createTag('meta', array( |
||
1312 | 'name' => 'x-page-id', |
||
1313 | 'content' => $this->obj('ID')->forTemplate(), |
||
1314 | )); |
||
1315 | $tags[] = HTML::createTag('meta', array( |
||
1316 | 'name' => 'x-cms-edit-link', |
||
1317 | 'content' => $this->obj('CMSEditLink')->forTemplate(), |
||
1318 | )); |
||
1319 | } |
||
1320 | |||
1321 | $tags = implode("\n", $tags); |
||
1322 | if ($this->ExtraMeta) { |
||
1323 | $tags .= $this->obj('ExtraMeta')->forTemplate(); |
||
1324 | } |
||
1325 | |||
1326 | $this->extend('MetaTags', $tags); |
||
1327 | |||
1328 | return $tags; |
||
1329 | } |
||
1330 | |||
1331 | /** |
||
1332 | * Returns the object that contains the content that a user would associate with this page. |
||
1333 | * |
||
1334 | * Ordinarily, this is just the page itself, but for example on RedirectorPages or VirtualPages ContentSource() will |
||
1335 | * return the page that is linked to. |
||
1336 | * |
||
1337 | * @return $this |
||
1338 | */ |
||
1339 | public function ContentSource() |
||
1340 | { |
||
1341 | return $this; |
||
1342 | } |
||
1343 | |||
1344 | /** |
||
1345 | * Add default records to database. |
||
1346 | * |
||
1347 | * This function is called whenever the database is built, after the database tables have all been created. Overload |
||
1348 | * this to add default records when the database is built, but make sure you call parent::requireDefaultRecords(). |
||
1349 | */ |
||
1350 | public function requireDefaultRecords() |
||
1351 | { |
||
1352 | parent::requireDefaultRecords(); |
||
1353 | |||
1354 | // default pages |
||
1355 | if (static::class == self::class && $this->config()->create_default_pages) { |
||
1356 | if (!SiteTree::get_by_link(RootURLController::config()->default_homepage_link)) { |
||
1357 | $homepage = new Page(); |
||
1358 | $homepage->Title = _t(__CLASS__.'.DEFAULTHOMETITLE', 'Home'); |
||
1359 | $homepage->Content = _t(__CLASS__.'.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>'); |
||
1360 | $homepage->URLSegment = RootURLController::config()->default_homepage_link; |
||
1361 | $homepage->Sort = 1; |
||
1362 | $homepage->write(); |
||
1363 | $homepage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
||
1364 | $homepage->flushCache(); |
||
1365 | DB::alteration_message('Home page created', 'created'); |
||
1366 | } |
||
1367 | |||
1368 | if (DB::query("SELECT COUNT(*) FROM \"SiteTree\"")->value() == 1) { |
||
1369 | $aboutus = new Page(); |
||
1370 | $aboutus->Title = _t(__CLASS__.'.DEFAULTABOUTTITLE', 'About Us'); |
||
1371 | $aboutus->Content = _t( |
||
1372 | 'SilverStripe\\CMS\\Model\\SiteTree.DEFAULTABOUTCONTENT', |
||
1373 | '<p>You can fill this page out with your own content, or delete it and create your own pages.</p>' |
||
1374 | ); |
||
1375 | $aboutus->Sort = 2; |
||
1376 | $aboutus->write(); |
||
1377 | $aboutus->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
||
1378 | $aboutus->flushCache(); |
||
1379 | DB::alteration_message('About Us page created', 'created'); |
||
1380 | |||
1381 | $contactus = new Page(); |
||
1382 | $contactus->Title = _t(__CLASS__.'.DEFAULTCONTACTTITLE', 'Contact Us'); |
||
1383 | $contactus->Content = _t( |
||
1384 | 'SilverStripe\\CMS\\Model\\SiteTree.DEFAULTCONTACTCONTENT', |
||
1385 | '<p>You can fill this page out with your own content, or delete it and create your own pages.</p>' |
||
1386 | ); |
||
1387 | $contactus->Sort = 3; |
||
1388 | $contactus->write(); |
||
1389 | $contactus->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
||
1390 | $contactus->flushCache(); |
||
1391 | DB::alteration_message('Contact Us page created', 'created'); |
||
1392 | } |
||
1393 | } |
||
1394 | } |
||
1395 | |||
1396 | protected function onBeforeWrite() |
||
1397 | { |
||
1398 | parent::onBeforeWrite(); |
||
1399 | |||
1400 | // If Sort hasn't been set, make this page come after it's siblings |
||
1401 | if (!$this->Sort) { |
||
1402 | $parentID = ($this->ParentID) ? $this->ParentID : 0; |
||
1403 | $this->Sort = DB::prepared_query( |
||
1404 | "SELECT MAX(\"Sort\") + 1 FROM \"SiteTree\" WHERE \"ParentID\" = ?", |
||
1405 | array($parentID) |
||
1406 | )->value(); |
||
1407 | } |
||
1408 | |||
1409 | // If there is no URLSegment set, generate one from Title |
||
1410 | $defaultSegment = $this->generateURLSegment(_t( |
||
1411 | 'SilverStripe\\CMS\\Controllers\\CMSMain.NEWPAGE', |
||
1412 | 'New {pagetype}', |
||
1413 | array('pagetype' => $this->i18n_singular_name()) |
||
1414 | )); |
||
1415 | if ((!$this->URLSegment || $this->URLSegment == $defaultSegment) && $this->Title) { |
||
1416 | $this->URLSegment = $this->generateURLSegment($this->Title); |
||
1417 | } elseif ($this->isChanged('URLSegment', 2)) { |
||
1418 | // Do a strict check on change level, to avoid double encoding caused by |
||
1419 | // bogus changes through forceChange() |
||
1420 | $filter = URLSegmentFilter::create(); |
||
1421 | $this->URLSegment = $filter->filter($this->URLSegment); |
||
1422 | // If after sanitising there is no URLSegment, give it a reasonable default |
||
1423 | if (!$this->URLSegment) { |
||
1424 | $this->URLSegment = "page-$this->ID"; |
||
1425 | } |
||
1426 | } |
||
1427 | |||
1428 | // Ensure that this object has a non-conflicting URLSegment value. |
||
1429 | $count = 2; |
||
1430 | while (!$this->validURLSegment()) { |
||
1431 | $this->URLSegment = preg_replace('/-[0-9]+$/', null, $this->URLSegment) . '-' . $count; |
||
1432 | $count++; |
||
1433 | } |
||
1434 | |||
1435 | $this->syncLinkTracking(); |
||
1436 | |||
1437 | // Check to see if we've only altered fields that shouldn't affect versioning |
||
1438 | $fieldsIgnoredByVersioning = array('HasBrokenLink', 'Status', 'HasBrokenFile', 'ToDo', 'VersionID', 'SaveCount'); |
||
1439 | $changedFields = array_keys($this->getChangedFields(true, 2)); |
||
1440 | |||
1441 | // This more rigorous check is inline with the test that write() does to decide whether or not to write to the |
||
1442 | // DB. We use that to avoid cluttering the system with a migrateVersion() call that doesn't get used |
||
1443 | $oneChangedFields = array_keys($this->getChangedFields(true, 1)); |
||
1444 | |||
1445 | if ($oneChangedFields && !array_diff($changedFields, $fieldsIgnoredByVersioning)) { |
||
1446 | // This will have the affect of preserving the versioning |
||
1447 | $this->migrateVersion($this->Version); |
||
1448 | } |
||
1449 | } |
||
1450 | |||
1451 | /** |
||
1452 | * Trigger synchronisation of link tracking |
||
1453 | * |
||
1454 | * {@see SiteTreeLinkTracking::augmentSyncLinkTracking} |
||
1455 | */ |
||
1456 | public function syncLinkTracking() |
||
1457 | { |
||
1458 | $this->extend('augmentSyncLinkTracking'); |
||
1459 | } |
||
1460 | |||
1461 | public function onBeforeDelete() |
||
1462 | { |
||
1463 | parent::onBeforeDelete(); |
||
1464 | |||
1465 | // If deleting this page, delete all its children. |
||
1466 | if (SiteTree::config()->enforce_strict_hierarchy && $children = $this->AllChildren()) { |
||
1467 | foreach ($children as $child) { |
||
1468 | /** @var SiteTree $child */ |
||
1469 | $child->delete(); |
||
1470 | } |
||
1471 | } |
||
1472 | } |
||
1473 | |||
1474 | public function onAfterDelete() |
||
1475 | { |
||
1476 | $this->updateDependentPages(); |
||
1477 | parent::onAfterDelete(); |
||
1478 | } |
||
1479 | |||
1480 | public function flushCache($persistent = true) |
||
1481 | { |
||
1482 | parent::flushCache($persistent); |
||
1483 | $this->_cache_statusFlags = null; |
||
1484 | } |
||
1485 | |||
1486 | public function validate() |
||
1487 | { |
||
1488 | $result = parent::validate(); |
||
1489 | |||
1490 | // Allowed children validation |
||
1491 | $parent = $this->getParent(); |
||
1492 | if ($parent && $parent->exists()) { |
||
1493 | // No need to check for subclasses or instanceof, as allowedChildren() already |
||
1494 | // deconstructs any inheritance trees already. |
||
1495 | $allowed = $parent->allowedChildren(); |
||
1496 | $subject = ($this instanceof VirtualPage && $this->CopyContentFromID) |
||
1497 | ? $this->CopyContentFrom() |
||
1498 | : $this; |
||
1499 | if (!in_array($subject->ClassName, $allowed)) { |
||
1500 | $result->addError( |
||
1501 | _t( |
||
1502 | 'SilverStripe\\CMS\\Model\\SiteTree.PageTypeNotAllowed', |
||
1503 | 'Page type "{type}" not allowed as child of this parent page', |
||
1504 | array('type' => $subject->i18n_singular_name()) |
||
1505 | ), |
||
1506 | ValidationResult::TYPE_ERROR, |
||
1507 | 'ALLOWED_CHILDREN' |
||
1508 | ); |
||
1509 | } |
||
1510 | } |
||
1511 | |||
1512 | // "Can be root" validation |
||
1513 | if (!$this->stat('can_be_root') && !$this->ParentID) { |
||
1514 | $result->addError( |
||
1515 | _t( |
||
1516 | 'SilverStripe\\CMS\\Model\\SiteTree.PageTypNotAllowedOnRoot', |
||
1517 | 'Page type "{type}" is not allowed on the root level', |
||
1518 | array('type' => $this->i18n_singular_name()) |
||
1519 | ), |
||
1520 | ValidationResult::TYPE_ERROR, |
||
1521 | 'CAN_BE_ROOT' |
||
1522 | ); |
||
1523 | } |
||
1524 | |||
1525 | return $result; |
||
1526 | } |
||
1527 | |||
1528 | /** |
||
1529 | * Returns true if this object has a URLSegment value that does not conflict with any other objects. This method |
||
1530 | * checks for: |
||
1531 | * - A page with the same URLSegment that has a conflict |
||
1532 | * - Conflicts with actions on the parent page |
||
1533 | * - A conflict caused by a root page having the same URLSegment as a class name |
||
1534 | * |
||
1535 | * @return bool |
||
1536 | */ |
||
1537 | public function validURLSegment() |
||
1538 | { |
||
1539 | if (self::config()->nested_urls && $parent = $this->Parent()) { |
||
1540 | if ($controller = ModelAsController::controller_for($parent)) { |
||
1541 | if ($controller instanceof Controller && $controller->hasAction($this->URLSegment)) { |
||
1542 | return false; |
||
1543 | } |
||
1544 | } |
||
1545 | } |
||
1546 | |||
1547 | if (!self::config()->nested_urls || !$this->ParentID) { |
||
1548 | if (class_exists($this->URLSegment) && is_subclass_of($this->URLSegment, RequestHandler::class)) { |
||
1549 | return false; |
||
1550 | } |
||
1551 | } |
||
1552 | |||
1553 | // Filters by url, id, and parent |
||
1554 | $filter = array('"SiteTree"."URLSegment"' => $this->URLSegment); |
||
1555 | if ($this->ID) { |
||
1556 | $filter['"SiteTree"."ID" <> ?'] = $this->ID; |
||
1557 | } |
||
1558 | if (self::config()->nested_urls) { |
||
1559 | $filter['"SiteTree"."ParentID"'] = $this->ParentID ? $this->ParentID : 0; |
||
1560 | } |
||
1561 | |||
1562 | // If any of the extensions return `0` consider the segment invalid |
||
1563 | $extensionResponses = array_filter( |
||
1564 | (array)$this->extend('augmentValidURLSegment'), |
||
1565 | function ($response) { |
||
1566 | return !is_null($response); |
||
1567 | } |
||
1568 | ); |
||
1569 | if ($extensionResponses) { |
||
1570 | return min($extensionResponses); |
||
1571 | } |
||
1572 | |||
1573 | // Check existence |
||
1574 | return !DataObject::get(self::class, $filter)->exists(); |
||
1575 | } |
||
1576 | |||
1577 | /** |
||
1578 | * Generate a URL segment based on the title provided. |
||
1579 | * |
||
1580 | * If {@link Extension}s wish to alter URL segment generation, they can do so by defining |
||
1581 | * updateURLSegment(&$url, $title). $url will be passed by reference and should be modified. $title will contain |
||
1582 | * the title that was originally used as the source of this generated URL. This lets extensions either start from |
||
1583 | * scratch, or incrementally modify the generated URL. |
||
1584 | * |
||
1585 | * @param string $title Page title |
||
1586 | * @return string Generated url segment |
||
1587 | */ |
||
1588 | public function generateURLSegment($title) |
||
1589 | { |
||
1590 | $filter = URLSegmentFilter::create(); |
||
1591 | $t = $filter->filter($title); |
||
1592 | |||
1593 | // Fallback to generic page name if path is empty (= no valid, convertable characters) |
||
1594 | if (!$t || $t == '-' || $t == '-1') { |
||
1595 | $t = "page-$this->ID"; |
||
1596 | } |
||
1597 | |||
1598 | // Hook for extensions |
||
1599 | $this->extend('updateURLSegment', $t, $title); |
||
1600 | |||
1601 | return $t; |
||
1602 | } |
||
1603 | |||
1604 | /** |
||
1605 | * Gets the URL segment for the latest draft version of this page. |
||
1606 | * |
||
1607 | * @return string |
||
1608 | */ |
||
1609 | public function getStageURLSegment() |
||
1610 | { |
||
1611 | $stageRecord = Versioned::get_one_by_stage(self::class, Versioned::DRAFT, array( |
||
1612 | '"SiteTree"."ID"' => $this->ID |
||
1613 | )); |
||
1614 | return ($stageRecord) ? $stageRecord->URLSegment : null; |
||
1615 | } |
||
1616 | |||
1617 | /** |
||
1618 | * Gets the URL segment for the currently published version of this page. |
||
1619 | * |
||
1620 | * @return string |
||
1621 | */ |
||
1622 | public function getLiveURLSegment() |
||
1623 | { |
||
1624 | $liveRecord = Versioned::get_one_by_stage(self::class, Versioned::LIVE, array( |
||
1625 | '"SiteTree"."ID"' => $this->ID |
||
1626 | )); |
||
1627 | return ($liveRecord) ? $liveRecord->URLSegment : null; |
||
1628 | } |
||
1629 | |||
1630 | /** |
||
1631 | * Returns the pages that depend on this page. This includes virtual pages, pages that link to it, etc. |
||
1632 | * |
||
1633 | * @param bool $includeVirtuals Set to false to exlcude virtual pages. |
||
1634 | * @return ArrayList |
||
1635 | */ |
||
1636 | public function DependentPages($includeVirtuals = true) |
||
1637 | { |
||
1638 | if (class_exists('Subsite')) { |
||
1639 | $origDisableSubsiteFilter = Subsite::$disable_subsite_filter; |
||
1640 | Subsite::disable_subsite_filter(true); |
||
1641 | } |
||
1642 | |||
1643 | // Content links |
||
1644 | $items = new ArrayList(); |
||
1645 | |||
1646 | // We merge all into a regular SS_List, because DataList doesn't support merge |
||
1647 | if ($contentLinks = $this->BackLinkTracking()) { |
||
1648 | $linkList = new ArrayList(); |
||
1649 | foreach ($contentLinks as $item) { |
||
1650 | $item->DependentLinkType = 'Content link'; |
||
1651 | $linkList->push($item); |
||
1652 | } |
||
1653 | $items->merge($linkList); |
||
1654 | } |
||
1655 | |||
1656 | // Virtual pages |
||
1657 | if ($includeVirtuals) { |
||
1658 | $virtuals = $this->VirtualPages(); |
||
1659 | if ($virtuals) { |
||
1660 | $virtualList = new ArrayList(); |
||
1661 | foreach ($virtuals as $item) { |
||
1662 | $item->DependentLinkType = 'Virtual page'; |
||
1663 | $virtualList->push($item); |
||
1664 | } |
||
1665 | $items->merge($virtualList); |
||
1666 | } |
||
1667 | } |
||
1668 | |||
1669 | // Redirector pages |
||
1670 | $redirectors = RedirectorPage::get()->where(array( |
||
1671 | '"RedirectorPage"."RedirectionType"' => 'Internal', |
||
1672 | '"RedirectorPage"."LinkToID"' => $this->ID |
||
1673 | )); |
||
1674 | if ($redirectors) { |
||
1675 | $redirectorList = new ArrayList(); |
||
1676 | foreach ($redirectors as $item) { |
||
1677 | $item->DependentLinkType = 'Redirector page'; |
||
1678 | $redirectorList->push($item); |
||
1679 | } |
||
1680 | $items->merge($redirectorList); |
||
1681 | } |
||
1682 | |||
1683 | if (class_exists('Subsite')) { |
||
1684 | Subsite::disable_subsite_filter($origDisableSubsiteFilter); |
||
1685 | } |
||
1686 | |||
1687 | return $items; |
||
1688 | } |
||
1689 | |||
1690 | /** |
||
1691 | * Return all virtual pages that link to this page. |
||
1692 | * |
||
1693 | * @return DataList |
||
1694 | */ |
||
1695 | public function VirtualPages() |
||
1696 | { |
||
1697 | $pages = parent::VirtualPages(); |
||
1698 | |||
1699 | // Disable subsite filter for these pages |
||
1700 | if ($pages instanceof DataList) { |
||
1701 | return $pages->setDataQueryParam('Subsite.filter', false); |
||
1702 | } else { |
||
1703 | return $pages; |
||
1704 | } |
||
1705 | } |
||
1706 | |||
1707 | /** |
||
1708 | * Returns a FieldList with which to create the main editing form. |
||
1709 | * |
||
1710 | * You can override this in your child classes to add extra fields - first get the parent fields using |
||
1711 | * parent::getCMSFields(), then use addFieldToTab() on the FieldList. |
||
1712 | * |
||
1713 | * See {@link getSettingsFields()} for a different set of fields concerned with configuration aspects on the record, |
||
1714 | * e.g. access control. |
||
1715 | * |
||
1716 | * @return FieldList The fields to be displayed in the CMS |
||
1717 | */ |
||
1718 | public function getCMSFields() |
||
1719 | { |
||
1720 | // Status / message |
||
1721 | // Create a status message for multiple parents |
||
1722 | if ($this->ID && is_numeric($this->ID)) { |
||
1723 | $linkedPages = $this->VirtualPages(); |
||
1724 | |||
1725 | $parentPageLinks = array(); |
||
1726 | |||
1727 | if ($linkedPages->count() > 0) { |
||
1728 | /** @var VirtualPage $linkedPage */ |
||
1729 | foreach ($linkedPages as $linkedPage) { |
||
1730 | $parentPage = $linkedPage->Parent(); |
||
1731 | if ($parentPage && $parentPage->exists()) { |
||
1732 | $link = Convert::raw2att($parentPage->CMSEditLink()); |
||
1733 | $title = Convert::raw2xml($parentPage->Title); |
||
1734 | } else { |
||
1735 | $link = CMSPageEditController::singleton()->Link('show'); |
||
1736 | $title = _t(__CLASS__.'.TOPLEVEL', 'Site Content (Top Level)'); |
||
1737 | } |
||
1738 | $parentPageLinks[] = "<a class=\"cmsEditlink\" href=\"{$link}\">{$title}</a>"; |
||
1739 | } |
||
1740 | |||
1741 | $lastParent = array_pop($parentPageLinks); |
||
1742 | $parentList = "'$lastParent'"; |
||
1743 | |||
1744 | if (count($parentPageLinks)) { |
||
1745 | $parentList = "'" . implode("', '", $parentPageLinks) . "' and " |
||
1746 | . $parentList; |
||
1747 | } |
||
1748 | |||
1749 | $statusMessage[] = _t( |
||
1750 | 'SilverStripe\\CMS\\Model\\SiteTree.APPEARSVIRTUALPAGES', |
||
1751 | "This content also appears on the virtual pages in the {title} sections.", |
||
1752 | array('title' => $parentList) |
||
1753 | ); |
||
1754 | } |
||
1755 | } |
||
1756 | |||
1757 | if ($this->HasBrokenLink || $this->HasBrokenFile) { |
||
1758 | $statusMessage[] = _t(__CLASS__.'.HASBROKENLINKS', "This page has broken links."); |
||
1759 | } |
||
1760 | |||
1761 | $dependentNote = ''; |
||
1762 | $dependentTable = new LiteralField('DependentNote', '<p></p>'); |
||
1763 | |||
1764 | // Create a table for showing pages linked to this one |
||
1765 | $dependentPages = $this->DependentPages(); |
||
1766 | $dependentPagesCount = $dependentPages->count(); |
||
1767 | if ($dependentPagesCount) { |
||
1768 | $dependentColumns = array( |
||
1769 | 'Title' => $this->fieldLabel('Title'), |
||
1770 | 'AbsoluteLink' => _t(__CLASS__.'.DependtPageColumnURL', 'URL'), |
||
1771 | 'DependentLinkType' => _t(__CLASS__.'.DependtPageColumnLinkType', 'Link type'), |
||
1772 | ); |
||
1773 | if (class_exists('Subsite')) { |
||
1774 | $dependentColumns['Subsite.Title'] = singleton('Subsite')->i18n_singular_name(); |
||
1775 | } |
||
1776 | |||
1777 | $dependentNote = new LiteralField('DependentNote', '<p>' . _t(__CLASS__.'.DEPENDENT_NOTE', 'The following pages depend on this page. This includes virtual pages, redirector pages, and pages with content links.') . '</p>'); |
||
1778 | $dependentTable = GridField::create( |
||
1779 | 'DependentPages', |
||
1780 | false, |
||
1781 | $dependentPages |
||
1782 | ); |
||
1783 | /** @var GridFieldDataColumns $dataColumns */ |
||
1784 | $dataColumns = $dependentTable->getConfig()->getComponentByType('SilverStripe\\Forms\\GridField\\GridFieldDataColumns'); |
||
1785 | $dataColumns |
||
1786 | ->setDisplayFields($dependentColumns) |
||
1787 | ->setFieldFormatting(array( |
||
1788 | 'Title' => function ($value, &$item) { |
||
1789 | return sprintf( |
||
1790 | '<a href="admin/pages/edit/show/%d">%s</a>', |
||
1791 | (int)$item->ID, |
||
1792 | Convert::raw2xml($item->Title) |
||
1793 | ); |
||
1794 | }, |
||
1795 | 'AbsoluteLink' => function ($value, &$item) { |
||
1796 | return sprintf( |
||
1797 | '<a href="%s" target="_blank">%s</a>', |
||
1798 | Convert::raw2xml($value), |
||
1799 | Convert::raw2xml($value) |
||
1800 | ); |
||
1801 | } |
||
1802 | )); |
||
1803 | } |
||
1804 | |||
1805 | $baseLink = Controller::join_links( |
||
1806 | Director::absoluteBaseURL(), |
||
1807 | (self::config()->nested_urls && $this->ParentID ? $this->Parent()->RelativeLink(true) : null) |
||
1808 | ); |
||
1809 | |||
1810 | $urlsegment = SiteTreeURLSegmentField::create("URLSegment", $this->fieldLabel('URLSegment')) |
||
1811 | ->setURLPrefix($baseLink) |
||
1812 | ->setDefaultURL($this->generateURLSegment(_t( |
||
1813 | 'SilverStripe\\CMS\\Controllers\\CMSMain.NEWPAGE', |
||
1814 | 'New {pagetype}', |
||
1815 | array('pagetype' => $this->i18n_singular_name()) |
||
1816 | ))); |
||
1817 | $helpText = (self::config()->nested_urls && $this->numChildren()) |
||
0 ignored issues
–
show
|
|||
1818 | ? $this->fieldLabel('LinkChangeNote') |
||
1819 | : ''; |
||
1820 | if (!Config::inst()->get('SilverStripe\\View\\Parsers\\URLSegmentFilter', 'default_allow_multibyte')) { |
||
1821 | $helpText .= _t('SilverStripe\\CMS\\Forms\\SiteTreeURLSegmentField.HelpChars', ' Special characters are automatically converted or removed.'); |
||
1822 | } |
||
1823 | $urlsegment->setHelpText($helpText); |
||
1824 | |||
1825 | $fields = new FieldList( |
||
1826 | $rootTab = new TabSet( |
||
1827 | "Root", |
||
1828 | $tabMain = new Tab( |
||
1829 | 'Main', |
||
1830 | new TextField("Title", $this->fieldLabel('Title')), |
||
1831 | $urlsegment, |
||
1832 | new TextField("MenuTitle", $this->fieldLabel('MenuTitle')), |
||
1833 | $htmlField = new HTMLEditorField("Content", _t(__CLASS__.'.HTMLEDITORTITLE', "Content", 'HTML editor title')), |
||
1834 | ToggleCompositeField::create( |
||
1835 | 'Metadata', |
||
1836 | _t(__CLASS__.'.MetadataToggle', 'Metadata'), |
||
1837 | array( |
||
1838 | $metaFieldDesc = new TextareaField("MetaDescription", $this->fieldLabel('MetaDescription')), |
||
1839 | $metaFieldExtra = new TextareaField("ExtraMeta", $this->fieldLabel('ExtraMeta')) |
||
1840 | ) |
||
1841 | )->setHeadingLevel(4) |
||
1842 | ), |
||
1843 | $tabDependent = new Tab( |
||
1844 | 'Dependent', |
||
1845 | $dependentNote, |
||
1846 | $dependentTable |
||
1847 | ) |
||
1848 | ) |
||
1849 | ); |
||
1850 | $htmlField->addExtraClass('stacked'); |
||
1851 | |||
1852 | // Help text for MetaData on page content editor |
||
1853 | $metaFieldDesc |
||
1854 | ->setRightTitle( |
||
1855 | _t( |
||
1856 | 'SilverStripe\\CMS\\Model\\SiteTree.METADESCHELP', |
||
1857 | "Search engines use this content for displaying search results (although it will not influence their ranking)." |
||
1858 | ) |
||
1859 | ) |
||
1860 | ->addExtraClass('help'); |
||
1861 | $metaFieldExtra |
||
1862 | ->setRightTitle( |
||
1863 | _t( |
||
1864 | 'SilverStripe\\CMS\\Model\\SiteTree.METAEXTRAHELP', |
||
1865 | "HTML tags for additional meta information. For example <meta name=\"customName\" content=\"your custom content here\" />" |
||
1866 | ) |
||
1867 | ) |
||
1868 | ->addExtraClass('help'); |
||
1869 | |||
1870 | // Conditional dependent pages tab |
||
1871 | if ($dependentPagesCount) { |
||
1872 | $tabDependent->setTitle(_t(__CLASS__.'.TABDEPENDENT', "Dependent pages") . " ($dependentPagesCount)"); |
||
1873 | } else { |
||
1874 | $fields->removeFieldFromTab('Root', 'Dependent'); |
||
1875 | } |
||
1876 | |||
1877 | $tabMain->setTitle(_t(__CLASS__.'.TABCONTENT', "Main Content")); |
||
1878 | |||
1879 | if ($this->ObsoleteClassName) { |
||
1880 | $obsoleteWarning = _t( |
||
1881 | 'SilverStripe\\CMS\\Model\\SiteTree.OBSOLETECLASS', |
||
1882 | "This page is of obsolete type {type}. Saving will reset its type and you may lose data", |
||
1883 | array('type' => $this->ObsoleteClassName) |
||
1884 | ); |
||
1885 | |||
1886 | $fields->addFieldToTab( |
||
1887 | "Root.Main", |
||
1888 | new LiteralField("ObsoleteWarningHeader", "<p class=\"message warning\">$obsoleteWarning</p>"), |
||
1889 | "Title" |
||
1890 | ); |
||
1891 | } |
||
1892 | |||
1893 | if (file_exists(BASE_PATH . '/install.php')) { |
||
1894 | $fields->addFieldToTab("Root.Main", new LiteralField( |
||
1895 | "InstallWarningHeader", |
||
1896 | "<p class=\"message warning\">" . _t( |
||
1897 | "SilverStripe\\CMS\\Model\\SiteTree.REMOVE_INSTALL_WARNING", |
||
1898 | "Warning: You should remove install.php from this SilverStripe install for security reasons." |
||
1899 | ) |
||
1900 | . "</p>" |
||
1901 | ), "Title"); |
||
1902 | } |
||
1903 | |||
1904 | if (self::$runCMSFieldsExtensions) { |
||
1905 | $this->extend('updateCMSFields', $fields); |
||
1906 | } |
||
1907 | |||
1908 | return $fields; |
||
1909 | } |
||
1910 | |||
1911 | |||
1912 | /** |
||
1913 | * Returns fields related to configuration aspects on this record, e.g. access control. See {@link getCMSFields()} |
||
1914 | * for content-related fields. |
||
1915 | * |
||
1916 | * @return FieldList |
||
1917 | */ |
||
1918 | public function getSettingsFields() |
||
1919 | { |
||
1920 | $mapFn = function ($groups = []) { |
||
1921 | $map = []; |
||
1922 | foreach ($groups as $group) { |
||
1923 | // Listboxfield values are escaped, use ASCII char instead of » |
||
1924 | $map[$group->ID] = $group->getBreadcrumbs(' > '); |
||
1925 | } |
||
1926 | asort($map); |
||
1927 | return $map; |
||
1928 | }; |
||
1929 | $groupsMap = $mapFn(Group::get()); |
||
1930 | $viewAllGroupsMap = $mapFn(Permission::get_groups_by_permission(['SITETREE_VIEW_ALL', 'ADMIN'])); |
||
1931 | $editAllGroupsMap = $mapFn(Permission::get_groups_by_permission(['SITETREE_EDIT_ALL', 'ADMIN'])); |
||
1932 | |||
1933 | $fields = new FieldList( |
||
1934 | $rootTab = new TabSet( |
||
1935 | "Root", |
||
1936 | $tabBehaviour = new Tab( |
||
1937 | 'Settings', |
||
1938 | new DropdownField( |
||
1939 | "ClassName", |
||
1940 | $this->fieldLabel('ClassName'), |
||
1941 | $this->getClassDropdown() |
||
1942 | ), |
||
1943 | $parentTypeSelector = new CompositeField( |
||
1944 | $parentType = new OptionsetField("ParentType", _t("SilverStripe\\CMS\\Model\\SiteTree.PAGELOCATION", "Page location"), array( |
||
1945 | "root" => _t("SilverStripe\\CMS\\Model\\SiteTree.PARENTTYPE_ROOT", "Top-level page"), |
||
1946 | "subpage" => _t("SilverStripe\\CMS\\Model\\SiteTree.PARENTTYPE_SUBPAGE", "Sub-page underneath a parent page"), |
||
1947 | )), |
||
1948 | $parentIDField = new TreeDropdownField("ParentID", $this->fieldLabel('ParentID'), self::class, 'ID', 'MenuTitle') |
||
1949 | ), |
||
1950 | $visibility = new FieldGroup( |
||
1951 | new CheckboxField("ShowInMenus", $this->fieldLabel('ShowInMenus')), |
||
1952 | new CheckboxField("ShowInSearch", $this->fieldLabel('ShowInSearch')) |
||
1953 | ), |
||
1954 | $viewersOptionsField = new OptionsetField( |
||
1955 | "CanViewType", |
||
1956 | _t(__CLASS__.'.ACCESSHEADER', "Who can view this page?") |
||
1957 | ), |
||
1958 | $viewerGroupsField = ListboxField::create("ViewerGroups", _t(__CLASS__.'.VIEWERGROUPS', "Viewer Groups")) |
||
1959 | ->setSource($groupsMap) |
||
1960 | ->setAttribute( |
||
1961 | 'data-placeholder', |
||
1962 | _t(__CLASS__.'.GroupPlaceholder', 'Click to select group') |
||
1963 | ), |
||
1964 | $editorsOptionsField = new OptionsetField( |
||
1965 | "CanEditType", |
||
1966 | _t(__CLASS__.'.EDITHEADER', "Who can edit this page?") |
||
1967 | ), |
||
1968 | $editorGroupsField = ListboxField::create("EditorGroups", _t(__CLASS__.'.EDITORGROUPS', "Editor Groups")) |
||
1969 | ->setSource($groupsMap) |
||
1970 | ->setAttribute( |
||
1971 | 'data-placeholder', |
||
1972 | _t(__CLASS__.'.GroupPlaceholder', 'Click to select group') |
||
1973 | ) |
||
1974 | ) |
||
1975 | ) |
||
1976 | ); |
||
1977 | |||
1978 | $parentType->addExtraClass('noborder'); |
||
1979 | $visibility->setTitle($this->fieldLabel('Visibility')); |
||
1980 | |||
1981 | |||
1982 | // This filter ensures that the ParentID dropdown selection does not show this node, |
||
1983 | // or its descendents, as this causes vanishing bugs |
||
1984 | $parentIDField->setFilterFunction(function ($node) { |
||
1985 | return $node->ID != $this->ID; |
||
1986 | }); |
||
1987 | $parentTypeSelector->addExtraClass('parentTypeSelector'); |
||
1988 | |||
1989 | $tabBehaviour->setTitle(_t(__CLASS__.'.TABBEHAVIOUR', "Behavior")); |
||
1990 | |||
1991 | // Make page location fields read-only if the user doesn't have the appropriate permission |
||
1992 | if (!Permission::check("SITETREE_REORGANISE")) { |
||
1993 | $fields->makeFieldReadonly('ParentType'); |
||
1994 | if ($this->getParentType() === 'root') { |
||
1995 | $fields->removeByName('ParentID'); |
||
1996 | } else { |
||
1997 | $fields->makeFieldReadonly('ParentID'); |
||
1998 | } |
||
1999 | } |
||
2000 | |||
2001 | $viewersOptionsSource = [ |
||
2002 | InheritedPermissions::INHERIT => _t(__CLASS__.'.INHERIT', "Inherit from parent page"), |
||
2003 | InheritedPermissions::ANYONE => _t(__CLASS__.'.ACCESSANYONE', "Anyone"), |
||
2004 | InheritedPermissions::LOGGED_IN_USERS => _t(__CLASS__.'.ACCESSLOGGEDIN', "Logged-in users"), |
||
2005 | InheritedPermissions::ONLY_THESE_USERS => _t( |
||
2006 | __CLASS__.'.ACCESSONLYTHESE', |
||
2007 | "Only these people (choose from list)" |
||
2008 | ), |
||
2009 | ]; |
||
2010 | $viewersOptionsField->setSource($viewersOptionsSource); |
||
2011 | |||
2012 | // Editors have same options, except no "Anyone" |
||
2013 | $editorsOptionsSource = $viewersOptionsSource; |
||
2014 | unset($editorsOptionsSource[InheritedPermissions::ANYONE]); |
||
2015 | $editorsOptionsField->setSource($editorsOptionsSource); |
||
2016 | |||
2017 | if ($viewAllGroupsMap) { |
||
2018 | $viewerGroupsField->setDescription(_t( |
||
2019 | 'SilverStripe\\CMS\\Model\\SiteTree.VIEWER_GROUPS_FIELD_DESC', |
||
2020 | 'Groups with global view permissions: {groupList}', |
||
2021 | ['groupList' => implode(', ', array_values($viewAllGroupsMap))] |
||
2022 | )); |
||
2023 | } |
||
2024 | |||
2025 | if ($editAllGroupsMap) { |
||
2026 | $editorGroupsField->setDescription(_t( |
||
2027 | 'SilverStripe\\CMS\\Model\\SiteTree.EDITOR_GROUPS_FIELD_DESC', |
||
2028 | 'Groups with global edit permissions: {groupList}', |
||
2029 | ['groupList' => implode(', ', array_values($editAllGroupsMap))] |
||
2030 | )); |
||
2031 | } |
||
2032 | |||
2033 | if (!Permission::check('SITETREE_GRANT_ACCESS')) { |
||
2034 | $fields->makeFieldReadonly($viewersOptionsField); |
||
2035 | if ($this->CanEditType === InheritedPermissions::ONLY_THESE_USERS) { |
||
2036 | $fields->makeFieldReadonly($viewerGroupsField); |
||
2037 | } else { |
||
2038 | $fields->removeByName('ViewerGroups'); |
||
2039 | } |
||
2040 | |||
2041 | $fields->makeFieldReadonly($editorsOptionsField); |
||
2042 | if ($this->CanEditType === InheritedPermissions::ONLY_THESE_USERS) { |
||
2043 | $fields->makeFieldReadonly($editorGroupsField); |
||
2044 | } else { |
||
2045 | $fields->removeByName('EditorGroups'); |
||
2046 | } |
||
2047 | } |
||
2048 | |||
2049 | if (self::$runCMSFieldsExtensions) { |
||
2050 | $this->extend('updateSettingsFields', $fields); |
||
2051 | } |
||
2052 | |||
2053 | return $fields; |
||
2054 | } |
||
2055 | |||
2056 | /** |
||
2057 | * @param bool $includerelations A boolean value to indicate if the labels returned should include relation fields |
||
2058 | * @return array |
||
2059 | */ |
||
2060 | public function fieldLabels($includerelations = true) |
||
2061 | { |
||
2062 | $cacheKey = static::class . '_' . $includerelations; |
||
2063 | if (!isset(self::$_cache_field_labels[$cacheKey])) { |
||
2064 | $labels = parent::fieldLabels($includerelations); |
||
2065 | $labels['Title'] = _t(__CLASS__.'.PAGETITLE', "Page name"); |
||
2066 | $labels['MenuTitle'] = _t(__CLASS__.'.MENUTITLE', "Navigation label"); |
||
2067 | $labels['MetaDescription'] = _t(__CLASS__.'.METADESC', "Meta Description"); |
||
2068 | $labels['ExtraMeta'] = _t(__CLASS__.'.METAEXTRA', "Custom Meta Tags"); |
||
2069 | $labels['ClassName'] = _t(__CLASS__.'.PAGETYPE', "Page type", 'Classname of a page object'); |
||
2070 | $labels['ParentType'] = _t(__CLASS__.'.PARENTTYPE', "Page location"); |
||
2071 | $labels['ParentID'] = _t(__CLASS__.'.PARENTID', "Parent page"); |
||
2072 | $labels['ShowInMenus'] =_t(__CLASS__.'.SHOWINMENUS', "Show in menus?"); |
||
2073 | $labels['ShowInSearch'] = _t(__CLASS__.'.SHOWINSEARCH', "Show in search?"); |
||
2074 | $labels['ViewerGroups'] = _t(__CLASS__.'.VIEWERGROUPS', "Viewer Groups"); |
||
2075 | $labels['EditorGroups'] = _t(__CLASS__.'.EDITORGROUPS', "Editor Groups"); |
||
2076 | $labels['URLSegment'] = _t(__CLASS__.'.URLSegment', 'URL Segment', 'URL for this page'); |
||
2077 | $labels['Content'] = _t(__CLASS__.'.Content', 'Content', 'Main HTML Content for a page'); |
||
2078 | $labels['CanViewType'] = _t(__CLASS__.'.Viewers', 'Viewers Groups'); |
||
2079 | $labels['CanEditType'] = _t(__CLASS__.'.Editors', 'Editors Groups'); |
||
2080 | $labels['Comments'] = _t(__CLASS__.'.Comments', 'Comments'); |
||
2081 | $labels['Visibility'] = _t(__CLASS__.'.Visibility', 'Visibility'); |
||
2082 | $labels['LinkChangeNote'] = _t( |
||
2083 | 'SilverStripe\\CMS\\Model\\SiteTree.LINKCHANGENOTE', |
||
2084 | 'Changing this page\'s link will also affect the links of all child pages.' |
||
2085 | ); |
||
2086 | |||
2087 | if ($includerelations) { |
||
2088 | $labels['Parent'] = _t(__CLASS__.'.has_one_Parent', 'Parent Page', 'The parent page in the site hierarchy'); |
||
2089 | $labels['LinkTracking'] = _t(__CLASS__.'.many_many_LinkTracking', 'Link Tracking'); |
||
2090 | $labels['ImageTracking'] = _t(__CLASS__.'.many_many_ImageTracking', 'Image Tracking'); |
||
2091 | $labels['BackLinkTracking'] = _t(__CLASS__.'.many_many_BackLinkTracking', 'Backlink Tracking'); |
||
2092 | } |
||
2093 | |||
2094 | self::$_cache_field_labels[$cacheKey] = $labels; |
||
2095 | } |
||
2096 | |||
2097 | return self::$_cache_field_labels[$cacheKey]; |
||
2098 | } |
||
2099 | |||
2100 | /** |
||
2101 | * Get the actions available in the CMS for this page - eg Save, Publish. |
||
2102 | * |
||
2103 | * Frontend scripts and styles know how to handle the following FormFields: |
||
2104 | * - top-level FormActions appear as standalone buttons |
||
2105 | * - top-level CompositeField with FormActions within appear as grouped buttons |
||
2106 | * - TabSet & Tabs appear as a drop ups |
||
2107 | * - FormActions within the Tab are restyled as links |
||
2108 | * - major actions can provide alternate states for richer presentation (see ssui.button widget extension) |
||
2109 | * |
||
2110 | * @return FieldList The available actions for this page. |
||
2111 | */ |
||
2112 | public function getCMSActions() |
||
2113 | { |
||
2114 | // Get status of page |
||
2115 | $isOnDraft = $this->isOnDraft(); |
||
2116 | $isPublished = $this->isPublished(); |
||
2117 | $stagesDiffer = $this->stagesDiffer(Versioned::DRAFT, Versioned::LIVE); |
||
2118 | |||
2119 | // Check permissions |
||
2120 | $canPublish = $this->canPublish(); |
||
2121 | $canUnpublish = $this->canUnpublish(); |
||
2122 | $canEdit = $this->canEdit(); |
||
2123 | |||
2124 | // Major actions appear as buttons immediately visible as page actions. |
||
2125 | $majorActions = CompositeField::create()->setName('MajorActions'); |
||
2126 | $majorActions->setFieldHolderTemplate(get_class($majorActions) . '_holder_buttongroup'); |
||
2127 | |||
2128 | // Minor options are hidden behind a drop-up and appear as links (although they are still FormActions). |
||
2129 | $rootTabSet = new TabSet('ActionMenus'); |
||
2130 | $moreOptions = new Tab( |
||
2131 | 'MoreOptions', |
||
2132 | _t(__CLASS__.'.MoreOptions', 'More options', 'Expands a view for more buttons') |
||
2133 | ); |
||
2134 | $moreOptions->addExtraClass('popover-actions-simulate'); |
||
2135 | $rootTabSet->push($moreOptions); |
||
2136 | $rootTabSet->addExtraClass('ss-ui-action-tabset action-menus noborder'); |
||
2137 | |||
2138 | // Render page information into the "more-options" drop-up, on the top. |
||
2139 | $liveRecord = Versioned::get_by_stage(self::class, Versioned::LIVE)->byID($this->ID); |
||
2140 | $infoTemplate = SSViewer::get_templates_by_class(static::class, '_Information', self::class); |
||
2141 | $moreOptions->push( |
||
2142 | new LiteralField( |
||
2143 | 'Information', |
||
2144 | $this->customise(array( |
||
2145 | 'Live' => $liveRecord, |
||
2146 | 'ExistsOnLive' => $isPublished |
||
2147 | ))->renderWith($infoTemplate) |
||
2148 | ) |
||
2149 | ); |
||
2150 | |||
2151 | // Add to campaign option if not-archived and has publish permission |
||
2152 | if (($isPublished || $isOnDraft) && $canPublish) { |
||
2153 | $moreOptions->push( |
||
2154 | AddToCampaignHandler_FormAction::create() |
||
2155 | ->removeExtraClass('btn-primary') |
||
2156 | ->addExtraClass('btn-secondary') |
||
2157 | ); |
||
2158 | } |
||
2159 | |||
2160 | // "readonly"/viewing version that isn't the current version of the record |
||
2161 | $stageRecord = Versioned::get_by_stage(static::class, Versioned::DRAFT)->byID($this->ID); |
||
2162 | /** @skipUpgrade */ |
||
2163 | if ($stageRecord && $stageRecord->Version != $this->Version) { |
||
2164 | $moreOptions->push(FormAction::create('email', _t('SilverStripe\\CMS\\Controllers\\CMSMain.EMAIL', 'Email'))); |
||
2165 | $moreOptions->push(FormAction::create('rollback', _t('SilverStripe\\CMS\\Controllers\\CMSMain.ROLLBACK', 'Roll back to this version'))); |
||
2166 | $actions = new FieldList(array($majorActions, $rootTabSet)); |
||
2167 | |||
2168 | // getCMSActions() can be extended with updateCMSActions() on a extension |
||
2169 | $this->extend('updateCMSActions', $actions); |
||
2170 | return $actions; |
||
2171 | } |
||
2172 | |||
2173 | // "unpublish" |
||
2174 | View Code Duplication | if ($isPublished && $canPublish && $isOnDraft && $canUnpublish) { |
|
2175 | $moreOptions->push( |
||
2176 | FormAction::create('unpublish', _t(__CLASS__.'.BUTTONUNPUBLISH', 'Unpublish'), 'delete') |
||
2177 | ->setDescription(_t(__CLASS__.'.BUTTONUNPUBLISHDESC', 'Remove this page from the published site')) |
||
2178 | ->addExtraClass('btn-secondary') |
||
2179 | ); |
||
2180 | } |
||
2181 | |||
2182 | // "rollback" |
||
2183 | View Code Duplication | if ($isOnDraft && $isPublished && $canEdit && $stagesDiffer) { |
|
2184 | $moreOptions->push( |
||
2185 | FormAction::create('rollback', _t(__CLASS__.'.BUTTONCANCELDRAFT', 'Cancel draft changes')) |
||
2186 | ->setDescription(_t( |
||
2187 | 'SilverStripe\\CMS\\Model\\SiteTree.BUTTONCANCELDRAFTDESC', |
||
2188 | 'Delete your draft and revert to the currently published page' |
||
2189 | )) |
||
2190 | ->addExtraClass('btn-secondary') |
||
2191 | ); |
||
2192 | } |
||
2193 | |||
2194 | // "restore" |
||
2195 | if ($canEdit && !$isOnDraft && $isPublished) { |
||
2196 | $majorActions->push(FormAction::create('revert', _t('SilverStripe\\CMS\\Controllers\\CMSMain.RESTORE', 'Restore'))); |
||
2197 | } |
||
2198 | |||
2199 | // Check if we can restore a deleted page |
||
2200 | // Note: It would be nice to have a canRestore() permission at some point |
||
2201 | if ($canEdit && !$isOnDraft && !$isPublished) { |
||
2202 | // Determine if we should force a restore to root (where once it was a subpage) |
||
2203 | $restoreToRoot = $this->isParentArchived(); |
||
2204 | |||
2205 | // "restore" |
||
2206 | $title = $restoreToRoot |
||
2207 | ? _t('SilverStripe\\CMS\\Controllers\\CMSMain.RESTORE_TO_ROOT', 'Restore draft at top level') |
||
2208 | : _t('SilverStripe\\CMS\\Controllers\\CMSMain.RESTORE', 'Restore draft'); |
||
2209 | $description = $restoreToRoot |
||
2210 | ? _t('SilverStripe\\CMS\\Controllers\\CMSMain.RESTORE_TO_ROOT_DESC', 'Restore the archived version to draft as a top level page') |
||
2211 | : _t('SilverStripe\\CMS\\Controllers\\CMSMain.RESTORE_DESC', 'Restore the archived version to draft'); |
||
2212 | $majorActions->push( |
||
2213 | FormAction::create('restore', $title) |
||
2214 | ->setDescription($description) |
||
2215 | ->setAttribute('data-to-root', $restoreToRoot) |
||
2216 | ->setAttribute('data-icon', 'decline') |
||
2217 | ); |
||
2218 | } |
||
2219 | |||
2220 | // If a page is on any stage it can be archived |
||
2221 | if (($isOnDraft || $isPublished) && $this->canArchive()) { |
||
2222 | $title = $isPublished |
||
2223 | ? _t('SilverStripe\\CMS\\Controllers\\CMSMain.UNPUBLISH_AND_ARCHIVE', 'Unpublish and archive') |
||
2224 | : _t('SilverStripe\\CMS\\Controllers\\CMSMain.ARCHIVE', 'Archive'); |
||
2225 | $moreOptions->push( |
||
2226 | FormAction::create('archive', $title) |
||
2227 | ->addExtraClass('delete btn btn-secondary') |
||
2228 | ->setDescription(_t( |
||
2229 | 'SilverStripe\\CMS\\Model\\SiteTree.BUTTONDELETEDESC', |
||
2230 | 'Remove from draft/live and send to archive' |
||
2231 | )) |
||
2232 | ); |
||
2233 | } |
||
2234 | |||
2235 | // "save", supports an alternate state that is still clickable, but notifies the user that the action is not needed. |
||
2236 | if ($canEdit && $isOnDraft) { |
||
2237 | $majorActions->push( |
||
2238 | FormAction::create('save', _t(__CLASS__.'.BUTTONSAVED', 'Saved')) |
||
2239 | ->addExtraClass('btn-secondary-outline font-icon-check-mark') |
||
2240 | ->setAttribute('data-btn-alternate', 'btn action btn-primary font-icon-save') |
||
2241 | ->setUseButtonTag(true) |
||
2242 | ->setAttribute('data-text-alternate', _t('SilverStripe\\CMS\\Controllers\\CMSMain.SAVEDRAFT', 'Save draft')) |
||
2243 | ); |
||
2244 | } |
||
2245 | |||
2246 | if ($canPublish && $isOnDraft) { |
||
2247 | // "publish", as with "save", it supports an alternate state to show when action is needed. |
||
2248 | $majorActions->push( |
||
2249 | $publish = FormAction::create('publish', _t(__CLASS__.'.BUTTONPUBLISHED', 'Published')) |
||
2250 | ->addExtraClass('btn-secondary-outline font-icon-check-mark') |
||
2251 | ->setAttribute('data-btn-alternate', 'btn action btn-primary font-icon-rocket') |
||
2252 | ->setUseButtonTag(true) |
||
2253 | ->setAttribute('data-text-alternate', _t(__CLASS__.'.BUTTONSAVEPUBLISH', 'Save & publish')) |
||
2254 | ); |
||
2255 | |||
2256 | // Set up the initial state of the button to reflect the state of the underlying SiteTree object. |
||
2257 | if ($stagesDiffer) { |
||
2258 | $publish->addExtraClass('btn-primary font-icon-rocket'); |
||
2259 | $publish->setTitle(_t(__CLASS__.'.BUTTONSAVEPUBLISH', 'Save & publish')); |
||
2260 | $publish->removeExtraClass('btn-secondary-outline font-icon-check-mark'); |
||
2261 | } |
||
2262 | } |
||
2263 | |||
2264 | $actions = new FieldList(array($majorActions, $rootTabSet)); |
||
2265 | |||
2266 | // Hook for extensions to add/remove actions. |
||
2267 | $this->extend('updateCMSActions', $actions); |
||
2268 | |||
2269 | return $actions; |
||
2270 | } |
||
2271 | |||
2272 | public function onAfterPublish() |
||
2273 | { |
||
2274 | // Force live sort order to match stage sort order |
||
2275 | DB::prepared_query( |
||
2276 | 'UPDATE "SiteTree_Live" |
||
2277 | SET "Sort" = (SELECT "SiteTree"."Sort" FROM "SiteTree" WHERE "SiteTree_Live"."ID" = "SiteTree"."ID") |
||
2278 | WHERE EXISTS (SELECT "SiteTree"."Sort" FROM "SiteTree" WHERE "SiteTree_Live"."ID" = "SiteTree"."ID") AND "ParentID" = ?', |
||
2279 | array($this->ParentID) |
||
2280 | ); |
||
2281 | } |
||
2282 | |||
2283 | /** |
||
2284 | * Update draft dependant pages |
||
2285 | */ |
||
2286 | public function onAfterRevertToLive() |
||
2287 | { |
||
2288 | // Use an alias to get the updates made by $this->publish |
||
2289 | /** @var SiteTree $stageSelf */ |
||
2290 | $stageSelf = Versioned::get_by_stage(self::class, Versioned::DRAFT)->byID($this->ID); |
||
2291 | $stageSelf->writeWithoutVersion(); |
||
2292 | |||
2293 | // Need to update pages linking to this one as no longer broken |
||
2294 | foreach ($stageSelf->DependentPages() as $page) { |
||
2295 | /** @var SiteTree $page */ |
||
2296 | $page->writeWithoutVersion(); |
||
2297 | } |
||
2298 | } |
||
2299 | |||
2300 | /** |
||
2301 | * Determine if this page references a parent which is archived, and not available in stage |
||
2302 | * |
||
2303 | * @return bool True if there is an archived parent |
||
2304 | */ |
||
2305 | protected function isParentArchived() |
||
2306 | { |
||
2307 | if ($parentID = $this->ParentID) { |
||
2308 | /** @var SiteTree $parentPage */ |
||
2309 | $parentPage = Versioned::get_latest_version(self::class, $parentID); |
||
2310 | if (!$parentPage || !$parentPage->isOnDraft()) { |
||
2311 | return true; |
||
2312 | } |
||
2313 | } |
||
2314 | return false; |
||
2315 | } |
||
2316 | |||
2317 | /** |
||
2318 | * Restore the content in the active copy of this SiteTree page to the stage site. |
||
2319 | * |
||
2320 | * @return self |
||
2321 | */ |
||
2322 | public function doRestoreToStage() |
||
2323 | { |
||
2324 | $this->invokeWithExtensions('onBeforeRestoreToStage', $this); |
||
2325 | |||
2326 | // Ensure that the parent page is restored, otherwise restore to root |
||
2327 | if ($this->isParentArchived()) { |
||
2328 | $this->ParentID = 0; |
||
2329 | } |
||
2330 | |||
2331 | // if no record can be found on draft stage (meaning it has been "deleted from draft" before), |
||
2332 | // create an empty record |
||
2333 | if (!DB::prepared_query("SELECT \"ID\" FROM \"SiteTree\" WHERE \"ID\" = ?", array($this->ID))->value()) { |
||
2334 | $conn = DB::get_conn(); |
||
2335 | if (method_exists($conn, 'allowPrimaryKeyEditing')) { |
||
2336 | $conn->allowPrimaryKeyEditing(self::class, true); |
||
2337 | } |
||
2338 | DB::prepared_query("INSERT INTO \"SiteTree\" (\"ID\") VALUES (?)", array($this->ID)); |
||
2339 | if (method_exists($conn, 'allowPrimaryKeyEditing')) { |
||
2340 | $conn->allowPrimaryKeyEditing(self::class, false); |
||
2341 | } |
||
2342 | } |
||
2343 | |||
2344 | $oldReadingMode = Versioned::get_reading_mode(); |
||
2345 | Versioned::set_stage(Versioned::DRAFT); |
||
2346 | $this->forceChange(); |
||
2347 | $this->write(); |
||
2348 | |||
2349 | /** @var SiteTree $result */ |
||
2350 | $result = DataObject::get_by_id(self::class, $this->ID); |
||
2351 | |||
2352 | Versioned::set_reading_mode($oldReadingMode); |
||
2353 | |||
2354 | // Need to update pages linking to this one as no longer broken |
||
2355 | $this->updateDependentPages(); |
||
2356 | |||
2357 | $this->invokeWithExtensions('onAfterRestoreToStage', $this); |
||
2358 | |||
2359 | return $result; |
||
2360 | } |
||
2361 | |||
2362 | /** |
||
2363 | * Check if this page is new - that is, if it has yet to have been written to the database. |
||
2364 | * |
||
2365 | * @return bool |
||
2366 | */ |
||
2367 | public function isNew() |
||
2368 | { |
||
2369 | /** |
||
2370 | * This check was a problem for a self-hosted site, and may indicate a bug in the interpreter on their server, |
||
2371 | * or a bug here. Changing the condition from empty($this->ID) to !$this->ID && !$this->record['ID'] fixed this. |
||
2372 | */ |
||
2373 | if (empty($this->ID)) { |
||
2374 | return true; |
||
2375 | } |
||
2376 | |||
2377 | if (is_numeric($this->ID)) { |
||
2378 | return false; |
||
2379 | } |
||
2380 | |||
2381 | return stripos($this->ID, 'new') === 0; |
||
2382 | } |
||
2383 | |||
2384 | /** |
||
2385 | * Get the class dropdown used in the CMS to change the class of a page. This returns the list of options in the |
||
2386 | * dropdown as a Map from class name to singular name. Filters by {@link SiteTree->canCreate()}, as well as |
||
2387 | * {@link SiteTree::$needs_permission}. |
||
2388 | * |
||
2389 | * @return array |
||
2390 | */ |
||
2391 | protected function getClassDropdown() |
||
2392 | { |
||
2393 | $classes = self::page_type_classes(); |
||
2394 | $currentClass = null; |
||
2395 | |||
2396 | $result = array(); |
||
2397 | foreach ($classes as $class) { |
||
2398 | $instance = singleton($class); |
||
2399 | |||
2400 | // if the current page type is this the same as the class type always show the page type in the list |
||
2401 | if ($this->ClassName != $instance->ClassName) { |
||
2402 | if ($instance instanceof HiddenClass) { |
||
2403 | continue; |
||
2404 | } |
||
2405 | if (!$instance->canCreate(null, array('Parent' => $this->ParentID ? $this->Parent() : null))) { |
||
2406 | continue; |
||
2407 | } |
||
2408 | } |
||
2409 | |||
2410 | if ($perms = $instance->stat('need_permission')) { |
||
2411 | if (!$this->can($perms)) { |
||
2412 | continue; |
||
2413 | } |
||
2414 | } |
||
2415 | |||
2416 | $pageTypeName = $instance->i18n_singular_name(); |
||
2417 | |||
2418 | $currentClass = $class; |
||
2419 | $result[$class] = $pageTypeName; |
||
2420 | |||
2421 | // If we're in translation mode, the link between the translated pagetype title and the actual classname |
||
2422 | // might not be obvious, so we add it in parantheses. Example: class "RedirectorPage" has the title |
||
2423 | // "Weiterleitung" in German, so it shows up as "Weiterleitung (RedirectorPage)" |
||
2424 | if (i18n::getData()->langFromLocale(i18n::get_locale()) != 'en') { |
||
2425 | $result[$class] = $result[$class] . " ({$class})"; |
||
2426 | } |
||
2427 | } |
||
2428 | |||
2429 | // sort alphabetically, and put current on top |
||
2430 | asort($result); |
||
2431 | if ($currentClass) { |
||
2432 | $currentPageTypeName = $result[$currentClass]; |
||
2433 | unset($result[$currentClass]); |
||
2434 | $result = array_reverse($result); |
||
2435 | $result[$currentClass] = $currentPageTypeName; |
||
2436 | $result = array_reverse($result); |
||
2437 | } |
||
2438 | |||
2439 | return $result; |
||
2440 | } |
||
2441 | |||
2442 | /** |
||
2443 | * Returns an array of the class names of classes that are allowed to be children of this class. |
||
2444 | * |
||
2445 | * @return string[] |
||
2446 | */ |
||
2447 | public function allowedChildren() |
||
2448 | { |
||
2449 | // Get config based on old FIRST_SET rules |
||
2450 | $candidates = null; |
||
2451 | $class = get_class($this); |
||
2452 | while ($class) { |
||
2453 | if (Config::inst()->exists($class, 'allowed_children', Config::UNINHERITED)) { |
||
2454 | $candidates = Config::inst()->get($class, 'allowed_children', Config::UNINHERITED); |
||
2455 | break; |
||
2456 | } |
||
2457 | $class = get_parent_class($class); |
||
2458 | } |
||
2459 | if (!$candidates || $candidates === 'none' || $candidates === 'SiteTree_root') { |
||
2460 | return []; |
||
2461 | } |
||
2462 | |||
2463 | // Parse candidate list |
||
2464 | $allowedChildren = []; |
||
2465 | foreach ($candidates as $candidate) { |
||
2466 | // If a classname is prefixed by "*", such as "*Page", then only that class is allowed - no subclasses. |
||
2467 | // Otherwise, the class and all its subclasses are allowed. |
||
2468 | if (substr($candidate, 0, 1) == '*') { |
||
2469 | $allowedChildren[] = substr($candidate, 1); |
||
2470 | } elseif ($subclasses = ClassInfo::subclassesFor($candidate)) { |
||
2471 | foreach ($subclasses as $subclass) { |
||
2472 | if ($subclass == 'SiteTree_root' || singleton($subclass) instanceof HiddenClass) { |
||
2473 | continue; |
||
2474 | } |
||
2475 | $allowedChildren[] = $subclass; |
||
2476 | } |
||
2477 | } |
||
2478 | } |
||
2479 | return $allowedChildren; |
||
2480 | } |
||
2481 | |||
2482 | /** |
||
2483 | * Returns the class name of the default class for children of this page. |
||
2484 | * |
||
2485 | * @return string |
||
2486 | */ |
||
2487 | public function defaultChild() |
||
2488 | { |
||
2489 | $default = $this->stat('default_child'); |
||
2490 | $allowed = $this->allowedChildren(); |
||
2491 | if ($allowed) { |
||
2492 | if (!$default || !in_array($default, $allowed)) { |
||
2493 | $default = reset($allowed); |
||
2494 | } |
||
2495 | return $default; |
||
2496 | } |
||
2497 | return null; |
||
2498 | } |
||
2499 | |||
2500 | /** |
||
2501 | * Returns the class name of the default class for the parent of this page. |
||
2502 | * |
||
2503 | * @return string |
||
2504 | */ |
||
2505 | public function defaultParent() |
||
2506 | { |
||
2507 | return $this->stat('default_parent'); |
||
2508 | } |
||
2509 | |||
2510 | /** |
||
2511 | * Get the title for use in menus for this page. If the MenuTitle field is set it returns that, else it returns the |
||
2512 | * Title field. |
||
2513 | * |
||
2514 | * @return string |
||
2515 | */ |
||
2516 | public function getMenuTitle() |
||
2517 | { |
||
2518 | if ($value = $this->getField("MenuTitle")) { |
||
2519 | return $value; |
||
2520 | } else { |
||
2521 | return $this->getField("Title"); |
||
2522 | } |
||
2523 | } |
||
2524 | |||
2525 | |||
2526 | /** |
||
2527 | * Set the menu title for this page. |
||
2528 | * |
||
2529 | * @param string $value |
||
2530 | */ |
||
2531 | public function setMenuTitle($value) |
||
2532 | { |
||
2533 | if ($value == $this->getField("Title")) { |
||
2534 | $this->setField("MenuTitle", null); |
||
2535 | } else { |
||
2536 | $this->setField("MenuTitle", $value); |
||
2537 | } |
||
2538 | } |
||
2539 | |||
2540 | /** |
||
2541 | * A flag provides the user with additional data about the current page status, for example a "removed from draft" |
||
2542 | * status. Each page can have more than one status flag. Returns a map of a unique key to a (localized) title for |
||
2543 | * the flag. The unique key can be reused as a CSS class. Use the 'updateStatusFlags' extension point to customize |
||
2544 | * the flags. |
||
2545 | * |
||
2546 | * Example (simple): |
||
2547 | * "deletedonlive" => "Deleted" |
||
2548 | * |
||
2549 | * Example (with optional title attribute): |
||
2550 | * "deletedonlive" => array('text' => "Deleted", 'title' => 'This page has been deleted') |
||
2551 | * |
||
2552 | * @param bool $cached Whether to serve the fields from cache; false regenerate them |
||
2553 | * @return array |
||
2554 | */ |
||
2555 | public function getStatusFlags($cached = true) |
||
2556 | { |
||
2557 | if (!$this->_cache_statusFlags || !$cached) { |
||
2558 | $flags = array(); |
||
2559 | if ($this->isOnLiveOnly()) { |
||
2560 | $flags['removedfromdraft'] = array( |
||
2561 | 'text' => _t(__CLASS__.'.ONLIVEONLYSHORT', 'On live only'), |
||
2562 | 'title' => _t(__CLASS__.'.ONLIVEONLYSHORTHELP', 'Page is published, but has been deleted from draft'), |
||
2563 | ); |
||
2564 | } elseif ($this->isArchived()) { |
||
2565 | $flags['archived'] = array( |
||
2566 | 'text' => _t(__CLASS__.'.ARCHIVEDPAGESHORT', 'Archived'), |
||
2567 | 'title' => _t(__CLASS__.'.ARCHIVEDPAGEHELP', 'Page is removed from draft and live'), |
||
2568 | ); |
||
2569 | } elseif ($this->isOnDraftOnly()) { |
||
2570 | $flags['addedtodraft'] = array( |
||
2571 | 'text' => _t(__CLASS__.'.ADDEDTODRAFTSHORT', 'Draft'), |
||
2572 | 'title' => _t(__CLASS__.'.ADDEDTODRAFTHELP', "Page has not been published yet") |
||
2573 | ); |
||
2574 | } elseif ($this->isModifiedOnDraft()) { |
||
2575 | $flags['modified'] = array( |
||
2576 | 'text' => _t(__CLASS__.'.MODIFIEDONDRAFTSHORT', 'Modified'), |
||
2577 | 'title' => _t(__CLASS__.'.MODIFIEDONDRAFTHELP', 'Page has unpublished changes'), |
||
2578 | ); |
||
2579 | } |
||
2580 | |||
2581 | $this->extend('updateStatusFlags', $flags); |
||
2582 | |||
2583 | $this->_cache_statusFlags = $flags; |
||
2584 | } |
||
2585 | |||
2586 | return $this->_cache_statusFlags; |
||
2587 | } |
||
2588 | |||
2589 | /** |
||
2590 | * getTreeTitle will return three <span> html DOM elements, an empty <span> with the class 'jstree-pageicon' in |
||
2591 | * front, following by a <span> wrapping around its MenutTitle, then following by a <span> indicating its |
||
2592 | * publication status. |
||
2593 | * |
||
2594 | * @return string An HTML string ready to be directly used in a template |
||
2595 | */ |
||
2596 | public function getTreeTitle() |
||
2597 | { |
||
2598 | // Build the list of candidate children |
||
2599 | $children = array(); |
||
2600 | $candidates = static::page_type_classes(); |
||
2601 | foreach ($this->allowedChildren() as $childClass) { |
||
2602 | if (!in_array($childClass, $candidates)) { |
||
2603 | continue; |
||
2604 | } |
||
2605 | $child = singleton($childClass); |
||
2606 | if ($child->canCreate(null, array('Parent' => $this))) { |
||
2607 | $children[$childClass] = $child->i18n_singular_name(); |
||
2608 | } |
||
2609 | } |
||
2610 | $flags = $this->getStatusFlags(); |
||
2611 | $treeTitle = sprintf( |
||
2612 | "<span class=\"jstree-pageicon\"></span><span class=\"item\" data-allowedchildren=\"%s\">%s</span>", |
||
2613 | Convert::raw2att(Convert::raw2json($children)), |
||
2614 | Convert::raw2xml(str_replace(array("\n","\r"), "", $this->MenuTitle)) |
||
2615 | ); |
||
2616 | foreach ($flags as $class => $data) { |
||
2617 | if (is_string($data)) { |
||
2618 | $data = array('text' => $data); |
||
2619 | } |
||
2620 | $treeTitle .= sprintf( |
||
2621 | "<span class=\"badge %s\"%s>%s</span>", |
||
2622 | 'status-' . Convert::raw2xml($class), |
||
2623 | (isset($data['title'])) ? sprintf(' title="%s"', Convert::raw2xml($data['title'])) : '', |
||
2624 | Convert::raw2xml($data['text']) |
||
2625 | ); |
||
2626 | } |
||
2627 | |||
2628 | return $treeTitle; |
||
2629 | } |
||
2630 | |||
2631 | /** |
||
2632 | * Returns the page in the current page stack of the given level. Level(1) will return the main menu item that |
||
2633 | * we're currently inside, etc. |
||
2634 | * |
||
2635 | * @param int $level |
||
2636 | * @return SiteTree |
||
2637 | */ |
||
2638 | public function Level($level) |
||
2639 | { |
||
2640 | $parent = $this; |
||
2641 | $stack = array($parent); |
||
2642 | while (($parent = $parent->Parent()) && $parent->exists()) { |
||
2643 | array_unshift($stack, $parent); |
||
2644 | } |
||
2645 | |||
2646 | return isset($stack[$level-1]) ? $stack[$level-1] : null; |
||
2647 | } |
||
2648 | |||
2649 | /** |
||
2650 | * Gets the depth of this page in the sitetree, where 1 is the root level |
||
2651 | * |
||
2652 | * @return int |
||
2653 | */ |
||
2654 | public function getPageLevel() |
||
2655 | { |
||
2656 | if ($this->ParentID) { |
||
2657 | return 1 + $this->Parent()->getPageLevel(); |
||
2658 | } |
||
2659 | return 1; |
||
2660 | } |
||
2661 | |||
2662 | /** |
||
2663 | * Find the controller name by our convention of {$ModelClass}Controller |
||
2664 | * |
||
2665 | * @return string |
||
2666 | */ |
||
2667 | public function getControllerName() |
||
2668 | { |
||
2669 | //default controller for SiteTree objects |
||
2670 | $controller = ContentController::class; |
||
2671 | |||
2672 | //go through the ancestry for this class looking for |
||
2673 | $ancestry = ClassInfo::ancestry(static::class); |
||
2674 | // loop over the array going from the deepest descendant (ie: the current class) to SiteTree |
||
2675 | while ($class = array_pop($ancestry)) { |
||
2676 | //we don't need to go any deeper than the SiteTree class |
||
2677 | if ($class == SiteTree::class) { |
||
2678 | break; |
||
2679 | } |
||
2680 | // If we have a class of "{$ClassName}Controller" then we found our controller |
||
2681 | if (class_exists($candidate = sprintf('%sController', $class))) { |
||
2682 | $controller = $candidate; |
||
2683 | break; |
||
2684 | } elseif (class_exists($candidate = sprintf('%s_Controller', $class))) { |
||
2685 | // Support the legacy underscored filename, but raise a deprecation notice |
||
2686 | Deprecation::notice( |
||
2687 | '5.0', |
||
2688 | 'Underscored controller class names are deprecated. Use "MyController" instead of "My_Controller".', |
||
2689 | Deprecation::SCOPE_GLOBAL |
||
2690 | ); |
||
2691 | $controller = $candidate; |
||
2692 | break; |
||
2693 | } |
||
2694 | } |
||
2695 | |||
2696 | return $controller; |
||
2697 | } |
||
2698 | |||
2699 | /** |
||
2700 | * Return the CSS classes to apply to this node in the CMS tree. |
||
2701 | * |
||
2702 | * @return string |
||
2703 | */ |
||
2704 | public function CMSTreeClasses() |
||
2705 | { |
||
2706 | $classes = sprintf('class-%s', static::class); |
||
2707 | if ($this->HasBrokenFile || $this->HasBrokenLink) { |
||
2708 | $classes .= " BrokenLink"; |
||
2709 | } |
||
2710 | |||
2711 | if (!$this->canAddChildren()) { |
||
2712 | $classes .= " nochildren"; |
||
2713 | } |
||
2714 | |||
2715 | if (!$this->canEdit() && !$this->canAddChildren()) { |
||
2716 | if (!$this->canView()) { |
||
2717 | $classes .= " disabled"; |
||
2718 | } else { |
||
2719 | $classes .= " edit-disabled"; |
||
2720 | } |
||
2721 | } |
||
2722 | |||
2723 | if (!$this->ShowInMenus) { |
||
2724 | $classes .= " notinmenu"; |
||
2725 | } |
||
2726 | |||
2727 | return $classes; |
||
2728 | } |
||
2729 | |||
2730 | /** |
||
2731 | * Stops extendCMSFields() being called on getCMSFields(). This is useful when you need access to fields added by |
||
2732 | * subclasses of SiteTree in a extension. Call before calling parent::getCMSFields(), and reenable afterwards. |
||
2733 | */ |
||
2734 | public static function disableCMSFieldsExtensions() |
||
2735 | { |
||
2736 | self::$runCMSFieldsExtensions = false; |
||
2737 | } |
||
2738 | |||
2739 | /** |
||
2740 | * Reenables extendCMSFields() being called on getCMSFields() after it has been disabled by |
||
2741 | * disableCMSFieldsExtensions(). |
||
2742 | */ |
||
2743 | public static function enableCMSFieldsExtensions() |
||
2744 | { |
||
2745 | self::$runCMSFieldsExtensions = true; |
||
2746 | } |
||
2747 | |||
2748 | public function providePermissions() |
||
2749 | { |
||
2750 | return array( |
||
2751 | 'SITETREE_GRANT_ACCESS' => array( |
||
2752 | 'name' => _t(__CLASS__.'.PERMISSION_GRANTACCESS_DESCRIPTION', 'Manage access rights for content'), |
||
2753 | 'help' => _t(__CLASS__.'.PERMISSION_GRANTACCESS_HELP', 'Allow setting of page-specific access restrictions in the "Pages" section.'), |
||
2754 | 'category' => _t('SilverStripe\\Security\\Permission.PERMISSIONS_CATEGORY', 'Roles and access permissions'), |
||
2755 | 'sort' => 100 |
||
2756 | ), |
||
2757 | 'SITETREE_VIEW_ALL' => array( |
||
2758 | 'name' => _t(__CLASS__.'.VIEW_ALL_DESCRIPTION', 'View any page'), |
||
2759 | 'category' => _t('SilverStripe\\Security\\Permission.CONTENT_CATEGORY', 'Content permissions'), |
||
2760 | 'sort' => -100, |
||
2761 | 'help' => _t(__CLASS__.'.VIEW_ALL_HELP', 'Ability to view any page on the site, regardless of the settings on the Access tab. Requires the "Access to \'Pages\' section" permission') |
||
2762 | ), |
||
2763 | 'SITETREE_EDIT_ALL' => array( |
||
2764 | 'name' => _t(__CLASS__.'.EDIT_ALL_DESCRIPTION', 'Edit any page'), |
||
2765 | 'category' => _t('SilverStripe\\Security\\Permission.CONTENT_CATEGORY', 'Content permissions'), |
||
2766 | 'sort' => -50, |
||
2767 | 'help' => _t(__CLASS__.'.EDIT_ALL_HELP', 'Ability to edit any page on the site, regardless of the settings on the Access tab. Requires the "Access to \'Pages\' section" permission') |
||
2768 | ), |
||
2769 | 'SITETREE_REORGANISE' => array( |
||
2770 | 'name' => _t(__CLASS__.'.REORGANISE_DESCRIPTION', 'Change site structure'), |
||
2771 | 'category' => _t('SilverStripe\\Security\\Permission.CONTENT_CATEGORY', 'Content permissions'), |
||
2772 | 'help' => _t(__CLASS__.'.REORGANISE_HELP', 'Rearrange pages in the site tree through drag&drop.'), |
||
2773 | 'sort' => 100 |
||
2774 | ), |
||
2775 | 'VIEW_DRAFT_CONTENT' => array( |
||
2776 | 'name' => _t(__CLASS__.'.VIEW_DRAFT_CONTENT', 'View draft content'), |
||
2777 | 'category' => _t('SilverStripe\\Security\\Permission.CONTENT_CATEGORY', 'Content permissions'), |
||
2778 | 'help' => _t(__CLASS__.'.VIEW_DRAFT_CONTENT_HELP', 'Applies to viewing pages outside of the CMS in draft mode. Useful for external collaborators without CMS access.'), |
||
2779 | 'sort' => 100 |
||
2780 | ) |
||
2781 | ); |
||
2782 | } |
||
2783 | |||
2784 | /** |
||
2785 | * Default singular name for page / sitetree |
||
2786 | * |
||
2787 | * @return string |
||
2788 | */ |
||
2789 | View Code Duplication | public function singular_name() |
|
2790 | { |
||
2791 | $base = in_array(static::class, [Page::class, self::class]); |
||
2792 | if ($base) { |
||
2793 | return $this->stat('base_singular_name'); |
||
2794 | } |
||
2795 | return parent::singular_name(); |
||
2796 | } |
||
2797 | |||
2798 | /** |
||
2799 | * Default plural name for page / sitetree |
||
2800 | * |
||
2801 | * @return string |
||
2802 | */ |
||
2803 | View Code Duplication | public function plural_name() |
|
2804 | { |
||
2805 | $base = in_array(static::class, [Page::class, self::class]); |
||
2806 | if ($base) { |
||
2807 | return $this->stat('base_plural_name'); |
||
2808 | } |
||
2809 | return parent::plural_name(); |
||
2810 | } |
||
2811 | |||
2812 | /** |
||
2813 | * Get description for this page type |
||
2814 | * |
||
2815 | * @return string|null |
||
2816 | */ |
||
2817 | public function classDescription() |
||
2818 | { |
||
2819 | $base = in_array(static::class, [Page::class, self::class]); |
||
2820 | if ($base) { |
||
2821 | return $this->stat('base_description'); |
||
2822 | } |
||
2823 | return $this->stat('description'); |
||
2824 | } |
||
2825 | |||
2826 | /** |
||
2827 | * Get localised description for this page |
||
2828 | * |
||
2829 | * @return string|null |
||
2830 | */ |
||
2831 | public function i18n_classDescription() |
||
2832 | { |
||
2833 | $description = $this->classDescription(); |
||
2834 | if ($description) { |
||
2835 | return _t(static::class.'.DESCRIPTION', $description); |
||
2836 | } |
||
2837 | return null; |
||
2838 | } |
||
2839 | |||
2840 | /** |
||
2841 | * Overloaded to also provide entities for 'Page' class which is usually located in custom code, hence textcollector |
||
2842 | * picks it up for the wrong folder. |
||
2843 | * |
||
2844 | * @return array |
||
2845 | */ |
||
2846 | public function provideI18nEntities() |
||
2847 | { |
||
2848 | $entities = parent::provideI18nEntities(); |
||
2849 | |||
2850 | // Add optional description |
||
2851 | $description = $this->classDescription(); |
||
2852 | if ($description) { |
||
2853 | $entities[static::class . '.DESCRIPTION'] = $description; |
||
2854 | } |
||
2855 | return $entities; |
||
2856 | } |
||
2857 | |||
2858 | /** |
||
2859 | * Returns 'root' if the current page has no parent, or 'subpage' otherwise |
||
2860 | * |
||
2861 | * @return string |
||
2862 | */ |
||
2863 | public function getParentType() |
||
2864 | { |
||
2865 | return $this->ParentID == 0 ? 'root' : 'subpage'; |
||
2866 | } |
||
2867 | |||
2868 | /** |
||
2869 | * Clear the permissions cache for SiteTree |
||
2870 | */ |
||
2871 | public static function reset() |
||
2872 | { |
||
2873 | $permissions = static::getPermissionChecker(); |
||
2874 | if ($permissions instanceof InheritedPermissions) { |
||
2875 | $permissions->clearCache(); |
||
2876 | } |
||
2877 | } |
||
2878 | |||
2879 | /** |
||
2880 | * Update dependant pages |
||
2881 | */ |
||
2882 | protected function updateDependentPages() |
||
2883 | { |
||
2884 | // Need to flush cache to avoid outdated versionnumber references |
||
2885 | $this->flushCache(); |
||
2886 | |||
2887 | // Need to mark pages depending to this one as broken |
||
2888 | $dependentPages = $this->DependentPages(); |
||
2889 | if ($dependentPages) { |
||
2890 | foreach ($dependentPages as $page) { |
||
2891 | // $page->write() calls syncLinkTracking, which does all the hard work for us. |
||
2892 | $page->write(); |
||
2893 | } |
||
2894 | } |
||
2895 | } |
||
2896 | } |
||
2897 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: