|
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\SolrSubsites\States |
|
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
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class \Firesphere\SolrSubsites\States\SubsiteState |
|
24
|
|
|
* |
|
25
|
|
|
* Apply states for Subsites |
|
26
|
|
|
* |
|
27
|
|
|
* @package Firesphere\SolrSubsites\States |
|
28
|
|
|
*/ |
|
29
|
|
|
class SubsiteState extends SiteState implements SiteStateInterface |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Is this state applicable to this extension |
|
34
|
|
|
* In case of subsites, only apply if there actually are subsites |
|
35
|
|
|
* |
|
36
|
|
|
* @param int|string $state |
|
37
|
|
|
* @return bool |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function stateIsApplicable($state): bool |
|
40
|
|
|
{ |
|
41
|
1 |
|
return Subsite::get()->byID($state) !== null; |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Reset the SiteState to it's default state |
|
46
|
|
|
* In case of subsites, we don't care about it, as it's handled at query time |
|
47
|
|
|
* |
|
48
|
|
|
* @param string|null $state |
|
49
|
|
|
* @return mixed |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function setDefaultState($state = null) |
|
52
|
|
|
{ |
|
53
|
1 |
|
Subsite::changeSubsite($state); |
|
|
|
|
|
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Return the current state of the site |
|
58
|
|
|
* The current state does not need to be reset in any way for pages |
|
59
|
|
|
* |
|
60
|
|
|
* @return string|null |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function currentState() |
|
63
|
|
|
{ |
|
64
|
1 |
|
$subsite = Subsite::currentSubsite(); |
|
65
|
1 |
|
return $subsite ? $subsite->ID : null; |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Activate a given state. This should only be done if the state is applicable |
|
70
|
|
|
* States don't need to be activated, as we index all pages anyway, so set it to disabled |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $state |
|
73
|
|
|
* @return mixed |
|
74
|
|
|
* @todo This needs to properly set the state |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function activateState($state) |
|
77
|
|
|
{ |
|
78
|
1 |
|
Subsite::$disable_subsite_filter = true; |
|
79
|
1 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Method to alter the query. Can be no-op. |
|
83
|
|
|
* |
|
84
|
|
|
* @param BaseQuery $query |
|
85
|
|
|
* @return mixed |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function updateQuery(&$query) |
|
88
|
|
|
{ |
|
89
|
|
|
// Only add a Subsite filter if there are actually subsites to filter on |
|
90
|
1 |
|
if (!Subsite::$disable_subsite_filter && Subsite::currentSubsite()) { |
|
91
|
1 |
|
foreach ($query->getClasses() as $class) { |
|
92
|
1 |
|
$this->addSubsiteFilter($query, $class); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
1 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Add the Subsite specific filter if the class has the extension applied |
|
99
|
|
|
* |
|
100
|
|
|
* @param BaseQuery $query |
|
101
|
|
|
* @param string $class |
|
102
|
|
|
*/ |
|
103
|
1 |
|
protected function addSubsiteFilter(&$query, $class): void |
|
104
|
|
|
{ |
|
105
|
1 |
|
if (DataObject::getSchema()->hasOneComponent($class, 'Subsite')) { |
|
106
|
1 |
|
$class = ClassInfo::shortName($class); |
|
107
|
1 |
|
$query->addFilter($class . '.SubsiteID', Subsite::currentSubsite()->ID); |
|
108
|
|
|
} |
|
109
|
1 |
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|