2007-08-16 08:38:29 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extension for the SiteTree object to add subsites support
|
|
|
|
*/
|
|
|
|
class SiteTreeSubsites extends DataObjectDecorator {
|
|
|
|
static $template_variables = array(
|
|
|
|
'((Company Name))' => 'Title'
|
|
|
|
);
|
|
|
|
|
2007-09-05 06:47:05 +02:00
|
|
|
static $template_fields = array(
|
2007-08-16 08:38:29 +02:00
|
|
|
"URLSegment",
|
|
|
|
"Title",
|
|
|
|
"MenuTitle",
|
|
|
|
"Content",
|
|
|
|
"MetaTitle",
|
|
|
|
"MetaDescription",
|
|
|
|
"MetaKeywords",
|
|
|
|
);
|
2008-02-27 05:59:22 +01:00
|
|
|
|
2007-08-16 08:38:29 +02:00
|
|
|
/**
|
|
|
|
* Set the fields that will be copied from the template.
|
|
|
|
* Note that ParentID and Sort are implied.
|
|
|
|
*/
|
|
|
|
static function set_template_fields($fieldList) {
|
|
|
|
self::$template_fields = $fieldList;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-20 07:36:14 +02:00
|
|
|
function extraStatics() {
|
2009-06-22 14:03:04 +02:00
|
|
|
if(!method_exists('DataObjectDecorator', 'load_extra_statics')) {
|
|
|
|
if($this->owner->class != 'SiteTree') return null;
|
2007-08-16 08:38:29 +02:00
|
|
|
}
|
2009-06-22 14:03:04 +02:00
|
|
|
return array(
|
|
|
|
'has_one' => array(
|
|
|
|
'Subsite' => 'Subsite', // The subsite that this page belongs to
|
|
|
|
'MasterPage' => 'SiteTree', // Optional; the page that is the content master
|
|
|
|
)
|
|
|
|
);
|
2007-08-16 08:38:29 +02:00
|
|
|
}
|
|
|
|
|
2009-07-14 08:42:31 +02:00
|
|
|
/**
|
|
|
|
* Check if we're currently looking at the main site.
|
|
|
|
* @return boolean TRUE main site | FALSE sub-site
|
|
|
|
*/
|
|
|
|
function isMainSite() {
|
|
|
|
if($this->owner->SubsiteID == 0) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-08-16 08:38:29 +02:00
|
|
|
/**
|
|
|
|
* Update any requests to limit the results to the current site
|
|
|
|
*/
|
|
|
|
function augmentSQL(SQLQuery &$query) {
|
2007-09-05 06:47:05 +02:00
|
|
|
if(Subsite::$disable_subsite_filter) return;
|
|
|
|
|
2007-08-16 08:38:29 +02:00
|
|
|
// If you're querying by ID, ignore the sub-site - this is a bit ugly...
|
2009-03-09 10:24:35 +01:00
|
|
|
if(!$query->where || (strpos($query->where[0], ".\"ID\" = ") === false && strpos($query->where[0], ".`ID` = ") === false && strpos($query->where[0], ".ID = ") === false && strpos($query->where[0], "ID = ") !== 0)) {
|
2007-08-16 08:38:29 +02:00
|
|
|
|
2009-10-16 03:10:52 +02:00
|
|
|
$context = DataObject::context_obj();
|
|
|
|
if($context && is_numeric($context->SubsiteID)) $subsiteID = (int) $context->SubsiteID;
|
2009-07-14 01:10:00 +02:00
|
|
|
else $subsiteID = (int) Subsite::currentSubsiteID();
|
2007-08-29 08:03:57 +02:00
|
|
|
|
2007-08-16 08:38:29 +02:00
|
|
|
// The foreach is an ugly way of getting the first key :-)
|
|
|
|
foreach($query->from as $tableName => $info) {
|
2009-07-14 01:10:00 +02:00
|
|
|
$where = "`$tableName`.SubsiteID IN ($subsiteID)";
|
|
|
|
if(defined('Database::USE_ANSI_SQL')) {
|
|
|
|
$where = "\"$tableName\".\"SubsiteID\" IN ($subsiteID)";
|
|
|
|
}
|
|
|
|
|
2007-11-06 04:06:02 +01:00
|
|
|
// The tableName should be SiteTree or SiteTree_Live...
|
|
|
|
if(strpos($tableName,'SiteTree') === false) break;
|
2009-07-14 01:10:00 +02:00
|
|
|
$query->where[] = $where;
|
2007-08-16 08:38:29 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-22 01:31:28 +02:00
|
|
|
/**
|
|
|
|
* Call this method before writing; the next write carried out by the system won't
|
|
|
|
* set the CustomContent value
|
|
|
|
*/
|
|
|
|
function nextWriteDoesntCustomise() {
|
|
|
|
$this->nextWriteDoesntCustomise = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected $nextWriteDoesntCustomise = false;
|
|
|
|
|
2007-08-16 08:38:29 +02:00
|
|
|
function augmentBeforeWrite() {
|
2009-10-05 03:55:55 +02:00
|
|
|
// if the page hasn't been written to a database table yet and no subsite has been set, then give it a subsite
|
|
|
|
if((!is_numeric($this->owner->ID) || $this->owner->ID == 0) && !$this->owner->SubsiteID) {
|
|
|
|
$this->owner->SubsiteID = Subsite::currentSubsiteID();
|
|
|
|
}
|
2007-08-16 08:38:29 +02:00
|
|
|
|
|
|
|
// If the content has been changed, then the page should be marked as 'custom content'
|
2008-08-22 01:31:28 +02:00
|
|
|
if(!$this->nextWriteDoesntCustomise && $this->owner->ID && $this->owner->MasterPageID && !$this->owner->CustomContent) {
|
2008-08-21 07:08:38 +02:00
|
|
|
$changed = $this->owner->getChangedFields();
|
|
|
|
|
2007-08-16 08:38:29 +02:00
|
|
|
foreach(self::$template_fields as $field) {
|
2008-08-21 07:08:38 +02:00
|
|
|
if(isset($changed[$field]) && $changed[$field]) {
|
2007-08-16 08:38:29 +02:00
|
|
|
$this->owner->CustomContent = true;
|
2008-09-17 01:49:05 +02:00
|
|
|
FormResponse::add("if($('Form_EditForm_CustomContent')) $('Form_EditForm_CustomContent').checked = true;");
|
2007-08-16 08:38:29 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-08-22 01:31:28 +02:00
|
|
|
|
|
|
|
$this->nextWriteDoesntCustomise = false;
|
2007-08-16 08:38:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateCMSFields(&$fields) {
|
|
|
|
if($this->owner->MasterPageID) {
|
|
|
|
$fields->insertFirst(new HeaderField('This page\'s content is copied from a master page: ' . $this->owner->MasterPage()->Title, 2));
|
|
|
|
}
|
2008-12-02 05:06:58 +01:00
|
|
|
|
|
|
|
// replace readonly link prefix
|
|
|
|
$subsite = $this->owner->Subsite();
|
|
|
|
if($subsite && $subsite->ID) {
|
|
|
|
$baseUrl = 'http://' . $subsite->domain() . '/';
|
|
|
|
$fields->removeByName('BaseUrlLabel');
|
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Content.Metadata',
|
|
|
|
new LabelField('BaseUrlLabel',$baseUrl),
|
|
|
|
'URLSegment'
|
|
|
|
);
|
|
|
|
}
|
2007-08-16 08:38:29 +02:00
|
|
|
}
|
2008-11-24 07:37:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Only allow editing of a page if the member satisfies one of the following conditions:
|
|
|
|
* - Is in a group which has access to the subsite this page belongs to
|
|
|
|
* - Is in a group with edit permissions on the "main site"
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function canEdit($member = null) {
|
2009-05-15 05:12:43 +02:00
|
|
|
if(!$member) $member = Member::currentUser();
|
|
|
|
|
2009-05-06 07:46:12 +02:00
|
|
|
// Check the CMS_ACCESS_CMSMain privileges on the subsite that owns this group
|
|
|
|
$oldSubsiteID = Session::get('SubsiteID');
|
2008-12-01 03:43:57 +01:00
|
|
|
|
2009-05-06 07:46:12 +02:00
|
|
|
Subsite::changeSubsite($this->owner->SubsiteID) ;
|
2009-05-15 05:12:43 +02:00
|
|
|
$access = Permission::checkMember($member, 'CMS_ACCESS_CMSMain');
|
2009-05-06 07:46:12 +02:00
|
|
|
Subsite::changeSubsite($oldSubsiteID);
|
2008-11-24 07:37:22 +01:00
|
|
|
|
2009-05-06 07:46:12 +02:00
|
|
|
if(!$access) $access = Permission::checkMember($member, 'SUBSITE_ACCESS_ALL');
|
2008-11-26 05:03:14 +01:00
|
|
|
|
2009-05-06 07:46:12 +02:00
|
|
|
return $access;
|
2008-11-24 07:37:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function canDelete($member = null) {
|
2008-11-26 05:03:14 +01:00
|
|
|
if(!$member && $member !== FALSE) $member = Member::currentUser();
|
2008-11-24 07:37:22 +01:00
|
|
|
|
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function canAddChildren($member = null) {
|
2008-11-26 05:03:14 +01:00
|
|
|
if(!$member && $member !== FALSE) $member = Member::currentUser();
|
2008-11-24 07:37:22 +01:00
|
|
|
|
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function canPublish($member = null) {
|
2008-11-26 05:03:14 +01:00
|
|
|
if(!$member && $member !== FALSE) $member = Member::currentUser();
|
|
|
|
|
2008-11-24 07:37:22 +01:00
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
2007-08-16 08:38:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a duplicate of this page and save it to another subsite
|
2007-08-29 00:29:44 +02:00
|
|
|
* @param $subsiteID int|Subsite The Subsite to copy to, or its ID
|
|
|
|
* @param $isTemplate boolean If this is true, then the current page will be treated as the template, and MasterPageID will be set
|
2007-08-16 08:38:29 +02:00
|
|
|
*/
|
2007-08-29 00:29:44 +02:00
|
|
|
public function duplicateToSubsite($subsiteID = null, $isTemplate = true) {
|
2007-08-16 08:38:29 +02:00
|
|
|
if(is_object($subsiteID)) {
|
|
|
|
$subsite = $subsiteID;
|
|
|
|
$subsiteID = $subsite->ID;
|
|
|
|
} else {
|
|
|
|
$subsite = DataObject::get_by_id('Subsite', $subsiteID);
|
|
|
|
}
|
|
|
|
|
|
|
|
$page = $this->owner->duplicate(false);
|
|
|
|
|
|
|
|
$page->CheckedPublicationDifferences = $page->AddedToStage = true;
|
|
|
|
$subsiteID = ($subsiteID ? $subsiteID : Subsite::currentSubsiteID());
|
|
|
|
$page->SubsiteID = $subsiteID;
|
2007-08-29 00:29:44 +02:00
|
|
|
|
|
|
|
if($isTemplate) $page->MasterPageID = $this->owner->ID;
|
2007-08-16 08:38:29 +02:00
|
|
|
|
|
|
|
$page->write();
|
|
|
|
|
|
|
|
return $page;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by ContentController::init();
|
|
|
|
*/
|
|
|
|
static function contentcontrollerInit($controller) {
|
|
|
|
// Need to set the SubsiteID to null incase we've been in the CMS
|
|
|
|
Session::set('SubsiteID', null);
|
2008-11-20 00:25:43 +01:00
|
|
|
$subsite = Subsite::currentSubsite();
|
|
|
|
if($subsite && $subsite->Theme) SSViewer::set_theme(Subsite::currentSubsite()->Theme);
|
2007-08-16 08:38:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by ModelAsController::init();
|
|
|
|
*/
|
|
|
|
static function modelascontrollerInit($controller) {
|
|
|
|
// Need to set the SubsiteID to null incase we've been in the CMS
|
|
|
|
Session::set('SubsiteID', null);
|
|
|
|
}
|
2007-09-05 08:42:57 +02:00
|
|
|
|
|
|
|
function alternateAbsoluteLink() {
|
2009-06-18 06:53:14 +02:00
|
|
|
// Generate the existing absolute URL and replace the domain with the subsite domain.
|
|
|
|
// This helps deal with Link() returning an absolute URL.
|
|
|
|
$url = Director::absoluteURL($this->owner->Link());
|
|
|
|
return preg_replace('/\/\/[^\/]+\//', '//' . $this->owner->Subsite()->domain() . '/', $url);
|
2007-09-05 08:42:57 +02:00
|
|
|
}
|
2009-07-14 08:42:31 +02:00
|
|
|
|
2007-08-16 08:38:29 +02:00
|
|
|
}
|
|
|
|
|
2008-02-01 00:11:47 +01:00
|
|
|
?>
|