Issues (1779)

src/helpers/MultiSite.php (31 issues)

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