/
customize.php
229 lines (204 loc) · 8.36 KB
/
customize.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
227
228
229
<?php
namespace Concrete\Controller\Panel\Page\Design;
use Concrete\Controller\Backend\UserInterface\Page as BackendInterfacePageController;
use Concrete\Core\Page\PageList;
use Concrete\Core\Page;
use Concrete\Core\Permission\Key\Key as PermissionKey;
use Concrete\Core\Page\Theme\Theme as PageTheme;
use Concrete\Core\Page\EditResponse as PageEditResponse;
use Concrete\Core\Http\Request;
use Concrete\Core\Http\Response;
use Concrete\Core\StyleCustomizer\Style\ValueList;
use Concrete\Core\StyleCustomizer\CustomCssRecord as CustomCssRecordService;
use URL;
use Exception;
class Customize extends BackendInterfacePageController
{
protected $viewPath = '/panels/page/design/customize';
protected $controllerActionPath = '/ccm/system/panels/page/design/customize';
protected $helpers = ['form'];
public function canAccess()
{
return $this->permissions->canEditPageTheme();
}
public function view($pThemeID)
{
$this->requireAsset('core/style-customizer');
$pt = PageTheme::getByID($pThemeID);
if (is_object($pt) && $pt->isThemeCustomizable()) {
$presets = $pt->getThemeCustomizableStylePresets();
foreach ($presets as $preset) {
if ($preset->isDefaultPreset()) {
$defaultPreset = $preset;
}
}
// load the defaults for the panel, before we get to any
// request-based tomfoolery.
if ($this->page->hasPageThemeCustomizations()) {
$object = $this->page->getCustomStyleObject();
} else {
$object = $pt->getThemeCustomStyleObject();
}
if (is_object($object)) {
$handle = $object->getPresetHandle();
if ($handle) {
$selectedPreset = $pt->getThemeCustomizablePreset($handle);
}
$valueList = $object->getValueList();
$sccRecord = $object->getCustomCssRecord();
} else {
$selectedPreset = $defaultPreset;
$valueList = $defaultPreset->getStyleValueList();
$sccRecord = null;
}
if ($this->request->request->has('handle')) {
$preset = $pt->getThemeCustomizablePreset($this->request->request->get('handle'));
if (is_object($preset)) {
$selectedPreset = $preset;
$valueList = $preset->getStyleValueList();
}
}
// finally, we sort the presets so that the selected
// preset is at the top
usort($presets, function ($a, $b) use ($selectedPreset) {
if (!$selectedPreset) {
return -1;
}
if ($selectedPreset->getPresetHandle() == $a->getPresetHandle()) {
return -1;
}
if ($selectedPreset->getPresetHandle() == $b->getPresetHandle()) {
return 1;
}
return strcasecmp($a->getPresetDisplayName('text'), $b->getPresetDisplayName('text'));
});
$styleList = $pt->getThemeCustomizableStyleList();
$sccRecordID = 0;
if ($sccRecord) {
$sccRecordID = $sccRecord->getRecordID();
}
$this->set('sccRecordID', $sccRecordID);
$this->set('valueList', $valueList);
$this->set('presets', $presets);
$this->set('selectedPreset', $selectedPreset);
$this->set('styleSets', $styleList->getSets());
$this->set('theme', $pt);
$this->set('token', $this->app->make('token'));
} else {
throw new Exception(t('Invalid or non-customizable theme.'));
}
}
protected function getValueListFromRequest($pThemeID)
{
$pt = PageTheme::getByID($pThemeID);
$styles = $pt->getThemeCustomizableStyleList();
$vl = ValueList::loadFromRequest($this->request->request, $styles);
return $vl;
}
public function preview($pThemeID)
{
$vl = $this->getValueListFromRequest($pThemeID);
$pt = PageTheme::getByID($pThemeID);
$pt->enablePreviewRequest();
$sheets = $pt->getThemeCustomizableStyleSheets();
// for each customizable stylesheet in the theme, we take the value list
// and send its variables through the LESS parser.
foreach ($sheets as $sheet) {
$sheet->setValueList($vl);
// we save each sheet to the preview location.
$sheet->output();
}
// and finally, we pass our modified theme into the updated view, which we send back in the iframe.
$req = Request::getInstance();
$req->setCurrentPage($this->page);
$controller = $this->page->getPageController();
$view = $controller->getViewObject();
$view->setCustomPageTheme($pt);
$req->setCustomRequestUser(-1);
$response = new Response();
$content = $view->render();
$response->setContent($content);
return $response;
}
public function apply_to_site($pThemeID)
{
$pk = PermissionKey::getByHandle('customize_themes');
if ($this->validateAction() && $pk->can()) {
$vl = $this->getValueListFromRequest($pThemeID);
$pt = PageTheme::getByID($pThemeID);
$vl->save();
$sccRecord = null;
if ($this->request->request->has('sccRecordID')) {
$sccRecord = $this->app->make(CustomCssRecordService::class)->getByID($this->request->request->get('sccRecordID'));
}
$preset = false;
if ($this->request->request->has('handle')) {
$preset = $pt->getThemeCustomizablePreset($this->request->request->get('handle'));
}
// reset all custom styles on particular pages
$pl = new PageList();
$pl->filterByPagesWithCustomStyles();
$results = $pl->getResults();
foreach ($results as $csc) {
$cscv = $csc->getVersionToModify();
$cscv->resetCustomThemeStyles();
$vo = $csc->getVersionObject();
if ($vo->isApprovedNow()) {
$vo = $cscv->getVersionObject();
$vo->approve();
}
}
// set the global style object.
$pt->setCustomStyleObject($vl, $preset, $sccRecord);
$r = new PageEditResponse();
$r->setPage($this->page);
$r->setRedirectURL(URL::to($this->page));
$r->outputJSON();
}
}
public function apply_to_page($pThemeID)
{
if ($this->validateAction()) {
$vl = $this->getValueListFromRequest($pThemeID);
$pt = PageTheme::getByID($pThemeID);
$vl->save();
$sccRecord = null;
if ($this->request->request->has('sccRecordID')) {
$sccRecord = $this->app->make(CustomCssRecordService::class)->getByID($this->request->request->get('sccRecordID'));
}
$preset = false;
if ($this->request->request->has('handle')) {
$preset = $pt->getThemeCustomizablePreset($this->request->request->get('handle'));
}
$nvc = $this->page->getVersionToModify();
$nvc->setCustomStyleObject($pt, $vl, $preset, $sccRecord);
$r = new PageEditResponse();
$r->setPage($this->page);
$r->setRedirectURL(URL::to($this->page));
$r->outputJSON();
}
}
public function reset_page_customizations()
{
if ($this->validateAction()) {
$nvc = $this->page->getVersionToModify();
$nvc->resetCustomThemeStyles();
$r = new PageEditResponse();
$r->setPage($this->page);
$r->setRedirectURL(URL::to($this->page));
$r->outputJSON();
}
}
public function reset_site_customizations($pThemeID)
{
if ($this->validateAction()) {
\Concrete\Core\Page\Page::resetAllCustomStyles();
$pt = PageTheme::getByID($pThemeID);
$pt->resetThemeCustomStyles();
$r = new PageEditResponse();
$r->setPage($this->page);
$r->setRedirectURL(URL::to($this->page));
$r->outputJSON();
}
}
}