Issues (96)

src/Helpers/SubsiteHelper.php (6 issues)

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