From 761493f64e8cdbf0c0558b4dfebeecf8247afc05 Mon Sep 17 00:00:00 2001 From: Tom Rix Date: Mon, 1 Mar 2010 02:59:04 +0000 Subject: [PATCH] --- _config.php | 1 + code/SiteConfigSubsites.php | 38 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 code/SiteConfigSubsites.php diff --git a/_config.php b/_config.php index e59da04..ca2dc3c 100644 --- a/_config.php +++ b/_config.php @@ -16,5 +16,6 @@ Object::add_extension('Group', 'GroupSubsites'); Object::add_extension('Member', 'MemberSubsites'); Object::add_extension('File', 'FileSubsites'); Object::add_extension('ErrorPage', 'ErrorPageSubsite'); +if (class_exists('SiteConfig')) Object::add_extension('SiteConfig', 'SiteConfigSubsites'); ?> \ No newline at end of file diff --git a/code/SiteConfigSubsites.php b/code/SiteConfigSubsites.php new file mode 100644 index 0000000..39c0a2e --- /dev/null +++ b/code/SiteConfigSubsites.php @@ -0,0 +1,38 @@ + array( + 'Subsite' => 'Subsite', // The subsite that this page belongs to + ) + ); + } + + /** + * 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... + if (!$query->where || (!preg_match('/\.(\'|"|`|)ID(\'|"|`|)( ?)=/', $query->where[0]))) { + + if($context = DataObject::context_obj()) $subsiteID = (int)$context->SubsiteID; + else $subsiteID = (int)Subsite::currentSubsiteID(); + + $tableName = array_shift(array_keys($query->from)); + if($tableName != 'SiteConfig') return; + $query->where[] = "`$tableName`.SubsiteID IN ($subsiteID)"; + } + } + + function augmentBeforeWrite() { + if((!is_numeric($this->owner->ID) || !$this->owner->ID) && !$this->owner->SubsiteID) { + $this->owner->SubsiteID = Subsite::currentSubsiteID(); + } + } +}