2011-03-18 04:01:06 +01:00
< ? php
2016-06-16 06:57:19 +02:00
2016-07-22 01:32:32 +02:00
namespace SilverStripe\CMS\Model ;
2016-08-23 04:36:06 +02:00
use Page ;
use SilverStripe\Admin\AddToCampaignHandler_FormAction ;
use SilverStripe\Admin\CMSPreviewable ;
2016-08-10 06:08:39 +02:00
use SilverStripe\CMS\Controllers\CMSPageEditController ;
use SilverStripe\CMS\Controllers\ContentController ;
2016-08-23 04:36:06 +02:00
use SilverStripe\CMS\Controllers\ModelAsController ;
use SilverStripe\CMS\Controllers\RootURLController ;
use SilverStripe\CMS\Forms\SiteTreeURLSegmentField ;
use SilverStripe\Control\Controller ;
use SilverStripe\Control\Director ;
use SilverStripe\Core\ClassInfo ;
use SilverStripe\Core\Config\Config ;
use SilverStripe\Core\Convert ;
use SilverStripe\Dev\Deprecation ;
use SilverStripe\Forms\CheckboxField ;
use SilverStripe\Forms\CompositeField ;
use SilverStripe\Forms\DropdownField ;
use SilverStripe\Forms\FieldGroup ;
use SilverStripe\Forms\FieldList ;
use SilverStripe\Forms\FormAction ;
use SilverStripe\Forms\FormField ;
use SilverStripe\Forms\GridField\GridField ;
use SilverStripe\Forms\GridField\GridFieldDataColumns ;
use SilverStripe\Forms\HTMLEditor\HTMLEditorField ;
use SilverStripe\Forms\ListboxField ;
use SilverStripe\Forms\LiteralField ;
use SilverStripe\Forms\OptionsetField ;
use SilverStripe\Forms\Tab ;
use SilverStripe\Forms\TabSet ;
use SilverStripe\Forms\TextareaField ;
use SilverStripe\Forms\TextField ;
use SilverStripe\Forms\ToggleCompositeField ;
use SilverStripe\Forms\TreeDropdownField ;
use SilverStripe\i18n\i18n ;
use SilverStripe\i18n\i18nEntityProvider ;
use SilverStripe\ORM\ArrayList ;
use SilverStripe\ORM\DataList ;
2016-06-16 06:57:19 +02:00
use SilverStripe\ORM\DataObject ;
2016-08-23 04:36:06 +02:00
use SilverStripe\ORM\DB ;
use SilverStripe\ORM\HiddenClass ;
2016-06-16 06:57:19 +02:00
use SilverStripe\ORM\Hierarchy\Hierarchy ;
use SilverStripe\ORM\ManyManyList ;
use SilverStripe\ORM\Versioning\Versioned ;
2016-08-23 04:36:06 +02:00
use SilverStripe\Security\Group ;
2016-06-23 01:51:20 +02:00
use SilverStripe\Security\Member ;
use SilverStripe\Security\Permission ;
use SilverStripe\Security\PermissionProvider ;
2016-08-23 04:36:06 +02:00
use SilverStripe\SiteConfig\SiteConfig ;
use SilverStripe\View\ArrayData ;
use SilverStripe\View\Parsers\ShortcodeParser ;
use SilverStripe\View\Parsers\URLSegmentFilter ;
use SilverStripe\View\SSViewer ;
2016-07-22 01:32:32 +02:00
use Subsite ;
2016-06-16 06:57:19 +02:00
2011-03-18 04:01:06 +01:00
/**
2014-12-01 12:22:48 +01:00
* Basic data - object representing all pages within the site tree . All page types that live within the hierarchy should
* inherit from this . In addition , it contains a number of static methods for querying the site tree and working with
* draft and published states .
2016-01-06 00:42:07 +01:00
*
2012-02-06 11:58:04 +01:00
* < h2 > URLs </ h2 >
2014-12-01 12:22:48 +01:00
* A page is identified during request handling via its " URLSegment " database column . As pages can be nested , the full
* path of a URL might contain multiple segments . Each segment is stored in its filtered representation ( through
* { @ link URLSegmentFilter }) . The full path is constructed via { @ link Link ()}, { @ link RelativeLink ()} and
* { @ link AbsoluteLink ()} . You can allow these segments to contain multibyte characters through
* { @ link URLSegmentFilter :: $default_allow_multibyte } .
2014-02-10 00:20:55 +01:00
*
* @ property string URLSegment
* @ property string Title
* @ property string MenuTitle
* @ property string Content HTML content of the page .
* @ property string MetaDescription
* @ property string ExtraMeta
* @ property string ShowInMenus
* @ property string ShowInSearch
* @ property string Sort Integer value denoting the sort order .
* @ property string ReportClass
* @ property string CanViewType Type of restriction for viewing this object .
* @ property string CanEditType Type of restriction for editing this object .
*
2016-08-11 04:10:51 +02:00
* @ method ManyManyList ViewerGroups () List of groups that can view this object .
* @ method ManyManyList EditorGroups () List of groups that can edit this object .
2016-08-10 06:08:39 +02:00
* @ method SiteTree Parent ()
2014-12-01 12:22:48 +01:00
*
* @ mixin Hierarchy
* @ mixin Versioned
* @ mixin SiteTreeLinkTracking
2011-03-18 04:01:06 +01:00
*/
2011-07-21 20:01:00 +02:00
class SiteTree extends DataObject implements PermissionProvider , i18nEntityProvider , CMSPreviewable {
2011-03-18 04:01:06 +01:00
/**
* Indicates what kind of children this page type can have .
* This can be an array of allowed child classes , or the string " none " -
* indicating that this page type can ' t have children .
* If a classname is prefixed by " * " , such as " *Page " , then only that
* class is allowed - no subclasses . Otherwise , the class and all its
* subclasses are allowed .
2011-10-06 16:47:59 +02:00
* To control allowed children on root level ( no parent ), use { @ link $can_be_root } .
2016-01-06 00:42:07 +01:00
*
2012-04-13 18:42:15 +02:00
* Note that this setting is cached when used in the CMS , use the " flush " query parameter to clear it .
2011-03-18 04:01:06 +01:00
*
2013-03-18 11:47:15 +01:00
* @ config
2011-03-18 04:01:06 +01:00
* @ var array
*/
2016-07-22 01:32:32 +02:00
private static $allowed_children = array ( " SilverStripe \\ CMS \\ Model \\ SiteTree " );
2011-03-18 04:01:06 +01:00
/**
* The default child class for this page .
2012-04-13 18:42:15 +02:00
* Note : Value might be cached , see { @ link $allowed_chilren } .
2011-03-18 04:01:06 +01:00
*
2013-03-18 11:47:15 +01:00
* @ config
2011-03-18 04:01:06 +01:00
* @ var string
*/
2013-03-18 11:47:15 +01:00
private static $default_child = " Page " ;
2011-03-18 04:01:06 +01:00
2016-01-21 05:12:24 +01:00
/**
* Default value for SiteTree . ClassName enum
* { @ see DBClassName :: getDefault }
*
* @ config
* @ var string
*/
private static $default_classname = " Page " ;
2011-03-18 04:01:06 +01:00
/**
* The default parent class for this page .
2012-04-13 18:42:15 +02:00
* Note : Value might be cached , see { @ link $allowed_chilren } .
2011-03-18 04:01:06 +01:00
*
2013-03-18 11:47:15 +01:00
* @ config
2011-03-18 04:01:06 +01:00
* @ var string
*/
2013-03-18 11:47:15 +01:00
private static $default_parent = null ;
2011-03-18 04:01:06 +01:00
/**
* Controls whether a page can be in the root of the site tree .
2012-04-13 18:42:15 +02:00
* Note : Value might be cached , see { @ link $allowed_chilren } .
2011-03-18 04:01:06 +01:00
*
2013-03-18 11:47:15 +01:00
* @ config
2011-03-18 04:01:06 +01:00
* @ var bool
*/
2013-03-18 11:47:15 +01:00
private static $can_be_root = true ;
2011-03-18 04:01:06 +01:00
/**
2012-04-13 18:42:15 +02:00
* List of permission codes a user can have to allow a user to create a page of this type .
* Note : Value might be cached , see { @ link $allowed_chilren } .
2011-03-18 04:01:06 +01:00
*
2013-03-18 11:47:15 +01:00
* @ config
2011-03-18 04:01:06 +01:00
* @ var array
*/
2013-03-18 11:47:15 +01:00
private static $need_permission = null ;
2011-03-18 04:01:06 +01:00
/**
* If you extend a class , and don ' t want to be able to select the old class
* in the cms , set this to the old class name . Eg , if you extended Product
* to make ImprovedProduct , then you would set $hide_ancestor to Product .
*
2013-03-18 11:47:15 +01:00
* @ config
2011-03-18 04:01:06 +01:00
* @ var string
*/
2013-03-18 11:47:15 +01:00
private static $hide_ancestor = null ;
2011-03-18 04:01:06 +01:00
2013-03-18 11:47:15 +01:00
private static $db = array (
2011-03-18 04:01:06 +01:00
" URLSegment " => " Varchar(255) " ,
" Title " => " Varchar(255) " ,
" MenuTitle " => " Varchar(100) " ,
" Content " => " HTMLText " ,
" MetaDescription " => " Text " ,
2015-02-11 00:13:55 +01:00
" ExtraMeta " => " HTMLFragment(['whitelist' => ['meta', 'link']]) " ,
2011-03-18 04:01:06 +01:00
" ShowInMenus " => " Boolean " ,
" ShowInSearch " => " Boolean " ,
" Sort " => " Int " ,
" HasBrokenFile " => " Boolean " ,
" HasBrokenLink " => " Boolean " ,
" ReportClass " => " Varchar " ,
" CanViewType " => " Enum('Anyone, LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit') " ,
" CanEditType " => " Enum('LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit') " ,
);
2013-03-18 11:47:15 +01:00
private static $indexes = array (
2011-03-18 04:01:06 +01:00
" URLSegment " => true ,
);
2013-03-18 11:47:15 +01:00
private static $many_many = array (
2016-06-23 01:51:20 +02:00
" ViewerGroups " => " SilverStripe \\ Security \\ Group " ,
" EditorGroups " => " SilverStripe \\ Security \\ Group " ,
2011-03-18 04:01:06 +01:00
);
2016-03-17 01:02:50 +01:00
private static $has_many = array (
2016-08-10 06:08:39 +02:00
" VirtualPages " => " SilverStripe \\ CMS \\ Model \\ VirtualPage.CopyContentFrom "
2016-03-17 01:02:50 +01:00
);
private static $owned_by = array (
" VirtualPages "
);
2013-03-18 11:47:15 +01:00
private static $casting = array (
2015-02-11 00:13:55 +01:00
" Breadcrumbs " => " HTMLFragment " ,
" LastEdited " => " Datetime " ,
" Created " => " Datetime " ,
2011-03-18 04:01:06 +01:00
'Link' => 'Text' ,
'RelativeLink' => 'Text' ,
'AbsoluteLink' => 'Text' ,
2015-02-11 00:13:55 +01:00
'CMSEditLink' => 'Text' ,
'TreeTitle' => 'HTMLFragment' ,
'MetaTags' => 'HTMLFragment' ,
2011-03-18 04:01:06 +01:00
);
2013-03-18 11:47:15 +01:00
private static $defaults = array (
2011-03-18 04:01:06 +01:00
" ShowInMenus " => 1 ,
" ShowInSearch " => 1 ,
" CanViewType " => " Inherit " ,
" CanEditType " => " Inherit "
);
2016-08-10 06:08:39 +02:00
private static $table_name = 'SiteTree' ;
2013-03-18 11:47:15 +01:00
private static $versioning = array (
2011-03-18 04:01:06 +01:00
" Stage " , " Live "
);
2013-03-18 11:47:15 +01:00
private static $default_sort = " \" Sort \" " ;
2011-03-18 04:01:06 +01:00
/**
2012-04-11 05:48:17 +02:00
* If this is false , the class cannot be created in the CMS by regular content authors , only by ADMINs .
2011-03-18 04:01:06 +01:00
* @ var boolean
2014-12-01 12:22:48 +01:00
* @ config
*/
2013-03-18 11:47:15 +01:00
private static $can_create = true ;
2011-03-18 04:01:06 +01:00
/**
2011-04-12 01:43:01 +02:00
* Icon to use in the CMS page tree . This should be the full filename , relative to the webroot .
* Also supports custom CSS rule contents ( applied to the correct selector for the tree UI implementation ) .
2016-01-06 00:42:07 +01:00
*
2011-04-12 01:43:01 +02:00
* @ see CMSMain :: generateTreeStylingCSS ()
2013-03-18 11:47:15 +01:00
* @ config
2011-04-12 01:43:01 +02:00
* @ var string
2011-03-18 04:01:06 +01:00
*/
2013-03-18 11:47:15 +01:00
private static $icon = null ;
2016-03-08 21:50:55 +01:00
2011-04-24 01:03:51 +02:00
/**
2013-03-18 11:47:15 +01:00
* @ config
2015-09-28 11:31:31 +02:00
* @ var string Description of the class functionality , typically shown to a user
2011-04-24 01:03:51 +02:00
* when selecting which page type to create . Translated through { @ link provideI18nEntities ()} .
*/
2013-03-18 11:47:15 +01:00
private static $description = 'Generic content page' ;
2011-03-18 04:01:06 +01:00
2013-03-18 11:47:15 +01:00
private static $extensions = array (
2016-08-23 04:36:06 +02:00
'SilverStripe\\ORM\\Hierarchy\\Hierarchy' ,
'SilverStripe\\ORM\\Versioning\\Versioned' ,
2016-07-22 01:32:32 +02:00
" SilverStripe \\ CMS \\ Model \\ SiteTreeLinkTracking "
2011-03-18 04:01:06 +01:00
);
2016-03-08 21:50:55 +01:00
2013-03-18 11:47:15 +01:00
private static $searchable_fields = array (
2011-03-18 04:01:06 +01:00
'Title' ,
'Content' ,
);
2012-05-10 14:18:22 +02:00
2013-03-18 11:47:15 +01:00
private static $field_labels = array (
2012-05-10 14:18:22 +02:00
'URLSegment' => 'URL'
);
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
2013-03-18 11:47:15 +01:00
* @ config
2011-03-18 04:01:06 +01:00
*/
2012-10-08 17:05:46 +02:00
private static $nested_urls = true ;
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
2013-03-18 11:47:15 +01:00
* @ config
2011-03-18 04:01:06 +01:00
*/
private static $create_default_pages = true ;
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
* This controls whether of not extendCMSFields () is called by getCMSFields .
*/
private static $runCMSFieldsExtensions = true ;
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
2011-09-09 17:35:11 +02:00
* Cache for canView / Edit / Publish / Delete permissions .
* Keyed by permission type ( e . g . 'edit' ), with an array
* of IDs mapped to their boolean permission ability ( true = allow , false = deny ) .
* See { @ link batch_permission_check ()} for details .
2011-03-18 04:01:06 +01:00
*/
2013-03-18 11:47:15 +01:00
private static $cache_permissions = array ();
2012-06-12 15:55:05 +02:00
2011-03-18 04:01:06 +01:00
/**
2013-03-18 11:47:15 +01:00
* @ config
2011-09-23 09:58:08 +02:00
* @ var boolean
2011-03-18 04:01:06 +01:00
*/
2013-03-18 11:47:15 +01:00
private static $enforce_strict_hierarchy = true ;
2012-06-12 15:55:05 +02:00
2013-06-17 22:29:02 +02:00
/**
2014-12-01 12:22:48 +01:00
* The value used for the meta generator tag . Leave blank to omit the tag .
2013-06-17 22:29:02 +02:00
*
* @ config
* @ var string
*/
private static $meta_generator = 'SilverStripe - http://silverstripe.org' ;
2012-06-12 15:55:05 +02:00
protected $_cache_statusFlags = null ;
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
* Fetches the { @ link SiteTree } object that maps to a link .
*
2013-03-18 11:47:15 +01:00
* If you have enabled { @ link SiteTree :: config () -> nested_urls } on this site , then you can use a nested link such as
2011-03-18 04:01:06 +01:00
* " about-us/staff/ " , and this function will traverse down the URL chain and grab the appropriate link .
*
2011-04-15 11:37:15 +02:00
* Note that if no model can be found , this method will fall over to a extended alternateGetByLink method provided
* by a extension attached to { @ link SiteTree }
2011-03-18 04:01:06 +01:00
*
2014-12-01 12:22:48 +01:00
* @ param string $link The link of the page to search for
* @ param bool $cache True ( default ) to use caching , false to force a fresh search from the database
2011-03-18 04:01:06 +01:00
* @ return SiteTree
*/
2012-09-19 12:07:46 +02:00
static public function get_by_link ( $link , $cache = true ) {
2011-03-18 04:01:06 +01:00
if ( trim ( $link , '/' )) {
$link = trim ( Director :: makeRelative ( $link ), '/' );
} else {
$link = RootURLController :: get_homepage_link ();
}
2016-03-08 21:50:55 +01:00
2013-06-21 00:45:33 +02:00
$parts = preg_split ( '|/+|' , $link );
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// Grab the initial root level page to traverse down from.
$URLSegment = array_shift ( $parts );
2013-06-21 00:45:33 +02:00
$conditions = array ( '"SiteTree"."URLSegment"' => rawurlencode ( $URLSegment ));
if ( self :: config () -> nested_urls ) {
$conditions [] = array ( '"SiteTree"."ParentID"' => 0 );
}
2016-08-10 06:08:39 +02:00
/** @var SiteTree $sitetree */
2016-09-28 00:39:28 +02:00
$sitetree = DataObject :: get_one ( self :: class , $conditions , $cache );
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/// Fall back on a unique URLSegment for b/c.
2013-06-21 00:45:33 +02:00
if ( ! $sitetree
2013-08-29 03:59:45 +02:00
&& self :: config () -> nested_urls
2016-09-28 00:39:28 +02:00
&& $sitetree = DataObject :: get_one ( self :: class , array (
2013-06-21 00:45:33 +02:00
'"SiteTree"."URLSegment"' => $URLSegment
), $cache )
2013-08-29 03:59:45 +02:00
) {
2016-08-10 06:08:39 +02:00
return $sitetree ;
2011-03-18 04:01:06 +01:00
}
2016-03-08 21:50:55 +01:00
2011-04-15 11:37:15 +02:00
// Attempt to grab an alternative page from extensions.
2011-03-18 04:01:06 +01:00
if ( ! $sitetree ) {
2013-03-18 11:47:15 +01:00
$parentID = self :: config () -> nested_urls ? 0 : null ;
2016-03-08 21:50:55 +01:00
2016-08-10 06:08:39 +02:00
if ( $alternatives = static :: singleton () -> extend ( 'alternateGetByLink' , $URLSegment , $parentID )) {
foreach ( $alternatives as $alternative ) {
if ( $alternative ) {
$sitetree = $alternative ;
2016-08-11 03:18:02 +02:00
}
2016-08-10 06:08:39 +02:00
}
2011-03-18 04:01:06 +01:00
}
2016-03-08 21:50:55 +01:00
2016-08-10 06:08:39 +02:00
if ( ! $sitetree ) {
return null ;
2016-08-11 03:18:02 +02:00
}
2011-03-18 04:01:06 +01:00
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// Check if we have any more URL parts to parse.
2016-08-10 06:08:39 +02:00
if ( ! self :: config () -> nested_urls || ! count ( $parts )) {
return $sitetree ;
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// Traverse down the remaining URL segments and grab the relevant SiteTree objects.
foreach ( $parts as $segment ) {
2016-10-25 02:22:31 +02:00
$next = DataObject :: get_one ( self :: class , array (
2016-01-06 00:42:07 +01:00
'"SiteTree"."URLSegment"' => $segment ,
2013-06-21 00:45:33 +02:00
'"SiteTree"."ParentID"' => $sitetree -> ID
),
2013-08-29 03:59:45 +02:00
$cache
2011-03-18 04:01:06 +01:00
);
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
if ( ! $next ) {
$parentID = ( int ) $sitetree -> ID ;
2016-03-08 21:50:55 +01:00
2016-08-10 06:08:39 +02:00
if ( $alternatives = static :: singleton () -> extend ( 'alternateGetByLink' , $segment , $parentID )) {
2011-03-18 04:01:06 +01:00
foreach ( $alternatives as $alternative ) if ( $alternative ) $next = $alternative ;
}
2016-03-08 21:50:55 +01:00
2016-08-10 06:08:39 +02:00
if ( ! $next ) {
return null ;
}
2011-03-18 04:01:06 +01:00
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
$sitetree -> destroy ();
$sitetree = $next ;
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
return $sitetree ;
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
2014-12-01 12:22:48 +01:00
* Return a subclass map of SiteTree that shouldn ' t be hidden through { @ link SiteTree :: $hide_ancestor }
2011-03-18 04:01:06 +01:00
*
* @ return array
*/
2015-11-04 11:17:04 +01:00
public static function page_type_classes () {
2011-03-18 04:01:06 +01:00
$classes = ClassInfo :: getValidSubClasses ();
2016-09-28 00:39:28 +02:00
$baseClassIndex = array_search ( self :: class , $classes );
2016-08-11 04:10:51 +02:00
if ( $baseClassIndex !== false ) {
unset ( $classes [ $baseClassIndex ]);
}
2011-03-18 04:01:06 +01:00
$kill_ancestors = array ();
// figure out if there are any classes we don't want to appear
foreach ( $classes as $class ) {
$instance = singleton ( $class );
// do any of the progeny want to hide an ancestor?
if ( $ancestor_to_hide = $instance -> stat ( 'hide_ancestor' )) {
// note for killing later
$kill_ancestors [] = $ancestor_to_hide ;
}
}
2014-12-01 12:22:48 +01:00
// If any of the descendents don't want any of the elders to show up, cruelly render the elders surplus to
// requirements
2011-03-18 04:01:06 +01:00
if ( $kill_ancestors ) {
$kill_ancestors = array_unique ( $kill_ancestors );
foreach ( $kill_ancestors as $mark ) {
// unset from $classes
2015-11-04 11:17:04 +01:00
$idx = array_search ( $mark , $classes , true );
if ( $idx !== false ) {
unset ( $classes [ $idx ]);
}
2011-03-18 04:01:06 +01:00
}
}
return $classes ;
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
2012-07-15 21:29:46 +02:00
* Replace a " [sitetree_link id=n] " shortcode with a link to the page with the corresponding ID .
2011-03-18 04:01:06 +01:00
*
2016-08-11 03:18:02 +02:00
* @ param array $arguments
* @ param string $content
2016-08-10 06:08:39 +02:00
* @ param ShortcodeParser $parser
2014-12-01 12:22:48 +01:00
* @ return string
2011-03-18 04:01:06 +01:00
*/
2012-09-19 12:07:46 +02:00
static public function link_shortcode_handler ( $arguments , $content = null , $parser = null ) {
2016-08-10 06:08:39 +02:00
if ( ! isset ( $arguments [ 'id' ]) || ! is_numeric ( $arguments [ 'id' ])) {
return null ;
}
2016-03-08 21:50:55 +01:00
2016-08-11 04:10:51 +02:00
/** @var SiteTree $page */
2011-03-18 04:01:06 +01:00
if (
2016-09-28 00:39:28 +02:00
! ( $page = DataObject :: get_by_id ( self :: class , $arguments [ 'id' ])) // Get the current page by ID.
&& ! ( $page = Versioned :: get_latest_version ( self :: class , $arguments [ 'id' ])) // Attempt link to old version.
2011-03-18 04:01:06 +01:00
) {
2016-03-17 01:02:50 +01:00
return null ; // There were no suitable matches at all.
2011-03-18 04:01:06 +01:00
}
2013-06-21 00:45:33 +02:00
2016-08-10 06:08:39 +02:00
/** @var SiteTree $page */
2013-05-10 04:05:06 +02:00
$link = Convert :: raw2att ( $page -> Link ());
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
if ( $content ) {
2013-05-10 04:05:06 +02:00
return sprintf ( '<a href="%s">%s</a>' , $link , $parser -> parse ( $content ));
2011-03-18 04:01:06 +01:00
} else {
2013-05-10 04:05:06 +02:00
return $link ;
2011-03-18 04:01:06 +01:00
}
}
2014-12-01 12:22:48 +01:00
2011-03-18 04:01:06 +01:00
/**
* Return the link for this { @ link SiteTree } object , with the { @ link Director :: baseURL ()} included .
*
2014-12-01 12:22:48 +01:00
* @ param string $action Optional controller action ( method ) .
* Note : URI encoding of this parameter is applied automatically through template casting ,
* don ' t encode the passed parameter . Please use { @ link Controller :: join_links ()} instead to
* append GET parameters .
2011-03-18 04:01:06 +01:00
* @ return string
*/
public function Link ( $action = null ) {
return Controller :: join_links ( Director :: baseURL (), $this -> RelativeLink ( $action ));
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
* Get the absolute URL for this page , including protocol and host .
*
* @ param string $action See { @ link Link ()}
* @ return string
*/
public function AbsoluteLink ( $action = null ) {
if ( $this -> hasMethod ( 'alternateAbsoluteLink' )) {
return $this -> alternateAbsoluteLink ( $action );
} else {
return Director :: absoluteURL ( $this -> Link ( $action ));
}
}
2016-03-08 21:50:55 +01:00
2013-01-14 12:06:34 +01:00
/**
2014-12-01 12:22:48 +01:00
* Base link used for previewing . Defaults to absolute URL , in order to account for domain changes , e . g . on multi
* site setups . Does not contain hints about the stage , see { @ link SilverStripeNavigator } for details .
2016-01-06 00:42:07 +01:00
*
2013-01-14 12:06:34 +01:00
* @ param string $action See { @ link Link ()}
* @ return string
*/
public function PreviewLink ( $action = null ) {
2013-05-17 04:57:17 +02:00
if ( $this -> hasMethod ( 'alternatePreviewLink' )) {
2016-04-27 09:47:25 +02:00
Deprecation :: notice ( '5.0' , 'Use updatePreviewLink or override PreviewLink method' );
2013-05-17 04:57:17 +02:00
return $this -> alternatePreviewLink ( $action );
}
2016-04-27 09:47:25 +02:00
$link = $this -> AbsoluteLink ( $action );
$this -> extend ( 'updatePreviewLink' , $link , $action );
return $link ;
}
public function getMimeType () {
return 'text/html' ;
2013-01-14 12:06:34 +01:00
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
* Return the link for this { @ link SiteTree } object relative to the SilverStripe root .
*
2014-12-01 12:22:48 +01:00
* By default , if this page is the current home page , and there is no action specified then this will return a link
2011-03-18 04:01:06 +01:00
* to the root of the site . However , if you set the $action parameter to TRUE then the link will not be rewritten
* and returned in its full form .
*
* @ uses RootURLController :: get_homepage_link ()
2016-01-06 00:42:07 +01:00
*
2011-03-18 04:01:06 +01:00
* @ param string $action See { @ link Link ()}
* @ return string
*/
public function RelativeLink ( $action = null ) {
2013-03-18 11:47:15 +01:00
if ( $this -> ParentID && self :: config () -> nested_urls ) {
2015-05-15 01:51:23 +02:00
$parent = $this -> Parent ();
// If page is removed select parent from version history (for archive page view)
2016-10-05 03:08:34 +02:00
if (( ! $parent || ! $parent -> exists ()) && ! $this -> isOnDraft ()) {
2016-09-28 00:39:28 +02:00
$parent = Versioned :: get_latest_version ( self :: class , $this -> ParentID );
2015-05-15 01:51:23 +02:00
}
$base = $parent -> RelativeLink ( $this -> URLSegment );
2013-05-10 02:22:22 +02:00
} elseif ( ! $action && $this -> URLSegment == RootURLController :: get_homepage_link ()) {
// Unset base for root-level homepages.
2016-01-06 00:42:07 +01:00
// Note: Homepages with action parameters (or $action === true)
2013-05-10 02:22:22 +02:00
// need to retain their URLSegment.
$base = null ;
2011-03-18 04:01:06 +01:00
} else {
$base = $this -> URLSegment ;
}
2016-03-08 21:50:55 +01:00
2013-05-10 02:22:22 +02:00
$this -> extend ( 'updateRelativeLink' , $base , $action );
2016-03-08 21:50:55 +01:00
2013-05-10 02:22:22 +02:00
// Legacy support: If $action === true, retain URLSegment for homepages,
// but don't append any action
2011-03-18 04:01:06 +01:00
if ( $action === true ) $action = null ;
return Controller :: join_links ( $base , '/' , $action );
}
2014-12-01 12:22:48 +01:00
2011-03-18 04:01:06 +01:00
/**
* Get the absolute URL for this page on the Live site .
2014-12-01 12:22:48 +01:00
*
* @ param bool $includeStageEqualsLive Whether to append the URL with ? stage = Live to force Live mode
* @ return string
2011-03-18 04:01:06 +01:00
*/
public function getAbsoluteLiveLink ( $includeStageEqualsLive = true ) {
2016-06-02 05:56:45 +02:00
$oldReadingMode = Versioned :: get_reading_mode ();
2016-03-17 01:02:50 +01:00
Versioned :: set_stage ( Versioned :: LIVE );
2016-08-10 06:08:39 +02:00
/** @var SiteTree $live */
2016-09-28 00:39:28 +02:00
$live = Versioned :: get_one_by_stage ( self :: class , Versioned :: LIVE , array (
2013-06-21 00:45:33 +02:00
'"SiteTree"."ID"' => $this -> ID
));
2011-03-18 04:01:06 +01:00
if ( $live ) {
$link = $live -> AbsoluteLink ();
2016-07-12 05:57:52 +02:00
if ( $includeStageEqualsLive ) {
$link = Controller :: join_links ( $link , '?stage=Live' );
}
2012-08-28 19:00:42 +02:00
} else {
$link = null ;
2011-03-18 04:01:06 +01:00
}
2012-08-28 19:00:42 +02:00
2016-06-02 05:56:45 +02:00
Versioned :: set_reading_mode ( $oldReadingMode );
2012-08-28 19:00:42 +02:00
return $link ;
2011-03-18 04:01:06 +01:00
}
2016-03-08 21:50:55 +01:00
2011-07-21 20:01:00 +02:00
/**
2014-12-01 12:22:48 +01:00
* Generates a link to edit this page in the CMS .
*
* @ return string
2011-07-21 20:01:00 +02:00
*/
2012-09-19 12:07:46 +02:00
public function CMSEditLink () {
2016-04-27 09:47:25 +02:00
$link = Controller :: join_links (
2016-08-10 06:08:39 +02:00
CMSPageEditController :: singleton () -> Link ( 'show' ),
2016-04-27 09:47:25 +02:00
$this -> ID
);
return Director :: absoluteURL ( $link );
2011-07-21 20:01:00 +02:00
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
* Return a CSS identifier generated from this page ' s link .
*
* @ return string The URL segment
*/
public function ElementName () {
return str_replace ( '/' , '-' , trim ( $this -> RelativeLink ( true ), '/' ));
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
2014-12-01 12:22:48 +01:00
* Returns true if this is the currently active page being used to handle this request .
2011-03-18 04:01:06 +01:00
*
* @ return bool
*/
public function isCurrent () {
2016-08-10 06:08:39 +02:00
$currentPage = Director :: get_current_page ();
if ( $currentPage instanceof ContentController ) {
$currentPage = $currentPage -> data ();
2016-08-11 03:18:02 +02:00
}
2016-08-10 06:08:39 +02:00
if ( $currentPage instanceof SiteTree ) {
return $currentPage === $this || $currentPage -> ID === $this -> ID ;
}
return false ;
2011-03-18 04:01:06 +01:00
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
2014-12-01 12:22:48 +01:00
* Check if this page is in the currently active section ( e . g . it is either current or one of its children is
* currently being viewed ) .
2011-03-18 04:01:06 +01:00
*
* @ return bool
*/
public function isSection () {
return $this -> isCurrent () || (
Director :: get_current_page () instanceof SiteTree && in_array ( $this -> ID , Director :: get_current_page () -> getAncestors () -> column ())
);
}
2016-03-08 21:50:55 +01:00
2014-04-07 02:21:57 +02:00
/**
2014-12-01 12:22:48 +01:00
* Check if the parent of this page has been removed ( or made otherwise unavailable ), and is still referenced by
* this child . Any such orphaned page may still require access via the CMS , but should not be shown as accessible
* to external users .
2016-01-06 00:42:07 +01:00
*
2014-04-07 02:21:57 +02:00
* @ return bool
*/
public function isOrphaned () {
// Always false for root pages
2016-08-10 06:08:39 +02:00
if ( empty ( $this -> ParentID )) {
return false ;
}
2016-03-08 21:50:55 +01:00
2014-04-07 02:21:57 +02:00
// Parent must exist and not be an orphan itself
$parent = $this -> Parent ();
return ! $parent || ! $parent -> exists () || $parent -> isOrphaned ();
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
* Return " link " or " current " depending on if this is the { @ link SiteTree :: isCurrent ()} current page .
*
* @ return string
*/
public function LinkOrCurrent () {
return $this -> isCurrent () ? 'current' : 'link' ;
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
* Return " link " or " section " depending on if this is the { @ link SiteTree :: isSeciton ()} current section .
*
* @ return string
*/
public function LinkOrSection () {
return $this -> isSection () ? 'section' : 'link' ;
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
2014-12-01 12:22:48 +01:00
* Return " link " , " current " or " section " depending on if this page is the current page , or not on the current page
* but in the current section .
2011-03-18 04:01:06 +01:00
*
* @ return string
*/
public function LinkingMode () {
if ( $this -> isCurrent ()) {
return 'current' ;
} elseif ( $this -> isSection ()) {
return 'section' ;
} else {
return 'link' ;
}
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
* Check if this page is in the given current section .
*
2014-12-01 12:22:48 +01:00
* @ param string $sectionName Name of the section to check
* @ return bool True if we are in the given section
2011-03-18 04:01:06 +01:00
*/
public function InSection ( $sectionName ) {
$page = Director :: get_current_page ();
2016-08-10 06:08:39 +02:00
while ( $page && $page -> exists ()) {
if ( $sectionName == $page -> URLSegment ) {
2011-03-18 04:01:06 +01:00
return true ;
2016-08-10 06:08:39 +02:00
}
$page = $page -> Parent ();
2011-03-18 04:01:06 +01:00
}
return false ;
}
/**
2016-06-13 05:47:47 +02:00
* Reset Sort on duped page
2011-03-18 04:01:06 +01:00
*
2016-06-13 05:47:47 +02:00
* @ param SiteTree $original
* @ param bool $doWrite
2011-03-18 04:01:06 +01:00
*/
2016-06-13 05:47:47 +02:00
public function onBeforeDuplicate ( $original , $doWrite ) {
$this -> Sort = 0 ;
2011-03-18 04:01:06 +01:00
}
/**
2014-12-01 12:22:48 +01:00
* Duplicates each child of this node recursively and returns the top - level duplicate node .
2011-03-18 04:01:06 +01:00
*
2016-08-10 06:08:39 +02:00
* @ return static The duplicated object
2011-03-18 04:01:06 +01:00
*/
public function duplicateWithChildren () {
2016-08-10 06:08:39 +02:00
/** @var SiteTree $clone */
2011-03-18 04:01:06 +01:00
$clone = $this -> duplicate ();
$children = $this -> AllChildren ();
if ( $children ) {
2016-08-10 06:08:39 +02:00
/** @var SiteTree $child */
2011-03-18 04:01:06 +01:00
foreach ( $children as $child ) {
2016-08-10 06:08:39 +02:00
$childClone = $child -> duplicateWithChildren ();
2011-03-18 04:01:06 +01:00
$childClone -> ParentID = $clone -> ID ;
$childClone -> write ();
}
}
return $clone ;
}
/**
2014-12-01 12:22:48 +01:00
* Duplicate this node and its children as a child of the node with the given ID
2011-03-18 04:01:06 +01:00
*
* @ param int $id ID of the new node ' s new parent
*/
public function duplicateAsChild ( $id ) {
2016-08-10 06:08:39 +02:00
/** @var SiteTree $newSiteTree */
2011-03-18 04:01:06 +01:00
$newSiteTree = $this -> duplicate ();
$newSiteTree -> ParentID = $id ;
$newSiteTree -> Sort = 0 ;
$newSiteTree -> write ();
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
2014-12-01 12:22:48 +01:00
* Return a breadcrumb trail to this page . Excludes " hidden " pages ( with ShowInMenus = 0 ) by default .
2011-03-18 04:01:06 +01:00
*
* @ param int $maxDepth The maximum depth to traverse .
2014-05-08 04:09:02 +02:00
* @ param boolean $unlinked Whether to link page titles .
2014-02-10 21:35:13 +01:00
* @ param boolean | string $stopAtPageType ClassName of a page to stop the upwards traversal .
* @ param boolean $showHidden Include pages marked with the attribute ShowInMenus = 0
2015-02-11 00:13:55 +01:00
* @ return string The breadcrumb trail .
2011-03-18 04:01:06 +01:00
*/
public function Breadcrumbs ( $maxDepth = 20 , $unlinked = false , $stopAtPageType = false , $showHidden = false ) {
2014-05-08 04:09:02 +02:00
$pages = $this -> getBreadcrumbItems ( $maxDepth , $stopAtPageType , $showHidden );
$template = new SSViewer ( 'BreadcrumbsTemplate' );
return $template -> process ( $this -> customise ( new ArrayData ( array (
" Pages " => $pages ,
" Unlinked " => $unlinked
))));
}
/**
* Returns a list of breadcrumbs for the current page .
*
* @ param int $maxDepth The maximum depth to traverse .
* @ param boolean | string $stopAtPageType ClassName of a page to stop the upwards traversal .
* @ param boolean $showHidden Include pages marked with the attribute ShowInMenus = 0
*
* @ return ArrayList
*/
public function getBreadcrumbItems ( $maxDepth = 20 , $stopAtPageType = false , $showHidden = false ) {
2011-03-18 04:01:06 +01:00
$page = $this ;
2012-02-11 03:13:27 +01:00
$pages = array ();
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
while (
2016-01-06 00:42:07 +01:00
$page
2016-08-10 06:08:39 +02:00
&& $page -> exists ()
2016-01-06 00:42:07 +01:00
&& ( ! $maxDepth || count ( $pages ) < $maxDepth )
2011-03-18 04:01:06 +01:00
&& ( ! $stopAtPageType || $page -> ClassName != $stopAtPageType )
) {
2016-01-06 00:42:07 +01:00
if ( $showHidden || $page -> ShowInMenus || ( $page -> ID == $this -> ID )) {
2012-02-11 03:13:27 +01:00
$pages [] = $page ;
2011-03-18 04:01:06 +01:00
}
2016-03-08 21:50:55 +01:00
2016-08-10 06:08:39 +02:00
$page = $page -> Parent ();
2011-03-18 04:01:06 +01:00
}
2014-05-08 04:09:02 +02:00
return new ArrayList ( array_reverse ( $pages ));
2011-03-18 04:01:06 +01:00
}
2014-05-08 04:09:02 +02:00
2011-03-18 04:01:06 +01:00
/**
* Make this page a child of another page .
2016-01-06 00:42:07 +01:00
*
2014-12-01 12:22:48 +01:00
* If the parent page does not exist , resolve it to a valid ID before updating this page ' s reference .
2011-03-18 04:01:06 +01:00
*
* @ param SiteTree | int $item Either the parent object , or the parent ID
*/
public function setParent ( $item ) {
if ( is_object ( $item )) {
if ( ! $item -> exists ()) $item -> write ();
$this -> setField ( " ParentID " , $item -> ID );
} else {
$this -> setField ( " ParentID " , $item );
}
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
* Get the parent of this page .
*
2014-12-01 12:22:48 +01:00
* @ return SiteTree Parent of this page
2011-03-18 04:01:06 +01:00
*/
public function getParent () {
2013-06-21 00:45:33 +02:00
if ( $parentID = $this -> getField ( " ParentID " )) {
2016-07-22 01:32:32 +02:00
return DataObject :: get_by_id ( " SilverStripe \\ CMS \\ Model \\ SiteTree " , $parentID );
2011-03-18 04:01:06 +01:00
}
2016-08-10 06:08:39 +02:00
return null ;
2011-03-18 04:01:06 +01:00
}
/**
2014-12-01 12:22:48 +01:00
* Return a string of the form " parent - page " or " grandparent - parent - page " using page titles
2011-03-18 04:01:06 +01:00
*
* @ param int $level The maximum amount of levels to traverse .
2014-02-10 21:35:13 +01:00
* @ param string $separator Seperating string
2011-03-18 04:01:06 +01:00
* @ return string The resulting string
*/
2012-09-19 12:07:46 +02:00
public function NestedTitle ( $level = 2 , $separator = " - " ) {
2011-03-18 04:01:06 +01:00
$item = $this ;
2015-02-11 00:13:55 +01:00
$parts = [];
2011-03-18 04:01:06 +01:00
while ( $item && $level > 0 ) {
$parts [] = $item -> Title ;
2016-08-10 06:08:39 +02:00
$item = $item -> getParent ();
2011-03-18 04:01:06 +01:00
$level -- ;
}
return implode ( $separator , array_reverse ( $parts ));
}
/**
2014-12-01 12:22:48 +01:00
* This function should return true if the current user can execute this action . It can be overloaded to customise
* the security model for an application .
2016-01-06 00:42:07 +01:00
*
2011-10-07 09:29:03 +02:00
* Slightly altered from parent behaviour in { @ link DataObject -> can ()} :
* - Checks for existence of a method named " can< $perm >() " on the object
* - Calls decorators and only returns for FALSE " vetoes "
* - Falls back to { @ link Permission :: check ()}
* - Does NOT check for many - many relations named " Can< $perm > "
2011-03-18 04:01:06 +01:00
*
2011-10-07 09:29:03 +02:00
* @ uses DataObjectDecorator -> can ()
2011-03-18 04:01:06 +01:00
*
2014-12-01 12:22:48 +01:00
* @ param string $perm The permission to be checked , such as 'View'
* @ param Member $member The member whose permissions need checking . Defaults to the currently logged in user .
2016-03-29 00:39:12 +02:00
* @ param array $context Context argument for canCreate ()
2014-12-01 12:22:48 +01:00
* @ return bool True if the the member is allowed to do the given action
2011-03-18 04:01:06 +01:00
*/
2016-03-29 00:39:12 +02:00
public function can ( $perm , $member = null , $context = array ()) {
2016-06-23 01:51:20 +02:00
if ( ! $member || ! ( $member instanceof Member ) || is_numeric ( $member )) {
2011-03-18 04:01:06 +01:00
$member = Member :: currentUserID ();
}
if ( $member && Permission :: checkMember ( $member , " ADMIN " )) return true ;
2016-03-08 21:50:55 +01:00
2011-10-07 09:29:03 +02:00
if ( is_string ( $perm ) && method_exists ( $this , 'can' . ucfirst ( $perm ))) {
2011-03-18 04:01:06 +01:00
$method = 'can' . ucfirst ( $perm );
return $this -> $method ( $member );
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
$results = $this -> extend ( 'can' , $member );
if ( $results && is_array ( $results )) if ( ! min ( $results )) return false ;
2011-10-07 09:29:03 +02:00
return ( $member && Permission :: checkMember ( $member , $perm ));
2011-03-18 04:01:06 +01:00
}
/**
2014-12-01 12:22:48 +01:00
* This function should return true if the current user can add children to this page . It can be overloaded to
* customise the security model for an application .
2016-01-06 00:42:07 +01:00
*
2014-12-01 12:22:48 +01:00
* Denies permission if any of the following conditions is true :
* - alternateCanAddChildren () on a extension returns false
2011-03-18 04:01:06 +01:00
* - canEdit () is not granted
* - There are no classes defined in { @ link $allowed_children }
2016-01-06 00:42:07 +01:00
*
2011-04-15 11:37:15 +02:00
* @ uses SiteTreeExtension -> canAddChildren ()
2011-03-18 04:01:06 +01:00
* @ uses canEdit ()
* @ uses $allowed_children
*
2014-12-01 12:22:48 +01:00
* @ param Member | int $member
* @ return bool True if the current user can add children
2011-03-18 04:01:06 +01:00
*/
public function canAddChildren ( $member = null ) {
2015-08-24 05:16:09 +02:00
// Disable adding children to archived pages
2016-10-05 03:08:34 +02:00
if ( ! $this -> isOnDraft ()) {
2015-08-24 05:16:09 +02:00
return false ;
}
2016-06-23 01:51:20 +02:00
if ( ! $member || ! ( $member instanceof Member ) || is_numeric ( $member )) {
2011-03-18 04:01:06 +01:00
$member = Member :: currentUserID ();
}
2011-04-15 11:37:15 +02:00
// Standard mechanism for accepting permission changes from extensions
2011-03-18 04:01:06 +01:00
$extended = $this -> extendedCan ( 'canAddChildren' , $member );
2016-05-23 06:12:48 +02:00
if ( $extended !== null ) {
return $extended ;
}
// Default permissions
if ( $member && Permission :: checkMember ( $member , " ADMIN " )) {
return true ;
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
return $this -> canEdit ( $member ) && $this -> stat ( 'allowed_children' ) != 'none' ;
}
/**
2014-12-01 12:22:48 +01:00
* This function should return true if the current user can view this page . It can be overloaded to customise the
* security model for an application .
2016-01-06 00:42:07 +01:00
*
2014-12-01 12:22:48 +01:00
* Denies permission if any of the following conditions is true :
* - canView () on any extension returns false
2011-03-18 04:01:06 +01:00
* - " CanViewType " directive is set to " Inherit " and any parent page return false for canView ()
* - " CanViewType " directive is set to " LoggedInUsers " and no user is logged in
* - " CanViewType " directive is set to " OnlyTheseUsers " and user is not in the given groups
*
2011-04-15 11:37:15 +02:00
* @ uses DataExtension -> canView ()
2011-03-18 04:01:06 +01:00
* @ uses ViewerGroups ()
*
2014-12-01 12:22:48 +01:00
* @ param Member | int $member
* @ return bool True if the current user can view this page
2011-03-18 04:01:06 +01:00
*/
public function canView ( $member = null ) {
2016-06-23 01:51:20 +02:00
if ( ! $member || ! ( $member instanceof Member ) || is_numeric ( $member )) {
2011-03-18 04:01:06 +01:00
$member = Member :: currentUserID ();
}
2016-05-23 06:12:48 +02:00
// Standard mechanism for accepting permission changes from extensions
$extended = $this -> extendedCan ( 'canView' , $member );
if ( $extended !== null ) {
return $extended ;
}
2011-03-18 04:01:06 +01:00
// admin override
2016-05-23 06:12:48 +02:00
if ( $member && Permission :: checkMember ( $member , array ( " ADMIN " , " SITETREE_VIEW_ALL " ))) {
return true ;
}
2016-03-08 21:50:55 +01:00
2014-04-07 02:21:57 +02:00
// Orphaned pages (in the current stage) are unavailable, except for admins via the CMS
2016-05-23 06:12:48 +02:00
if ( $this -> isOrphaned ()) {
return false ;
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// check for empty spec
2016-05-23 06:12:48 +02:00
if ( ! $this -> CanViewType || $this -> CanViewType == 'Anyone' ) {
return true ;
}
2011-03-18 04:01:06 +01:00
// check for inherit
if ( $this -> CanViewType == 'Inherit' ) {
if ( $this -> ParentID ) return $this -> Parent () -> canView ( $member );
2015-03-11 06:54:08 +01:00
else return $this -> getSiteConfig () -> canViewPages ( $member );
2011-03-18 04:01:06 +01:00
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// check for any logged-in users
if ( $this -> CanViewType == 'LoggedInUsers' && $member ) {
return true ;
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// check for specific groups
2016-05-23 06:12:48 +02:00
if ( $member && is_numeric ( $member )) {
2016-06-23 01:51:20 +02:00
$member = DataObject :: get_by_id ( 'SilverStripe\\Security\\Member' , $member );
2016-05-23 06:12:48 +02:00
}
2011-03-18 04:01:06 +01:00
if (
2016-01-06 00:42:07 +01:00
$this -> CanViewType == 'OnlyTheseUsers'
&& $member
2011-03-18 04:01:06 +01:00
&& $member -> inGroups ( $this -> ViewerGroups ())
) return true ;
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
return false ;
}
2016-06-30 07:33:46 +02:00
/**
* Check if this page can be published
*
* @ param Member $member
* @ return bool
*/
public function canPublish ( $member = null ) {
if ( ! $member ) {
$member = Member :: currentUser ();
}
// Check extension
$extended = $this -> extendedCan ( 'canPublish' , $member );
if ( $extended !== null ) {
return $extended ;
}
if ( Permission :: checkMember ( $member , " ADMIN " )) {
return true ;
}
// Default to relying on edit permission
return $this -> canEdit ( $member );
}
2011-03-18 04:01:06 +01:00
/**
2014-12-01 12:22:48 +01:00
* This function should return true if the current user can delete this page . It can be overloaded to customise the
* security model for an application .
2016-01-06 00:42:07 +01:00
*
2014-12-01 12:22:48 +01:00
* Denies permission if any of the following conditions is true :
* - canDelete () returns false on any extension
* - canEdit () returns false
* - any descendant page returns false for canDelete ()
2016-01-06 00:42:07 +01:00
*
2011-03-18 04:01:06 +01:00
* @ uses canDelete ()
2011-04-15 11:37:15 +02:00
* @ uses SiteTreeExtension -> canDelete ()
2011-03-18 04:01:06 +01:00
* @ uses canEdit ()
*
* @ param Member $member
2014-12-01 12:22:48 +01:00
* @ return bool True if the current user can delete this page
2011-03-18 04:01:06 +01:00
*/
public function canDelete ( $member = null ) {
if ( $member instanceof Member ) $memberID = $member -> ID ;
else if ( is_numeric ( $member )) $memberID = $member ;
else $memberID = Member :: currentUserID ();
2016-03-08 21:50:55 +01:00
2016-05-23 06:12:48 +02:00
// Standard mechanism for accepting permission changes from extensions
$extended = $this -> extendedCan ( 'canDelete' , $memberID );
if ( $extended !== null ) {
return $extended ;
}
// Default permission check
2011-03-18 04:01:06 +01:00
if ( $memberID && Permission :: checkMember ( $memberID , array ( " ADMIN " , " SITETREE_EDIT_ALL " ))) {
return true ;
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// Regular canEdit logic is handled by can_edit_multiple
$results = self :: can_delete_multiple ( array ( $this -> ID ), $memberID );
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// If this page no longer exists in stage/live results won't contain the page.
// Fail-over to false
return isset ( $results [ $this -> ID ]) ? $results [ $this -> ID ] : false ;
}
/**
2016-01-06 00:42:07 +01:00
* This function should return true if the current user can create new pages of this class , regardless of class . It
2014-12-01 12:22:48 +01:00
* can be overloaded to customise the security model for an application .
2016-01-06 00:42:07 +01:00
*
* By default , permission to create at the root level is based on the SiteConfig configuration , and permission to
2014-12-01 12:22:48 +01:00
* create beneath a parent is based on the ability to edit that parent page .
2016-01-06 00:42:07 +01:00
*
2011-03-18 04:01:06 +01:00
* Use { @ link canAddChildren ()} to control behaviour of creating children under this page .
2016-01-06 00:42:07 +01:00
*
2011-03-18 04:01:06 +01:00
* @ uses $can_create
2011-04-15 11:37:15 +02:00
* @ uses DataExtension -> canCreate ()
2011-03-18 04:01:06 +01:00
*
* @ param Member $member
2015-03-11 06:54:08 +01:00
* @ param array $context Optional array which may contain array ( 'Parent' => $parentObj )
2014-12-01 12:22:48 +01:00
* If a parent page is known , it will be checked for validity .
* If omitted , it will be assumed this is to be created as a top level page .
* @ return bool True if the current user can create pages on this class .
2011-03-18 04:01:06 +01:00
*/
2015-06-09 01:32:28 +02:00
public function canCreate ( $member = null , $context = array ()) {
2016-06-23 01:51:20 +02:00
if ( ! $member || ! ( is_a ( $member , 'SilverStripe\\Security\\Member' )) || is_numeric ( $member )) {
2011-03-18 04:01:06 +01:00
$member = Member :: currentUserID ();
}
2015-03-11 06:54:08 +01:00
// Check parent (custom canCreate option for SiteTree)
// Block children not allowed for this parent type
$parent = isset ( $context [ 'Parent' ]) ? $context [ 'Parent' ] : null ;
2016-09-28 00:39:28 +02:00
if ( $parent && ! in_array ( static :: class , $parent -> allowedChildren ())) {
2015-06-09 01:32:28 +02:00
return false ;
}
2015-03-11 06:54:08 +01:00
2011-04-15 11:37:15 +02:00
// Standard mechanism for accepting permission changes from extensions
2015-06-09 01:32:28 +02:00
$extended = $this -> extendedCan ( __FUNCTION__ , $member , $context );
if ( $extended !== null ) {
return $extended ;
2015-03-11 06:54:08 +01:00
}
2011-03-18 04:01:06 +01:00
2016-05-23 06:12:48 +02:00
// Check permission
if ( $member && Permission :: checkMember ( $member , " ADMIN " )) {
return true ;
}
2015-03-11 06:54:08 +01:00
// Fall over to inherited permissions
if ( $parent ) {
return $parent -> canAddChildren ( $member );
} else {
// This doesn't necessarily mean we are creating a root page, but that
// we don't know if there is a parent, so default to this permission
return SiteConfig :: current_site_config () -> canCreateTopLevel ( $member );
}
}
2011-03-18 04:01:06 +01:00
/**
2014-12-01 12:22:48 +01:00
* This function should return true if the current user can edit this page . It can be overloaded to customise the
* security model for an application .
2016-01-06 00:42:07 +01:00
*
2014-12-01 12:22:48 +01:00
* Denies permission if any of the following conditions is true :
* - canEdit () on any extension returns false
2011-03-18 04:01:06 +01:00
* - canView () return false
* - " CanEditType " directive is set to " Inherit " and any parent page return false for canEdit ()
2014-12-01 12:22:48 +01:00
* - " CanEditType " directive is set to " LoggedInUsers " and no user is logged in or doesn ' t have the
* CMS_Access_CMSMAIN permission code
2011-03-18 04:01:06 +01:00
* - " CanEditType " directive is set to " OnlyTheseUsers " and user is not in the given groups
2016-01-06 00:42:07 +01:00
*
2011-03-18 04:01:06 +01:00
* @ uses canView ()
* @ uses EditorGroups ()
2011-04-15 11:37:15 +02:00
* @ uses DataExtension -> canEdit ()
2011-03-18 04:01:06 +01:00
*
2014-12-01 12:22:48 +01:00
* @ param Member $member Set to false if you want to explicitly test permissions without a valid user ( useful for
* unit tests )
* @ return bool True if the current user can edit this page
2011-03-18 04:01:06 +01:00
*/
public function canEdit ( $member = null ) {
if ( $member instanceof Member ) $memberID = $member -> ID ;
else if ( is_numeric ( $member )) $memberID = $member ;
else $memberID = Member :: currentUserID ();
2016-03-08 21:50:55 +01:00
2011-04-15 11:37:15 +02:00
// Standard mechanism for accepting permission changes from extensions
2011-03-18 04:01:06 +01:00
$extended = $this -> extendedCan ( 'canEdit' , $memberID );
2016-05-23 06:12:48 +02:00
if ( $extended !== null ) {
return $extended ;
}
// Default permissions
if ( $memberID && Permission :: checkMember ( $memberID , array ( " ADMIN " , " SITETREE_EDIT_ALL " ))) {
return true ;
}
2011-03-18 04:01:06 +01:00
if ( $this -> ID ) {
// Regular canEdit logic is handled by can_edit_multiple
$results = self :: can_edit_multiple ( array ( $this -> ID ), $memberID );
// If this page no longer exists in stage/live results won't contain the page.
// Fail-over to false
return isset ( $results [ $this -> ID ]) ? $results [ $this -> ID ] : false ;
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// Default for unsaved pages
} else {
2015-03-11 06:54:08 +01:00
return $this -> getSiteConfig () -> canEditPages ( $member );
2011-03-18 04:01:06 +01:00
}
}
/**
2014-12-01 12:22:48 +01:00
* Stub method to get the site config , unless the current class can provide an alternate .
*
* @ return SiteConfig
2011-03-18 04:01:06 +01:00
*/
2012-09-19 12:07:46 +02:00
public function getSiteConfig () {
2015-02-11 00:13:55 +01:00
$configs = $this -> invokeWithExtensions ( 'alternateSiteConfig' );
foreach ( array_filter ( $configs ) as $config ) {
return $config ;
2011-03-18 04:01:06 +01:00
}
2016-03-08 21:50:55 +01:00
2012-09-07 17:06:29 +02:00
return SiteConfig :: current_site_config ();
2011-03-18 04:01:06 +01:00
}
/**
2014-12-01 12:22:48 +01:00
* Pre - populate the cache of canEdit , canView , canDelete , canPublish permissions . This method will use the static
* can_ ( perm ) _multiple method for efficiency .
*
* @ param string $permission The permission : edit , view , publish , approve , etc .
* @ param array $ids An array of page IDs
* @ param callable | string $batchCallback The function / static method to call to calculate permissions . Defaults
* to 'SiteTree::can_(permission)_multiple'
2011-03-18 04:01:06 +01:00
*/
2012-09-19 12:07:46 +02:00
static public function prepopulate_permission_cache ( $permission = 'CanEditType' , $ids , $batchCallback = null ) {
2016-08-10 06:08:39 +02:00
if ( ! $batchCallback ) {
2016-09-28 00:39:28 +02:00
$batchCallback = self :: class . " ::can_ { $permission } _multiple " ;
2016-08-10 06:08:39 +02:00
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
if ( is_callable ( $batchCallback )) {
2011-10-07 10:36:56 +02:00
call_user_func ( $batchCallback , $ids , Member :: currentUserID (), false );
2011-03-18 04:01:06 +01:00
} else {
2012-01-23 20:28:55 +01:00
user_error ( " SiteTree::prepopulate_permission_cache can't calculate ' $permission ' "
2011-03-18 04:01:06 +01:00
. " with callback ' $batchCallback ' " , E_USER_WARNING );
}
}
2014-12-01 12:22:48 +01:00
2011-09-09 17:35:11 +02:00
/**
2014-12-01 12:22:48 +01:00
* This method is NOT a full replacement for the individual can * () methods , e . g . { @ link canEdit ()} . Rather than
* checking ( potentially slow ) PHP logic , it relies on the database group associations , e . g . the " CanEditType " field
* plus the " SiteTree_EditorGroups " many - many table . By batch checking multiple records , we can combine the queries
* efficiently .
*
* Caches based on $typeField data . To invalidate the cache , use { @ link SiteTree :: reset ()} or set the $useCached
* property to FALSE .
*
* @ param array $ids Of { @ link SiteTree } IDs
* @ param int $memberID Member ID
* @ param string $typeField A property on the data record , e . g . " CanEditType " .
* @ param string $groupJoinTable A many - many table name on this record , e . g . " SiteTree_EditorGroups "
* @ param string $siteConfigMethod Method to call on { @ link SiteConfig } for toplevel items , e . g . " canEdit "
* @ param string $globalPermission If the member doesn 't have this permission code, don' t bother iterating deeper
* @ param bool $useCached
* @ return array An map of { @ link SiteTree } ID keys to boolean values
*/
public static function batch_permission_check ( $ids , $memberID , $typeField , $groupJoinTable , $siteConfigMethod ,
$globalPermission = null , $useCached = true ) {
2013-12-13 10:03:01 +01:00
if ( $globalPermission === NULL ) $globalPermission = array ( 'CMS_ACCESS_LeftAndMain' , 'CMS_ACCESS_CMSMain' );
2011-03-18 04:01:06 +01:00
// Sanitise the IDs
$ids = array_filter ( $ids , 'is_numeric' );
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// This is the name used on the permission cache
// converts something like 'CanEditType' to 'edit'.
2011-10-07 10:36:56 +02:00
$cacheKey = strtolower ( substr ( $typeField , 3 , - 4 )) . " - $memberID " ;
2011-03-18 04:01:06 +01:00
// Default result: nothing editable
$result = array_fill_keys ( $ids , false );
if ( $ids ) {
// Look in the cache for values
if ( $useCached && isset ( self :: $cache_permissions [ $cacheKey ])) {
$cachedValues = array_intersect_key ( self :: $cache_permissions [ $cacheKey ], $result );
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// If we can't find everything in the cache, then look up the remainder separately
$uncachedValues = array_diff_key ( $result , self :: $cache_permissions [ $cacheKey ]);
if ( $uncachedValues ) {
$cachedValues = self :: batch_permission_check ( array_keys ( $uncachedValues ), $memberID , $typeField , $groupJoinTable , $siteConfigMethod , $globalPermission , false ) + $cachedValues ;
}
return $cachedValues ;
}
2016-03-08 21:50:55 +01:00
2011-09-09 17:35:11 +02:00
// If a member doesn't have a certain permission then they can't edit anything
2011-03-18 04:01:06 +01:00
if ( ! $memberID || ( $globalPermission && ! Permission :: checkMember ( $memberID , $globalPermission ))) {
return $result ;
}
2013-06-21 00:45:33 +02:00
// Placeholder for parameterised ID list
$idPlaceholders = DB :: placeholders ( $ids );
2011-03-18 04:01:06 +01:00
2014-12-01 12:22:48 +01:00
// If page can't be viewed, don't grant edit permissions to do - implement can_view_multiple(), so this can
// be enabled
2011-03-18 04:01:06 +01:00
//$ids = array_keys(array_filter(self::can_view_multiple($ids, $memberID)));
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// Get the groups that the given member belongs to
2016-08-10 06:08:39 +02:00
/** @var Member $member */
$member = DataObject :: get_by_id ( 'SilverStripe\\Security\\Member' , $memberID );
$groupIDs = $member -> Groups () -> column ( " ID " );
2011-03-18 04:01:06 +01:00
$SQL_groupList = implode ( " , " , $groupIDs );
2016-08-10 06:08:39 +02:00
if ( ! $SQL_groupList ) {
$SQL_groupList = '0' ;
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
$combinedStageResult = array ();
2016-04-01 05:17:37 +02:00
foreach ( array ( Versioned :: DRAFT , Versioned :: LIVE ) as $stage ) {
2011-03-18 04:01:06 +01:00
// Start by filling the array with the pages that actually exist
2016-08-10 06:08:39 +02:00
/** @skipUpgrade */
$table = ( $stage == 'Stage' ) ? " SiteTree " : " SiteTree_ $stage " ;
2016-03-08 21:50:55 +01:00
2013-06-21 00:45:33 +02:00
if ( $ids ) {
$idQuery = " SELECT \" ID \" FROM \" $table\ " WHERE \ " ID \" IN ( $idPlaceholders ) " ;
$stageIds = DB :: prepared_query ( $idQuery , $ids ) -> column ();
} else {
$stageIds = array ();
}
$result = array_fill_keys ( $stageIds , false );
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// Get the uninherited permissions
2016-07-22 01:32:32 +02:00
$uninheritedPermissions = Versioned :: get_by_stage ( " SilverStripe \\ CMS \\ Model \\ SiteTree " , $stage )
2013-06-21 00:45:33 +02:00
-> where ( array (
" ( \" $typeField\ " = 'LoggedInUsers' OR
2011-10-29 06:40:31 +02:00
( \ " $typeField\ " = 'OnlyTheseUsers' AND \ " $groupJoinTable\ " . \ " SiteTreeID \" IS NOT NULL))
2013-06-21 00:45:33 +02:00
AND \ " SiteTree \" . \" ID \" IN ( $idPlaceholders ) "
=> $ids
))
2011-10-29 06:40:31 +02:00
-> leftJoin ( $groupJoinTable , " \" $groupJoinTable\ " . \ " SiteTreeID \" = \" SiteTree \" . \" ID \" AND \" $groupJoinTable\ " . \ " GroupID \" IN ( $SQL_groupList ) " );
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
if ( $uninheritedPermissions ) {
// Set all the relevant items in $result to true
$result = array_fill_keys ( $uninheritedPermissions -> column ( 'ID' ), true ) + $result ;
}
// Get permissions that are inherited
2013-06-21 00:45:33 +02:00
$potentiallyInherited = Versioned :: get_by_stage (
2016-07-22 01:32:32 +02:00
" SilverStripe \\ CMS \\ Model \\ SiteTree " ,
2016-01-06 00:42:07 +01:00
$stage ,
2013-06-21 00:45:33 +02:00
array ( " \" $typeField\ " = 'Inherit' AND \ " SiteTree \" . \" ID \" IN ( $idPlaceholders ) " => $ids )
);
2011-03-18 04:01:06 +01:00
if ( $potentiallyInherited ) {
2014-12-01 12:22:48 +01:00
// Group $potentiallyInherited by ParentID; we'll look at the permission of all those parents and
// then see which ones the user has permission on
2011-03-18 04:01:06 +01:00
$groupedByParent = array ();
foreach ( $potentiallyInherited as $item ) {
2015-02-11 00:13:55 +01:00
/** @var SiteTree $item */
2011-03-18 04:01:06 +01:00
if ( $item -> ParentID ) {
if ( ! isset ( $groupedByParent [ $item -> ParentID ])) $groupedByParent [ $item -> ParentID ] = array ();
$groupedByParent [ $item -> ParentID ][] = $item -> ID ;
} else {
2014-12-01 12:22:48 +01:00
// Might return different site config based on record context, e.g. when subsites module
// is used
2011-09-09 17:36:34 +02:00
$siteConfig = $item -> getSiteConfig ();
$result [ $item -> ID ] = $siteConfig -> { $siteConfigMethod }( $memberID );
2011-03-18 04:01:06 +01:00
}
}
if ( $groupedByParent ) {
$actuallyInherited = self :: batch_permission_check ( array_keys ( $groupedByParent ), $memberID , $typeField , $groupJoinTable , $siteConfigMethod );
if ( $actuallyInherited ) {
$parentIDs = array_keys ( array_filter ( $actuallyInherited ));
foreach ( $parentIDs as $parentID ) {
// Set all the relevant items in $result to true
$result = array_fill_keys ( $groupedByParent [ $parentID ], true ) + $result ;
}
}
}
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
$combinedStageResult = $combinedStageResult + $result ;
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
}
}
if ( isset ( $combinedStageResult )) {
2011-10-07 10:36:56 +02:00
// Cache the results
if ( empty ( self :: $cache_permissions [ $cacheKey ])) self :: $cache_permissions [ $cacheKey ] = array ();
self :: $cache_permissions [ $cacheKey ] = $combinedStageResult + self :: $cache_permissions [ $cacheKey ];
2015-03-11 06:54:08 +01:00
return $combinedStageResult ;
2011-03-18 04:01:06 +01:00
} else {
return array ();
}
}
2014-12-01 12:22:48 +01:00
2011-03-18 04:01:06 +01:00
/**
* Get the 'can edit' information for a number of SiteTree pages .
2014-12-01 12:22:48 +01:00
*
* @ param array $ids An array of IDs of the SiteTree pages to look up
* @ param int $memberID ID of member
* @ param bool $useCached Return values from the permission cache if they exist
* @ return array A map where the IDs are keys and the values are booleans stating whether the given page can be
* edited
2011-03-18 04:01:06 +01:00
*/
2012-09-19 12:07:46 +02:00
static public function can_edit_multiple ( $ids , $memberID , $useCached = true ) {
2015-03-11 06:54:08 +01:00
return self :: batch_permission_check ( $ids , $memberID , 'CanEditType' , 'SiteTree_EditorGroups' , 'canEditPages' , null , $useCached );
2011-03-18 04:01:06 +01:00
}
/**
* Get the 'can edit' information for a number of SiteTree pages .
2014-12-01 12:22:48 +01:00
*
* @ param array $ids An array of IDs of the SiteTree pages to look up
* @ param int $memberID ID of member
* @ param bool $useCached Return values from the permission cache if they exist
2014-02-10 21:35:13 +01:00
* @ return array
2011-03-18 04:01:06 +01:00
*/
2012-09-19 12:07:46 +02:00
static public function can_delete_multiple ( $ids , $memberID , $useCached = true ) {
2011-03-18 04:01:06 +01:00
$deletable = array ();
2011-10-07 10:36:56 +02:00
$result = array_fill_keys ( $ids , false );
$cacheKey = " delete- $memberID " ;
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// Look in the cache for values
2011-10-07 10:36:56 +02:00
if ( $useCached && isset ( self :: $cache_permissions [ $cacheKey ])) {
$cachedValues = array_intersect_key ( self :: $cache_permissions [ $cacheKey ], $result );
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// If we can't find everything in the cache, then look up the remainder separately
2011-10-07 10:36:56 +02:00
$uncachedValues = array_diff_key ( $result , self :: $cache_permissions [ $cacheKey ]);
2011-03-18 04:01:06 +01:00
if ( $uncachedValues ) {
$cachedValues = self :: can_delete_multiple ( array_keys ( $uncachedValues ), $memberID , false )
+ $cachedValues ;
}
return $cachedValues ;
}
// You can only delete pages that you can edit
$editableIDs = array_keys ( array_filter ( self :: can_edit_multiple ( $ids , $memberID )));
if ( $editableIDs ) {
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// You can only delete pages whose children you can delete
2013-06-21 00:45:33 +02:00
$editablePlaceholders = DB :: placeholders ( $editableIDs );
$childRecords = SiteTree :: get () -> where ( array (
" \" SiteTree \" . \" ParentID \" IN ( $editablePlaceholders ) " => $editableIDs
));
2011-03-18 04:01:06 +01:00
if ( $childRecords ) {
$children = $childRecords -> map ( " ID " , " ParentID " );
// Find out the children that can be deleted
2011-10-29 06:41:00 +02:00
$deletableChildren = self :: can_delete_multiple ( $children -> keys (), $memberID );
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// Get a list of all the parents that have no undeletable children
$deletableParents = array_fill_keys ( $editableIDs , true );
foreach ( $deletableChildren as $id => $canDelete ) {
if ( ! $canDelete ) unset ( $deletableParents [ $children [ $id ]]);
}
// Use that to filter the list of deletable parents that have children
$deletableParents = array_keys ( $deletableParents );
// Also get the $ids that don't have children
2011-10-29 06:41:00 +02:00
$parents = array_unique ( $children -> values ());
2011-03-18 04:01:06 +01:00
$deletableLeafNodes = array_diff ( $editableIDs , $parents );
// Combine the two
$deletable = array_merge ( $deletableParents , $deletableLeafNodes );
} else {
$deletable = $editableIDs ;
}
}
2016-03-08 21:50:55 +01:00
2014-12-01 12:22:48 +01:00
// Convert the array of deletable IDs into a map of the original IDs with true/false as the value
2011-03-18 04:01:06 +01:00
return array_fill_keys ( $deletable , true ) + array_fill_keys ( $ids , false );
}
/**
* Collate selected descendants of this page .
*
2014-12-01 12:22:48 +01:00
* { @ link $condition } will be evaluated on each descendant , and if it is succeeds , that item will be added to the
* $collator array .
2011-03-18 04:01:06 +01:00
*
2014-12-01 12:22:48 +01:00
* @ param string $condition The PHP condition to be evaluated . The page will be called $item
* @ param array $collator An array , passed by reference , to collect all of the matching descendants .
* @ return bool
2011-03-18 04:01:06 +01:00
*/
public function collateDescendants ( $condition , & $collator ) {
2016-08-10 06:08:39 +02:00
$children = $this -> Children ();
if ( $children ) {
2011-03-18 04:01:06 +01:00
foreach ( $children as $item ) {
2015-02-11 00:13:55 +01:00
if ( eval ( " return $condition ; " )) {
$collator [] = $item ;
}
/** @var SiteTree $item */
2011-03-18 04:01:06 +01:00
$item -> collateDescendants ( $condition , $collator );
}
return true ;
}
2016-08-10 06:08:39 +02:00
return false ;
2011-03-18 04:01:06 +01:00
}
/**
* Return the title , description , keywords and language metatags .
2014-12-01 12:22:48 +01:00
*
2011-03-18 04:01:06 +01:00
* @ todo Move < title > tag in separate getter for easier customization and more obvious usage
2014-12-01 12:22:48 +01:00
*
* @ param bool $includeTitle Show default < title >- tag , set to false for custom templating
2011-03-18 04:01:06 +01:00
* @ return string The XHTML metatags
*/
public function MetaTags ( $includeTitle = true ) {
2015-02-11 00:13:55 +01:00
$tags = array ();
if ( $includeTitle && strtolower ( $includeTitle ) != 'false' ) {
$tags [] = FormField :: create_tag ( 'title' , array (), $this -> obj ( 'Title' ) -> forTemplate ());
2011-03-18 04:01:06 +01:00
}
2016-09-28 00:39:28 +02:00
$generator = trim ( Config :: inst () -> get ( self :: class , 'meta_generator' ));
2013-06-17 22:29:02 +02:00
if ( ! empty ( $generator )) {
2015-02-11 00:13:55 +01:00
$tags [] = FormField :: create_tag ( 'meta' , array (
'name' => 'generator' ,
'content' => $generator ,
));
2013-06-17 22:29:02 +02:00
}
2011-03-18 04:01:06 +01:00
2016-08-23 04:36:06 +02:00
$charset = Config :: inst () -> get ( 'SilverStripe\\Control\\ContentNegotiator' , 'encoding' );
2015-02-11 00:13:55 +01:00
$tags [] = FormField :: create_tag ( 'meta' , array (
'http-equiv' => 'Content-Type' ,
'content' => 'text/html; charset=' . $charset ,
));
2011-03-18 04:01:06 +01:00
if ( $this -> MetaDescription ) {
2015-02-11 00:13:55 +01:00
$tags [] = FormField :: create_tag ( 'meta' , array (
'name' => 'description' ,
'content' => $this -> MetaDescription ,
));
2016-01-06 00:42:07 +01:00
}
2016-03-08 21:50:55 +01:00
2014-05-30 04:44:35 +02:00
if ( Permission :: check ( 'CMS_ACCESS_CMSMain' )
&& ! $this instanceof ErrorPage
&& $this -> ID > 0
) {
2015-02-11 00:13:55 +01:00
$tags [] = FormField :: create_tag ( 'meta' , array (
'name' => 'x-page-id' ,
'content' => $this -> obj ( 'ID' ) -> forTemplate (),
));
$tags [] = FormField :: create_tag ( 'meta' , array (
'name' => 'x-cms-edit-link' ,
'content' => $this -> obj ( 'CMSEditLink' ) -> forTemplate (),
));
}
$tags = implode ( " \n " , $tags );
if ( $this -> ExtraMeta ) {
$tags .= $this -> obj ( 'ExtraMeta' ) -> forTemplate ();
2011-07-21 20:01:00 +02:00
}
2011-03-18 04:01:06 +01:00
$this -> extend ( 'MetaTags' , $tags );
return $tags ;
}
/**
2014-12-01 12:22:48 +01:00
* Returns the object that contains the content that a user would associate with this page .
2011-03-18 04:01:06 +01:00
*
2014-12-01 12:22:48 +01:00
* Ordinarily , this is just the page itself , but for example on RedirectorPages or VirtualPages ContentSource () will
* return the page that is linked to .
2011-03-18 04:01:06 +01:00
*
2014-12-01 12:22:48 +01:00
* @ return $this
2011-03-18 04:01:06 +01:00
*/
public function ContentSource () {
return $this ;
}
/**
* Add default records to database .
*
2014-12-01 12:22:48 +01:00
* This function is called whenever the database is built , after the database tables have all been created . Overload
* this to add default records when the database is built , but make sure you call parent :: requireDefaultRecords () .
2011-03-18 04:01:06 +01:00
*/
2012-09-19 12:07:46 +02:00
public function requireDefaultRecords () {
2011-03-18 04:01:06 +01:00
parent :: requireDefaultRecords ();
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// default pages
2016-09-28 00:39:28 +02:00
if ( static :: class == self :: class && $this -> config () -> create_default_pages ) {
2016-08-10 06:08:39 +02:00
if ( ! SiteTree :: get_by_link ( RootURLController :: config () -> default_homepage_link )) {
2011-03-18 04:01:06 +01:00
$homepage = new Page ();
$homepage -> Title = _t ( 'SiteTree.DEFAULTHOMETITLE' , 'Home' );
2015-06-18 17:47:10 +02:00
$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>' );
2016-08-10 06:08:39 +02:00
$homepage -> URLSegment = RootURLController :: config () -> default_homepage_link ;
2011-03-18 04:01:06 +01:00
$homepage -> Sort = 1 ;
$homepage -> write ();
2016-04-01 05:17:37 +02:00
$homepage -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
2011-03-18 04:01:06 +01:00
$homepage -> flushCache ();
DB :: alteration_message ( 'Home page created' , 'created' );
}
if ( DB :: query ( " SELECT COUNT(*) FROM \" SiteTree \" " ) -> value () == 1 ) {
$aboutus = new Page ();
$aboutus -> Title = _t ( 'SiteTree.DEFAULTABOUTTITLE' , 'About Us' );
2016-08-10 06:08:39 +02:00
$aboutus -> Content = _t (
'SiteTree.DEFAULTABOUTCONTENT' ,
'<p>You can fill this page out with your own content, or delete it and create your own pages.</p>'
);
2011-03-18 04:01:06 +01:00
$aboutus -> Sort = 2 ;
$aboutus -> write ();
2016-04-01 05:17:37 +02:00
$aboutus -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
2011-03-18 04:01:06 +01:00
$aboutus -> flushCache ();
DB :: alteration_message ( 'About Us page created' , 'created' );
$contactus = new Page ();
$contactus -> Title = _t ( 'SiteTree.DEFAULTCONTACTTITLE' , 'Contact Us' );
2016-08-10 06:08:39 +02:00
$contactus -> Content = _t (
'SiteTree.DEFAULTCONTACTCONTENT' ,
'<p>You can fill this page out with your own content, or delete it and create your own pages.</p>'
);
2011-03-18 04:01:06 +01:00
$contactus -> Sort = 3 ;
$contactus -> write ();
2016-04-01 05:17:37 +02:00
$contactus -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
2011-03-18 04:01:06 +01:00
$contactus -> flushCache ();
DB :: alteration_message ( 'Contact Us page created' , 'created' );
}
}
2016-08-11 03:18:02 +02:00
}
2011-03-18 04:01:06 +01:00
protected function onBeforeWrite () {
parent :: onBeforeWrite ();
// If Sort hasn't been set, make this page come after it's siblings
if ( ! $this -> Sort ) {
$parentID = ( $this -> ParentID ) ? $this -> ParentID : 0 ;
2013-06-21 00:45:33 +02:00
$this -> Sort = DB :: prepared_query (
2016-01-06 00:42:07 +01:00
" SELECT MAX( \" Sort \" ) + 1 FROM \" SiteTree \" WHERE \" ParentID \" = ? " ,
2013-06-21 00:45:33 +02:00
array ( $parentID )
) -> value ();
2011-03-18 04:01:06 +01:00
}
// If there is no URLSegment set, generate one from Title
2015-02-20 23:33:59 +01:00
$defaultSegment = $this -> generateURLSegment ( _t (
'CMSMain.NEWPAGE' ,
array ( 'pagetype' => $this -> i18n_singular_name ())
));
if (( ! $this -> URLSegment || $this -> URLSegment == $defaultSegment ) && $this -> Title ) {
2011-03-18 04:01:06 +01:00
$this -> URLSegment = $this -> generateURLSegment ( $this -> Title );
2012-05-08 22:11:17 +02:00
} else if ( $this -> isChanged ( 'URLSegment' , 2 )) {
// Do a strict check on change level, to avoid double encoding caused by
// bogus changes through forceChange()
2012-04-04 16:59:22 +02:00
$filter = URLSegmentFilter :: create ();
2011-11-14 12:28:25 +01:00
$this -> URLSegment = $filter -> filter ( $this -> URLSegment );
2011-03-18 04:01:06 +01:00
// If after sanitising there is no URLSegment, give it a reasonable default
2011-11-14 12:28:25 +01:00
if ( ! $this -> URLSegment ) $this -> URLSegment = " page- $this->ID " ;
2011-03-18 04:01:06 +01:00
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// Ensure that this object has a non-conflicting URLSegment value.
$count = 2 ;
while ( ! $this -> validURLSegment ()) {
$this -> URLSegment = preg_replace ( '/-[0-9]+$/' , null , $this -> URLSegment ) . '-' . $count ;
$count ++ ;
}
$this -> syncLinkTracking ();
// Check to see if we've only altered fields that shouldn't affect versioning
2011-09-23 10:36:25 +02:00
$fieldsIgnoredByVersioning = array ( 'HasBrokenLink' , 'Status' , 'HasBrokenFile' , 'ToDo' , 'VersionID' , 'SaveCount' );
2011-03-18 04:01:06 +01:00
$changedFields = array_keys ( $this -> getChangedFields ( true , 2 ));
2016-03-07 21:37:29 +01:00
// This more rigorous check is inline with the test that write() does to decide whether or not to write to the
2014-12-01 12:22:48 +01:00
// DB. We use that to avoid cluttering the system with a migrateVersion() call that doesn't get used
2011-03-18 04:01:06 +01:00
$oneChangedFields = array_keys ( $this -> getChangedFields ( true , 1 ));
if ( $oneChangedFields && ! array_diff ( $changedFields , $fieldsIgnoredByVersioning )) {
// This will have the affect of preserving the versioning
$this -> migrateVersion ( $this -> Version );
}
}
2016-01-26 06:38:42 +01:00
/**
* Trigger synchronisation of link tracking
*
* { @ see SiteTreeLinkTracking :: augmentSyncLinkTracking }
*/
2012-09-19 12:07:46 +02:00
public function syncLinkTracking () {
2011-03-18 04:01:06 +01:00
$this -> extend ( 'augmentSyncLinkTracking' );
}
2016-03-08 21:50:55 +01:00
2012-09-19 12:07:46 +02:00
public function onBeforeDelete () {
2011-03-18 04:01:06 +01:00
parent :: onBeforeDelete ();
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// If deleting this page, delete all its children.
2014-06-10 06:26:08 +02:00
if ( SiteTree :: config () -> enforce_strict_hierarchy && $children = $this -> AllChildren ()) {
2011-03-18 04:01:06 +01:00
foreach ( $children as $child ) {
2016-08-10 06:08:39 +02:00
/** @var SiteTree $child */
2011-03-18 04:01:06 +01:00
$child -> delete ();
}
}
}
2016-03-08 21:50:55 +01:00
2012-09-19 12:07:46 +02:00
public function onAfterDelete () {
2011-03-18 04:01:06 +01:00
// Need to flush cache to avoid outdated versionnumber references
$this -> flushCache ();
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// Need to mark pages depending to this one as broken
$dependentPages = $this -> DependentPages ();
if ( $dependentPages ) foreach ( $dependentPages as $page ) {
// $page->write() calls syncLinkTracking, which does all the hard work for us.
$page -> write ();
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
parent :: onAfterDelete ();
}
2012-06-12 15:55:05 +02:00
2012-09-19 12:07:46 +02:00
public function flushCache ( $persistent = true ) {
2012-06-12 15:55:05 +02:00
parent :: flushCache ( $persistent );
$this -> _cache_statusFlags = null ;
}
2016-03-08 21:50:55 +01:00
2015-06-17 05:56:44 +02:00
public function validate () {
2011-10-06 16:47:59 +02:00
$result = parent :: validate ();
2016-01-06 00:42:07 +01:00
// Allowed children validation
2011-10-06 16:47:59 +02:00
$parent = $this -> getParent ();
if ( $parent && $parent -> exists ()) {
2016-01-06 00:42:07 +01:00
// No need to check for subclasses or instanceof, as allowedChildren() already
2011-10-06 16:47:59 +02:00
// deconstructs any inheritance trees already.
$allowed = $parent -> allowedChildren ();
2016-08-10 06:08:39 +02:00
$subject = ( $this instanceof VirtualPage && $this -> CopyContentFromID )
? $this -> CopyContentFrom ()
: $this ;
2012-09-02 18:06:25 +02:00
if ( ! in_array ( $subject -> ClassName , $allowed )) {
2011-10-06 16:47:59 +02:00
$result -> error (
2012-05-01 21:43:43 +02:00
_t (
2016-01-06 00:42:07 +01:00
'SiteTree.PageTypeNotAllowed' ,
'Page type "{type}" not allowed as child of this parent page' ,
2012-05-01 21:43:43 +02:00
array ( 'type' => $subject -> i18n_singular_name ())
2011-10-06 16:47:59 +02:00
),
'ALLOWED_CHILDREN'
);
}
}
2012-01-14 11:20:54 +01:00
// "Can be root" validation
if ( ! $this -> stat ( 'can_be_root' ) && ! $this -> ParentID ) {
$result -> error (
2012-05-01 21:43:43 +02:00
_t (
2016-01-06 00:42:07 +01:00
'SiteTree.PageTypNotAllowedOnRoot' ,
'Page type "{type}" is not allowed on the root level' ,
2012-05-01 21:43:43 +02:00
array ( 'type' => $this -> i18n_singular_name ())
2012-01-14 11:20:54 +01:00
),
'CAN_BE_ROOT'
);
}
2016-03-08 21:50:55 +01:00
2011-10-06 16:47:59 +02:00
return $result ;
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
2014-12-01 12:22:48 +01:00
* Returns true if this object has a URLSegment value that does not conflict with any other objects . This method
2011-03-18 04:01:06 +01:00
* checks for :
2014-12-01 12:22:48 +01:00
* - A page with the same URLSegment that has a conflict
* - Conflicts with actions on the parent page
* - A conflict caused by a root page having the same URLSegment as a class name
2011-03-18 04:01:06 +01:00
*
* @ return bool
*/
public function validURLSegment () {
2013-03-18 11:47:15 +01:00
if ( self :: config () -> nested_urls && $parent = $this -> Parent ()) {
2011-03-18 04:01:06 +01:00
if ( $controller = ModelAsController :: controller_for ( $parent )) {
if ( $controller instanceof Controller && $controller -> hasAction ( $this -> URLSegment )) return false ;
}
}
2016-03-08 21:50:55 +01:00
2013-03-18 11:47:15 +01:00
if ( ! self :: config () -> nested_urls || ! $this -> ParentID ) {
2016-08-23 04:36:06 +02:00
if ( class_exists ( $this -> URLSegment ) && is_subclass_of ( $this -> URLSegment , 'SilverStripe\\Control\\RequestHandler' )) return false ;
2011-03-18 04:01:06 +01:00
}
2016-03-08 21:50:55 +01:00
2013-06-21 00:45:33 +02:00
// Filters by url, id, and parent
$filter = array ( '"SiteTree"."URLSegment"' => $this -> URLSegment );
if ( $this -> ID ) {
$filter [ '"SiteTree"."ID" <> ?' ] = $this -> ID ;
}
2013-03-18 11:47:15 +01:00
if ( self :: config () -> nested_urls ) {
2013-06-21 00:45:33 +02:00
$filter [ '"SiteTree"."ParentID"' ] = $this -> ParentID ? $this -> ParentID : 0 ;
2011-03-18 04:01:06 +01:00
}
2016-03-08 21:50:55 +01:00
2013-06-12 12:32:42 +02:00
$votes = array_filter (
2016-01-06 00:42:07 +01:00
( array ) $this -> extend ( 'augmentValidURLSegment' ),
2013-06-12 12:32:42 +02:00
function ( $v ) { return ! is_null ( $v );}
2011-03-18 04:01:06 +01:00
);
2011-08-29 14:47:29 +02:00
if ( $votes ) {
2012-07-24 06:02:11 +02:00
return min ( $votes );
2011-03-18 04:01:06 +01:00
}
2013-06-12 12:32:42 +02:00
2013-06-21 00:45:33 +02:00
// Check existence
2016-10-25 02:22:31 +02:00
$existingPage = DataObject :: get_one ( self :: class , $filter );
2013-06-21 00:45:33 +02:00
if ( $existingPage ) return false ;
2011-03-18 04:01:06 +01:00
2013-06-12 12:32:42 +02:00
return ! ( $existingPage );
2011-03-18 04:01:06 +01:00
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
* Generate a URL segment based on the title provided .
2014-12-01 12:22:48 +01:00
*
2011-03-18 04:01:06 +01:00
* If { @ link Extension } s wish to alter URL segment generation , they can do so by defining
2014-12-01 12:22:48 +01:00
* updateURLSegment ( & $url , $title ) . $url will be passed by reference and should be modified . $title will contain
* the title that was originally used as the source of this generated URL . This lets extensions either start from
* scratch , or incrementally modify the generated URL .
2016-01-06 00:42:07 +01:00
*
2014-12-01 12:22:48 +01:00
* @ param string $title Page title
2011-03-18 04:01:06 +01:00
* @ return string Generated url segment
*/
2012-09-19 12:07:46 +02:00
public function generateURLSegment ( $title ){
2012-04-04 16:59:22 +02:00
$filter = URLSegmentFilter :: create ();
2011-11-14 12:28:25 +01:00
$t = $filter -> filter ( $title );
2016-03-08 21:50:55 +01:00
2011-11-14 12:28:25 +01:00
// Fallback to generic page name if path is empty (= no valid, convertable characters)
if ( ! $t || $t == '-' || $t == '-1' ) $t = " page- $this->ID " ;
2016-03-08 21:50:55 +01:00
2011-04-15 11:37:15 +02:00
// Hook for extensions
2011-03-18 04:01:06 +01:00
$this -> extend ( 'updateURLSegment' , $t , $title );
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
return $t ;
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
2014-12-01 12:22:48 +01:00
* Gets the URL segment for the latest draft version of this page .
*
2011-03-18 04:01:06 +01:00
* @ return string
*/
2012-09-19 12:07:46 +02:00
public function getStageURLSegment () {
2016-10-25 02:22:31 +02:00
$stageRecord = Versioned :: get_one_by_stage ( self :: class , Versioned :: DRAFT , array (
2013-06-21 00:45:33 +02:00
'"SiteTree"."ID"' => $this -> ID
));
2011-03-18 04:01:06 +01:00
return ( $stageRecord ) ? $stageRecord -> URLSegment : null ;
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
2014-12-01 12:22:48 +01:00
* Gets the URL segment for the currently published version of this page .
*
2011-03-18 04:01:06 +01:00
* @ return string
*/
2012-09-19 12:07:46 +02:00
public function getLiveURLSegment () {
2016-10-25 02:22:31 +02:00
$liveRecord = Versioned :: get_one_by_stage ( self :: class , Versioned :: LIVE , array (
2013-06-21 00:45:33 +02:00
'"SiteTree"."ID"' => $this -> ID
));
2011-03-18 04:01:06 +01:00
return ( $liveRecord ) ? $liveRecord -> URLSegment : null ;
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
2014-12-01 12:22:48 +01:00
* Returns the pages that depend on this page . This includes virtual pages , pages that link to it , etc .
2016-01-06 00:42:07 +01:00
*
2014-02-10 21:35:13 +01:00
* @ param bool $includeVirtuals Set to false to exlcude virtual pages .
* @ return ArrayList
2011-03-18 04:01:06 +01:00
*/
2012-09-19 12:07:46 +02:00
public function DependentPages ( $includeVirtuals = true ) {
2011-10-07 09:38:11 +02:00
if ( class_exists ( 'Subsite' )) {
$origDisableSubsiteFilter = Subsite :: $disable_subsite_filter ;
Subsite :: disable_subsite_filter ( true );
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// Content links
2013-06-21 00:45:33 +02:00
$items = new ArrayList ();
// We merge all into a regular SS_List, because DataList doesn't support merge
if ( $contentLinks = $this -> BackLinkTracking ()) {
$linkList = new ArrayList ();
foreach ( $contentLinks as $item ) {
$item -> DependentLinkType = 'Content link' ;
$linkList -> push ( $item );
}
2013-01-07 23:34:25 +01:00
$items -> merge ( $linkList );
2013-06-21 00:45:33 +02:00
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// Virtual pages
if ( $includeVirtuals ) {
$virtuals = $this -> VirtualPages ();
if ( $virtuals ) {
2013-01-07 23:34:25 +01:00
$virtualList = new ArrayList ();
foreach ( $virtuals as $item ) {
$item -> DependentLinkType = 'Virtual page' ;
$virtualList -> push ( $item );
2013-06-21 00:45:33 +02:00
}
2013-08-20 20:48:24 +02:00
$items -> merge ( $virtualList );
2013-06-21 00:45:33 +02:00
}
2011-03-18 04:01:06 +01:00
}
// Redirector pages
2013-06-21 00:45:33 +02:00
$redirectors = RedirectorPage :: get () -> where ( array (
'"RedirectorPage"."RedirectionType"' => 'Internal' ,
'"RedirectorPage"."LinkToID"' => $this -> ID
));
2011-03-18 04:01:06 +01:00
if ( $redirectors ) {
2013-01-07 23:34:25 +01:00
$redirectorList = new ArrayList ();
foreach ( $redirectors as $item ) {
$item -> DependentLinkType = 'Redirector page' ;
$redirectorList -> push ( $item );
2013-06-21 00:45:33 +02:00
}
2013-01-07 23:34:25 +01:00
$items -> merge ( $redirectorList );
2011-03-18 04:01:06 +01:00
}
2016-08-10 06:08:39 +02:00
if ( class_exists ( 'Subsite' )) {
Subsite :: disable_subsite_filter ( $origDisableSubsiteFilter );
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
return $items ;
}
2012-12-04 22:27:12 +01:00
2011-03-18 04:01:06 +01:00
/**
2014-12-01 12:22:48 +01:00
* Return all virtual pages that link to this page .
*
* @ return DataList
2011-03-18 04:01:06 +01:00
*/
2012-09-19 12:07:46 +02:00
public function VirtualPages () {
2016-03-17 01:02:50 +01:00
$pages = parent :: VirtualPages ();
2016-03-08 21:50:55 +01:00
2016-03-17 01:02:50 +01:00
// Disable subsite filter for these pages
if ( $pages instanceof DataList ) {
return $pages -> setDataQueryParam ( 'Subsite.filter' , false );
} else {
return $pages ;
2013-06-21 00:45:33 +02:00
}
2011-03-18 04:01:06 +01:00
}
/**
2011-10-26 07:35:51 +02:00
* Returns a FieldList with which to create the main editing form .
2011-03-18 04:01:06 +01:00
*
2014-12-01 12:22:48 +01:00
* You can override this in your child classes to add extra fields - first get the parent fields using
* parent :: getCMSFields (), then use addFieldToTab () on the FieldList .
*
* See { @ link getSettingsFields ()} for a different set of fields concerned with configuration aspects on the record ,
* e . g . access control .
2011-03-18 04:01:06 +01:00
*
2014-12-01 12:22:48 +01:00
* @ return FieldList The fields to be displayed in the CMS
2011-03-18 04:01:06 +01:00
*/
2012-09-19 12:07:46 +02:00
public function getCMSFields () {
2011-03-18 04:01:06 +01:00
// Status / message
// Create a status message for multiple parents
if ( $this -> ID && is_numeric ( $this -> ID )) {
$linkedPages = $this -> VirtualPages ();
2012-02-17 00:06:12 +01:00
$parentPageLinks = array ();
2016-08-10 06:08:39 +02:00
if ( $linkedPages -> count () > 0 ) {
/** @var VirtualPage $linkedPage */
2012-02-17 00:06:12 +01:00
foreach ( $linkedPages as $linkedPage ) {
2016-08-10 06:08:39 +02:00
$parentPage = $linkedPage -> Parent ();
if ( $parentPage && $parentPage -> exists ()) {
$link = Convert :: raw2att ( $parentPage -> CMSEditLink ());
$title = Convert :: raw2xml ( $parentPage -> Title );
2016-08-11 03:18:02 +02:00
} else {
2016-08-10 06:08:39 +02:00
$link = CMSPageEditController :: singleton () -> Link ( 'show' );
$title = _t ( 'SiteTree.TOPLEVEL' , 'Site Content (Top Level)' );
2016-08-11 03:18:02 +02:00
}
2016-08-10 06:08:39 +02:00
$parentPageLinks [] = " <a class= \" cmsEditlink \" href= \" { $link } \" > { $title } </a> " ;
2011-03-18 04:01:06 +01:00
}
2012-02-17 00:06:12 +01:00
$lastParent = array_pop ( $parentPageLinks );
$parentList = " ' $lastParent ' " ;
2011-03-18 04:01:06 +01:00
2016-08-10 06:08:39 +02:00
if ( count ( $parentPageLinks )) {
2012-02-17 00:06:12 +01:00
$parentList = " ' " . implode ( " ', ' " , $parentPageLinks ) . " ' and "
. $parentList ;
}
2011-03-18 04:01:06 +01:00
2012-05-01 21:43:43 +02:00
$statusMessage [] = _t (
2016-01-06 00:42:07 +01:00
'SiteTree.APPEARSVIRTUALPAGES' ,
2012-05-01 21:43:43 +02:00
" This content also appears on the virtual pages in the { title} sections. " ,
array ( 'title' => $parentList )
2012-02-17 00:06:12 +01:00
);
}
2011-03-18 04:01:06 +01:00
}
if ( $this -> HasBrokenLink || $this -> HasBrokenFile ) {
$statusMessage [] = _t ( 'SiteTree.HASBROKENLINKS' , " This page has broken links. " );
}
2012-08-29 04:48:23 +02:00
2011-03-18 04:01:06 +01:00
$dependentNote = '' ;
$dependentTable = new LiteralField ( 'DependentNote' , '<p></p>' );
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// Create a table for showing pages linked to this one
2012-12-04 22:27:12 +01:00
$dependentPages = $this -> DependentPages ();
2016-08-10 06:08:39 +02:00
$dependentPagesCount = $dependentPages -> count ();
2011-03-18 04:01:06 +01:00
if ( $dependentPagesCount ) {
$dependentColumns = array (
'Title' => $this -> fieldLabel ( 'Title' ),
'AbsoluteLink' => _t ( 'SiteTree.DependtPageColumnURL' , 'URL' ),
'DependentLinkType' => _t ( 'SiteTree.DependtPageColumnLinkType' , 'Link type' ),
);
if ( class_exists ( 'Subsite' )) $dependentColumns [ 'Subsite.Title' ] = singleton ( 'Subsite' ) -> i18n_singular_name ();
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
$dependentNote = new LiteralField ( 'DependentNote' , '<p>' . _t ( 'SiteTree.DEPENDENT_NOTE' , 'The following pages depend on this page. This includes virtual pages, redirector pages, and pages with content links.' ) . '</p>' );
2012-04-30 16:10:24 +02:00
$dependentTable = GridField :: create (
2011-03-18 04:01:06 +01:00
'DependentPages' ,
2012-04-30 16:10:24 +02:00
false ,
2012-12-04 22:27:12 +01:00
$dependentPages
2011-03-18 04:01:06 +01:00
);
2016-08-10 06:08:39 +02:00
/** @var GridFieldDataColumns $dataColumns */
2016-08-23 04:36:06 +02:00
$dataColumns = $dependentTable -> getConfig () -> getComponentByType ( 'SilverStripe\\Forms\\GridField\\GridFieldDataColumns' );
2016-08-10 06:08:39 +02:00
$dataColumns
2013-01-07 23:34:25 +01:00
-> setDisplayFields ( $dependentColumns )
2012-04-30 16:10:24 +02:00
-> setFieldFormatting ( array (
2013-09-24 12:12:21 +02:00
'Title' => function ( $value , & $item ) {
return sprintf (
2014-10-20 04:18:25 +02:00
'<a href="admin/pages/edit/show/%d">%s</a>' ,
2013-09-24 12:12:21 +02:00
( int ) $item -> ID ,
Convert :: raw2xml ( $item -> Title )
);
},
'AbsoluteLink' => function ( $value , & $item ) {
return sprintf (
2014-10-20 04:18:25 +02:00
'<a href="%s" target="_blank">%s</a>' ,
2013-09-24 12:12:21 +02:00
Convert :: raw2xml ( $value ),
Convert :: raw2xml ( $value )
);
}
2012-04-30 16:10:24 +02:00
));
2011-03-18 04:01:06 +01:00
}
2016-03-08 21:50:55 +01:00
2011-08-01 06:23:00 +02:00
$baseLink = Controller :: join_links (
Director :: absoluteBaseURL (),
2013-03-18 11:47:15 +01:00
( self :: config () -> nested_urls && $this -> ParentID ? $this -> Parent () -> RelativeLink ( true ) : null )
2011-08-01 06:23:00 +02:00
);
2016-03-08 21:50:55 +01:00
2015-02-20 23:33:59 +01:00
$urlsegment = SiteTreeURLSegmentField :: create ( " URLSegment " , $this -> fieldLabel ( 'URLSegment' ))
-> setURLPrefix ( $baseLink )
-> setDefaultURL ( $this -> generateURLSegment ( _t (
'CMSMain.NEWPAGE' ,
array ( 'pagetype' => $this -> i18n_singular_name ())
)));
2016-08-10 06:08:39 +02:00
$helpText = ( self :: config () -> nested_urls && $this -> Children () -> count ())
? $this -> fieldLabel ( 'LinkChangeNote' )
: '' ;
2016-08-23 04:36:06 +02:00
if ( ! Config :: inst () -> get ( 'SilverStripe\\View\\Parsers\\URLSegmentFilter' , 'default_allow_multibyte' )) {
2012-05-10 14:18:22 +02:00
$helpText .= _t ( 'SiteTreeURLSegmentField.HelpChars' , ' Special characters are automatically converted or removed.' );
}
$urlsegment -> setHelpText ( $helpText );
2016-03-08 21:50:55 +01:00
2011-10-26 07:35:51 +02:00
$fields = new FieldList (
2011-03-18 04:01:06 +01:00
$rootTab = new TabSet ( " Root " ,
2011-04-15 06:35:19 +02:00
$tabMain = new Tab ( 'Main' ,
new TextField ( " Title " , $this -> fieldLabel ( 'Title' )),
2012-05-10 02:01:57 +02:00
$urlsegment ,
2011-04-15 06:35:19 +02:00
new TextField ( " MenuTitle " , $this -> fieldLabel ( 'MenuTitle' )),
2016-05-06 04:31:19 +02:00
$htmlField = new HTMLEditorField ( " Content " , _t ( 'SiteTree.HTMLEDITORTITLE' , " Content " , 'HTML editor title' )),
2012-07-15 21:29:46 +02:00
ToggleCompositeField :: create ( 'Metadata' , _t ( 'SiteTree.MetadataToggle' , 'Metadata' ),
array (
2012-09-20 03:22:12 +02:00
$metaFieldDesc = new TextareaField ( " MetaDescription " , $this -> fieldLabel ( 'MetaDescription' )),
$metaFieldExtra = new TextareaField ( " ExtraMeta " , $this -> fieldLabel ( 'ExtraMeta' ))
2012-07-15 21:29:46 +02:00
)
) -> setHeadingLevel ( 4 )
2011-03-18 04:01:06 +01:00
),
2011-04-15 06:35:19 +02:00
$tabDependent = new Tab ( 'Dependent' ,
$dependentNote ,
$dependentTable
)
)
);
2012-01-03 17:57:04 +01:00
$htmlField -> addExtraClass ( 'stacked' );
2016-03-08 21:50:55 +01:00
2012-09-20 03:22:12 +02:00
// Help text for MetaData on page content editor
2012-09-21 11:31:00 +02:00
$metaFieldDesc
-> setRightTitle (
_t (
2016-01-06 00:42:07 +01:00
'SiteTree.METADESCHELP' ,
2012-09-21 11:31:00 +02:00
" Search engines use this content for displaying search results (although it will not influence their ranking). "
)
)
-> addExtraClass ( 'help' );
$metaFieldExtra
-> setRightTitle (
_t (
2016-01-06 00:42:07 +01:00
'SiteTree.METAEXTRAHELP' ,
2012-09-21 11:31:00 +02:00
" HTML tags for additional meta information. For example <meta name= \" customName \" content= \" your custom content here \" /> "
)
)
-> addExtraClass ( 'help' );
2012-09-20 03:22:12 +02:00
2011-04-15 06:35:19 +02:00
// Conditional dependent pages tab
if ( $dependentPagesCount ) $tabDependent -> setTitle ( _t ( 'SiteTree.TABDEPENDENT' , " Dependent pages " ) . " ( $dependentPagesCount ) " );
else $fields -> removeFieldFromTab ( 'Root' , 'Dependent' );
2016-03-08 21:50:55 +01:00
2012-04-18 16:28:07 +02:00
$tabMain -> setTitle ( _t ( 'SiteTree.TABCONTENT' , " Main Content " ));
2011-04-15 06:35:19 +02:00
2012-08-29 04:48:23 +02:00
if ( $this -> ObsoleteClassName ) {
$obsoleteWarning = _t (
'SiteTree.OBSOLETECLASS' ,
2013-01-17 20:31:35 +01:00
" This page is of obsolete type { type}. Saving will reset its type and you may lose data " ,
2012-08-29 04:48:23 +02:00
array ( 'type' => $this -> ObsoleteClassName )
);
$fields -> addFieldToTab (
" Root.Main " ,
new LiteralField ( " ObsoleteWarningHeader " , " <p class= \" message warning \" > $obsoleteWarning </p> " ),
" Title "
);
}
2011-04-15 06:35:19 +02:00
if ( file_exists ( BASE_PATH . '/install.php' )) {
2016-01-06 00:42:07 +01:00
$fields -> addFieldToTab ( " Root.Main " , new LiteralField ( " InstallWarningHeader " ,
" <p class= \" message warning \" > " . _t ( " SiteTree.REMOVE_INSTALL_WARNING " ,
2011-04-15 06:35:19 +02:00
" Warning: You should remove install.php from this SilverStripe install for security reasons. " )
. " </p> " ), " Title " );
}
if ( self :: $runCMSFieldsExtensions ) {
$this -> extend ( 'updateCMSFields' , $fields );
}
return $fields ;
}
2016-03-08 21:50:55 +01:00
2011-04-15 06:35:19 +02:00
/**
2014-12-01 12:22:48 +01:00
* Returns fields related to configuration aspects on this record , e . g . access control . See { @ link getCMSFields ()}
* for content - related fields .
2016-01-06 00:42:07 +01:00
*
2011-10-26 07:35:51 +02:00
* @ return FieldList
2011-04-15 06:35:19 +02:00
*/
2012-09-19 12:07:46 +02:00
public function getSettingsFields () {
2013-10-11 00:27:14 +02:00
$groupsMap = array ();
foreach ( Group :: get () as $group ) {
// Listboxfield values are escaped, use ASCII char instead of »
$groupsMap [ $group -> ID ] = $group -> getBreadcrumbs ( ' > ' );
}
2012-03-04 22:10:25 +01:00
asort ( $groupsMap );
2016-03-08 21:50:55 +01:00
2011-10-26 07:35:51 +02:00
$fields = new FieldList (
2011-04-15 06:35:19 +02:00
$rootTab = new TabSet ( " Root " ,
$tabBehaviour = new Tab ( 'Settings' ,
2011-03-18 04:01:06 +01:00
new DropdownField (
2016-01-06 00:42:07 +01:00
" ClassName " ,
$this -> fieldLabel ( 'ClassName' ),
2011-03-18 04:01:06 +01:00
$this -> getClassDropdown ()
),
2012-02-11 00:49:51 +01:00
$parentTypeSelector = new CompositeField (
2016-08-09 07:19:45 +02:00
$parentType = new OptionsetField ( " ParentType " , _t ( " SiteTree.PAGELOCATION " , " Page location " ), array (
2012-02-11 00:49:51 +01:00
" root " => _t ( " SiteTree.PARENTTYPE_ROOT " , " Top-level page " ),
2012-04-23 11:02:29 +02:00
" subpage " => _t ( " SiteTree.PARENTTYPE_SUBPAGE " , " Sub-page underneath a parent page " ),
2012-02-11 00:49:51 +01:00
)),
2016-10-25 02:22:31 +02:00
$parentIDField = new TreeDropdownField ( " ParentID " , $this -> fieldLabel ( 'ParentID' ), self :: class , 'ID' , 'MenuTitle' )
2012-02-11 00:49:51 +01:00
),
2012-04-14 08:16:47 +02:00
$visibility = new FieldGroup (
new CheckboxField ( " ShowInMenus " , $this -> fieldLabel ( 'ShowInMenus' )),
new CheckboxField ( " ShowInSearch " , $this -> fieldLabel ( 'ShowInSearch' ))
2012-05-09 17:48:59 +02:00
),
2011-03-18 04:01:06 +01:00
$viewersOptionsField = new OptionsetField (
2016-01-06 00:42:07 +01:00
" CanViewType " ,
2011-04-19 12:37:48 +02:00
_t ( 'SiteTree.ACCESSHEADER' , " Who can view this page? " )
2011-03-18 04:01:06 +01:00
),
2012-04-04 16:59:22 +02:00
$viewerGroupsField = ListboxField :: create ( " ViewerGroups " , _t ( 'SiteTree.VIEWERGROUPS' , " Viewer Groups " ))
2012-12-15 19:57:14 +01:00
-> setSource ( $groupsMap )
-> setAttribute (
2016-01-06 00:42:07 +01:00
'data-placeholder' ,
2012-12-15 19:57:14 +01:00
_t ( 'SiteTree.GroupPlaceholder' , 'Click to select group' )
),
2011-03-18 04:01:06 +01:00
$editorsOptionsField = new OptionsetField (
2016-01-06 00:42:07 +01:00
" CanEditType " ,
2011-04-19 12:37:48 +02:00
_t ( 'SiteTree.EDITHEADER' , " Who can edit this page? " )
2011-03-18 04:01:06 +01:00
),
2012-04-04 16:59:22 +02:00
$editorGroupsField = ListboxField :: create ( " EditorGroups " , _t ( 'SiteTree.EDITORGROUPS' , " Editor Groups " ))
2012-12-15 19:57:14 +01:00
-> setSource ( $groupsMap )
-> setAttribute (
2016-01-06 00:42:07 +01:00
'data-placeholder' ,
2012-12-15 19:57:14 +01:00
_t ( 'SiteTree.GroupPlaceholder' , 'Click to select group' )
2013-06-21 00:45:33 +02:00
)
2011-03-18 04:01:06 +01:00
)
)
);
2016-03-08 21:50:55 +01:00
2016-08-09 07:19:45 +02:00
$parentType -> addExtraClass ( 'noborder' );
2012-04-14 08:16:47 +02:00
$visibility -> setTitle ( $this -> fieldLabel ( 'Visibility' ));
2016-03-08 21:50:55 +01:00
2014-12-01 12:22:48 +01:00
// This filter ensures that the ParentID dropdown selection does not show this node,
// or its descendents, as this causes vanishing bugs
2011-03-18 04:01:06 +01:00
$parentIDField -> setFilterFunction ( create_function ( '$node' , " return \$ node->ID != { $this -> ID } ; " ));
2012-02-11 00:49:51 +01:00
$parentTypeSelector -> addExtraClass ( 'parentTypeSelector' );
2016-03-08 21:50:55 +01:00
2011-04-15 06:35:19 +02:00
$tabBehaviour -> setTitle ( _t ( 'SiteTree.TABBEHAVIOUR' , " Behavior " ));
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// Make page location fields read-only if the user doesn't have the appropriate permission
if ( ! Permission :: check ( " SITETREE_REORGANISE " )) {
$fields -> makeFieldReadonly ( 'ParentType' );
2016-08-10 06:08:39 +02:00
if ( $this -> getParentType () === 'root' ) {
2011-03-18 04:01:06 +01:00
$fields -> removeByName ( 'ParentID' );
} else {
$fields -> makeFieldReadonly ( 'ParentID' );
}
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
$viewersOptionsSource = array ();
$viewersOptionsSource [ " Inherit " ] = _t ( 'SiteTree.INHERIT' , " Inherit from parent page " );
$viewersOptionsSource [ " Anyone " ] = _t ( 'SiteTree.ACCESSANYONE' , " Anyone " );
$viewersOptionsSource [ " LoggedInUsers " ] = _t ( 'SiteTree.ACCESSLOGGEDIN' , " Logged-in users " );
$viewersOptionsSource [ " OnlyTheseUsers " ] = _t ( 'SiteTree.ACCESSONLYTHESE' , " Only these people (choose from list) " );
$viewersOptionsField -> setSource ( $viewersOptionsSource );
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
$editorsOptionsSource = array ();
$editorsOptionsSource [ " Inherit " ] = _t ( 'SiteTree.INHERIT' , " Inherit from parent page " );
$editorsOptionsSource [ " LoggedInUsers " ] = _t ( 'SiteTree.EDITANYONE' , " Anyone who can log-in to the CMS " );
$editorsOptionsSource [ " OnlyTheseUsers " ] = _t ( 'SiteTree.EDITONLYTHESE' , " Only these people (choose from list) " );
$editorsOptionsField -> setSource ( $editorsOptionsSource );
if ( ! Permission :: check ( 'SITETREE_GRANT_ACCESS' )) {
$fields -> makeFieldReadonly ( $viewersOptionsField );
if ( $this -> CanViewType == 'OnlyTheseUsers' ) {
$fields -> makeFieldReadonly ( $viewerGroupsField );
} else {
$fields -> removeByName ( 'ViewerGroups' );
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
$fields -> makeFieldReadonly ( $editorsOptionsField );
if ( $this -> CanEditType == 'OnlyTheseUsers' ) {
$fields -> makeFieldReadonly ( $editorGroupsField );
} else {
$fields -> removeByName ( 'EditorGroups' );
}
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
if ( self :: $runCMSFieldsExtensions ) {
2011-04-15 06:35:19 +02:00
$this -> extend ( 'updateSettingsFields' , $fields );
2011-03-18 04:01:06 +01:00
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
return $fields ;
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
2014-12-01 12:22:48 +01:00
* @ param bool $includerelations A boolean value to indicate if the labels returned should include relation fields
* @ return array
2011-03-18 04:01:06 +01:00
*/
2012-09-19 12:07:46 +02:00
public function fieldLabels ( $includerelations = true ) {
2016-09-28 00:39:28 +02:00
$cacheKey = static :: class . '_' . $includerelations ;
2012-04-13 18:38:57 +02:00
if ( ! isset ( self :: $_cache_field_labels [ $cacheKey ])) {
$labels = parent :: fieldLabels ( $includerelations );
$labels [ 'Title' ] = _t ( 'SiteTree.PAGETITLE' , " Page name " );
$labels [ 'MenuTitle' ] = _t ( 'SiteTree.MENUTITLE' , " Navigation label " );
$labels [ 'MetaDescription' ] = _t ( 'SiteTree.METADESC' , " Meta Description " );
$labels [ 'ExtraMeta' ] = _t ( 'SiteTree.METAEXTRA' , " Custom Meta Tags " );
2012-04-13 21:54:56 +02:00
$labels [ 'ClassName' ] = _t ( 'SiteTree.PAGETYPE' , " Page type " , 'Classname of a page object' );
$labels [ 'ParentType' ] = _t ( 'SiteTree.PARENTTYPE' , " Page location " );
$labels [ 'ParentID' ] = _t ( 'SiteTree.PARENTID' , " Parent page " );
2012-04-13 18:38:57 +02:00
$labels [ 'ShowInMenus' ] = _t ( 'SiteTree.SHOWINMENUS' , " Show in menus? " );
$labels [ 'ShowInSearch' ] = _t ( 'SiteTree.SHOWINSEARCH' , " Show in search? " );
$labels [ 'ProvideComments' ] = _t ( 'SiteTree.ALLOWCOMMENTS' , " Allow comments on this page? " );
$labels [ 'ViewerGroups' ] = _t ( 'SiteTree.VIEWERGROUPS' , " Viewer Groups " );
$labels [ 'EditorGroups' ] = _t ( 'SiteTree.EDITORGROUPS' , " Editor Groups " );
2012-04-13 21:54:56 +02:00
$labels [ 'URLSegment' ] = _t ( 'SiteTree.URLSegment' , 'URL Segment' , 'URL for this page' );
$labels [ 'Content' ] = _t ( 'SiteTree.Content' , 'Content' , 'Main HTML Content for a page' );
2012-04-13 18:38:57 +02:00
$labels [ 'CanViewType' ] = _t ( 'SiteTree.Viewers' , 'Viewers Groups' );
$labels [ 'CanEditType' ] = _t ( 'SiteTree.Editors' , 'Editors Groups' );
$labels [ 'Comments' ] = _t ( 'SiteTree.Comments' , 'Comments' );
$labels [ 'Visibility' ] = _t ( 'SiteTree.Visibility' , 'Visibility' );
$labels [ 'LinkChangeNote' ] = _t (
'SiteTree.LINKCHANGENOTE' , 'Changing this page\'s link will also affect the links of all child pages.'
);
2016-03-08 21:50:55 +01:00
2012-04-13 18:38:57 +02:00
if ( $includerelations ){
2012-04-13 21:54:56 +02:00
$labels [ 'Parent' ] = _t ( 'SiteTree.has_one_Parent' , 'Parent Page' , 'The parent page in the site hierarchy' );
2012-04-13 18:38:57 +02:00
$labels [ 'LinkTracking' ] = _t ( 'SiteTree.many_many_LinkTracking' , 'Link Tracking' );
$labels [ 'ImageTracking' ] = _t ( 'SiteTree.many_many_ImageTracking' , 'Image Tracking' );
$labels [ 'BackLinkTracking' ] = _t ( 'SiteTree.many_many_BackLinkTracking' , 'Backlink Tracking' );
}
self :: $_cache_field_labels [ $cacheKey ] = $labels ;
2011-03-18 04:01:06 +01:00
}
2012-04-13 18:38:57 +02:00
return self :: $_cache_field_labels [ $cacheKey ];
2011-03-18 04:01:06 +01:00
}
/**
* Get the actions available in the CMS for this page - eg Save , Publish .
2012-11-21 21:46:45 +01:00
*
* Frontend scripts and styles know how to handle the following FormFields :
2014-12-01 12:22:48 +01:00
* - top - level FormActions appear as standalone buttons
* - top - level CompositeField with FormActions within appear as grouped buttons
* - TabSet & Tabs appear as a drop ups
* - FormActions within the Tab are restyled as links
* - major actions can provide alternate states for richer presentation ( see ssui . button widget extension )
2012-11-21 21:46:45 +01:00
*
2011-10-26 07:35:51 +02:00
* @ return FieldList The available actions for this page .
2011-03-18 04:01:06 +01:00
*/
2012-09-19 12:07:46 +02:00
public function getCMSActions () {
2016-10-25 02:22:31 +02:00
// Get status of page
$isOnDraft = $this -> isOnDraft ();
$isPublished = $this -> isPublished ();
$stagesDiffer = $this -> stagesDiffer ( Versioned :: DRAFT , Versioned :: LIVE );
2012-11-21 21:46:45 +01:00
// Major actions appear as buttons immediately visible as page actions.
2016-10-25 02:22:31 +02:00
$majorActions = CompositeField :: create () -> setName ( 'MajorActions' );
2016-09-21 05:02:06 +02:00
$majorActions -> setFieldHolderTemplate ( get_class ( $majorActions ) . '_holder_buttongroup' );
2012-11-21 21:46:45 +01:00
// Minor options are hidden behind a drop-up and appear as links (although they are still FormActions).
$rootTabSet = new TabSet ( 'ActionMenus' );
$moreOptions = new Tab (
2016-01-06 00:42:07 +01:00
'MoreOptions' ,
2012-11-21 21:46:45 +01:00
_t ( 'SiteTree.MoreOptions' , 'More options' , 'Expands a view for more buttons' )
);
$rootTabSet -> push ( $moreOptions );
2016-01-26 15:42:41 +01:00
$rootTabSet -> addExtraClass ( 'ss-ui-action-tabset action-menus noborder' );
2012-11-21 21:46:45 +01:00
// Render page information into the "more-options" drop-up, on the top.
2016-10-25 02:22:31 +02:00
$liveRecord = Versioned :: get_by_stage ( self :: class , Versioned :: LIVE ) -> byID ( $this -> ID );
2016-09-28 00:39:28 +02:00
$infoTemplate = SSViewer :: get_templates_by_class ( static :: class , '_Information' , self :: class );
2012-11-21 21:46:45 +01:00
$moreOptions -> push (
new LiteralField ( 'Information' ,
$this -> customise ( array (
2016-10-25 02:22:31 +02:00
'Live' => $liveRecord ,
'ExistsOnLive' => $isPublished
2016-08-23 04:36:06 +02:00
)) -> renderWith ( $infoTemplate )
2012-11-21 21:46:45 +01:00
)
);
2016-03-17 02:42:53 +01:00
$moreOptions -> push ( AddToCampaignHandler_FormAction :: create ());
2011-03-18 04:01:06 +01:00
// "readonly"/viewing version that isn't the current version of the record
2016-10-25 02:22:31 +02:00
$stageRecord = Versioned :: get_by_stage ( static :: class , Versioned :: DRAFT ) -> byID ( $this -> ID );
/** @skipUpgrade */
if ( $stageRecord && $stageRecord -> Version != $this -> Version ) {
$moreOptions -> push ( FormAction :: create ( 'email' , _t ( 'CMSMain.EMAIL' , 'Email' )));
2012-11-21 21:46:45 +01:00
$moreOptions -> push ( FormAction :: create ( 'rollback' , _t ( 'CMSMain.ROLLBACK' , 'Roll back to this version' )));
2012-12-14 01:11:52 +01:00
$actions = new FieldList ( array ( $majorActions , $rootTabSet ));
2011-04-15 11:37:15 +02:00
// getCMSActions() can be extended with updateCMSActions() on a extension
2011-03-18 04:01:06 +01:00
$this -> extend ( 'updateCMSActions' , $actions );
return $actions ;
}
2016-10-25 02:22:31 +02:00
// Check permissions
$canPublish = $this -> canPublish ();
$canUnpublish = $this -> canUnpublish ();
$canEdit = $this -> canEdit ();
// "unpublish"
if ( $isPublished && $canPublish && $isOnDraft && $canUnpublish ) {
2012-11-21 21:46:45 +01:00
$moreOptions -> push (
2012-02-16 22:59:47 +01:00
FormAction :: create ( 'unpublish' , _t ( 'SiteTree.BUTTONUNPUBLISH' , 'Unpublish' ), 'delete' )
2012-03-07 12:03:00 +01:00
-> setDescription ( _t ( 'SiteTree.BUTTONUNPUBLISHDESC' , 'Remove this page from the published site' ))
2012-11-21 21:46:45 +01:00
-> addExtraClass ( 'ss-ui-action-destructive' )
2012-02-16 22:59:47 +01:00
);
2011-03-18 04:01:06 +01:00
}
2016-10-25 02:22:31 +02:00
// "rollback"
if ( $isOnDraft && $isPublished && $canEdit && $stagesDiffer ) {
$moreOptions -> push (
FormAction :: create ( 'rollback' , _t ( 'SiteTree.BUTTONCANCELDRAFT' , 'Cancel draft changes' ))
-> setDescription ( _t (
'SiteTree.BUTTONCANCELDRAFTDESC' ,
'Delete your draft and revert to the currently published page'
))
);
2011-03-18 04:01:06 +01:00
}
2016-10-25 02:22:31 +02:00
// "restore"
if ( $canEdit && ! $isOnDraft && $isPublished ) {
$majorActions -> push ( FormAction :: create ( 'revert' , _t ( 'CMSMain.RESTORE' , 'Restore' )));
}
2016-03-08 21:50:55 +01:00
2016-10-25 02:22:31 +02:00
// Check if we can restore a deleted page
// Note: It would be nice to have a canRestore() permission at some point
if ( $canEdit && ! $isOnDraft && ! $isPublished ) {
// Determine if we should force a restore to root (where once it was a subpage)
$restoreToRoot = $this -> isParentArchived ();
// "restore"
$title = $restoreToRoot
? _t ( 'CMSMain.RESTORE_TO_ROOT' , 'Restore draft at top level' )
: _t ( 'CMSMain.RESTORE' , 'Restore draft' );
$description = $restoreToRoot
? _t ( 'CMSMain.RESTORE_TO_ROOT_DESC' , 'Restore the archived version to draft as a top level page' )
: _t ( 'CMSMain.RESTORE_DESC' , 'Restore the archived version to draft' );
$majorActions -> push (
FormAction :: create ( 'restore' , $title )
-> setDescription ( $description )
-> setAttribute ( 'data-to-root' , $restoreToRoot )
-> setAttribute ( 'data-icon' , 'decline' )
);
}
// If a page is on any stage it can be archived
if (( $isOnDraft || $isPublished ) && $this -> canArchive ()) {
$title = $isPublished
? _t ( 'CMSMain.UNPUBLISH_AND_ARCHIVE' , 'Unpublish and archive' )
: _t ( 'CMSMain.ARCHIVE' , 'Archive' );
$moreOptions -> push (
FormAction :: create ( 'archive' , $title )
-> addExtraClass ( 'delete ss-ui-action-destructive' )
-> setDescription ( _t (
'SiteTree.BUTTONDELETEDESC' ,
'Remove from draft/live and send to archive'
))
);
}
// "save", supports an alternate state that is still clickable, but notifies the user that the action is not needed.
if ( $canEdit && $isOnDraft ) {
$majorActions -> push (
FormAction :: create ( 'save' , _t ( 'SiteTree.BUTTONSAVED' , 'Saved' ))
-> setAttribute ( 'data-icon' , 'accept' )
-> setAttribute ( 'data-icon-alternate' , 'addpage' )
-> setAttribute ( 'data-text-alternate' , _t ( 'CMSMain.SAVEDRAFT' , 'Save draft' ))
);
2011-03-18 04:01:06 +01:00
}
2016-10-25 02:22:31 +02:00
if ( $canPublish && $isOnDraft ) {
2012-11-21 21:46:45 +01:00
// "publish", as with "save", it supports an alternate state to show when action is needed.
$majorActions -> push (
$publish = FormAction :: create ( 'publish' , _t ( 'SiteTree.BUTTONPUBLISHED' , 'Published' ))
-> setAttribute ( 'data-icon' , 'accept' )
-> setAttribute ( 'data-icon-alternate' , 'disk' )
-> setAttribute ( 'data-text-alternate' , _t ( 'SiteTree.BUTTONSAVEPUBLISH' , 'Save & publish' ))
2012-02-16 22:59:47 +01:00
);
2012-11-21 21:46:45 +01:00
// Set up the initial state of the button to reflect the state of the underlying SiteTree object.
2016-10-25 02:22:31 +02:00
if ( $stagesDiffer ) {
2012-11-21 21:46:45 +01:00
$publish -> addExtraClass ( 'ss-ui-alternate' );
2013-06-21 00:45:33 +02:00
}
2011-03-18 04:01:06 +01:00
}
2016-03-08 21:50:55 +01:00
2012-12-14 01:11:52 +01:00
$actions = new FieldList ( array ( $majorActions , $rootTabSet ));
2016-03-08 21:50:55 +01:00
2012-11-21 21:46:45 +01:00
// Hook for extensions to add/remove actions.
2011-03-18 04:01:06 +01:00
$this -> extend ( 'updateCMSActions' , $actions );
2016-03-08 21:50:55 +01:00
2012-12-14 01:11:52 +01:00
return $actions ;
2011-03-18 04:01:06 +01:00
}
2016-03-08 21:50:55 +01:00
2016-03-17 01:02:50 +01:00
public function onAfterPublish () {
// Force live sort order to match stage sort order
2013-06-21 00:45:33 +02:00
DB :: prepared_query ( ' UPDATE " SiteTree_Live "
SET " Sort " = ( SELECT " SiteTree " . " Sort " FROM " SiteTree " WHERE " SiteTree_Live " . " ID " = " SiteTree " . " ID " )
WHERE EXISTS ( SELECT " SiteTree " . " Sort " FROM " SiteTree " WHERE " SiteTree_Live " . " ID " = " SiteTree " . " ID " ) AND " ParentID " = ? ' ,
array ( $this -> ParentID )
);
2016-03-09 23:57:39 +01:00
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
2016-03-17 01:02:50 +01:00
* Update draft dependant pages
2011-03-18 04:01:06 +01:00
*/
2016-03-17 01:02:50 +01:00
public function onAfterRevertToLive () {
// Use an alias to get the updates made by $this->publish
/** @var SiteTree $stageSelf */
2016-10-25 02:22:31 +02:00
$stageSelf = Versioned :: get_by_stage ( self :: class , Versioned :: DRAFT ) -> byID ( $this -> ID );
2016-03-17 01:02:50 +01:00
$stageSelf -> writeWithoutVersion ();
2011-03-18 04:01:06 +01:00
// Need to update pages linking to this one as no longer broken
2016-03-17 01:02:50 +01:00
foreach ( $stageSelf -> DependentPages () as $page ) {
/** @var SiteTree $page */
$page -> writeWithoutVersion ();
2011-03-18 04:01:06 +01:00
}
2015-05-15 01:51:23 +02:00
}
/**
* Determine if this page references a parent which is archived , and not available in stage
*
* @ return bool True if there is an archived parent
*/
protected function isParentArchived () {
if ( $parentID = $this -> ParentID ) {
2016-10-05 03:08:34 +02:00
/** @var SiteTree $parentPage */
$parentPage = Versioned :: get_latest_version ( self :: class , $parentID );
if ( ! $parentPage || ! $parentPage -> isOnDraft ()) {
2015-05-15 01:51:23 +02:00
return true ;
}
}
return false ;
2011-03-18 04:01:06 +01:00
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
* Restore the content in the active copy of this SiteTree page to the stage site .
2014-12-01 12:22:48 +01:00
*
* @ return self
2011-03-18 04:01:06 +01:00
*/
2012-09-19 12:07:46 +02:00
public function doRestoreToStage () {
2015-11-10 03:30:15 +01:00
$this -> invokeWithExtensions ( 'onBeforeRestoreToStage' , $this );
2015-05-15 01:51:23 +02:00
// Ensure that the parent page is restored, otherwise restore to root
if ( $this -> isParentArchived ()) {
$this -> ParentID = 0 ;
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// if no record can be found on draft stage (meaning it has been "deleted from draft" before),
// create an empty record
2013-06-21 00:45:33 +02:00
if ( ! DB :: prepared_query ( " SELECT \" ID \" FROM \" SiteTree \" WHERE \" ID \" = ? " , array ( $this -> ID )) -> value ()) {
$conn = DB :: get_conn ();
2016-10-25 02:22:31 +02:00
if ( method_exists ( $conn , 'allowPrimaryKeyEditing' )) $conn -> allowPrimaryKeyEditing ( self :: class , true );
2013-06-21 00:45:33 +02:00
DB :: prepared_query ( " INSERT INTO \" SiteTree \" ( \" ID \" ) VALUES (?) " , array ( $this -> ID ));
2016-10-25 02:22:31 +02:00
if ( method_exists ( $conn , 'allowPrimaryKeyEditing' )) $conn -> allowPrimaryKeyEditing ( self :: class , false );
2011-03-18 04:01:06 +01:00
}
2016-03-08 21:50:55 +01:00
2016-06-02 05:56:45 +02:00
$oldReadingMode = Versioned :: get_reading_mode ();
2016-03-17 01:02:50 +01:00
Versioned :: set_stage ( Versioned :: DRAFT );
2011-03-18 04:01:06 +01:00
$this -> forceChange ();
2012-04-10 12:27:08 +02:00
$this -> write ();
2016-03-08 21:50:55 +01:00
2016-08-10 06:08:39 +02:00
/** @var SiteTree $result */
2016-09-28 00:39:28 +02:00
$result = DataObject :: get_by_id ( self :: class , $this -> ID );
2011-03-18 04:01:06 +01:00
// Need to update pages linking to this one as no longer broken
foreach ( $result -> DependentPages ( false ) as $page ) {
// $page->write() calls syncLinkTracking, which does all the hard work for us.
$page -> write ();
}
2016-03-08 21:50:55 +01:00
2016-06-02 05:56:45 +02:00
Versioned :: set_reading_mode ( $oldReadingMode );
2015-11-10 03:30:15 +01:00
$this -> invokeWithExtensions ( 'onAfterRestoreToStage' , $this );
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
return $result ;
}
/**
2014-12-01 12:22:48 +01:00
* Check if this page is new - that is , if it has yet to have been written to the database .
2011-03-18 04:01:06 +01:00
*
2014-12-01 12:22:48 +01:00
* @ return bool
2011-03-18 04:01:06 +01:00
*/
2012-09-19 12:07:46 +02:00
public function isNew () {
2011-03-18 04:01:06 +01:00
/**
2014-12-01 12:22:48 +01:00
* This check was a problem for a self - hosted site , and may indicate a bug in the interpreter on their server ,
* or a bug here . Changing the condition from empty ( $this -> ID ) to ! $this -> ID && ! $this -> record [ 'ID' ] fixed this .
2011-03-18 04:01:06 +01:00
*/
if ( empty ( $this -> ID )) return true ;
if ( is_numeric ( $this -> ID )) return false ;
return stripos ( $this -> ID , 'new' ) === 0 ;
}
/**
2014-12-01 12:22:48 +01:00
* Get the class dropdown used in the CMS to change the class of a page . This returns the list of options in the
* dropdown as a Map from class name to singular name . Filters by { @ link SiteTree -> canCreate ()}, as well as
* { @ link SiteTree :: $needs_permission } .
2011-03-18 04:01:06 +01:00
*
* @ return array
*/
protected function getClassDropdown () {
$classes = self :: page_type_classes ();
$currentClass = null ;
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
$result = array ();
foreach ( $classes as $class ) {
$instance = singleton ( $class );
2012-07-16 04:56:24 +02:00
2014-12-01 12:22:48 +01:00
// if the current page type is this the same as the class type always show the page type in the list
2012-07-16 04:56:24 +02:00
if ( $this -> ClassName != $instance -> ClassName ) {
2016-01-02 23:06:06 +01:00
if ( $instance instanceof HiddenClass ) continue ;
if ( ! $instance -> canCreate ( null , array ( 'Parent' => $this -> ParentID ? $this -> Parent () : null ))) continue ;
2012-07-16 04:56:24 +02:00
}
2016-03-08 21:50:55 +01:00
2011-10-07 09:29:03 +02:00
if ( $perms = $instance -> stat ( 'need_permission' )) {
if ( ! $this -> can ( $perms )) continue ;
}
2011-03-18 04:01:06 +01:00
$pageTypeName = $instance -> i18n_singular_name ();
2012-06-15 16:22:27 +02:00
$currentClass = $class ;
$result [ $class ] = $pageTypeName ;
2011-03-18 04:01:06 +01:00
2014-12-01 12:22:48 +01:00
// If we're in translation mode, the link between the translated pagetype title and the actual classname
// might not be obvious, so we add it in parantheses. Example: class "RedirectorPage" has the title
// "Weiterleitung" in German, so it shows up as "Weiterleitung (RedirectorPage)"
2011-09-19 14:40:01 +02:00
if ( i18n :: get_lang_from_locale ( i18n :: get_locale ()) != 'en' ) {
2011-03-18 04:01:06 +01:00
$result [ $class ] = $result [ $class ] . " ( { $class } ) " ;
}
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
// sort alphabetically, and put current on top
asort ( $result );
if ( $currentClass ) {
$currentPageTypeName = $result [ $currentClass ];
unset ( $result [ $currentClass ]);
$result = array_reverse ( $result );
$result [ $currentClass ] = $currentPageTypeName ;
$result = array_reverse ( $result );
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
return $result ;
}
/**
2014-12-01 12:22:48 +01:00
* Returns an array of the class names of classes that are allowed to be children of this class .
2011-03-18 04:01:06 +01:00
*
2014-12-01 12:22:48 +01:00
* @ return string []
2011-03-18 04:01:06 +01:00
*/
2012-09-19 12:07:46 +02:00
public function allowedChildren () {
2011-10-06 16:47:59 +02:00
$allowedChildren = array ();
2011-03-18 04:01:06 +01:00
$candidates = $this -> stat ( 'allowed_children' );
if ( $candidates && $candidates != " none " && $candidates != " SiteTree_root " ) {
foreach ( $candidates as $candidate ) {
2014-12-01 12:22:48 +01:00
// If a classname is prefixed by "*", such as "*Page", then only that class is allowed - no subclasses.
// Otherwise, the class and all its subclasses are allowed.
2011-03-18 04:01:06 +01:00
if ( substr ( $candidate , 0 , 1 ) == '*' ) {
$allowedChildren [] = substr ( $candidate , 1 );
} else {
$subclasses = ClassInfo :: subclassesFor ( $candidate );
foreach ( $subclasses as $subclass ) {
2016-07-22 10:45:14 +02:00
if ( $subclass == 'SiteTree_root' || singleton ( $subclass ) instanceof HiddenClass ) {
continue ;
}
$allowedChildren [] = $subclass ;
2011-03-18 04:01:06 +01:00
}
}
}
}
2016-03-08 21:50:55 +01:00
2011-10-06 16:47:59 +02:00
return $allowedChildren ;
2011-03-18 04:01:06 +01:00
}
/**
* Returns the class name of the default class for children of this page .
*
* @ return string
*/
2012-09-19 12:07:46 +02:00
public function defaultChild () {
2011-03-18 04:01:06 +01:00
$default = $this -> stat ( 'default_child' );
$allowed = $this -> allowedChildren ();
if ( $allowed ) {
2016-08-10 06:08:39 +02:00
if ( ! $default || ! in_array ( $default , $allowed )) {
2011-03-18 04:01:06 +01:00
$default = reset ( $allowed );
2016-08-10 06:08:39 +02:00
}
2011-03-18 04:01:06 +01:00
return $default ;
}
2016-08-10 06:08:39 +02:00
return null ;
2011-03-18 04:01:06 +01:00
}
/**
2014-12-01 12:22:48 +01:00
* Returns the class name of the default class for the parent of this page .
2011-03-18 04:01:06 +01:00
*
* @ return string
*/
2012-09-19 12:07:46 +02:00
public function defaultParent () {
2011-03-18 04:01:06 +01:00
return $this -> stat ( 'default_parent' );
}
/**
2014-12-01 12:22:48 +01:00
* Get the title for use in menus for this page . If the MenuTitle field is set it returns that , else it returns the
* Title field .
2011-03-18 04:01:06 +01:00
*
* @ return string
*/
2012-09-19 12:07:46 +02:00
public function getMenuTitle (){
2011-03-18 04:01:06 +01:00
if ( $value = $this -> getField ( " MenuTitle " )) {
return $value ;
} else {
return $this -> getField ( " Title " );
}
}
/**
* Set the menu title for this page .
*
* @ param string $value
*/
2012-09-19 12:07:46 +02:00
public function setMenuTitle ( $value ) {
2011-03-18 04:01:06 +01:00
if ( $value == $this -> getField ( " Title " )) {
$this -> setField ( " MenuTitle " , null );
} else {
$this -> setField ( " MenuTitle " , $value );
}
}
2016-03-08 21:50:55 +01:00
2012-03-29 00:47:28 +02:00
/**
2014-12-01 12:22:48 +01:00
* A flag provides the user with additional data about the current page status , for example a " removed from draft "
* status . Each page can have more than one status flag . Returns a map of a unique key to a ( localized ) title for
* the flag . The unique key can be reused as a CSS class . Use the 'updateStatusFlags' extension point to customize
* the flags .
2016-01-06 00:42:07 +01:00
*
* Example ( simple ) :
2014-12-01 12:22:48 +01:00
* " deletedonlive " => " Deleted "
2016-01-06 00:42:07 +01:00
*
* Example ( with optional title attribute ) :
2014-12-01 12:22:48 +01:00
* " deletedonlive " => array ( 'text' => " Deleted " , 'title' => 'This page has been deleted' )
2012-06-12 15:55:05 +02:00
*
2014-12-01 12:22:48 +01:00
* @ param bool $cached Whether to serve the fields from cache ; false regenerate them
2012-03-29 00:47:28 +02:00
* @ return array
*/
2012-09-19 12:07:46 +02:00
public function getStatusFlags ( $cached = true ) {
2012-06-12 15:55:05 +02:00
if ( ! $this -> _cache_statusFlags || ! $cached ) {
$flags = array ();
2016-10-05 03:08:34 +02:00
if ( $this -> isOnLiveOnly ()) {
$flags [ 'removedfromdraft' ] = array (
'text' => _t ( 'SiteTree.ONLIVEONLYSHORT' , 'On live only' ),
'title' => _t ( 'SiteTree.ONLIVEONLYSHORTHELP' , 'Page is published, but has been deleted from draft' ),
);
} elseif ( $this -> isArchived ()) {
$flags [ 'archived' ] = array (
'text' => _t ( 'SiteTree.ARCHIVEDPAGESHORT' , 'Archived' ),
'title' => _t ( 'SiteTree.ARCHIVEDPAGEHELP' , 'Page is removed from draft and live' ),
);
} else if ( $this -> isOnDraftOnly ()) {
2012-06-12 15:55:05 +02:00
$flags [ 'addedtodraft' ] = array (
'text' => _t ( 'SiteTree.ADDEDTODRAFTSHORT' , 'Draft' ),
'title' => _t ( 'SiteTree.ADDEDTODRAFTHELP' , " Page has not been published yet " )
2012-04-30 14:56:42 +02:00
);
2016-10-05 03:08:34 +02:00
} else if ( $this -> isModifiedOnDraft ()) {
2012-06-12 15:55:05 +02:00
$flags [ 'modified' ] = array (
'text' => _t ( 'SiteTree.MODIFIEDONDRAFTSHORT' , 'Modified' ),
'title' => _t ( 'SiteTree.MODIFIEDONDRAFTHELP' , 'Page has unpublished changes' ),
2012-04-30 14:56:42 +02:00
);
2012-03-29 00:47:28 +02:00
}
2012-06-12 15:55:05 +02:00
$this -> extend ( 'updateStatusFlags' , $flags );
$this -> _cache_statusFlags = $flags ;
}
2016-03-08 21:50:55 +01:00
2012-06-12 15:55:05 +02:00
return $this -> _cache_statusFlags ;
2012-03-29 00:47:28 +02:00
}
2011-03-18 04:01:06 +01:00
/**
2014-12-01 12:22:48 +01:00
* getTreeTitle will return three < span > html DOM elements , an empty < span > with the class ' jstree - pageicon ' in
* front , following by a < span > wrapping around its MenutTitle , then following by a < span > indicating its
* publication status .
2011-03-18 04:01:06 +01:00
*
2014-12-01 12:22:48 +01:00
* @ return string An HTML string ready to be directly used in a template
2011-03-18 04:01:06 +01:00
*/
2012-09-19 12:07:46 +02:00
public function getTreeTitle () {
2015-03-11 06:54:08 +01:00
// Build the list of candidate children
$children = array ();
$candidates = static :: page_type_classes ();
foreach ( $this -> allowedChildren () as $childClass ) {
if ( ! in_array ( $childClass , $candidates )) continue ;
$child = singleton ( $childClass );
if ( $child -> canCreate ( null , array ( 'Parent' => $this ))) {
$children [ $childClass ] = $child -> i18n_singular_name ();
}
}
2012-03-29 00:47:28 +02:00
$flags = $this -> getStatusFlags ();
$treeTitle = sprintf (
2015-03-11 06:54:08 +01:00
" <span class= \" jstree-pageicon \" ></span><span class= \" item \" data-allowedchildren= \" %s \" >%s</span> " ,
Convert :: raw2att ( Convert :: raw2json ( $children )),
2012-03-29 00:47:28 +02:00
Convert :: raw2xml ( str_replace ( array ( " \n " , " \r " ), " " , $this -> MenuTitle ))
);
2012-04-30 14:56:42 +02:00
foreach ( $flags as $class => $data ) {
if ( is_string ( $data )) $data = array ( 'text' => $data );
2012-03-29 00:47:28 +02:00
$treeTitle .= sprintf (
2012-04-30 14:56:42 +02:00
" <span class= \" badge %s \" %s>%s</span> " ,
2013-05-26 02:31:15 +02:00
'status-' . Convert :: raw2xml ( $class ),
2012-04-30 14:56:42 +02:00
( isset ( $data [ 'title' ])) ? sprintf ( ' title="%s"' , Convert :: raw2xml ( $data [ 'title' ])) : '' ,
Convert :: raw2xml ( $data [ 'text' ])
2012-03-29 00:47:28 +02:00
);
2011-03-18 04:01:06 +01:00
}
2016-03-08 21:50:55 +01:00
2012-03-29 00:47:28 +02:00
return $treeTitle ;
2011-03-18 04:01:06 +01:00
}
/**
2014-12-01 12:22:48 +01:00
* Returns the page in the current page stack of the given level . Level ( 1 ) will return the main menu item that
* we ' re currently inside , etc .
*
* @ param int $level
* @ return SiteTree
2011-03-18 04:01:06 +01:00
*/
public function Level ( $level ) {
$parent = $this ;
$stack = array ( $parent );
2016-08-10 06:08:39 +02:00
while (( $parent = $parent -> Parent ()) && $parent -> exists ()) {
2011-03-18 04:01:06 +01:00
array_unshift ( $stack , $parent );
}
return isset ( $stack [ $level - 1 ]) ? $stack [ $level - 1 ] : null ;
}
2014-07-20 01:46:15 +02:00
2015-08-03 04:52:10 +02:00
/**
* Gets the depth of this page in the sitetree , where 1 is the root level
*
* @ return int
*/
public function getPageLevel () {
if ( $this -> ParentID ) {
return 1 + $this -> Parent () -> getPageLevel ();
}
return 1 ;
}
2016-09-08 16:46:48 +02:00
/**
2016-09-23 06:00:13 +02:00
* Find the controller name by our convention of { $ModelClass } _Controller
*
2016-09-08 16:46:48 +02:00
* @ return string
*/
public function getControllerName () {
2016-09-23 06:00:13 +02:00
//default controller for SiteTree objects
$controller = ContentController :: class ;
//go through the ancestry for this class looking for
2016-09-28 00:39:28 +02:00
$ancestry = ClassInfo :: ancestry ( static :: class );
2016-09-23 06:00:13 +02:00
// loop over the array going from the deepest descendant (ie: the current class) to SiteTree
while ( $class = array_pop ( $ancestry )) {
//we don't need to go any deeper than the SiteTree class
if ( $class == SiteTree :: class ) {
break ;
}
//if we have a class of "{$ClassName}_Controller" then we found our controller
if ( class_exists ( $candidate = sprintf ( '%s_Controller' , $class ))) {
$controller = $candidate ;
break ;
2016-09-08 16:46:48 +02:00
}
}
return $controller ;
}
2011-03-18 04:01:06 +01:00
/**
2014-12-01 12:22:48 +01:00
* Return the CSS classes to apply to this node in the CMS tree .
2011-03-18 04:01:06 +01:00
*
2014-07-20 01:46:15 +02:00
* @ param string $numChildrenMethod
2011-03-18 04:01:06 +01:00
* @ return string
*/
2014-07-20 01:46:15 +02:00
public function CMSTreeClasses ( $numChildrenMethod = " numChildren " ) {
2016-09-28 00:39:28 +02:00
$classes = sprintf ( 'class-%s' , static :: class );
2013-05-30 15:50:23 +02:00
if ( $this -> HasBrokenFile || $this -> HasBrokenLink ) {
2011-03-18 04:01:06 +01:00
$classes .= " BrokenLink " ;
2013-05-30 15:50:23 +02:00
}
2011-03-18 04:01:06 +01:00
2013-05-30 15:50:23 +02:00
if ( ! $this -> canAddChildren ()) {
2011-03-18 04:01:06 +01:00
$classes .= " nochildren " ;
2013-05-30 15:50:23 +02:00
}
2011-03-18 04:01:06 +01:00
2013-05-30 15:50:23 +02:00
if ( ! $this -> canEdit () && ! $this -> canAddChildren ()) {
if ( ! $this -> canView ()) {
2013-06-21 00:45:33 +02:00
$classes .= " disabled " ;
2013-05-30 15:50:23 +02:00
} else {
$classes .= " edit-disabled " ;
}
}
2011-03-18 04:01:06 +01:00
2013-05-30 15:50:23 +02:00
if ( ! $this -> ShowInMenus ) {
2011-03-18 04:01:06 +01:00
$classes .= " notinmenu " ;
2013-05-30 15:50:23 +02:00
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
//TODO: Add integration
/*
if ( $this -> hasExtension ( 'Translatable' ) && $controller -> Locale != Translatable :: default_locale () && ! $this -> isTranslation ())
$classes .= " untranslated " ;
*/
2014-07-20 01:46:15 +02:00
$classes .= $this -> markingClasses ( $numChildrenMethod );
2011-03-18 04:01:06 +01:00
return $classes ;
}
2016-01-26 06:38:42 +01:00
2011-03-18 04:01:06 +01:00
/**
2014-12-01 12:22:48 +01:00
* Stops extendCMSFields () being called on getCMSFields () . This is useful when you need access to fields added by
* subclasses of SiteTree in a extension . Call before calling parent :: getCMSFields (), and reenable afterwards .
2011-03-18 04:01:06 +01:00
*/
2012-09-19 12:07:46 +02:00
static public function disableCMSFieldsExtensions () {
2011-03-18 04:01:06 +01:00
self :: $runCMSFieldsExtensions = false ;
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
2014-12-01 12:22:48 +01:00
* Reenables extendCMSFields () being called on getCMSFields () after it has been disabled by
* disableCMSFieldsExtensions () .
2011-03-18 04:01:06 +01:00
*/
2012-09-19 12:07:46 +02:00
static public function enableCMSFieldsExtensions () {
2011-03-18 04:01:06 +01:00
self :: $runCMSFieldsExtensions = true ;
}
2012-09-19 12:07:46 +02:00
public function providePermissions () {
2011-03-18 04:01:06 +01:00
return array (
'SITETREE_GRANT_ACCESS' => array (
'name' => _t ( 'SiteTree.PERMISSION_GRANTACCESS_DESCRIPTION' , 'Manage access rights for content' ),
'help' => _t ( 'SiteTree.PERMISSION_GRANTACCESS_HELP' , 'Allow setting of page-specific access restrictions in the "Pages" section.' ),
'category' => _t ( 'Permissions.PERMISSIONS_CATEGORY' , 'Roles and access permissions' ),
'sort' => 100
),
'SITETREE_VIEW_ALL' => array (
'name' => _t ( 'SiteTree.VIEW_ALL_DESCRIPTION' , 'View any page' ),
'category' => _t ( 'Permissions.CONTENT_CATEGORY' , 'Content permissions' ),
'sort' => - 100 ,
2013-04-30 07:58:30 +02:00
'help' => _t ( 'SiteTree.VIEW_ALL_HELP' , 'Ability to view any page on the site, regardless of the settings on the Access tab. Requires the "Access to \'Pages\' section" permission' )
2011-03-18 04:01:06 +01:00
),
'SITETREE_EDIT_ALL' => array (
'name' => _t ( 'SiteTree.EDIT_ALL_DESCRIPTION' , 'Edit any page' ),
'category' => _t ( 'Permissions.CONTENT_CATEGORY' , 'Content permissions' ),
'sort' => - 50 ,
2012-03-05 16:07:53 +01:00
'help' => _t ( 'SiteTree.EDIT_ALL_HELP' , 'Ability to edit any page on the site, regardless of the settings on the Access tab. Requires the "Access to \'Pages\' section" permission' )
2011-03-18 04:01:06 +01:00
),
'SITETREE_REORGANISE' => array (
'name' => _t ( 'SiteTree.REORGANISE_DESCRIPTION' , 'Change site structure' ),
'category' => _t ( 'Permissions.CONTENT_CATEGORY' , 'Content permissions' ),
'help' => _t ( 'SiteTree.REORGANISE_HELP' , 'Rearrange pages in the site tree through drag&drop.' ),
'sort' => 100
),
'VIEW_DRAFT_CONTENT' => array (
'name' => _t ( 'SiteTree.VIEW_DRAFT_CONTENT' , 'View draft content' ),
'category' => _t ( 'Permissions.CONTENT_CATEGORY' , 'Content permissions' ),
'help' => _t ( 'SiteTree.VIEW_DRAFT_CONTENT_HELP' , 'Applies to viewing pages outside of the CMS in draft mode. Useful for external collaborators without CMS access.' ),
'sort' => 100
)
);
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
2014-12-01 12:22:48 +01:00
* Return the translated Singular name .
2016-01-06 00:42:07 +01:00
*
2014-12-01 12:22:48 +01:00
* @ return string
2011-03-18 04:01:06 +01:00
*/
2012-09-19 12:07:46 +02:00
public function i18n_singular_name () {
2012-08-26 23:05:13 +02:00
// Convert 'Page' to 'SiteTree' for correct localization lookups
2016-08-10 06:08:39 +02:00
/** @skipUpgrade */
// @todo When we namespace translations, change 'SiteTree' to FQN of the class
2016-09-28 00:39:28 +02:00
$class = ( static :: class == 'Page' || static :: class === self :: class )
2016-08-10 06:08:39 +02:00
? 'SiteTree'
2016-09-28 00:39:28 +02:00
: static :: class ;
2012-08-26 23:05:13 +02:00
return _t ( $class . '.SINGULARNAME' , $this -> singular_name ());
2011-03-18 04:01:06 +01:00
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:01:06 +01:00
/**
2014-12-01 12:22:48 +01:00
* Overloaded to also provide entities for 'Page' class which is usually located in custom code , hence textcollector
* picks it up for the wrong folder .
*
* @ return array
2011-03-18 04:01:06 +01:00
*/
2012-09-19 12:07:46 +02:00
public function provideI18nEntities () {
2011-03-18 04:01:06 +01:00
$entities = parent :: provideI18nEntities ();
2016-03-08 21:50:55 +01:00
2012-08-20 22:25:18 +02:00
if ( isset ( $entities [ 'Page.SINGULARNAME' ])) $entities [ 'Page.SINGULARNAME' ][ 3 ] = CMS_DIR ;
2016-03-08 21:50:55 +01:00
if ( isset ( $entities [ 'Page.PLURALNAME' ])) $entities [ 'Page.PLURALNAME' ][ 3 ] = CMS_DIR ;
2011-03-18 04:01:06 +01:00
2016-09-28 00:39:28 +02:00
$entities [ static :: class . '.DESCRIPTION' ] = array (
2012-08-06 14:28:04 +02:00
$this -> stat ( 'description' ),
'Description of the page type (shown in the "add page" dialog)'
);
2011-04-24 01:03:51 +02:00
2012-08-20 22:25:18 +02:00
$entities [ 'SiteTree.SINGULARNAME' ][ 0 ] = 'Page' ;
$entities [ 'SiteTree.PLURALNAME' ][ 0 ] = 'Pages' ;
2011-03-18 04:01:06 +01:00
return $entities ;
}
2014-12-01 12:22:48 +01:00
/**
* Returns 'root' if the current page has no parent , or 'subpage' otherwise
*
* @ return string
*/
2012-09-19 12:07:46 +02:00
public function getParentType () {
2011-03-18 04:01:06 +01:00
return $this -> ParentID == 0 ? 'root' : 'subpage' ;
}
2014-12-01 12:22:48 +01:00
/**
* Clear the permissions cache for SiteTree
*/
public static function reset () {
2011-03-18 04:01:06 +01:00
self :: $cache_permissions = array ();
}
2016-03-08 21:50:55 +01:00
2012-09-19 12:07:46 +02:00
static public function on_db_reset () {
2011-10-07 10:36:56 +02:00
self :: $cache_permissions = array ();
}
2011-03-18 04:01:06 +01:00
2012-09-25 05:31:42 +02:00
}