silverstripe-subsites/code/pages/SubsitesVirtualPage.php

217 lines
7.1 KiB
PHP
Raw Normal View History

<?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
class SubsitesVirtualPage extends VirtualPage {
private static $description = 'Displays the content of a page on another subsite';
private static $db = array(
'CustomMetaTitle' => 'Varchar(255)',
'CustomMetaKeywords' => 'Varchar(255)',
'CustomMetaDescription' => 'Text',
'CustomExtraMeta' => 'HTMLText'
);
2016-09-22 16:38:29 +02:00
BUG: Modifying the module to work with SS 3.0 Replaced deprecated DataObjectDecorator with DataExtension Fixed hard crashes in the cms Updated to support new LeftAndMain template structure Made the subsites model admin functional Moved the LeftAndMain_Menu template up a directory so it overrides the core Fixed some errors caused by changes to the framework Re-organized the code folder Fixed permission issue causing to default to first subsite regardless if it is the default or not Fixed crashes on the subsite virtual page when creating/editing Removed toDropdownMap() calls replacing with map() Fixed the URLSegment field on subsites Fixed error when detecting subsite for a domain Improved styles on the subsite dropdown Updated LeftAndMain_Subsites.js to work with jQuery entwine Started porting the SubsitesTreeDropdownField.js to use jQuery entwine and work with the new TreeDropdownField.js Fixed issue causing crash when viewing a page who is linked to by a subsite virtual page Removed unused methods on SubsitesTreeDropdownField.js Re-added classes that were moved Fixed hard crash after saving caused by the many_many definition on SiteTreeSubsites Replaced deprecated DataObjectSet creation with ArrayList Compatibility fixes with SS 3.0 beta 2 Fixed crash in cms caused by no parameter being passed to the SubsiteReportWrapper constructor Proper fix for report wrapper Removed table list field in favor of a basic grid field Fixed updateCMSFields() for file subsites Migrated translations to yml Fixed issue causing the current page to not get cleared when changing subsites in the cms Fixed virtual page icon Fixed language files issue
2012-03-25 18:35:01 +02:00
public function getCMSFields() {
$fields = parent::getCMSFields();
2016-09-22 16:38:29 +02:00
2017-05-24 13:36:04 +02:00
$subsites = DataObject::get(Subsite::class);
BUG: Modifying the module to work with SS 3.0 Replaced deprecated DataObjectDecorator with DataExtension Fixed hard crashes in the cms Updated to support new LeftAndMain template structure Made the subsites model admin functional Moved the LeftAndMain_Menu template up a directory so it overrides the core Fixed some errors caused by changes to the framework Re-organized the code folder Fixed permission issue causing to default to first subsite regardless if it is the default or not Fixed crashes on the subsite virtual page when creating/editing Removed toDropdownMap() calls replacing with map() Fixed the URLSegment field on subsites Fixed error when detecting subsite for a domain Improved styles on the subsite dropdown Updated LeftAndMain_Subsites.js to work with jQuery entwine Started porting the SubsitesTreeDropdownField.js to use jQuery entwine and work with the new TreeDropdownField.js Fixed issue causing crash when viewing a page who is linked to by a subsite virtual page Removed unused methods on SubsitesTreeDropdownField.js Re-added classes that were moved Fixed hard crash after saving caused by the many_many definition on SiteTreeSubsites Replaced deprecated DataObjectSet creation with ArrayList Compatibility fixes with SS 3.0 beta 2 Fixed crash in cms caused by no parameter being passed to the SubsiteReportWrapper constructor Proper fix for report wrapper Removed table list field in favor of a basic grid field Fixed updateCMSFields() for file subsites Migrated translations to yml Fixed issue causing the current page to not get cleared when changing subsites in the cms Fixed virtual page icon Fixed language files issue
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
$subsites->push(new ArrayData(array('Title' => 'Main site', 'ID' => 0)));
$fields->addFieldToTab(
BUG: Modifying the module to work with SS 3.0 Replaced deprecated DataObjectDecorator with DataExtension Fixed hard crashes in the cms Updated to support new LeftAndMain template structure Made the subsites model admin functional Moved the LeftAndMain_Menu template up a directory so it overrides the core Fixed some errors caused by changes to the framework Re-organized the code folder Fixed permission issue causing to default to first subsite regardless if it is the default or not Fixed crashes on the subsite virtual page when creating/editing Removed toDropdownMap() calls replacing with map() Fixed the URLSegment field on subsites Fixed error when detecting subsite for a domain Improved styles on the subsite dropdown Updated LeftAndMain_Subsites.js to work with jQuery entwine Started porting the SubsitesTreeDropdownField.js to use jQuery entwine and work with the new TreeDropdownField.js Fixed issue causing crash when viewing a page who is linked to by a subsite virtual page Removed unused methods on SubsitesTreeDropdownField.js Re-added classes that were moved Fixed hard crash after saving caused by the many_many definition on SiteTreeSubsites Replaced deprecated DataObjectSet creation with ArrayList Compatibility fixes with SS 3.0 beta 2 Fixed crash in cms caused by no parameter being passed to the SubsiteReportWrapper constructor Proper fix for report wrapper Removed table list field in favor of a basic grid field Fixed updateCMSFields() for file subsites Migrated translations to yml Fixed issue causing the current page to not get cleared when changing subsites in the cms Fixed virtual page icon Fixed language files issue
2012-03-25 18:35:01 +02:00
'Root.Main',
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),
$subsites->map('ID', 'Title')
)->addExtraClass('subsitestreedropdownfield-chooser no-change-track'),
'CopyContentFromID'
);
2016-09-22 16:38:29 +02: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"
);
2016-09-22 16:38:29 +02:00
if(Controller::has_curr() && Controller::curr()->getRequest()) {
$subsiteID = Controller::curr()->getRequest()->requestVar('CopyContentFromID_SubsiteID');
$pageSelectionField->setSubsiteID($subsiteID);
}
$fields->replaceField('CopyContentFromID', $pageSelectionField);
2016-09-22 16:38:29 +02:00
// Create links back to the original object in the CMS
if($this->CopyContentFromID) {
$editLink = "admin/pages/edit/show/$this->CopyContentFromID/?SubsiteID=" . $this->CopyContentFrom()->SubsiteID;
$linkToContent = "
2016-09-22 16:38:29 +02:00
<a class=\"cmsEditlink\" href=\"$editLink\">" .
_t('VirtualPage.EDITCONTENT', 'Click here to edit the content') .
"</a>";
$fields->removeByName("VirtualPageContentLinkLabel");
$fields->addFieldToTab(
2016-09-22 16:38:29 +02:00
"Root.Main",
$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
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;
}
public function getCopyContentFromID_SubsiteID() {
return ($this->CopyContentFromID) ? (int)$this->CopyContentFrom()->SubsiteID : (int)Session::get('SubsiteID');
}
2016-09-22 16:38:29 +02:00
BUG: Modifying the module to work with SS 3.0 Replaced deprecated DataObjectDecorator with DataExtension Fixed hard crashes in the cms Updated to support new LeftAndMain template structure Made the subsites model admin functional Moved the LeftAndMain_Menu template up a directory so it overrides the core Fixed some errors caused by changes to the framework Re-organized the code folder Fixed permission issue causing to default to first subsite regardless if it is the default or not Fixed crashes on the subsite virtual page when creating/editing Removed toDropdownMap() calls replacing with map() Fixed the URLSegment field on subsites Fixed error when detecting subsite for a domain Improved styles on the subsite dropdown Updated LeftAndMain_Subsites.js to work with jQuery entwine Started porting the SubsitesTreeDropdownField.js to use jQuery entwine and work with the new TreeDropdownField.js Fixed issue causing crash when viewing a page who is linked to by a subsite virtual page Removed unused methods on SubsitesTreeDropdownField.js Re-added classes that were moved Fixed hard crash after saving caused by the many_many definition on SiteTreeSubsites Replaced deprecated DataObjectSet creation with ArrayList Compatibility fixes with SS 3.0 beta 2 Fixed crash in cms caused by no parameter being passed to the SubsiteReportWrapper constructor Proper fix for report wrapper Removed table list field in favor of a basic grid field Fixed updateCMSFields() for file subsites Migrated translations to yml Fixed issue causing the current page to not get cleared when changing subsites in the cms Fixed virtual page icon Fixed language files issue
2012-03-25 18:35:01 +02:00
public function getVirtualFields() {
$fields = parent::getVirtualFields();
foreach($fields as $k => $v) {
if($v == 'SubsiteID') unset($fields[$k]);
}
2016-09-22 16:38:29 +02:00
foreach(self::$db as $field => $type) if (in_array($field, $fields)) unset($fields[array_search($field, $fields)]);
return $fields;
}
2016-09-22 16:38:29 +02:00
BUG: Modifying the module to work with SS 3.0 Replaced deprecated DataObjectDecorator with DataExtension Fixed hard crashes in the cms Updated to support new LeftAndMain template structure Made the subsites model admin functional Moved the LeftAndMain_Menu template up a directory so it overrides the core Fixed some errors caused by changes to the framework Re-organized the code folder Fixed permission issue causing to default to first subsite regardless if it is the default or not Fixed crashes on the subsite virtual page when creating/editing Removed toDropdownMap() calls replacing with map() Fixed the URLSegment field on subsites Fixed error when detecting subsite for a domain Improved styles on the subsite dropdown Updated LeftAndMain_Subsites.js to work with jQuery entwine Started porting the SubsitesTreeDropdownField.js to use jQuery entwine and work with the new TreeDropdownField.js Fixed issue causing crash when viewing a page who is linked to by a subsite virtual page Removed unused methods on SubsitesTreeDropdownField.js Re-added classes that were moved Fixed hard crash after saving caused by the many_many definition on SiteTreeSubsites Replaced deprecated DataObjectSet creation with ArrayList Compatibility fixes with SS 3.0 beta 2 Fixed crash in cms caused by no parameter being passed to the SubsiteReportWrapper constructor Proper fix for report wrapper Removed table list field in favor of a basic grid field Fixed updateCMSFields() for file subsites Migrated translations to yml Fixed issue causing the current page to not get cleared when changing subsites in the cms Fixed virtual page icon Fixed language files issue
2012-03-25 18:35:01 +02:00
public function syncLinkTracking() {
$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;
Subsite::$disable_subsite_filter = $oldState;
}
BUG: Modifying the module to work with SS 3.0 Replaced deprecated DataObjectDecorator with DataExtension Fixed hard crashes in the cms Updated to support new LeftAndMain template structure Made the subsites model admin functional Moved the LeftAndMain_Menu template up a directory so it overrides the core Fixed some errors caused by changes to the framework Re-organized the code folder Fixed permission issue causing to default to first subsite regardless if it is the default or not Fixed crashes on the subsite virtual page when creating/editing Removed toDropdownMap() calls replacing with map() Fixed the URLSegment field on subsites Fixed error when detecting subsite for a domain Improved styles on the subsite dropdown Updated LeftAndMain_Subsites.js to work with jQuery entwine Started porting the SubsitesTreeDropdownField.js to use jQuery entwine and work with the new TreeDropdownField.js Fixed issue causing crash when viewing a page who is linked to by a subsite virtual page Removed unused methods on SubsitesTreeDropdownField.js Re-added classes that were moved Fixed hard crash after saving caused by the many_many definition on SiteTreeSubsites Replaced deprecated DataObjectSet creation with ArrayList Compatibility fixes with SS 3.0 beta 2 Fixed crash in cms caused by no parameter being passed to the SubsiteReportWrapper constructor Proper fix for report wrapper Removed table list field in favor of a basic grid field Fixed updateCMSFields() for file subsites Migrated translations to yml Fixed issue causing the current page to not get cleared when changing subsites in the cms Fixed virtual page icon Fixed language files issue
2012-03-25 18:35:01 +02:00
public function onBeforeWrite() {
parent::onBeforeWrite();
2016-09-22 16:38:29 +02:00
if($this->CustomMetaTitle) $this->MetaTitle = $this->CustomMetaTitle;
else {
2016-09-22 16:38:29 +02:00
$this->MetaTitle = $this->ContentSource()->MetaTitle ? $this->ContentSource()->MetaTitle : $this->MetaTitle;
}
if($this->CustomMetaKeywords) $this->MetaKeywords = $this->CustomMetaKeywords;
else {
2016-09-22 16:38:29 +02:00
$this->MetaKeywords = $this->ContentSource()->MetaKeywords ? $this->ContentSource()->MetaKeywords : $this->MetaKeywords;
}
if($this->CustomMetaDescription) $this->MetaDescription = $this->CustomMetaDescription;
else {
2016-09-22 16:38:29 +02:00
$this->MetaDescription = $this->ContentSource()->MetaDescription ? $this->ContentSource()->MetaDescription : $this->MetaDescription;
}
if($this->CustomExtraMeta) $this->ExtraMeta = $this->CustomExtraMeta;
else {
2016-09-22 16:38:29 +02:00
$this->ExtraMeta = $this->ContentSource()->ExtraMeta ? $this->ContentSource()->ExtraMeta : $this->ExtraMeta;
}
}
2016-09-22 16:38:29 +02:00
BUG: Modifying the module to work with SS 3.0 Replaced deprecated DataObjectDecorator with DataExtension Fixed hard crashes in the cms Updated to support new LeftAndMain template structure Made the subsites model admin functional Moved the LeftAndMain_Menu template up a directory so it overrides the core Fixed some errors caused by changes to the framework Re-organized the code folder Fixed permission issue causing to default to first subsite regardless if it is the default or not Fixed crashes on the subsite virtual page when creating/editing Removed toDropdownMap() calls replacing with map() Fixed the URLSegment field on subsites Fixed error when detecting subsite for a domain Improved styles on the subsite dropdown Updated LeftAndMain_Subsites.js to work with jQuery entwine Started porting the SubsitesTreeDropdownField.js to use jQuery entwine and work with the new TreeDropdownField.js Fixed issue causing crash when viewing a page who is linked to by a subsite virtual page Removed unused methods on SubsitesTreeDropdownField.js Re-added classes that were moved Fixed hard crash after saving caused by the many_many definition on SiteTreeSubsites Replaced deprecated DataObjectSet creation with ArrayList Compatibility fixes with SS 3.0 beta 2 Fixed crash in cms caused by no parameter being passed to the SubsiteReportWrapper constructor Proper fix for report wrapper Removed table list field in favor of a basic grid field Fixed updateCMSFields() for file subsites Migrated translations to yml Fixed issue causing the current page to not get cleared when changing subsites in the cms Fixed virtual page icon Fixed language files issue
2012-03-25 18:35:01 +02:00
public function validURLSegment() {
$isValid = parent::validURLSegment();
2016-09-22 16:38:29 +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;
$parentFilter = null;
2016-09-22 16:38:29 +02:00
if(Config::inst()->get('SilverStripe\\CMS\\Model\\SiteTree', 'nested_urls')) {
if($this->ParentID) {
$parentFilter = " AND \"SiteTree\".\"ParentID\" = $this->ParentID";
} else {
$parentFilter = ' AND "SiteTree"."ParentID" = 0';
}
}
2016-09-22 16:38:29 +02:00
$origDisableSubsiteFilter = Subsite::$disable_subsite_filter;
Subsite::$disable_subsite_filter = true;
$existingPage = DataObject::get_one(
2016-09-22 16:38:29 +02:00
'SilverStripe\\CMS\\Model\\SiteTree',
"\"URLSegment\" = '$this->URLSegment' $IDFilter $parentFilter",
false // disable cache, it doesn't include subsite status in the key
);
Subsite::$disable_subsite_filter = $origDisableSubsiteFilter;
$existingPageInSubsite = DataObject::get_one(
2016-09-22 16:38:29 +02:00
'SilverStripe\\CMS\\Model\\SiteTree',
"\"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);
}
2016-09-22 16:38:29 +02:00
return $isValid;
}
}