2008-12-02 04:50:06 +01:00
< ? php
class SubsitesVirtualPage extends VirtualPage {
2013-04-29 02:25:29 +02:00
private static $description = 'Displays the content of a page on another subsite' ;
2012-03-25 18:35:01 +02:00
public static $db = array (
2010-03-01 04:00:29 +01:00
'CustomMetaTitle' => 'Varchar(255)' ,
'CustomMetaKeywords' => 'Varchar(255)' ,
'CustomMetaDescription' => 'Text' ,
'CustomExtraMeta' => 'HTMLText'
);
2008-12-02 04:50:06 +01:00
2012-03-25 18:35:01 +02:00
public function getCMSFields () {
2008-12-02 04:50:06 +01:00
$fields = parent :: getCMSFields ();
2010-03-01 22:26:35 +01:00
$subsites = DataObject :: get ( 'Subsite' );
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 ());
}
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
$subsiteSelectionField = new DropdownField (
" CopyContentFromID_SubsiteID " ,
" Subsite " ,
2012-03-25 18:35:01 +02:00
$subsites -> map ( 'ID' , 'Title' ),
2010-03-01 22:26:35 +01:00
( $this -> CopyContentFromID ) ? $this -> CopyContentFrom () -> SubsiteID : Session :: get ( 'SubsiteID' )
);
$fields -> addFieldToTab (
2012-03-25 18:35:01 +02:00
'Root.Main' ,
2010-03-01 22:26:35 +01:00
$subsiteSelectionField ,
'CopyContentFromID'
);
2008-12-02 04:50:06 +01:00
// Setup the linking to the original page.
$pageSelectionField = new SubsitesTreeDropdownField (
" CopyContentFromID " ,
_t ( 'VirtualPage.CHOOSE' , " Choose a page to link to " ),
2010-03-01 03:58:31 +01:00
" SiteTree " ,
" ID " ,
" MenuTitle "
2008-12-02 04:50:06 +01:00
);
2009-05-04 07:04:08 +02:00
2010-09-22 03:43:57 +02:00
if ( Controller :: has_curr () && Controller :: curr () -> getRequest ()) {
2012-03-25 18:35:01 +02:00
$subsiteID = Controller :: curr () -> getRequest () -> postVar ( '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 );
// Create links back to the original object in the CMS
if ( $this -> CopyContentFromID ) {
2012-03-25 18:35:01 +02:00
$editLink = " admin/page/edit/show/ $this->CopyContentFromID /?SubsiteID= " . $this -> CopyContentFrom () -> SubsiteID ;
2008-12-02 04:50:06 +01:00
$linkToContent = "
< a class = \ " cmsEditlink \" href= \" $editLink\ " > " .
_t ( 'VirtualPage.EDITCONTENT' , 'Click here to edit the content' ) .
" </a> " ;
$fields -> removeByName ( " VirtualPageContentLinkLabel " );
$fields -> addFieldToTab (
2012-03-25 18:35:01 +02:00
" Root.Main " ,
2008-12-02 04:50:06 +01:00
$linkToContentLabelField = new LabelField ( 'VirtualPageContentLinkLabel' , $linkToContent ),
'Title'
);
$linkToContentLabelField -> setAllowHTML ( true );
}
2012-07-10 15:43:53 +02:00
2012-07-11 15:32:10 +02:00
$fields -> addFieldToTab ( 'Root.Main' , new TextField ( 'CustomMetaTitle' , 'Title (overrides inherited value from the source)' ), 'MetaTitle' );
$fields -> addFieldToTab ( 'Root.Main' , new TextareaField ( 'CustomMetaKeywords' , 'Keywords (overrides inherited value from the source)' ), 'MetaKeywords' );
$fields -> addFieldToTab ( 'Root.Main' , new TextareaField ( 'CustomMetaDescription' , 'Description (overrides inherited value from the source)' ), 'MetaDescription' );
$fields -> addFieldToTab ( 'Root.Main' , new TextField ( 'CustomExtraMeta' , 'Custom Meta Tags (overrides inherited value from the source)' ), 'ExtraMeta' );
2010-03-01 04:00:29 +01:00
2008-12-02 04:50:06 +01:00
return $fields ;
}
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 ]);
}
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 ;
}
2010-03-01 04:00:29 +01: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 ;
2010-03-01 22:40:36 +01:00
if ( $this -> CopyContentFromID ) $this -> HasBrokenLink = DataObject :: get_by_id ( '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 ();
if ( $this -> CustomMetaTitle ) $this -> MetaTitle = $this -> CustomMetaTitle ;
2010-03-01 22:33:46 +01:00
else {
$this -> MetaTitle = $this -> ContentSource () -> MetaTitle ? $this -> ContentSource () -> MetaTitle : $this -> MetaTitle ;
}
2010-03-01 04:00:29 +01:00
if ( $this -> CustomMetaKeywords ) $this -> MetaKeywords = $this -> CustomMetaKeywords ;
2010-03-01 22:33:46 +01:00
else {
$this -> MetaKeywords = $this -> ContentSource () -> MetaKeywords ? $this -> ContentSource () -> MetaKeywords : $this -> MetaKeywords ;
}
2010-03-01 04:00:29 +01:00
if ( $this -> CustomMetaDescription ) $this -> MetaDescription = $this -> CustomMetaDescription ;
2010-03-01 22:33:46 +01:00
else {
$this -> MetaDescription = $this -> ContentSource () -> MetaDescription ? $this -> ContentSource () -> MetaDescription : $this -> MetaDescription ;
}
2010-03-01 04:00:29 +01:00
if ( $this -> CustomExtraMeta ) $this -> ExtraMeta = $this -> CustomExtraMeta ;
2010-03-01 22:33:46 +01:00
else {
$this -> ExtraMeta = $this -> ContentSource () -> ExtraMeta ? $this -> ContentSource () -> ExtraMeta : $this -> ExtraMeta ;
}
2010-03-01 04:00:29 +01:00
}
2011-08-22 17:52:21 +02:00
2012-03-25 18:35:01 +02:00
public function validURLSegment () {
2011-08-22 17:52:21 +02:00
$isValid = parent :: validURLSegment ();
// 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 ;
if ( self :: nested_urls ()) {
if ( $this -> ParentID ) {
$parentFilter = " AND \" SiteTree \" . \" ParentID \" = $this->ParentID " ;
} else {
$parentFilter = ' AND "SiteTree"."ParentID" = 0' ;
}
}
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 (
'SiteTree' ,
" \" 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 (
'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 );
}
return $isValid ;
}
2008-12-02 04:50:06 +01:00
}
class SubsitesVirtualPage_Controller extends VirtualPage_Controller {
2012-03-25 18:35:01 +02:00
public function reloadContent () {
2008-12-02 04:50:06 +01:00
$this -> failover -> copyFrom ( $this -> failover -> CopyContentFrom ());
$this -> failover -> write ();
return ;
}
2012-03-25 18:35:01 +02:00
public function init (){
2011-09-12 12:18:56 +02:00
$origDisableSubsiteFilter = Subsite :: $disable_subsite_filter ;
2008-12-02 04:50:06 +01:00
Subsite :: $disable_subsite_filter = true ;
parent :: init ();
2011-09-12 12:18:56 +02:00
Subsite :: $disable_subsite_filter = $origDisableSubsiteFilter ;
2008-12-02 04:50:06 +01:00
}
}