Passed
Push — develop ( 9005d1...6a4cbf )
by Andrew
04:30
created

MultiSite::setSitesMenuVariables()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 20
rs 9.8666
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
/**
3
 * Retour plugin for Craft CMS 3.x
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/
9
 * @copyright Copyright (c) 2018 nystudio107
10
 */
11
12
namespace nystudio107\retour\helpers;
13
14
use Craft;
15
16
use craft\models\Site;
17
use yii\web\ForbiddenHttpException;
18
use yii\web\NotFoundHttpException;
19
20
/**
21
 * @author    nystudio107
22
 * @package   Retour
23
 * @since     3.0.0
24
 */
25
class MultiSite
26
{
27
    // Constants
28
    // =========================================================================
29
30
    // Public Static Methods
31
    // =========================================================================
32
33
    /**
34
     * @param array $variables
35
     */
36
    public static function setSitesMenuVariables(array &$variables)
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
49
            /** @var Site $site */
50
            foreach ($sites->getAllGroups() as $group) {
51
                $groupSites = $sites->getSitesByGroupId($group->id);
52
                $variables['sitesMenu'][$group->name]
53
                    = ['optgroup' => $group->name];
54
                foreach ($groupSites as $groupSite) {
55
                    $variables['sitesMenu'][$groupSite->id] = $groupSite->name;
56
                }
57
            }
58
        }
59
    }
60
61
    /**
62
     * @param string $siteHandle
63
     * @param        $siteId
64
     * @param        $variables
65
     *
66
     * @throws \yii\web\ForbiddenHttpException
67
     */
68
    public static function setMultiSiteVariables($siteHandle, &$siteId, array &$variables)
69
    {
70
        // Enabled sites
71
        $sites = Craft::$app->getSites();
72
        if (Craft::$app->getIsMultiSite()) {
73
            // Set defaults based on the section settings
74
            $variables['enabledSiteIds'] = [];
75
            $variables['siteIds'] = [];
76
77
            /** @var Site $site */
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
    /**
125
     * Return a siteId from a siteHandle
126
     *
127
     * @param string $siteHandle
128
     *
129
     * @return int|null
130
     * @throws NotFoundHttpException
131
     */
132
    public static function getSiteIdFromHandle($siteHandle)
133
    {
134
        // Get the site to edit
135
        if ($siteHandle !== null) {
0 ignored issues
show
introduced by
The condition $siteHandle !== null is always true.
Loading history...
136
            $site = Craft::$app->getSites()->getSiteByHandle($siteHandle);
137
            if (!$site) {
138
                throw new NotFoundHttpException('Invalid site handle: '.$siteHandle);
139
            }
140
            $siteId = $site->id;
141
        } else {
142
            $siteId = 0;
143
        }
144
145
        return $siteId;
146
    }
147
148
    // Protected Static Methods
149
    // =========================================================================
150
151
    /**
152
     * @param string $permissionName
153
     *
154
     * @throws ForbiddenHttpException
155
     */
156
    protected static function requirePermission(string $permissionName)
157
    {
158
        if (!Craft::$app->getUser()->checkPermission($permissionName)) {
159
            throw new ForbiddenHttpException('User is not permitted to perform this action');
160
        }
161
    }
162
}
163