Issues (186)

src/Search/Variants/SearchVariantVersioned.php (1 issue)

Severity
1
<?php
2
3
namespace SilverStripe\FullTextSearch\Search\Variants;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\Core\ClassInfo;
7
use SilverStripe\FullTextSearch\Search\SearchIntrospection;
8
use SilverStripe\Versioned\Versioned;
9
use SilverStripe\FullTextSearch\Search\Queries\SearchQuery;
10
11
class SearchVariantVersioned extends SearchVariant
12
{
13
    public function appliesTo($class, $includeSubclasses)
14
    {
15
        if (!$this->appliesToEnvironment()) {
16
            return false;
17
        }
18
19
        return SearchIntrospection::has_extension($class, Versioned::class, $includeSubclasses);
20
    }
21
22
    public function currentState()
23
    {
24
        return Versioned::get_stage();
25
    }
26
    public function reindexStates()
27
    {
28
        return [Versioned::DRAFT, Versioned::LIVE];
29
    }
30
    public function activateState($state)
31
    {
32
        Versioned::set_stage($state);
33
    }
34
35
    public function alterDefinition($class, $index)
36
    {
37
        $this->addFilterField($index, '_versionedstage', [
38
            'name' => '_versionedstage',
39
            'field' => '_versionedstage',
40
            'fullfield' => '_versionedstage',
41
            'base' => DataObject::getSchema()->baseDataClass($class),
42
            'origin' => $class,
43
            'type' => 'String',
44
            'lookup_chain' => [
45
                [
46
                    'call' => 'variant',
47
                    'variant' => get_class($this),
48
                    'method' => 'currentState'
49
                ]
50
            ]
51
        ]);
52
    }
53
54
    public function alterQuery($query, $index)
55
    {
56
        $query->addFilter('_versionedstage', [
57
            $this->currentState(),
58
            SearchQuery::$missing
59
        ]);
60
    }
61
62
    public function extractManipulationState(&$manipulation)
63
    {
64
        foreach ($manipulation as $table => $details) {
65
            $class = $details['class'];
66
            $stage = Versioned::DRAFT;
67
68
            if (preg_match('/^(.*)_' . Versioned::LIVE . '$/', $table, $matches)) {
69
                $class = DataObject::getSchema()->tableClass($matches[1]);
70
                $stage = Versioned::LIVE;
71
            }
72
73
            if (ClassInfo::exists($class) && $this->appliesTo($class, false)) {
74
                $manipulation[$table]['class'] = $class;
75
                $manipulation[$table]['state'][get_class($this)] = $stage;
76
            }
77
        }
78
    }
79
80
    public function extractStates(&$table, &$ids, &$fields)
0 ignored issues
show
The parameter $fields is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

80
    public function extractStates(&$table, &$ids, /** @scrutinizer ignore-unused */ &$fields)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
    {
82
        $class = $table;
83
        $suffix = null;
84
85
86
        if (ClassInfo::exists($class) && $this->appliesTo($class, false)) {
87
            $table = $class;
88
89
            foreach ($ids as $i => $statefulid) {
90
                $ids[$i]['state'][get_class($this)] = $suffix ?: Versioned::DRAFT;
91
            }
92
        }
93
    }
94
}
95