Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like SiteTree often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SiteTree, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 51 | class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvider,CMSPreviewable { | 
            ||
| 52 | |||
| 53 | /**  | 
            ||
| 54 | * Indicates what kind of children this page type can have.  | 
            ||
| 55 | * This can be an array of allowed child classes, or the string "none" -  | 
            ||
| 56 | * indicating that this page type can't have children.  | 
            ||
| 57 | * If a classname is prefixed by "*", such as "*Page", then only that  | 
            ||
| 58 | * class is allowed - no subclasses. Otherwise, the class and all its  | 
            ||
| 59 | * subclasses are allowed.  | 
            ||
| 60 | 	 * To control allowed children on root level (no parent), use {@link $can_be_root}. | 
            ||
| 61 | *  | 
            ||
| 62 | * Note that this setting is cached when used in the CMS, use the "flush" query parameter to clear it.  | 
            ||
| 63 | *  | 
            ||
| 64 | * @config  | 
            ||
| 65 | * @var array  | 
            ||
| 66 | */  | 
            ||
| 67 | 	private static $allowed_children = array("SiteTree"); | 
            ||
| 68 | |||
| 69 | /**  | 
            ||
| 70 | * The default child class for this page.  | 
            ||
| 71 | 	 * Note: Value might be cached, see {@link $allowed_chilren}. | 
            ||
| 72 | *  | 
            ||
| 73 | * @config  | 
            ||
| 74 | * @var string  | 
            ||
| 75 | */  | 
            ||
| 76 | private static $default_child = "Page";  | 
            ||
| 77 | |||
| 78 | /**  | 
            ||
| 79 | * Default value for SiteTree.ClassName enum  | 
            ||
| 80 | 	 * {@see DBClassName::getDefault} | 
            ||
| 81 | *  | 
            ||
| 82 | * @config  | 
            ||
| 83 | * @var string  | 
            ||
| 84 | */  | 
            ||
| 85 | private static $default_classname = "Page";  | 
            ||
| 86 | |||
| 87 | /**  | 
            ||
| 88 | * The default parent class for this page.  | 
            ||
| 89 | 	 * Note: Value might be cached, see {@link $allowed_chilren}. | 
            ||
| 90 | *  | 
            ||
| 91 | * @config  | 
            ||
| 92 | * @var string  | 
            ||
| 93 | */  | 
            ||
| 94 | private static $default_parent = null;  | 
            ||
| 95 | |||
| 96 | /**  | 
            ||
| 97 | * Controls whether a page can be in the root of the site tree.  | 
            ||
| 98 | 	 * Note: Value might be cached, see {@link $allowed_chilren}. | 
            ||
| 99 | *  | 
            ||
| 100 | * @config  | 
            ||
| 101 | * @var bool  | 
            ||
| 102 | */  | 
            ||
| 103 | private static $can_be_root = true;  | 
            ||
| 104 | |||
| 105 | /**  | 
            ||
| 106 | * List of permission codes a user can have to allow a user to create a page of this type.  | 
            ||
| 107 | 	 * Note: Value might be cached, see {@link $allowed_chilren}. | 
            ||
| 108 | *  | 
            ||
| 109 | * @config  | 
            ||
| 110 | * @var array  | 
            ||
| 111 | */  | 
            ||
| 112 | private static $need_permission = null;  | 
            ||
| 113 | |||
| 114 | /**  | 
            ||
| 115 | * If you extend a class, and don't want to be able to select the old class  | 
            ||
| 116 | * in the cms, set this to the old class name. Eg, if you extended Product  | 
            ||
| 117 | * to make ImprovedProduct, then you would set $hide_ancestor to Product.  | 
            ||
| 118 | *  | 
            ||
| 119 | * @config  | 
            ||
| 120 | * @var string  | 
            ||
| 121 | */  | 
            ||
| 122 | private static $hide_ancestor = null;  | 
            ||
| 123 | |||
| 124 | private static $db = array(  | 
            ||
| 125 | "URLSegment" => "Varchar(255)",  | 
            ||
| 126 | "Title" => "Varchar(255)",  | 
            ||
| 127 | "MenuTitle" => "Varchar(100)",  | 
            ||
| 128 | "Content" => "HTMLText",  | 
            ||
| 129 | "MetaDescription" => "Text",  | 
            ||
| 130 | "ExtraMeta" => "HTMLFragment(['whitelist' => ['meta', 'link']])",  | 
            ||
| 131 | "ShowInMenus" => "Boolean",  | 
            ||
| 132 | "ShowInSearch" => "Boolean",  | 
            ||
| 133 | "Sort" => "Int",  | 
            ||
| 134 | "HasBrokenFile" => "Boolean",  | 
            ||
| 135 | "HasBrokenLink" => "Boolean",  | 
            ||
| 136 | "ReportClass" => "Varchar",  | 
            ||
| 137 | 		"CanViewType" => "Enum('Anyone, LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit')", | 
            ||
| 138 | 		"CanEditType" => "Enum('LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit')", | 
            ||
| 139 | );  | 
            ||
| 140 | |||
| 141 | private static $indexes = array(  | 
            ||
| 142 | "URLSegment" => true,  | 
            ||
| 143 | );  | 
            ||
| 144 | |||
| 145 | private static $many_many = array(  | 
            ||
| 146 | "ViewerGroups" => "SilverStripe\\Security\\Group",  | 
            ||
| 147 | "EditorGroups" => "SilverStripe\\Security\\Group",  | 
            ||
| 148 | );  | 
            ||
| 149 | |||
| 150 | private static $has_many = array(  | 
            ||
| 151 | "VirtualPages" => "VirtualPage.CopyContentFrom"  | 
            ||
| 152 | );  | 
            ||
| 153 | |||
| 154 | private static $owned_by = array(  | 
            ||
| 155 | "VirtualPages"  | 
            ||
| 156 | );  | 
            ||
| 157 | |||
| 158 | private static $casting = array(  | 
            ||
| 159 | "Breadcrumbs" => "HTMLFragment",  | 
            ||
| 160 | "LastEdited" => "Datetime",  | 
            ||
| 161 | "Created" => "Datetime",  | 
            ||
| 162 | 'Link' => 'Text',  | 
            ||
| 163 | 'RelativeLink' => 'Text',  | 
            ||
| 164 | 'AbsoluteLink' => 'Text',  | 
            ||
| 165 | 'CMSEditLink' => 'Text',  | 
            ||
| 166 | 'TreeTitle' => 'HTMLFragment',  | 
            ||
| 167 | 'MetaTags' => 'HTMLFragment',  | 
            ||
| 168 | );  | 
            ||
| 169 | |||
| 170 | private static $defaults = array(  | 
            ||
| 171 | "ShowInMenus" => 1,  | 
            ||
| 172 | "ShowInSearch" => 1,  | 
            ||
| 173 | "CanViewType" => "Inherit",  | 
            ||
| 174 | "CanEditType" => "Inherit"  | 
            ||
| 175 | );  | 
            ||
| 176 | |||
| 177 | private static $versioning = array(  | 
            ||
| 178 | "Stage", "Live"  | 
            ||
| 179 | );  | 
            ||
| 180 | |||
| 181 | private static $default_sort = "\"Sort\"";  | 
            ||
| 182 | |||
| 183 | /**  | 
            ||
| 184 | * If this is false, the class cannot be created in the CMS by regular content authors, only by ADMINs.  | 
            ||
| 185 | * @var boolean  | 
            ||
| 186 | * @config  | 
            ||
| 187 | */  | 
            ||
| 188 | private static $can_create = true;  | 
            ||
| 189 | |||
| 190 | /**  | 
            ||
| 191 | * Icon to use in the CMS page tree. This should be the full filename, relative to the webroot.  | 
            ||
| 192 | * Also supports custom CSS rule contents (applied to the correct selector for the tree UI implementation).  | 
            ||
| 193 | *  | 
            ||
| 194 | * @see CMSMain::generateTreeStylingCSS()  | 
            ||
| 195 | * @config  | 
            ||
| 196 | * @var string  | 
            ||
| 197 | */  | 
            ||
| 198 | private static $icon = null;  | 
            ||
| 199 | |||
| 200 | /**  | 
            ||
| 201 | * @config  | 
            ||
| 202 | * @var string Description of the class functionality, typically shown to a user  | 
            ||
| 203 | 	 * when selecting which page type to create. Translated through {@link provideI18nEntities()}. | 
            ||
| 204 | */  | 
            ||
| 205 | private static $description = 'Generic content page';  | 
            ||
| 206 | |||
| 207 | private static $extensions = array(  | 
            ||
| 208 | 'SilverStripe\ORM\Hierarchy\Hierarchy',  | 
            ||
| 209 | 'SilverStripe\ORM\Versioning\Versioned',  | 
            ||
| 210 | "SiteTreeLinkTracking"  | 
            ||
| 211 | );  | 
            ||
| 212 | |||
| 213 | private static $searchable_fields = array(  | 
            ||
| 214 | 'Title',  | 
            ||
| 215 | 'Content',  | 
            ||
| 216 | );  | 
            ||
| 217 | |||
| 218 | private static $field_labels = array(  | 
            ||
| 219 | 'URLSegment' => 'URL'  | 
            ||
| 220 | );  | 
            ||
| 221 | |||
| 222 | /**  | 
            ||
| 223 | * @config  | 
            ||
| 224 | */  | 
            ||
| 225 | private static $nested_urls = true;  | 
            ||
| 226 | |||
| 227 | /**  | 
            ||
| 228 | * @config  | 
            ||
| 229 | */  | 
            ||
| 230 | private static $create_default_pages = true;  | 
            ||
| 231 | |||
| 232 | /**  | 
            ||
| 233 | * This controls whether of not extendCMSFields() is called by getCMSFields.  | 
            ||
| 234 | */  | 
            ||
| 235 | private static $runCMSFieldsExtensions = true;  | 
            ||
| 236 | |||
| 237 | /**  | 
            ||
| 238 | * Cache for canView/Edit/Publish/Delete permissions.  | 
            ||
| 239 | * Keyed by permission type (e.g. 'edit'), with an array  | 
            ||
| 240 | * of IDs mapped to their boolean permission ability (true=allow, false=deny).  | 
            ||
| 241 | 	 * See {@link batch_permission_check()} for details. | 
            ||
| 242 | */  | 
            ||
| 243 | private static $cache_permissions = array();  | 
            ||
| 244 | |||
| 245 | /**  | 
            ||
| 246 | * @config  | 
            ||
| 247 | * @var boolean  | 
            ||
| 248 | */  | 
            ||
| 249 | private static $enforce_strict_hierarchy = true;  | 
            ||
| 250 | |||
| 251 | /**  | 
            ||
| 252 | * The value used for the meta generator tag. Leave blank to omit the tag.  | 
            ||
| 253 | *  | 
            ||
| 254 | * @config  | 
            ||
| 255 | * @var string  | 
            ||
| 256 | */  | 
            ||
| 257 | private static $meta_generator = 'SilverStripe - http://silverstripe.org';  | 
            ||
| 258 | |||
| 259 | protected $_cache_statusFlags = null;  | 
            ||
| 260 | |||
| 261 | /**  | 
            ||
| 262 | 	 * Fetches the {@link SiteTree} object that maps to a link. | 
            ||
| 263 | *  | 
            ||
| 264 | 	 * If you have enabled {@link SiteTree::config()->nested_urls} on this site, then you can use a nested link such as | 
            ||
| 265 | * "about-us/staff/", and this function will traverse down the URL chain and grab the appropriate link.  | 
            ||
| 266 | *  | 
            ||
| 267 | * Note that if no model can be found, this method will fall over to a extended alternateGetByLink method provided  | 
            ||
| 268 | 	 * by a extension attached to {@link SiteTree} | 
            ||
| 269 | *  | 
            ||
| 270 | * @param string $link The link of the page to search for  | 
            ||
| 271 | * @param bool $cache True (default) to use caching, false to force a fresh search from the database  | 
            ||
| 272 | * @return SiteTree  | 
            ||
| 273 | */  | 
            ||
| 274 | 	static public function get_by_link($link, $cache = true) { | 
            ||
| 275 | 		if(trim($link, '/')) { | 
            ||
| 276 | $link = trim(Director::makeRelative($link), '/');  | 
            ||
| 277 | 		} else { | 
            ||
| 278 | $link = RootURLController::get_homepage_link();  | 
            ||
| 279 | }  | 
            ||
| 280 | |||
| 281 | 		$parts = preg_split('|/+|', $link); | 
            ||
| 282 | |||
| 283 | // Grab the initial root level page to traverse down from.  | 
            ||
| 284 | $URLSegment = array_shift($parts);  | 
            ||
| 285 | 		$conditions = array('"SiteTree"."URLSegment"' => rawurlencode($URLSegment)); | 
            ||
| 286 | 		if(self::config()->nested_urls) { | 
            ||
| 287 | 			$conditions[] = array('"SiteTree"."ParentID"' => 0); | 
            ||
| 288 | }  | 
            ||
| 289 | 		$sitetree = DataObject::get_one('SiteTree', $conditions, $cache); | 
            ||
| 290 | |||
| 291 | /// Fall back on a unique URLSegment for b/c.  | 
            ||
| 292 | if( !$sitetree  | 
            ||
| 293 | && self::config()->nested_urls  | 
            ||
| 294 | 			&& $page = DataObject::get_one('SiteTree', array( | 
            ||
| 295 | '"SiteTree"."URLSegment"' => $URLSegment  | 
            ||
| 296 | ), $cache)  | 
            ||
| 297 | 		) { | 
            ||
| 298 | return $page;  | 
            ||
| 299 | }  | 
            ||
| 300 | |||
| 301 | // Attempt to grab an alternative page from extensions.  | 
            ||
| 302 | 		if(!$sitetree) { | 
            ||
| 303 | $parentID = self::config()->nested_urls ? 0 : null;  | 
            ||
| 304 | |||
| 305 | View Code Duplication | 			if($alternatives = singleton('SiteTree')->extend('alternateGetByLink', $URLSegment, $parentID)) { | 
            |
| 306 | foreach($alternatives as $alternative) if($alternative) $sitetree = $alternative;  | 
            ||
| 307 | }  | 
            ||
| 308 | |||
| 309 | if(!$sitetree) return false;  | 
            ||
| 310 | }  | 
            ||
| 311 | |||
| 312 | // Check if we have any more URL parts to parse.  | 
            ||
| 313 | if(!self::config()->nested_urls || !count($parts)) return $sitetree;  | 
            ||
| 314 | |||
| 315 | // Traverse down the remaining URL segments and grab the relevant SiteTree objects.  | 
            ||
| 316 | 		foreach($parts as $segment) { | 
            ||
| 317 | 			$next = DataObject::get_one('SiteTree', array( | 
            ||
| 318 | '"SiteTree"."URLSegment"' => $segment,  | 
            ||
| 319 | '"SiteTree"."ParentID"' => $sitetree->ID  | 
            ||
| 320 | ),  | 
            ||
| 321 | $cache  | 
            ||
| 322 | );  | 
            ||
| 323 | |||
| 324 | 			if(!$next) { | 
            ||
| 325 | $parentID = (int) $sitetree->ID;  | 
            ||
| 326 | |||
| 327 | View Code Duplication | 				if($alternatives = singleton('SiteTree')->extend('alternateGetByLink', $segment, $parentID)) { | 
            |
| 328 | foreach($alternatives as $alternative) if($alternative) $next = $alternative;  | 
            ||
| 329 | }  | 
            ||
| 330 | |||
| 331 | if(!$next) return false;  | 
            ||
| 332 | }  | 
            ||
| 333 | |||
| 334 | $sitetree->destroy();  | 
            ||
| 335 | $sitetree = $next;  | 
            ||
| 336 | }  | 
            ||
| 337 | |||
| 338 | return $sitetree;  | 
            ||
| 339 | }  | 
            ||
| 340 | |||
| 341 | /**  | 
            ||
| 342 | 	 * Return a subclass map of SiteTree that shouldn't be hidden through {@link SiteTree::$hide_ancestor} | 
            ||
| 343 | *  | 
            ||
| 344 | * @return array  | 
            ||
| 345 | */  | 
            ||
| 346 | 	public static function page_type_classes() { | 
            ||
| 347 | $classes = ClassInfo::getValidSubClasses();  | 
            ||
| 348 | |||
| 349 | 		$baseClassIndex = array_search('SiteTree', $classes); | 
            ||
| 350 | if($baseClassIndex !== FALSE) unset($classes[$baseClassIndex]);  | 
            ||
| 351 | |||
| 352 | $kill_ancestors = array();  | 
            ||
| 353 | |||
| 354 | // figure out if there are any classes we don't want to appear  | 
            ||
| 355 | 		foreach($classes as $class) { | 
            ||
| 356 | $instance = singleton($class);  | 
            ||
| 357 | |||
| 358 | // do any of the progeny want to hide an ancestor?  | 
            ||
| 359 | 			if($ancestor_to_hide = $instance->stat('hide_ancestor')) { | 
            ||
| 360 | // note for killing later  | 
            ||
| 361 | $kill_ancestors[] = $ancestor_to_hide;  | 
            ||
| 362 | }  | 
            ||
| 363 | }  | 
            ||
| 364 | |||
| 365 | // If any of the descendents don't want any of the elders to show up, cruelly render the elders surplus to  | 
            ||
| 366 | // requirements  | 
            ||
| 367 | 		if($kill_ancestors) { | 
            ||
| 368 | $kill_ancestors = array_unique($kill_ancestors);  | 
            ||
| 369 | 			foreach($kill_ancestors as $mark) { | 
            ||
| 370 | // unset from $classes  | 
            ||
| 371 | $idx = array_search($mark, $classes, true);  | 
            ||
| 372 | 				if ($idx !== false) { | 
            ||
| 373 | unset($classes[$idx]);  | 
            ||
| 374 | }  | 
            ||
| 375 | }  | 
            ||
| 376 | }  | 
            ||
| 377 | |||
| 378 | return $classes;  | 
            ||
| 379 | }  | 
            ||
| 380 | |||
| 381 | /**  | 
            ||
| 382 | * Replace a "[sitetree_link id=n]" shortcode with a link to the page with the corresponding ID.  | 
            ||
| 383 | *  | 
            ||
| 384 | * @param array $arguments  | 
            ||
| 385 | * @param string $content  | 
            ||
| 386 | * @param TextParser $parser  | 
            ||
| 387 | * @return string  | 
            ||
| 388 | */  | 
            ||
| 389 | 	static public function link_shortcode_handler($arguments, $content = null, $parser = null) { | 
            ||
| 390 | if(!isset($arguments['id']) || !is_numeric($arguments['id'])) return;  | 
            ||
| 391 | |||
| 392 | if (  | 
            ||
| 393 | 			   !($page = DataObject::get_by_id('SiteTree', $arguments['id']))         // Get the current page by ID. | 
            ||
| 394 | 			&& !($page = Versioned::get_latest_version('SiteTree', $arguments['id'])) // Attempt link to old version. | 
            ||
| 395 | 		) { | 
            ||
| 396 | return null; // There were no suitable matches at all.  | 
            ||
| 397 | }  | 
            ||
| 398 | |||
| 399 | $link = Convert::raw2att($page->Link());  | 
            ||
| 400 | |||
| 401 | 		if($content) { | 
            ||
| 402 | 			return sprintf('<a href="%s">%s</a>', $link, $parser->parse($content)); | 
            ||
| 403 | 		} else { | 
            ||
| 404 | return $link;  | 
            ||
| 405 | }  | 
            ||
| 406 | }  | 
            ||
| 407 | |||
| 408 | /**  | 
            ||
| 409 | 	 * Return the link for this {@link SiteTree} object, with the {@link Director::baseURL()} included. | 
            ||
| 410 | *  | 
            ||
| 411 | * @param string $action Optional controller action (method).  | 
            ||
| 412 | * Note: URI encoding of this parameter is applied automatically through template casting,  | 
            ||
| 413 | 	 *                       don't encode the passed parameter. Please use {@link Controller::join_links()} instead to | 
            ||
| 414 | * append GET parameters.  | 
            ||
| 415 | * @return string  | 
            ||
| 416 | */  | 
            ||
| 417 | 	public function Link($action = null) { | 
            ||
| 418 | return Controller::join_links(Director::baseURL(), $this->RelativeLink($action));  | 
            ||
| 419 | }  | 
            ||
| 420 | |||
| 421 | /**  | 
            ||
| 422 | * Get the absolute URL for this page, including protocol and host.  | 
            ||
| 423 | *  | 
            ||
| 424 | 	 * @param string $action See {@link Link()} | 
            ||
| 425 | * @return string  | 
            ||
| 426 | */  | 
            ||
| 427 | 	public function AbsoluteLink($action = null) { | 
            ||
| 428 | 		if($this->hasMethod('alternateAbsoluteLink')) { | 
            ||
| 429 | return $this->alternateAbsoluteLink($action);  | 
            ||
| 430 | 		} else { | 
            ||
| 431 | return Director::absoluteURL($this->Link($action));  | 
            ||
| 432 | }  | 
            ||
| 433 | }  | 
            ||
| 434 | |||
| 435 | /**  | 
            ||
| 436 | * Base link used for previewing. Defaults to absolute URL, in order to account for domain changes, e.g. on multi  | 
            ||
| 437 | 	 * site setups. Does not contain hints about the stage, see {@link SilverStripeNavigator} for details. | 
            ||
| 438 | *  | 
            ||
| 439 | 	 * @param string $action See {@link Link()} | 
            ||
| 440 | * @return string  | 
            ||
| 441 | */  | 
            ||
| 442 | 	public function PreviewLink($action = null) { | 
            ||
| 443 | 		if($this->hasMethod('alternatePreviewLink')) { | 
            ||
| 444 | 			Deprecation::notice('5.0', 'Use updatePreviewLink or override PreviewLink method'); | 
            ||
| 445 | return $this->alternatePreviewLink($action);  | 
            ||
| 446 | }  | 
            ||
| 447 | |||
| 448 | $link = $this->AbsoluteLink($action);  | 
            ||
| 449 | 		$this->extend('updatePreviewLink', $link, $action); | 
            ||
| 450 | return $link;  | 
            ||
| 451 | }  | 
            ||
| 452 | |||
| 453 | 	public function getMimeType() { | 
            ||
| 454 | return 'text/html';  | 
            ||
| 455 | }  | 
            ||
| 456 | |||
| 457 | /**  | 
            ||
| 458 | 	 * Return the link for this {@link SiteTree} object relative to the SilverStripe root. | 
            ||
| 459 | *  | 
            ||
| 460 | * By default, if this page is the current home page, and there is no action specified then this will return a link  | 
            ||
| 461 | * to the root of the site. However, if you set the $action parameter to TRUE then the link will not be rewritten  | 
            ||
| 462 | * and returned in its full form.  | 
            ||
| 463 | *  | 
            ||
| 464 | * @uses RootURLController::get_homepage_link()  | 
            ||
| 465 | *  | 
            ||
| 466 | 	 * @param string $action See {@link Link()} | 
            ||
| 467 | * @return string  | 
            ||
| 468 | */  | 
            ||
| 469 | 	public function RelativeLink($action = null) { | 
            ||
| 470 | 		if($this->ParentID && self::config()->nested_urls) { | 
            ||
| 471 | $parent = $this->Parent();  | 
            ||
| 472 | // If page is removed select parent from version history (for archive page view)  | 
            ||
| 473 | 			if((!$parent || !$parent->exists()) && $this->IsDeletedFromStage) { | 
            ||
| 474 | 				$parent = Versioned::get_latest_version('SiteTree', $this->ParentID); | 
            ||
| 475 | }  | 
            ||
| 476 | $base = $parent->RelativeLink($this->URLSegment);  | 
            ||
| 477 | 		} elseif(!$action && $this->URLSegment == RootURLController::get_homepage_link()) { | 
            ||
| 478 | // Unset base for root-level homepages.  | 
            ||
| 479 | // Note: Homepages with action parameters (or $action === true)  | 
            ||
| 480 | // need to retain their URLSegment.  | 
            ||
| 481 | $base = null;  | 
            ||
| 482 | 		} else { | 
            ||
| 483 | $base = $this->URLSegment;  | 
            ||
| 484 | }  | 
            ||
| 485 | |||
| 486 | 		$this->extend('updateRelativeLink', $base, $action); | 
            ||
| 487 | |||
| 488 | // Legacy support: If $action === true, retain URLSegment for homepages,  | 
            ||
| 489 | // but don't append any action  | 
            ||
| 490 | if($action === true) $action = null;  | 
            ||
| 491 | |||
| 492 | return Controller::join_links($base, '/', $action);  | 
            ||
| 493 | }  | 
            ||
| 494 | |||
| 495 | /**  | 
            ||
| 496 | * Get the absolute URL for this page on the Live site.  | 
            ||
| 497 | *  | 
            ||
| 498 | * @param bool $includeStageEqualsLive Whether to append the URL with ?stage=Live to force Live mode  | 
            ||
| 499 | * @return string  | 
            ||
| 500 | */  | 
            ||
| 501 | 	public function getAbsoluteLiveLink($includeStageEqualsLive = true) { | 
            ||
| 502 | $oldReadingMode = Versioned::get_reading_mode();  | 
            ||
| 503 | Versioned::set_stage(Versioned::LIVE);  | 
            ||
| 504 | 		$live = Versioned::get_one_by_stage('SiteTree', Versioned::LIVE, array( | 
            ||
| 505 | '"SiteTree"."ID"' => $this->ID  | 
            ||
| 506 | ));  | 
            ||
| 507 | 		if($live) { | 
            ||
| 508 | $link = $live->AbsoluteLink();  | 
            ||
| 509 | if($includeStageEqualsLive) $link .= '?stage=Live';  | 
            ||
| 510 | 		} else { | 
            ||
| 511 | $link = null;  | 
            ||
| 512 | }  | 
            ||
| 513 | |||
| 514 | Versioned::set_reading_mode($oldReadingMode);  | 
            ||
| 515 | return $link;  | 
            ||
| 516 | }  | 
            ||
| 517 | |||
| 518 | /**  | 
            ||
| 519 | * Generates a link to edit this page in the CMS.  | 
            ||
| 520 | *  | 
            ||
| 521 | * @return string  | 
            ||
| 522 | */  | 
            ||
| 523 | 	public function CMSEditLink() { | 
            ||
| 524 | $link = Controller::join_links(  | 
            ||
| 525 | 			singleton('CMSPageEditController')->Link('show'), | 
            ||
| 526 | $this->ID  | 
            ||
| 527 | );  | 
            ||
| 528 | return Director::absoluteURL($link);  | 
            ||
| 529 | }  | 
            ||
| 530 | |||
| 531 | |||
| 532 | /**  | 
            ||
| 533 | * Return a CSS identifier generated from this page's link.  | 
            ||
| 534 | *  | 
            ||
| 535 | * @return string The URL segment  | 
            ||
| 536 | */  | 
            ||
| 537 | 	public function ElementName() { | 
            ||
| 538 | 		return str_replace('/', '-', trim($this->RelativeLink(true), '/')); | 
            ||
| 539 | }  | 
            ||
| 540 | |||
| 541 | /**  | 
            ||
| 542 | * Returns true if this is the currently active page being used to handle this request.  | 
            ||
| 543 | *  | 
            ||
| 544 | * @return bool  | 
            ||
| 545 | */  | 
            ||
| 546 | 	public function isCurrent() { | 
            ||
| 547 | return $this->ID ? $this->ID == Director::get_current_page()->ID : $this === Director::get_current_page();  | 
            ||
| 548 | }  | 
            ||
| 549 | |||
| 550 | /**  | 
            ||
| 551 | * Check if this page is in the currently active section (e.g. it is either current or one of its children is  | 
            ||
| 552 | * currently being viewed).  | 
            ||
| 553 | *  | 
            ||
| 554 | * @return bool  | 
            ||
| 555 | */  | 
            ||
| 556 | 	public function isSection() { | 
            ||
| 557 | return $this->isCurrent() || (  | 
            ||
| 558 | Director::get_current_page() instanceof SiteTree && in_array($this->ID, Director::get_current_page()->getAncestors()->column())  | 
            ||
| 559 | );  | 
            ||
| 560 | }  | 
            ||
| 561 | |||
| 562 | /**  | 
            ||
| 563 | * Check if the parent of this page has been removed (or made otherwise unavailable), and is still referenced by  | 
            ||
| 564 | * this child. Any such orphaned page may still require access via the CMS, but should not be shown as accessible  | 
            ||
| 565 | * to external users.  | 
            ||
| 566 | *  | 
            ||
| 567 | * @return bool  | 
            ||
| 568 | */  | 
            ||
| 569 | 	public function isOrphaned() { | 
            ||
| 570 | // Always false for root pages  | 
            ||
| 571 | if(empty($this->ParentID)) return false;  | 
            ||
| 572 | |||
| 573 | // Parent must exist and not be an orphan itself  | 
            ||
| 574 | $parent = $this->Parent();  | 
            ||
| 575 | return !$parent || !$parent->exists() || $parent->isOrphaned();  | 
            ||
| 576 | }  | 
            ||
| 577 | |||
| 578 | /**  | 
            ||
| 579 | 	 * Return "link" or "current" depending on if this is the {@link SiteTree::isCurrent()} current page. | 
            ||
| 580 | *  | 
            ||
| 581 | * @return string  | 
            ||
| 582 | */  | 
            ||
| 583 | 	public function LinkOrCurrent() { | 
            ||
| 584 | return $this->isCurrent() ? 'current' : 'link';  | 
            ||
| 585 | }  | 
            ||
| 586 | |||
| 587 | /**  | 
            ||
| 588 | 	 * Return "link" or "section" depending on if this is the {@link SiteTree::isSeciton()} current section. | 
            ||
| 589 | *  | 
            ||
| 590 | * @return string  | 
            ||
| 591 | */  | 
            ||
| 592 | 	public function LinkOrSection() { | 
            ||
| 593 | return $this->isSection() ? 'section' : 'link';  | 
            ||
| 594 | }  | 
            ||
| 595 | |||
| 596 | /**  | 
            ||
| 597 | * Return "link", "current" or "section" depending on if this page is the current page, or not on the current page  | 
            ||
| 598 | * but in the current section.  | 
            ||
| 599 | *  | 
            ||
| 600 | * @return string  | 
            ||
| 601 | */  | 
            ||
| 602 | 	public function LinkingMode() { | 
            ||
| 603 | 		if($this->isCurrent()) { | 
            ||
| 604 | return 'current';  | 
            ||
| 605 | 		} elseif($this->isSection()) { | 
            ||
| 606 | return 'section';  | 
            ||
| 607 | 		} else { | 
            ||
| 608 | return 'link';  | 
            ||
| 609 | }  | 
            ||
| 610 | }  | 
            ||
| 611 | |||
| 612 | /**  | 
            ||
| 613 | * Check if this page is in the given current section.  | 
            ||
| 614 | *  | 
            ||
| 615 | * @param string $sectionName Name of the section to check  | 
            ||
| 616 | * @return bool True if we are in the given section  | 
            ||
| 617 | */  | 
            ||
| 618 | 	public function InSection($sectionName) { | 
            ||
| 619 | $page = Director::get_current_page();  | 
            ||
| 620 | 		while($page) { | 
            ||
| 621 | if($sectionName == $page->URLSegment)  | 
            ||
| 622 | return true;  | 
            ||
| 623 | $page = $page->Parent;  | 
            ||
| 624 | }  | 
            ||
| 625 | return false;  | 
            ||
| 626 | }  | 
            ||
| 627 | |||
| 628 | /**  | 
            ||
| 629 | * Reset Sort on duped page  | 
            ||
| 630 | *  | 
            ||
| 631 | * @param SiteTree $original  | 
            ||
| 632 | * @param bool $doWrite  | 
            ||
| 633 | */  | 
            ||
| 634 | 	public function onBeforeDuplicate($original, $doWrite) { | 
            ||
| 635 | $this->Sort = 0;  | 
            ||
| 636 | }  | 
            ||
| 637 | |||
| 638 | /**  | 
            ||
| 639 | * Duplicates each child of this node recursively and returns the top-level duplicate node.  | 
            ||
| 640 | *  | 
            ||
| 641 | * @return self The duplicated object  | 
            ||
| 642 | */  | 
            ||
| 643 | 	public function duplicateWithChildren() { | 
            ||
| 644 | $clone = $this->duplicate();  | 
            ||
| 645 | $children = $this->AllChildren();  | 
            ||
| 646 | |||
| 647 | 		if($children) { | 
            ||
| 648 | 			foreach($children as $child) { | 
            ||
| 649 | $childClone = method_exists($child, 'duplicateWithChildren')  | 
            ||
| 650 | ? $child->duplicateWithChildren()  | 
            ||
| 651 | : $child->duplicate();  | 
            ||
| 652 | $childClone->ParentID = $clone->ID;  | 
            ||
| 653 | $childClone->write();  | 
            ||
| 654 | }  | 
            ||
| 655 | }  | 
            ||
| 656 | |||
| 657 | return $clone;  | 
            ||
| 658 | }  | 
            ||
| 659 | |||
| 660 | /**  | 
            ||
| 661 | * Duplicate this node and its children as a child of the node with the given ID  | 
            ||
| 662 | *  | 
            ||
| 663 | * @param int $id ID of the new node's new parent  | 
            ||
| 664 | */  | 
            ||
| 665 | 	public function duplicateAsChild($id) { | 
            ||
| 671 | |||
| 672 | /**  | 
            ||
| 673 | * Return a breadcrumb trail to this page. Excludes "hidden" pages (with ShowInMenus=0) by default.  | 
            ||
| 674 | *  | 
            ||
| 675 | * @param int $maxDepth The maximum depth to traverse.  | 
            ||
| 676 | * @param boolean $unlinked Whether to link page titles.  | 
            ||
| 677 | * @param boolean|string $stopAtPageType ClassName of a page to stop the upwards traversal.  | 
            ||
| 678 | * @param boolean $showHidden Include pages marked with the attribute ShowInMenus = 0  | 
            ||
| 679 | * @return string The breadcrumb trail.  | 
            ||
| 680 | */  | 
            ||
| 681 | 	public function Breadcrumbs($maxDepth = 20, $unlinked = false, $stopAtPageType = false, $showHidden = false) { | 
            ||
| 689 | |||
| 690 | |||
| 691 | /**  | 
            ||
| 692 | * Returns a list of breadcrumbs for the current page.  | 
            ||
| 693 | *  | 
            ||
| 694 | * @param int $maxDepth The maximum depth to traverse.  | 
            ||
| 695 | * @param boolean|string $stopAtPageType ClassName of a page to stop the upwards traversal.  | 
            ||
| 696 | * @param boolean $showHidden Include pages marked with the attribute ShowInMenus = 0  | 
            ||
| 697 | *  | 
            ||
| 698 | * @return ArrayList  | 
            ||
| 699 | */  | 
            ||
| 700 | 	public function getBreadcrumbItems($maxDepth = 20, $stopAtPageType = false, $showHidden = false) { | 
            ||
| 718 | |||
| 719 | |||
| 720 | /**  | 
            ||
| 721 | * Make this page a child of another page.  | 
            ||
| 722 | *  | 
            ||
| 723 | * If the parent page does not exist, resolve it to a valid ID before updating this page's reference.  | 
            ||
| 724 | *  | 
            ||
| 725 | * @param SiteTree|int $item Either the parent object, or the parent ID  | 
            ||
| 726 | */  | 
            ||
| 727 | 	public function setParent($item) { | 
            ||
| 735 | |||
| 736 | /**  | 
            ||
| 737 | * Get the parent of this page.  | 
            ||
| 738 | *  | 
            ||
| 739 | * @return SiteTree Parent of this page  | 
            ||
| 740 | */  | 
            ||
| 741 | 	public function getParent() { | 
            ||
| 746 | |||
| 747 | /**  | 
            ||
| 748 | * Return a string of the form "parent - page" or "grandparent - parent - page" using page titles  | 
            ||
| 749 | *  | 
            ||
| 750 | * @param int $level The maximum amount of levels to traverse.  | 
            ||
| 751 | * @param string $separator Seperating string  | 
            ||
| 752 | * @return string The resulting string  | 
            ||
| 753 | */  | 
            ||
| 754 | 	public function NestedTitle($level = 2, $separator = " - ") { | 
            ||
| 764 | |||
| 765 | /**  | 
            ||
| 766 | * This function should return true if the current user can execute this action. It can be overloaded to customise  | 
            ||
| 767 | * the security model for an application.  | 
            ||
| 768 | *  | 
            ||
| 769 | 	 * Slightly altered from parent behaviour in {@link DataObject->can()}: | 
            ||
| 770 | * - Checks for existence of a method named "can<$perm>()" on the object  | 
            ||
| 771 | * - Calls decorators and only returns for FALSE "vetoes"  | 
            ||
| 772 | 	 * - Falls back to {@link Permission::check()} | 
            ||
| 773 | * - Does NOT check for many-many relations named "Can<$perm>"  | 
            ||
| 774 | *  | 
            ||
| 775 | * @uses DataObjectDecorator->can()  | 
            ||
| 776 | *  | 
            ||
| 777 | * @param string $perm The permission to be checked, such as 'View'  | 
            ||
| 778 | * @param Member $member The member whose permissions need checking. Defaults to the currently logged in user.  | 
            ||
| 779 | * @param array $context Context argument for canCreate()  | 
            ||
| 780 | * @return bool True if the the member is allowed to do the given action  | 
            ||
| 781 | */  | 
            ||
| 782 | 	public function can($perm, $member = null, $context = array()) { | 
            ||
| 783 | View Code Duplication | 		if(!$member || !($member instanceof Member) || is_numeric($member)) { | 
            |
| 784 | $member = Member::currentUserID();  | 
            ||
| 785 | }  | 
            ||
| 786 | |||
| 787 | if($member && Permission::checkMember($member, "ADMIN")) return true;  | 
            ||
| 788 | |||
| 789 | 		if(is_string($perm) && method_exists($this, 'can' . ucfirst($perm))) { | 
            ||
| 790 | $method = 'can' . ucfirst($perm);  | 
            ||
| 791 | return $this->$method($member);  | 
            ||
| 792 | }  | 
            ||
| 793 | |||
| 794 | 		$results = $this->extend('can', $member); | 
            ||
| 795 | if($results && is_array($results)) if(!min($results)) return false;  | 
            ||
| 796 | |||
| 797 | return ($member && Permission::checkMember($member, $perm));  | 
            ||
| 798 | }  | 
            ||
| 799 | |||
| 800 | /**  | 
            ||
| 801 | * This function should return true if the current user can add children to this page. It can be overloaded to  | 
            ||
| 802 | * customise the security model for an application.  | 
            ||
| 803 | *  | 
            ||
| 804 | * Denies permission if any of the following conditions is true:  | 
            ||
| 805 | * - alternateCanAddChildren() on a extension returns false  | 
            ||
| 806 | * - canEdit() is not granted  | 
            ||
| 807 | 	 * - There are no classes defined in {@link $allowed_children} | 
            ||
| 808 | *  | 
            ||
| 809 | * @uses SiteTreeExtension->canAddChildren()  | 
            ||
| 810 | * @uses canEdit()  | 
            ||
| 811 | * @uses $allowed_children  | 
            ||
| 812 | *  | 
            ||
| 813 | * @param Member|int $member  | 
            ||
| 814 | * @return bool True if the current user can add children  | 
            ||
| 815 | */  | 
            ||
| 816 | 	public function canAddChildren($member = null) { | 
            ||
| 839 | |||
| 840 | /**  | 
            ||
| 841 | * This function should return true if the current user can view this page. It can be overloaded to customise the  | 
            ||
| 842 | * security model for an application.  | 
            ||
| 843 | *  | 
            ||
| 844 | * Denies permission if any of the following conditions is true:  | 
            ||
| 845 | * - canView() on any extension returns false  | 
            ||
| 846 | * - "CanViewType" directive is set to "Inherit" and any parent page return false for canView()  | 
            ||
| 847 | * - "CanViewType" directive is set to "LoggedInUsers" and no user is logged in  | 
            ||
| 848 | * - "CanViewType" directive is set to "OnlyTheseUsers" and user is not in the given groups  | 
            ||
| 849 | *  | 
            ||
| 850 | * @uses DataExtension->canView()  | 
            ||
| 851 | * @uses ViewerGroups()  | 
            ||
| 852 | *  | 
            ||
| 853 | * @param Member|int $member  | 
            ||
| 854 | * @return bool True if the current user can view this page  | 
            ||
| 855 | */  | 
            ||
| 856 | 	public function canView($member = null) { | 
            ||
| 857 | View Code Duplication | 		if(!$member || !($member instanceof Member) || is_numeric($member)) { | 
            |
| 858 | $member = Member::currentUserID();  | 
            ||
| 859 | }  | 
            ||
| 860 | |||
| 861 | // Standard mechanism for accepting permission changes from extensions  | 
            ||
| 862 | 		$extended = $this->extendedCan('canView', $member); | 
            ||
| 863 | 		if($extended !== null) { | 
            ||
| 864 | return $extended;  | 
            ||
| 865 | }  | 
            ||
| 866 | |||
| 867 | // admin override  | 
            ||
| 868 | 		if($member && Permission::checkMember($member, array("ADMIN", "SITETREE_VIEW_ALL"))) { | 
            ||
| 869 | return true;  | 
            ||
| 870 | }  | 
            ||
| 871 | |||
| 872 | // Orphaned pages (in the current stage) are unavailable, except for admins via the CMS  | 
            ||
| 873 | 		if($this->isOrphaned()) { | 
            ||
| 874 | return false;  | 
            ||
| 875 | }  | 
            ||
| 876 | |||
| 877 | // check for empty spec  | 
            ||
| 878 | 		if(!$this->CanViewType || $this->CanViewType == 'Anyone') { | 
            ||
| 879 | return true;  | 
            ||
| 880 | }  | 
            ||
| 881 | |||
| 882 | // check for inherit  | 
            ||
| 883 | 		if($this->CanViewType == 'Inherit') { | 
            ||
| 884 | if($this->ParentID) return $this->Parent()->canView($member);  | 
            ||
| 885 | else return $this->getSiteConfig()->canViewPages($member);  | 
            ||
| 886 | }  | 
            ||
| 887 | |||
| 888 | // check for any logged-in users  | 
            ||
| 889 | 		if($this->CanViewType == 'LoggedInUsers' && $member) { | 
            ||
| 890 | return true;  | 
            ||
| 891 | }  | 
            ||
| 892 | |||
| 893 | // check for specific groups  | 
            ||
| 894 | 		if($member && is_numeric($member)) { | 
            ||
| 895 | 			$member = DataObject::get_by_id('SilverStripe\\Security\\Member', $member); | 
            ||
| 896 | }  | 
            ||
| 897 | if(  | 
            ||
| 898 | $this->CanViewType == 'OnlyTheseUsers'  | 
            ||
| 899 | && $member  | 
            ||
| 900 | && $member->inGroups($this->ViewerGroups())  | 
            ||
| 901 | ) return true;  | 
            ||
| 902 | |||
| 903 | return false;  | 
            ||
| 904 | }  | 
            ||
| 905 | |||
| 906 | /**  | 
            ||
| 907 | * This function should return true if the current user can delete this page. It can be overloaded to customise the  | 
            ||
| 908 | * security model for an application.  | 
            ||
| 909 | *  | 
            ||
| 910 | * Denies permission if any of the following conditions is true:  | 
            ||
| 911 | * - canDelete() returns false on any extension  | 
            ||
| 912 | * - canEdit() returns false  | 
            ||
| 913 | * - any descendant page returns false for canDelete()  | 
            ||
| 914 | *  | 
            ||
| 915 | * @uses canDelete()  | 
            ||
| 916 | * @uses SiteTreeExtension->canDelete()  | 
            ||
| 917 | * @uses canEdit()  | 
            ||
| 918 | *  | 
            ||
| 919 | * @param Member $member  | 
            ||
| 920 | * @return bool True if the current user can delete this page  | 
            ||
| 921 | */  | 
            ||
| 922 | 	public function canDelete($member = null) { | 
            ||
| 945 | |||
| 946 | /**  | 
            ||
| 947 | * This function should return true if the current user can create new pages of this class, regardless of class. It  | 
            ||
| 948 | * can be overloaded to customise the security model for an application.  | 
            ||
| 949 | *  | 
            ||
| 950 | * By default, permission to create at the root level is based on the SiteConfig configuration, and permission to  | 
            ||
| 951 | * create beneath a parent is based on the ability to edit that parent page.  | 
            ||
| 952 | *  | 
            ||
| 953 | 	 * Use {@link canAddChildren()} to control behaviour of creating children under this page. | 
            ||
| 954 | *  | 
            ||
| 955 | * @uses $can_create  | 
            ||
| 956 | * @uses DataExtension->canCreate()  | 
            ||
| 957 | *  | 
            ||
| 958 | * @param Member $member  | 
            ||
| 959 | 	 * @param array $context Optional array which may contain array('Parent' => $parentObj) | 
            ||
| 960 | * If a parent page is known, it will be checked for validity.  | 
            ||
| 961 | * If omitted, it will be assumed this is to be created as a top level page.  | 
            ||
| 962 | * @return bool True if the current user can create pages on this class.  | 
            ||
| 963 | */  | 
            ||
| 964 | 	public function canCreate($member = null, $context = array()) { | 
            ||
| 965 | View Code Duplication | 		if(!$member || !(is_a($member, 'SilverStripe\\Security\\Member')) || is_numeric($member)) { | 
            |
| 966 | $member = Member::currentUserID();  | 
            ||
| 967 | }  | 
            ||
| 968 | |||
| 969 | // Check parent (custom canCreate option for SiteTree)  | 
            ||
| 970 | // Block children not allowed for this parent type  | 
            ||
| 971 | $parent = isset($context['Parent']) ? $context['Parent'] : null;  | 
            ||
| 972 | 		if($parent && !in_array(get_class($this), $parent->allowedChildren())) { | 
            ||
| 973 | return false;  | 
            ||
| 974 | }  | 
            ||
| 975 | |||
| 976 | // Standard mechanism for accepting permission changes from extensions  | 
            ||
| 977 | $extended = $this->extendedCan(__FUNCTION__, $member, $context);  | 
            ||
| 978 | 		if($extended !== null) { | 
            ||
| 979 | return $extended;  | 
            ||
| 980 | }  | 
            ||
| 981 | |||
| 982 | // Check permission  | 
            ||
| 983 | 		if($member && Permission::checkMember($member, "ADMIN")) { | 
            ||
| 984 | return true;  | 
            ||
| 985 | }  | 
            ||
| 986 | |||
| 987 | // Fall over to inherited permissions  | 
            ||
| 988 | 		if($parent) { | 
            ||
| 989 | return $parent->canAddChildren($member);  | 
            ||
| 990 | 		} else { | 
            ||
| 991 | // This doesn't necessarily mean we are creating a root page, but that  | 
            ||
| 992 | // we don't know if there is a parent, so default to this permission  | 
            ||
| 993 | return SiteConfig::current_site_config()->canCreateTopLevel($member);  | 
            ||
| 994 | }  | 
            ||
| 995 | }  | 
            ||
| 996 | |||
| 997 | /**  | 
            ||
| 998 | * This function should return true if the current user can edit this page. It can be overloaded to customise the  | 
            ||
| 999 | * security model for an application.  | 
            ||
| 1000 | *  | 
            ||
| 1001 | * Denies permission if any of the following conditions is true:  | 
            ||
| 1002 | * - canEdit() on any extension returns false  | 
            ||
| 1003 | * - canView() return false  | 
            ||
| 1004 | * - "CanEditType" directive is set to "Inherit" and any parent page return false for canEdit()  | 
            ||
| 1005 | * - "CanEditType" directive is set to "LoggedInUsers" and no user is logged in or doesn't have the  | 
            ||
| 1006 | * CMS_Access_CMSMAIN permission code  | 
            ||
| 1007 | * - "CanEditType" directive is set to "OnlyTheseUsers" and user is not in the given groups  | 
            ||
| 1008 | *  | 
            ||
| 1009 | * @uses canView()  | 
            ||
| 1010 | * @uses EditorGroups()  | 
            ||
| 1011 | * @uses DataExtension->canEdit()  | 
            ||
| 1012 | *  | 
            ||
| 1013 | * @param Member $member Set to false if you want to explicitly test permissions without a valid user (useful for  | 
            ||
| 1014 | * unit tests)  | 
            ||
| 1015 | * @return bool True if the current user can edit this page  | 
            ||
| 1016 | */  | 
            ||
| 1017 | 	public function canEdit($member = null) { | 
            ||
| 1046 | |||
| 1047 | /**  | 
            ||
| 1048 | * Stub method to get the site config, unless the current class can provide an alternate.  | 
            ||
| 1049 | *  | 
            ||
| 1050 | * @return SiteConfig  | 
            ||
| 1051 | */  | 
            ||
| 1052 | 	public function getSiteConfig() { | 
            ||
| 1053 | 		$configs = $this->invokeWithExtensions('alternateSiteConfig'); | 
            ||
| 1054 | 		foreach(array_filter($configs) as $config) { | 
            ||
| 1055 | return $config;  | 
            ||
| 1056 | }  | 
            ||
| 1057 | |||
| 1058 | return SiteConfig::current_site_config();  | 
            ||
| 1059 | }  | 
            ||
| 1060 | |||
| 1061 | /**  | 
            ||
| 1062 | * Pre-populate the cache of canEdit, canView, canDelete, canPublish permissions. This method will use the static  | 
            ||
| 1063 | * can_(perm)_multiple method for efficiency.  | 
            ||
| 1064 | *  | 
            ||
| 1065 | * @param string $permission The permission: edit, view, publish, approve, etc.  | 
            ||
| 1066 | * @param array $ids An array of page IDs  | 
            ||
| 1067 | * @param callable|string $batchCallback The function/static method to call to calculate permissions. Defaults  | 
            ||
| 1068 | * to 'SiteTree::can_(permission)_multiple'  | 
            ||
| 1069 | */  | 
            ||
| 1070 | 	static public function prepopulate_permission_cache($permission = 'CanEditType', $ids, $batchCallback = null) { | 
            ||
| 1080 | |||
| 1081 | /**  | 
            ||
| 1082 | 	 * This method is NOT a full replacement for the individual can*() methods, e.g. {@link canEdit()}. Rather than | 
            ||
| 1083 | * checking (potentially slow) PHP logic, it relies on the database group associations, e.g. the "CanEditType" field  | 
            ||
| 1084 | * plus the "SiteTree_EditorGroups" many-many table. By batch checking multiple records, we can combine the queries  | 
            ||
| 1085 | * efficiently.  | 
            ||
| 1086 | *  | 
            ||
| 1087 | 	 * Caches based on $typeField data. To invalidate the cache, use {@link SiteTree::reset()} or set the $useCached | 
            ||
| 1088 | * property to FALSE.  | 
            ||
| 1089 | *  | 
            ||
| 1090 | 	 * @param array  $ids              Of {@link SiteTree} IDs | 
            ||
| 1091 | * @param int $memberID Member ID  | 
            ||
| 1092 | * @param string $typeField A property on the data record, e.g. "CanEditType".  | 
            ||
| 1093 | * @param string $groupJoinTable A many-many table name on this record, e.g. "SiteTree_EditorGroups"  | 
            ||
| 1094 | 	 * @param string $siteConfigMethod Method to call on {@link SiteConfig} for toplevel items, e.g. "canEdit" | 
            ||
| 1095 | * @param string $globalPermission If the member doesn't have this permission code, don't bother iterating deeper  | 
            ||
| 1096 | * @param bool $useCached  | 
            ||
| 1097 | 	 * @return array An map of {@link SiteTree} ID keys to boolean values | 
            ||
| 1098 | */  | 
            ||
| 1099 | public static function batch_permission_check($ids, $memberID, $typeField, $groupJoinTable, $siteConfigMethod,  | 
            ||
| 1222 | |||
| 1223 | /**  | 
            ||
| 1224 | * Get the 'can edit' information for a number of SiteTree pages.  | 
            ||
| 1225 | *  | 
            ||
| 1226 | * @param array $ids An array of IDs of the SiteTree pages to look up  | 
            ||
| 1227 | * @param int $memberID ID of member  | 
            ||
| 1228 | * @param bool $useCached Return values from the permission cache if they exist  | 
            ||
| 1229 | * @return array A map where the IDs are keys and the values are booleans stating whether the given page can be  | 
            ||
| 1230 | * edited  | 
            ||
| 1231 | */  | 
            ||
| 1232 | 	static public function can_edit_multiple($ids, $memberID, $useCached = true) { | 
            ||
| 1235 | |||
| 1236 | /**  | 
            ||
| 1237 | * Get the 'can edit' information for a number of SiteTree pages.  | 
            ||
| 1238 | *  | 
            ||
| 1239 | * @param array $ids An array of IDs of the SiteTree pages to look up  | 
            ||
| 1240 | * @param int $memberID ID of member  | 
            ||
| 1241 | * @param bool $useCached Return values from the permission cache if they exist  | 
            ||
| 1242 | * @return array  | 
            ||
| 1243 | */  | 
            ||
| 1244 | 	static public function can_delete_multiple($ids, $memberID, $useCached = true) { | 
            ||
| 1301 | |||
| 1302 | /**  | 
            ||
| 1303 | * Collate selected descendants of this page.  | 
            ||
| 1304 | *  | 
            ||
| 1305 | 	 * {@link $condition} will be evaluated on each descendant, and if it is succeeds, that item will be added to the | 
            ||
| 1306 | * $collator array.  | 
            ||
| 1307 | *  | 
            ||
| 1308 | * @param string $condition The PHP condition to be evaluated. The page will be called $item  | 
            ||
| 1309 | * @param array $collator An array, passed by reference, to collect all of the matching descendants.  | 
            ||
| 1310 | * @return bool  | 
            ||
| 1311 | */  | 
            ||
| 1312 | 	public function collateDescendants($condition, &$collator) { | 
            ||
| 1325 | |||
| 1326 | /**  | 
            ||
| 1327 | * Return the title, description, keywords and language metatags.  | 
            ||
| 1328 | *  | 
            ||
| 1329 | * @todo Move <title> tag in separate getter for easier customization and more obvious usage  | 
            ||
| 1330 | *  | 
            ||
| 1331 | * @param bool $includeTitle Show default <title>-tag, set to false for custom templating  | 
            ||
| 1332 | * @return string The XHTML metatags  | 
            ||
| 1333 | */  | 
            ||
| 1334 | 	public function MetaTags($includeTitle = true) { | 
            ||
| 1384 | |||
| 1385 | /**  | 
            ||
| 1386 | * Returns the object that contains the content that a user would associate with this page.  | 
            ||
| 1387 | *  | 
            ||
| 1388 | * Ordinarily, this is just the page itself, but for example on RedirectorPages or VirtualPages ContentSource() will  | 
            ||
| 1389 | * return the page that is linked to.  | 
            ||
| 1390 | *  | 
            ||
| 1391 | * @return $this  | 
            ||
| 1392 | */  | 
            ||
| 1393 | 	public function ContentSource() { | 
            ||
| 1396 | |||
| 1397 | /**  | 
            ||
| 1398 | * Add default records to database.  | 
            ||
| 1399 | *  | 
            ||
| 1400 | * This function is called whenever the database is built, after the database tables have all been created. Overload  | 
            ||
| 1401 | * this to add default records when the database is built, but make sure you call parent::requireDefaultRecords().  | 
            ||
| 1402 | */  | 
            ||
| 1403 | 	public function requireDefaultRecords() { | 
            ||
| 1452 | |||
| 1453 | 	protected function onBeforeWrite() { | 
            ||
| 1503 | |||
| 1504 | /**  | 
            ||
| 1505 | * Trigger synchronisation of link tracking  | 
            ||
| 1506 | *  | 
            ||
| 1507 | 	 * {@see SiteTreeLinkTracking::augmentSyncLinkTracking} | 
            ||
| 1508 | */  | 
            ||
| 1509 | 	public function syncLinkTracking() { | 
            ||
| 1512 | |||
| 1513 | 	public function onBeforeDelete() { | 
            ||
| 1523 | |||
| 1524 | 	public function onAfterDelete() { | 
            ||
| 1537 | |||
| 1538 | 	public function flushCache($persistent = true) { | 
            ||
| 1542 | |||
| 1543 | 	public function validate() { | 
            ||
| 1580 | |||
| 1581 | /**  | 
            ||
| 1582 | * Returns true if this object has a URLSegment value that does not conflict with any other objects. This method  | 
            ||
| 1583 | * checks for:  | 
            ||
| 1584 | * - A page with the same URLSegment that has a conflict  | 
            ||
| 1585 | * - Conflicts with actions on the parent page  | 
            ||
| 1586 | * - A conflict caused by a root page having the same URLSegment as a class name  | 
            ||
| 1587 | *  | 
            ||
| 1588 | * @return bool  | 
            ||
| 1589 | */  | 
            ||
| 1590 | 	public function validURLSegment() { | 
            ||
| 1624 | |||
| 1625 | /**  | 
            ||
| 1626 | * Generate a URL segment based on the title provided.  | 
            ||
| 1627 | *  | 
            ||
| 1628 | 	 * If {@link Extension}s wish to alter URL segment generation, they can do so by defining | 
            ||
| 1629 | * updateURLSegment(&$url, $title). $url will be passed by reference and should be modified. $title will contain  | 
            ||
| 1630 | * the title that was originally used as the source of this generated URL. This lets extensions either start from  | 
            ||
| 1631 | * scratch, or incrementally modify the generated URL.  | 
            ||
| 1632 | *  | 
            ||
| 1633 | * @param string $title Page title  | 
            ||
| 1634 | * @return string Generated url segment  | 
            ||
| 1635 | */  | 
            ||
| 1636 | 	public function generateURLSegment($title){ | 
            ||
| 1648 | |||
| 1649 | /**  | 
            ||
| 1650 | * Gets the URL segment for the latest draft version of this page.  | 
            ||
| 1651 | *  | 
            ||
| 1652 | * @return string  | 
            ||
| 1653 | */  | 
            ||
| 1654 | 	public function getStageURLSegment() { | 
            ||
| 1660 | |||
| 1661 | /**  | 
            ||
| 1662 | * Gets the URL segment for the currently published version of this page.  | 
            ||
| 1663 | *  | 
            ||
| 1664 | * @return string  | 
            ||
| 1665 | */  | 
            ||
| 1666 | 	public function getLiveURLSegment() { | 
            ||
| 1672 | |||
| 1673 | /**  | 
            ||
| 1674 | * Returns the pages that depend on this page. This includes virtual pages, pages that link to it, etc.  | 
            ||
| 1675 | *  | 
            ||
| 1676 | * @param bool $includeVirtuals Set to false to exlcude virtual pages.  | 
            ||
| 1677 | * @return ArrayList  | 
            ||
| 1678 | */  | 
            ||
| 1679 | 	public function DependentPages($includeVirtuals = true) { | 
            ||
| 1729 | |||
| 1730 | /**  | 
            ||
| 1731 | * Return all virtual pages that link to this page.  | 
            ||
| 1732 | *  | 
            ||
| 1733 | * @return DataList  | 
            ||
| 1734 | */  | 
            ||
| 1735 | 	public function VirtualPages() { | 
            ||
| 1745 | |||
| 1746 | /**  | 
            ||
| 1747 | * Returns a FieldList with which to create the main editing form.  | 
            ||
| 1748 | *  | 
            ||
| 1749 | * You can override this in your child classes to add extra fields - first get the parent fields using  | 
            ||
| 1750 | * parent::getCMSFields(), then use addFieldToTab() on the FieldList.  | 
            ||
| 1751 | *  | 
            ||
| 1752 | 	 * See {@link getSettingsFields()} for a different set of fields concerned with configuration aspects on the record, | 
            ||
| 1753 | * e.g. access control.  | 
            ||
| 1754 | *  | 
            ||
| 1755 | * @return FieldList The fields to be displayed in the CMS  | 
            ||
| 1756 | */  | 
            ||
| 1757 | 	public function getCMSFields() { | 
            ||
| 1937 | |||
| 1938 | |||
| 1939 | /**  | 
            ||
| 1940 | 	 * Returns fields related to configuration aspects on this record, e.g. access control. See {@link getCMSFields()} | 
            ||
| 1941 | * for content-related fields.  | 
            ||
| 1942 | *  | 
            ||
| 1943 | * @return FieldList  | 
            ||
| 1944 | */  | 
            ||
| 1945 | 	public function getSettingsFields() { | 
            ||
| 2051 | |||
| 2052 | /**  | 
            ||
| 2053 | * @param bool $includerelations A boolean value to indicate if the labels returned should include relation fields  | 
            ||
| 2054 | * @return array  | 
            ||
| 2055 | */  | 
            ||
| 2056 | 	public function fieldLabels($includerelations = true) { | 
            ||
| 2094 | |||
| 2095 | /**  | 
            ||
| 2096 | * Get the actions available in the CMS for this page - eg Save, Publish.  | 
            ||
| 2097 | *  | 
            ||
| 2098 | * Frontend scripts and styles know how to handle the following FormFields:  | 
            ||
| 2099 | * - top-level FormActions appear as standalone buttons  | 
            ||
| 2100 | * - top-level CompositeField with FormActions within appear as grouped buttons  | 
            ||
| 2101 | * - TabSet & Tabs appear as a drop ups  | 
            ||
| 2102 | * - FormActions within the Tab are restyled as links  | 
            ||
| 2103 | * - major actions can provide alternate states for richer presentation (see ssui.button widget extension)  | 
            ||
| 2104 | *  | 
            ||
| 2105 | * @return FieldList The available actions for this page.  | 
            ||
| 2106 | */  | 
            ||
| 2107 | 	public function getCMSActions() { | 
            ||
| 2255 | |||
| 2256 | 	public function onAfterPublish() { | 
            ||
| 2264 | |||
| 2265 | /**  | 
            ||
| 2266 | * Update draft dependant pages  | 
            ||
| 2267 | */  | 
            ||
| 2268 | 	public function onAfterRevertToLive() { | 
            ||
| 2280 | |||
| 2281 | /**  | 
            ||
| 2282 | * Determine if this page references a parent which is archived, and not available in stage  | 
            ||
| 2283 | *  | 
            ||
| 2284 | * @return bool True if there is an archived parent  | 
            ||
| 2285 | */  | 
            ||
| 2286 | 	protected function isParentArchived() { | 
            ||
| 2295 | |||
| 2296 | /**  | 
            ||
| 2297 | * Restore the content in the active copy of this SiteTree page to the stage site.  | 
            ||
| 2298 | *  | 
            ||
| 2299 | * @return self  | 
            ||
| 2300 | */  | 
            ||
| 2301 | 	public function doRestoreToStage() { | 
            ||
| 2337 | |||
| 2338 | /**  | 
            ||
| 2339 | * Check if this page is new - that is, if it has yet to have been written to the database.  | 
            ||
| 2340 | *  | 
            ||
| 2341 | * @return bool  | 
            ||
| 2342 | */  | 
            ||
| 2343 | 	public function isNew() { | 
            ||
| 2354 | |||
| 2355 | /**  | 
            ||
| 2356 | * Get the class dropdown used in the CMS to change the class of a page. This returns the list of options in the  | 
            ||
| 2357 | 	 * dropdown as a Map from class name to singular name. Filters by {@link SiteTree->canCreate()}, as well as | 
            ||
| 2358 | 	 * {@link SiteTree::$needs_permission}. | 
            ||
| 2359 | *  | 
            ||
| 2360 | * @return array  | 
            ||
| 2361 | */  | 
            ||
| 2362 | 	protected function getClassDropdown() { | 
            ||
| 2406 | |||
| 2407 | /**  | 
            ||
| 2408 | * Returns an array of the class names of classes that are allowed to be children of this class.  | 
            ||
| 2409 | *  | 
            ||
| 2410 | * @return string[]  | 
            ||
| 2411 | */  | 
            ||
| 2412 | 	public function allowedChildren() { | 
            ||
| 2432 | |||
| 2433 | /**  | 
            ||
| 2434 | * Returns the class name of the default class for children of this page.  | 
            ||
| 2435 | *  | 
            ||
| 2436 | * @return string  | 
            ||
| 2437 | */  | 
            ||
| 2438 | 	public function defaultChild() { | 
            ||
| 2447 | |||
| 2448 | /**  | 
            ||
| 2449 | * Returns the class name of the default class for the parent of this page.  | 
            ||
| 2450 | *  | 
            ||
| 2451 | * @return string  | 
            ||
| 2452 | */  | 
            ||
| 2453 | 	public function defaultParent() { | 
            ||
| 2456 | |||
| 2457 | /**  | 
            ||
| 2458 | * Get the title for use in menus for this page. If the MenuTitle field is set it returns that, else it returns the  | 
            ||
| 2459 | * Title field.  | 
            ||
| 2460 | *  | 
            ||
| 2461 | * @return string  | 
            ||
| 2462 | */  | 
            ||
| 2463 | 	public function getMenuTitle(){ | 
            ||
| 2470 | |||
| 2471 | |||
| 2472 | /**  | 
            ||
| 2473 | * Set the menu title for this page.  | 
            ||
| 2474 | *  | 
            ||
| 2475 | * @param string $value  | 
            ||
| 2476 | */  | 
            ||
| 2477 | 	public function setMenuTitle($value) { | 
            ||
| 2484 | |||
| 2485 | /**  | 
            ||
| 2486 | * A flag provides the user with additional data about the current page status, for example a "removed from draft"  | 
            ||
| 2487 | * status. Each page can have more than one status flag. Returns a map of a unique key to a (localized) title for  | 
            ||
| 2488 | * the flag. The unique key can be reused as a CSS class. Use the 'updateStatusFlags' extension point to customize  | 
            ||
| 2489 | * the flags.  | 
            ||
| 2490 | *  | 
            ||
| 2491 | * Example (simple):  | 
            ||
| 2492 | * "deletedonlive" => "Deleted"  | 
            ||
| 2493 | *  | 
            ||
| 2494 | * Example (with optional title attribute):  | 
            ||
| 2495 | 	 *   "deletedonlive" => array('text' => "Deleted", 'title' => 'This page has been deleted') | 
            ||
| 2496 | *  | 
            ||
| 2497 | * @param bool $cached Whether to serve the fields from cache; false regenerate them  | 
            ||
| 2498 | * @return array  | 
            ||
| 2499 | */  | 
            ||
| 2500 | 	public function getStatusFlags($cached = true) { | 
            ||
| 2534 | |||
| 2535 | /**  | 
            ||
| 2536 | * getTreeTitle will return three <span> html DOM elements, an empty <span> with the class 'jstree-pageicon' in  | 
            ||
| 2537 | * front, following by a <span> wrapping around its MenutTitle, then following by a <span> indicating its  | 
            ||
| 2538 | * publication status.  | 
            ||
| 2539 | *  | 
            ||
| 2540 | * @return string An HTML string ready to be directly used in a template  | 
            ||
| 2541 | */  | 
            ||
| 2542 | 	public function getTreeTitle() { | 
            ||
| 2571 | |||
| 2572 | /**  | 
            ||
| 2573 | * Returns the page in the current page stack of the given level. Level(1) will return the main menu item that  | 
            ||
| 2574 | * we're currently inside, etc.  | 
            ||
| 2575 | *  | 
            ||
| 2576 | * @param int $level  | 
            ||
| 2577 | * @return SiteTree  | 
            ||
| 2578 | */  | 
            ||
| 2579 | 	public function Level($level) { | 
            ||
| 2588 | |||
| 2589 | /**  | 
            ||
| 2590 | * Gets the depth of this page in the sitetree, where 1 is the root level  | 
            ||
| 2591 | *  | 
            ||
| 2592 | * @return int  | 
            ||
| 2593 | */  | 
            ||
| 2594 | 	public function getPageLevel() { | 
            ||
| 2600 | |||
| 2601 | /**  | 
            ||
| 2602 | * Return the CSS classes to apply to this node in the CMS tree.  | 
            ||
| 2603 | *  | 
            ||
| 2604 | * @param string $numChildrenMethod  | 
            ||
| 2605 | * @return string  | 
            ||
| 2606 | */  | 
            ||
| 2607 | 	public function CMSTreeClasses($numChildrenMethod="numChildren") { | 
            ||
| 2638 | |||
| 2639 | /**  | 
            ||
| 2640 | * Compares current draft with live version, and returns true if no draft version of this page exists but the page  | 
            ||
| 2641 | * is still published (eg, after triggering "Delete from draft site" in the CMS).  | 
            ||
| 2642 | *  | 
            ||
| 2643 | * @return bool  | 
            ||
| 2644 | */  | 
            ||
| 2645 | 	public function getIsDeletedFromStage() { | 
            ||
| 2654 | |||
| 2655 | /**  | 
            ||
| 2656 | * Return true if this page exists on the live site  | 
            ||
| 2657 | *  | 
            ||
| 2658 | * @return bool  | 
            ||
| 2659 | */  | 
            ||
| 2660 | 	public function getExistsOnLive() { | 
            ||
| 2663 | |||
| 2664 | /**  | 
            ||
| 2665 | * Compares current draft with live version, and returns true if these versions differ, meaning there have been  | 
            ||
| 2666 | * unpublished changes to the draft site.  | 
            ||
| 2667 | *  | 
            ||
| 2668 | * @return bool  | 
            ||
| 2669 | */  | 
            ||
| 2670 | 	public function getIsModifiedOnStage() { | 
            ||
| 2682 | |||
| 2683 | /**  | 
            ||
| 2684 | * Compares current draft with live version, and returns true if no live version exists, meaning the page was never  | 
            ||
| 2685 | * published.  | 
            ||
| 2686 | *  | 
            ||
| 2687 | * @return bool  | 
            ||
| 2688 | */  | 
            ||
| 2689 | 	public function getIsAddedToStage() { | 
            ||
| 2698 | |||
| 2699 | /**  | 
            ||
| 2700 | * Stops extendCMSFields() being called on getCMSFields(). This is useful when you need access to fields added by  | 
            ||
| 2701 | * subclasses of SiteTree in a extension. Call before calling parent::getCMSFields(), and reenable afterwards.  | 
            ||
| 2702 | */  | 
            ||
| 2703 | 	static public function disableCMSFieldsExtensions() { | 
            ||
| 2706 | |||
| 2707 | /**  | 
            ||
| 2708 | * Reenables extendCMSFields() being called on getCMSFields() after it has been disabled by  | 
            ||
| 2709 | * disableCMSFieldsExtensions().  | 
            ||
| 2710 | */  | 
            ||
| 2711 | 	static public function enableCMSFieldsExtensions() { | 
            ||
| 2714 | |||
| 2715 | 	public function providePermissions() { | 
            ||
| 2749 | |||
| 2750 | /**  | 
            ||
| 2751 | * Return the translated Singular name.  | 
            ||
| 2752 | *  | 
            ||
| 2753 | * @return string  | 
            ||
| 2754 | */  | 
            ||
| 2755 | 	public function i18n_singular_name() { | 
            ||
| 2760 | |||
| 2761 | /**  | 
            ||
| 2762 | * Overloaded to also provide entities for 'Page' class which is usually located in custom code, hence textcollector  | 
            ||
| 2763 | * picks it up for the wrong folder.  | 
            ||
| 2764 | *  | 
            ||
| 2765 | * @return array  | 
            ||
| 2766 | */  | 
            ||
| 2767 | 	public function provideI18nEntities() { | 
            ||
| 2783 | |||
| 2784 | /**  | 
            ||
| 2785 | * Returns 'root' if the current page has no parent, or 'subpage' otherwise  | 
            ||
| 2786 | *  | 
            ||
| 2787 | * @return string  | 
            ||
| 2788 | */  | 
            ||
| 2789 | 	public function getParentType() { | 
            ||
| 2792 | |||
| 2793 | /**  | 
            ||
| 2794 | * Clear the permissions cache for SiteTree  | 
            ||
| 2795 | */  | 
            ||
| 2796 | 	public static function reset() { | 
            ||
| 2799 | |||
| 2800 | 	static public function on_db_reset() { | 
            ||
| 2803 | |||
| 2804 | }  | 
            ||
| 2805 | 
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: