From 7f2e9a4e72dd94a75ee0e1dff02b7e522884c3a2 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Wed, 13 Feb 2013 10:16:39 +1300 Subject: [PATCH] NEW Add variant for regular subsite module. --- code/search/SearchVariantSubsites.php | 92 +++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 code/search/SearchVariantSubsites.php diff --git a/code/search/SearchVariantSubsites.php b/code/search/SearchVariantSubsites.php new file mode 100644 index 0000000..f2a9b37 --- /dev/null +++ b/code/search/SearchVariantSubsites.php @@ -0,0 +1,92 @@ +ID; + } + + return $ids; + } + + function activateState($state) { + if (Controller::has_curr()) { + Subsite::changeSubsite($state); + } + else { + // TODO: This is a nasty hack - calling Subsite::changeSubsite after request ends + // throws error because no current controller to access session on + $_GET['SubsiteID'] = $state; + } + } + + function alterDefinition($base, $index) { + $self = get_class($this); + + $index->filterFields['_subsite'] = array( + 'name' => '_subsite', + 'field' => '_subsite', + 'fullfield' => '_subsite', + 'base' => $base, + 'origin' => $base, + 'type' => 'Int', + 'lookup_chain' => array(array('call' => 'variant', 'variant' => $self, 'method' => 'currentState')) + ); + } + + function alterQuery($query, $index) { + $subsite = Subsite::currentSubsiteID(); + $query->filter('_subsite', array($subsite, SearchQuery::$missing)); + } + + static $subsites = null; + + /** + * We need _really_ complicated logic to find just the changed subsites (because we use versions there's no explicit + * deletes, just new versions with different members) so just always use all of them + */ + function extractManipulationWriteState(&$writes) { + $self = get_class($this); + + foreach ($writes as $key => $write) { + if (!$this->appliesTo($write['class'], true)) continue; + + if (self::$subsites === null) { + $query = new SQLQuery('ID', 'Subsite'); + self::$subsites = array_merge(array('0'), $query->execute()->column()); + } + + $next = array(); + + foreach ($write['statefulids'] as $i => $statefulid) { + foreach (self::$subsites as $subsiteID) { + $next[] = array('id' => $statefulid['id'], 'state' => array_merge($statefulid['state'], array($self => $subsiteID))); + } + } + + $writes[$key]['statefulids'] = $next; + } + } + +}