2013-02-12 22:16:39 +01:00
|
|
|
<?php
|
2017-04-21 02:27:01 +02:00
|
|
|
namespace SilverStripe\FullTextSearch\Search;
|
2015-11-21 07:19:20 +01:00
|
|
|
class SearchVariantSubsites extends SearchVariant
|
|
|
|
{
|
|
|
|
public function appliesToEnvironment()
|
|
|
|
{
|
|
|
|
return class_exists('Subsite');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function appliesTo($class, $includeSubclasses)
|
|
|
|
{
|
|
|
|
// Include all DataExtensions that contain a SubsiteID.
|
|
|
|
// TODO: refactor subsites to inherit a common interface, so we can run introspection once only.
|
|
|
|
return SearchIntrospection::has_extension($class, 'SiteTreeSubsites', $includeSubclasses) ||
|
|
|
|
SearchIntrospection::has_extension($class, 'GroupSubsites', $includeSubclasses) ||
|
|
|
|
SearchIntrospection::has_extension($class, 'FileSubsites', $includeSubclasses) ||
|
|
|
|
SearchIntrospection::has_extension($class, 'SiteConfigSubsites', $includeSubclasses);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function currentState()
|
|
|
|
{
|
|
|
|
return (string)Subsite::currentSubsiteID();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function reindexStates()
|
|
|
|
{
|
|
|
|
static $ids = null;
|
|
|
|
|
|
|
|
if ($ids === null) {
|
|
|
|
$ids = array('0');
|
|
|
|
foreach (DataObject::get('Subsite') as $subsite) {
|
|
|
|
$ids[] = (string)$subsite->ID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function activateState($state)
|
|
|
|
{
|
|
|
|
// We always just set the $_GET variable rather than store in Session - this always works, has highest priority
|
|
|
|
// in Subsite::currentSubsiteID() and doesn't persist unlike Subsite::changeSubsite
|
|
|
|
$_GET['SubsiteID'] = $state;
|
|
|
|
Permission::flush_permission_cache();
|
|
|
|
}
|
|
|
|
|
2016-04-15 07:59:10 +02:00
|
|
|
public function alterDefinition($class, $index)
|
2015-11-21 07:19:20 +01:00
|
|
|
{
|
|
|
|
$self = get_class($this);
|
2016-04-15 07:59:10 +02:00
|
|
|
|
|
|
|
// Add field to root
|
|
|
|
$this->addFilterField($index, '_subsite', array(
|
2015-11-21 07:19:20 +01:00
|
|
|
'name' => '_subsite',
|
|
|
|
'field' => '_subsite',
|
|
|
|
'fullfield' => '_subsite',
|
2016-04-15 07:59:10 +02:00
|
|
|
'base' => ClassInfo::baseDataClass($class),
|
|
|
|
'origin' => $class,
|
2015-11-21 07:19:20 +01:00
|
|
|
'type' => 'Int',
|
|
|
|
'lookup_chain' => array(array('call' => 'variant', 'variant' => $self, 'method' => 'currentState'))
|
2016-04-15 07:59:10 +02:00
|
|
|
));
|
2015-11-21 07:19:20 +01:00
|
|
|
}
|
|
|
|
|
2016-04-15 07:59:10 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
public function alterQuery($query, $index)
|
|
|
|
{
|
|
|
|
$subsite = Subsite::currentSubsiteID();
|
|
|
|
$query->filter('_subsite', array($subsite, SearchQuery::$missing));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public function extractManipulationWriteState(&$writes)
|
|
|
|
{
|
|
|
|
$self = get_class($this);
|
2016-04-15 07:59:10 +02:00
|
|
|
$query = new SQLQuery('"ID"', '"Subsite"');
|
|
|
|
$subsites = array_merge(array('0'), $query->execute()->column());
|
2015-11-21 07:19:20 +01:00
|
|
|
|
|
|
|
foreach ($writes as $key => $write) {
|
2016-04-15 07:59:10 +02:00
|
|
|
$applies = $this->appliesTo($write['class'], true);
|
|
|
|
if (!$applies) {
|
2015-11-21 07:19:20 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-12-02 15:08:38 +01:00
|
|
|
if (isset($write['fields']['SiteTree:SubsiteID'])) {
|
|
|
|
$subsitesForWrite = array($write['fields']['SiteTree:SubsiteID']);
|
|
|
|
}
|
|
|
|
// files in subsite 0 should be in all subsites as they are global
|
|
|
|
elseif (isset($write['fields']['File:SubsiteID']) && intval($write['fields']['File:SubsiteID']) !== 0) {
|
|
|
|
$subsitesForWrite = array($write['fields']['File:SubsiteID']);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$subsitesForWrite = $subsites;
|
|
|
|
}
|
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
$next = array();
|
|
|
|
foreach ($write['statefulids'] as $i => $statefulid) {
|
2016-12-02 15:08:38 +01:00
|
|
|
foreach ($subsitesForWrite as $subsiteID) {
|
2016-04-15 07:59:10 +02:00
|
|
|
$next[] = array(
|
|
|
|
'id' => $statefulid['id'],
|
|
|
|
'state' => array_merge(
|
|
|
|
$statefulid['state'],
|
|
|
|
array($self => (string)$subsiteID)
|
|
|
|
)
|
|
|
|
);
|
2015-11-21 07:19:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$writes[$key]['statefulids'] = $next;
|
|
|
|
}
|
|
|
|
}
|
2013-02-12 22:16:39 +01:00
|
|
|
}
|