/** * Update any requests to limit the results to the current site */ function augmentSQL(SQLQuery &$query) { if (Subsite::$disable_subsite_filter) { return; } // Don't run on delete queries, since they are always tied to // a specific ID. if ($query->delete) { return; } // If you're querying by ID, ignore the sub-site - this is a bit ugly... // if(!$query->where || (strpos($query->where[0], ".\"ID\" = ") === false && strpos($query->where[0], ".`ID` = ") === false && strpos($query->where[0], ".ID = ") === false && strpos($query->where[0], "ID = ") !== 0)) { if (!$query->where || !preg_match('/\\.(\'|"|`|)ID(\'|"|`|)( ?)=/', $query->where[0])) { if (Subsite::$force_subsite) { $subsiteID = Subsite::$force_subsite; } else { if ($context = DataObject::context_obj()) { $subsiteID = (int) $context->SubsiteID; } else { $subsiteID = (int) Subsite::currentSubsiteID(); } } // The foreach is an ugly way of getting the first key :-) foreach ($query->from as $tableName => $info) { // The tableName should be SiteTree or SiteTree_Live... if (strpos($tableName, 'SiteTree') === false) { break; } $query->where[] = "\"{$tableName}\".\"SubsiteID\" IN ({$subsiteID})"; break; } } }
/** * 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]) && !preg_match('/\\.?(\'|"|`|)SubsiteID(\'|"|`|)( ?)=/', $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})"; } }
/** * Update any requests to limit the results to the current site */ function augmentSQL(SQLQuery &$query) { // If you're querying by ID, ignore the sub-site - this is a bit ugly... (but it was WAYYYYYYYYY worse) if (!$query->where || !preg_match('/\\.(\'|"|`|)ID(\'|"|`|)/', $query->where[0])) { if ($context = DataObject::context_obj()) { $subsiteID = (int) $context->SubsiteID; } else { $subsiteID = (int) Subsite::currentSubsiteID(); } // The foreach is an ugly way of getting the first key :-) foreach ($query->from as $tableName => $info) { $where = "\"{$tableName}\".\"SubsiteID\" IN (0, {$subsiteID})"; $query->where[] = $where; break; } $isCounting = strpos($query->select[0], 'COUNT') !== false; // Ordering when deleting or counting doesn't apply if (!$query->delete && !$isCounting) { $query->orderby = "\"SubsiteID\"" . ($query->orderby ? ', ' : '') . $query->orderby; } } }
/** * Sets a 'context object' that can be used to provide hints about how to process a particular get / get_one request. * In particular, DataObjectDecorators can use this to amend queries more effectively. * Care must be taken to unset the context object after you're done with it, otherwise you will have a stale context, * which could cause horrible bugs. */ public static function set_context_obj($obj) { if($obj && self::$context_obj) user_error("Dataobject::set_context_obj passed " . $obj->class . "." . $obj->ID . " when there is already a context: " . self::$context_obj->class . '.' . self::$context_obj->ID, E_USER_WARNING); self::$context_obj = $obj; }
/** * Sets a 'context object' that can be used to provide hints about how to process a particular get / get_one request. * In particular, DataObjectDecorators can use this to amend queries more effectively. * Care must be taken to unset the context object after you're done with it, otherwise you will have a stale context, * which could cause horrible bugs. */ public static function set_context_obj($obj) { if ($obj && self::$context_obj) { user_error("Dataobject::set_context_obj called when there is already a context.", E_USER_WARNING); } self::$context_obj = $obj; }
/** * Update any requests to limit the results to the current site */ function augmentSQL(SQLQuery &$query) { if (Subsite::$disable_subsite_filter) { return; } if (Cookie::get('noSubsiteFilter') == 'true') { return; } // If you're querying by ID, ignore the sub-site - this is a bit ugly... if (!$query->filtersOnID()) { if ($context = DataObject::context_obj()) { $subsiteID = (int) $context->SubsiteID; } else { $subsiteID = (int) Subsite::currentSubsiteID(); } // Don't filter by Group_Subsites if we've already done that $hasGroupSubsites = false; foreach ($query->from as $item) { if (strpos($item, 'Group_Subsites') !== false) { $hasGroupSubsites = true; break; } } if (!$hasGroupSubsites) { if ($subsiteID) { $query->leftJoin("Group_Subsites", "\"Group_Subsites\".\"GroupID\" \n\t\t\t\t\t\t= \"Group\".\"ID\" AND \"Group_Subsites\".\"SubsiteID\" = {$subsiteID}"); $query->where[] = "(\"Group_Subsites\".\"SubsiteID\" IS NOT NULL OR\n\t\t\t\t\t\t\"Group\".\"AccessAllSubsites\" = 1)"; } else { $query->where[] = "\"Group\".\"AccessAllSubsites\" = 1"; } } // WORKAROUND for databases that complain about an ORDER BY when the column wasn't selected (e.g. SQL Server) if (!$query->select[0] == 'COUNT(*)') { $query->orderby = "\"AccessAllSubsites\" DESC" . ($query->orderby ? ', ' : '') . $query->orderby; } } }