/
customize_preset.php
226 lines (202 loc) · 9.02 KB
/
customize_preset.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
namespace Concrete\Controller\Panel\Theme;
use Concrete\Controller\Backend\UserInterface as BackendInterfaceController;
use Concrete\Core\Error\UserMessageException;
use Concrete\Core\Http\ResponseFactory;
use Concrete\Core\Page\EditResponse;
use Concrete\Core\Page\Theme\Command\ApplyCustomizationsToPageCommand;
use Concrete\Core\Page\Theme\Command\ApplyCustomizationsToSiteCommand;
use Concrete\Core\Page\Theme\Command\CreateCustomSkinCommand;
use Concrete\Core\Page\Theme\Command\DeleteCustomSkinCommand;
use Concrete\Core\Page\Theme\Command\SkinCommandValidator;
use Concrete\Core\Page\Theme\Command\UpdateCustomSkinCommand;
use Concrete\Core\Page\Theme\Theme;
use Concrete\Core\StyleCustomizer\Traits\CustomizeControllerTrait;
use Concrete\Core\User\User;
use Symfony\Component\HttpFoundation\JsonResponse;
class CustomizePreset extends BackendInterfaceController
{
use CustomizeControllerTrait;
protected $viewPath = '/panels/theme/customize';
protected $controllerActionPath = '/panels/theme/customize';
public function view($pThemeID, $presetIdentifier, $previewPageID)
{
$this->loadPreviewPage($previewPageID);
$customizer = $this->getCustomizer($pThemeID);
$preset = $customizer->getPresetByIdentifier($presetIdentifier);
if ($preset) {
$this->loadCustomizer($customizer, $preset);
$this->set('presetIdentifier', $presetIdentifier);
$this->set('customizer', $customizer);
} else {
throw new \RuntimeException(t('Invalid preset: %s', h($presetIdentifier)));
}
}
public function viewSkin($pThemeID, $skinIdentifier, $previewPageID)
{
$this->loadPreviewPage($previewPageID);
$customizer = $this->getCustomizer($pThemeID);
$skin = $customizer->getTheme()->getSkinByIdentifier($skinIdentifier);
if ($skin) {
$preset = $customizer->getPresetByIdentifier($skin->getPresetStartingPoint());
$this->loadCustomizer($customizer, $preset, $skin);
$this->set('skinIdentifier', $skinIdentifier);
} else {
throw new \RuntimeException(t('Invalid skin: %s', h($skinIdentifier)));
}
}
/**
* Controller method run when a preset skin is saved as a new skin, with or without customizations.
* Preset skins cannot be modified, they can only be saved as custom skins.
*
* @param $pThemeID
* @param $skinIdentifier
* @return mixed
*/
public function createSkin($pThemeID, $presetIdentifier)
{
if ($this->app->make('token')->validate()) {
$theme = Theme::getByID($pThemeID);
if ($theme) {
$customizer = $theme->getThemeCustomizer();
$preset = $customizer->getPresetByIdentifier($presetIdentifier);
if ($preset) {
$responseFactory = $this->app->make(ResponseFactory::class);
$validator = $this->app->make(SkinCommandValidator::class);
$error = $validator->validate($this->request->request->get('skinName'), $theme);
if ($error->has()) {
return $responseFactory->json($error);
} else {
$u = $this->app->make(User::class);
$command = new CreateCustomSkinCommand();
$command->setAuthorID($u->getUserID());
$command->setThemeID($theme->getThemeID());
$command->setPresetStartingPoint($presetIdentifier);
$command->setSkinName($this->request->request->get('skinName'));
$command->setCustomCss($this->request->request->get('customCss'));
$command->setStyles($this->request->request->get('styles'));
$skin = $this->app->executeCommand($command);
return $responseFactory->json(
[
'created' => true,
'skin' => $skin
]
);
}
}
}
throw new \RuntimeException(t('Invalid theme ID or skin identifier.'));
}
throw new UserMessageException(t('Access Denied'));
}
/**
* Controller method run when a preset is used with the legacy customizer or other customizer that does not
* suppport skins.
* @deprecated
* @param $pThemeID
* @param $skinIdentifier
* @return mixed
*/
public function saveStyles($previewPageID, $pThemeID, $presetIdentifier)
{
if ($this->app->make('token')->validate()) {
$this->loadPreviewPage($previewPageID);
$theme = Theme::getByID($pThemeID);
if ($theme) {
$customizer = $theme->getThemeCustomizer();
$preset = $customizer->getPresetByIdentifier($presetIdentifier);
if ($preset) {
if ($this->request->request->get('applyTo') == 'site') {
$command = new ApplyCustomizationsToSiteCommand();
} else {
$command = new ApplyCustomizationsToPageCommand();
$previewPage = $this->get('previewPage');
$nvc = $previewPage->getVersionToModify();
$command->setPage($nvc);
}
$command->setThemeID($theme->getThemeID());
$command->setPresetStartingPoint($presetIdentifier);
if ($this->request->request->has('customCss')) {
$command->setCustomCss($this->request->request->get('customCss'));
}
$command->setStyles($this->request->request->get('styles'));
$this->app->executeCommand($command);
$editResponse = new EditResponse();
$editResponse->setPage($this->get('previewPage'));
$editResponse->setTitle(t('Theme Customization'));
if ($this->request->request->get('applyTo') == 'site') {
$editResponse->setMessage(t('Global styles updated successfully.'));
} else {
$editResponse->setMessage(t('Page styles updated successfully.'));
}
return new JsonResponse($editResponse);
}
}
throw new \RuntimeException(t('Invalid theme ID or skin identifier.'));
}
throw new UserMessageException(t('Access Denied'));
}
/**
* Controller method run when custom skins are saved. Custom skins can have custom variables, then be updated
* and tweaked and re-saved as necessary.
*
* @param $pThemeID
* @param $skinIdentifier
* @return mixed
*/
public function save($pThemeID, $skinIdentifier)
{
if ($this->app->make('token')->validate()) {
$theme = Theme::getByID($pThemeID);
if ($theme) {
$skin = $theme->getSkinByIdentifier($skinIdentifier);
if ($skin) {
$responseFactory = $this->app->make(ResponseFactory::class);
$command = new UpdateCustomSkinCommand();
$command->setCustomSkin($skin);
$command->setStyles($this->request->request->get('styles'));
$command->setCustomCss($this->request->request->get('customCss'));
$skin = $this->app->executeCommand($command);
return $responseFactory->json(
[
'updated' => true,
'skin' => $skin
]
);
}
}
throw new \RuntimeException(t('Invalid theme ID or skin identifier.'));
}
throw new UserMessageException(t('Access Denied'));
}
/**
* Method run when deleting a theme.
*
* @param $pThemeID
* @param $skinIdentifier
* @return mixed
*/
public function delete($pThemeID, $skinIdentifier)
{
if ($this->app->make('token')->validate()) {
$theme = Theme::getByID($pThemeID);
if ($theme) {
$skin = $theme->getSkinByIdentifier($skinIdentifier);
if ($skin) {
$responseFactory = $this->app->make(ResponseFactory::class);
$command = new DeleteCustomSkinCommand();
$command->setCustomSkin($skin);
$this->app->executeCommand($command);
return $responseFactory->json(
[
'delete' => true,
'skin' => $skin
]
);
}
}
throw new \RuntimeException(t('Invalid theme ID or skin identifier.'));
}
throw new UserMessageException(t('Access Denied'));
}
}