Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like SiteTree often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SiteTree, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 36 | class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvider,CMSPreviewable { | 
            ||
| 37 | |||
| 38 | /**  | 
            ||
| 39 | * Indicates what kind of children this page type can have.  | 
            ||
| 40 | * This can be an array of allowed child classes, or the string "none" -  | 
            ||
| 41 | * indicating that this page type can't have children.  | 
            ||
| 42 | * If a classname is prefixed by "*", such as "*Page", then only that  | 
            ||
| 43 | * class is allowed - no subclasses. Otherwise, the class and all its  | 
            ||
| 44 | * subclasses are allowed.  | 
            ||
| 45 | 	 * To control allowed children on root level (no parent), use {@link $can_be_root}. | 
            ||
| 46 | *  | 
            ||
| 47 | * Note that this setting is cached when used in the CMS, use the "flush" query parameter to clear it.  | 
            ||
| 48 | *  | 
            ||
| 49 | * @config  | 
            ||
| 50 | * @var array  | 
            ||
| 51 | */  | 
            ||
| 52 | 	private static $allowed_children = array("SiteTree"); | 
            ||
| 53 | |||
| 54 | /**  | 
            ||
| 55 | * The default child class for this page.  | 
            ||
| 56 | 	 * Note: Value might be cached, see {@link $allowed_chilren}. | 
            ||
| 57 | *  | 
            ||
| 58 | * @config  | 
            ||
| 59 | * @var string  | 
            ||
| 60 | */  | 
            ||
| 61 | private static $default_child = "Page";  | 
            ||
| 62 | |||
| 63 | /**  | 
            ||
| 64 | * Default value for SiteTree.ClassName enum  | 
            ||
| 65 | 	 * {@see DBClassName::getDefault} | 
            ||
| 66 | *  | 
            ||
| 67 | * @config  | 
            ||
| 68 | * @var string  | 
            ||
| 69 | */  | 
            ||
| 70 | private static $default_classname = "Page";  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 71 | |||
| 72 | /**  | 
            ||
| 73 | * The default parent class for this page.  | 
            ||
| 74 | 	 * Note: Value might be cached, see {@link $allowed_chilren}. | 
            ||
| 75 | *  | 
            ||
| 76 | * @config  | 
            ||
| 77 | * @var string  | 
            ||
| 78 | */  | 
            ||
| 79 | private static $default_parent = null;  | 
            ||
| 80 | |||
| 81 | /**  | 
            ||
| 82 | * Controls whether a page can be in the root of the site tree.  | 
            ||
| 83 | 	 * Note: Value might be cached, see {@link $allowed_chilren}. | 
            ||
| 84 | *  | 
            ||
| 85 | * @config  | 
            ||
| 86 | * @var bool  | 
            ||
| 87 | */  | 
            ||
| 88 | private static $can_be_root = true;  | 
            ||
| 89 | |||
| 90 | /**  | 
            ||
| 91 | * List of permission codes a user can have to allow a user to create a page of this type.  | 
            ||
| 92 | 	 * Note: Value might be cached, see {@link $allowed_chilren}. | 
            ||
| 93 | *  | 
            ||
| 94 | * @config  | 
            ||
| 95 | * @var array  | 
            ||
| 96 | */  | 
            ||
| 97 | private static $need_permission = null;  | 
            ||
| 98 | |||
| 99 | /**  | 
            ||
| 100 | * If you extend a class, and don't want to be able to select the old class  | 
            ||
| 101 | * in the cms, set this to the old class name. Eg, if you extended Product  | 
            ||
| 102 | * to make ImprovedProduct, then you would set $hide_ancestor to Product.  | 
            ||
| 103 | *  | 
            ||
| 104 | * @config  | 
            ||
| 105 | * @var string  | 
            ||
| 106 | */  | 
            ||
| 107 | private static $hide_ancestor = null;  | 
            ||
| 108 | |||
| 109 | private static $db = array(  | 
            ||
| 110 | "URLSegment" => "Varchar(255)",  | 
            ||
| 111 | "Title" => "Varchar(255)",  | 
            ||
| 112 | "MenuTitle" => "Varchar(100)",  | 
            ||
| 113 | "Content" => "HTMLText",  | 
            ||
| 114 | "MetaDescription" => "Text",  | 
            ||
| 115 | 		"ExtraMeta" => "HTMLText('meta, link')", | 
            ||
| 116 | "ShowInMenus" => "Boolean",  | 
            ||
| 117 | "ShowInSearch" => "Boolean",  | 
            ||
| 118 | "Sort" => "Int",  | 
            ||
| 119 | "HasBrokenFile" => "Boolean",  | 
            ||
| 120 | "HasBrokenLink" => "Boolean",  | 
            ||
| 121 | "ReportClass" => "Varchar",  | 
            ||
| 122 | 		"CanViewType" => "Enum('Anyone, LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit')", | 
            ||
| 123 | 		"CanEditType" => "Enum('LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit')", | 
            ||
| 124 | );  | 
            ||
| 125 | |||
| 126 | private static $indexes = array(  | 
            ||
| 127 | "URLSegment" => true,  | 
            ||
| 128 | );  | 
            ||
| 129 | |||
| 130 | private static $many_many = array(  | 
            ||
| 131 | "ViewerGroups" => "Group",  | 
            ||
| 132 | "EditorGroups" => "Group",  | 
            ||
| 133 | );  | 
            ||
| 134 | |||
| 135 | private static $casting = array(  | 
            ||
| 136 | "Breadcrumbs" => "HTMLText",  | 
            ||
| 137 | "LastEdited" => "SS_Datetime",  | 
            ||
| 138 | "Created" => "SS_Datetime",  | 
            ||
| 139 | 'Link' => 'Text',  | 
            ||
| 140 | 'RelativeLink' => 'Text',  | 
            ||
| 141 | 'AbsoluteLink' => 'Text',  | 
            ||
| 142 | 'TreeTitle' => 'HTMLText',  | 
            ||
| 143 | );  | 
            ||
| 144 | |||
| 145 | private static $defaults = array(  | 
            ||
| 146 | "ShowInMenus" => 1,  | 
            ||
| 147 | "ShowInSearch" => 1,  | 
            ||
| 148 | "CanViewType" => "Inherit",  | 
            ||
| 149 | "CanEditType" => "Inherit"  | 
            ||
| 150 | );  | 
            ||
| 151 | |||
| 152 | private static $versioning = array(  | 
            ||
| 153 | "Stage", "Live"  | 
            ||
| 154 | );  | 
            ||
| 155 | |||
| 156 | private static $default_sort = "\"Sort\"";  | 
            ||
| 157 | |||
| 158 | /**  | 
            ||
| 159 | * If this is false, the class cannot be created in the CMS by regular content authors, only by ADMINs.  | 
            ||
| 160 | * @var boolean  | 
            ||
| 161 | * @config  | 
            ||
| 162 | */  | 
            ||
| 163 | private static $can_create = true;  | 
            ||
| 164 | |||
| 165 | /**  | 
            ||
| 166 | * Icon to use in the CMS page tree. This should be the full filename, relative to the webroot.  | 
            ||
| 167 | * Also supports custom CSS rule contents (applied to the correct selector for the tree UI implementation).  | 
            ||
| 168 | *  | 
            ||
| 169 | * @see CMSMain::generateTreeStylingCSS()  | 
            ||
| 170 | * @config  | 
            ||
| 171 | * @var string  | 
            ||
| 172 | */  | 
            ||
| 173 | private static $icon = null;  | 
            ||
| 174 | |||
| 175 | /**  | 
            ||
| 176 | * @config  | 
            ||
| 177 | * @var string Description of the class functionality, typically shown to a user  | 
            ||
| 178 | 	 * when selecting which page type to create. Translated through {@link provideI18nEntities()}. | 
            ||
| 179 | */  | 
            ||
| 180 | private static $description = 'Generic content page';  | 
            ||
| 181 | |||
| 182 | private static $extensions = array(  | 
            ||
| 183 | "Hierarchy",  | 
            ||
| 184 | 		"Versioned('Stage', 'Live')", | 
            ||
| 185 | "SiteTreeLinkTracking"  | 
            ||
| 186 | );  | 
            ||
| 187 | |||
| 188 | private static $searchable_fields = array(  | 
            ||
| 189 | 'Title',  | 
            ||
| 190 | 'Content',  | 
            ||
| 191 | );  | 
            ||
| 192 | |||
| 193 | private static $field_labels = array(  | 
            ||
| 194 | 'URLSegment' => 'URL'  | 
            ||
| 195 | );  | 
            ||
| 196 | |||
| 197 | /**  | 
            ||
| 198 | * @config  | 
            ||
| 199 | */  | 
            ||
| 200 | private static $nested_urls = true;  | 
            ||
| 201 | |||
| 202 | /**  | 
            ||
| 203 | * @config  | 
            ||
| 204 | */  | 
            ||
| 205 | private static $create_default_pages = true;  | 
            ||
| 206 | |||
| 207 | /**  | 
            ||
| 208 | * This controls whether of not extendCMSFields() is called by getCMSFields.  | 
            ||
| 209 | */  | 
            ||
| 210 | private static $runCMSFieldsExtensions = true;  | 
            ||
| 211 | |||
| 212 | /**  | 
            ||
| 213 | * Cache for canView/Edit/Publish/Delete permissions.  | 
            ||
| 214 | * Keyed by permission type (e.g. 'edit'), with an array  | 
            ||
| 215 | * of IDs mapped to their boolean permission ability (true=allow, false=deny).  | 
            ||
| 216 | 	 * See {@link batch_permission_check()} for details. | 
            ||
| 217 | */  | 
            ||
| 218 | private static $cache_permissions = array();  | 
            ||
| 219 | |||
| 220 | /**  | 
            ||
| 221 | * @config  | 
            ||
| 222 | * @var boolean  | 
            ||
| 223 | */  | 
            ||
| 224 | private static $enforce_strict_hierarchy = true;  | 
            ||
| 225 | |||
| 226 | /**  | 
            ||
| 227 | * The value used for the meta generator tag. Leave blank to omit the tag.  | 
            ||
| 228 | *  | 
            ||
| 229 | * @config  | 
            ||
| 230 | * @var string  | 
            ||
| 231 | */  | 
            ||
| 232 | private static $meta_generator = 'SilverStripe - http://silverstripe.org';  | 
            ||
| 233 | |||
| 234 | protected $_cache_statusFlags = null;  | 
            ||
| 235 | |||
| 236 | /**  | 
            ||
| 237 | * Determines if the system should avoid orphaned pages  | 
            ||
| 238 | * by deleting all children when the their parent is deleted (TRUE),  | 
            ||
| 239 | * or rather preserve this data even if its not reachable through any navigation path (FALSE).  | 
            ||
| 240 | *  | 
            ||
| 241 | * @deprecated 4.0 Use the "SiteTree.enforce_strict_hierarchy" config setting instead  | 
            ||
| 242 | * @param boolean  | 
            ||
| 243 | */  | 
            ||
| 244 | 	static public function set_enforce_strict_hierarchy($to) { | 
            ||
| 248 | |||
| 249 | /**  | 
            ||
| 250 | * @deprecated 4.0 Use the "SiteTree.enforce_strict_hierarchy" config setting instead  | 
            ||
| 251 | * @return boolean  | 
            ||
| 252 | */  | 
            ||
| 253 | 	static public function get_enforce_strict_hierarchy() { | 
            ||
| 257 | |||
| 258 | /**  | 
            ||
| 259 | * Returns TRUE if nested URLs (e.g. page/sub-page/) are currently enabled on this site.  | 
            ||
| 260 | *  | 
            ||
| 261 | * @deprecated 4.0 Use the "SiteTree.nested_urls" config setting instead  | 
            ||
| 262 | * @return bool  | 
            ||
| 263 | */  | 
            ||
| 264 | 	static public function nested_urls() { | 
            ||
| 268 | |||
| 269 | /**  | 
            ||
| 270 | * @deprecated 4.0 Use the "SiteTree.nested_urls" config setting instead  | 
            ||
| 271 | */  | 
            ||
| 272 | 	static public function enable_nested_urls() { | 
            ||
| 276 | |||
| 277 | /**  | 
            ||
| 278 | * @deprecated 4.0 Use the "SiteTree.nested_urls" config setting instead  | 
            ||
| 279 | */  | 
            ||
| 280 | 	static public function disable_nested_urls() { | 
            ||
| 284 | |||
| 285 | /**  | 
            ||
| 286 | * Set the (re)creation of default pages on /dev/build  | 
            ||
| 287 | *  | 
            ||
| 288 | * @deprecated 4.0 Use the "SiteTree.create_default_pages" config setting instead  | 
            ||
| 289 | * @param bool $option  | 
            ||
| 290 | */  | 
            ||
| 291 | 	static public function set_create_default_pages($option = true) { | 
            ||
| 295 | |||
| 296 | /**  | 
            ||
| 297 | * Return true if default pages should be created on /dev/build.  | 
            ||
| 298 | *  | 
            ||
| 299 | * @deprecated 4.0 Use the "SiteTree.create_default_pages" config setting instead  | 
            ||
| 300 | * @return bool  | 
            ||
| 301 | */  | 
            ||
| 302 | 	static public function get_create_default_pages() { | 
            ||
| 306 | |||
| 307 | /**  | 
            ||
| 308 | 	 * Fetches the {@link SiteTree} object that maps to a link. | 
            ||
| 309 | *  | 
            ||
| 310 | 	 * If you have enabled {@link SiteTree::config()->nested_urls} on this site, then you can use a nested link such as | 
            ||
| 311 | * "about-us/staff/", and this function will traverse down the URL chain and grab the appropriate link.  | 
            ||
| 312 | *  | 
            ||
| 313 | * Note that if no model can be found, this method will fall over to a extended alternateGetByLink method provided  | 
            ||
| 314 | 	 * by a extension attached to {@link SiteTree} | 
            ||
| 315 | *  | 
            ||
| 316 | * @param string $link The link of the page to search for  | 
            ||
| 317 | * @param bool $cache True (default) to use caching, false to force a fresh search from the database  | 
            ||
| 318 | * @return SiteTree  | 
            ||
| 319 | */  | 
            ||
| 320 | 	static public function get_by_link($link, $cache = true) { | 
            ||
| 321 | 		if(trim($link, '/')) { | 
            ||
| 322 | $link = trim(Director::makeRelative($link), '/');  | 
            ||
| 323 | 		} else { | 
            ||
| 324 | $link = RootURLController::get_homepage_link();  | 
            ||
| 325 | }  | 
            ||
| 326 | |||
| 327 | 		$parts = preg_split('|/+|', $link); | 
            ||
| 328 | |||
| 329 | // Grab the initial root level page to traverse down from.  | 
            ||
| 330 | $URLSegment = array_shift($parts);  | 
            ||
| 331 | 		$conditions = array('"SiteTree"."URLSegment"' => rawurlencode($URLSegment)); | 
            ||
| 332 | 		if(self::config()->nested_urls) { | 
            ||
| 333 | 			$conditions[] = array('"SiteTree"."ParentID"' => 0); | 
            ||
| 334 | }  | 
            ||
| 335 | 		$sitetree = DataObject::get_one('SiteTree', $conditions, $cache); | 
            ||
| 336 | |||
| 337 | /// Fall back on a unique URLSegment for b/c.  | 
            ||
| 338 | if( !$sitetree  | 
            ||
| 339 | && self::config()->nested_urls  | 
            ||
| 340 | 			&& $page = DataObject::get_one('SiteTree', array( | 
            ||
| 341 | '"SiteTree"."URLSegment"' => $URLSegment  | 
            ||
| 342 | ), $cache)  | 
            ||
| 343 | 		) { | 
            ||
| 344 | return $page;  | 
            ||
| 345 | }  | 
            ||
| 346 | |||
| 347 | // Attempt to grab an alternative page from extensions.  | 
            ||
| 348 | 		if(!$sitetree) { | 
            ||
| 349 | $parentID = self::config()->nested_urls ? 0 : null;  | 
            ||
| 350 | |||
| 351 | View Code Duplication | 			if($alternatives = singleton('SiteTree')->extend('alternateGetByLink', $URLSegment, $parentID)) { | 
            |
| 352 | foreach($alternatives as $alternative) if($alternative) $sitetree = $alternative;  | 
            ||
| 353 | }  | 
            ||
| 354 | |||
| 355 | if(!$sitetree) return false;  | 
            ||
| 356 | }  | 
            ||
| 357 | |||
| 358 | // Check if we have any more URL parts to parse.  | 
            ||
| 359 | if(!self::config()->nested_urls || !count($parts)) return $sitetree;  | 
            ||
| 360 | |||
| 361 | // Traverse down the remaining URL segments and grab the relevant SiteTree objects.  | 
            ||
| 362 | 		foreach($parts as $segment) { | 
            ||
| 363 | 			$next = DataObject::get_one('SiteTree', array( | 
            ||
| 364 | '"SiteTree"."URLSegment"' => $segment,  | 
            ||
| 365 | '"SiteTree"."ParentID"' => $sitetree->ID  | 
            ||
| 366 | ),  | 
            ||
| 367 | $cache  | 
            ||
| 368 | );  | 
            ||
| 369 | |||
| 370 | 			if(!$next) { | 
            ||
| 371 | $parentID = (int) $sitetree->ID;  | 
            ||
| 372 | |||
| 373 | View Code Duplication | 				if($alternatives = singleton('SiteTree')->extend('alternateGetByLink', $segment, $parentID)) { | 
            |
| 374 | foreach($alternatives as $alternative) if($alternative) $next = $alternative;  | 
            ||
| 375 | }  | 
            ||
| 376 | |||
| 377 | if(!$next) return false;  | 
            ||
| 378 | }  | 
            ||
| 379 | |||
| 380 | $sitetree->destroy();  | 
            ||
| 381 | $sitetree = $next;  | 
            ||
| 382 | }  | 
            ||
| 383 | |||
| 384 | return $sitetree;  | 
            ||
| 385 | }  | 
            ||
| 386 | |||
| 387 | /**  | 
            ||
| 388 | 	 * Return a subclass map of SiteTree that shouldn't be hidden through {@link SiteTree::$hide_ancestor} | 
            ||
| 389 | *  | 
            ||
| 390 | * @return array  | 
            ||
| 391 | */  | 
            ||
| 392 | 	public static function page_type_classes() { | 
            ||
| 426 | |||
| 427 | /**  | 
            ||
| 428 | * Replace a "[sitetree_link id=n]" shortcode with a link to the page with the corresponding ID.  | 
            ||
| 429 | *  | 
            ||
| 430 | * @param array $arguments  | 
            ||
| 431 | * @param string $content  | 
            ||
| 432 | * @param TextParser $parser  | 
            ||
| 433 | * @return string  | 
            ||
| 434 | */  | 
            ||
| 435 | 	static public function link_shortcode_handler($arguments, $content = null, $parser = null) { | 
            ||
| 436 | if(!isset($arguments['id']) || !is_numeric($arguments['id'])) return;  | 
            ||
| 437 | |||
| 438 | if (  | 
            ||
| 439 | 			   !($page = DataObject::get_by_id('SiteTree', $arguments['id']))         // Get the current page by ID. | 
            ||
| 440 | 			&& !($page = Versioned::get_latest_version('SiteTree', $arguments['id'])) // Attempt link to old version. | 
            ||
| 441 | 		) { | 
            ||
| 442 | return; // There were no suitable matches at all.  | 
            ||
| 443 | }  | 
            ||
| 444 | |||
| 445 | $link = Convert::raw2att($page->Link());  | 
            ||
| 446 | |||
| 447 | 		if($content) { | 
            ||
| 448 | 			return sprintf('<a href="%s">%s</a>', $link, $parser->parse($content)); | 
            ||
| 449 | 		} else { | 
            ||
| 450 | return $link;  | 
            ||
| 451 | }  | 
            ||
| 452 | }  | 
            ||
| 453 | |||
| 454 | /**  | 
            ||
| 455 | 	 * Return the link for this {@link SiteTree} object, with the {@link Director::baseURL()} included. | 
            ||
| 456 | *  | 
            ||
| 457 | * @param string $action Optional controller action (method).  | 
            ||
| 458 | * Note: URI encoding of this parameter is applied automatically through template casting,  | 
            ||
| 459 | 	 *                       don't encode the passed parameter. Please use {@link Controller::join_links()} instead to | 
            ||
| 460 | * append GET parameters.  | 
            ||
| 461 | * @return string  | 
            ||
| 462 | */  | 
            ||
| 463 | 	public function Link($action = null) { | 
            ||
| 466 | |||
| 467 | /**  | 
            ||
| 468 | * Get the absolute URL for this page, including protocol and host.  | 
            ||
| 469 | *  | 
            ||
| 470 | 	 * @param string $action See {@link Link()} | 
            ||
| 471 | * @return string  | 
            ||
| 472 | */  | 
            ||
| 473 | 	public function AbsoluteLink($action = null) { | 
            ||
| 480 | |||
| 481 | /**  | 
            ||
| 482 | * Base link used for previewing. Defaults to absolute URL, in order to account for domain changes, e.g. on multi  | 
            ||
| 483 | 	 * site setups. Does not contain hints about the stage, see {@link SilverStripeNavigator} for details. | 
            ||
| 484 | *  | 
            ||
| 485 | 	 * @param string $action See {@link Link()} | 
            ||
| 486 | * @return string  | 
            ||
| 487 | */  | 
            ||
| 488 | 	public function PreviewLink($action = null) { | 
            ||
| 495 | |||
| 496 | /**  | 
            ||
| 497 | 	 * Return the link for this {@link SiteTree} object relative to the SilverStripe root. | 
            ||
| 498 | *  | 
            ||
| 499 | * By default, if this page is the current home page, and there is no action specified then this will return a link  | 
            ||
| 500 | * to the root of the site. However, if you set the $action parameter to TRUE then the link will not be rewritten  | 
            ||
| 501 | * and returned in its full form.  | 
            ||
| 502 | *  | 
            ||
| 503 | * @uses RootURLController::get_homepage_link()  | 
            ||
| 504 | *  | 
            ||
| 505 | 	 * @param string $action See {@link Link()} | 
            ||
| 506 | * @return string  | 
            ||
| 507 | */  | 
            ||
| 508 | 	public function RelativeLink($action = null) { | 
            ||
| 509 | 		if($this->ParentID && self::config()->nested_urls) { | 
            ||
| 510 | $parent = $this->Parent();  | 
            ||
| 511 | // If page is removed select parent from version history (for archive page view)  | 
            ||
| 512 | 			if((!$parent || !$parent->exists()) && $this->IsDeletedFromStage) { | 
            ||
| 513 | 				$parent = Versioned::get_latest_version('SiteTree', $this->ParentID); | 
            ||
| 514 | }  | 
            ||
| 515 | $base = $parent->RelativeLink($this->URLSegment);  | 
            ||
| 516 | 		} elseif(!$action && $this->URLSegment == RootURLController::get_homepage_link()) { | 
            ||
| 517 | // Unset base for root-level homepages.  | 
            ||
| 518 | // Note: Homepages with action parameters (or $action === true)  | 
            ||
| 519 | // need to retain their URLSegment.  | 
            ||
| 520 | $base = null;  | 
            ||
| 521 | 		} else { | 
            ||
| 522 | $base = $this->URLSegment;  | 
            ||
| 523 | }  | 
            ||
| 524 | |||
| 525 | 		$this->extend('updateRelativeLink', $base, $action); | 
            ||
| 526 | |||
| 527 | // Legacy support: If $action === true, retain URLSegment for homepages,  | 
            ||
| 528 | // but don't append any action  | 
            ||
| 529 | if($action === true) $action = null;  | 
            ||
| 530 | |||
| 531 | return Controller::join_links($base, '/', $action);  | 
            ||
| 532 | }  | 
            ||
| 533 | |||
| 534 | /**  | 
            ||
| 535 | * Get the absolute URL for this page on the Live site.  | 
            ||
| 536 | *  | 
            ||
| 537 | * @param bool $includeStageEqualsLive Whether to append the URL with ?stage=Live to force Live mode  | 
            ||
| 538 | * @return string  | 
            ||
| 539 | */  | 
            ||
| 540 | 	public function getAbsoluteLiveLink($includeStageEqualsLive = true) { | 
            ||
| 556 | |||
| 557 | /**  | 
            ||
| 558 | * Generates a link to edit this page in the CMS.  | 
            ||
| 559 | *  | 
            ||
| 560 | * @return string  | 
            ||
| 561 | */  | 
            ||
| 562 | 	public function CMSEditLink() { | 
            ||
| 565 | |||
| 566 | |||
| 567 | /**  | 
            ||
| 568 | * Return a CSS identifier generated from this page's link.  | 
            ||
| 569 | *  | 
            ||
| 570 | * @return string The URL segment  | 
            ||
| 571 | */  | 
            ||
| 572 | 	public function ElementName() { | 
            ||
| 575 | |||
| 576 | /**  | 
            ||
| 577 | * Returns true if this is the currently active page being used to handle this request.  | 
            ||
| 578 | *  | 
            ||
| 579 | * @return bool  | 
            ||
| 580 | */  | 
            ||
| 581 | 	public function isCurrent() { | 
            ||
| 584 | |||
| 585 | /**  | 
            ||
| 586 | * Check if this page is in the currently active section (e.g. it is either current or one of its children is  | 
            ||
| 587 | * currently being viewed).  | 
            ||
| 588 | *  | 
            ||
| 589 | * @return bool  | 
            ||
| 590 | */  | 
            ||
| 591 | 	public function isSection() { | 
            ||
| 596 | |||
| 597 | /**  | 
            ||
| 598 | * Check if the parent of this page has been removed (or made otherwise unavailable), and is still referenced by  | 
            ||
| 599 | * this child. Any such orphaned page may still require access via the CMS, but should not be shown as accessible  | 
            ||
| 600 | * to external users.  | 
            ||
| 601 | *  | 
            ||
| 602 | * @return bool  | 
            ||
| 603 | */  | 
            ||
| 604 | 	public function isOrphaned() { | 
            ||
| 612 | |||
| 613 | /**  | 
            ||
| 614 | 	 * Return "link" or "current" depending on if this is the {@link SiteTree::isCurrent()} current page. | 
            ||
| 615 | *  | 
            ||
| 616 | * @return string  | 
            ||
| 617 | */  | 
            ||
| 618 | 	public function LinkOrCurrent() { | 
            ||
| 621 | |||
| 622 | /**  | 
            ||
| 623 | 	 * Return "link" or "section" depending on if this is the {@link SiteTree::isSeciton()} current section. | 
            ||
| 624 | *  | 
            ||
| 625 | * @return string  | 
            ||
| 626 | */  | 
            ||
| 627 | 	public function LinkOrSection() { | 
            ||
| 630 | |||
| 631 | /**  | 
            ||
| 632 | * Return "link", "current" or "section" depending on if this page is the current page, or not on the current page  | 
            ||
| 633 | * but in the current section.  | 
            ||
| 634 | *  | 
            ||
| 635 | * @return string  | 
            ||
| 636 | */  | 
            ||
| 637 | 	public function LinkingMode() { | 
            ||
| 646 | |||
| 647 | /**  | 
            ||
| 648 | * Check if this page is in the given current section.  | 
            ||
| 649 | *  | 
            ||
| 650 | * @param string $sectionName Name of the section to check  | 
            ||
| 651 | * @return bool True if we are in the given section  | 
            ||
| 652 | */  | 
            ||
| 653 | 	public function InSection($sectionName) { | 
            ||
| 662 | |||
| 663 | /**  | 
            ||
| 664 | * Create a duplicate of this node. Doesn't affect joined data - create a custom overloading of this if you need  | 
            ||
| 665 | * such behaviour.  | 
            ||
| 666 | *  | 
            ||
| 667 | * @param bool $doWrite Whether to write the new object before returning it  | 
            ||
| 668 | * @return self The duplicated object  | 
            ||
| 669 | */  | 
            ||
| 670 | 	 public function duplicate($doWrite = true) { | 
            ||
| 671 | |||
| 672 | $page = parent::duplicate(false);  | 
            ||
| 673 | $page->Sort = 0;  | 
            ||
| 674 | 		$this->invokeWithExtensions('onBeforeDuplicate', $page); | 
            ||
| 675 | |||
| 676 | 		if($doWrite) { | 
            ||
| 677 | $page->write();  | 
            ||
| 678 | |||
| 679 | $page = $this->duplicateManyManyRelations($this, $page);  | 
            ||
| 680 | }  | 
            ||
| 681 | 		$this->invokeWithExtensions('onAfterDuplicate', $page); | 
            ||
| 682 | |||
| 683 | return $page;  | 
            ||
| 684 | }  | 
            ||
| 685 | |||
| 686 | /**  | 
            ||
| 687 | * Duplicates each child of this node recursively and returns the top-level duplicate node.  | 
            ||
| 688 | *  | 
            ||
| 689 | * @return self The duplicated object  | 
            ||
| 690 | */  | 
            ||
| 691 | 	public function duplicateWithChildren() { | 
            ||
| 707 | |||
| 708 | /**  | 
            ||
| 709 | * Duplicate this node and its children as a child of the node with the given ID  | 
            ||
| 710 | *  | 
            ||
| 711 | * @param int $id ID of the new node's new parent  | 
            ||
| 712 | */  | 
            ||
| 713 | 	public function duplicateAsChild($id) { | 
            ||
| 719 | |||
| 720 | /**  | 
            ||
| 721 | * Return a breadcrumb trail to this page. Excludes "hidden" pages (with ShowInMenus=0) by default.  | 
            ||
| 722 | *  | 
            ||
| 723 | * @param int $maxDepth The maximum depth to traverse.  | 
            ||
| 724 | * @param boolean $unlinked Whether to link page titles.  | 
            ||
| 725 | * @param boolean|string $stopAtPageType ClassName of a page to stop the upwards traversal.  | 
            ||
| 726 | * @param boolean $showHidden Include pages marked with the attribute ShowInMenus = 0  | 
            ||
| 727 | * @return HTMLText The breadcrumb trail.  | 
            ||
| 728 | */  | 
            ||
| 729 | 	public function Breadcrumbs($maxDepth = 20, $unlinked = false, $stopAtPageType = false, $showHidden = false) { | 
            ||
| 737 | |||
| 738 | |||
| 739 | /**  | 
            ||
| 740 | * Returns a list of breadcrumbs for the current page.  | 
            ||
| 741 | *  | 
            ||
| 742 | * @param int $maxDepth The maximum depth to traverse.  | 
            ||
| 743 | * @param boolean|string $stopAtPageType ClassName of a page to stop the upwards traversal.  | 
            ||
| 744 | * @param boolean $showHidden Include pages marked with the attribute ShowInMenus = 0  | 
            ||
| 745 | *  | 
            ||
| 746 | * @return ArrayList  | 
            ||
| 747 | */  | 
            ||
| 748 | 	public function getBreadcrumbItems($maxDepth = 20, $stopAtPageType = false, $showHidden = false) { | 
            ||
| 749 | $page = $this;  | 
            ||
| 750 | $pages = array();  | 
            ||
| 751 | |||
| 752 | while(  | 
            ||
| 753 | $page  | 
            ||
| 754 | && (!$maxDepth || count($pages) < $maxDepth)  | 
            ||
| 755 | && (!$stopAtPageType || $page->ClassName != $stopAtPageType)  | 
            ||
| 756 |  		) { | 
            ||
| 757 | 			if($showHidden || $page->ShowInMenus || ($page->ID == $this->ID)) { | 
            ||
| 758 | $pages[] = $page;  | 
            ||
| 759 | }  | 
            ||
| 760 | |||
| 761 | $page = $page->Parent;  | 
            ||
| 762 | }  | 
            ||
| 763 | |||
| 764 | return new ArrayList(array_reverse($pages));  | 
            ||
| 765 | }  | 
            ||
| 766 | |||
| 767 | |||
| 768 | /**  | 
            ||
| 769 | * Make this page a child of another page.  | 
            ||
| 770 | *  | 
            ||
| 771 | * If the parent page does not exist, resolve it to a valid ID before updating this page's reference.  | 
            ||
| 772 | *  | 
            ||
| 773 | * @param SiteTree|int $item Either the parent object, or the parent ID  | 
            ||
| 774 | */  | 
            ||
| 775 | 	public function setParent($item) { | 
            ||
| 783 | |||
| 784 | /**  | 
            ||
| 785 | * Get the parent of this page.  | 
            ||
| 786 | *  | 
            ||
| 787 | * @return SiteTree Parent of this page  | 
            ||
| 788 | */  | 
            ||
| 789 | 	public function getParent() { | 
            ||
| 794 | |||
| 795 | /**  | 
            ||
| 796 | * Return a string of the form "parent - page" or "grandparent - parent - page" using page titles  | 
            ||
| 797 | *  | 
            ||
| 798 | * @param int $level The maximum amount of levels to traverse.  | 
            ||
| 799 | * @param string $separator Seperating string  | 
            ||
| 800 | * @return string The resulting string  | 
            ||
| 801 | */  | 
            ||
| 802 | 	public function NestedTitle($level = 2, $separator = " - ") { | 
            ||
| 811 | |||
| 812 | /**  | 
            ||
| 813 | * This function should return true if the current user can execute this action. It can be overloaded to customise  | 
            ||
| 814 | * the security model for an application.  | 
            ||
| 815 | *  | 
            ||
| 816 | 	 * Slightly altered from parent behaviour in {@link DataObject->can()}: | 
            ||
| 817 | * - Checks for existence of a method named "can<$perm>()" on the object  | 
            ||
| 818 | * - Calls decorators and only returns for FALSE "vetoes"  | 
            ||
| 819 | 	 * - Falls back to {@link Permission::check()} | 
            ||
| 820 | * - Does NOT check for many-many relations named "Can<$perm>"  | 
            ||
| 821 | *  | 
            ||
| 822 | * @uses DataObjectDecorator->can()  | 
            ||
| 823 | *  | 
            ||
| 824 | * @param string $perm The permission to be checked, such as 'View'  | 
            ||
| 825 | * @param Member $member The member whose permissions need checking. Defaults to the currently logged in user.  | 
            ||
| 826 | * @return bool True if the the member is allowed to do the given action  | 
            ||
| 827 | */  | 
            ||
| 828 | 	public function can($perm, $member = null) { | 
            ||
| 829 | View Code Duplication | 		if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) { | 
            |
| 830 | $member = Member::currentUserID();  | 
            ||
| 831 | }  | 
            ||
| 832 | |||
| 833 | if($member && Permission::checkMember($member, "ADMIN")) return true;  | 
            ||
| 834 | |||
| 835 | 		if(is_string($perm) && method_exists($this, 'can' . ucfirst($perm))) { | 
            ||
| 836 | $method = 'can' . ucfirst($perm);  | 
            ||
| 837 | return $this->$method($member);  | 
            ||
| 838 | }  | 
            ||
| 839 | |||
| 840 | 		$results = $this->extend('can', $member); | 
            ||
| 841 | if($results && is_array($results)) if(!min($results)) return false;  | 
            ||
| 842 | |||
| 843 | return ($member && Permission::checkMember($member, $perm));  | 
            ||
| 844 | }  | 
            ||
| 845 | |||
| 846 | /**  | 
            ||
| 847 | * This function should return true if the current user can add children to this page. It can be overloaded to  | 
            ||
| 848 | * customise the security model for an application.  | 
            ||
| 849 | *  | 
            ||
| 850 | * Denies permission if any of the following conditions is true:  | 
            ||
| 851 | * - alternateCanAddChildren() on a extension returns false  | 
            ||
| 852 | * - canEdit() is not granted  | 
            ||
| 853 | 	 * - There are no classes defined in {@link $allowed_children} | 
            ||
| 854 | *  | 
            ||
| 855 | * @uses SiteTreeExtension->canAddChildren()  | 
            ||
| 856 | * @uses canEdit()  | 
            ||
| 857 | * @uses $allowed_children  | 
            ||
| 858 | *  | 
            ||
| 859 | * @param Member|int $member  | 
            ||
| 860 | * @return bool True if the current user can add children  | 
            ||
| 861 | */  | 
            ||
| 862 | 	public function canAddChildren($member = null) { | 
            ||
| 863 | // Disable adding children to archived pages  | 
            ||
| 864 | 		if($this->getIsDeletedFromStage()) { | 
            ||
| 865 | return false;  | 
            ||
| 866 | }  | 
            ||
| 867 | |||
| 868 | View Code Duplication | 		if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) { | 
            |
| 869 | $member = Member::currentUserID();  | 
            ||
| 870 | }  | 
            ||
| 871 | |||
| 872 | if($member && Permission::checkMember($member, "ADMIN")) return true;  | 
            ||
| 873 | |||
| 874 | // Standard mechanism for accepting permission changes from extensions  | 
            ||
| 875 | 		$extended = $this->extendedCan('canAddChildren', $member); | 
            ||
| 876 | if($extended !== null) return $extended;  | 
            ||
| 877 | |||
| 878 | 		return $this->canEdit($member) && $this->stat('allowed_children') != 'none'; | 
            ||
| 879 | }  | 
            ||
| 880 | |||
| 881 | /**  | 
            ||
| 882 | * This function should return true if the current user can view this page. It can be overloaded to customise the  | 
            ||
| 883 | * security model for an application.  | 
            ||
| 884 | *  | 
            ||
| 885 | * Denies permission if any of the following conditions is true:  | 
            ||
| 886 | * - canView() on any extension returns false  | 
            ||
| 887 | * - "CanViewType" directive is set to "Inherit" and any parent page return false for canView()  | 
            ||
| 888 | * - "CanViewType" directive is set to "LoggedInUsers" and no user is logged in  | 
            ||
| 889 | * - "CanViewType" directive is set to "OnlyTheseUsers" and user is not in the given groups  | 
            ||
| 890 | *  | 
            ||
| 891 | * @uses DataExtension->canView()  | 
            ||
| 892 | * @uses ViewerGroups()  | 
            ||
| 893 | *  | 
            ||
| 894 | * @param Member|int $member  | 
            ||
| 895 | * @return bool True if the current user can view this page  | 
            ||
| 896 | */  | 
            ||
| 897 | 	public function canView($member = null) { | 
            ||
| 898 | View Code Duplication | 		if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) { | 
            |
| 899 | $member = Member::currentUserID();  | 
            ||
| 900 | }  | 
            ||
| 901 | |||
| 902 | // admin override  | 
            ||
| 903 | 		if($member && Permission::checkMember($member, array("ADMIN", "SITETREE_VIEW_ALL"))) return true; | 
            ||
| 904 | |||
| 905 | // Orphaned pages (in the current stage) are unavailable, except for admins via the CMS  | 
            ||
| 906 | if($this->isOrphaned()) return false;  | 
            ||
| 907 | |||
| 908 | // Standard mechanism for accepting permission changes from extensions  | 
            ||
| 909 | 		$extended = $this->extendedCan('canView', $member); | 
            ||
| 910 | if($extended !== null) return $extended;  | 
            ||
| 911 | |||
| 912 | // check for empty spec  | 
            ||
| 913 | if(!$this->CanViewType || $this->CanViewType == 'Anyone') return true;  | 
            ||
| 914 | |||
| 915 | // check for inherit  | 
            ||
| 916 | 		if($this->CanViewType == 'Inherit') { | 
            ||
| 917 | if($this->ParentID) return $this->Parent()->canView($member);  | 
            ||
| 918 | else return $this->getSiteConfig()->canViewPages($member);  | 
            ||
| 919 | }  | 
            ||
| 920 | |||
| 921 | // check for any logged-in users  | 
            ||
| 922 | 		if($this->CanViewType == 'LoggedInUsers' && $member) { | 
            ||
| 923 | return true;  | 
            ||
| 924 | }  | 
            ||
| 925 | |||
| 926 | // check for specific groups  | 
            ||
| 927 | 		if($member && is_numeric($member)) $member = DataObject::get_by_id('Member', $member); | 
            ||
| 928 | if(  | 
            ||
| 929 | $this->CanViewType == 'OnlyTheseUsers'  | 
            ||
| 930 | && $member  | 
            ||
| 931 | && $member->inGroups($this->ViewerGroups())  | 
            ||
| 932 | ) return true;  | 
            ||
| 933 | |||
| 934 | return false;  | 
            ||
| 935 | }  | 
            ||
| 936 | |||
| 937 | /**  | 
            ||
| 938 | * This function should return true if the current user can delete this page. It can be overloaded to customise the  | 
            ||
| 939 | * security model for an application.  | 
            ||
| 940 | *  | 
            ||
| 941 | * Denies permission if any of the following conditions is true:  | 
            ||
| 942 | * - canDelete() returns false on any extension  | 
            ||
| 943 | * - canEdit() returns false  | 
            ||
| 944 | * - any descendant page returns false for canDelete()  | 
            ||
| 945 | *  | 
            ||
| 946 | * @uses canDelete()  | 
            ||
| 947 | * @uses SiteTreeExtension->canDelete()  | 
            ||
| 948 | * @uses canEdit()  | 
            ||
| 949 | *  | 
            ||
| 950 | * @param Member $member  | 
            ||
| 951 | * @return bool True if the current user can delete this page  | 
            ||
| 952 | */  | 
            ||
| 953 | 	public function canDelete($member = null) { | 
            ||
| 954 | View Code Duplication | if($member instanceof Member) $memberID = $member->ID;  | 
            |
| 955 | else if(is_numeric($member)) $memberID = $member;  | 
            ||
| 956 | else $memberID = Member::currentUserID();  | 
            ||
| 957 | |||
| 958 | 		if($memberID && Permission::checkMember($memberID, array("ADMIN", "SITETREE_EDIT_ALL"))) { | 
            ||
| 959 | return true;  | 
            ||
| 960 | }  | 
            ||
| 961 | |||
| 962 | // Standard mechanism for accepting permission changes from extensions  | 
            ||
| 963 | 		$extended = $this->extendedCan('canDelete', $memberID); | 
            ||
| 964 | if($extended !== null) return $extended;  | 
            ||
| 965 | |||
| 966 | // Regular canEdit logic is handled by can_edit_multiple  | 
            ||
| 967 | $results = self::can_delete_multiple(array($this->ID), $memberID);  | 
            ||
| 968 | |||
| 969 | // If this page no longer exists in stage/live results won't contain the page.  | 
            ||
| 970 | // Fail-over to false  | 
            ||
| 971 | return isset($results[$this->ID]) ? $results[$this->ID] : false;  | 
            ||
| 972 | }  | 
            ||
| 973 | |||
| 974 | /**  | 
            ||
| 975 | * This function should return true if the current user can create new pages of this class, regardless of class. It  | 
            ||
| 976 | * can be overloaded to customise the security model for an application.  | 
            ||
| 977 | *  | 
            ||
| 978 | * By default, permission to create at the root level is based on the SiteConfig configuration, and permission to  | 
            ||
| 979 | * create beneath a parent is based on the ability to edit that parent page.  | 
            ||
| 980 | *  | 
            ||
| 981 | 	 * Use {@link canAddChildren()} to control behaviour of creating children under this page. | 
            ||
| 982 | *  | 
            ||
| 983 | * @uses $can_create  | 
            ||
| 984 | * @uses DataExtension->canCreate()  | 
            ||
| 985 | *  | 
            ||
| 986 | * @param Member $member  | 
            ||
| 987 | 	 * @param array $context Optional array which may contain array('Parent' => $parentObj) | 
            ||
| 988 | * If a parent page is known, it will be checked for validity.  | 
            ||
| 989 | * If omitted, it will be assumed this is to be created as a top level page.  | 
            ||
| 990 | * @return bool True if the current user can create pages on this class.  | 
            ||
| 991 | */  | 
            ||
| 992 | 	public function canCreate($member = null, $context = array()) { | 
            ||
| 1024 | |||
| 1025 | /**  | 
            ||
| 1026 | * This function should return true if the current user can edit this page. It can be overloaded to customise the  | 
            ||
| 1027 | * security model for an application.  | 
            ||
| 1028 | *  | 
            ||
| 1029 | * Denies permission if any of the following conditions is true:  | 
            ||
| 1030 | * - canEdit() on any extension returns false  | 
            ||
| 1031 | * - canView() return false  | 
            ||
| 1032 | * - "CanEditType" directive is set to "Inherit" and any parent page return false for canEdit()  | 
            ||
| 1033 | * - "CanEditType" directive is set to "LoggedInUsers" and no user is logged in or doesn't have the  | 
            ||
| 1034 | * CMS_Access_CMSMAIN permission code  | 
            ||
| 1035 | * - "CanEditType" directive is set to "OnlyTheseUsers" and user is not in the given groups  | 
            ||
| 1036 | *  | 
            ||
| 1037 | * @uses canView()  | 
            ||
| 1038 | * @uses EditorGroups()  | 
            ||
| 1039 | * @uses DataExtension->canEdit()  | 
            ||
| 1040 | *  | 
            ||
| 1041 | * @param Member $member Set to false if you want to explicitly test permissions without a valid user (useful for  | 
            ||
| 1042 | * unit tests)  | 
            ||
| 1043 | * @return bool True if the current user can edit this page  | 
            ||
| 1044 | */  | 
            ||
| 1045 | 	public function canEdit($member = null) { | 
            ||
| 1046 | View Code Duplication | if($member instanceof Member) $memberID = $member->ID;  | 
            |
| 1047 | else if(is_numeric($member)) $memberID = $member;  | 
            ||
| 1048 | else $memberID = Member::currentUserID();  | 
            ||
| 1049 | |||
| 1050 | 		if($memberID && Permission::checkMember($memberID, array("ADMIN", "SITETREE_EDIT_ALL"))) return true; | 
            ||
| 1051 | |||
| 1052 | // Standard mechanism for accepting permission changes from extensions  | 
            ||
| 1053 | 		$extended = $this->extendedCan('canEdit', $memberID); | 
            ||
| 1054 | if($extended !== null) return $extended;  | 
            ||
| 1055 | |||
| 1056 | 		if($this->ID) { | 
            ||
| 1057 | // Regular canEdit logic is handled by can_edit_multiple  | 
            ||
| 1058 | $results = self::can_edit_multiple(array($this->ID), $memberID);  | 
            ||
| 1059 | |||
| 1060 | // If this page no longer exists in stage/live results won't contain the page.  | 
            ||
| 1061 | // Fail-over to false  | 
            ||
| 1062 | return isset($results[$this->ID]) ? $results[$this->ID] : false;  | 
            ||
| 1063 | |||
| 1064 | // Default for unsaved pages  | 
            ||
| 1065 | 		} else { | 
            ||
| 1066 | return $this->getSiteConfig()->canEditPages($member);  | 
            ||
| 1067 | }  | 
            ||
| 1068 | }  | 
            ||
| 1069 | |||
| 1070 | /**  | 
            ||
| 1071 | * @deprecated  | 
            ||
| 1072 | */  | 
            ||
| 1073 | 	public function canDeleteFromLive($member = null) { | 
            ||
| 1085 | |||
| 1086 | /**  | 
            ||
| 1087 | * Stub method to get the site config, unless the current class can provide an alternate.  | 
            ||
| 1088 | *  | 
            ||
| 1089 | * @return SiteConfig  | 
            ||
| 1090 | */  | 
            ||
| 1091 | 	public function getSiteConfig() { | 
            ||
| 1092 | |||
| 1093 | 		if($this->hasMethod('alternateSiteConfig')) { | 
            ||
| 1094 | $altConfig = $this->alternateSiteConfig();  | 
            ||
| 1095 | if($altConfig) return $altConfig;  | 
            ||
| 1096 | }  | 
            ||
| 1097 | |||
| 1098 | return SiteConfig::current_site_config();  | 
            ||
| 1099 | }  | 
            ||
| 1100 | |||
| 1101 | /**  | 
            ||
| 1102 | * Pre-populate the cache of canEdit, canView, canDelete, canPublish permissions. This method will use the static  | 
            ||
| 1103 | * can_(perm)_multiple method for efficiency.  | 
            ||
| 1104 | *  | 
            ||
| 1105 | * @param string $permission The permission: edit, view, publish, approve, etc.  | 
            ||
| 1106 | * @param array $ids An array of page IDs  | 
            ||
| 1107 | * @param callable|string $batchCallback The function/static method to call to calculate permissions. Defaults  | 
            ||
| 1108 | * to 'SiteTree::can_(permission)_multiple'  | 
            ||
| 1109 | */  | 
            ||
| 1110 | 	static public function prepopulate_permission_cache($permission = 'CanEditType', $ids, $batchCallback = null) { | 
            ||
| 1120 | |||
| 1121 | /**  | 
            ||
| 1122 | 	 * This method is NOT a full replacement for the individual can*() methods, e.g. {@link canEdit()}. Rather than | 
            ||
| 1123 | * checking (potentially slow) PHP logic, it relies on the database group associations, e.g. the "CanEditType" field  | 
            ||
| 1124 | * plus the "SiteTree_EditorGroups" many-many table. By batch checking multiple records, we can combine the queries  | 
            ||
| 1125 | * efficiently.  | 
            ||
| 1126 | *  | 
            ||
| 1127 | 	 * Caches based on $typeField data. To invalidate the cache, use {@link SiteTree::reset()} or set the $useCached | 
            ||
| 1128 | * property to FALSE.  | 
            ||
| 1129 | *  | 
            ||
| 1130 | 	 * @param array  $ids              Of {@link SiteTree} IDs | 
            ||
| 1131 | * @param int $memberID Member ID  | 
            ||
| 1132 | * @param string $typeField A property on the data record, e.g. "CanEditType".  | 
            ||
| 1133 | * @param string $groupJoinTable A many-many table name on this record, e.g. "SiteTree_EditorGroups"  | 
            ||
| 1134 | 	 * @param string $siteConfigMethod Method to call on {@link SiteConfig} for toplevel items, e.g. "canEdit" | 
            ||
| 1135 | * @param string $globalPermission If the member doesn't have this permission code, don't bother iterating deeper  | 
            ||
| 1136 | * @param bool $useCached  | 
            ||
| 1137 | 	 * @return array An map of {@link SiteTree} ID keys to boolean values | 
            ||
| 1138 | */  | 
            ||
| 1139 | public static function batch_permission_check($ids, $memberID, $typeField, $groupJoinTable, $siteConfigMethod,  | 
            ||
| 1140 | 												  $globalPermission = null, $useCached = true) { | 
            ||
| 1141 | 		if($globalPermission === NULL) $globalPermission = array('CMS_ACCESS_LeftAndMain', 'CMS_ACCESS_CMSMain'); | 
            ||
| 1142 | |||
| 1143 | // Sanitise the IDs  | 
            ||
| 1144 | $ids = array_filter($ids, 'is_numeric');  | 
            ||
| 1145 | |||
| 1146 | // This is the name used on the permission cache  | 
            ||
| 1147 | // converts something like 'CanEditType' to 'edit'.  | 
            ||
| 1148 | $cacheKey = strtolower(substr($typeField, 3, -4)) . "-$memberID";  | 
            ||
| 1149 | |||
| 1150 | // Default result: nothing editable  | 
            ||
| 1151 | $result = array_fill_keys($ids, false);  | 
            ||
| 1152 | 		if($ids) { | 
            ||
| 1153 | |||
| 1154 | // Look in the cache for values  | 
            ||
| 1155 | 			if($useCached && isset(self::$cache_permissions[$cacheKey])) { | 
            ||
| 1156 | $cachedValues = array_intersect_key(self::$cache_permissions[$cacheKey], $result);  | 
            ||
| 1157 | |||
| 1158 | // If we can't find everything in the cache, then look up the remainder separately  | 
            ||
| 1159 | $uncachedValues = array_diff_key($result, self::$cache_permissions[$cacheKey]);  | 
            ||
| 1160 | 				if($uncachedValues) { | 
            ||
| 1161 | $cachedValues = self::batch_permission_check(array_keys($uncachedValues), $memberID, $typeField, $groupJoinTable, $siteConfigMethod, $globalPermission, false) + $cachedValues;  | 
            ||
| 1162 | }  | 
            ||
| 1163 | return $cachedValues;  | 
            ||
| 1164 | }  | 
            ||
| 1165 | |||
| 1166 | // If a member doesn't have a certain permission then they can't edit anything  | 
            ||
| 1167 | 			if(!$memberID || ($globalPermission && !Permission::checkMember($memberID, $globalPermission))) { | 
            ||
| 1168 | return $result;  | 
            ||
| 1169 | }  | 
            ||
| 1170 | |||
| 1171 | // Placeholder for parameterised ID list  | 
            ||
| 1172 | $idPlaceholders = DB::placeholders($ids);  | 
            ||
| 1173 | |||
| 1174 | // If page can't be viewed, don't grant edit permissions to do - implement can_view_multiple(), so this can  | 
            ||
| 1175 | // be enabled  | 
            ||
| 1176 | //$ids = array_keys(array_filter(self::can_view_multiple($ids, $memberID)));  | 
            ||
| 1177 | |||
| 1178 | // Get the groups that the given member belongs to  | 
            ||
| 1179 | 			$groupIDs = DataObject::get_by_id('Member', $memberID)->Groups()->column("ID"); | 
            ||
| 1180 | 			$SQL_groupList = implode(", ", $groupIDs); | 
            ||
| 1181 | if (!$SQL_groupList) $SQL_groupList = '0';  | 
            ||
| 1182 | |||
| 1183 | $combinedStageResult = array();  | 
            ||
| 1184 | |||
| 1185 | 			foreach(array('Stage', 'Live') as $stage) { | 
            ||
| 1186 | // Start by filling the array with the pages that actually exist  | 
            ||
| 1187 | $table = ($stage=='Stage') ? "SiteTree" : "SiteTree_$stage";  | 
            ||
| 1188 | |||
| 1189 | 				if($ids) { | 
            ||
| 1190 | $idQuery = "SELECT \"ID\" FROM \"$table\" WHERE \"ID\" IN ($idPlaceholders)";  | 
            ||
| 1191 | $stageIds = DB::prepared_query($idQuery, $ids)->column();  | 
            ||
| 1192 | 				} else { | 
            ||
| 1193 | $stageIds = array();  | 
            ||
| 1194 | }  | 
            ||
| 1195 | $result = array_fill_keys($stageIds, false);  | 
            ||
| 1196 | |||
| 1197 | // Get the uninherited permissions  | 
            ||
| 1198 | 				$uninheritedPermissions = Versioned::get_by_stage("SiteTree", $stage) | 
            ||
| 1199 | ->where(array(  | 
            ||
| 1200 | "(\"$typeField\" = 'LoggedInUsers' OR  | 
            ||
| 1201 | (\"$typeField\" = 'OnlyTheseUsers' AND \"$groupJoinTable\".\"SiteTreeID\" IS NOT NULL))  | 
            ||
| 1202 | AND \"SiteTree\".\"ID\" IN ($idPlaceholders)"  | 
            ||
| 1203 | => $ids  | 
            ||
| 1204 | ))  | 
            ||
| 1205 | ->leftJoin($groupJoinTable, "\"$groupJoinTable\".\"SiteTreeID\" = \"SiteTree\".\"ID\" AND \"$groupJoinTable\".\"GroupID\" IN ($SQL_groupList)");  | 
            ||
| 1206 | |||
| 1207 | 				if($uninheritedPermissions) { | 
            ||
| 1208 | // Set all the relevant items in $result to true  | 
            ||
| 1209 | 					$result = array_fill_keys($uninheritedPermissions->column('ID'), true) + $result; | 
            ||
| 1210 | }  | 
            ||
| 1211 | |||
| 1212 | // Get permissions that are inherited  | 
            ||
| 1213 | $potentiallyInherited = Versioned::get_by_stage(  | 
            ||
| 1214 | "SiteTree",  | 
            ||
| 1215 | $stage,  | 
            ||
| 1216 | 					array("\"$typeField\" = 'Inherit' AND \"SiteTree\".\"ID\" IN ($idPlaceholders)" => $ids) | 
            ||
| 1217 | );  | 
            ||
| 1218 | |||
| 1219 | 				if($potentiallyInherited) { | 
            ||
| 1220 | // Group $potentiallyInherited by ParentID; we'll look at the permission of all those parents and  | 
            ||
| 1221 | // then see which ones the user has permission on  | 
            ||
| 1222 | $groupedByParent = array();  | 
            ||
| 1223 | 					foreach($potentiallyInherited as $item) { | 
            ||
| 1224 | 						if($item->ParentID) { | 
            ||
| 1225 | if(!isset($groupedByParent[$item->ParentID])) $groupedByParent[$item->ParentID] = array();  | 
            ||
| 1226 | $groupedByParent[$item->ParentID][] = $item->ID;  | 
            ||
| 1227 | 						} else { | 
            ||
| 1228 | // Might return different site config based on record context, e.g. when subsites module  | 
            ||
| 1229 | // is used  | 
            ||
| 1230 | $siteConfig = $item->getSiteConfig();  | 
            ||
| 1231 | 							$result[$item->ID] = $siteConfig->{$siteConfigMethod}($memberID); | 
            ||
| 1232 | }  | 
            ||
| 1233 | }  | 
            ||
| 1234 | |||
| 1235 | 					if($groupedByParent) { | 
            ||
| 1236 | $actuallyInherited = self::batch_permission_check(array_keys($groupedByParent), $memberID, $typeField, $groupJoinTable, $siteConfigMethod);  | 
            ||
| 1237 | 						if($actuallyInherited) { | 
            ||
| 1238 | $parentIDs = array_keys(array_filter($actuallyInherited));  | 
            ||
| 1239 | 							foreach($parentIDs as $parentID) { | 
            ||
| 1240 | // Set all the relevant items in $result to true  | 
            ||
| 1241 | $result = array_fill_keys($groupedByParent[$parentID], true) + $result;  | 
            ||
| 1242 | }  | 
            ||
| 1243 | }  | 
            ||
| 1244 | }  | 
            ||
| 1245 | }  | 
            ||
| 1246 | |||
| 1247 | $combinedStageResult = $combinedStageResult + $result;  | 
            ||
| 1248 | |||
| 1249 | }  | 
            ||
| 1250 | }  | 
            ||
| 1251 | |||
| 1252 | 		if(isset($combinedStageResult)) { | 
            ||
| 1253 | // Cache the results  | 
            ||
| 1254 | if(empty(self::$cache_permissions[$cacheKey])) self::$cache_permissions[$cacheKey] = array();  | 
            ||
| 1255 | self::$cache_permissions[$cacheKey] = $combinedStageResult + self::$cache_permissions[$cacheKey];  | 
            ||
| 1256 | |||
| 1257 | return $combinedStageResult;  | 
            ||
| 1258 | 		} else { | 
            ||
| 1259 | return array();  | 
            ||
| 1260 | }  | 
            ||
| 1261 | }  | 
            ||
| 1262 | |||
| 1263 | /**  | 
            ||
| 1264 | * Get the 'can edit' information for a number of SiteTree pages.  | 
            ||
| 1265 | *  | 
            ||
| 1266 | * @param array $ids An array of IDs of the SiteTree pages to look up  | 
            ||
| 1267 | * @param int $memberID ID of member  | 
            ||
| 1268 | * @param bool $useCached Return values from the permission cache if they exist  | 
            ||
| 1269 | * @return array A map where the IDs are keys and the values are booleans stating whether the given page can be  | 
            ||
| 1270 | * edited  | 
            ||
| 1271 | */  | 
            ||
| 1272 | 	static public function can_edit_multiple($ids, $memberID, $useCached = true) { | 
            ||
| 1275 | |||
| 1276 | /**  | 
            ||
| 1277 | * Get the 'can edit' information for a number of SiteTree pages.  | 
            ||
| 1278 | *  | 
            ||
| 1279 | * @param array $ids An array of IDs of the SiteTree pages to look up  | 
            ||
| 1280 | * @param int $memberID ID of member  | 
            ||
| 1281 | * @param bool $useCached Return values from the permission cache if they exist  | 
            ||
| 1282 | * @return array  | 
            ||
| 1283 | */  | 
            ||
| 1284 | 	static public function can_delete_multiple($ids, $memberID, $useCached = true) { | 
            ||
| 1285 | $deletable = array();  | 
            ||
| 1286 | $result = array_fill_keys($ids, false);  | 
            ||
| 1287 | $cacheKey = "delete-$memberID";  | 
            ||
| 1288 | |||
| 1289 | // Look in the cache for values  | 
            ||
| 1290 | 		if($useCached && isset(self::$cache_permissions[$cacheKey])) { | 
            ||
| 1291 | $cachedValues = array_intersect_key(self::$cache_permissions[$cacheKey], $result);  | 
            ||
| 1292 | |||
| 1293 | // If we can't find everything in the cache, then look up the remainder separately  | 
            ||
| 1294 | $uncachedValues = array_diff_key($result, self::$cache_permissions[$cacheKey]);  | 
            ||
| 1295 | 			if($uncachedValues) { | 
            ||
| 1296 | $cachedValues = self::can_delete_multiple(array_keys($uncachedValues), $memberID, false)  | 
            ||
| 1297 | + $cachedValues;  | 
            ||
| 1298 | }  | 
            ||
| 1299 | return $cachedValues;  | 
            ||
| 1300 | }  | 
            ||
| 1301 | |||
| 1302 | // You can only delete pages that you can edit  | 
            ||
| 1303 | $editableIDs = array_keys(array_filter(self::can_edit_multiple($ids, $memberID)));  | 
            ||
| 1304 | 		if($editableIDs) { | 
            ||
| 1305 | |||
| 1306 | // You can only delete pages whose children you can delete  | 
            ||
| 1307 | $editablePlaceholders = DB::placeholders($editableIDs);  | 
            ||
| 1308 | $childRecords = SiteTree::get()->where(array(  | 
            ||
| 1309 | "\"SiteTree\".\"ParentID\" IN ($editablePlaceholders)" => $editableIDs  | 
            ||
| 1310 | ));  | 
            ||
| 1311 | 			if($childRecords) { | 
            ||
| 1312 | 				$children = $childRecords->map("ID", "ParentID"); | 
            ||
| 1313 | |||
| 1314 | // Find out the children that can be deleted  | 
            ||
| 1315 | $deletableChildren = self::can_delete_multiple($children->keys(), $memberID);  | 
            ||
| 1316 | |||
| 1317 | // Get a list of all the parents that have no undeletable children  | 
            ||
| 1318 | $deletableParents = array_fill_keys($editableIDs, true);  | 
            ||
| 1319 | 				foreach($deletableChildren as $id => $canDelete) { | 
            ||
| 1320 | if(!$canDelete) unset($deletableParents[$children[$id]]);  | 
            ||
| 1321 | }  | 
            ||
| 1322 | |||
| 1323 | // Use that to filter the list of deletable parents that have children  | 
            ||
| 1324 | $deletableParents = array_keys($deletableParents);  | 
            ||
| 1325 | |||
| 1326 | // Also get the $ids that don't have children  | 
            ||
| 1327 | $parents = array_unique($children->values());  | 
            ||
| 1328 | $deletableLeafNodes = array_diff($editableIDs, $parents);  | 
            ||
| 1329 | |||
| 1330 | // Combine the two  | 
            ||
| 1331 | $deletable = array_merge($deletableParents, $deletableLeafNodes);  | 
            ||
| 1332 | |||
| 1333 | 			} else { | 
            ||
| 1334 | $deletable = $editableIDs;  | 
            ||
| 1335 | }  | 
            ||
| 1336 | 		} else { | 
            ||
| 1337 | $deletable = array();  | 
            ||
| 1338 | }  | 
            ||
| 1339 | |||
| 1340 | // Convert the array of deletable IDs into a map of the original IDs with true/false as the value  | 
            ||
| 1341 | return array_fill_keys($deletable, true) + array_fill_keys($ids, false);  | 
            ||
| 1342 | }  | 
            ||
| 1343 | |||
| 1344 | /**  | 
            ||
| 1345 | * Collate selected descendants of this page.  | 
            ||
| 1346 | *  | 
            ||
| 1347 | 	 * {@link $condition} will be evaluated on each descendant, and if it is succeeds, that item will be added to the | 
            ||
| 1348 | * $collator array.  | 
            ||
| 1349 | *  | 
            ||
| 1350 | * @param string $condition The PHP condition to be evaluated. The page will be called $item  | 
            ||
| 1351 | * @param array $collator An array, passed by reference, to collect all of the matching descendants.  | 
            ||
| 1352 | * @return bool  | 
            ||
| 1353 | */  | 
            ||
| 1354 | 	public function collateDescendants($condition, &$collator) { | 
            ||
| 1363 | |||
| 1364 | /**  | 
            ||
| 1365 | * Return the title, description, keywords and language metatags.  | 
            ||
| 1366 | *  | 
            ||
| 1367 | * @todo Move <title> tag in separate getter for easier customization and more obvious usage  | 
            ||
| 1368 | *  | 
            ||
| 1369 | * @param bool $includeTitle Show default <title>-tag, set to false for custom templating  | 
            ||
| 1370 | * @return string The XHTML metatags  | 
            ||
| 1371 | */  | 
            ||
| 1372 | 	public function MetaTags($includeTitle = true) { | 
            ||
| 1405 | |||
| 1406 | /**  | 
            ||
| 1407 | * Returns the object that contains the content that a user would associate with this page.  | 
            ||
| 1408 | *  | 
            ||
| 1409 | * Ordinarily, this is just the page itself, but for example on RedirectorPages or VirtualPages ContentSource() will  | 
            ||
| 1410 | * return the page that is linked to.  | 
            ||
| 1411 | *  | 
            ||
| 1412 | * @return $this  | 
            ||
| 1413 | */  | 
            ||
| 1414 | 	public function ContentSource() { | 
            ||
| 1417 | |||
| 1418 | /**  | 
            ||
| 1419 | * Add default records to database.  | 
            ||
| 1420 | *  | 
            ||
| 1421 | * This function is called whenever the database is built, after the database tables have all been created. Overload  | 
            ||
| 1422 | * this to add default records when the database is built, but make sure you call parent::requireDefaultRecords().  | 
            ||
| 1423 | */  | 
            ||
| 1424 | 	public function requireDefaultRecords() { | 
            ||
| 1425 | parent::requireDefaultRecords();  | 
            ||
| 1426 | |||
| 1427 | // default pages  | 
            ||
| 1428 | 		if($this->class == 'SiteTree' && $this->config()->create_default_pages) { | 
            ||
| 1429 | 			if(!SiteTree::get_by_link(Config::inst()->get('RootURLController', 'default_homepage_link'))) { | 
            ||
| 1430 | $homepage = new Page();  | 
            ||
| 1431 | 				$homepage->Title = _t('SiteTree.DEFAULTHOMETITLE', 'Home'); | 
            ||
| 1432 | 				$homepage->Content = _t('SiteTree.DEFAULTHOMECONTENT', '<p>Welcome to SilverStripe! This is the default homepage. You can edit this page by opening <a href="admin/">the CMS</a>.</p><p>You can now access the <a href="http://docs.silverstripe.org">developer documentation</a>, or begin the <a href="http://www.silverstripe.org/learn/lessons">SilverStripe lessons</a>.</p>'); | 
            ||
| 1433 | 				$homepage->URLSegment = Config::inst()->get('RootURLController', 'default_homepage_link'); | 
            ||
| 1434 | $homepage->Sort = 1;  | 
            ||
| 1435 | $homepage->write();  | 
            ||
| 1436 | 				$homepage->publish('Stage', 'Live'); | 
            ||
| 1437 | $homepage->flushCache();  | 
            ||
| 1438 | 				DB::alteration_message('Home page created', 'created'); | 
            ||
| 1439 | }  | 
            ||
| 1440 | |||
| 1441 | 			if(DB::query("SELECT COUNT(*) FROM \"SiteTree\"")->value() == 1) { | 
            ||
| 1442 | $aboutus = new Page();  | 
            ||
| 1443 | 				$aboutus->Title = _t('SiteTree.DEFAULTABOUTTITLE', 'About Us'); | 
            ||
| 1444 | 				$aboutus->Content = _t('SiteTree.DEFAULTABOUTCONTENT', '<p>You can fill this page out with your own content, or delete it and create your own pages.<br /></p>'); | 
            ||
| 1445 | $aboutus->Sort = 2;  | 
            ||
| 1446 | $aboutus->write();  | 
            ||
| 1447 | 				$aboutus->publish('Stage', 'Live'); | 
            ||
| 1448 | $aboutus->flushCache();  | 
            ||
| 1449 | 				DB::alteration_message('About Us page created', 'created'); | 
            ||
| 1450 | |||
| 1451 | $contactus = new Page();  | 
            ||
| 1452 | 				$contactus->Title = _t('SiteTree.DEFAULTCONTACTTITLE', 'Contact Us'); | 
            ||
| 1453 | 				$contactus->Content = _t('SiteTree.DEFAULTCONTACTCONTENT', '<p>You can fill this page out with your own content, or delete it and create your own pages.<br /></p>'); | 
            ||
| 1454 | $contactus->Sort = 3;  | 
            ||
| 1455 | $contactus->write();  | 
            ||
| 1456 | 				$contactus->publish('Stage', 'Live'); | 
            ||
| 1457 | $contactus->flushCache();  | 
            ||
| 1458 | 				DB::alteration_message('Contact Us page created', 'created'); | 
            ||
| 1459 | }  | 
            ||
| 1460 | }  | 
            ||
| 1461 | |||
| 1462 | // schema migration  | 
            ||
| 1463 | // @todo Move to migration task once infrastructure is implemented  | 
            ||
| 1464 | 		if($this->class == 'SiteTree') { | 
            ||
| 1465 | $conn = DB::get_schema();  | 
            ||
| 1466 | // only execute command if fields haven't been renamed to _obsolete_<fieldname> already by the task  | 
            ||
| 1467 | 			if($conn->hasField('SiteTree' ,'Viewers')) { | 
            ||
| 1468 | $task = new UpgradeSiteTreePermissionSchemaTask();  | 
            ||
| 1469 | 				$task->run(new SS_HTTPRequest('GET','/')); | 
            ||
| 1470 | }  | 
            ||
| 1471 | }  | 
            ||
| 1472 | }  | 
            ||
| 1473 | |||
| 1474 | 	protected function onBeforeWrite() { | 
            ||
| 1524 | |||
| 1525 | /**  | 
            ||
| 1526 | * Trigger synchronisation of link tracking  | 
            ||
| 1527 | *  | 
            ||
| 1528 | 	 * {@see SiteTreeLinkTracking::augmentSyncLinkTracking} | 
            ||
| 1529 | */  | 
            ||
| 1530 | 	public function syncLinkTracking() { | 
            ||
| 1533 | |||
| 1534 | 	public function onAfterWrite() { | 
            ||
| 1535 | // Need to flush cache to avoid outdated versionnumber references  | 
            ||
| 1536 | $this->flushCache();  | 
            ||
| 1537 | |||
| 1538 | $linkedPages = $this->VirtualPages();  | 
            ||
| 1539 | 		if($linkedPages) { | 
            ||
| 1540 | // The only way after a write() call to determine if it was triggered by a writeWithoutVersion(),  | 
            ||
| 1541 | // which we have to pass on to the virtual page writes as well.  | 
            ||
| 1542 | $previous = ($this->Version > 1) ? Versioned::get_version($this->class, $this->ID, $this->Version-1) : null;  | 
            ||
| 1543 | 			$withoutVersion = $this->getExtensionInstance('Versioned')->_nextWriteWithoutVersion; | 
            ||
| 1544 | 			foreach($linkedPages as $page) { | 
            ||
| 1545 | $page->copyFrom($page->CopyContentFrom());  | 
            ||
| 1546 | if($withoutVersion) $page->writeWithoutVersion();  | 
            ||
| 1547 | else $page->write();  | 
            ||
| 1548 | }  | 
            ||
| 1549 | }  | 
            ||
| 1550 | |||
| 1551 | parent::onAfterWrite();  | 
            ||
| 1552 | }  | 
            ||
| 1553 | |||
| 1554 | 	public function onBeforeDelete() { | 
            ||
| 1564 | |||
| 1565 | 	public function onAfterDelete() { | 
            ||
| 1566 | // Need to flush cache to avoid outdated versionnumber references  | 
            ||
| 1567 | $this->flushCache();  | 
            ||
| 1568 | |||
| 1569 | // Need to mark pages depending to this one as broken  | 
            ||
| 1570 | $dependentPages = $this->DependentPages();  | 
            ||
| 1571 | 		if($dependentPages) foreach($dependentPages as $page) { | 
            ||
| 1572 | // $page->write() calls syncLinkTracking, which does all the hard work for us.  | 
            ||
| 1573 | $page->write();  | 
            ||
| 1574 | }  | 
            ||
| 1575 | |||
| 1576 | parent::onAfterDelete();  | 
            ||
| 1577 | }  | 
            ||
| 1578 | |||
| 1579 | 	public function flushCache($persistent = true) { | 
            ||
| 1583 | |||
| 1584 | 	public function validate() { | 
            ||
| 1585 | $result = parent::validate();  | 
            ||
| 1586 | |||
| 1587 | // Allowed children validation  | 
            ||
| 1588 | $parent = $this->getParent();  | 
            ||
| 1589 | 		if($parent && $parent->exists()) { | 
            ||
| 1590 | // No need to check for subclasses or instanceof, as allowedChildren() already  | 
            ||
| 1591 | // deconstructs any inheritance trees already.  | 
            ||
| 1592 | $allowed = $parent->allowedChildren();  | 
            ||
| 1593 | $subject = ($this instanceof VirtualPage && $this->CopyContentFromID) ? $this->CopyContentFrom() : $this;  | 
            ||
| 1594 | 			if(!in_array($subject->ClassName, $allowed)) { | 
            ||
| 1595 | |||
| 1596 | $result->error(  | 
            ||
| 1597 | _t(  | 
            ||
| 1598 | 'SiteTree.PageTypeNotAllowed',  | 
            ||
| 1599 | 						'Page type "{type}" not allowed as child of this parent page', | 
            ||
| 1600 | 						array('type' => $subject->i18n_singular_name()) | 
            ||
| 1601 | ),  | 
            ||
| 1602 | 'ALLOWED_CHILDREN'  | 
            ||
| 1603 | );  | 
            ||
| 1604 | }  | 
            ||
| 1605 | }  | 
            ||
| 1606 | |||
| 1607 | // "Can be root" validation  | 
            ||
| 1608 | View Code Duplication | 		if(!$this->stat('can_be_root') && !$this->ParentID) { | 
            |
| 1609 | $result->error(  | 
            ||
| 1610 | _t(  | 
            ||
| 1611 | 'SiteTree.PageTypNotAllowedOnRoot',  | 
            ||
| 1612 | 					'Page type "{type}" is not allowed on the root level', | 
            ||
| 1613 | 					array('type' => $this->i18n_singular_name()) | 
            ||
| 1614 | ),  | 
            ||
| 1615 | 'CAN_BE_ROOT'  | 
            ||
| 1616 | );  | 
            ||
| 1617 | }  | 
            ||
| 1618 | |||
| 1619 | return $result;  | 
            ||
| 1620 | }  | 
            ||
| 1621 | |||
| 1622 | /**  | 
            ||
| 1623 | * Returns true if this object has a URLSegment value that does not conflict with any other objects. This method  | 
            ||
| 1624 | * checks for:  | 
            ||
| 1625 | * - A page with the same URLSegment that has a conflict  | 
            ||
| 1626 | * - Conflicts with actions on the parent page  | 
            ||
| 1627 | * - A conflict caused by a root page having the same URLSegment as a class name  | 
            ||
| 1628 | *  | 
            ||
| 1629 | * @return bool  | 
            ||
| 1630 | */  | 
            ||
| 1631 | 	public function validURLSegment() { | 
            ||
| 1632 | 		if(self::config()->nested_urls && $parent = $this->Parent()) { | 
            ||
| 1633 | 			if($controller = ModelAsController::controller_for($parent)) { | 
            ||
| 1634 | if($controller instanceof Controller && $controller->hasAction($this->URLSegment)) return false;  | 
            ||
| 1635 | }  | 
            ||
| 1636 | }  | 
            ||
| 1637 | |||
| 1638 | 		if(!self::config()->nested_urls || !$this->ParentID) { | 
            ||
| 1639 | if(class_exists($this->URLSegment) && is_subclass_of($this->URLSegment, 'RequestHandler')) return false;  | 
            ||
| 1640 | }  | 
            ||
| 1641 | |||
| 1642 | // Filters by url, id, and parent  | 
            ||
| 1643 | 		$filter = array('"SiteTree"."URLSegment"' => $this->URLSegment); | 
            ||
| 1644 | 		if($this->ID) { | 
            ||
| 1645 | $filter['"SiteTree"."ID" <> ?'] = $this->ID;  | 
            ||
| 1646 | }  | 
            ||
| 1647 | 		if(self::config()->nested_urls) { | 
            ||
| 1648 | $filter['"SiteTree"."ParentID"'] = $this->ParentID ? $this->ParentID : 0;  | 
            ||
| 1649 | }  | 
            ||
| 1650 | |||
| 1651 | $votes = array_filter(  | 
            ||
| 1652 | 			(array)$this->extend('augmentValidURLSegment'), | 
            ||
| 1653 | 			function($v) {return !is_null($v);} | 
            ||
| 1654 | );  | 
            ||
| 1655 | 		if($votes) { | 
            ||
| 1656 | return min($votes);  | 
            ||
| 1657 | }  | 
            ||
| 1658 | |||
| 1659 | // Check existence  | 
            ||
| 1660 | 		$existingPage = DataObject::get_one('SiteTree', $filter); | 
            ||
| 1661 | if ($existingPage) return false;  | 
            ||
| 1662 | |||
| 1663 | return !($existingPage);  | 
            ||
| 1664 | }  | 
            ||
| 1665 | |||
| 1666 | /**  | 
            ||
| 1667 | * Generate a URL segment based on the title provided.  | 
            ||
| 1668 | *  | 
            ||
| 1669 | 	 * If {@link Extension}s wish to alter URL segment generation, they can do so by defining | 
            ||
| 1670 | * updateURLSegment(&$url, $title). $url will be passed by reference and should be modified. $title will contain  | 
            ||
| 1671 | * the title that was originally used as the source of this generated URL. This lets extensions either start from  | 
            ||
| 1672 | * scratch, or incrementally modify the generated URL.  | 
            ||
| 1673 | *  | 
            ||
| 1674 | * @param string $title Page title  | 
            ||
| 1675 | * @return string Generated url segment  | 
            ||
| 1676 | */  | 
            ||
| 1677 | 	public function generateURLSegment($title){ | 
            ||
| 1678 | $filter = URLSegmentFilter::create();  | 
            ||
| 1679 | $t = $filter->filter($title);  | 
            ||
| 1680 | |||
| 1681 | // Fallback to generic page name if path is empty (= no valid, convertable characters)  | 
            ||
| 1682 | if(!$t || $t == '-' || $t == '-1') $t = "page-$this->ID";  | 
            ||
| 1683 | |||
| 1684 | // Hook for extensions  | 
            ||
| 1685 | 		$this->extend('updateURLSegment', $t, $title); | 
            ||
| 1686 | |||
| 1687 | return $t;  | 
            ||
| 1688 | }  | 
            ||
| 1689 | |||
| 1690 | /**  | 
            ||
| 1691 | * Gets the URL segment for the latest draft version of this page.  | 
            ||
| 1692 | *  | 
            ||
| 1693 | * @return string  | 
            ||
| 1694 | */  | 
            ||
| 1695 | 	public function getStageURLSegment() { | 
            ||
| 1701 | |||
| 1702 | /**  | 
            ||
| 1703 | * Gets the URL segment for the currently published version of this page.  | 
            ||
| 1704 | *  | 
            ||
| 1705 | * @return string  | 
            ||
| 1706 | */  | 
            ||
| 1707 | 	public function getLiveURLSegment() { | 
            ||
| 1713 | |||
| 1714 | /**  | 
            ||
| 1715 | * Returns the pages that depend on this page. This includes virtual pages, pages that link to it, etc.  | 
            ||
| 1716 | *  | 
            ||
| 1717 | * @param bool $includeVirtuals Set to false to exlcude virtual pages.  | 
            ||
| 1718 | * @return ArrayList  | 
            ||
| 1719 | */  | 
            ||
| 1720 | 	public function DependentPages($includeVirtuals = true) { | 
            ||
| 1721 | 		if(class_exists('Subsite')) { | 
            ||
| 1722 | $origDisableSubsiteFilter = Subsite::$disable_subsite_filter;  | 
            ||
| 1723 | Subsite::disable_subsite_filter(true);  | 
            ||
| 1724 | }  | 
            ||
| 1725 | |||
| 1726 | // Content links  | 
            ||
| 1727 | $items = new ArrayList();  | 
            ||
| 1728 | |||
| 1729 | // We merge all into a regular SS_List, because DataList doesn't support merge  | 
            ||
| 1730 | 		if($contentLinks = $this->BackLinkTracking()) { | 
            ||
| 1731 | $linkList = new ArrayList();  | 
            ||
| 1732 | 			foreach($contentLinks as $item) { | 
            ||
| 1770 | |||
| 1771 | /**  | 
            ||
| 1772 | * Return all virtual pages that link to this page.  | 
            ||
| 1773 | *  | 
            ||
| 1774 | * @return DataList  | 
            ||
| 1775 | */  | 
            ||
| 1776 | 	public function VirtualPages() { | 
            ||
| 1798 | |||
| 1799 | /**  | 
            ||
| 1800 | * Returns a FieldList with which to create the main editing form.  | 
            ||
| 1801 | *  | 
            ||
| 1802 | * You can override this in your child classes to add extra fields - first get the parent fields using  | 
            ||
| 1803 | * parent::getCMSFields(), then use addFieldToTab() on the FieldList.  | 
            ||
| 1804 | *  | 
            ||
| 1805 | 	 * See {@link getSettingsFields()} for a different set of fields concerned with configuration aspects on the record, | 
            ||
| 1806 | * e.g. access control.  | 
            ||
| 1807 | *  | 
            ||
| 1808 | * @return FieldList The fields to be displayed in the CMS  | 
            ||
| 1809 | */  | 
            ||
| 1810 | 	public function getCMSFields() { | 
            ||
| 1990 | |||
| 1991 | |||
| 1992 | /**  | 
            ||
| 1993 | 	 * Returns fields related to configuration aspects on this record, e.g. access control. See {@link getCMSFields()} | 
            ||
| 1994 | * for content-related fields.  | 
            ||
| 1995 | *  | 
            ||
| 1996 | * @return FieldList  | 
            ||
| 1997 | */  | 
            ||
| 1998 | 	public function getSettingsFields() { | 
            ||
| 2104 | |||
| 2105 | /**  | 
            ||
| 2106 | * @param bool $includerelations A boolean value to indicate if the labels returned should include relation fields  | 
            ||
| 2107 | * @return array  | 
            ||
| 2108 | */  | 
            ||
| 2109 | 	public function fieldLabels($includerelations = true) { | 
            ||
| 2147 | |||
| 2148 | /**  | 
            ||
| 2149 | * Get the actions available in the CMS for this page - eg Save, Publish.  | 
            ||
| 2150 | *  | 
            ||
| 2151 | * Frontend scripts and styles know how to handle the following FormFields:  | 
            ||
| 2152 | * - top-level FormActions appear as standalone buttons  | 
            ||
| 2153 | * - top-level CompositeField with FormActions within appear as grouped buttons  | 
            ||
| 2154 | * - TabSet & Tabs appear as a drop ups  | 
            ||
| 2155 | * - FormActions within the Tab are restyled as links  | 
            ||
| 2156 | * - major actions can provide alternate states for richer presentation (see ssui.button widget extension)  | 
            ||
| 2157 | *  | 
            ||
| 2158 | * @return FieldList The available actions for this page.  | 
            ||
| 2159 | */  | 
            ||
| 2160 | 	public function getCMSActions() { | 
            ||
| 2311 | |||
| 2312 | /**  | 
            ||
| 2313 | * Publish this page.  | 
            ||
| 2314 | *  | 
            ||
| 2315 | * @uses SiteTreeExtension->onBeforePublish()  | 
            ||
| 2316 | * @uses SiteTreeExtension->onAfterPublish()  | 
            ||
| 2317 | * @return bool True if published  | 
            ||
| 2318 | */  | 
            ||
| 2319 | 	public function doPublish() { | 
            ||
| 2361 | |||
| 2362 | /**  | 
            ||
| 2363 | * Unpublish this page - remove it from the live site  | 
            ||
| 2364 | *  | 
            ||
| 2365 | 	 * Overrides {@see Versioned::doUnpublish()} | 
            ||
| 2366 | *  | 
            ||
| 2367 | * @uses SiteTreeExtension->onBeforeUnpublish()  | 
            ||
| 2368 | * @uses SiteTreeExtension->onAfterUnpublish()  | 
            ||
| 2369 | */  | 
            ||
| 2370 | 	public function doUnpublish() { | 
            ||
| 2408 | |||
| 2409 | /**  | 
            ||
| 2410 | * Revert the draft changes: replace the draft content with the content on live  | 
            ||
| 2411 | */  | 
            ||
| 2412 | 	public function doRevertToLive() { | 
            ||
| 2430 | |||
| 2431 | /**  | 
            ||
| 2432 | * Determine if this page references a parent which is archived, and not available in stage  | 
            ||
| 2433 | *  | 
            ||
| 2434 | * @return bool True if there is an archived parent  | 
            ||
| 2435 | */  | 
            ||
| 2436 | 	protected function isParentArchived() { | 
            ||
| 2445 | |||
| 2446 | /**  | 
            ||
| 2447 | * Restore the content in the active copy of this SiteTree page to the stage site.  | 
            ||
| 2448 | *  | 
            ||
| 2449 | * @return self  | 
            ||
| 2450 | */  | 
            ||
| 2451 | 	public function doRestoreToStage() { | 
            ||
| 2487 | |||
| 2488 | /**  | 
            ||
| 2489 | * @deprecated  | 
            ||
| 2490 | */  | 
            ||
| 2491 | 	public function doDeleteFromLive() { | 
            ||
| 2495 | |||
| 2496 | /**  | 
            ||
| 2497 | * Check if this page is new - that is, if it has yet to have been written to the database.  | 
            ||
| 2498 | *  | 
            ||
| 2499 | * @return bool  | 
            ||
| 2500 | */  | 
            ||
| 2501 | 	public function isNew() { | 
            ||
| 2512 | |||
| 2513 | /**  | 
            ||
| 2514 | * Get the class dropdown used in the CMS to change the class of a page. This returns the list of options in the  | 
            ||
| 2515 | 	 * dropdown as a Map from class name to singular name. Filters by {@link SiteTree->canCreate()}, as well as | 
            ||
| 2516 | 	 * {@link SiteTree::$needs_permission}. | 
            ||
| 2517 | *  | 
            ||
| 2518 | * @return array  | 
            ||
| 2519 | */  | 
            ||
| 2520 | 	protected function getClassDropdown() { | 
            ||
| 2564 | |||
| 2565 | /**  | 
            ||
| 2566 | * Returns an array of the class names of classes that are allowed to be children of this class.  | 
            ||
| 2567 | *  | 
            ||
| 2568 | * @return string[]  | 
            ||
| 2569 | */  | 
            ||
| 2570 | 	public function allowedChildren() { | 
            ||
| 2590 | |||
| 2591 | /**  | 
            ||
| 2592 | * Returns the class name of the default class for children of this page.  | 
            ||
| 2593 | *  | 
            ||
| 2594 | * @return string  | 
            ||
| 2595 | */  | 
            ||
| 2596 | 	public function defaultChild() { | 
            ||
| 2605 | |||
| 2606 | /**  | 
            ||
| 2607 | * Returns the class name of the default class for the parent of this page.  | 
            ||
| 2608 | *  | 
            ||
| 2609 | * @return string  | 
            ||
| 2610 | */  | 
            ||
| 2611 | 	public function defaultParent() { | 
            ||
| 2614 | |||
| 2615 | /**  | 
            ||
| 2616 | * Get the title for use in menus for this page. If the MenuTitle field is set it returns that, else it returns the  | 
            ||
| 2617 | * Title field.  | 
            ||
| 2618 | *  | 
            ||
| 2619 | * @return string  | 
            ||
| 2620 | */  | 
            ||
| 2621 | 	public function getMenuTitle(){ | 
            ||
| 2628 | |||
| 2629 | |||
| 2630 | /**  | 
            ||
| 2631 | * Set the menu title for this page.  | 
            ||
| 2632 | *  | 
            ||
| 2633 | * @param string $value  | 
            ||
| 2634 | */  | 
            ||
| 2635 | 	public function setMenuTitle($value) { | 
            ||
| 2642 | |||
| 2643 | /**  | 
            ||
| 2644 | * A flag provides the user with additional data about the current page status, for example a "removed from draft"  | 
            ||
| 2645 | * status. Each page can have more than one status flag. Returns a map of a unique key to a (localized) title for  | 
            ||
| 2646 | * the flag. The unique key can be reused as a CSS class. Use the 'updateStatusFlags' extension point to customize  | 
            ||
| 2647 | * the flags.  | 
            ||
| 2648 | *  | 
            ||
| 2649 | * Example (simple):  | 
            ||
| 2650 | * "deletedonlive" => "Deleted"  | 
            ||
| 2651 | *  | 
            ||
| 2652 | * Example (with optional title attribute):  | 
            ||
| 2653 | 	 *   "deletedonlive" => array('text' => "Deleted", 'title' => 'This page has been deleted') | 
            ||
| 2654 | *  | 
            ||
| 2655 | * @param bool $cached Whether to serve the fields from cache; false regenerate them  | 
            ||
| 2656 | * @return array  | 
            ||
| 2657 | */  | 
            ||
| 2658 | 	public function getStatusFlags($cached = true) { | 
            ||
| 2692 | |||
| 2693 | /**  | 
            ||
| 2694 | * getTreeTitle will return three <span> html DOM elements, an empty <span> with the class 'jstree-pageicon' in  | 
            ||
| 2695 | * front, following by a <span> wrapping around its MenutTitle, then following by a <span> indicating its  | 
            ||
| 2696 | * publication status.  | 
            ||
| 2697 | *  | 
            ||
| 2698 | * @return string An HTML string ready to be directly used in a template  | 
            ||
| 2699 | */  | 
            ||
| 2700 | 	public function getTreeTitle() { | 
            ||
| 2729 | |||
| 2730 | /**  | 
            ||
| 2731 | * Returns the page in the current page stack of the given level. Level(1) will return the main menu item that  | 
            ||
| 2732 | * we're currently inside, etc.  | 
            ||
| 2733 | *  | 
            ||
| 2734 | * @param int $level  | 
            ||
| 2735 | * @return SiteTree  | 
            ||
| 2736 | */  | 
            ||
| 2737 | 	public function Level($level) { | 
            ||
| 2746 | |||
| 2747 | /**  | 
            ||
| 2748 | * Gets the depth of this page in the sitetree, where 1 is the root level  | 
            ||
| 2749 | *  | 
            ||
| 2750 | * @return int  | 
            ||
| 2751 | */  | 
            ||
| 2752 | 	public function getPageLevel() { | 
            ||
| 2758 | |||
| 2759 | /**  | 
            ||
| 2760 | * Return the CSS classes to apply to this node in the CMS tree.  | 
            ||
| 2761 | *  | 
            ||
| 2762 | * @param string $numChildrenMethod  | 
            ||
| 2763 | * @return string  | 
            ||
| 2764 | */  | 
            ||
| 2765 | 	public function CMSTreeClasses($numChildrenMethod="numChildren") { | 
            ||
| 2796 | |||
| 2797 | /**  | 
            ||
| 2798 | * Compares current draft with live version, and returns true if no draft version of this page exists but the page  | 
            ||
| 2799 | * is still published (eg, after triggering "Delete from draft site" in the CMS).  | 
            ||
| 2800 | *  | 
            ||
| 2801 | * @return bool  | 
            ||
| 2802 | */  | 
            ||
| 2803 | 	public function getIsDeletedFromStage() { | 
            ||
| 2812 | |||
| 2813 | /**  | 
            ||
| 2814 | * Return true if this page exists on the live site  | 
            ||
| 2815 | *  | 
            ||
| 2816 | * @return bool  | 
            ||
| 2817 | */  | 
            ||
| 2818 | 	public function getExistsOnLive() { | 
            ||
| 2821 | |||
| 2822 | /**  | 
            ||
| 2823 | * Compares current draft with live version, and returns true if these versions differ, meaning there have been  | 
            ||
| 2824 | * unpublished changes to the draft site.  | 
            ||
| 2825 | *  | 
            ||
| 2826 | * @return bool  | 
            ||
| 2827 | */  | 
            ||
| 2828 | 	public function getIsModifiedOnStage() { | 
            ||
| 2840 | |||
| 2841 | /**  | 
            ||
| 2842 | * Compares current draft with live version, and returns true if no live version exists, meaning the page was never  | 
            ||
| 2843 | * published.  | 
            ||
| 2844 | *  | 
            ||
| 2845 | * @return bool  | 
            ||
| 2846 | */  | 
            ||
| 2847 | 	public function getIsAddedToStage() { | 
            ||
| 2856 | |||
| 2857 | /**  | 
            ||
| 2858 | * Stops extendCMSFields() being called on getCMSFields(). This is useful when you need access to fields added by  | 
            ||
| 2859 | * subclasses of SiteTree in a extension. Call before calling parent::getCMSFields(), and reenable afterwards.  | 
            ||
| 2860 | */  | 
            ||
| 2861 | 	static public function disableCMSFieldsExtensions() { | 
            ||
| 2864 | |||
| 2865 | /**  | 
            ||
| 2866 | * Reenables extendCMSFields() being called on getCMSFields() after it has been disabled by  | 
            ||
| 2867 | * disableCMSFieldsExtensions().  | 
            ||
| 2868 | */  | 
            ||
| 2869 | 	static public function enableCMSFieldsExtensions() { | 
            ||
| 2872 | |||
| 2873 | 	public function providePermissions() { | 
            ||
| 2907 | |||
| 2908 | /**  | 
            ||
| 2909 | * Return the translated Singular name.  | 
            ||
| 2910 | *  | 
            ||
| 2911 | * @return string  | 
            ||
| 2912 | */  | 
            ||
| 2913 | 	public function i18n_singular_name() { | 
            ||
| 2918 | |||
| 2919 | /**  | 
            ||
| 2920 | * Overloaded to also provide entities for 'Page' class which is usually located in custom code, hence textcollector  | 
            ||
| 2921 | * picks it up for the wrong folder.  | 
            ||
| 2922 | *  | 
            ||
| 2923 | * @return array  | 
            ||
| 2924 | */  | 
            ||
| 2925 | 	public function provideI18nEntities() { | 
            ||
| 2941 | |||
| 2942 | /**  | 
            ||
| 2943 | * Returns 'root' if the current page has no parent, or 'subpage' otherwise  | 
            ||
| 2944 | *  | 
            ||
| 2945 | * @return string  | 
            ||
| 2946 | */  | 
            ||
| 2947 | 	public function getParentType() { | 
            ||
| 2950 | |||
| 2951 | /**  | 
            ||
| 2952 | * Clear the permissions cache for SiteTree  | 
            ||
| 2953 | */  | 
            ||
| 2954 | 	public static function reset() { | 
            ||
| 2957 | |||
| 2958 | 	static public function on_db_reset() { | 
            ||
| 2961 | |||
| 2962 | }  | 
            ||
| 2963 |