2008-12-02 04:50:06 +01:00
|
|
|
<?php
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2017-05-24 12:32:05 +02:00
|
|
|
namespace SilverStripe\Subsites\Pages;
|
|
|
|
|
|
|
|
|
2016-09-22 16:38:29 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\ArrayList;
|
|
|
|
use SilverStripe\View\ArrayData;
|
|
|
|
use SilverStripe\Forms\DropdownField;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Forms\LabelField;
|
|
|
|
use SilverStripe\Forms\TextField;
|
|
|
|
use SilverStripe\Forms\TextareaField;
|
|
|
|
use SilverStripe\Control\Session;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\CMS\Model\VirtualPage;
|
2017-05-24 13:36:04 +02:00
|
|
|
use SilverStripe\CMS\Model\VirtualPageController;
|
|
|
|
|
|
|
|
|
|
|
|
use SilverStripe\Subsites\Model\Subsite;
|
|
|
|
use SilverStripe\Subsites\Forms\SubsitesTreeDropdownField;
|
|
|
|
|
2017-05-24 12:32:05 +02:00
|
|
|
|
2008-12-02 04:50:06 +01:00
|
|
|
class SubsitesVirtualPage extends VirtualPage {
|
2013-04-29 02:25:29 +02:00
|
|
|
|
|
|
|
private static $description = 'Displays the content of a page on another subsite';
|
|
|
|
|
2013-05-06 12:21:09 +02:00
|
|
|
private static $db = array(
|
2010-03-01 04:00:29 +01:00
|
|
|
'CustomMetaTitle' => 'Varchar(255)',
|
|
|
|
'CustomMetaKeywords' => 'Varchar(255)',
|
|
|
|
'CustomMetaDescription' => 'Text',
|
|
|
|
'CustomExtraMeta' => 'HTMLText'
|
|
|
|
);
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2012-03-25 18:35:01 +02:00
|
|
|
public function getCMSFields() {
|
2008-12-02 04:50:06 +01:00
|
|
|
$fields = parent::getCMSFields();
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2017-05-24 13:36:04 +02:00
|
|
|
$subsites = DataObject::get(Subsite::class);
|
2012-03-25 18:35:01 +02:00
|
|
|
if(!$subsites) {
|
2012-07-10 15:43:53 +02:00
|
|
|
$subsites = new ArrayList();
|
|
|
|
}else {
|
|
|
|
$subsites=ArrayList::create($subsites->toArray());
|
|
|
|
}
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2010-03-01 22:26:35 +01:00
|
|
|
$subsites->push(new ArrayData(array('Title' => 'Main site', 'ID' => 0)));
|
2008-12-02 04:50:06 +01:00
|
|
|
|
2010-03-01 22:26:35 +01:00
|
|
|
$fields->addFieldToTab(
|
2012-03-25 18:35:01 +02:00
|
|
|
'Root.Main',
|
2013-07-10 15:29:22 +02:00
|
|
|
DropdownField::create(
|
2016-09-22 16:38:29 +02:00
|
|
|
"CopyContentFromID_SubsiteID",
|
2017-05-24 13:36:04 +02:00
|
|
|
_t('SubsitesVirtualPage.SubsiteField',Subsite::class),
|
2013-07-10 15:29:22 +02:00
|
|
|
$subsites->map('ID', 'Title')
|
|
|
|
)->addExtraClass('subsitestreedropdownfield-chooser no-change-track'),
|
2010-03-01 22:26:35 +01:00
|
|
|
'CopyContentFromID'
|
|
|
|
);
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2008-12-02 04:50:06 +01:00
|
|
|
// Setup the linking to the original page.
|
|
|
|
$pageSelectionField = new SubsitesTreeDropdownField(
|
2016-09-22 16:38:29 +02:00
|
|
|
"CopyContentFromID",
|
|
|
|
_t('VirtualPage.CHOOSE', "Choose a page to link to"),
|
|
|
|
"SilverStripe\\CMS\\Model\\SiteTree",
|
2010-03-01 03:58:31 +01:00
|
|
|
"ID",
|
|
|
|
"MenuTitle"
|
2008-12-02 04:50:06 +01:00
|
|
|
);
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2010-09-22 03:43:57 +02:00
|
|
|
if(Controller::has_curr() && Controller::curr()->getRequest()) {
|
2013-07-10 15:29:22 +02:00
|
|
|
$subsiteID = Controller::curr()->getRequest()->requestVar('CopyContentFromID_SubsiteID');
|
2009-07-30 04:11:02 +02:00
|
|
|
$pageSelectionField->setSubsiteID($subsiteID);
|
2008-12-02 04:50:06 +01:00
|
|
|
}
|
|
|
|
$fields->replaceField('CopyContentFromID', $pageSelectionField);
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2008-12-02 04:50:06 +01:00
|
|
|
// Create links back to the original object in the CMS
|
|
|
|
if($this->CopyContentFromID) {
|
2015-06-17 16:30:56 +02:00
|
|
|
$editLink = "admin/pages/edit/show/$this->CopyContentFromID/?SubsiteID=" . $this->CopyContentFrom()->SubsiteID;
|
2008-12-02 04:50:06 +01:00
|
|
|
$linkToContent = "
|
2016-09-22 16:38:29 +02:00
|
|
|
<a class=\"cmsEditlink\" href=\"$editLink\">" .
|
|
|
|
_t('VirtualPage.EDITCONTENT', 'Click here to edit the content') .
|
2008-12-02 04:50:06 +01:00
|
|
|
"</a>";
|
|
|
|
$fields->removeByName("VirtualPageContentLinkLabel");
|
|
|
|
$fields->addFieldToTab(
|
2016-09-22 16:38:29 +02:00
|
|
|
"Root.Main",
|
2008-12-02 04:50:06 +01:00
|
|
|
$linkToContentLabelField = new LabelField('VirtualPageContentLinkLabel', $linkToContent),
|
|
|
|
'Title'
|
|
|
|
);
|
|
|
|
$linkToContentLabelField->setAllowHTML(true);
|
|
|
|
}
|
2016-09-22 16:38:29 +02:00
|
|
|
|
|
|
|
|
2013-10-30 13:43:59 +01:00
|
|
|
$fields->addFieldToTab(
|
2016-09-22 16:38:29 +02:00
|
|
|
'Root.Main',
|
2013-10-30 13:43:59 +01:00
|
|
|
TextField::create(
|
2016-09-22 16:38:29 +02:00
|
|
|
'CustomMetaTitle',
|
2013-10-30 13:43:59 +01:00
|
|
|
$this->fieldLabel('CustomMetaTitle')
|
2016-09-22 16:38:29 +02:00
|
|
|
)->setDescription(_t('SubsitesVirtualPage.OverrideNote', 'Overrides inherited value from the source')),
|
2013-10-30 13:43:59 +01:00
|
|
|
'MetaTitle'
|
|
|
|
);
|
|
|
|
$fields->addFieldToTab(
|
2016-09-22 16:38:29 +02:00
|
|
|
'Root.Main',
|
2013-10-30 13:43:59 +01:00
|
|
|
TextareaField::create(
|
2016-09-22 16:38:29 +02:00
|
|
|
'CustomMetaKeywords',
|
2013-10-30 13:43:59 +01:00
|
|
|
$this->fieldLabel('CustomMetaTitle')
|
2016-09-22 16:38:29 +02:00
|
|
|
)->setDescription(_t('SubsitesVirtualPage.OverrideNote')),
|
2013-10-30 13:43:59 +01:00
|
|
|
'MetaKeywords'
|
|
|
|
);
|
|
|
|
$fields->addFieldToTab(
|
2016-09-22 16:38:29 +02:00
|
|
|
'Root.Main',
|
2013-10-30 13:43:59 +01:00
|
|
|
TextareaField::create(
|
2016-09-22 16:38:29 +02:00
|
|
|
'CustomMetaDescription',
|
2013-10-30 13:43:59 +01:00
|
|
|
$this->fieldLabel('CustomMetaTitle')
|
2016-09-22 16:38:29 +02:00
|
|
|
)->setDescription(_t('SubsitesVirtualPage.OverrideNote')),
|
2013-10-30 13:43:59 +01:00
|
|
|
'MetaDescription'
|
|
|
|
);
|
|
|
|
$fields->addFieldToTab(
|
2016-09-22 16:38:29 +02:00
|
|
|
'Root.Main',
|
2013-10-30 13:43:59 +01:00
|
|
|
TextField::create(
|
2016-09-22 16:38:29 +02:00
|
|
|
'CustomExtraMeta',
|
2013-10-30 13:43:59 +01:00
|
|
|
$this->fieldLabel('CustomMetaTitle')
|
2016-09-22 16:38:29 +02:00
|
|
|
)->setDescription(_t('SubsitesVirtualPage.OverrideNote')),
|
2013-10-30 13:43:59 +01:00
|
|
|
'ExtraMeta'
|
|
|
|
);
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2008-12-02 04:50:06 +01:00
|
|
|
return $fields;
|
|
|
|
}
|
2013-10-30 13:43:59 +01:00
|
|
|
|
|
|
|
public function fieldLabels($includerelations = true) {
|
|
|
|
$labels = parent::fieldLabels($includerelations);
|
|
|
|
$labels['CustomMetaTitle'] = _t('Subsite.CustomMetaTitle','Title');
|
|
|
|
$labels['CustomMetaKeywords'] = _t('Subsite.CustomMetaKeywords','Keywords');
|
|
|
|
$labels['CustomMetaDescription'] = _t('Subsite.CustomMetaDescription','Description');
|
|
|
|
$labels['CustomExtraMeta'] = _t('Subsite.CustomExtraMeta','Custom Meta Tags');
|
|
|
|
|
|
|
|
return $labels;
|
|
|
|
}
|
2013-07-10 15:29:22 +02:00
|
|
|
|
|
|
|
public function getCopyContentFromID_SubsiteID() {
|
|
|
|
return ($this->CopyContentFromID) ? (int)$this->CopyContentFrom()->SubsiteID : (int)Session::get('SubsiteID');
|
|
|
|
}
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2012-03-25 18:35:01 +02:00
|
|
|
public function getVirtualFields() {
|
2008-12-02 04:50:06 +01:00
|
|
|
$fields = parent::getVirtualFields();
|
|
|
|
foreach($fields as $k => $v) {
|
|
|
|
if($v == 'SubsiteID') unset($fields[$k]);
|
|
|
|
}
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2010-03-01 22:49:50 +01:00
|
|
|
foreach(self::$db as $field => $type) if (in_array($field, $fields)) unset($fields[array_search($field, $fields)]);
|
2008-12-02 04:50:06 +01:00
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2012-03-25 18:35:01 +02:00
|
|
|
public function syncLinkTracking() {
|
2010-03-01 22:33:12 +01:00
|
|
|
$oldState = Subsite::$disable_subsite_filter;
|
|
|
|
Subsite::$disable_subsite_filter = true;
|
2016-09-22 16:38:29 +02:00
|
|
|
if ($this->CopyContentFromID) $this->HasBrokenLink = DataObject::get_by_id('SilverStripe\\CMS\\Model\\SiteTree', $this->CopyContentFromID) ? false : true;
|
2010-03-01 22:33:12 +01:00
|
|
|
Subsite::$disable_subsite_filter = $oldState;
|
|
|
|
}
|
2010-03-01 23:13:35 +01:00
|
|
|
|
2012-03-25 18:35:01 +02:00
|
|
|
public function onBeforeWrite() {
|
2010-03-01 04:00:29 +01:00
|
|
|
parent::onBeforeWrite();
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2010-03-01 04:00:29 +01:00
|
|
|
if($this->CustomMetaTitle) $this->MetaTitle = $this->CustomMetaTitle;
|
2010-03-01 22:33:46 +01:00
|
|
|
else {
|
2016-09-22 16:38:29 +02:00
|
|
|
$this->MetaTitle = $this->ContentSource()->MetaTitle ? $this->ContentSource()->MetaTitle : $this->MetaTitle;
|
2010-03-01 22:33:46 +01:00
|
|
|
}
|
2010-03-01 04:00:29 +01:00
|
|
|
if($this->CustomMetaKeywords) $this->MetaKeywords = $this->CustomMetaKeywords;
|
2010-03-01 22:33:46 +01:00
|
|
|
else {
|
2016-09-22 16:38:29 +02:00
|
|
|
$this->MetaKeywords = $this->ContentSource()->MetaKeywords ? $this->ContentSource()->MetaKeywords : $this->MetaKeywords;
|
2010-03-01 22:33:46 +01:00
|
|
|
}
|
2010-03-01 04:00:29 +01:00
|
|
|
if($this->CustomMetaDescription) $this->MetaDescription = $this->CustomMetaDescription;
|
2010-03-01 22:33:46 +01:00
|
|
|
else {
|
2016-09-22 16:38:29 +02:00
|
|
|
$this->MetaDescription = $this->ContentSource()->MetaDescription ? $this->ContentSource()->MetaDescription : $this->MetaDescription;
|
2010-03-01 22:33:46 +01:00
|
|
|
}
|
2010-03-01 04:00:29 +01:00
|
|
|
if($this->CustomExtraMeta) $this->ExtraMeta = $this->CustomExtraMeta;
|
2010-03-01 22:33:46 +01:00
|
|
|
else {
|
2016-09-22 16:38:29 +02:00
|
|
|
$this->ExtraMeta = $this->ContentSource()->ExtraMeta ? $this->ContentSource()->ExtraMeta : $this->ExtraMeta;
|
2010-03-01 22:33:46 +01:00
|
|
|
}
|
2010-03-01 04:00:29 +01:00
|
|
|
}
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2012-03-25 18:35:01 +02:00
|
|
|
public function validURLSegment() {
|
2011-08-22 17:52:21 +02:00
|
|
|
$isValid = parent::validURLSegment();
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2011-08-22 17:52:21 +02:00
|
|
|
// Veto the validation rules if its false. In this case, some logic
|
|
|
|
// needs to be duplicated from parent to find out the exact reason the validation failed.
|
|
|
|
if(!$isValid) {
|
2012-07-10 15:43:53 +02:00
|
|
|
$IDFilter = ($this->ID) ? "AND \"SiteTree\".\"ID\" <> $this->ID" : null;
|
2011-08-22 17:52:21 +02:00
|
|
|
$parentFilter = null;
|
|
|
|
|
2016-09-22 16:38:29 +02:00
|
|
|
if(Config::inst()->get('SilverStripe\\CMS\\Model\\SiteTree', 'nested_urls')) {
|
2011-08-22 17:52:21 +02:00
|
|
|
if($this->ParentID) {
|
|
|
|
$parentFilter = " AND \"SiteTree\".\"ParentID\" = $this->ParentID";
|
|
|
|
} else {
|
|
|
|
$parentFilter = ' AND "SiteTree"."ParentID" = 0';
|
|
|
|
}
|
|
|
|
}
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2011-09-12 12:18:56 +02:00
|
|
|
$origDisableSubsiteFilter = Subsite::$disable_subsite_filter;
|
2011-08-22 17:52:21 +02:00
|
|
|
Subsite::$disable_subsite_filter = true;
|
|
|
|
$existingPage = DataObject::get_one(
|
2016-09-22 16:38:29 +02:00
|
|
|
'SilverStripe\\CMS\\Model\\SiteTree',
|
2011-08-22 17:52:21 +02:00
|
|
|
"\"URLSegment\" = '$this->URLSegment' $IDFilter $parentFilter",
|
|
|
|
false // disable cache, it doesn't include subsite status in the key
|
|
|
|
);
|
2011-09-12 12:18:56 +02:00
|
|
|
Subsite::$disable_subsite_filter = $origDisableSubsiteFilter;
|
2011-08-22 17:52:21 +02:00
|
|
|
$existingPageInSubsite = DataObject::get_one(
|
2016-09-22 16:38:29 +02:00
|
|
|
'SilverStripe\\CMS\\Model\\SiteTree',
|
2011-08-22 17:52:21 +02:00
|
|
|
"\"URLSegment\" = '$this->URLSegment' $IDFilter $parentFilter",
|
|
|
|
false // disable cache, it doesn't include subsite status in the key
|
|
|
|
);
|
|
|
|
|
|
|
|
// If URL has been vetoed because of an existing page,
|
|
|
|
// be more specific and allow same URLSegments in different subsites
|
|
|
|
$isValid = !($existingPage && $existingPageInSubsite);
|
|
|
|
}
|
2008-12-02 04:50:06 +01:00
|
|
|
|
2016-09-22 16:38:29 +02:00
|
|
|
return $isValid;
|
2008-12-02 04:50:06 +01:00
|
|
|
}
|
|
|
|
}
|