Issues (68)

src/Extensions/EmailTemplateSubsiteExtension.php (11 issues)

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;
0 ignored issues
show
The type LeKoala\EmailTemplates\Models\EmailTemplate 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...
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
0 ignored issues
show
Deprecated Code introduced by
The class SilverStripe\ORM\DataExtension has been deprecated: 5.3.0 Subclass SilverStripe\Core\Extension\Extension instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

21
class EmailSubsiteExtension extends /** @scrutinizer ignore-deprecated */ DataExtension
Loading history...
22
{
23
24
    private static $has_one = [
0 ignored issues
show
The private property $has_one is not used, and could be removed.
Loading history...
25
        'Subsite' => 'Subsite',
26
    ];
27
28
    public function isMainDataObject()
29
    {
30
        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...
Bug Best Practice introduced by
The property SubsiteID does not exist on LeKoala\EmailTemplates\Models\Emailing. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property SubsiteID does not exist on LeKoala\EmailTemplates\Models\SentEmail. Since you implemented __get, consider adding a @property annotation.
Loading history...
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) {
0 ignored issues
show
The condition $dataQuery->getQueryPara...site.filter') === false is always false.
Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The property SubsiteID does not exist on LeKoala\EmailTemplates\Models\Emailing. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property SubsiteID does not exist on LeKoala\EmailTemplates\Models\SentEmail. Since you implemented __get, consider adding a @property annotation.
Loading history...
83
            $this->owner->SubsiteID = SubsiteHelper::currentSubsiteID();
0 ignored issues
show
Bug Best Practice introduced by
The property SubsiteID does not exist on LeKoala\EmailTemplates\Models\SentEmail. Since you implemented __set, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property SubsiteID does not exist on LeKoala\EmailTemplates\Models\Emailing. Since you implemented __set, consider adding a @property annotation.
Loading history...
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