Passed
Push — master ( 2ea0b2...d8ed7e )
by Thomas
02:39 queued 11s
created

src/Extensions/EmailTemplateSubsiteExtension.php (4 issues)

1
<?php
2
3
namespace LeKoala\EmailTemplates\Extensions;
4
5
use SilverStripe\ORM\DataQuery;
6
use SilverStripe\ORM\DataExtension;
7
use SilverStripe\Security\Permission;
8
use LeKoala\Base\Subsite\SubsiteHelper;
0 ignored issues
show
The type LeKoala\Base\Subsite\SubsiteHelper was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use SilverStripe\ORM\Queries\SQLSelect;
10
11
/**
12
 * Add subsites support
13
 *
14
 * @author lekoala
15
 */
16
class EmailSubsiteExtension extends DataExtension
17
{
18
19
    private static $has_one = array(
0 ignored issues
show
The private property $has_one is not used, and could be removed.
Loading history...
20
        'Subsite' => 'Subsite',
21
    );
22
23
    public function isMainDataObject()
24
    {
25
        if ($this->owner->SubsiteID == 0) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $this->owner->SubsiteID of type mixed|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
26
            return true;
27
        }
28
        return false;
29
    }
30
31
    /**
32
     * Update any requests to limit the results to the current site
33
     */
34
    public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null)
35
    {
36
        if (SubsiteHelper::subsiteFilterDisabled()) {
37
            return;
38
        }
39
        if ($dataQuery && $dataQuery->getQueryParam('Subsite.filter') === false) {
0 ignored issues
show
The condition $dataQuery->getQueryPara...site.filter') === false is always false.
Loading history...
40
            return;
41
        }
42
43
        // If you're querying by ID, don't filter
44
        if ($query->filtersOnID()) {
45
            return;
46
        }
47
48
        // Don't run on delete queries, since they are always tied to a specific ID.
49
        // if ($query->getDelete()) {
50
        //     return;
51
        // }
52
53
        // If we match on a subsite, don't filter twice
54
        $regexp = '/^(.*\.)?("|`)?SubsiteID("|`)?\s?=/';
55
        foreach ($query->getWhereParameterised($parameters) as $predicate) {
56
            if (preg_match($regexp, $predicate)) {
57
                return;
58
            }
59
        }
60
61
        $subsiteID = (int) SubsiteHelper::currentSubsiteID();
62
63
        $froms = $query->getFrom();
64
        $froms = array_keys($froms);
65
        $tableName = array_shift($froms);
66
67
        if ($subsiteID) {
68
            $query->addWhere("\"$tableName\".\"SubsiteID\" IN ($subsiteID)");
69
        }
70
    }
71
72
    public function onBeforeWrite()
73
    {
74
        parent::onBeforeWrite();
75
76
        // Assign to current subsite when created
77
        if (!$this->owner->ID && !$this->owner->SubsiteID) {
78
            $this->owner->SubsiteID = SubsiteHelper::currentSubsiteID();
79
        }
80
    }
81
82
    public function canView($member = null)
83
    {
84
        return $this->canEdit($member);
85
    }
86
87
    /**
88
     * @param Member
89
     * @return boolean|null
90
     */
91
    public function canEdit($member = null)
92
    {
93
        return Permission::check('CMS_ACCESS', 'any', $member);
94
    }
95
96
    /**
97
     * @param Member
98
     * @return boolean|null
99
     */
100
    public function canCreate($member = null)
101
    {
102
        return Permission::check('CMS_ACCESS', 'any', $member);
103
    }
104
105
    /**
106
     * @param Member
107
     * @return boolean|null
108
     */
109
    public function canDelete($member = null)
110
    {
111
        return Permission::check('CMS_ACCESS', 'any', $member);
112
    }
113
114
    /**
115
     * Return a piece of text to keep DataObject cache keys appropriately specific
116
     */
117
    public function cacheKeyComponent()
118
    {
119
        return 'subsite-' . SubsiteHelper::currentSubsiteID();
120
    }
121
}
122