|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LeKoala\EmailTemplates\Extensions; |
|
4
|
|
|
|
|
5
|
|
|
use LeKoala\EmailTemplates\Helpers\SubsiteHelper; |
|
6
|
|
|
use LeKoala\EmailTemplates\Models\Emailing; |
|
7
|
|
|
use LeKoala\EmailTemplates\Models\EmailTemplate; |
|
|
|
|
|
|
8
|
|
|
use LeKoala\EmailTemplates\Models\SentEmail; |
|
9
|
|
|
use SilverStripe\ORM\DataQuery; |
|
10
|
|
|
use SilverStripe\ORM\DataExtension; |
|
11
|
|
|
use SilverStripe\Security\Permission; |
|
12
|
|
|
use SilverStripe\ORM\Queries\SQLSelect; |
|
13
|
|
|
use SilverStripe\Security\Member; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Add subsites support |
|
17
|
|
|
* |
|
18
|
|
|
* @property-read EmailTemplate|SentEmail|Emailing $owner |
|
19
|
|
|
* @author lekoala |
|
20
|
|
|
*/ |
|
21
|
|
|
class EmailSubsiteExtension extends DataExtension |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
private static $has_one = [ |
|
|
|
|
|
|
25
|
|
|
'Subsite' => 'Subsite', |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
public function isMainDataObject() |
|
29
|
|
|
{ |
|
30
|
|
|
if ($this->owner->SubsiteID == 0) { |
|
|
|
|
|
|
31
|
|
|
return true; |
|
32
|
|
|
} |
|
33
|
|
|
return false; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Update any requests to limit the results to the current site |
|
38
|
|
|
*/ |
|
39
|
|
|
public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) |
|
40
|
|
|
{ |
|
41
|
|
|
if (SubsiteHelper::subsiteFilterDisabled()) { |
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
if ($dataQuery && $dataQuery->getQueryParam('Subsite.filter') === false) { |
|
|
|
|
|
|
45
|
|
|
return; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// If you're querying by ID, don't filter |
|
49
|
|
|
if ($query->filtersOnID()) { |
|
50
|
|
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// Don't run on delete queries, since they are always tied to a specific ID. |
|
54
|
|
|
// if ($query->getDelete()) { |
|
55
|
|
|
// return; |
|
56
|
|
|
// } |
|
57
|
|
|
|
|
58
|
|
|
// If we match on a subsite, don't filter twice |
|
59
|
|
|
$regexp = '/^(.*\.)?("|`)?SubsiteID("|`)?\s?=/'; |
|
60
|
|
|
foreach ($query->getWhereParameterised($parameters) as $predicate) { |
|
61
|
|
|
if (preg_match($regexp, $predicate)) { |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$subsiteID = (int) SubsiteHelper::currentSubsiteID(); |
|
67
|
|
|
|
|
68
|
|
|
$froms = $query->getFrom(); |
|
69
|
|
|
$froms = array_keys($froms); |
|
70
|
|
|
$tableName = array_shift($froms); |
|
71
|
|
|
|
|
72
|
|
|
if ($subsiteID) { |
|
73
|
|
|
$query->addWhere("\"$tableName\".\"SubsiteID\" IN ($subsiteID)"); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function onBeforeWrite() |
|
78
|
|
|
{ |
|
79
|
|
|
parent::onBeforeWrite(); |
|
80
|
|
|
|
|
81
|
|
|
// Assign to current subsite when created |
|
82
|
|
|
if (!$this->owner->ID && !$this->owner->SubsiteID) { |
|
|
|
|
|
|
83
|
|
|
$this->owner->SubsiteID = SubsiteHelper::currentSubsiteID(); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function canView($member = null) |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->canEdit($member); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param Member $member |
|
94
|
|
|
* @return boolean|null |
|
95
|
|
|
*/ |
|
96
|
|
|
public function canEdit($member = null) |
|
97
|
|
|
{ |
|
98
|
|
|
return Permission::check('CMS_ACCESS', 'any', $member); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param Member $member |
|
103
|
|
|
* @return boolean|null |
|
104
|
|
|
*/ |
|
105
|
|
|
public function canCreate($member = null) |
|
106
|
|
|
{ |
|
107
|
|
|
return Permission::check('CMS_ACCESS', 'any', $member); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param Member $member |
|
112
|
|
|
* @return boolean|null |
|
113
|
|
|
*/ |
|
114
|
|
|
public function canDelete($member = null) |
|
115
|
|
|
{ |
|
116
|
|
|
return Permission::check('CMS_ACCESS', 'any', $member); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Return a piece of text to keep DataObject cache keys appropriately specific |
|
121
|
|
|
*/ |
|
122
|
|
|
public function cacheKeyComponent() |
|
123
|
|
|
{ |
|
124
|
|
|
return 'subsite-' . SubsiteHelper::currentSubsiteID(); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths