Issues (68)

src/Helpers/SubsiteHelper.php (5 issues)

1
<?php
2
3
namespace LeKoala\EmailTemplates\Helpers;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Control\Director;
8
use SilverStripe\Control\HTTPRequest;
9
use SilverStripe\Subsites\Model\Subsite;
0 ignored issues
show
The type SilverStripe\Subsites\Model\Subsite 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...
10
use SilverStripe\Subsites\State\SubsiteState;
0 ignored issues
show
The type SilverStripe\Subsites\State\SubsiteState 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...
11
12
/**
13
 * Helps providing base functionnalities where including
14
 * subsite module is optional and yet provide a consistent api
15
 *
16
 * TODO: externalize this to a specific module
17
 */
18
class SubsiteHelper
19
{
20
    /**
21
     * @var boolean
22
     */
23
    protected static $previousState;
24
25
    /**
26
     * @var int
27
     */
28
    protected static $previousSubsite;
29
30
    /**
31
     * Return current subsite id (even if module is not installed, which returns 0)
32
     *
33
     * @return int
34
     */
35
    public static function currentSubsiteID()
36
    {
37
        if (self::usesSubsite() && class_exists(SubsiteState::class)) {
38
            return SubsiteState::singleton()->getSubsiteId();
39
        }
40
        return 0;
41
    }
42
43
    /**
44
     * @return Subsite|null
45
     */
46
    public static function currentSubsite()
47
    {
48
        $id = self::currentSubsiteID();
49
        if (self::usesSubsite() && class_exists(Subsite::class)) {
50
            /** @var Subsite|null */
51
            return DataObject::get_by_id(Subsite::class, $id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return SilverStripe\ORM\...el\Subsite::class, $id) also could return the type SilverStripe\ORM\DataObject which is incompatible with the documented return type SilverStripe\Subsites\Model\Subsite|null.
Loading history...
52
        }
53
        return null;
54
    }
55
56
    /**
57
     * Do we have the subsite module installed
58
     * TODO: check if it might be better to use module manifest instead?
59
     *
60
     * @return bool
61
     */
62
    public static function usesSubsite()
63
    {
64
        return class_exists(SubsiteState::class) && class_exists(Subsite::class);
65
    }
66
67
    public static function safeAbsoluteURL($absUrl)
68
    {
69
        if (!self::usesSubsite() || !class_exists(Subsite::class)) {
70
            return $absUrl;
71
        }
72
73
        $subsite = SubsiteHelper::currentSubsite();
74
        if ($subsite->hasMethod('getPrimarySubsiteDomain')) {
75
            $domain = $subsite->getPrimarySubsiteDomain();
76
            $link = $subsite->domain();
77
            $protocol = $domain->getFullProtocol();
78
        } else {
79
            $protocol = Director::protocol();
80
            $link = $subsite->domain();
81
        }
82
        $absUrl = preg_replace('/\/\/[^\/]+\//', '//' . $link . '/', $absUrl);
83
        $absUrl = preg_replace('/http(s)?:\/\//', $protocol, $absUrl);
84
85
        return $absUrl;
86
    }
87
88
89
    /**
90
     * @return bool
91
     */
92
    public static function subsiteFilterDisabled()
93
    {
94
        if (!self::usesSubsite() && class_exists(Subsite::class)) {
95
            return true;
96
        }
97
        return Subsite::$disable_subsite_filter;
98
    }
99
100
    /**
101
     * Enable subsite filter and store previous state
102
     *
103
     * @return void
104
     */
105
    public static function enableFilter()
106
    {
107
        if (!self::usesSubsite() || !class_exists(Subsite::class)) {
108
            return;
109
        }
110
        self::$previousState = Subsite::$disable_subsite_filter;
111
        Subsite::$disable_subsite_filter = false;
112
    }
113
114
    /**
115
     * Disable subsite filter and store previous state
116
     *
117
     * @return void
118
     */
119
    public static function disableFilter()
120
    {
121
        if (!self::usesSubsite() || !class_exists(Subsite::class)) {
122
            return;
123
        }
124
        self::$previousState = Subsite::$disable_subsite_filter;
125
        Subsite::$disable_subsite_filter = true;
126
    }
127
128
    /**
129
     * Restore subsite filter based on previous set (set when called enableFilter or disableFilter)
130
     */
131
    public static function restoreFilter()
132
    {
133
        if (!self::usesSubsite() || !class_exists(Subsite::class)) {
134
            return;
135
        }
136
        Subsite::$disable_subsite_filter = self::$previousState;
137
    }
138
139
    /**
140
     * @return int
141
     */
142
    public static function SubsiteIDFromSession()
143
    {
144
        $session = Controller::curr()->getRequest()->getSession();
145
        if ($session) {
0 ignored issues
show
$session is of type SilverStripe\Control\Session, thus it always evaluated to true.
Loading history...
146
            return $session->get('SubsiteID');
147
        }
148
        return 0;
149
    }
150
151
    /**
152
     * Typically call this on PageController::init
153
     * This is due to InitStateMiddleware not using session in front end and not persisting get var parameters
154
     *
155
     * @param HTTPRequest $request
156
     * @return int
157
     */
158
    public static function forceSubsiteFromRequest(HTTPRequest $request)
159
    {
160
        $subsiteID = $request->getVar('SubsiteID');
161
        if ($subsiteID) {
162
            $request->getSession()->set('ForcedSubsiteID', $subsiteID);
163
        } else {
164
            $subsiteID = $request->getSession()->get('ForcedSubsiteID');
165
        }
166
        if ($subsiteID) {
167
            self::changeSubsite($subsiteID, true);
168
        }
169
        return $subsiteID;
170
    }
171
172
    /**
173
     * @param string $ID
174
     * @param bool $flush
175
     * @return void
176
     */
177
    public static function changeSubsite($ID, $flush = null)
178
    {
179
        if (!self::usesSubsite() || !class_exists(Subsite::class) || !class_exists(SubsiteState::class)) {
180
            return;
181
        }
182
        self::$previousSubsite = self::currentSubsiteID();
183
184
        // Do this otherwise changeSubsite has no effect if false
185
        SubsiteState::singleton()->setUseSessions(true);
186
        Subsite::changeSubsite($ID);
187
        // This can help avoiding getting static objects like SiteConfig
188
        if ($flush !== null && $flush) {
189
            DataObject::reset();
190
        }
191
    }
192
193
    /**
194
     * @return void
195
     */
196
    public static function restoreSubsite()
197
    {
198
        if (!self::usesSubsite() || !class_exists(Subsite::class)) {
199
            return;
200
        }
201
        Subsite::changeSubsite(self::$previousSubsite);
202
    }
203
204
    /**
205
     * @return array
206
     */
207
    public static function listSubsites()
208
    {
209
        if (!self::usesSubsite() || !class_exists(Subsite::class)) {
210
            return [];
211
        }
212
        return  Subsite::get()->map();
213
    }
214
215
    /**
216
     * Execute the callback in given subsite
217
     *
218
     * @param int $ID Subsite ID or 0 for main site
219
     * @param callable $cb
220
     * @return void
221
     */
222
    public static function withSubsite($ID, $cb)
223
    {
224
        if (!class_exists(SubsiteState::class)) {
225
            return;
226
        }
227
        $currentID = self::currentSubsiteID();
228
        SubsiteState::singleton()->setSubsiteId($ID);
229
        $cb();
230
        SubsiteState::singleton()->setSubsiteId($currentID);
231
    }
232
233
    /**
234
     * Execute the callback in all subsites
235
     *
236
     * @param callable $cb
237
     * @param bool $includeMainSite
238
     * @return void
239
     */
240
    public static function withSubsites($cb, $includeMainSite = true)
241
    {
242
        if (!self::usesSubsite() || !class_exists(SubsiteState::class) || !class_exists(State::class)) {
0 ignored issues
show
The type LeKoala\EmailTemplates\Helpers\State 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...
243
            $cb();
244
            return;
245
        }
246
247
        if ($includeMainSite) {
248
            SubsiteState::singleton()->setSubsiteId(0);
249
            $cb(0);
250
        }
251
252
        $currentID = self::currentSubsiteID();
253
        $subsites = Subsite::get();
254
        foreach ($subsites as $subsite) {
255
            // TODO: maybe use changeSubsite instead?
256
            SubsiteState::singleton()->setSubsiteId($subsite->ID);
257
            $cb($subsite->ID);
258
        }
259
        SubsiteState::singleton()->setSubsiteId($currentID);
260
    }
261
}
262