2010-03-01 03:59:04 +01:00
|
|
|
<?php
|
|
|
|
|
2017-05-24 12:32:05 +02:00
|
|
|
namespace SilverStripe\Subsites\Extensions;
|
|
|
|
|
|
|
|
|
2016-09-22 16:38:29 +02:00
|
|
|
use SilverStripe\ORM\Queries\SQLSelect;
|
|
|
|
use SilverStripe\ORM\DataQuery;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\HiddenField;
|
|
|
|
use SilverStripe\ORM\DataExtension;
|
2017-05-24 12:32:05 +02:00
|
|
|
use SilverStripe\Subsites\Model\Subsite;
|
2017-05-24 13:36:04 +02:00
|
|
|
use SilverStripe\SiteConfig\SiteConfig;
|
|
|
|
|
2017-05-24 12:32:05 +02:00
|
|
|
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2010-03-01 03:59:04 +01:00
|
|
|
/**
|
|
|
|
* Extension for the SiteConfig object to add subsites support
|
|
|
|
*/
|
2013-05-24 06:26:57 +02:00
|
|
|
class SiteConfigSubsites extends DataExtension {
|
2013-05-06 12:21:09 +02:00
|
|
|
|
|
|
|
private static $has_one = array(
|
2017-05-24 13:36:04 +02:00
|
|
|
'Subsite' => Subsite::class, // The subsite that this page belongs to
|
2012-07-10 15:43:53 +02:00
|
|
|
);
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2010-03-01 03:59:04 +01:00
|
|
|
/**
|
|
|
|
* Update any requests to limit the results to the current site
|
|
|
|
*/
|
2015-11-20 03:32:52 +01:00
|
|
|
public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) {
|
2010-03-01 03:59:04 +01:00
|
|
|
if(Subsite::$disable_subsite_filter) return;
|
2014-08-26 01:37:16 +02:00
|
|
|
|
2010-03-01 03:59:04 +01:00
|
|
|
// If you're querying by ID, ignore the sub-site - this is a bit ugly...
|
2014-08-26 01:37:16 +02:00
|
|
|
if($query->filtersOnID()) return;
|
|
|
|
$regexp = '/^(.*\.)?("|`)?SubsiteID("|`)?\s?=/';
|
|
|
|
foreach($query->getWhereParameterised($parameters) as $predicate) {
|
|
|
|
if(preg_match($regexp, $predicate)) return;
|
2010-03-01 03:59:04 +01:00
|
|
|
}
|
2014-08-26 01:37:16 +02:00
|
|
|
|
|
|
|
/*if($context = DataObject::context_obj()) $subsiteID = (int)$context->SubsiteID;
|
|
|
|
else */$subsiteID = (int)Subsite::currentSubsiteID();
|
|
|
|
|
|
|
|
$froms=$query->getFrom();
|
|
|
|
$froms=array_keys($froms);
|
|
|
|
$tableName = array_shift($froms);
|
2017-05-24 13:36:04 +02:00
|
|
|
if($tableName != SiteConfig::class) return;
|
2014-08-26 01:37:16 +02:00
|
|
|
$query->addWhere("\"$tableName\".\"SubsiteID\" IN ($subsiteID)");
|
2010-03-01 03:59:04 +01:00
|
|
|
}
|
|
|
|
|
2010-03-21 23:32:22 +01:00
|
|
|
function onBeforeWrite() {
|
|
|
|
if((!is_numeric($this->owner->ID) || !$this->owner->ID) && !$this->owner->SubsiteID) $this->owner->SubsiteID = Subsite::currentSubsiteID();
|
2010-03-01 03:59:04 +01:00
|
|
|
}
|
2010-03-01 22:41:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a piece of text to keep DataObject cache keys appropriately specific
|
|
|
|
*/
|
|
|
|
function cacheKeyComponent() {
|
|
|
|
return 'subsite-'.Subsite::currentSubsiteID();
|
|
|
|
}
|
2013-05-24 06:26:57 +02:00
|
|
|
|
|
|
|
function updateCMSFields(FieldList $fields) {
|
|
|
|
$fields->push(new HiddenField('SubsiteID','SubsiteID', Subsite::currentSubsiteID()));
|
|
|
|
}
|
2010-03-01 03:59:04 +01:00
|
|
|
}
|