1 | <?php |
||||
2 | /** |
||||
3 | * Class SubsiteState|Firesphere\SolrSubsites\States\SubsiteState Enable each subsite to be indexed independently by |
||||
4 | * switching the SiteState |
||||
5 | * {@see \Firesphere\SolrSearch\States\SiteState} and {@see \Firesphere\SolrSearch\Interfaces\SiteStateInterface} |
||||
6 | * |
||||
7 | * @package Firesphere\Solr\Subsites |
||||
8 | * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo |
||||
9 | * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy |
||||
10 | */ |
||||
11 | |||||
12 | |||||
13 | namespace Firesphere\SolrSubsites\States; |
||||
14 | |||||
15 | use Firesphere\SolrSearch\Interfaces\SiteStateInterface; |
||||
16 | use Firesphere\SolrSearch\Queries\BaseQuery; |
||||
17 | use Firesphere\SolrSearch\States\SiteState; |
||||
18 | use SilverStripe\Core\ClassInfo; |
||||
19 | use SilverStripe\ORM\DataObject; |
||||
20 | use SilverStripe\Subsites\Model\Subsite; |
||||
21 | use SilverStripe\Subsites\State\SubsiteState as BaseSubsiteState; |
||||
22 | |||||
23 | /** |
||||
24 | * Class \Firesphere\SolrSubsites\States\SubsiteState |
||||
25 | * |
||||
26 | * Apply states for Subsites |
||||
27 | * |
||||
28 | * @package Firesphere\Solr\Subsites |
||||
29 | */ |
||||
30 | class SubsiteState extends SiteState implements SiteStateInterface |
||||
31 | { |
||||
32 | |||||
33 | /** |
||||
34 | * Is this state applicable to this extension |
||||
35 | * In case of subsites, only apply if there actually are subsites |
||||
36 | * |
||||
37 | * @param int|string $state |
||||
38 | * @return bool |
||||
39 | */ |
||||
40 | 1 | public function stateIsApplicable($state): bool |
|||
41 | { |
||||
42 | 1 | return Subsite::get()->byID($state) !== null && $state !== 0; |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
43 | } |
||||
44 | |||||
45 | /** |
||||
46 | * Reset the SiteState to it's default state |
||||
47 | * In case of subsites, we don't care about it, as it's handled at query time |
||||
48 | * |
||||
49 | * @param string|null $state |
||||
50 | * @return mixed |
||||
51 | */ |
||||
52 | 1 | public function setDefaultState($state = null) |
|||
53 | { |
||||
54 | 1 | singleton(BaseSubsiteState::class)->setUseSessions(true); |
|||
55 | 1 | Subsite::changeSubsite($state); |
|||
0 ignored issues
–
show
It seems like
$state can also be of type string ; however, parameter $subsite of SilverStripe\Subsites\Mo...ubsite::changeSubsite() does only seem to accept SilverStripe\Subsites\Model\Subsite|integer , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
56 | 1 | } |
|||
57 | |||||
58 | /** |
||||
59 | * Return the current state of the site |
||||
60 | * The current state does not need to be reset in any way for pages |
||||
61 | * |
||||
62 | * @return string|null |
||||
63 | */ |
||||
64 | 1 | public function currentState() |
|||
65 | { |
||||
66 | 1 | $subsite = Subsite::currentSubsite(); |
|||
67 | 1 | return $subsite ? $subsite->ID : null; |
|||
0 ignored issues
–
show
|
|||||
68 | } |
||||
69 | |||||
70 | /** |
||||
71 | * Activate a given state. This should only be done if the state is applicable |
||||
72 | * In the case of Subsites, we just want to disable the filter |
||||
73 | * |
||||
74 | * @param int $state |
||||
75 | * @return void |
||||
76 | */ |
||||
77 | 1 | public function activateState($state) |
|||
78 | { |
||||
79 | 1 | Subsite::changeSubsite($state); |
|||
80 | 1 | } |
|||
81 | |||||
82 | /** |
||||
83 | * Method to alter the query. Can be no-op. |
||||
84 | * |
||||
85 | * @param BaseQuery $query |
||||
86 | * @return void |
||||
87 | */ |
||||
88 | 1 | public function updateQuery(&$query) |
|||
89 | { |
||||
90 | // Only add a Subsite filter if there are actually subsites to filter on |
||||
91 | 1 | if (!Subsite::$disable_subsite_filter && Subsite::currentSubsite()) { |
|||
92 | 1 | foreach ($query->getClasses() as $class) { |
|||
93 | 1 | $this->addSubsiteFilter($query, $class); |
|||
94 | } |
||||
95 | } |
||||
96 | 1 | } |
|||
97 | |||||
98 | /** |
||||
99 | * Add the Subsite specific filter if the class has the extension applied |
||||
100 | * |
||||
101 | * @param BaseQuery $query |
||||
102 | * @param string $class |
||||
103 | */ |
||||
104 | 1 | protected function addSubsiteFilter(&$query, $class): void |
|||
105 | { |
||||
106 | 1 | if (DataObject::getSchema()->hasOneComponent($class, 'Subsite')) { |
|||
107 | 1 | $class = ClassInfo::shortName($class); |
|||
108 | 1 | $query->addFilter($class . '.SubsiteID', Subsite::currentSubsite()->ID); |
|||
109 | } |
||||
110 | 1 | } |
|||
111 | } |
||||
112 |