1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DNADesign\ElementalSubsites\Extensions; |
4
|
|
|
|
5
|
|
|
use DNADesign\Elemental\Models\BaseElement; |
6
|
|
|
use DNADesign\Elemental\Models\ElementalArea; |
7
|
|
|
|
8
|
|
|
use SilverStripe\ORM\DataExtension; |
9
|
|
|
use SilverStripe\ORM\Queries\SQLSelect; |
10
|
|
|
use SilverStripe\ORM\DataQuery; |
11
|
|
|
use SilverStripe\Forms\FieldList; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @package elemental |
15
|
|
|
* |
16
|
|
|
* Make elements compatibale with subsites |
17
|
|
|
* Apply this extension to BaseElement |
18
|
|
|
*/ |
19
|
|
|
class ElementSubsiteExtension extends DataExtension |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
private static $has_one = array( |
|
|
|
|
23
|
|
|
'Subsite' => Subsite::class |
|
|
|
|
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
public function updateCMSFields(FieldList $fields) { |
27
|
|
|
|
28
|
|
|
// add SubsiteID if Subsites is installed andf Elemental has a subsite |
29
|
|
|
if(class_exists('Subsite')) { |
30
|
|
|
$fields->push(new HiddenField('SubsiteID', null, Subsite::currentSubsiteID())); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Update any requests for elements to limit the results to the current site |
36
|
|
|
*/ |
37
|
|
|
public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = NULL) { |
38
|
|
|
|
39
|
|
|
if(!class_exists('Subsite')) { |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (Subsite::$disable_subsite_filter) { |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($dataQuery && $dataQuery->getQueryParam('Subsite.filter') === false) { |
|
|
|
|
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($query->filtersOnID()) { |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (Subsite::$force_subsite) { |
56
|
|
|
$subsiteID = Subsite::$force_subsite; |
57
|
|
|
} else { |
58
|
|
|
$subsiteID = (int)Subsite::currentSubsiteID(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// The foreach is an ugly way of getting the first key :-) |
62
|
|
|
foreach ($query->getFrom() as $tableName => $info) { |
63
|
|
|
// The tableName should be Element or Element_Live... |
64
|
|
|
if(strpos($tableName, 'Element') !== false) { |
65
|
|
|
$query->addWhere("\"$tableName\".\"SubsiteID\" IN ($subsiteID)"); |
66
|
|
|
break; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
|