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