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
![]() |
|||
9 | * @copyright Copyright (c) 2018 nystudio107 |
||
0 ignored issues
–
show
|
|||
10 | */ |
||
0 ignored issues
–
show
|
|||
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
|
|||
21 | * @author nystudio107 |
||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||
22 | * @package Retour |
||
0 ignored issues
–
show
|
|||
23 | * @since 3.0.0 |
||
0 ignored issues
–
show
|
|||
24 | */ |
||
0 ignored issues
–
show
|
|||
25 | class MultiSite |
||
26 | { |
||
27 | // Constants |
||
28 | // ========================================================================= |
||
29 | |||
30 | // Public Static Methods |
||
31 | // ========================================================================= |
||
32 | |||
33 | /** |
||
0 ignored issues
–
show
|
|||
34 | * @param array $variables |
||
0 ignored issues
–
show
|
|||
35 | */ |
||
0 ignored issues
–
show
|
|||
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
|
|||
63 | * @param string $siteHandle |
||
0 ignored issues
–
show
|
|||
64 | * @param $siteId |
||
0 ignored issues
–
show
|
|||
65 | * @param $variables |
||
0 ignored issues
–
show
|
|||
66 | * |
||
67 | * @throws ForbiddenHttpException |
||
68 | */ |
||
0 ignored issues
–
show
|
|||
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
|
|||
125 | * @param string $permissionName |
||
0 ignored issues
–
show
|
|||
126 | * |
||
127 | * @throws ForbiddenHttpException |
||
128 | */ |
||
0 ignored issues
–
show
|
|||
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
|
|||
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 |