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

88 lines
2.5 KiB
PHP
Raw Normal View History

<?php
2017-04-26 13:16:32 +02:00
namespace SilverStripe\FullTextSearch\Search\Variants;
2017-04-26 13:16:32 +02:00
use SilverStripe\ORM\DataObject;
use SilverStripe\Core\ClassInfo;
use SilverStripe\FullTextSearch\Search\SearchIntrospection;
use SilverStripe\Versioned\Versioned;
use SilverStripe\FullTextSearch\Search\Queries\SearchQuery;
2015-11-21 07:19:20 +01:00
class SearchVariantVersioned extends SearchVariant
{
public function appliesTo($class, $includeSubclasses)
{
2017-04-22 11:24:33 +02:00
return SearchIntrospection::has_extension($class, Versioned::class, $includeSubclasses);
2015-11-21 07:19:20 +01:00
}
2015-11-21 07:19:20 +01:00
public function currentState()
{
2017-04-26 13:16:32 +02:00
return Versioned::get_stage();
2015-11-21 07:19:20 +01:00
}
public function reindexStates()
{
return array('Stage', 'Live');
}
public function activateState($state)
{
2017-04-26 13:16:32 +02:00
Versioned::set_stage($state);
2015-11-21 07:19:20 +01:00
}
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',
2017-04-26 13:16:32 +02:00
'base' => DataObject::getSchema()->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)
{
2017-04-26 13:16:32 +02:00
$stage = $this->currentState();
2015-11-21 07:19:20 +01:00
$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 = DataObject::getSchema()->tableClass($matches[1]);
2015-11-21 07:19:20 +01:00
$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';
}
}
}
}