silverstripe-fulltextsearch/code/search/variants/SearchVariantVersioned.php

86 lines
2.3 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\FullTextSearch\Search\Variants;
2015-11-21 07:19:20 +01:00
class SearchVariantVersioned extends SearchVariant
{
public function appliesToEnvironment()
{
return class_exists('Versioned');
}
2015-11-21 07:19:20 +01:00
public function appliesTo($class, $includeSubclasses)
{
return SearchIntrospection::has_extension($class, 'Versioned', $includeSubclasses);
}
2015-11-21 07:19:20 +01:00
public function currentState()
{
return Versioned::current_stage();
}
public function reindexStates()
{
return array('Stage', 'Live');
}
public function activateState($state)
{
Versioned::reading_stage($state);
}
public function alterDefinition($class, $index)
2015-11-21 07:19:20 +01:00
{
$self = get_class($this);
$this->addFilterField($index, '_versionedstage', array(
2015-11-21 07:19:20 +01:00
'name' => '_versionedstage',
'field' => '_versionedstage',
'fullfield' => '_versionedstage',
'base' => ClassInfo::baseDataClass($class),
'origin' => $class,
2015-11-21 07:19:20 +01:00
'type' => 'String',
'lookup_chain' => array(array('call' => 'variant', 'variant' => $self, 'method' => 'currentState'))
));
2015-11-21 07:19:20 +01:00
}
2015-11-21 07:19:20 +01:00
public function alterQuery($query, $index)
{
$stage = Versioned::current_stage();
$query->filter('_versionedstage', array($stage, SearchQuery::$missing));
}
2015-11-21 07:19:20 +01:00
public function extractManipulationState(&$manipulation)
{
$self = get_class($this);
2015-11-21 07:19:20 +01:00
foreach ($manipulation as $table => $details) {
$class = $details['class'];
$stage = 'Stage';
2015-11-21 07:19:20 +01:00
if (preg_match('/^(.*)_Live$/', $table, $matches)) {
$class = $matches[1];
$stage = 'Live';
}
2015-11-21 07:19:20 +01:00
if (ClassInfo::exists($class) && $this->appliesTo($class, false)) {
$manipulation[$table]['class'] = $class;
$manipulation[$table]['state'][$self] = $stage;
}
}
}
2015-11-21 07:19:20 +01:00
public function extractStates(&$table, &$ids, &$fields)
{
$class = $table;
$suffix = null;
2015-11-21 07:19:20 +01:00
if (ClassInfo::exists($class) && $this->appliesTo($class, false)) {
$table = $class;
$self = get_class($this);
2015-11-21 07:19:20 +01:00
foreach ($ids as $i => $statefulid) {
$ids[$i]['state'][$self] = $suffix ? $suffix : 'Stage';
}
}
}
}