|
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/ |
|
|
|
|
|
|
9
|
|
|
* @copyright Copyright (c) 2018 nystudio107 |
|
|
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace nystudio107\retour\controllers; |
|
13
|
|
|
|
|
14
|
|
|
use Craft; |
|
15
|
|
|
use craft\errors\MissingComponentException; |
|
16
|
|
|
use craft\helpers\UrlHelper; |
|
17
|
|
|
use craft\web\Controller; |
|
18
|
|
|
use craft\web\UrlManager; |
|
19
|
|
|
use nystudio107\retour\assetbundles\retour\RetourAsset; |
|
20
|
|
|
use nystudio107\retour\helpers\Permission as PermissionHelper; |
|
21
|
|
|
use nystudio107\retour\models\Settings; |
|
22
|
|
|
use nystudio107\retour\Retour; |
|
23
|
|
|
use yii\base\InvalidConfigException; |
|
24
|
|
|
use yii\web\BadRequestHttpException; |
|
25
|
|
|
use yii\web\ForbiddenHttpException; |
|
26
|
|
|
use yii\web\NotFoundHttpException; |
|
27
|
|
|
use yii\web\Response; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
|
|
|
|
|
30
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
31
|
|
|
* @package Retour |
|
|
|
|
|
|
32
|
|
|
* @since 3.0.0 |
|
|
|
|
|
|
33
|
|
|
*/ |
|
|
|
|
|
|
34
|
|
|
class SettingsController extends Controller |
|
35
|
|
|
{ |
|
36
|
|
|
// Constants |
|
37
|
|
|
// ========================================================================= |
|
38
|
|
|
|
|
39
|
|
|
protected const DOCUMENTATION_URL = 'https://github.com/nystudio107/craft-retour/'; |
|
40
|
|
|
|
|
41
|
|
|
// Protected Properties |
|
42
|
|
|
// ========================================================================= |
|
43
|
|
|
|
|
44
|
|
|
protected array|bool|int $allowAnonymous = []; |
|
45
|
|
|
|
|
46
|
|
|
// Public Methods |
|
47
|
|
|
// ========================================================================= |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Plugin settings |
|
51
|
|
|
* |
|
52
|
|
|
* @param Settings|bool|null $settings |
|
|
|
|
|
|
53
|
|
|
* |
|
54
|
|
|
* @return Response The rendered result |
|
55
|
|
|
* @throws ForbiddenHttpException |
|
56
|
|
|
*/ |
|
57
|
|
|
public function actionPluginSettings(Settings|bool|null $settings = null): Response |
|
58
|
|
|
{ |
|
59
|
|
|
$variables = []; |
|
60
|
|
|
PermissionHelper::controllerPermissionCheck('retour:settings'); |
|
61
|
|
|
if ($settings === null) { |
|
62
|
|
|
$settings = Retour::$settings; |
|
63
|
|
|
} |
|
64
|
|
|
/** @var Settings $settings */ |
|
|
|
|
|
|
65
|
|
|
$pluginName = $settings->pluginName; |
|
66
|
|
|
$templateTitle = Craft::t('retour', 'Settings'); |
|
67
|
|
|
$view = Craft::$app->getView(); |
|
68
|
|
|
// Asset bundle |
|
69
|
|
|
try { |
|
70
|
|
|
$view->registerAssetBundle(RetourAsset::class); |
|
71
|
|
|
} catch (InvalidConfigException $e) { |
|
72
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
|
73
|
|
|
} |
|
74
|
|
|
$variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl( |
|
75
|
|
|
'@nystudio107/retour/web/assets/dist', |
|
76
|
|
|
true |
|
|
|
|
|
|
77
|
|
|
); |
|
78
|
|
|
// Basic variables |
|
79
|
|
|
$variables['fullPageForm'] = true; |
|
80
|
|
|
$variables['docsUrl'] = self::DOCUMENTATION_URL; |
|
81
|
|
|
$variables['pluginName'] = $pluginName; |
|
82
|
|
|
$variables['title'] = $templateTitle; |
|
83
|
|
|
$variables['crumbs'] = [ |
|
84
|
|
|
[ |
|
85
|
|
|
'label' => $pluginName, |
|
86
|
|
|
'url' => UrlHelper::cpUrl('retour'), |
|
87
|
|
|
], |
|
88
|
|
|
[ |
|
89
|
|
|
'label' => $templateTitle, |
|
90
|
|
|
'url' => UrlHelper::cpUrl('retour/settings'), |
|
91
|
|
|
], |
|
92
|
|
|
]; |
|
93
|
|
|
$variables['docTitle'] = "{$pluginName} - {$templateTitle}"; |
|
94
|
|
|
$variables['selectedSubnavItem'] = 'settings'; |
|
95
|
|
|
$variables['settings'] = $settings; |
|
96
|
|
|
|
|
97
|
|
|
// Render the template |
|
98
|
|
|
return $this->renderTemplate('retour/settings', $variables); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Saves a plugin’s settings. |
|
103
|
|
|
* |
|
104
|
|
|
* @return Response|null |
|
105
|
|
|
* @throws NotFoundHttpException if the requested plugin cannot be found |
|
106
|
|
|
* @throws BadRequestHttpException |
|
107
|
|
|
* @throws MissingComponentException |
|
108
|
|
|
* @throws ForbiddenHttpException |
|
109
|
|
|
*/ |
|
110
|
|
|
public function actionSavePluginSettings(): ?Response |
|
111
|
|
|
{ |
|
112
|
|
|
PermissionHelper::controllerPermissionCheck('retour:settings'); |
|
113
|
|
|
$this->requirePostRequest(); |
|
114
|
|
|
$pluginHandle = Craft::$app->getRequest()->getRequiredBodyParam('pluginHandle'); |
|
115
|
|
|
$settings = Craft::$app->getRequest()->getBodyParam('settings', []); |
|
116
|
|
|
$plugin = Craft::$app->getPlugins()->getPlugin($pluginHandle); |
|
117
|
|
|
|
|
118
|
|
|
if ($plugin === null) { |
|
119
|
|
|
throw new NotFoundHttpException('Plugin not found'); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if (!is_array($settings['additionalHeaders'])) { |
|
123
|
|
|
$settings['additionalHeaders'] = []; |
|
124
|
|
|
} |
|
125
|
|
|
if (!is_array($settings['excludePatterns'])) { |
|
126
|
|
|
$settings['excludePatterns'] = []; |
|
127
|
|
|
} |
|
128
|
|
|
if (!Craft::$app->getPlugins()->savePluginSettings($plugin, $settings)) { |
|
129
|
|
|
Craft::$app->getSession()->setError(Craft::t('app', "Couldn't save plugin settings.")); |
|
130
|
|
|
|
|
131
|
|
|
// Send the plugin back to the template |
|
132
|
|
|
/** @var UrlManager $urlManager */ |
|
|
|
|
|
|
133
|
|
|
$urlManager = Craft::$app->getUrlManager(); |
|
134
|
|
|
$urlManager->setRouteParams([ |
|
|
|
|
|
|
135
|
|
|
'plugin' => $plugin, |
|
136
|
|
|
]); |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
return null; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
Retour::$plugin->clearAllCaches(); |
|
|
|
|
|
|
142
|
|
|
Craft::$app->getSession()->setNotice(Craft::t('app', 'Plugin settings saved.')); |
|
143
|
|
|
|
|
144
|
|
|
return $this->redirectToPostedUrl(); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|