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 {
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-10-25 04:47:45 +02:00
|
|
|
static $add_action = "Virtual page (another page's content)";
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
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',
|
|
|
|
'ShowInSearch'
|
|
|
|
);
|
|
|
|
|
|
|
|
$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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
|
|
|
// Don't do this stuff when we're publishing
|
|
|
|
if(!$this->extension_instances['Versioned']->migratingVersion) {
|
2008-12-04 23:38:32 +01:00
|
|
|
if(
|
2009-05-27 04:09:18 +02:00
|
|
|
$this->isChanged('CopyContentFromID')
|
2008-12-04 23:38:32 +01:00
|
|
|
&& $this->CopyContentFromID != 0
|
|
|
|
&& $this instanceof VirtualPage
|
|
|
|
) {
|
2009-05-04 05:42:55 +02:00
|
|
|
$source = DataObject::get_one("SiteTree",sprintf('"SiteTree"."ID" = %d', $this->CopyContentFromID));
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->copyFrom($source);
|
|
|
|
$this->URLSegment = $source->URLSegment . '-' . $this->ID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::onBeforeWrite();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Ensure we have an up-to-date version of everything.
|
|
|
|
*/
|
|
|
|
function copyFrom($source) {
|
|
|
|
if($source) {
|
|
|
|
foreach($this->getVirtualFields() AS $virtualField)
|
|
|
|
$this->$virtualField = $source->$virtualField;
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
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 {
|
|
|
|
|
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) {
|
|
|
|
$name = get_class($this->CopyContentFrom())."_Controller";
|
|
|
|
$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-02-02 00:49:53 +01:00
|
|
|
?>
|