MultiSite::setSitesMenuVariables()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 20
rs 9.4888
cc 5
nc 5
nop 1
1
<?php
2
/**
3
 * Retour plugin for Craft CMS
4
 *
5
 * Retour allows you to intelligently redirect legacy URLs, so that you don't
6
 * lose SEO value when rebuilding & restructuring a website
7
 *
8
 * @link      https://nystudio107.com/
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @copyright Copyright (c) 2018 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
11
12
namespace nystudio107\retour\helpers;
13
14
use Craft;
15
use yii\web\ForbiddenHttpException;
16
use yii\web\NotFoundHttpException;
17
use function count;
18
use function in_array;
19
20
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
21
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
22
 * @package   Retour
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
23
 * @since     3.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
24
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
25
class MultiSite
26
{
27
    // Constants
28
    // =========================================================================
29
30
    // Public Static Methods
31
    // =========================================================================
32
33
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
34
     * @param array $variables
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
35
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
36
    public static function setSitesMenuVariables(array &$variables): void
37
    {
38
        // Set defaults based on the section settings
39
        $variables['sitesMenu'] = [
40
            0 => Craft::t(
41
                'retour',
42
                'All Sites'
43
            ),
44
        ];
45
        // Enabled sites
46
        $sites = Craft::$app->getSites();
47
        if (Craft::$app->getIsMultiSite()) {
48
            $editableSites = $sites->getEditableSiteIds();
49
            foreach ($sites->getAllGroups() as $group) {
50
                $groupSites = $sites->getSitesByGroupId($group->id);
51
                $variables['sitesMenu'][$group->name]
52
                    = ['optgroup' => $group->name];
53
                foreach ($groupSites as $groupSite) {
54
                    if (in_array($groupSite->id, $editableSites, false)) {
55
                        $variables['sitesMenu'][$groupSite->id] = $groupSite->name;
56
                    }
57
                }
58
            }
59
        }
60
    }
61
62
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
63
     * @param string $siteHandle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
64
     * @param        $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 1 spaces but found 8
Loading history...
65
     * @param        $variables
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 1 spaces but found 8
Loading history...
66
     *
67
     * @throws ForbiddenHttpException
68
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
69
    public static function setMultiSiteVariables($siteHandle, &$siteId, array &$variables): void
70
    {
71
        // Enabled sites
72
        $sites = Craft::$app->getSites();
73
        if (Craft::$app->getIsMultiSite()) {
74
            // Set defaults based on the section settings
75
            $variables['enabledSiteIds'] = [];
76
            $variables['siteIds'] = [];
77
78
            foreach ($sites->getEditableSiteIds() as $editableSiteId) {
79
                $variables['enabledSiteIds'][] = $editableSiteId;
80
                $variables['siteIds'][] = $editableSiteId;
81
            }
82
83
            // Make sure the $siteId they are trying to edit is in our array of editable sites
84
            if (!in_array($siteId, $variables['enabledSiteIds'], false)) {
85
                if (!empty($variables['enabledSiteIds'])) {
86
                    if ($siteId !== 0) {
87
                        $siteId = reset($variables['enabledSiteIds']);
88
                    }
89
                } else {
90
                    self::requirePermission('editSite:' . $siteId);
91
                }
92
            }
93
        }
94
        // Set the currentSiteId and currentSiteHandle
95
        $variables['currentSiteId'] = empty($siteId) ? 0 : $siteId;
96
        $variables['currentSiteHandle'] = empty($siteHandle)
97
            ? Craft::$app->getSites()->currentSite->handle
98
            : $siteHandle;
99
100
        // Page title
101
        $variables['showSites'] = (
102
            Craft::$app->getIsMultiSite() &&
103
            count($variables['enabledSiteIds'])
104
        );
105
106
        if ($variables['showSites']) {
107
            if ($variables['currentSiteId'] === 0) {
108
                $variables['sitesMenuLabel'] = Craft::t(
109
                    'retour',
110
                    'All Sites'
111
                );
112
            } else {
113
                $variables['sitesMenuLabel'] = Craft::t(
114
                    'site',
115
                    $sites->getSiteById((int)$variables['currentSiteId'])->name
116
                );
117
            }
118
        } else {
119
            $variables['currentSiteId'] = 0;
120
            $variables['sitesMenuLabel'] = '';
121
        }
122
    }
123
124
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
125
     * @param string $permissionName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
126
     *
127
     * @throws ForbiddenHttpException
128
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
129
    public static function requirePermission(string $permissionName): void
130
    {
131
        if (!Craft::$app->getUser()->checkPermission($permissionName)) {
132
            throw new ForbiddenHttpException('User is not permitted to perform this action');
133
        }
134
    }
135
136
    /**
137
     * Return a siteId from a siteHandle
138
     *
139
     * @param ?string $siteHandle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
140
     *
141
     * @return int|null
142
     * @throws NotFoundHttpException
143
     */
144
    public static function getSiteIdFromHandle(?string $siteHandle): ?int
145
    {
146
        // Get the site to edit
147
        if ($siteHandle !== null) {
148
            $site = Craft::$app->getSites()->getSiteByHandle($siteHandle);
149
            if (!$site) {
150
                throw new NotFoundHttpException('Invalid site handle: ' . $siteHandle);
151
            }
152
            $siteId = $site->id;
153
        } else {
154
            $siteId = 0;
155
        }
156
157
        return $siteId;
158
    }
159
160
    // Protected Static Methods
161
    // =========================================================================
162
}
163