2007-07-19 12:40:28 +02:00
< ? php
/**
* Basic data - object representing all pages within the site tree .
* This data - object takes care of the heirachy . All page types that live within the heirachy
2007-09-14 21:17:37 +02:00
* should inherit from this .
*
2007-07-19 12:40:28 +02:00
* In addition , it contains a number of static methods for querying the site tree .
2008-02-25 03:10:37 +01:00
* @ package cms
2007-07-19 12:40:28 +02:00
*/
2009-01-05 07:19:48 +01:00
class SiteTree extends DataObject implements PermissionProvider , i18nEntityProvider {
2007-09-15 02:03:12 +02: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 .
*
* @ var array
*/
static $allowed_children = array ( " SiteTree " );
/**
* The default child class for this page .
*
* @ var string
*/
static $default_child = " Page " ;
/**
* The default parent class for this page .
*
* @ var string
*/
static $default_parent = null ;
/**
* Controls whether a page can be in the root of the site tree .
*
* @ var bool
*/
static $can_be_root = true ;
/**
* List of permission codes a user can have to allow a user to create a
* page of this type .
*
* @ var array
*/
static $need_permission = null ;
/**
* 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 .
*
* @ var string
*/
static $hide_ancestor = null ;
static $db = array (
" URLSegment " => " Varchar(255) " ,
" Title " => " Varchar(255) " ,
" MenuTitle " => " Varchar(100) " ,
" Content " => " HTMLText " ,
" MetaTitle " => " Varchar(255) " ,
2009-05-22 02:53:53 +02:00
" MetaDescription " => " Text " ,
2007-09-15 02:03:12 +02:00
" MetaKeywords " => " Varchar(255) " ,
2007-12-02 22:19:26 +01:00
" ExtraMeta " => " HTMLText " ,
2007-09-15 02:03:12 +02:00
" ShowInMenus " => " Boolean " ,
" ShowInSearch " => " Boolean " ,
" HomepageForDomain " => " Varchar(100) " ,
" ProvideComments " => " Boolean " ,
" Sort " => " Int " ,
" HasBrokenFile " => " Boolean " ,
" HasBrokenLink " => " Boolean " ,
" ReportClass " => " Varchar " ,
2008-11-18 02:48:37 +01:00
" CanViewType " => " Enum('Anyone, LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit') " ,
" CanEditType " => " Enum('LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit') " ,
2008-11-05 00:17:14 +01:00
2008-02-25 03:10:37 +01:00
// Simple task tracking
" ToDo " => " Text " ,
2007-09-15 02:03:12 +02:00
);
2008-11-24 23:04:32 +01:00
static $indexes = array (
" URLSegment " => true ,
2007-09-15 02:03:12 +02:00
);
static $has_many = array (
" Comments " => " PageComment "
);
static $many_many = array (
" LinkTracking " => " SiteTree " ,
2008-11-03 15:52:35 +01:00
" ImageTracking " => " File " ,
" ViewerGroups " => " Group " ,
" EditorGroups " => " Group " ,
2007-09-15 02:03:12 +02:00
);
static $belongs_many_many = array (
" BackLinkTracking " => " SiteTree "
);
static $many_many_extraFields = array (
" LinkTracking " => array ( " FieldName " => " Varchar " ),
" ImageTracking " => array ( " FieldName " => " Varchar " )
);
static $casting = array (
" Breadcrumbs " => " HTMLText " ,
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
" LastEdited " => " SS_Datetime " ,
" Created " => " SS_Datetime " ,
2007-09-15 02:03:12 +02:00
);
static $defaults = array (
" ShowInMenus " => 1 ,
" ShowInSearch " => 1 ,
2008-11-18 02:48:37 +01:00
" CanViewType " => " Inherit " ,
" CanEditType " => " Inherit "
2007-09-15 02:03:12 +02:00
);
static $versioning = array (
" Stage " , " Live "
);
2010-02-04 20:45:14 +01:00
static $default_sort = " \" Sort \" " ;
2007-09-15 02:03:12 +02:00
/**
* If this is false , the class cannot be created in the CMS .
* @ var boolean
*/
static $can_create = true ;
2010-04-12 05:38:58 +02:00
2007-09-15 02:03:12 +02:00
/**
* Icon to use in the CMS
*
* This should be the base filename . The suffixes - file . gif ,
* - openfolder . gif and - closedfolder . gif will be appended to the base name
* that you provide there .
* If you prefer , you can pass an array :
2009-11-21 03:29:59 +01:00
* array ( " sapphire \ javascript \t ree \ images \ page " , $option ) .
2007-09-15 02:03:12 +02:00
* $option can be either " file " or " folder " to force the icon to always
* be a file or folder , regardless of whether the page has children or not
*
* @ var string | array
*/
2009-11-21 03:29:59 +01:00
static $icon = array ( " sapphire/javascript/tree/images/page " , " file " );
2007-09-15 02:03:12 +02:00
static $extensions = array (
" Hierarchy " ,
Merged, debugged and enhanced Translatable patches from branches/translatable at r64523, r64523, 64523, thanks wakeless!
API CHANGE Changed Translatable schema from auxilliary tables (SiteTree_lang, SiteTree_lang_Live) to automatically filtered records on the original table (SiteTree, SiteTree_Live), using $Lang and $OriginalID properties. Incompatible update to old schema, migration script is in the works.
API CHANGE Removed Translatable::get_one(), Translatable::write()
ENHANCEMENT Simplified Translatable tree generation by using getSiteTreeFor() in CMSMain->createtranslation()
ENHANCEMENT Added AllChildrenIncludingDeleted(), augmentNumChildrenCountQuery(), augmentAllChildrenIncludingDeleted(), augmentStageChildren() to Translatable class to allow for more stable tree generation.
ENHANCEMENT Moved definition of Translatable schema from augmentDatabase() to Translatable->extraStatics()
ENHANCEMENT Changes to the CMS language selection refresh the whole admin interface instead of the tree only. This way we can add a URL parameter ?lang=<lang> to /admin, which makes the specific language bookmarkable and reloadable. Changes to LangSelector.js
ENHANCEMENT Added fallback to ModelAsController->getNestedController() to fetch page with matching URLSegment but different language in case no page is found in the current language.
ENHANCEMENT Added helper methods to Translatable: getTranslation(), hasTranslation(), isTranslation(), findOriginalIDs()
ENHANCEMENT Getters and setters for Translatable->getOriginalPage() etc.
ENHANCEMENT Hooking Translatable into ModelAsController and ContentController initialization in order to call choose_site_lang()
ENHANCEMENT Simplified Translatable->augmentSQL(), augmentWrite() by not using auxilliary tables
ENHANCEMENT Showing clickable links for Translations in Translatable->updateCMSFields()
BUGFIX Modifying Hierarchy/SiteTree Children getters to accept optional "context" which can be used to set a language explicitly through the $Lang property, rather than implicitly reyling on the static Translatable::current_lang()
BUGFIX Fixed TranslatableTest to work with new datamodel
BUGFIX Temporarily disabled cookie/session selection in Translatable::choose_site_lang() until we have a good test suite for the side effects.
MINOR Added "untranslated" CSS styles to tree nodes and marking them as inactive/grey
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@69959 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-01-10 13:15:30 +01:00
" Versioned('Stage', 'Live') " ,
2008-02-25 03:10:37 +01:00
);
/**
* Delimit breadcrumb - links generated by BreadCrumbs ()
*
* @ var string
*/
public static $breadcrumbs_delimiter = " » " ;
2008-08-11 01:35:11 +02:00
2010-04-14 05:50:21 +02:00
/**
* Whether or not to write the homepage map for static publisher
*/
public static $write_homepage_map = true ;
2008-08-11 01:35:11 +02:00
static $searchable_fields = array (
'Title' ,
2008-08-11 04:25:44 +02:00
'Content' ,
2008-08-11 01:35:11 +02:00
);
2008-11-10 04:51:35 +01:00
2009-10-11 02:06:54 +02:00
/**
* @ see SiteTree :: nested_urls ()
*/
private static $nested_urls = false ;
2008-11-10 04:51:35 +01:00
/**
* This controls whether of not extendCMSFields () is called by getCMSFields .
*/
private static $runCMSFieldsExtensions = true ;
2009-07-31 07:43:41 +02:00
/**
* Cache for canView / Edit / Publish / Delete permissions
*/
2010-04-13 04:29:03 +02:00
public static $cache_permissions = array ();
2009-10-11 02:06:54 +02:00
2010-04-14 05:56:22 +02:00
/**
* @ see SiteTree :: enforce_strict_hierarchy ()
*/
private static $enforce_strict_hierarchy = true ;
2010-04-14 05:57:45 +02:00
public static function set_enforce_strict_hierarchy ( $to ) {
self :: $enforce_strict_hierarchy = $to ;
}
public static function get_enforce_strict_hierarchy () {
2010-04-14 05:56:22 +02:00
return self :: $enforce_strict_hierarchy ;
}
2009-10-11 02:06:54 +02:00
/**
* Returns TRUE if nested URLs ( e . g . page / sub - page / ) are currently enabled on this site .
*
* @ return bool
*/
public static function nested_urls () {
return self :: $nested_urls ;
}
public static function enable_nested_urls () {
self :: $nested_urls = true ;
}
public static function disable_nested_urls () {
self :: $nested_urls = false ;
}
2009-10-11 02:07:08 +02:00
/**
* Fetches the { @ link SiteTree } object that maps to a link .
*
* If you have enabled { @ link SiteTree :: nested_urls ()} on this site , then you can use a nested link such as
* " about-us/staff/ " , and this function will traverse down the URL chain and grab the appropriate link .
*
* Note that if no model can be found , this method will fall over to a decorated alternateGetByLink method provided
* by a decorator attached to { @ link SiteTree }
*
* @ param string $link
* @ param bool $cache
* @ return SiteTree
*/
public static function get_by_link ( $link , $cache = true ) {
if ( trim ( $link , '/' )) {
$link = trim ( Director :: makeRelative ( $link ), '/' );
} else {
2009-10-11 02:07:17 +02:00
$link = RootURLController :: get_homepage_link ();
2009-10-11 02:07:08 +02:00
}
$parts = Convert :: raw2sql ( preg_split ( '|/+|' , $link ));
// Grab the initial root level page to traverse down from.
$URLSegment = array_shift ( $parts );
$sitetree = DataObject :: get_one (
'SiteTree' , " \" URLSegment \" = ' $URLSegment ' " . ( self :: nested_urls () ? ' AND "ParentID" = 0' : '' ), $cache
);
/// Fall back on a unique URLSegment for b/c.
if ( ! $sitetree && self :: nested_urls () && $pages = DataObject :: get ( 'SiteTree' , " \" URLSegment \" = ' $URLSegment ' " )) {
return ( $pages -> Count () == 1 ) ? $pages -> First () : null ;
}
// Attempt to grab an alternative page from decorators.
if ( ! $sitetree ) {
2009-10-11 02:07:22 +02:00
$parentID = self :: nested_urls () ? 0 : null ;
if ( $alternatives = singleton ( 'SiteTree' ) -> extend ( 'alternateGetByLink' , $URLSegment , $parentID )) {
foreach ( $alternatives as $alternative ) if ( $alternative ) $sitetree = $alternative ;
2009-10-11 02:07:08 +02:00
}
2009-10-11 02:07:22 +02:00
if ( ! $sitetree ) return false ;
2009-10-11 02:07:08 +02:00
}
// Check if we have any more URL parts to parse.
if ( ! self :: nested_urls () || ! count ( $parts )) return $sitetree ;
// Traverse down the remaining URL segments and grab the relevant SiteTree objects.
foreach ( $parts as $segment ) {
$next = DataObject :: get_one (
'SiteTree' , " \" URLSegment \" = ' $segment ' AND \" ParentID \" = $sitetree->ID " , $cache
);
if ( ! $next ) {
2009-10-11 02:07:22 +02:00
$parentID = ( int ) $sitetree -> ID ;
if ( $alternatives = singleton ( 'SiteTree' ) -> extend ( 'alternateGetByLink' , $segment , $parentID )) {
foreach ( $alternatives as $alternative ) if ( $alternative ) $next = $alternative ;
2009-10-11 02:07:08 +02:00
}
2009-10-11 02:07:22 +02:00
if ( ! $next ) return false ;
2009-10-11 02:07:08 +02:00
}
$sitetree -> destroy ();
$sitetree = $next ;
}
return $sitetree ;
}
2009-02-02 00:49:53 +01:00
/**
* Return a subclass map of SiteTree
* that shouldn ' t be hidden through
* { @ link SiteTree :: $hide_ancestor }
*
* @ return array
*/
public static function page_type_classes () {
$classes = ClassInfo :: getValidSubClasses ();
2010-10-13 03:35:19 +02:00
$baseClassIndex = array_search ( 'SiteTree' , $classes );
if ( $baseClassIndex !== FALSE ) unset ( $classes [ $baseClassIndex ]);
2010-10-13 03:28:02 +02:00
2009-02-02 00:49:53 +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 ;
}
}
// If any of the descendents don't want any of the elders to show up, cruelly render the elders surplus to requirements.
if ( $kill_ancestors ) {
$kill_ancestors = array_unique ( $kill_ancestors );
foreach ( $kill_ancestors as $mark ) {
// unset from $classes
$idx = array_search ( $mark , $classes );
unset ( $classes [ $idx ]);
}
}
2010-10-13 03:28:02 +02:00
2009-02-02 00:49:53 +01:00
return $classes ;
}
2009-10-11 02:07:08 +02:00
/**
* Replace a " [sitetree_link id=n] " shortcode with a link to the page with the corresponding ID .
*
* @ return string
*/
public static function link_shortcode_handler ( $arguments , $content = null , $parser = null ) {
if ( ! isset ( $arguments [ 'id' ]) || ! is_numeric ( $arguments [ 'id' ])) return ;
if (
! ( $page = DataObject :: get_by_id ( 'SiteTree' , $arguments [ 'id' ])) // Get the current page by ID.
&& ! ( $page = Versioned :: get_latest_version ( 'SiteTree' , $arguments [ 'id' ])) // Attempt link to old version.
&& ! ( $page = DataObject :: get_one ( 'ErrorPage' , '"ErrorCode" = \'404\'' )) // Link to 404 page directly.
) {
return ; // There were no suitable matches at all.
}
if ( $content ) {
return sprintf ( '<a href="%s">%s</a>' , $page -> Link (), $parser -> parse ( $content ));
} else {
return $page -> Link ();
2009-10-11 02:07:04 +02:00
}
}
2007-07-19 12:40:28 +02:00
/**
2009-10-11 02:07:00 +02:00
* Return the link for this { @ link SiteTree } object , with the { @ link Director :: baseURL ()} included .
2007-09-15 02:03:12 +02:00
*
2009-10-11 02:07:00 +02:00
* @ param string $action
* @ return string
2007-07-19 12:40:28 +02:00
*/
public function Link ( $action = null ) {
2009-10-11 02:07:00 +02:00
return Controller :: join_links ( Director :: baseURL (), $this -> RelativeLink ( $action ));
2007-07-19 12:40:28 +02:00
}
2007-09-05 08:42:26 +02:00
/**
2009-10-11 02:07:00 +02:00
* Get the absolute URL for this page , including protocol and host .
*
* @ param string $action
* @ return string
2007-09-05 08:42:26 +02:00
*/
2009-10-11 02:07:00 +02:00
public function AbsoluteLink ( $action = null ) {
if ( $this -> hasMethod ( 'alternateAbsoluteLink' )) {
return $this -> alternateAbsoluteLink ( $action );
} else {
return Director :: absoluteURL ( $this -> Link ( $action ));
}
2007-09-05 08:42:26 +02:00
}
2009-10-11 02:07:00 +02:00
/**
* Return the link for this { @ link SiteTree } object relative to the SilverStripe root .
*
2009-10-11 02:07:17 +02:00
* By default , it this page is the current home page , and there is no action specified then this will return a link
* 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 ()
2009-10-11 02:07:00 +02:00
* @ param string $action
* @ return string
*/
public function RelativeLink ( $action = null ) {
if ( $this -> ParentID && self :: nested_urls ()) {
$base = $this -> Parent () -> RelativeLink ( $this -> URLSegment );
} else {
$base = $this -> URLSegment ;
}
2007-07-19 12:40:28 +02:00
2010-04-12 05:39:50 +02:00
// Unset base for homepage URLSegments in their default language.
// Homepages with action parameters or in different languages
2010-10-13 03:01:38 +02:00
// need to retain their URLSegment. We can only do this if the homepage
// is on the root level.
if ( ! $action && $base == RootURLController :: get_homepage_link () && ! $this -> ParentID ) {
2009-10-11 02:07:17 +02:00
$base = null ;
2010-04-12 05:39:50 +02:00
if ( $this -> hasExtension ( 'Translatable' ) && $this -> Locale != Translatable :: default_locale ()){
$base = $this -> URLSegment ;
}
2009-10-11 02:07:17 +02:00
}
if ( is_string ( $action )) {
$action = str_replace ( '&' , '&' , $action );
} elseif ( $action === true ) {
$action = null ;
}
return Controller :: join_links ( $base , '/' , $action );
2009-10-11 02:07:00 +02:00
}
2010-04-13 04:13:12 +02:00
/**
* Get the absolute URL for this page on the Live site .
*/
public function getAbsoluteLiveLink ( $includeStageEqualsLive = true ) {
$live = Versioned :: get_one_by_stage ( 'SiteTree' , 'Live' , '"SiteTree"."ID" = ' . $this -> ID );
if ( $live ) {
$link = $live -> AbsoluteLink ();
if ( $includeStageEqualsLive ) {
$link .= '?stage=Live' ;
}
return $link ;
}
}
2009-10-11 02:07:26 +02: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 ), '/' ));
}
2007-07-19 12:40:28 +02:00
/**
2009-10-11 02:07:11 +02:00
* Returns TRUE if this is the currently active page that is being used to handle a request .
2007-09-15 02:03:12 +02:00
*
2009-10-11 02:07:11 +02:00
* @ return bool
*/
public function isCurrent () {
return $this -> ID ? $this -> ID == Director :: get_current_page () -> ID : $this === Director :: get_current_page ();
}
/**
* Check if this page is in the currently active section ( e . g . it is either current or one of it ' s children is
* currently being viewed .
*
* @ return bool
*/
public function isSection () {
return $this -> isCurrent () || (
Director :: get_current_page () instanceof SiteTree && in_array ( $this -> ID , Director :: get_current_page () -> getAncestors () -> column ())
);
}
/**
* Return " link " or " current " depending on if this is the { @ link SiteTree :: isCurrent ()} current page .
*
* @ return string
2007-07-19 12:40:28 +02:00
*/
public function LinkOrCurrent () {
2009-10-11 02:07:11 +02:00
return $this -> isCurrent () ? 'current' : 'link' ;
2007-07-19 12:40:28 +02:00
}
2009-10-11 02:07:11 +02:00
2007-07-19 12:40:28 +02:00
/**
2009-10-11 02:07:11 +02:00
* Return " link " or " section " depending on if this is the { @ link SiteTree :: isSeciton ()} current section .
2007-09-15 02:03:12 +02:00
*
2009-10-11 02:07:11 +02:00
* @ return string
2007-07-19 12:40:28 +02:00
*/
public function LinkOrSection () {
2009-10-11 02:07:11 +02:00
return $this -> isSection () ? 'section' : 'link' ;
2007-07-19 12:40:28 +02:00
}
2009-10-11 02:07:11 +02:00
2007-07-19 12:40:28 +02:00
/**
2009-10-11 02:07:11 +02: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 .
2007-09-15 02:03:12 +02:00
*
2009-10-11 02:07:11 +02:00
* @ return string
2007-07-19 12:40:28 +02:00
*/
public function LinkingMode () {
2009-10-11 02:07:11 +02:00
if ( $this -> isCurrent ()) {
return 'current' ;
} elseif ( $this -> isSection ()) {
return 'section' ;
2007-07-19 12:40:28 +02:00
} else {
2009-10-11 02:07:11 +02:00
return 'link' ;
2007-07-19 12:40:28 +02:00
}
}
2009-10-11 02:07:11 +02:00
2007-07-19 12:40:28 +02:00
/**
* Check if this page is in the given current section .
2007-09-15 02:03:12 +02:00
*
2007-07-19 12:40:28 +02:00
* @ param string $sectionName Name of the section to check .
* @ return boolean True if we are in the given section .
*/
public function InSection ( $sectionName ) {
2009-10-11 02:07:10 +02:00
$page = Director :: get_current_page ();
2007-07-19 12:40:28 +02:00
while ( $page ) {
2007-09-15 02:03:12 +02:00
if ( $sectionName == $page -> URLSegment )
return true ;
2007-07-19 12:40:28 +02:00
$page = $page -> Parent ;
}
return false ;
}
2007-09-14 21:17:37 +02:00
2007-09-15 02:03:12 +02:00
2007-07-19 12:40:28 +02:00
/**
* Returns comments on this page . This will only show comments that
* have been marked as spam if " ?showspam=1 " is appended to the URL .
2007-09-15 02:03:12 +02:00
*
2007-07-19 12:40:28 +02:00
* @ return DataObjectSet Comments on this page .
*/
public function Comments () {
2009-03-12 22:48:58 +01:00
$spamfilter = isset ( $_GET [ 'showspam' ]) ? '' : " AND \" IsSpam \" =0 " ;
$unmoderatedfilter = Permission :: check ( 'ADMIN' ) ? '' : " AND \" NeedsModeration \" =0 " ;
2008-11-24 10:31:14 +01:00
$comments = DataObject :: get ( " PageComment " , " \" ParentID \" = ' " . Convert :: raw2sql ( $this -> ID ) . " ' $spamfilter $unmoderatedfilter " , " \" Created \" DESC " );
2007-07-19 12:40:28 +02:00
return $comments ? $comments : new DataObjectSet ();
}
2007-09-14 21:17:37 +02:00
2007-09-15 02:03:12 +02:00
2007-07-19 12:40:28 +02:00
/**
2007-09-15 02:03:12 +02:00
* Create a duplicate of this node . Doesn ' t affect joined data - create a
* custom overloading of this if you need such behaviour .
*
2007-07-19 12:40:28 +02:00
* @ return SiteTree The duplicated object .
*/
2007-08-16 08:32:49 +02:00
public function duplicate ( $doWrite = true ) {
2010-04-12 05:28:07 +02:00
2009-10-16 00:27:48 +02:00
$page = parent :: duplicate ( false );
$page -> Sort = 0 ;
2010-04-12 05:28:22 +02:00
$this -> extend ( 'onBeforeDuplicate' , $page );
2009-10-16 00:27:48 +02:00
if ( $doWrite ) {
$page -> write ();
2010-09-16 05:40:34 +02:00
$page = $this -> duplicateManyManyRelations ( $this , $page );
2009-10-16 00:27:48 +02:00
}
2010-04-12 05:28:07 +02:00
$this -> extend ( 'onAfterDuplicate' , $page );
2010-04-12 05:28:22 +02:00
2007-07-19 12:40:28 +02:00
return $page ;
}
2007-09-14 21:17:37 +02:00
2007-09-15 02:03:12 +02:00
2007-07-19 12:40:28 +02:00
/**
2007-09-15 02:03:12 +02:00
* Duplicates each child of this node recursively and returns the
* duplicate node .
*
2007-07-19 12:40:28 +02:00
* @ return SiteTree The duplicated object .
*/
public function duplicateWithChildren () {
$clone = $this -> duplicate ();
$children = $this -> AllChildren ();
if ( $children ) {
foreach ( $children as $child ) {
2007-09-15 02:03:12 +02:00
$childClone = method_exists ( $child , 'duplicateWithChildren' )
? $child -> duplicateWithChildren ()
: $child -> duplicate ();
2007-07-19 12:40:28 +02:00
$childClone -> ParentID = $clone -> ID ;
$childClone -> write ();
}
}
2007-09-14 21:17:37 +02:00
2007-07-19 12:40:28 +02:00
return $clone ;
}
2007-09-14 21:17:37 +02:00
2007-09-15 02:03:12 +02:00
2007-07-19 12:40:28 +02:00
/**
2007-09-15 02:03:12 +02:00
* Duplicate this node and its children as a child of the node with the
* given ID
*
2007-07-19 12:40:28 +02:00
* @ param int $id ID of the new node ' s new parent
*/
2007-09-15 02:03:12 +02:00
public function duplicateAsChild ( $id ) {
2007-07-19 12:40:28 +02:00
$newSiteTree = $this -> duplicate ();
$newSiteTree -> ParentID = $id ;
2009-10-16 00:27:48 +02:00
$newSiteTree -> Sort = 0 ;
2007-07-19 12:40:28 +02:00
$newSiteTree -> write ();
}
2008-08-12 04:51:33 +02:00
2007-07-19 12:40:28 +02:00
/**
2008-10-08 04:00:12 +02:00
* Return a breadcrumb trail to this page . Excludes " hidden " pages
* ( with ShowInMenus = 0 ) .
2007-09-15 02:03:12 +02:00
*
2007-07-19 12:40:28 +02:00
* @ param int $maxDepth The maximum depth to traverse .
* @ param boolean $unlinked Do not make page names links
* @ param string $stopAtPageType ClassName of a page to stop the upwards traversal .
2008-10-08 04:00:12 +02:00
* @ param boolean $showHidden Include pages marked with the attribute ShowInMenus = 0
2007-07-19 12:40:28 +02:00
* @ return string The breadcrumb trail .
*/
2008-10-08 04:00:12 +02:00
public function Breadcrumbs ( $maxDepth = 20 , $unlinked = false , $stopAtPageType = false , $showHidden = false ) {
2007-07-19 12:40:28 +02:00
$page = $this ;
$parts = array ();
$i = 0 ;
2008-10-08 04:00:12 +02:00
while (
$page
&& ( ! $maxDepth || sizeof ( $parts ) < $maxDepth )
2008-10-09 02:28:36 +02:00
&& ( ! $stopAtPageType || $page -> ClassName != $stopAtPageType )
2008-10-08 04:00:12 +02:00
) {
if ( $showHidden || $page -> ShowInMenus || ( $page -> ID == $this -> ID )) {
if ( $page -> URLSegment == 'home' ) $hasHome = true ;
if (( $page -> ID == $this -> ID ) || $unlinked ) {
$parts [] = Convert :: raw2xml ( $page -> Title );
} else {
$parts [] = ( " <a href= \" " . $page -> Link () . " \" > " . Convert :: raw2xml ( $page -> Title ) . " </a> " );
2007-07-19 12:40:28 +02:00
}
}
$page = $page -> Parent ;
}
2007-09-14 21:17:37 +02:00
2008-02-25 03:10:37 +01:00
return implode ( self :: $breadcrumbs_delimiter , array_reverse ( $parts ));
2007-07-19 12:40:28 +02:00
}
2007-09-14 21:17:37 +02:00
2007-07-19 12:40:28 +02:00
/**
* Make this page a child of another page .
2008-07-14 13:09:44 +02:00
*
* If the parent page does not exist , resolve it to a valid ID
* before updating this page ' s reference .
2007-09-15 02:03:12 +02:00
*
* @ param SiteTree | int $item Either the parent object , or the parent ID
2007-07-19 12:40:28 +02:00
*/
public function setParent ( $item ) {
if ( is_object ( $item )) {
2008-07-14 13:09:44 +02:00
if ( ! $item -> exists ()) $item -> write ();
2007-07-19 12:40:28 +02:00
$this -> setField ( " ParentID " , $item -> ID );
} else {
$this -> setField ( " ParentID " , $item );
}
}
2008-07-14 13:09:44 +02:00
/**
* Get the parent of this page .
*
* @ return SiteTree Parent of this page .
*/
public function getParent () {
if ( $this -> getField ( " ParentID " )) {
2008-11-24 10:31:14 +01:00
return DataObject :: get_one ( " SiteTree " , " \" SiteTree \" . \" ID \" = " . $this -> getField ( " ParentID " ));
2008-07-14 13:09:44 +02:00
}
}
2007-09-15 02:03:12 +02:00
2007-07-19 12:40:28 +02:00
/**
2007-09-15 02:03:12 +02:00
* Return a string of the form " parent - page " or
* " grandparent - parent - page " .
*
2007-07-19 12:40:28 +02:00
* @ param int $level The maximum amount of levels to traverse .
* @ param string $seperator Seperating string
* @ return string The resulting string
*/
function NestedTitle ( $level = 2 , $separator = " - " ) {
$item = $this ;
while ( $item && $level > 0 ) {
$parts [] = $item -> Title ;
$item = $item -> Parent ;
$level -- ;
}
return implode ( $separator , array_reverse ( $parts ));
}
2007-09-15 02:03:12 +02:00
/**
* This function should return true if the current user can add children
2008-11-07 13:18:35 +01:00
* to this page . It can be overloaded to customise the security model for an
2007-09-15 02:03:12 +02:00
* application .
*
2007-07-24 05:43:21 +02:00
* Returns true if the member is allowed to do the given action .
*
2008-11-07 13:18:35 +01:00
* @ uses DataObjectDecorator -> can ()
*
2010-04-12 03:45:52 +02:00
* If a page is set to inherit , but has no parent , it inherits from
* { @ link SiteConfig }
*
2007-07-24 05:43:21 +02: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 .
*
* @ return boolean True if the the member is allowed to do the given
* action .
*
* @ todo Check we get a endless recursion if we use parent :: can ()
*/
function can ( $perm , $member = null ) {
2009-07-31 07:43:41 +02:00
if ( ! $member || ! ( is_a ( $member , 'Member' )) || is_numeric ( $member )) {
$member = Member :: currentUserID ();
}
2008-11-07 13:18:35 +01:00
2008-12-04 23:38:32 +01:00
if ( $member && Permission :: checkMember ( $member , " ADMIN " )) return true ;
2008-04-22 04:28:07 +02:00
if ( method_exists ( $this , 'can' . ucfirst ( $perm ))) {
$method = 'can' . ucfirst ( $perm );
return $this -> $method ( $member );
2007-07-24 05:43:21 +02:00
}
2008-04-22 04:28:07 +02:00
2008-11-07 13:18:35 +01:00
$results = $this -> extend ( 'can' , $member );
if ( $results && is_array ( $results )) if ( ! min ( $results )) return false ;
2007-07-24 05:43:21 +02:00
2007-09-15 02:03:12 +02:00
return true ;
2007-07-24 05:43:21 +02:00
}
/**
* This function should return true if the current user can add children
2008-11-03 15:52:35 +01:00
* to this page . It can be overloaded to customise the security model for an
2007-07-24 05:43:21 +02:00
* application .
2008-11-03 15:52:35 +01:00
*
* Denies permission if any of the following conditions is TRUE :
* - alternateCanAddChildren () on a decorator returns FALSE
* - canEdit () is not granted
* - There are no classes defined in { @ link $allowed_children }
*
2008-11-24 23:04:32 +01:00
* @ uses SiteTreeDecorator -> canAddChildren ()
2008-11-03 15:52:35 +01:00
* @ uses canEdit ()
* @ uses $allowed_children
2007-07-24 05:43:21 +02:00
*
2007-07-19 12:40:28 +02:00
* @ return boolean True if the current user can add children .
*/
2008-04-22 04:28:07 +02:00
public function canAddChildren ( $member = null ) {
2009-07-31 07:43:41 +02:00
if ( ! $member || ! ( is_a ( $member , 'Member' )) || is_numeric ( $member )) {
$member = Member :: currentUserID ();
}
2008-12-04 23:38:32 +01:00
if ( $member && Permission :: checkMember ( $member , " ADMIN " )) return true ;
2008-04-22 04:28:07 +02:00
2010-10-15 02:33:48 +02:00
// Standard mechanism for accepting permission changes from decorators
$extended = $this -> extendedCan ( 'canAddChildren' , $member );
if ( $extended !== null ) return $extended ;
2008-04-22 04:28:07 +02:00
2008-12-04 23:38:32 +01:00
return $this -> canEdit ( $member ) && $this -> stat ( 'allowed_children' ) != 'none' ;
2007-07-19 12:40:28 +02:00
}
2007-09-14 21:17:37 +02:00
2007-09-15 02:03:12 +02:00
/**
* This function should return true if the current user can view this
2008-11-03 15:52:35 +01:00
* page . It can be overloaded to customise the security model for an
2007-09-15 02:03:12 +02:00
* application .
2008-11-03 15:52:35 +01:00
*
* Denies permission if any of the following conditions is TRUE :
2008-11-07 13:18:35 +01:00
* - canView () on any decorator returns FALSE
2008-11-03 15:52:35 +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
*
2008-11-07 13:18:35 +01:00
* @ uses DataObjectDecorator -> canView ()
2008-11-03 15:52:35 +01:00
* @ uses ViewerGroups ()
2007-09-15 02:03:12 +02:00
*
* @ return boolean True if the current user can view this page .
*/
2008-04-22 04:28:07 +02:00
public function canView ( $member = null ) {
2009-07-31 07:43:41 +02:00
if ( ! $member || ! ( is_a ( $member , 'Member' )) || is_numeric ( $member )) {
$member = Member :: currentUserID ();
}
2008-11-03 15:52:35 +01:00
// admin override
2009-06-08 09:00:37 +02:00
if ( $member && Permission :: checkMember ( $member , array ( " ADMIN " , " SITETREE_VIEW_ALL " ))) return true ;
2010-10-15 02:33:48 +02:00
// Standard mechanism for accepting permission changes from decorators
$extended = $this -> extendedCan ( 'canView' , $member );
if ( $extended != null ) return $extended ;
2008-04-22 04:28:07 +02:00
2009-08-08 06:23:05 +02:00
// check for empty spec
if ( ! $this -> CanViewType || $this -> CanViewType == 'Anyone' ) return true ;
// check for inherit
if ( $this -> CanViewType == 'Inherit' ) {
if ( $this -> ParentID ) return $this -> Parent () -> canView ( $member );
2010-04-12 07:02:31 +02:00
else return $this -> getSiteConfig () -> canView ( $member );
2009-08-08 06:23:05 +02:00
}
// check for any logged-in users
if ( $this -> CanViewType == 'LoggedInUsers' && $member ) {
return true ;
}
// check for specific groups
if ( $member && is_numeric ( $member )) $member = DataObject :: get_by_id ( 'Member' , $member );
if (
$this -> CanViewType == 'OnlyTheseUsers'
&& $member
&& $member -> inGroups ( $this -> ViewerGroups ())
) return true ;
return false ;
2008-04-22 04:28:07 +02:00
}
2009-12-02 10:40:45 +01:00
/**
* Determines permissions for a specific stage ( see { @ link Versioned }) .
* Usually the stage is read from { @ link Versioned :: current_stage ()} .
* Falls back to { @ link canView } .
*
* @ todo Implement in CMS UI .
*
* @ param String $stage
* @ param Member $member
* @ return boolean
*/
function canViewStage ( $stage , $member = null ) {
if ( ! $member ) $member = Member :: currentUser ();
if (
strtolower ( $stage ) == 'stage' &&
2010-04-14 04:28:51 +02:00
! ( Permission :: checkMember ( $member , 'CMS_ACCESS_CMSMain' ) || Permission :: checkMember ( $member , 'VIEW_DRAFT_CONTENT' ))
2009-12-02 10:40:45 +01:00
) return false ;
return $this -> canView ( $member );
}
2007-09-15 02:03:12 +02:00
2007-07-19 12:40:28 +02:00
/**
2007-09-15 02:03:12 +02:00
* This function should return true if the current user can delete this
2008-11-03 15:52:35 +01:00
* page . It can be overloaded to customise the security model for an
2007-09-15 02:03:12 +02:00
* application .
2008-11-03 15:52:35 +01:00
*
* Denies permission if any of the following conditions is TRUE :
2008-11-07 13:18:35 +01:00
* - canDelete () returns FALSE on any decorator
2008-11-03 15:52:35 +01:00
* - canEdit () returns FALSE
* - any descendant page returns FALSE for canDelete ()
*
2008-11-07 13:18:35 +01:00
* @ uses canDelete ()
2008-11-24 23:04:32 +01:00
* @ uses DataObjectDecorator -> canDelete ()
2008-11-03 15:52:35 +01:00
* @ uses canEdit ()
2007-09-15 02:03:12 +02:00
*
2008-08-09 08:40:50 +02:00
* @ param Member $member
2007-07-19 12:40:28 +02:00
* @ return boolean True if the current user can delete this page .
*/
2008-04-22 04:28:07 +02:00
public function canDelete ( $member = null ) {
2009-07-31 07:43:41 +02:00
if ( $member instanceof Member ) $memberID = $member -> ID ;
else if ( is_numeric ( $member )) $memberID = $member ;
else $memberID = Member :: currentUserID ();
2008-11-03 15:52:35 +01:00
2009-07-31 07:43:41 +02:00
if ( $memberID && Permission :: checkMember ( $memberID , array ( " ADMIN " , " SITETREE_EDIT_ALL " ))) {
return true ;
}
2008-04-22 04:28:07 +02:00
2010-10-15 02:33:48 +02:00
// Standard mechanism for accepting permission changes from decorators
$extended = $this -> extendedCan ( 'canDelete' , $memberID );
if ( $extended !== null ) return $extended ;
2008-04-22 04:28:07 +02:00
2009-07-31 07:43:41 +02:00
// Check cache (the can_edit_multiple call below will also do this, but this is quicker)
if ( isset ( self :: $cache_permissions [ 'delete' ][ $this -> ID ])) {
return self :: $cache_permissions [ 'delete' ][ $this -> ID ];
2008-11-03 15:52:35 +01:00
}
2009-07-31 07:43:41 +02:00
// Regular canEdit logic is handled by can_edit_multiple
$results = self :: can_delete_multiple ( array ( $this -> ID ), $memberID );
2010-04-12 05:12:14 +02: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 ;
2007-07-19 12:40:28 +02:00
}
2007-09-14 21:17:37 +02:00
2007-07-19 12:40:28 +02:00
/**
2007-09-15 02:03:12 +02:00
* This function should return true if the current user can create new
2008-11-03 15:52:35 +01:00
* pages of this class . It can be overloaded to customise the security model for an
2007-09-15 02:03:12 +02:00
* application .
2008-11-03 15:52:35 +01:00
*
* Denies permission if any of the following conditions is TRUE :
2008-11-07 13:18:35 +01:00
* - canCreate () returns FALSE on any decorator
2008-11-03 15:52:35 +01:00
* - $can_create is set to FALSE and the site is not in " dev mode "
*
* Use { @ link canAddChildren ()} to control behaviour of creating children under this page .
*
* @ uses $can_create
2008-11-24 23:04:32 +01:00
* @ uses DataObjectDecorator -> canCreate ()
2007-09-15 02:03:12 +02:00
*
2008-08-09 08:40:50 +02:00
* @ param Member $member
2008-11-03 15:52:35 +01:00
* @ return boolean True if the current user can create pages on this class .
2007-07-19 12:40:28 +02:00
*/
2008-04-22 04:28:07 +02:00
public function canCreate ( $member = null ) {
2009-07-31 07:43:41 +02:00
if ( ! $member || ! ( is_a ( $member , 'Member' )) || is_numeric ( $member )) {
$member = Member :: currentUserID ();
}
2008-11-03 15:52:35 +01:00
2008-12-04 23:38:32 +01:00
if ( $member && Permission :: checkMember ( $member , " ADMIN " )) return true ;
2008-04-22 04:28:07 +02:00
2010-10-15 02:33:48 +02:00
// Standard mechanism for accepting permission changes from decorators
$extended = $this -> extendedCan ( 'canCreate' , $member );
if ( $extended !== null ) return $extended ;
2008-04-22 04:28:07 +02:00
2007-09-14 21:17:37 +02:00
return $this -> stat ( 'can_create' ) != false || Director :: isDev ();
2007-07-19 12:40:28 +02:00
}
2007-09-14 21:17:37 +02:00
2010-04-12 05:38:58 +02:00
2007-07-19 12:40:28 +02:00
/**
2007-09-15 02:03:12 +02:00
* This function should return true if the current user can edit this
2008-11-03 15:52:35 +01:00
* page . It can be overloaded to customise the security model for an
2007-09-15 02:03:12 +02:00
* application .
2008-11-03 15:52:35 +01:00
*
* Denies permission if any of the following conditions is TRUE :
2008-11-07 13:18:35 +01:00
* - canEdit () on any decorator returns FALSE
2008-11-03 15:52:35 +01:00
* - canView () return false
* - " CanEditType " directive is set to " Inherit " and any parent page return false for canEdit ()
* - " CanEditType " directive is set to " LoggedInUsers " and no user is logged in or doesn ' t have the CMS_Access_CMSMAIN permission code
* - " CanEditType " directive is set to " OnlyTheseUsers " and user is not in the given groups
*
* @ uses canView ()
* @ uses EditorGroups ()
2008-11-24 23:04:32 +01:00
* @ uses DataObjectDecorator -> canEdit ()
2007-09-15 02:03:12 +02:00
*
2008-11-25 23:23:47 +01:00
* @ param Member $member Set to FALSE if you want to explicitly test permissions without a valid user ( useful for unit tests )
2007-07-19 12:40:28 +02:00
* @ return boolean True if the current user can edit this page .
*/
2008-04-22 04:28:07 +02:00
public function canEdit ( $member = null ) {
2009-07-31 07:43:41 +02:00
if ( $member instanceof Member ) $memberID = $member -> ID ;
else if ( is_numeric ( $member )) $memberID = $member ;
else $memberID = Member :: currentUserID ();
2008-11-07 13:18:35 +01:00
2009-07-31 07:43:41 +02:00
if ( $memberID && Permission :: checkMember ( $memberID , array ( " ADMIN " , " SITETREE_EDIT_ALL " ))) return true ;
2008-11-07 13:18:35 +01:00
2010-10-15 02:33:48 +02:00
// Standard mechanism for accepting permission changes from decorators
$extended = $this -> extendedCan ( 'canEdit' , $memberID );
if ( $extended !== null ) return $extended ;
2010-04-12 07:02:31 +02:00
2010-04-12 04:35:43 +02:00
if ( $this -> ID ) {
// Check cache (the can_edit_multiple call below will also do this, but this is quicker)
2010-04-13 04:29:03 +02:00
if ( isset ( self :: $cache_permissions [ 'CanEditType' ][ $this -> ID ])) {
return self :: $cache_permissions [ 'CanEditType' ][ $this -> ID ];
2010-04-12 04:35:43 +02:00
}
2008-11-03 15:52:35 +01:00
2010-04-12 04:35:43 +02:00
// Regular canEdit logic is handled by can_edit_multiple
$results = self :: can_edit_multiple ( array ( $this -> ID ), $memberID );
2010-04-12 07:02:31 +02:00
2010-04-12 05:12:14 +02: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 ;
2010-04-12 04:35:43 +02:00
// Default for unsaved pages
} else {
2010-04-12 07:02:31 +02:00
return $this -> getSiteConfig () -> canEdit ( $member );
2010-04-12 04:35:43 +02:00
}
2007-07-19 12:40:28 +02:00
}
/**
2007-09-15 02:03:12 +02:00
* This function should return true if the current user can publish this
2008-11-03 15:52:35 +01:00
* page . It can be overloaded to customise the security model for an
2007-09-15 02:03:12 +02:00
* application .
2008-11-03 15:52:35 +01:00
*
* Denies permission if any of the following conditions is TRUE :
2008-11-07 13:18:35 +01:00
* - canPublish () on any decorator returns FALSE
2008-11-03 15:52:35 +01:00
* - canEdit () returns FALSE
*
2008-11-24 23:04:32 +01:00
* @ uses SiteTreeDecorator -> canPublish ()
2007-09-15 02:03:12 +02:00
*
2008-08-09 08:40:50 +02:00
* @ param Member $member
2007-07-19 12:40:28 +02:00
* @ return boolean True if the current user can publish this page .
*/
2008-04-22 04:28:07 +02:00
public function canPublish ( $member = null ) {
2009-02-02 00:49:53 +01:00
if ( ! $member || ! ( is_a ( $member , 'Member' )) || is_numeric ( $member )) $member = Member :: currentUser ();
2008-11-07 13:18:35 +01:00
2008-12-04 23:38:32 +01:00
if ( $member && Permission :: checkMember ( $member , " ADMIN " )) return true ;
2008-11-03 15:52:35 +01:00
2010-10-15 02:33:48 +02:00
// Standard mechanism for accepting permission changes from decorators
$extended = $this -> extendedCan ( 'canPublish' , $member );
if ( $extended !== null ) return $extended ;
// Normal case - fail over to canEdit()
2008-12-04 23:38:32 +01:00
return $this -> canEdit ( $member );
2007-07-19 12:40:28 +02:00
}
2010-04-12 03:44:06 +02:00
2010-04-12 05:12:45 +02:00
public function canDeleteFromLive ( $member = null ) {
2010-10-15 02:33:48 +02:00
// Standard mechanism for accepting permission changes from decorators
$extended = $this -> extendedCan ( 'canDeleteFromLive' , $member );
if ( $extended !== null ) return $extended ;
2010-04-12 05:12:45 +02:00
return $this -> canPublish ( $member );
}
2010-04-12 03:44:06 +02:00
/**
* Stub method to get the site config , provided so it ' s easy to override
*/
function getSiteConfig () {
2010-10-04 06:21:59 +02:00
$altConfig = false ;
if ( $this -> hasMethod ( 'alternateSiteConfig' )) {
$altConfig = $this -> alternateSiteConfig ();
}
if ( $altConfig ) {
return $altConfig ;
2010-04-12 07:02:31 +02:00
} elseif ( $this -> hasExtension ( 'Translatable' )) {
return SiteConfig :: current_site_config ( $this -> Locale );
} else {
return SiteConfig :: current_site_config ();
}
2010-04-12 03:44:06 +02:00
}
2007-09-14 21:17:37 +02:00
2009-07-31 07:43:41 +02:00
/**
* Pre - populate the cache of canEdit , canView , canDelete , canPublish permissions .
* This method will use the static can_ ( perm ) _multiple method for efficiency .
2010-04-14 05:57:21 +02:00
*
* @ param $permission String The permission : edit , view , publish , approve , etc .
* @ param $ids array An array of page IDs
* @ param $batchCallBack The function / static method to call to calculate permissions . Defaults
* to 'SiteTree::can_(permission)_multiple'
2009-07-31 07:43:41 +02:00
*/
2010-04-14 05:57:21 +02:00
static function prepopuplate_permission_cache ( $permission = 'CanEditType' , $ids , $batchCallback = null ) {
if ( ! $batchCallback ) $batchCallback = " SiteTree::can_ { $permission } _multiple " ;
2010-05-25 05:44:54 +02:00
//PHP 5.1 requires an array rather than a string for the call_user_func function
$batchCallback = explode ( '::' , $batchCallback );
2010-04-14 05:57:21 +02:00
if ( is_callable ( $batchCallback )) {
$permissionValues = call_user_func ( $batchCallback , $ids ,
2009-07-31 07:43:41 +02:00
Member :: currentUserID (), false );
if ( ! isset ( self :: $cache_permissions [ $permission ])) {
self :: $cache_permissions [ $permission ] = array ();
}
self :: $cache_permissions [ $permission ] = $permissionValues
+ self :: $cache_permissions [ $permission ];
} else {
2010-04-14 05:57:21 +02:00
user_error ( " SiteTree::prepopuplate_permission_cache can't calculate ' $permission ' "
. " with callback ' $batchCallback ' " , E_USER_WARNING );
2009-07-31 07:43:41 +02:00
}
}
2010-04-13 04:29:03 +02:00
static function batch_permission_check ( $ids , $memberID , $typeField , $groupJoinTable , $siteConfigMethod , $globalPermission = 'CMS_ACCESS_CMSMain' , $useCached = true ) {
2009-08-08 06:23:05 +02:00
// Sanitise the IDs
$ids = array_filter ( $ids , 'is_numeric' );
2010-04-14 05:59:54 +02:00
// This is the name used on the permission cache
// converts something like 'CanEditType' to 'edit'.
$cacheKey = strtolower ( substr ( $typeField , 3 , - 4 ));
2009-08-08 06:23:05 +02:00
2009-07-31 07:43:41 +02:00
// Default result: nothing editable
$result = array_fill_keys ( $ids , false );
2009-08-03 05:13:17 +02:00
if ( $ids ) {
2009-07-31 07:43:41 +02:00
2009-08-03 05:13:17 +02:00
// Look in the cache for values
2010-04-14 05:59:54 +02:00
if ( $useCached && isset ( self :: $cache_permissions [ $cacheKey ])) {
$cachedValues = array_intersect_key ( self :: $cache_permissions [ $cacheKey ], $result );
2009-07-31 07:43:41 +02:00
2009-08-03 05:13:17 +02:00
// If we can't find everything in the cache, then look up the remainder separately
2010-04-14 05:59:54 +02:00
$uncachedValues = array_diff_key ( $result , self :: $cache_permissions [ $cacheKey ]);
2009-08-03 05:13:17 +02:00
if ( $uncachedValues ) {
2010-04-13 04:29:03 +02:00
$cachedValues = self :: batch_permission_check ( array_keys ( $uncachedValues ), $memberID , $typeField , $groupJoinTable , $siteConfigMethod , $globalPermission , false ) + $cachedValues ;
2009-08-03 05:13:17 +02:00
}
return $cachedValues ;
2009-07-31 07:43:41 +02:00
}
2009-08-03 05:13:17 +02:00
// If a member doesn't have CMS_ACCESS_CMSMain permission then they can't edit anything
2010-04-13 04:29:03 +02:00
if ( ! $memberID || ( $globalPermission && ! Permission :: checkMember ( $memberID , $globalPermission ))) {
2009-08-03 05:13:17 +02:00
return $result ;
}
2009-07-31 07:43:41 +02:00
2009-08-03 05:13:17 +02:00
$SQL_idList = implode ( $ids , " , " );
// if page can't be viewed, don't grant edit permissions
// to do - implement can_view_multiple(), so this can be enabled
//$ids = array_keys(array_filter(self::can_view_multiple($ids, $memberID)));
// Get the groups that the given member belongs to
$groupIDs = DataObject :: get_by_id ( 'Member' , $memberID ) -> Groups () -> column ( " ID " );
$SQL_groupList = implode ( " , " , $groupIDs );
2010-05-25 05:40:56 +02:00
if ( ! $SQL_groupList ) $SQL_groupList = '0' ;
2009-07-31 07:43:41 +02:00
2010-04-12 04:04:15 +02:00
$combinedStageResult = array ();
2009-08-27 08:53:28 +02:00
foreach ( array ( 'Stage' , 'Live' ) as $stage ) {
2010-04-12 04:34:21 +02:00
// Start by filling the array with the pages that actually exist
$table = ( $stage == 'Stage' ) ? " SiteTree " : " SiteTree_ $stage " ;
2010-04-12 05:12:14 +02:00
2010-04-12 04:35:28 +02:00
$result = array_fill_keys ( DB :: query ( " SELECT \" ID \" FROM \" $table\ "
WHERE \ " ID \" IN ( " . implode ( " , " , $ids ) . " ) " ) -> column (), false );
2010-04-12 04:04:15 +02:00
2009-08-27 08:53:28 +02:00
// Get the uninherited permissions
2010-04-13 04:29:03 +02:00
$uninheritedPermissions = Versioned :: get_by_stage ( " SiteTree " , $stage , " ( \" $typeField\ " = 'LoggedInUsers' OR
( \ " $typeField\ " = 'OnlyTheseUsers' AND \ " $groupJoinTable\ " . \ " SiteTreeID \" IS NOT NULL))
2009-09-14 00:27:47 +02:00
AND \ " SiteTree \" . \" ID \" IN ( $SQL_idList ) " ,
2009-08-27 08:53:28 +02:00
" " ,
2010-04-13 04:29:03 +02:00
" LEFT JOIN \" $groupJoinTable\ "
ON \ " $groupJoinTable\ " . \ " SiteTreeID \" = \" SiteTree \" . \" ID \"
AND \ " $groupJoinTable\ " . \ " GroupID \" IN ( $SQL_groupList ) " );
2010-04-12 04:04:15 +02:00
2009-08-27 08:53:28 +02:00
if ( $uninheritedPermissions ) {
// Set all the relevant items in $result to true
$result = array_fill_keys ( $uninheritedPermissions -> column ( 'ID' ), true ) + $result ;
2009-08-03 05:13:17 +02:00
}
2009-08-27 08:53:28 +02:00
// Get permissions that are inherited
2010-04-13 04:29:03 +02:00
$potentiallyInherited = Versioned :: get_by_stage ( " SiteTree " , $stage , " \" $typeField\ " = 'Inherit'
2009-09-14 00:27:47 +02:00
AND \ " SiteTree \" . \" ID \" IN ( $SQL_idList ) " );
2009-08-27 08:53:28 +02:00
if ( $potentiallyInherited ) {
// Group $potentiallyInherited by ParentID; we'll look at the permission of all those
// parents and then see which ones the user has permission on
2010-04-13 04:29:03 +02:00
$siteConfigPermission = SiteConfig :: current_site_config () -> { $siteConfigMethod }( $memberID );
2010-04-14 06:11:18 +02:00
$groupedByParent = array ();
2009-08-27 08:53:28 +02:00
foreach ( $potentiallyInherited as $item ) {
2010-04-14 06:11:18 +02:00
if ( $item -> ParentID ) {
if ( ! isset ( $groupedByParent [ $item -> ParentID ])) $groupedByParent [ $item -> ParentID ] = array ();
$groupedByParent [ $item -> ParentID ][] = $item -> ID ;
} else {
$result [ $item -> ID ] = $siteConfigPermission ;
}
2009-08-27 08:53:28 +02:00
}
2010-04-14 06:11:18 +02: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 ;
}
2009-08-27 08:53:28 +02:00
}
2009-08-03 05:13:17 +02:00
}
2009-07-31 07:43:41 +02:00
}
2010-04-12 04:04:15 +02:00
$combinedStageResult = $combinedStageResult + $result ;
2010-04-12 04:34:21 +02:00
2009-07-31 07:43:41 +02:00
}
}
2010-04-14 06:11:18 +02:00
if ( isset ( $combinedStageResult )) {
// Cache results
2010-04-14 06:11:38 +02:00
// TODO - Caching permissions is breaking unit tests. One possible issue
// is the cache needs to be flushed when permission on a page is changed,
// but this only solved some of the failing unit tests. Disabled for now.
/* foreach ( $combinedStageResult as $id => $val ) {
2010-04-14 06:11:18 +02:00
self :: $cache_permissions [ $typeField ][ $id ] = $val ;
2010-04-14 06:11:38 +02:00
} */
2010-04-14 06:11:18 +02:00
return $combinedStageResult ;
} else {
2010-04-14 06:40:16 +02:00
return array ();
2010-04-14 06:11:18 +02:00
}
2010-04-13 04:29:03 +02:00
}
/**
* Get the 'can edit' information for a number of SiteTree pages .
*
* @ param An array of IDs of the SiteTree pages to look up .
* @ param useCached Return values from the permission cache if they exist .
* @ return A map where the IDs are keys and the values are booleans stating whether the given
* page can be edited .
*/
static function can_edit_multiple ( $ids , $memberID , $useCached = true ) {
return self :: batch_permission_check ( $ids , $memberID , 'CanEditType' , 'SiteTree_EditorGroups' , 'canEdit' , 'CMS_ACCESS_CMSMain' , $useCached );
2009-07-31 07:43:41 +02:00
}
/**
* Get the 'can edit' information for a number of SiteTree pages .
* @ param An array of IDs of the SiteTree pages to look up .
* @ param useCached Return values from the permission cache if they exist .
*/
static function can_delete_multiple ( $ids , $memberID , $useCached = true ) {
2009-08-02 01:54:36 +02:00
$deletable = array ();
2010-04-13 00:46:40 +02:00
$result = array_fill_keys ( $ids , false );
2009-07-31 07:43:41 +02:00
// Look in the cache for values
if ( $useCached && isset ( self :: $cache_permissions [ 'delete' ])) {
$cachedValues = array_intersect_key ( self :: $cache_permissions [ 'delete' ], $result );
// If we can't find everything in the cache, then look up the remainder separately
$uncachedValues = array_diff_key ( $result , self :: $cache_permissions [ 'delete' ]);
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 ) {
$idList = implode ( " , " , $editableIDs );
// You can only delete pages whose children you can delete
2009-09-14 00:27:47 +02:00
$childRecords = DataObject :: get ( " SiteTree " , " \" ParentID \" IN ( $idList ) " );
2009-07-31 07:43:41 +02:00
if ( $childRecords ) {
$children = $childRecords -> map ( " ID " , " ParentID " );
// Find out the children that can be deleted
$deletableChildren = self :: can_delete_multiple ( array_keys ( $children ), $memberID );
// 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
$parents = array_unique ( $children );
$deletableLeafNodes = array_diff ( $editableIDs , $parents );
// Combine the two
$deletable = array_merge ( $deletableParents , $deletableLeafNodes );
} else {
$deletable = $editableIDs ;
}
2009-08-03 05:13:17 +02:00
} else {
$deletable = array ();
2009-07-31 07:43:41 +02:00
}
// Convert the array of deletable IDs into a map of the original IDs with true/false as the
// value
return array_fill_keys ( $deletable , true ) + array_fill_keys ( $ids , false );
}
2007-07-19 12:40:28 +02:00
/**
2007-09-14 21:17:37 +02:00
* Collate selected descendants of this page .
2007-09-15 02:03:12 +02:00
*
* { @ link $condition } will be evaluated on each descendant , and if it is
* succeeds , that item will be added to the $collator array .
*
* @ 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 .
2007-07-19 12:40:28 +02:00
*/
public function collateDescendants ( $condition , & $collator ) {
if ( $children = $this -> Children ()) {
foreach ( $children as $item ) {
if ( eval ( " return $condition ; " )) $collator [] = $item ;
$item -> collateDescendants ( $condition , $collator );
}
return true ;
}
}
2007-09-14 21:17:37 +02:00
2007-09-15 02:03:12 +02:00
2007-07-19 12:40:28 +02:00
/**
2007-09-16 18:21:09 +02:00
* Return the title , description , keywords and language metatags .
2008-08-11 01:35:11 +02:00
*
* @ todo Move < title > tag in separate getter for easier customization and more obvious usage
*
2007-08-01 23:41:09 +02:00
* @ param boolean | string $includeTitle Show default < title >- tag , set to false for custom templating
2007-09-15 02:03:12 +02:00
* @ param boolean $includeTitle Show default < title >- tag , set to false for
* custom templating
2007-07-19 12:40:28 +02:00
* @ return string The XHTML metatags
*/
public function MetaTags ( $includeTitle = true ) {
$tags = " " ;
2007-08-12 23:51:51 +02:00
if ( $includeTitle === true || $includeTitle == 'true' ) {
2007-09-15 02:03:12 +02:00
$tags .= " <title> " . Convert :: raw2xml (( $this -> MetaTitle )
? $this -> MetaTitle
: $this -> Title ) . " </title> \n " ;
2007-07-19 12:40:28 +02:00
}
2009-02-02 00:49:53 +01:00
2010-03-08 21:26:34 +01:00
$tags .= " <meta name= \" generator \" content= \" SilverStripe - http://www.silverstripe.com \" /> \n " ;
2007-09-14 21:17:37 +02:00
2007-07-19 12:40:28 +02:00
$charset = ContentNegotiator :: get_encoding ();
$tags .= " <meta http-equiv= \" Content-type \" content= \" text/html; charset= $charset\ " /> \n " ;
if ( $this -> MetaKeywords ) {
2010-03-08 21:26:34 +01:00
$tags .= " <meta name= \" keywords \" content= \" " . Convert :: raw2att ( $this -> MetaKeywords ) . " \" /> \n " ;
2007-07-19 12:40:28 +02:00
}
if ( $this -> MetaDescription ) {
2010-03-08 21:26:34 +01:00
$tags .= " <meta name= \" description \" content= \" " . Convert :: raw2att ( $this -> MetaDescription ) . " \" /> \n " ;
2007-07-19 12:40:28 +02:00
}
2007-09-14 02:47:00 +02:00
if ( $this -> ExtraMeta ) {
$tags .= $this -> ExtraMeta . " \n " ;
}
2010-04-12 05:39:35 +02:00
2008-11-07 13:18:35 +01:00
$this -> extend ( 'MetaTags' , $tags );
2007-07-19 12:40:28 +02:00
return $tags ;
}
2007-09-14 21:17:37 +02:00
2007-09-15 02:03:12 +02:00
2007-07-19 12:40:28 +02:00
/**
2007-09-15 02:03:12 +02:00
* Returns the object that contains the content that a user would
* associate with this page .
*
* Ordinarily , this is just the page itself , but for example on
* RedirectorPages or VirtualPages ContentSource () will return the page
* that is linked to .
*
2007-07-19 12:40:28 +02:00
* @ return SiteTree The content source .
*/
public function ContentSource () {
return $this ;
}
2007-09-14 21:17:37 +02:00
2007-09-15 02:03:12 +02:00
/**
* Add default records to database .
*
* 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 () .
*/
2007-07-19 12:40:28 +02:00
function requireDefaultRecords () {
parent :: requireDefaultRecords ();
2008-11-10 00:58:48 +01:00
// default pages
2007-08-15 04:50:39 +02:00
if ( $this -> class == 'SiteTree' ) {
2010-04-12 23:19:54 +02:00
if ( ! SiteTree :: get_by_link ( 'home' )) {
2007-08-15 04:50:39 +02:00
$homepage = new Page ();
2008-02-25 03:10:37 +01:00
$homepage -> Title = _t ( 'SiteTree.DEFAULTHOMETITLE' , 'Home' );
2009-11-06 04:35:07 +01: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>. You can now access the <a href="http://doc.silverstripe.org">developer documentation</a>, or begin <a href="http://doc.silverstripe.org/doku.php?id=tutorials">the tutorials.</a></p>' );
2010-10-13 03:03:25 +02:00
$homepage -> URLSegment = 'home' ;
$homepage -> Status = 'Published' ;
$homepage -> Sort = 1 ;
2007-08-15 04:50:39 +02:00
$homepage -> write ();
2010-10-13 03:03:25 +02:00
$homepage -> publish ( 'Stage' , 'Live' );
2007-08-15 04:50:39 +02:00
$homepage -> flushCache ();
2010-10-13 03:03:25 +02:00
DB :: alteration_message ( 'Home page created' , 'created' );
2007-07-19 12:40:28 +02:00
}
2007-08-15 04:50:39 +02:00
2008-11-24 00:28:16 +01:00
if ( DB :: query ( " SELECT COUNT(*) FROM \" SiteTree \" " ) -> value () == 1 ) {
2007-08-15 04:50:39 +02:00
$aboutus = new Page ();
2008-02-25 03:10:37 +01:00
$aboutus -> Title = _t ( 'SiteTree.DEFAULTABOUTTITLE' , 'About Us' );
$aboutus -> Content = _t ( 'SiteTree.DEFAULTABOUTCONTENT' , '<p>You can fill this page out with your own content, or delete it and create your own pages.<br /></p>' );
2010-10-13 03:03:25 +02:00
$aboutus -> Status = 'Published' ;
$aboutus -> Sort = 2 ;
2007-08-15 04:50:39 +02:00
$aboutus -> write ();
2010-10-13 03:03:25 +02:00
$aboutus -> publish ( 'Stage' , 'Live' );
$aboutus -> flushCache ();
DB :: alteration_message ( 'About Us page created' , 'created' );
2007-08-15 04:50:39 +02:00
$contactus = new Page ();
2008-02-25 03:10:37 +01:00
$contactus -> Title = _t ( 'SiteTree.DEFAULTCONTACTTITLE' , 'Contact Us' );
$contactus -> Content = _t ( 'SiteTree.DEFAULTCONTACTCONTENT' , '<p>You can fill this page out with your own content, or delete it and create your own pages.<br /></p>' );
2010-10-13 03:03:25 +02:00
$contactus -> Status = 'Published' ;
$contactus -> Sort = 3 ;
2007-08-15 04:50:39 +02:00
$contactus -> write ();
2010-10-13 03:03:25 +02:00
$contactus -> publish ( 'Stage' , 'Live' );
2007-08-15 04:50:39 +02:00
$contactus -> flushCache ();
2010-10-13 03:03:25 +02:00
DB :: alteration_message ( 'Contact Us page created' , 'created' );
2007-07-19 12:40:28 +02:00
}
}
2008-11-10 00:58:48 +01:00
// schema migration
// @todo Move to migration task once infrastructure is implemented
if ( $this -> class == 'SiteTree' ) {
$conn = DB :: getConn ();
// only execute command if fields haven't been renamed to _obsolete_<fieldname> already by the task
if ( array_key_exists ( 'Viewers' , $conn -> fieldList ( 'SiteTree' ))) {
$task = new UpgradeSiteTreePermissionSchemaTask ();
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
$task -> run ( new SS_HTTPRequest ( 'GET' , '/' ));
2008-11-10 00:58:48 +01:00
}
}
2007-07-19 12:40:28 +02:00
}
2007-09-15 02:03:12 +02:00
2007-07-19 12:40:28 +02:00
//------------------------------------------------------------------------------------//
protected function onBeforeWrite () {
2010-05-25 05:43:28 +02:00
parent :: onBeforeWrite ();
2010-10-13 02:56:27 +02:00
2009-01-13 06:02:11 +01:00
// If Sort hasn't been set, make this page come after it's siblings
2010-10-13 02:56:27 +02:00
if ( ! $this -> Sort ) {
$parentID = ( $this -> ParentID ) ? $this -> ParentID : 0 ;
$this -> Sort = DB :: query ( " SELECT MAX( \" Sort \" ) + 1 FROM \" SiteTree \" WHERE \" ParentID \" = $parentID " ) -> value ();
2007-07-19 12:40:28 +02:00
}
2007-09-14 21:17:37 +02:00
2009-01-13 06:02:11 +01:00
// If there is no URLSegment set, generate one from Title
if (( ! $this -> URLSegment || $this -> URLSegment == 'new-page' ) && $this -> Title ) {
2007-07-19 12:40:28 +02:00
$this -> URLSegment = $this -> generateURLSegment ( $this -> Title );
2009-05-27 04:09:18 +02:00
} else if ( $this -> isChanged ( 'URLSegment' )) {
2009-01-13 06:02:11 +01:00
// Make sure the URLSegment is valid for use in a URL
2007-07-19 12:40:28 +02:00
$segment = ereg_replace ( '[^A-Za-z0-9]+' , '-' , $this -> URLSegment );
$segment = ereg_replace ( '-+' , '-' , $segment );
2009-01-13 06:02:11 +01:00
// If after sanitising there is no URLSegment, give it a reasonable default
2007-07-19 12:40:28 +02:00
if ( ! $segment ) {
$segment = " page- $this->ID " ;
}
$this -> URLSegment = $segment ;
}
2007-08-16 08:32:49 +02:00
DataObject :: set_context_obj ( $this );
2009-10-11 02:07:21 +02: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 ;
2007-07-19 12:40:28 +02:00
$count ++ ;
}
2009-10-11 02:07:21 +02:00
2007-08-16 08:32:49 +02:00
DataObject :: set_context_obj ( null );
2009-10-15 23:46:13 +02:00
$this -> syncLinkTracking ();
2010-10-04 06:52:46 +02:00
// Check to see if we've only altered fields that shouldn't affect versioning
$fieldsIgnoredByVersioning = array ( 'HasBrokenLink' , 'Status' , 'HasBrokenFile' , 'ToDo' );
$changedFields = array_keys ( $this -> getChangedFields ( true , 2 ));
// This more rigorous check is inline with the test that write()
// does to dedcide whether or not to write to the DB. We use that
// to avoid cluttering the system with a migrateVersion() call
// that doesn't get used
$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 );
}
2007-07-19 12:40:28 +02:00
}
2009-10-15 23:46:13 +02:00
function syncLinkTracking () {
2010-10-04 06:48:47 +02:00
// Build a list of HTMLText fields
$allFields = $this -> db ();
$htmlFields = array ();
foreach ( $allFields as $field => $fieldSpec ) {
if ( preg_match ( '/([^(]+)/' , $fieldSpec , $matches )) {
$class = $matches [ 0 ];
2010-10-13 02:55:31 +02:00
if ( class_exists ( $class )){
if ( $class == 'HTMLText' || is_subclass_of ( $class , 'HTMLText' )) $htmlFields [] = $field ;
}
2010-10-04 06:48:47 +02:00
}
}
2009-10-15 23:46:13 +02:00
$linkedPages = array ();
$linkedFiles = array ();
$this -> HasBrokenLink = false ;
$this -> HasBrokenFile = false ;
2010-10-04 06:48:47 +02:00
foreach ( $htmlFields as $field ) {
2010-10-12 23:42:25 +02:00
$formField = new HTMLEditorField ( $field );
2010-10-04 06:52:46 +02:00
$formField -> setValue ( $this -> $field );
$formField -> saveInto ( $this );
2009-10-15 23:46:13 +02:00
}
2009-10-16 00:20:39 +02:00
$this -> extend ( 'augmentSyncLinkTracking' );
2009-10-15 23:46:13 +02:00
}
2008-12-17 23:41:07 +01:00
function onAfterWrite () {
// Need to flush cache to avoid outdated versionnumber references
$this -> flushCache ();
2009-05-04 03:20:12 +02:00
// Update any virtual pages that might need updating
2010-10-04 06:52:46 +02:00
$linkedPages = $this -> VirtualPages ();
2009-05-04 03:20:12 +02:00
if ( $linkedPages ) foreach ( $linkedPages as $page ) {
$page -> copyFrom ( $page -> CopyContentFrom ());
$page -> write ();
}
2008-12-17 23:41:07 +01:00
parent :: onAfterWrite ();
}
2007-08-16 08:32:49 +02:00
2009-05-25 08:59:21 +02:00
function onBeforeDelete () {
parent :: onBeforeDelete ();
// If deleting this page, delete all its children.
2010-04-14 05:57:45 +02:00
if ( SiteTree :: get_enforce_strict_hierarchy () && $children = $this -> Children ()) {
2009-05-25 08:59:21 +02:00
foreach ( $children as $child ) {
$child -> delete ();
}
}
}
2008-12-17 23:41:07 +01:00
function onAfterDelete () {
// Need to flush cache to avoid outdated versionnumber references
$this -> flushCache ();
2010-10-04 06:52:46 +02:00
// Need to mark pages depending to this one as broken
$dependentPages = $this -> DependentPages ();
if ( $dependentPages ) foreach ( $dependentPages as $page ) {
2010-04-13 04:17:11 +02:00
// $page->write() calls syncLinkTracking, which does all the hard work for us.
$page -> write ();
}
2010-04-12 04:32:51 +02:00
2008-12-17 23:41:07 +01:00
parent :: onAfterDelete ();
}
2009-10-11 02:07:21 +02:00
/**
* Returns TRUE if this object has a URLSegment value that does not conflict with any other objects . This methods
* checks for :
* - 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 .
*
* @ return bool
*/
public function validURLSegment () {
if ( self :: nested_urls () && $parent = $this -> Parent ()) {
if ( $controller = ModelAsController :: controller_for ( $parent )) {
2009-10-11 02:07:23 +02:00
if ( $controller instanceof Controller && $controller -> hasAction ( $this -> URLSegment )) return false ;
2009-10-11 02:07:21 +02:00
}
}
if ( ! self :: nested_urls () || ! $this -> ParentID ) {
if ( class_exists ( $this -> URLSegment ) && is_subclass_of ( $this -> URLSegment , 'RequestHandler' )) return false ;
}
$IDFilter = ( $this -> ID ) ? " AND \" SiteTree \" . \" ID \" <> $this->ID " : null ;
$parentFilter = null ;
if ( self :: nested_urls ()) {
if ( $this -> ParentID ) {
$parentFilter = " AND \" SiteTree \" . \" ParentID \" = $this->ParentID " ;
} else {
$parentFilter = ' AND "SiteTree"."ParentID" = 0' ;
}
}
2009-10-16 02:31:39 +02:00
$existingPage = DataObject :: get_one (
'SiteTree' ,
" \" URLSegment \" = ' $this->URLSegment ' $IDFilter $parentFilter "
);
2010-05-25 05:16:54 +02:00
if ( $existingPage ) {
return false ;
}
$values = $this -> extend ( 'augmentValidURLSegment' );
if ( count ( $values ) && ! min ( $values )) {
return false ;
}
return true ;
2009-10-11 02:07:21 +02:00
}
2007-07-19 12:40:28 +02:00
/**
* Generate a URL segment based on the title provided .
2010-10-15 02:39:36 +02:00
*
* If { @ link Extension } s wish to alter URL segment generation , they can do so by defining
* 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 decorators either start from scratch , or incrementally modify the generated URL .
*
2007-07-19 12:40:28 +02:00
* @ param string $title Page title .
* @ return string Generated url segment
*/
function generateURLSegment ( $title ){
2010-10-15 02:39:36 +02:00
$t = mb_strtolower ( $title );
$t = Object :: create ( 'Transliterator' ) -> toASCII ( $title );
2007-07-19 12:40:28 +02:00
$t = str_replace ( '&' , '-and-' , $t );
$t = str_replace ( '&' , '-and-' , $t );
$t = ereg_replace ( '[^A-Za-z0-9]+' , '-' , $t );
$t = ereg_replace ( '-+' , '-' , $t );
2009-08-18 00:30:27 +02:00
if ( ! $t || $t == '-' || $t == '-1' ) {
2007-07-19 12:40:28 +02:00
$t = " page- $this->ID " ;
}
2010-10-15 02:39:36 +02:00
$t = trim ( $t , '-' );
// Hook for decorators
$this -> extend ( 'updateURLSegment' , $t , $title );
return $t ;
2008-11-02 01:24:23 +01:00
}
/**
2009-11-21 03:31:18 +01:00
* @ return string
*/
function getStageURLSegment () {
$stageRecord = Versioned :: get_one_by_stage (
'SiteTree' ,
'Stage' ,
" \" SiteTree \" . \" ID \" = $this->ID "
);
return ( $stageRecord ) ? $stageRecord -> URLSegment : null ;
}
/**
* @ return string
*/
function getLiveURLSegment () {
$liveRecord = Versioned :: get_one_by_stage (
'SiteTree' ,
'Live' ,
" \" SiteTree \" . \" ID \" = $this->ID "
);
return ( $liveRecord ) ? $liveRecord -> URLSegment : null ;
}
2009-10-16 00:40:06 +02:00
/**
* Rewrite a file URL on this page , after its been renamed .
* Triggers the onRenameLinkedAsset action on extensions .
*/
function rewriteFileURL ( $old , $new ) {
2010-04-12 03:47:19 +02:00
$fields = $this -> inheritedDatabaseFields ();
2010-04-12 04:42:51 +02:00
// Update the content without actually creating a new version
foreach ( array ( " SiteTree_Live " , " SiteTree " ) as $table ) {
2009-10-16 00:40:06 +02:00
// Published site
2010-04-12 04:42:51 +02:00
$published = DB :: query ( " SELECT * FROM \" $table\ " WHERE \ " ID \" = $this->ID " ) -> record ();
2009-10-16 00:40:06 +02:00
$origPublished = $published ;
2010-04-12 04:42:51 +02:00
foreach ( $fields as $fieldName => $fieldType ) {
if ( $fieldType != 'HTMLText' ) continue ;
// TODO: This doesn't work for HTMLText fields on other tables.
if ( isset ( $published [ $fieldName ])) {
$published [ $fieldName ] = str_replace ( $old , $new , $published [ $fieldName ], $numReplaced );
if ( $numReplaced ) {
DB :: query ( " UPDATE \" $table\ " SET \ " $fieldName\ " = ' "
. Convert :: raw2sql ( $published [ $fieldName ]) . " ' WHERE \" ID \" = $this->ID " );
// Tell static caching to update itself
if ( $table == 'SiteTree_Live' ) {
$publishedClass = $origPublished [ 'ClassName' ];
$origPublishedObj = new $publishedClass ( $origPublished );
$this -> extend ( 'onRenameLinkedAsset' , $origPublishedObj );
}
}
2010-04-12 03:49:50 +02:00
}
2009-10-16 00:40:06 +02:00
}
}
}
2010-10-04 06:52:46 +02:00
/**
* Returns the pages that depend on this page .
* This includes virtual pages , pages that link to it , etc .
*
* @ param $includeVirtuals Set to false to exlcude virtual pages .
*/
function DependentPages ( $includeVirtuals = true ) {
if ( is_callable ( 'Subsite::disable_subsite_filter' )) Subsite :: disable_subsite_filter ( true );
// Content links
$items = $this -> BackLinkTracking ();
if ( ! $items ) $items = new DataObjectSet ();
else foreach ( $items as $item ) $item -> DependentLinkType = 'Content link' ;
// Virtual pages
if ( $includeVirtuals ) {
$virtuals = $this -> VirtualPages ();
if ( $virtuals ) {
foreach ( $virtuals as $item ) $item -> DependentLinkType = 'Virtual page' ;
$items -> merge ( $virtuals );
}
}
// Redirector pages
2010-10-15 02:33:48 +02:00
$redirectors = DataObject :: get ( " RedirectorPage " , " \" RedirectorPage \" . \" RedirectionType \" = 'Internal' AND \" LinkToID \" = $this->ID " );
2010-10-04 06:52:46 +02:00
if ( $redirectors ) {
foreach ( $redirectors as $item ) $item -> DependentLinkType = 'Redirector page' ;
$items -> merge ( $redirectors );
}
if ( is_callable ( 'Subsite::disable_subsite_filter' )) Subsite :: disable_subsite_filter ( false );
return $items ;
}
/**
* Return the number of { @ link DependentPages ()}
*
* @ param $includeVirtuals Set to false to exlcude virtual pages .
*/
function DependentPagesCount ( $includeVirtuals = true ) {
$links = DB :: query ( " SELECT COUNT(*) FROM \" SiteTree_LinkTracking \"
INNER JOIN \ " SiteTree \" ON \" SiteTree \" . \" ID \" = \" SiteTree_LinkTracking \" . \" SiteTreeID \"
WHERE \ " ChildID \" = $this->ID " ) -> value ();
if ( $includeVirtuals ) {
$virtuals = DB :: query ( " SELECT COUNT(*) FROM \" VirtualPage \"
INNER JOIN \ " SiteTree \" ON \" SiteTree \" . \" ID \" = \" VirtualPage \" . \" ID \"
WHERE \ " CopyContentFromID \" = $this->ID " ) -> value ();
} else {
$virtuals = 0 ;
}
$redirectors = DB :: query ( " SELECT COUNT(*) FROM \" RedirectorPage \"
INNER JOIN \ " SiteTree \" ON \" SiteTree \" . \" ID \" = \" RedirectorPage \" . \" ID \"
WHERE \ " RedirectionType \" = 'Internal' AND \" LinkToID \" = $this->ID " ) -> value ();
return 0 + $links + $virtuals + $redirectors ;
}
/**
* Return all virtual pages that link to this page
*/
function VirtualPages () {
if ( ! $this -> ID ) return null ;
if ( class_exists ( 'Subsite' )) {
return Subsite :: get_from_all_subsites ( 'VirtualPage' , " \" CopyContentFromID \" = " . ( int ) $this -> ID );
} else {
return DataObject :: get ( 'VirtualPage' , " \" CopyContentFromID \" = " . ( int ) $this -> ID );
}
}
2009-10-16 00:40:06 +02:00
2007-07-19 12:40:28 +02:00
/**
* Returns a FieldSet with which to create the CMS editing form .
2007-09-15 02:03:12 +02:00
*
2007-07-19 12:40:28 +02:00
* You can override this in your child classes to add extra fields - first
2007-09-15 02:03:12 +02:00
* get the parent fields using parent :: getCMSFields (), then use
* addFieldToTab () on the FieldSet .
*
2007-07-19 12:40:28 +02:00
* @ return FieldSet The fields to be displayed in the CMS .
*/
function getCMSFields () {
require_once ( " forms/Form.php " );
2009-11-21 03:29:59 +01:00
Requirements :: javascript ( SAPPHIRE_DIR . " /thirdparty/prototype/prototype.js " );
Requirements :: javascript ( SAPPHIRE_DIR . " /thirdparty/behaviour/behaviour.js " );
2009-03-10 23:08:52 +01:00
Requirements :: add_i18n_javascript ( SAPPHIRE_DIR . '/javascript/lang' );
2008-11-12 05:31:33 +01:00
Requirements :: javascript ( SAPPHIRE_DIR . '/javascript/UpdateURL.js' );
2007-07-19 12:40:28 +02:00
// Status / message
// Create a status message for multiple parents
if ( $this -> ID && is_numeric ( $this -> ID )) {
2010-10-04 06:52:46 +02:00
$linkedPages = $this -> VirtualPages ();
2007-07-19 12:40:28 +02:00
}
2009-05-18 01:15:31 +02:00
$parentPageLinks = array ();
2007-07-19 12:40:28 +02:00
if ( isset ( $linkedPages )) {
foreach ( $linkedPages as $linkedPage ) {
$parentPage = $linkedPage -> Parent ;
2009-05-18 00:59:48 +02:00
if ( $parentPage ) {
if ( $parentPage -> ID ) {
$parentPageLinks [] = " <a class= \" cmsEditlink \" href= \" admin/show/ $linkedPage->ID\ " > { $parentPage -> Title } </ a > " ;
} else {
$parentPageLinks [] = " <a class= \" cmsEditlink \" href= \" admin/show/ $linkedPage->ID\ " > " .
_t ( 'SiteTree.TOPLEVEL' , 'Site Content (Top Level)' ) .
" </a> " ;
}
2007-07-19 12:40:28 +02:00
}
}
2007-09-14 21:17:37 +02:00
2007-07-19 12:40:28 +02:00
$lastParent = array_pop ( $parentPageLinks );
$parentList = " ' $lastParent ' " ;
2007-09-14 21:17:37 +02:00
2007-09-15 02:03:12 +02:00
if ( count ( $parentPageLinks ) > 0 ) {
$parentList = " ' " . implode ( " ', ' " , $parentPageLinks ) . " ' and "
. $parentList ;
2007-07-19 12:40:28 +02:00
}
2007-09-14 21:17:37 +02:00
2007-10-25 04:47:45 +02:00
$statusMessage [] = sprintf (
_t ( 'SiteTree.APPEARSVIRTUALPAGES' , " This content also appears on the virtual pages in the %s sections. " ),
$parentList
);
2007-07-19 12:40:28 +02:00
}
2007-09-14 21:17:37 +02:00
2007-07-19 12:40:28 +02:00
if ( $this -> HasBrokenLink || $this -> HasBrokenFile ) {
2007-10-25 04:47:45 +02:00
$statusMessage [] = _t ( 'SiteTree.HASBROKENLINKS' , " This page has broken links. " );
2007-07-19 12:40:28 +02:00
}
2007-11-08 05:23:33 +01:00
2010-10-04 06:52:46 +02:00
$dependentNote = '' ;
$dependentTable = new LiteralField ( 'DependentNote' , '<p></p>' );
2009-02-02 00:49:53 +01:00
// Create a table for showing pages linked to this one
2010-10-04 06:52:46 +02:00
$dependentPagesCount = $this -> DependentPagesCount ();
if ( $dependentPagesCount ) {
$dependentColumns = array (
'Title' => 'Title' ,
'Subsite.Title' => 'Subsite' ,
'AbsoluteLink' => 'URL' ,
'DependentLinkType' => 'Link type' ,
);
if ( ! class_exists ( 'Subsite' )) unset ( $dependentColumns [ 'Subsite.Title' ]);
$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>' );
$dependentTable = new TableListField (
'DependentPages' ,
2009-02-02 00:49:53 +01:00
'SiteTree' ,
2010-10-04 06:52:46 +02:00
$dependentColumns
2009-02-02 00:49:53 +01:00
);
2010-10-04 06:52:46 +02:00
$dependentTable -> setCustomSourceItems ( $this -> DependentPages ());
$dependentTable -> setFieldFormatting ( array (
2010-04-13 04:00:41 +02:00
'Title' => '<a href=\"admin/show/$ID\">$Title</a>' ,
'AbsoluteLink' => '<a href=\"$value\">$value</a>' ,
2009-02-02 00:49:53 +01:00
));
2010-10-04 06:52:46 +02:00
$dependentTable -> setPermissions ( array (
2009-02-02 00:49:53 +01:00
'show' ,
'export'
));
}
2007-09-14 21:17:37 +02:00
// Lay out the fields
2007-07-19 12:40:28 +02:00
$fields = new FieldSet (
2010-04-13 04:29:51 +02:00
$rootTab = new TabSet ( " Root " ,
2007-10-25 04:47:45 +02:00
$tabContent = new TabSet ( 'Content' ,
$tabMain = new Tab ( 'Main' ,
2008-11-02 21:04:10 +01:00
new TextField ( " Title " , $this -> fieldLabel ( 'Title' )),
new TextField ( " MenuTitle " , $this -> fieldLabel ( 'MenuTitle' )),
2010-04-12 03:47:04 +02:00
new HtmlEditorField ( " Content " , _t ( 'SiteTree.HTMLEDITORTITLE' , " Content " , PR_MEDIUM , 'HTML editor title' ))
2007-07-19 12:40:28 +02:00
),
2008-12-04 23:38:32 +01:00
$tabMeta = new Tab ( 'Metadata' ,
2007-10-25 04:47:45 +02:00
new FieldGroup ( _t ( 'SiteTree.URL' , " URL " ),
2009-10-11 02:07:20 +02:00
new LabelField ( 'BaseUrlLabel' , Controller :: join_links (
Director :: absoluteBaseURL (),
2009-12-16 06:49:39 +01:00
( self :: nested_urls () && $this -> ParentID ? $this -> Parent () -> RelativeLink ( true ) : null )
2009-10-11 02:07:20 +02:00
)),
2009-11-21 03:31:42 +01:00
new TextField ( " URLSegment " , " URLSegment " ),
2008-10-16 15:26:50 +02:00
new LabelField ( 'TrailingSlashLabel' , " / " )
2007-07-19 12:40:28 +02:00
),
2009-10-11 02:07:20 +02:00
new LiteralField ( 'LinkChangeNote' , self :: nested_urls () && count ( $this -> Children ()) ?
'<p>' . $this -> fieldLabel ( 'LinkChangeNote' ) . '</p>' : null
),
2008-11-02 21:04:10 +01:00
new HeaderField ( 'MetaTagsHeader' , $this -> fieldLabel ( 'MetaTagsHeader' )),
new TextField ( " MetaTitle " , $this -> fieldLabel ( 'MetaTitle' )),
2009-05-22 02:53:53 +02:00
new TextareaField ( " MetaKeywords " , $this -> fieldLabel ( 'MetaKeywords' ), 1 ),
2008-11-02 21:04:10 +01:00
new TextareaField ( " MetaDescription " , $this -> fieldLabel ( 'MetaDescription' )),
2008-11-24 23:04:32 +01:00
new TextareaField ( " ExtraMeta " , $this -> fieldLabel ( 'ExtraMeta' ))
2007-07-19 12:40:28 +02:00
)
),
2007-10-25 04:47:45 +02:00
$tabBehaviour = new Tab ( 'Behaviour' ,
new DropdownField (
" ClassName " ,
2008-11-02 21:04:10 +01:00
$this -> fieldLabel ( 'ClassName' ),
2007-10-25 04:47:45 +02:00
$this -> getClassDropdown ()
),
2009-11-21 03:31:33 +01:00
$parentIdComposite = new CompositeField (
new OptionsetField ( " ParentType " , " Page location " , array (
" root " => _t ( " SiteTree.PARENTTYPE_ROOT " , " Top-level page " ),
" subpage " => _t ( " SiteTree.PARENTTYPE_SUBPAGE " , " Sub-page underneath a parent page (choose below) " ),
)),
2010-04-14 07:36:33 +02:00
$parentIDField = new TreeDropdownField ( " ParentID " , $this -> fieldLabel ( 'ParentID' ), 'SiteTree' )
2009-11-21 03:31:33 +01:00
),
2008-11-02 21:04:10 +01:00
new CheckboxField ( " ShowInMenus " , $this -> fieldLabel ( 'ShowInMenus' )),
new CheckboxField ( " ShowInSearch " , $this -> fieldLabel ( 'ShowInSearch' )),
2007-07-19 12:40:28 +02:00
/*, new TreeMultiselectField("MultipleParents", "Page appears within", "SiteTree")*/
2008-11-02 21:04:10 +01:00
new CheckboxField ( " ProvideComments " , $this -> fieldLabel ( 'ProvideComments' )),
2007-10-25 04:47:45 +02:00
new LiteralField (
2009-05-20 01:16:20 +02:00
" HomepageForDomainInfo " ,
2007-10-25 04:47:45 +02:00
" <p> " .
_t ( 'SiteTree.NOTEUSEASHOMEPAGE' ,
" Use this page as the 'home page' for the following domains:
( separate multiple domains with commas ) " ) .
" </p> "
),
new TextField (
" HomepageForDomain " ,
_t ( 'SiteTree.HOMEPAGEFORDOMAIN' , " Domain(s) " , PR_MEDIUM , 'Listing domains that should be used as homepage' )
)
2007-07-19 12:40:28 +02:00
),
2008-02-25 03:10:37 +01:00
$tabToDo = new Tab ( $this -> ToDo ? 'To-do **' : 'To-do' ,
new LiteralField ( " ToDoHelp " , _t ( 'SiteTree.TODOHELP' , " <p>You can use this to keep track of work that needs to be done to the content of your site. To see all your pages with to do information, open the 'Site Reports' window on the left and select 'To Do'</p> " )),
new TextareaField ( " ToDo " , " " )
),
2010-10-04 06:52:46 +02:00
$tabDependent = new Tab ( 'Dependent' ,
$dependentNote ,
$dependentTable
2007-07-24 05:43:21 +02:00
),
2007-10-25 04:47:45 +02:00
$tabAccess = new Tab ( 'Access' ,
2008-11-03 15:52:35 +01:00
new HeaderField ( 'WhoCanViewHeader' , _t ( 'SiteTree.ACCESSHEADER' , " Who can view this page? " ), 2 ),
$viewersOptionsField = new OptionsetField (
" CanViewType " ,
" "
2007-10-25 04:47:45 +02:00
),
2008-12-04 23:38:32 +01:00
$viewerGroupsField = new TreeMultiselectField ( " ViewerGroups " , $this -> fieldLabel ( 'ViewerGroups' )),
2008-11-03 15:52:35 +01:00
new HeaderField ( 'WhoCanEditHeader' , _t ( 'SiteTree.EDITHEADER' , " Who can edit this page? " ), 2 ),
$editorsOptionsField = new OptionsetField (
" CanEditType " ,
" "
2007-10-25 04:47:45 +02:00
),
2008-12-04 23:38:32 +01:00
$editorGroupsField = new TreeMultiselectField ( " EditorGroups " , $this -> fieldLabel ( 'EditorGroups' ))
2007-07-19 12:40:28 +02:00
)
2007-11-01 23:31:27 +01:00
)
2007-07-19 12:40:28 +02:00
);
2010-04-14 07:36:33 +02:00
/*
* This filter ensures that the ParentID dropdown selection does not show this node ,
* or its descendents , as this causes vanishing bugs .
*/
$parentIDField -> setFilterFunction ( create_function ( '$node' , " return \$ node->ID != { $this -> ID } ; " ));
2008-12-17 01:40:24 +01:00
2010-10-04 06:52:46 +02:00
// Conditional dependent pages tab
if ( $dependentPagesCount ) $tabDependent -> setTitle ( _t ( 'SiteTree.TABDEPENDENT' , " Dependent pages " ) . " ( $dependentPagesCount ) " );
else $fields -> removeFieldFromTab ( 'Root' , 'Dependent' );
2010-04-13 04:29:51 +02:00
2010-04-12 05:17:25 +02:00
// Make page location fields read-only if the user doesn't have the appropriate permission
if ( ! Permission :: check ( " SITETREE_REORGANISE " )) {
$fields -> makeFieldReadonly ( 'ParentType' );
if ( $this -> ParentType == 'root' ) {
$fields -> removeByName ( 'ParentID' );
} else {
$fields -> makeFieldReadonly ( 'ParentID' );
}
}
2008-11-03 15:52:35 +01:00
$viewersOptionsSource = array ();
2010-04-14 06:42:07 +02:00
$viewersOptionsSource [ " Inherit " ] = _t ( 'SiteTree.INHERIT' , " Inherit from parent page " );
2008-11-03 15:52:35 +01:00
$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 );
$editorsOptionsSource = array ();
2010-04-14 06:42:07 +02:00
$editorsOptionsSource [ " Inherit " ] = _t ( 'SiteTree.INHERIT' , " Inherit from parent page " );
2008-11-03 15:52:35 +01:00
$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 );
2009-11-21 03:31:33 +01:00
$parentIdComposite -> addExtraClass ( 'parentTypeSelector' );
2009-06-17 13:36:49 +02:00
if ( ! Permission :: check ( 'SITETREE_GRANT_ACCESS' )) {
$fields -> makeFieldReadonly ( $viewersOptionsField );
2009-10-16 00:21:41 +02:00
if ( $this -> CanViewType == 'OnlyTheseUsers' ) {
$fields -> makeFieldReadonly ( $viewerGroupsField );
} else {
$fields -> removeByName ( 'ViewerGroups' );
}
2009-06-17 13:36:49 +02:00
$fields -> makeFieldReadonly ( $editorsOptionsField );
2009-10-16 00:21:41 +02:00
if ( $this -> CanEditType == 'OnlyTheseUsers' ) {
$fields -> makeFieldReadonly ( $editorGroupsField );
} else {
$fields -> removeByName ( 'EditorGroups' );
}
2009-06-17 13:36:49 +02:00
}
2008-11-03 15:52:35 +01:00
2007-10-25 04:47:45 +02:00
$tabContent -> setTitle ( _t ( 'SiteTree.TABCONTENT' , " Content " ));
$tabMain -> setTitle ( _t ( 'SiteTree.TABMAIN' , " Main " ));
2008-12-04 23:38:32 +01:00
$tabMeta -> setTitle ( _t ( 'SiteTree.TABMETA' , " Metadata " ));
2007-10-25 04:47:45 +02:00
$tabBehaviour -> setTitle ( _t ( 'SiteTree.TABBEHAVIOUR' , " Behaviour " ));
$tabAccess -> setTitle ( _t ( 'SiteTree.TABACCESS' , " Access " ));
2008-11-10 04:51:35 +01:00
if ( self :: $runCMSFieldsExtensions ) {
$this -> extend ( 'updateCMSFields' , $fields );
}
2007-07-24 05:43:21 +02:00
2007-07-19 12:40:28 +02:00
return $fields ;
}
2008-11-02 21:04:10 +01:00
2009-04-29 02:07:39 +02:00
/**
*
* @ param boolean $includerelations a boolean value to indicate if the labels returned include relation fields
*
*/
function fieldLabels ( $includerelations = true ) {
$labels = parent :: fieldLabels ( $includerelations );
2008-11-02 21:04:10 +01:00
$labels [ 'Title' ] = _t ( 'SiteTree.PAGETITLE' , " Page name " );
$labels [ 'MenuTitle' ] = _t ( 'SiteTree.MENUTITLE' , " Navigation label " );
$labels [ 'MetaTagsHeader' ] = _t ( 'SiteTree.METAHEADER' , " Search Engine Meta-tags " );
$labels [ 'MetaTitle' ] = _t ( 'SiteTree.METATITLE' , " Title " );
$labels [ 'MetaDescription' ] = _t ( 'SiteTree.METADESC' , " Description " );
$labels [ 'MetaKeywords' ] = _t ( 'SiteTree.METAKEYWORDS' , " Keywords " );
$labels [ 'ExtraMeta' ] = _t ( 'SiteTree.METAEXTRA' , " Custom Meta Tags " );
$labels [ 'ClassName' ] = _t ( 'SiteTree.PAGETYPE' , " Page type " , PR_MEDIUM , 'Classname of a page object' );
2009-05-01 05:49:34 +02:00
$labels [ 'ParentType' ] = _t ( 'SiteTree.PARENTTYPE' , " Page location " , PR_MEDIUM );
$labels [ 'ParentID' ] = _t ( 'SiteTree.PARENTID' , " Parent page " , PR_MEDIUM );
2008-11-02 21:04:10 +01: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? " );
2009-05-14 07:26:47 +02:00
$labels [ 'ViewerGroups' ] = _t ( 'SiteTree.VIEWERGROUPS' , " Viewer Groups " );
$labels [ 'EditorGroups' ] = _t ( 'SiteTree.EDITORGROUPS' , " Editor Groups " );
2008-11-02 21:04:10 +01:00
$labels [ 'URLSegment' ] = _t ( 'SiteTree.URLSegment' , 'URL Segment' , PR_MEDIUM , 'URL for this page' );
$labels [ 'Content' ] = _t ( 'SiteTree.Content' , 'Content' , PR_MEDIUM , 'Main HTML Content for a page' );
$labels [ 'HomepageForDomain' ] = _t ( 'SiteTree.HomepageForDomain' , 'Hompage for this domain' );
2008-11-03 15:52:35 +01:00
$labels [ 'CanViewType' ] = _t ( 'SiteTree.Viewers' , 'Viewers Groups' );
$labels [ 'CanEditType' ] = _t ( 'SiteTree.Editors' , 'Editors Groups' );
2008-11-02 21:04:10 +01:00
$labels [ 'ToDo' ] = _t ( 'SiteTree.ToDo' , 'Todo Notes' );
$labels [ 'Comments' ] = _t ( 'SiteTree.Comments' , 'Comments' );
2009-10-11 02:07:20 +02:00
$labels [ 'LinkChangeNote' ] = _t (
'SiteTree.LINKCHANGENOTE' , 'Changing this page\'s link will also affect the links of all child pages.'
);
2009-04-29 02:07:39 +02:00
if ( $includerelations ){
$labels [ 'Parent' ] = _t ( 'SiteTree.has_one_Parent' , 'Parent Page' , PR_MEDIUM , 'The parent page in the site hierarchy' );
$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' );
}
2008-11-02 21:04:10 +01:00
return $labels ;
}
2007-07-19 12:40:28 +02:00
2008-08-12 04:51:33 +02:00
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2007-09-15 02:03:12 +02:00
2007-07-19 12:40:28 +02:00
/**
* Get the actions available in the CMS for this page - eg Save , Publish .
2008-12-04 23:38:32 +01:00
* @ return FieldSet The available actions for this page .
2007-07-19 12:40:28 +02:00
*/
function getCMSActions () {
2008-12-04 23:38:32 +01:00
$actions = new FieldSet ();
2007-09-15 02:03:12 +02:00
2008-12-17 23:41:07 +01:00
if ( $this -> isPublished () && $this -> canPublish () && ! $this -> IsDeletedFromStage ) {
2008-12-04 23:38:32 +01:00
// "unpublish"
2007-10-25 04:47:45 +02:00
$unpublish = FormAction :: create ( 'unpublish' , _t ( 'SiteTree.BUTTONUNPUBLISH' , 'Unpublish' ), 'delete' );
$unpublish -> describe ( _t ( 'SiteTree.BUTTONUNPUBLISHDESC' , " Remove this page from the published site " ));
2007-10-19 03:42:14 +02:00
$unpublish -> addExtraClass ( 'delete' );
2008-12-04 23:38:32 +01:00
$actions -> push ( $unpublish );
2007-07-19 12:40:28 +02:00
}
2007-09-15 02:03:12 +02:00
2008-12-17 23:41:07 +01:00
if ( $this -> stagesDiffer ( 'Stage' , 'Live' ) && ! $this -> IsDeletedFromStage ) {
2007-07-19 12:40:28 +02:00
if ( $this -> isPublished () && $this -> canEdit ()) {
2008-12-04 23:38:32 +01:00
// "rollback"
2007-10-25 04:47:45 +02:00
$rollback = FormAction :: create ( 'rollback' , _t ( 'SiteTree.BUTTONCANCELDRAFT' , 'Cancel draft changes' ), 'delete' );
$rollback -> describe ( _t ( 'SiteTree.BUTTONCANCELDRAFTDESC' , " Delete your draft and revert to the currently published page " ));
2007-10-19 03:42:14 +02:00
$rollback -> addExtraClass ( 'delete' );
2008-12-04 23:38:32 +01:00
$actions -> push ( $rollback );
2007-07-19 12:40:28 +02:00
}
}
2009-05-25 08:59:21 +02:00
if ( $this -> canEdit ()) {
if ( $this -> IsDeletedFromStage ) {
2009-05-01 05:49:34 +02:00
if ( $this -> ExistsOnLive ) {
// "restore"
$actions -> push ( new FormAction ( 'revert' , _t ( 'CMSMain.RESTORE' , 'Restore' )));
2010-10-13 03:41:14 +02:00
if ( $this -> canDelete () && $this -> canDeleteFromLive ()) {
2009-10-17 07:11:23 +02:00
// "delete from live"
$actions -> push ( new FormAction ( 'deletefromlive' , _t ( 'CMSMain.DELETEFP' , 'Delete from the published site' )));
}
2009-05-01 05:49:34 +02:00
} else {
// "restore"
$actions -> push ( new FormAction ( 'restore' , _t ( 'CMSMain.RESTORE' , 'Restore' )));
}
2009-05-25 08:59:21 +02:00
} else {
2009-10-17 07:11:23 +02:00
if ( $this -> canDelete ()) {
// "delete"
$actions -> push ( $deleteAction = new FormAction ( 'delete' , _t ( 'CMSMain.DELETE' , 'Delete from the draft site' )));
$deleteAction -> addExtraClass ( 'delete' );
}
2009-05-25 08:59:21 +02:00
2008-12-04 23:38:32 +01:00
// "save"
$actions -> push ( new FormAction ( 'save' , _t ( 'CMSMain.SAVE' , 'Save' )));
}
2008-09-01 06:45:38 +02:00
}
2008-12-17 01:40:24 +01:00
2008-12-17 23:41:07 +01:00
if ( $this -> canPublish () && ! $this -> IsDeletedFromStage ) {
2008-12-04 23:38:32 +01:00
// "publish"
$actions -> push ( new FormAction ( 'publish' , _t ( 'SiteTree.BUTTONSAVEPUBLISH' , 'Save and Publish' )));
}
2008-12-17 01:40:24 +01:00
2008-12-04 23:38:32 +01:00
// getCMSActions() can be extended with updateCMSActions() on a decorator
2008-09-01 06:45:38 +02:00
$this -> extend ( 'updateCMSActions' , $actions );
2008-12-04 23:38:32 +01:00
return $actions ;
2007-07-19 12:40:28 +02:00
}
2008-08-12 04:51:33 +02:00
/**
2008-11-24 23:04:32 +01:00
* Publish this page .
*
* @ uses SiteTreeDecorator -> onBeforePublish ()
* @ uses SiteTreeDecorator -> onAfterPublish ()
2008-08-12 04:51:33 +02:00
*/
function doPublish () {
2009-10-16 00:24:41 +02:00
if ( ! $this -> canPublish ()) return false ;
2008-11-23 01:31:06 +01:00
$original = Versioned :: get_one_by_stage ( " SiteTree " , " Live " , " \" SiteTree \" . \" ID \" = $this->ID " );
2008-10-08 05:36:38 +02:00
if ( ! $original ) $original = new SiteTree ();
2008-08-12 04:51:33 +02:00
// Handle activities undertaken by decorators
2010-10-13 05:35:19 +02:00
$this -> invokeWithExtensions ( 'onBeforePublish' , $original );
2009-11-21 03:32:03 +01:00
2008-08-12 04:51:33 +02:00
//$this->PublishedByID = Member::currentUser()->ID;
$this -> write ();
$this -> publish ( " Stage " , " Live " );
2010-04-13 01:11:58 +02:00
DB :: 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 \" = " . sprintf ( '%d' , $this -> ParentID ) );
2008-11-24 20:28:46 +01:00
2009-05-04 03:20:12 +02:00
// Publish any virtual pages that might need publishing
2010-10-04 06:52:46 +02:00
$linkedPages = $this -> VirtualPages ();
2009-05-04 03:20:12 +02:00
if ( $linkedPages ) foreach ( $linkedPages as $page ) {
$page -> copyFrom ( $page -> CopyContentFrom ());
2010-04-12 05:14:36 +02:00
$page -> write ();
if ( $page -> ExistsOnLive ) $page -> doPublish ();
2009-05-04 03:20:12 +02:00
}
2009-10-15 23:46:13 +02:00
2010-10-04 06:52:46 +02:00
// Need to update pages linking to this one as no longer broken, on the live site
$origMode = Versioned :: get_reading_mode ();
Versioned :: reading_stage ( 'Live' );
foreach ( $this -> DependentPages ( false ) as $page ) {
// $page->write() calls syncLinkTracking, which does all the hard work for us.
$page -> write ();
2009-10-15 23:46:13 +02:00
}
2010-10-04 06:52:46 +02:00
Versioned :: set_reading_mode ( $origMode );
2009-10-15 23:46:13 +02:00
2010-04-14 05:50:21 +02:00
// Check to write CMS homepage map.
$usingStaticPublishing = false ;
foreach ( ClassInfo :: subclassesFor ( 'StaticPublisher' ) as $class ) if ( $this -> hasExtension ( $class )) $usingStaticPublishing = true ;
2009-05-04 03:20:12 +02:00
2010-04-14 05:50:21 +02:00
// NOTE: if you change the path here, you must also change it in sapphire/static-main.php
if ( self :: $write_homepage_map ) {
2010-04-14 05:50:41 +02:00
if ( $usingStaticPublishing && $map = SiteTree :: generate_homepage_domain_map ()) {
2010-04-14 05:50:21 +02:00
@ file_put_contents ( BASE_PATH . '/' . ASSETS_DIR . '/_homepage-map.php' , " <?php \n \$ homepageMap = " . var_export ( $map , true ) . " ; ?> " );
} else { if ( file_exists ( BASE_PATH . '/' . ASSETS_DIR . '/_homepage-map.php' )) unlink ( BASE_PATH . '/' . ASSETS_DIR . '/_homepage-map.php' ); }
}
2008-08-12 04:51:33 +02:00
// Handle activities undertaken by decorators
2010-10-13 05:35:19 +02:00
$this -> invokeWithExtensions ( 'onAfterPublish' , $original );
2009-10-16 00:40:06 +02:00
return true ;
2008-08-12 04:51:33 +02:00
}
2008-10-16 05:21:58 +02:00
2010-04-14 05:50:21 +02:00
static function generate_homepage_domain_map () {
2010-05-25 06:22:50 +02:00
$domainSpecificHomepages = Versioned :: get_by_stage ( 'Page' , 'Live' , " \" HomepageForDomain \" != '' " , " \" URLSegment \" ASC " );
2010-04-14 05:50:21 +02:00
if ( ! $domainSpecificHomepages ) return false ;
$map = array ();
foreach ( $domainSpecificHomepages -> map ( 'URLSegment' , 'HomepageForDomain' ) as $url => $domains ) {
foreach ( explode ( ',' , $domains ) as $domain ) $map [ $domain ] = $url ;
}
return $map ;
}
2008-10-16 05:21:58 +02:00
/**
* Unpublish this page - remove it from the live site
2008-11-24 23:04:32 +01:00
*
* @ uses SiteTreeDecorator -> onBeforeUnpublish ()
* @ uses SiteTreeDecorator -> onAfterUnpublish ()
2008-10-16 05:21:58 +02:00
*/
function doUnpublish () {
2010-10-12 23:42:25 +02:00
if ( ! $this -> canDeleteFromLive ()) return false ;
2010-10-04 06:52:46 +02:00
if ( ! $this -> ID ) return false ;
2009-10-16 00:24:41 +02:00
2008-11-24 23:04:32 +01:00
$this -> extend ( 'onBeforeUnpublish' );
2010-10-04 06:52:46 +02:00
$origStage = Versioned :: current_stage ();
Versioned :: reading_stage ( 'Live' );
2008-10-16 05:21:58 +02:00
2010-10-04 06:52:46 +02:00
// We should only unpublish virtualpages that exist on live
$virtualPages = $this -> VirtualPages ();
2010-04-13 04:17:11 +02:00
2010-10-04 06:52:46 +02:00
// This way our ID won't be unset
$clone = clone $this ;
$clone -> delete ();
// Rewrite backlinks
$dependentPages = $this -> DependentPages ( false );
if ( $dependentPages ) foreach ( $dependentPages as $page ) {
2010-04-13 04:17:11 +02:00
// $page->write() calls syncLinkTracking, which does all the hard work for us.
$page -> write ();
}
2010-10-04 06:52:46 +02:00
Versioned :: reading_stage ( $origStage );
// Unpublish any published virtual pages
if ( $virtualPages ) foreach ( $virtualPages as $vp ) $vp -> doUnpublish ();
// If we're on the draft site, then we can update the status.
// Otherwise, these lines will resurrect an inappropriate record
2010-10-12 23:49:52 +02:00
if ( DB :: query ( " SELECT \" ID \" FROM \" SiteTree \" WHERE \" ID \" = $this->ID " ) -> value ()
&& Versioned :: current_stage () != 'Live' ) {
2010-10-04 06:52:46 +02:00
$this -> Status = " Unpublished " ;
$this -> write ();
}
2008-11-24 23:04:32 +01:00
$this -> extend ( 'onAfterUnpublish' );
2010-10-04 06:52:46 +02:00
2010-10-04 06:48:07 +02:00
return true ;
2008-10-16 05:21:58 +02:00
}
/**
2010-10-13 03:04:38 +02:00
* Roll the draft version of this page to match the published page .
* Caution : Doesn ' t overwrite the object properties with the rolled back version .
*
2008-10-16 05:21:58 +02:00
* @ param $version Either the string 'Live' or a version number
*/
function doRollbackTo ( $version ) {
$this -> publish ( $version , " Stage " , true );
$this -> writeWithoutVersion ();
}
/**
* Revert the draft changes : replace the draft content with the content on live
*/
function doRevertToLive () {
$this -> publish ( " Live " , " Stage " , false );
// Use a clone to get the updates made by $this->publish
$clone = DataObject :: get_by_id ( " SiteTree " , $this -> ID );
$clone -> writeWithoutVersion ();
2010-10-04 06:52:46 +02:00
// Need to update pages linking to this one as no longer broken
foreach ( $this -> DependentPages ( false ) as $page ) {
// $page->write() calls syncLinkTracking, which does all the hard work for us.
$page -> write ();
}
2009-04-29 01:52:15 +02:00
$this -> extend ( 'onAfterRevertToLive' );
2008-10-16 05:21:58 +02:00
}
2009-05-01 05:49:34 +02:00
/**
* Restore the content in the active copy of this SiteTree page to the stage site .
* @ return The SiteTree object .
*/
function doRestoreToStage () {
// if no record can be found on draft stage (meaning it has been "deleted from draft" before),
// create an empty record
2009-09-17 02:08:21 +02:00
if ( ! DB :: query ( " SELECT \" ID \" FROM \" SiteTree \" WHERE \" ID \" = $this->ID " ) -> value ()) {
2009-05-07 07:59:24 +02:00
$conn = DB :: getConn ();
2009-08-08 06:23:05 +02:00
if ( method_exists ( $conn , 'allowPrimaryKeyEditing' )) $conn -> allowPrimaryKeyEditing ( 'SiteTree' , true );
2009-05-07 07:59:24 +02:00
DB :: query ( " INSERT INTO \" SiteTree \" ( \" ID \" ) VALUES ( $this->ID ) " );
2009-08-08 06:23:05 +02:00
if ( method_exists ( $conn , 'allowPrimaryKeyEditing' )) $conn -> allowPrimaryKeyEditing ( 'SiteTree' , false );
2009-05-01 05:49:34 +02:00
}
$oldStage = Versioned :: current_stage ();
Versioned :: reading_stage ( 'Stage' );
$this -> forceChange ();
$this -> writeWithoutVersion ();
$result = DataObject :: get_by_id ( $this -> class , $this -> ID );
2010-10-04 06:52:46 +02: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 ();
}
2009-05-01 05:49:34 +02:00
Versioned :: reading_stage ( $oldStage );
return $result ;
}
2010-04-12 03:57:56 +02:00
2010-10-04 06:52:46 +02:00
/**
* Synonym of { @ link doUnpublish }
*/
function doDeleteFromLive () {
return $this -> doUnpublish ();
2009-05-14 07:26:47 +02:00
}
2007-09-15 02:03:12 +02:00
2007-07-19 12:40:28 +02:00
/**
* Check if this page is new - that is , if it has yet to have been written
* to the database .
2007-09-15 02:03:12 +02:00
*
2007-07-19 12:40:28 +02:00
* @ return boolean True if this page is new .
*/
function isNew () {
/**
2007-09-15 02:03:12 +02: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 .
2007-07-19 12:40:28 +02:00
*/
2008-11-22 04:33:00 +01:00
if ( empty ( $this -> ID )) return true ;
2007-09-14 21:17:37 +02:00
2008-11-22 04:33:00 +01:00
if ( is_numeric ( $this -> ID )) return false ;
2007-09-14 21:17:37 +02:00
2007-07-19 12:40:28 +02:00
return stripos ( $this -> ID , 'new' ) === 0 ;
}
2007-09-14 21:17:37 +02:00
2007-09-15 02:03:12 +02:00
2007-07-19 12:40:28 +02:00
/**
* Check if this page has been published .
2007-09-15 02:03:12 +02:00
*
2007-07-19 12:40:28 +02:00
* @ return boolean True if this page has been published .
*/
function isPublished () {
2007-09-15 02:03:12 +02:00
if ( $this -> isNew ())
return false ;
2008-11-24 00:28:16 +01:00
return ( DB :: query ( " SELECT \" ID \" FROM \" SiteTree_Live \" WHERE \" ID \" = $this->ID " ) -> value ())
2007-09-15 02:03:12 +02:00
? true
: false ;
2007-07-19 12:40:28 +02:00
}
2007-09-14 21:17:37 +02:00
2007-07-19 12:40:28 +02:00
/**
* Get the class dropdown used in the CMS to change the class of a page .
* This returns the list of options in the drop as a Map from class name
* to text in dropdown .
2007-09-15 02:03:12 +02:00
*
2007-07-19 12:40:28 +02:00
* @ return array
*/
2008-10-16 15:00:02 +02:00
protected function getClassDropdown () {
2009-02-02 00:49:53 +01:00
$classes = self :: page_type_classes ();
2008-11-18 02:52:32 +01:00
$currentClass = null ;
2009-02-02 00:49:53 +01:00
$result = array ();
2008-11-24 00:28:16 +01:00
$result = array ();
2007-07-19 12:40:28 +02:00
foreach ( $classes as $class ) {
$instance = singleton ( $class );
2007-07-24 23:53:56 +02:00
if ((( $instance instanceof HiddenClass ) || ! $instance -> canCreate ()) && ( $class != $this -> class )) continue ;
2007-07-19 12:40:28 +02:00
2009-01-05 07:19:48 +01:00
$pageTypeName = $instance -> i18n_singular_name ();
2007-07-19 12:40:28 +02:00
2008-02-25 03:10:37 +01:00
if ( $class == $this -> class ) {
$currentClass = $class ;
2009-01-05 07:19:48 +01:00
$result [ $class ] = $pageTypeName ;
2008-02-25 03:10:37 +01:00
} else {
2009-01-05 07:19:48 +01:00
$translation = _t (
'SiteTree.CHANGETO' ,
'Change to "%s"' ,
PR_MEDIUM ,
" Pagetype selection dropdown with class names "
);
// @todo legacy fix to avoid empty classname dropdowns when translation doesn't include %s
if ( strpos ( $translation , '%s' ) !== FALSE ) {
$result [ $class ] = sprintf (
$translation ,
$pageTypeName
);
} else {
$result [ $class ] = " { $translation } \" { $pageTypeName } \" " ;
}
}
// 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)"
if ( i18n :: get_locale () != 'en_US' ) {
$result [ $class ] = $result [ $class ] . " ( { $class } ) " ;
2008-02-25 03:10:37 +01:00
}
2007-07-19 12:40:28 +02:00
}
2008-02-25 03:10:37 +01:00
// sort alphabetically, and put current on top
asort ( $result );
2009-05-01 05:49:34 +02:00
if ( $currentClass ) {
$currentPageTypeName = $result [ $currentClass ];
unset ( $result [ $currentClass ]);
$result = array_reverse ( $result );
$result [ $currentClass ] = $currentPageTypeName ;
$result = array_reverse ( $result );
}
2008-02-25 03:10:37 +01:00
2007-07-19 12:40:28 +02:00
return $result ;
}
2007-09-14 21:17:37 +02:00
2007-09-15 02:03:12 +02:00
2007-07-19 12:40:28 +02:00
/**
* Returns an array of the class names of classes that are allowed
* to be children of this class .
2007-09-15 02:03:12 +02:00
*
2007-07-19 12:40:28 +02:00
* @ return array
*/
function allowedChildren () {
$candidates = $this -> stat ( 'allowed_children' );
if ( $candidates && $candidates != " none " && $candidates != " SiteTree_root " ) {
foreach ( $candidates as $candidate ) {
if ( substr ( $candidate , 0 , 1 ) == '*' ) {
$allowedChildren [] = substr ( $candidate , 1 );
} else {
$subclasses = ClassInfo :: subclassesFor ( $candidate );
foreach ( $subclasses as $subclass ) {
if ( $subclass != " SiteTree_root " ) $allowedChildren [] = $subclass ;
}
}
}
return $allowedChildren ;
}
}
2007-09-14 21:17:37 +02:00
2007-09-15 02:03:12 +02:00
2007-07-19 12:40:28 +02:00
/**
2007-09-15 02:03:12 +02:00
* Returns the class name of the default class for children of this page .
*
2007-07-19 12:40:28 +02:00
* @ return string
*/
function defaultChild () {
$default = $this -> stat ( 'default_child' );
$allowed = $this -> allowedChildren ();
if ( $allowed ) {
if ( ! $default || ! in_array ( $default , $allowed ))
$default = reset ( $allowed );
return $default ;
}
}
2007-09-14 21:17:37 +02:00
2007-09-15 02:03:12 +02:00
2007-07-19 12:40:28 +02:00
/**
2007-09-15 02:03:12 +02:00
* Returns the class name of the default class for the parent of this
* page .
*
2007-07-19 12:40:28 +02:00
* @ return string
*/
function defaultParent () {
return $this -> stat ( 'default_parent' );
}
2007-09-14 21:17:37 +02:00
/**
2007-07-19 12:40:28 +02: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 .
2007-09-15 02:03:12 +02:00
*
2007-07-19 12:40:28 +02:00
* @ return string
*/
function getMenuTitle (){
if ( $value = $this -> getField ( " MenuTitle " )) {
return $value ;
} else {
return $this -> getField ( " Title " );
}
}
2007-09-14 21:17:37 +02:00
2007-09-15 02:03:12 +02:00
2007-07-19 12:40:28 +02:00
/**
* Set the menu title for this page .
2007-09-15 02:03:12 +02:00
*
2007-07-19 12:40:28 +02:00
* @ param string $value
*/
function setMenuTitle ( $value ) {
if ( $value == $this -> getField ( " Title " )) {
$this -> setField ( " MenuTitle " , null );
} else {
$this -> setField ( " MenuTitle " , $value );
}
}
2007-09-14 21:17:37 +02:00
2007-07-19 12:40:28 +02:00
/**
2007-09-15 02:03:12 +02:00
* TitleWithStatus will return the title in an < ins > , < del > or
* < span class = \ " modified \" > tag depending on its publication status.
*
2007-07-19 12:40:28 +02:00
* @ return string
*/
2009-11-21 03:31:27 +01:00
function getTreeTitle () {
2008-12-17 23:41:07 +01:00
if ( $this -> IsDeletedFromStage ) {
2009-05-01 05:49:34 +02:00
if ( $this -> ExistsOnLive ) {
$tag = " del title= \" " . _t ( 'SiteTree.REMOVEDFROMDRAFT' , 'Removed from draft site' ) . " \" " ;
} else {
$tag = " del class= \" deletedOnLive \" title= \" " . _t ( 'SiteTree.DELETEDPAGE' , 'Deleted page' ) . " \" " ;
}
2008-12-17 23:41:07 +01:00
} elseif ( $this -> IsAddedToStage ) {
$tag = " ins title= \" " . _t ( 'SiteTree.ADDEDTODRAFT' , 'Added to draft site' ) . " \" " ;
} elseif ( $this -> IsModifiedOnStage ) {
$tag = " span title= \" " . _t ( 'SiteTree.MODIFIEDONDRAFT' , 'Modified on draft site' ) . " \" class= \" modified \" " ;
} else {
$tag = '' ;
2007-07-19 12:40:28 +02:00
}
2007-09-14 21:17:37 +02:00
2009-05-26 05:45:12 +02:00
$text = Convert :: raw2xml ( str_replace ( array ( " \n " , " \r " ), " " , $this -> MenuTitle ));
return ( $tag ) ? " < $tag > " . $text . " </ " . strtok ( $tag , ' ' ) . " > " : $text ;
2007-07-19 12:40:28 +02:00
}
2007-09-14 21:17:37 +02:00
2008-02-25 03:10:37 +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 .
*/
public function Level ( $level ) {
$parent = $this ;
$stack = array ( $parent );
while ( $parent = $parent -> Parent ) {
array_unshift ( $stack , $parent );
}
2007-09-15 02:03:12 +02:00
2008-02-25 03:10:37 +01:00
return isset ( $stack [ $level - 1 ]) ? $stack [ $level - 1 ] : null ;
}
2007-07-19 12:40:28 +02:00
/**
* Return the CSS classes to apply to this node in the CMS tree
2007-09-15 02:03:12 +02:00
*
* @ param Controller $controller The controller object that the tree
* appears on
2007-07-19 12:40:28 +02:00
* @ return string
*/
function CMSTreeClasses ( $controller ) {
2009-11-21 03:31:21 +01:00
$classes = sprintf ( 'class-%s' , $this -> class );
2007-09-15 02:03:12 +02:00
if ( $this -> HasBrokenFile || $this -> HasBrokenLink )
$classes .= " BrokenLink " ;
2007-09-14 21:17:37 +02:00
2007-09-15 02:03:12 +02:00
if ( ! $this -> canAddChildren ())
$classes .= " nochildren " ;
2007-09-14 21:17:37 +02:00
2007-09-15 02:03:12 +02:00
if ( $controller -> isCurrentPage ( $this ))
$classes .= " current " ;
2007-09-14 21:17:37 +02:00
2008-02-25 03:10:37 +01:00
if ( ! $this -> canEdit () && ! $this -> canAddChildren ())
$classes .= " disabled " ;
2008-11-18 02:48:37 +01:00
if ( ! $this -> ShowInMenus )
$classes .= " notinmenu " ;
Merged, debugged and enhanced Translatable patches from branches/translatable at r64523, r64523, 64523, thanks wakeless!
API CHANGE Changed Translatable schema from auxilliary tables (SiteTree_lang, SiteTree_lang_Live) to automatically filtered records on the original table (SiteTree, SiteTree_Live), using $Lang and $OriginalID properties. Incompatible update to old schema, migration script is in the works.
API CHANGE Removed Translatable::get_one(), Translatable::write()
ENHANCEMENT Simplified Translatable tree generation by using getSiteTreeFor() in CMSMain->createtranslation()
ENHANCEMENT Added AllChildrenIncludingDeleted(), augmentNumChildrenCountQuery(), augmentAllChildrenIncludingDeleted(), augmentStageChildren() to Translatable class to allow for more stable tree generation.
ENHANCEMENT Moved definition of Translatable schema from augmentDatabase() to Translatable->extraStatics()
ENHANCEMENT Changes to the CMS language selection refresh the whole admin interface instead of the tree only. This way we can add a URL parameter ?lang=<lang> to /admin, which makes the specific language bookmarkable and reloadable. Changes to LangSelector.js
ENHANCEMENT Added fallback to ModelAsController->getNestedController() to fetch page with matching URLSegment but different language in case no page is found in the current language.
ENHANCEMENT Added helper methods to Translatable: getTranslation(), hasTranslation(), isTranslation(), findOriginalIDs()
ENHANCEMENT Getters and setters for Translatable->getOriginalPage() etc.
ENHANCEMENT Hooking Translatable into ModelAsController and ContentController initialization in order to call choose_site_lang()
ENHANCEMENT Simplified Translatable->augmentSQL(), augmentWrite() by not using auxilliary tables
ENHANCEMENT Showing clickable links for Translations in Translatable->updateCMSFields()
BUGFIX Modifying Hierarchy/SiteTree Children getters to accept optional "context" which can be used to set a language explicitly through the $Lang property, rather than implicitly reyling on the static Translatable::current_lang()
BUGFIX Fixed TranslatableTest to work with new datamodel
BUGFIX Temporarily disabled cookie/session selection in Translatable::choose_site_lang() until we have a good test suite for the side effects.
MINOR Added "untranslated" CSS styles to tree nodes and marking them as inactive/grey
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@69959 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-01-10 13:15:30 +01:00
//TODO: Add integration
2009-03-18 17:15:46 +01:00
/*
2009-04-01 18:35:32 +02:00
if ( $this -> hasExtension ( 'Translatable' ) && $controller -> Locale != Translatable :: default_locale () && ! $this -> isTranslation ())
Merged, debugged and enhanced Translatable patches from branches/translatable at r64523, r64523, 64523, thanks wakeless!
API CHANGE Changed Translatable schema from auxilliary tables (SiteTree_lang, SiteTree_lang_Live) to automatically filtered records on the original table (SiteTree, SiteTree_Live), using $Lang and $OriginalID properties. Incompatible update to old schema, migration script is in the works.
API CHANGE Removed Translatable::get_one(), Translatable::write()
ENHANCEMENT Simplified Translatable tree generation by using getSiteTreeFor() in CMSMain->createtranslation()
ENHANCEMENT Added AllChildrenIncludingDeleted(), augmentNumChildrenCountQuery(), augmentAllChildrenIncludingDeleted(), augmentStageChildren() to Translatable class to allow for more stable tree generation.
ENHANCEMENT Moved definition of Translatable schema from augmentDatabase() to Translatable->extraStatics()
ENHANCEMENT Changes to the CMS language selection refresh the whole admin interface instead of the tree only. This way we can add a URL parameter ?lang=<lang> to /admin, which makes the specific language bookmarkable and reloadable. Changes to LangSelector.js
ENHANCEMENT Added fallback to ModelAsController->getNestedController() to fetch page with matching URLSegment but different language in case no page is found in the current language.
ENHANCEMENT Added helper methods to Translatable: getTranslation(), hasTranslation(), isTranslation(), findOriginalIDs()
ENHANCEMENT Getters and setters for Translatable->getOriginalPage() etc.
ENHANCEMENT Hooking Translatable into ModelAsController and ContentController initialization in order to call choose_site_lang()
ENHANCEMENT Simplified Translatable->augmentSQL(), augmentWrite() by not using auxilliary tables
ENHANCEMENT Showing clickable links for Translations in Translatable->updateCMSFields()
BUGFIX Modifying Hierarchy/SiteTree Children getters to accept optional "context" which can be used to set a language explicitly through the $Lang property, rather than implicitly reyling on the static Translatable::current_lang()
BUGFIX Fixed TranslatableTest to work with new datamodel
BUGFIX Temporarily disabled cookie/session selection in Translatable::choose_site_lang() until we have a good test suite for the side effects.
MINOR Added "untranslated" CSS styles to tree nodes and marking them as inactive/grey
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@69959 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-01-10 13:15:30 +01:00
$classes .= " untranslated " ;
2009-03-18 17:15:46 +01:00
*/
2007-09-15 02:03:12 +02:00
$classes .= $this -> markingClasses ();
2007-09-14 21:17:37 +02:00
2007-09-15 02:03:12 +02:00
return $classes ;
}
2008-11-10 04:51:35 +01:00
2008-12-17 23:41:07 +01:00
/**
* Compares current draft with live version ,
* and returns TRUE if no draft version of this page exists ,
* but the page is still published ( after triggering " Delete from draft site " in the CMS ) .
*
* @ return boolean
*/
function getIsDeletedFromStage () {
if ( ! $this -> ID ) return true ;
2009-01-08 00:01:38 +01:00
if ( $this -> isNew ()) return false ;
2008-12-17 23:41:07 +01:00
$stageVersion = Versioned :: get_versionnumber_by_stage ( 'SiteTree' , 'Stage' , $this -> ID );
2009-05-01 05:49:34 +02:00
// Return true for both completely deleted pages and for pages just deleted from stage.
2009-08-27 08:53:28 +02:00
return ! ( $stageVersion );
2009-05-01 05:49:34 +02:00
}
/**
* Return true if this page exists on the live site
*/
function getExistsOnLive () {
return ( bool ) Versioned :: get_versionnumber_by_stage ( 'SiteTree' , 'Live' , $this -> ID );
2008-12-17 23:41:07 +01:00
}
/**
* Compares current draft with live version ,
* and returns TRUE if these versions differ ,
* meaning there have been unpublished changes to the draft site .
*
* @ return boolean
*/
public function getIsModifiedOnStage () {
2009-01-10 12:35:50 +01:00
// new unsaved pages could be never be published
if ( $this -> isNew ()) return false ;
2008-12-17 23:41:07 +01:00
$stageVersion = Versioned :: get_versionnumber_by_stage ( 'SiteTree' , 'Stage' , $this -> ID );
$liveVersion = Versioned :: get_versionnumber_by_stage ( 'SiteTree' , 'Live' , $this -> ID );
return ( $stageVersion != $liveVersion );
}
/**
* Compares current draft with live version ,
* and returns true if no live version exists ,
* meaning the page was never published .
*
* @ return boolean
*/
public function getIsAddedToStage () {
2009-01-10 12:35:50 +01:00
// new unsaved pages could be never be published
if ( $this -> isNew ()) return false ;
2008-12-17 23:41:07 +01:00
$stageVersion = Versioned :: get_versionnumber_by_stage ( 'SiteTree' , 'Stage' , $this -> ID );
$liveVersion = Versioned :: get_versionnumber_by_stage ( 'SiteTree' , 'Live' , $this -> ID );
return ( $stageVersion && ! $liveVersion );
}
2008-11-10 04:51:35 +01:00
/**
* Stops extendCMSFields () being called on getCMSFields () .
* This is useful when you need access to fields added by subclasses
* of SiteTree in a decorator . Call before calling parent :: getCMSFields (),
* and reenable afterwards .
*/
public static function disableCMSFieldsExtensions () {
self :: $runCMSFieldsExtensions = false ;
}
/**
* Reenables extendCMSFields () being called on getCMSFields () after
* it has been disabled by disableCMSFieldsExtensions () .
*/
public static function enableCMSFieldsExtensions () {
self :: $runCMSFieldsExtensions = true ;
}
2008-12-17 01:40:24 +01:00
2008-12-04 23:38:32 +01:00
function providePermissions () {
return array (
2009-10-29 01:55:20 +01:00
'SITETREE_GRANT_ACCESS' => array (
'name' => _t ( 'SiteTree.PERMISSION_GRANTACCESS_DESCRIPTION' , 'Manage access rights for content' ),
2010-05-25 07:02:08 +02:00
'help' => _t ( 'SiteTree.PERMISSION_GRANTACCESS_HELP' , 'Allow setting of page-specific access restrictions in the "Pages" section.' ),
2009-10-30 02:43:34 +01:00
'category' => _t ( 'Permissions.PERMISSIONS_CATEGORY' , 'Roles and access permissions' ),
2009-10-29 01:55:20 +01:00
'sort' => 100
2009-06-08 09:00:37 +02:00
),
2009-10-29 01:55:20 +01:00
'SITETREE_VIEW_ALL' => array (
'name' => _t ( 'SiteTree.VIEW_ALL_DESCRIPTION' , 'View any page' ),
2009-10-30 02:43:34 +01:00
'category' => _t ( 'Permissions.CONTENT_CATEGORY' , 'Content permissions' ),
2009-10-29 01:55:20 +01:00
'sort' => - 100 ,
2010-05-25 07:02:08 +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 Site Content" permission' )
2009-06-08 09:00:37 +02:00
),
2009-10-29 01:55:20 +01:00
'SITETREE_EDIT_ALL' => array (
'name' => _t ( 'SiteTree.EDIT_ALL_DESCRIPTION' , 'Edit any page' ),
2009-10-30 02:43:34 +01:00
'category' => _t ( 'Permissions.CONTENT_CATEGORY' , 'Content permissions' ),
2009-10-29 01:55:20 +01:00
'sort' => - 50 ,
2010-05-25 07:02:08 +02: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 Site Content" permission' )
2009-08-10 00:29:45 +02:00
),
2009-10-29 01:55:20 +01:00
'SITETREE_REORGANISE' => array (
'name' => _t ( 'SiteTree.REORGANISE_DESCRIPTION' , 'Change site structure' ),
2009-10-30 02:43:34 +01:00
'category' => _t ( 'Permissions.CONTENT_CATEGORY' , 'Content permissions' ),
2010-05-25 07:02:08 +02:00
'help' => _t ( 'SiteTree.REORGANISE_HELP' , 'Rearrange pages in the site tree through drag&drop.' ),
2009-10-29 01:55:20 +01:00
'sort' => 100
2010-04-13 03:34:28 +02:00
),
'VIEW_DRAFT_CONTENT' => array (
'name' => _t ( 'SiteTree.VIEW_DRAFT_CONTENT' , 'View draft content' ),
'category' => _t ( 'Permissions.CONTENT_CATEGORY' , 'Content permissions' ),
2010-05-25 07:02:08 +02:00
'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.' ),
2010-04-13 03:34:28 +02:00
'sort' => 100
2009-10-29 01:55:20 +01:00
)
2008-12-04 23:38:32 +01:00
);
}
2009-01-05 07:19:48 +01:00
2009-11-26 23:05:06 +01:00
/**
* Return the translated Singular name
*
* @ return String
*/
2009-01-10 12:35:50 +01:00
function i18n_singular_name () {
2009-11-26 23:05:06 +01:00
return _t ( $this -> class . '.SINGULARNAME' , $this -> singular_name ());
2009-01-10 12:35:50 +01:00
}
2009-01-05 07:19: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 .
*/
function provideI18nEntities () {
$entities = parent :: provideI18nEntities ();
if ( isset ( $entities [ 'Page.SINGULARNAME' ])) $entities [ 'Page.SINGULARNAME' ][ 3 ] = 'sapphire' ;
if ( isset ( $entities [ 'Page.PLURALNAME' ])) $entities [ 'Page.PLURALNAME' ][ 3 ] = 'sapphire' ;
return $entities ;
}
2009-05-01 05:49:34 +02:00
function getParentType () {
return $this -> ParentID == 0 ? 'root' : 'subpage' ;
}
2009-08-27 08:53:28 +02:00
2009-08-27 08:55:32 +02:00
static function reset () {
self :: $cache_permissions = array ();
}
2007-07-19 12:40:28 +02:00
}
2009-06-22 04:42:42 +02:00
2009-10-11 02:06:54 +02:00
?>