2011-05-02 06:33:05 +02:00
|
|
|
<?php
|
2017-04-26 13:16:32 +02:00
|
|
|
|
2017-04-21 03:26:24 +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
|
|
|
}
|
2012-07-19 02:52:37 +02: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
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
|
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);
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2016-04-15 07:59:10 +02:00
|
|
|
$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),
|
2016-04-15 07:59:10 +02:00
|
|
|
'origin' => $class,
|
2015-11-21 07:19:20 +01:00
|
|
|
'type' => 'String',
|
|
|
|
'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
|
|
|
}
|
2011-05-02 06:33:05 +02: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));
|
|
|
|
}
|
2016-04-15 07:59:10 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
public function extractManipulationState(&$manipulation)
|
|
|
|
{
|
|
|
|
$self = get_class($this);
|
2016-04-15 07:59:10 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
foreach ($manipulation as $table => $details) {
|
|
|
|
$class = $details['class'];
|
|
|
|
$stage = 'Stage';
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
if (preg_match('/^(.*)_Live$/', $table, $matches)) {
|
|
|
|
$class = $matches[1];
|
|
|
|
$stage = 'Live';
|
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
public function extractStates(&$table, &$ids, &$fields)
|
|
|
|
{
|
|
|
|
$class = $table;
|
|
|
|
$suffix = null;
|
2011-05-02 06:33:05 +02:00
|
|
|
|
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
if (ClassInfo::exists($class) && $this->appliesTo($class, false)) {
|
|
|
|
$table = $class;
|
|
|
|
$self = get_class($this);
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
foreach ($ids as $i => $statefulid) {
|
|
|
|
$ids[$i]['state'][$self] = $suffix ? $suffix : 'Stage';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
}
|