2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Virtual Page creates an instance of a page, with the same fields that the original page had, but readonly.
|
|
|
|
* This allows you can have a page in mulitple places in the site structure, with different children without duplicating the content
|
|
|
|
* Note: This Only duplicates $db fields and not the $has_one etc..
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package cms
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class VirtualPage extends Page {
|
|
|
|
|
|
|
|
static $icon = array("cms/images/treeicons/page-shortcut-gold","file");
|
|
|
|
|
|
|
|
public static $virtualFields;
|
|
|
|
|
|
|
|
static $has_one = array(
|
|
|
|
"CopyContentFrom" => "SiteTree",
|
|
|
|
);
|
|
|
|
|
|
|
|
static $db = array(
|
|
|
|
"VersionID" => "Int",
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the array of fields required for the page type.
|
|
|
|
*/
|
|
|
|
function getVirtualFields() {
|
|
|
|
$nonVirtualFields = array(
|
|
|
|
"SecurityTypeID",
|
|
|
|
"OwnerID",
|
|
|
|
"URLSegment",
|
|
|
|
"Sort",
|
|
|
|
"Status",
|
|
|
|
'ShowInMenus',
|
2010-04-13 04:28:34 +02:00
|
|
|
// 'Locale'
|
2010-04-14 05:54:36 +02:00
|
|
|
'ShowInSearch',
|
|
|
|
'Version',
|
2010-05-25 06:59:45 +02:00
|
|
|
"Embargo",
|
|
|
|
"Expiry",
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$allFields = $this->db();
|
|
|
|
if($hasOne = $this->has_one()) foreach($hasOne as $link) $allFields[$link . 'ID'] = "Int";
|
|
|
|
foreach($allFields as $field => $type) {
|
|
|
|
if(!in_array($field, $nonVirtualFields)) $virtualFields[] = $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $virtualFields;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ContentSource() {
|
|
|
|
return $this->CopyContentFrom();
|
|
|
|
}
|
|
|
|
|
2009-10-15 23:46:45 +02:00
|
|
|
function allowedChildren() {
|
|
|
|
if($this->CopyContentFrom()) {
|
|
|
|
return $this->CopyContentFrom()->allowedChildren();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-24 01:26:10 +02:00
|
|
|
public function syncLinkTracking() {
|
|
|
|
if($this->CopyContentFromID) {
|
|
|
|
$this->HasBrokenLink = !(bool) DataObject::get_by_id('SiteTree', $this->CopyContentFromID);
|
|
|
|
}
|
2009-10-16 00:39:26 +02:00
|
|
|
}
|
|
|
|
|
2010-04-12 04:40:09 +02:00
|
|
|
/**
|
|
|
|
* We can only publish the page if there is a published source page
|
|
|
|
*/
|
|
|
|
public function canPublish($member = null) {
|
2010-04-12 04:41:23 +02:00
|
|
|
return $this->isPublishable() && parent::canPublish($member);
|
|
|
|
}
|
|
|
|
|
2010-04-12 05:13:26 +02:00
|
|
|
/**
|
|
|
|
* Return true if we can delete this page from the live site, which is different from can
|
|
|
|
* we publish it.
|
|
|
|
*/
|
|
|
|
public function canDeleteFromLive($member = null) {
|
|
|
|
return parent::canPublish($member);
|
|
|
|
}
|
|
|
|
|
2010-04-12 04:41:23 +02:00
|
|
|
/**
|
|
|
|
* Returns true if is page is publishable by anyone at all
|
|
|
|
* Return false if the source page isn't published yet.
|
2010-04-12 05:13:26 +02:00
|
|
|
*
|
|
|
|
* Note that isPublishable doesn't affect ete from live, only publish.
|
2010-04-12 04:41:23 +02:00
|
|
|
*/
|
|
|
|
public function isPublishable() {
|
2010-04-12 04:40:09 +02:00
|
|
|
// No source
|
2010-04-12 05:28:56 +02:00
|
|
|
if(!$this->CopyContentFrom() || !$this->CopyContentFrom()->ID) {
|
2010-04-12 04:40:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unpublished source
|
|
|
|
if(!Versioned::get_versionnumber_by_stage('SiteTree', 'Live', $this->CopyContentFromID)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-12 04:41:23 +02:00
|
|
|
// Default - publishable
|
|
|
|
return true;
|
2010-04-12 04:40:09 +02:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Generate the CMS fields from the fields from the original page.
|
|
|
|
*/
|
|
|
|
function getCMSFields($cms = null) {
|
|
|
|
$fields = parent::getCMSFields($cms);
|
|
|
|
|
|
|
|
// Setup the linking to the original page.
|
2007-10-25 04:47:45 +02:00
|
|
|
$copyContentFromField = new TreeDropdownField(
|
|
|
|
"CopyContentFromID",
|
|
|
|
_t('VirtualPage.CHOOSE', "Choose a page to link to"),
|
|
|
|
"SiteTree"
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
$copyContentFromField->setFilterFunction(create_function('$item', 'return $item->ClassName != "VirtualPage";'));
|
|
|
|
|
|
|
|
// Setup virtual fields
|
|
|
|
if($virtualFields = $this->getVirtualFields()) {
|
|
|
|
$roTransformation = new ReadonlyTransformation();
|
|
|
|
foreach($virtualFields as $virtualField) {
|
|
|
|
if($fields->dataFieldByName($virtualField))
|
|
|
|
$fields->replaceField($virtualField, $fields->dataFieldByName($virtualField)->transform($roTransformation));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add fields to the tab
|
2007-10-25 04:47:45 +02:00
|
|
|
$fields->addFieldToTab("Root.Content.Main",
|
2008-10-16 15:26:50 +02:00
|
|
|
new HeaderField('VirtualPageHeader',_t('VirtualPage.HEADER', "This is a virtual page")),
|
2007-10-25 04:47:45 +02:00
|
|
|
"Title"
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
$fields->addFieldToTab("Root.Content.Main", $copyContentFromField, "Title");
|
|
|
|
|
|
|
|
// Create links back to the original object in the CMS
|
|
|
|
if($this->CopyContentFromID) {
|
2007-10-25 04:47:45 +02:00
|
|
|
$linkToContent = "<a class=\"cmsEditlink\" href=\"admin/show/$this->CopyContentFromID\">" .
|
|
|
|
_t('VirtualPage.EDITCONTENT', 'click here to edit the content') . "</a>";
|
2008-10-16 15:26:50 +02:00
|
|
|
$fields->addFieldToTab("Root.Content.Main",
|
|
|
|
$linkToContentLabelField = new LabelField('VirtualPageContentLinkLabel', $linkToContent),
|
|
|
|
"Title"
|
|
|
|
);
|
|
|
|
$linkToContentLabelField->setAllowHTML(true);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We have to change it to copy all the content from the original page first.
|
|
|
|
*/
|
|
|
|
function onBeforeWrite() {
|
2010-04-12 04:40:09 +02:00
|
|
|
// On regular write, this will copy from published source. This happens on every publish
|
|
|
|
if($this->extension_instances['Versioned']->migratingVersion
|
|
|
|
&& Versioned::current_stage() == 'Live') {
|
|
|
|
$performCopyFrom = true;
|
2010-04-14 05:58:16 +02:00
|
|
|
|
2010-05-25 06:22:50 +02:00
|
|
|
$stageSourceVersion = DB::query("SELECT \"Version\" FROM \"SiteTree\" WHERE \"ID\" = $this->CopyContentFromID")->value();
|
|
|
|
$liveSourceVersion = DB::query("SELECT \"Version\" FROM \"SiteTree_Live\" WHERE \"ID\" = $this->CopyContentFromID")->value();
|
2010-04-14 05:58:16 +02:00
|
|
|
|
|
|
|
// We're going to create a new VP record in SiteTree_versions because the published
|
|
|
|
// version might not exist, unless we're publishing the latest version
|
|
|
|
if($stageSourceVersion != $liveSourceVersion) {
|
|
|
|
$this->extension_instances['Versioned']->migratingVersion = null;
|
|
|
|
}
|
2010-04-12 04:40:09 +02:00
|
|
|
|
|
|
|
// On regular write, this will copy from draft source. This is only executed when the source
|
|
|
|
// page changeds
|
|
|
|
} else {
|
|
|
|
$performCopyFrom = $this->isChanged('CopyContentFromID') && $this->CopyContentFromID != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// On publish, this will copy from published source
|
|
|
|
if($performCopyFrom && $this instanceof VirtualPage) {
|
|
|
|
// This flush is needed because the get_one cache doesn't respect site version :-(
|
|
|
|
singleton('SiteTree')->flushCache();
|
|
|
|
$source = DataObject::get_one("SiteTree",sprintf('"SiteTree"."ID" = %d', $this->CopyContentFromID));
|
|
|
|
// Leave the updating of image tracking until after write, in case its a new record
|
|
|
|
$this->copyFrom($source, false);
|
|
|
|
$this->URLSegment = $source->URLSegment . '-' . $this->ID;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
parent::onBeforeWrite();
|
|
|
|
}
|
2009-10-21 04:19:40 +02:00
|
|
|
|
2010-04-12 03:49:23 +02:00
|
|
|
function onAfterWrite() {
|
|
|
|
parent::onAfterWrite();
|
|
|
|
|
|
|
|
// Don't do this stuff when we're publishing
|
|
|
|
if(!$this->extension_instances['Versioned']->migratingVersion) {
|
|
|
|
if(
|
|
|
|
$this->isChanged('CopyContentFromID')
|
|
|
|
&& $this->CopyContentFromID != 0
|
|
|
|
&& $this instanceof VirtualPage
|
|
|
|
) {
|
|
|
|
$this->updateImageTracking();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Ensure we have an up-to-date version of everything.
|
|
|
|
*/
|
2010-04-12 03:49:23 +02:00
|
|
|
function copyFrom($source, $updateImageTracking = true) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($source) {
|
2009-10-16 00:11:26 +02:00
|
|
|
foreach($this->getVirtualFields() as $virtualField) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->$virtualField = $source->$virtualField;
|
2009-10-16 00:11:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// We also want to copy ShowInMenus, but only if we're copying the
|
|
|
|
// source page for the first time.
|
|
|
|
if($this->isChanged('CopyContentFromID')) {
|
|
|
|
$this->ShowInMenus = $source->ShowInMenus;
|
|
|
|
}
|
2010-04-12 03:49:23 +02:00
|
|
|
|
|
|
|
if($updateImageTracking) $this->updateImageTracking();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 02:23:12 +02:00
|
|
|
|
2010-04-12 03:49:23 +02:00
|
|
|
function updateImageTracking() {
|
|
|
|
// Doesn't work on unsaved records
|
|
|
|
if(!$this->ID) return;
|
|
|
|
|
|
|
|
// Remove CopyContentFrom() from the cache
|
|
|
|
unset($this->components['CopyContentFrom']);
|
|
|
|
|
|
|
|
// Update ImageTracking
|
|
|
|
$this->ImageTracking()->setByIdList($this->CopyContentFrom()->ImageTracking()->column('ID'));
|
|
|
|
}
|
|
|
|
|
2009-07-24 02:23:12 +02:00
|
|
|
/**
|
|
|
|
* Allow attributes on the master page to pass
|
|
|
|
* through to the virtual page
|
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function __get($field) {
|
|
|
|
$return = parent::__get($field);
|
|
|
|
if ($return === null) {
|
|
|
|
if($this->copyContentFrom()->hasMethod($funcName = "get$field")) {
|
|
|
|
$return = $this->copyContentFrom()->$funcName();
|
|
|
|
} else if($this->copyContentFrom()->hasField($field)) {
|
|
|
|
$return = $this->copyContentFrom()->getField($field);
|
2010-04-14 04:17:29 +02:00
|
|
|
} else if($field == 'Content') {
|
|
|
|
return '<p>' . _t('VirtualPage.NOTFOUND', 'We could not find the content for this virtual page.') . '</p>';
|
2009-07-24 02:23:12 +02:00
|
|
|
}
|
|
|
|
}
|
2009-07-30 01:21:59 +02:00
|
|
|
|
2009-07-24 02:23:12 +02:00
|
|
|
return $return;
|
|
|
|
}
|
2009-07-30 01:21:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pass unrecognized method calls on to the original data object
|
|
|
|
*
|
|
|
|
* @param string $method
|
|
|
|
* @param string $args
|
|
|
|
*/
|
|
|
|
function __call($method, $args) {
|
|
|
|
try {
|
|
|
|
return parent::__call($method, $args);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// Hack... detect exception type. We really should use exception subclasses.
|
|
|
|
// if the exception isn't a 'no method' error, rethrow it
|
|
|
|
if ($e->getCode() !== 2175) throw $e;
|
|
|
|
$original = $this->copyContentFrom();
|
|
|
|
return call_user_func_array(array($original, $method), $args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overwrite to also check for method on the original data object
|
|
|
|
*
|
|
|
|
* @param string $method
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function hasMethod($method) {
|
|
|
|
$haveIt = parent::hasMethod($method);
|
|
|
|
if (!$haveIt) $haveIt = $this->copyContentFrom()->hasMethod($method);
|
|
|
|
return $haveIt;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-02-25 03:10:37 +01:00
|
|
|
* Controller for the virtual page.
|
|
|
|
* @package cms
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class VirtualPage_Controller extends Page_Controller {
|
2009-07-30 01:21:59 +02:00
|
|
|
|
2008-04-06 06:09:59 +02:00
|
|
|
static $allowed_actions = array(
|
|
|
|
'loadcontentall' => 'ADMIN',
|
|
|
|
);
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Reloads the content if the version is different ;-)
|
|
|
|
*/
|
|
|
|
function reloadContent() {
|
|
|
|
$this->failover->copyFrom($this->failover->CopyContentFrom());
|
|
|
|
$this->failover->write();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-24 02:23:12 +02:00
|
|
|
function getViewer($action) {
|
2009-08-10 02:11:28 +02:00
|
|
|
$originalClass = get_class($this->CopyContentFrom());
|
|
|
|
if ($originalClass == 'SiteTree') $name = 'Page_Controller';
|
|
|
|
else $name = $originalClass."_Controller";
|
2009-07-24 02:23:12 +02:00
|
|
|
$controller = new $name();
|
|
|
|
return $controller->getViewer($action);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* When the virtualpage is loaded, check to see if the versions are the same
|
|
|
|
* if not, reload the content.
|
|
|
|
* NOTE: Virtual page must have a container object of subclass of sitetree.
|
|
|
|
* We can't load the content without an ID or record to copy it from.
|
|
|
|
*/
|
|
|
|
function init(){
|
2008-12-04 23:38:32 +01:00
|
|
|
if(isset($this->record) && $this->record->ID){
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->record->VersionID != $this->failover->CopyContentFrom()->Version){
|
|
|
|
$this->reloadContent();
|
|
|
|
$this->VersionID = $this->failover->CopyContentFrom()->VersionID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parent::init();
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadcontentall() {
|
|
|
|
$pages = DataObject::get("VirtualPage");
|
|
|
|
foreach($pages as $page) {
|
|
|
|
$page->copyFrom($page->CopyContentFrom());
|
|
|
|
$page->write();
|
|
|
|
$page->publish("Stage", "Live");
|
|
|
|
echo "<li>Published $page->URLSegment";
|
|
|
|
}
|
|
|
|
}
|
2009-07-30 01:21:59 +02:00
|
|
|
|
|
|
|
/**
|
2010-04-13 01:44:25 +02:00
|
|
|
* Also check the original object's original controller for the method
|
2009-07-30 01:21:59 +02:00
|
|
|
*
|
|
|
|
* @param string $method
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function hasMethod($method) {
|
|
|
|
$haveIt = parent::hasMethod($method);
|
|
|
|
if (!$haveIt) {
|
2009-08-06 22:50:46 +02:00
|
|
|
$originalClass = get_class($this->CopyContentFrom());
|
|
|
|
if ($originalClass == 'SiteTree') $name = 'ContentController';
|
|
|
|
else $name = $originalClass."_Controller";
|
2009-07-30 01:21:59 +02:00
|
|
|
$controller = new $name($this->dataRecord->copyContentFrom());
|
|
|
|
$haveIt = $controller->hasMethod($method);
|
|
|
|
}
|
|
|
|
return $haveIt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pass unrecognized method calls on to the original controller
|
|
|
|
*
|
|
|
|
* @param string $method
|
|
|
|
* @param string $args
|
|
|
|
*/
|
|
|
|
function __call($method, $args) {
|
|
|
|
try {
|
|
|
|
return parent::__call($method, $args);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// Hack... detect exception type. We really should use exception subclasses.
|
|
|
|
// if the exception isn't a 'no method' error, rethrow it
|
|
|
|
if ($e->getCode() !== 2175) throw $e;
|
|
|
|
$original = $this->copyContentFrom();
|
2010-04-13 01:44:25 +02:00
|
|
|
$originalClass = get_class($original);
|
|
|
|
if ($originalClass == 'SiteTree') $name = 'ContentController';
|
|
|
|
else $name = $originalClass."_Controller";
|
|
|
|
$controller = new $name($this->dataRecord->copyContentFrom());
|
|
|
|
return call_user_func_array(array($controller, $method), $args);
|
2009-07-30 01:21:59 +02:00
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2009-10-15 23:46:45 +02:00
|
|
|
?>
|