2010-03-01 03:59:04 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extension for the SiteConfig object to add subsites support
|
|
|
|
*/
|
2012-03-25 18:35:01 +02:00
|
|
|
class SiteConfigSubsites extends DataExtension {
|
2013-05-06 12:21:09 +02:00
|
|
|
|
|
|
|
private static $has_one = array(
|
2012-07-10 15:43:53 +02:00
|
|
|
'Subsite' => 'Subsite', // The subsite that this page belongs to
|
|
|
|
);
|
|
|
|
|
2010-03-01 03:59:04 +01:00
|
|
|
/**
|
|
|
|
* Update any requests to limit the results to the current site
|
|
|
|
*/
|
|
|
|
function augmentSQL(SQLQuery &$query) {
|
|
|
|
if(Subsite::$disable_subsite_filter) return;
|
|
|
|
|
|
|
|
// If you're querying by ID, ignore the sub-site - this is a bit ugly...
|
2010-03-21 23:32:22 +01:00
|
|
|
if (!$query->where || (!preg_match('/\.(\'|"|`|)ID(\'|"|`|)( ?)=/', $query->where[0]) && !preg_match('/\.?(\'|"|`|)SubsiteID(\'|"|`|)( ?)=/', $query->where[0]))) {
|
2012-03-25 18:35:01 +02:00
|
|
|
/*if($context = DataObject::context_obj()) $subsiteID = (int)$context->SubsiteID;
|
|
|
|
else */$subsiteID = (int)Subsite::currentSubsiteID();
|
2010-03-01 03:59:04 +01:00
|
|
|
|
2012-07-10 15:43:53 +02:00
|
|
|
$froms=$query->getFrom();
|
|
|
|
$froms=array_keys($froms);
|
2012-04-25 14:29:32 +02:00
|
|
|
$tableName = array_shift($froms);
|
2010-03-01 03:59:04 +01:00
|
|
|
if($tableName != 'SiteConfig') return;
|
2012-04-25 14:29:32 +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();
|
|
|
|
}
|
2010-03-01 03:59:04 +01:00
|
|
|
}
|