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