Version: current

      1 <?php
      2 class SubsitesVirtualPage extends VirtualPage {
      3     static $db = array(
      4         'CustomMetaTitle' => 'Varchar(255)',
      5         'CustomMetaKeywords' => 'Varchar(255)',
      6         'CustomMetaDescription' => 'Text',
      7         'CustomExtraMeta' => 'HTMLText'
      8     );
      9
     10     function getCMSFields() {
     11         $fields = parent::getCMSFields();
     12
     13         $subsites = DataObject::get('Subsite');
     14         if(!$subsites) $subsites = new DataObjectSet();
     15         $subsites->insertFirst(new ArrayData(array('Title' => 'Main site', 'ID' => 0)));
     16
     17         $subsiteSelectionField = new DropdownField(
     18             "CopyContentFromID_SubsiteID",
     19             "Subsite",
     20             $subsites->toDropdownMap('ID', 'Title'),
     21             ($this->CopyContentFromID) ? $this->CopyContentFrom()->SubsiteID : Session::get('SubsiteID')
     22         );
     23         $fields->addFieldToTab(
     24             'Root.Content.Main',
     25             $subsiteSelectionField,
     26             'CopyContentFromID'
     27         );
     28
     29         // Setup the linking to the original page.
     30         $pageSelectionField = new SubsitesTreeDropdownField(
     31             "CopyContentFromID",
     32             _t('VirtualPage.CHOOSE', "Choose a page to link to"),
     33             "SiteTree",
     34             "ID",
     35             "MenuTitle"
     36         );
     37
     38         $pageSelectionField->setFilterFunction(create_function('$item', 'return !($item instanceof VirtualPage);'));
     39
     40         if(Controller::has_curr() && Controller::curr()->getRequest()) {
     41             $subsiteID = Controller::curr()->getRequest()->getVar('CopyContentFromID_SubsiteID');
     42             $pageSelectionField->setSubsiteID($subsiteID);
     43         }
     44         $fields->replaceField('CopyContentFromID', $pageSelectionField);
     45
     46         // Create links back to the original object in the CMS
     47         if($this->CopyContentFromID) {
     48             $editLink = "admin/show/$this->CopyContentFromID/?SubsiteID=" . $this->CopyContentFrom()->SubsiteID;
     49             $linkToContent = "
     50                 <a class=\"cmsEditlink\" href=\"$editLink\">" .
     51                 _t('VirtualPage.EDITCONTENT', 'Click here to edit the content') .
     52                 "</a>";
     53             $fields->removeByName("VirtualPageContentLinkLabel");
     54             $fields->addFieldToTab(
     55                 "Root.Content.Main",
     56                 $linkToContentLabelField = new LabelField('VirtualPageContentLinkLabel', $linkToContent),
     57                 'Title'
     58             );
     59             $linkToContentLabelField->setAllowHTML(true);
     60         }
     61
     62         $fields->addFieldToTab('Root.Content.Metadata', new TextField('CustomMetaTitle', 'Title (overrides inherited value from the source)'), 'MetaTitle');
     63         $fields->addFieldToTab('Root.Content.Metadata', new TextareaField('CustomMetaKeywords', 'Keywords (overrides inherited value from the source)'), 'MetaKeywords');
     64         $fields->addFieldToTab('Root.Content.Metadata', new TextareaField('CustomMetaDescription', 'Description (overrides inherited value from the source)'), 'MetaDescription');
     65         $fields->addFieldToTab('Root.Content.Metadata', new TextField('CustomExtraMeta', 'Custom Meta Tags (overrides inherited value from the source)'), 'ExtraMeta');
     66
     67         return $fields;
     68     }
     69
     70     function getVirtualFields() {
     71         $fields = parent::getVirtualFields();
     72         foreach($fields as $k => $v) {
     73             if($v == 'SubsiteID') unset($fields[$k]);
     74         }
     75
     76         foreach(self::$db as $field => $type) if (in_array($field, $fields)) unset($fields[array_search($field, $fields)]);
     77
     78         return $fields;
     79     }
     80
     81     function syncLinkTracking() {
     82         $oldState = Subsite::$disable_subsite_filter;
     83         Subsite::$disable_subsite_filter = true;
     84         if ($this->CopyContentFromID) $this->HasBrokenLink = DataObject::get_by_id('SiteTree', $this->CopyContentFromID) ? false : true;
     85         Subsite::$disable_subsite_filter = $oldState;
     86     }
     87
     88     function onBeforeWrite() {
     89         parent::onBeforeWrite();
     90
     91         if($this->CustomMetaTitle) $this->MetaTitle = $this->CustomMetaTitle;
     92         else {
     93             $this->MetaTitle = $this->ContentSource()->MetaTitle ? $this->ContentSource()->MetaTitle : $this->MetaTitle;
     94         }
     95         if($this->CustomMetaKeywords) $this->MetaKeywords = $this->CustomMetaKeywords;
     96         else {
     97             $this->MetaKeywords = $this->ContentSource()->MetaKeywords ? $this->ContentSource()->MetaKeywords : $this->MetaKeywords;
     98         }
     99         if($this->CustomMetaDescription) $this->MetaDescription = $this->CustomMetaDescription;
    100         else {
    101             $this->MetaDescription = $this->ContentSource()->MetaDescription ? $this->ContentSource()->MetaDescription : $this->MetaDescription;
    102         }
    103         if($this->CustomExtraMeta) $this->ExtraMeta = $this->CustomExtraMeta;
    104         else {
    105             $this->ExtraMeta = $this->ContentSource()->ExtraMeta ? $this->ContentSource()->ExtraMeta : $this->ExtraMeta;
    106         }
    107     }
    108 }
    109
    110 class SubsitesVirtualPage_Controller extends VirtualPage_Controller {
    111
    112     function reloadContent() {
    113         $this->failover->nextWriteDoesntCustomise();
    114         $this->failover->copyFrom($this->failover->CopyContentFrom());
    115         $this->failover->nextWriteDoesntCustomise();
    116         $this->failover->write();
    117         return;
    118     }
    119
    120     function init(){
    121         Subsite::$disable_subsite_filter = true;
    122
    123         parent::init();
    124
    125         Subsite::$disable_subsite_filter = false;
    126     }
    127 }
    128 ?>
    129

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -