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