1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* User: Jens |
4
|
|
|
* Date: 29-1-2017 |
5
|
|
|
* Time: 15:23 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace CloudControl\Cms\components\cms; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use CloudControl\Cms\cc\Request; |
12
|
|
|
use CloudControl\Cms\cc\ResponseHeaders; |
13
|
|
|
use CloudControl\Cms\components\cms\document\FolderRouting; |
14
|
|
|
use CloudControl\Cms\components\CmsComponent; |
15
|
|
|
use CloudControl\Cms\search\Search; |
16
|
|
|
use CloudControl\Cms\storage\Cache; |
17
|
|
|
use CloudControl\Cms\storage\entities\Document; |
18
|
|
|
|
19
|
|
|
class DocumentRouting extends CmsRouting |
20
|
|
|
{ |
21
|
|
|
protected static $routes = array( |
22
|
|
|
'/documents' => 'overviewRouting', |
23
|
|
|
'/documents/new-document' => 'documentNewRoute', |
24
|
|
|
'/documents/edit-document' => 'editDocumentRoute', |
25
|
|
|
'/documents/get-brick' => 'getBrickRoute', |
26
|
|
|
'/documents/delete-document' => 'deleteDocumentRoute', |
27
|
|
|
'/documents/publish-document' => 'publishDocumentRoute', |
28
|
|
|
'/documents/unpublish-document' => 'unpublishDocumentRoute', |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* DocumentRouting constructor. |
33
|
|
|
* @param $request |
34
|
|
|
* @param $relativeCmsUri |
35
|
|
|
* @param CmsComponent $cmsComponent |
36
|
|
|
* @throws \Exception |
37
|
|
|
*/ |
38
|
|
|
public function __construct(Request $request, $relativeCmsUri, CmsComponent $cmsComponent) |
39
|
|
|
{ |
40
|
|
|
$this->doRouting($request, $relativeCmsUri, $cmsComponent); |
41
|
|
|
new FolderRouting($request, $relativeCmsUri, $cmsComponent); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $request |
46
|
|
|
* @param CmsComponent $cmsComponent |
47
|
|
|
* |
48
|
|
|
* @throws \Exception |
49
|
|
|
*/ |
50
|
|
|
protected function documentNewRoute($request, $cmsComponent) |
51
|
|
|
{ |
52
|
|
|
if (isset($request::$get[CmsConstants::GET_PARAMETER_PATH])) { |
53
|
|
|
$cmsComponent->subTemplate = 'documents/document-form'; |
54
|
|
|
$cmsComponent->setParameter(CmsConstants::PARAMETER_MAIN_NAV_CLASS, CmsConstants::PARAMETER_DOCUMENTS); |
55
|
|
|
$cmsComponent->setParameter(CmsConstants::PARAMETER_SMALLEST_IMAGE, |
56
|
|
|
$cmsComponent->storage->getImageSet()->getSmallestImageSet()->slug); |
57
|
|
|
if (isset($request::$get[CmsConstants::PARAMETER_DOCUMENT_TYPE])) { |
58
|
|
|
$this->newDocumentRoute($request, $cmsComponent); |
59
|
|
|
} else { |
60
|
|
|
$this->selectDocumentTypesRoute($request, $cmsComponent); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $request |
68
|
|
|
* @param CmsComponent $cmsComponent |
69
|
|
|
* @throws \Exception |
70
|
|
|
*/ |
71
|
|
|
protected function editDocumentRoute($request, $cmsComponent) |
72
|
|
|
{ |
73
|
|
|
$cmsComponent->subTemplate = 'documents/document-form'; |
74
|
|
|
$cmsComponent->setParameter(CmsConstants::PARAMETER_MAIN_NAV_CLASS, CmsConstants::PARAMETER_DOCUMENTS); |
75
|
|
|
$cmsComponent->setParameter(CmsConstants::PARAMETER_SMALLEST_IMAGE, |
76
|
|
|
$cmsComponent->storage->getImageSet()->getSmallestImageSet()->slug); |
77
|
|
|
if (isset($request::$post[CmsConstants::POST_PARAMETER_TITLE], $request::$get[CmsConstants::GET_PARAMETER_SLUG])) { |
78
|
|
|
$this->saveDocument($request, $cmsComponent); |
79
|
|
|
} |
80
|
|
|
$document = $cmsComponent->storage->getDocuments()->getDocumentBySlug($request::$get[CmsConstants::GET_PARAMETER_SLUG], |
81
|
|
|
'unpublished'); |
82
|
|
|
$cmsComponent->setParameter(CmsConstants::PARAMETER_DOCUMENT, $document); |
83
|
|
|
|
84
|
|
|
$request::$get[CmsConstants::GET_PARAMETER_PATH] = $request::$get[CmsConstants::GET_PARAMETER_SLUG]; |
85
|
|
|
if ($document instanceof Document) { |
86
|
|
|
$this->setDocumentTypeParameter($cmsComponent, $document); |
87
|
|
|
} else { |
88
|
|
|
header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents?not-found'); |
89
|
|
|
exit; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$cmsComponent->setParameter(CmsConstants::PARAMETER_BRICKS, $cmsComponent->storage->getBricks()->getBricks()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $request |
97
|
|
|
* @param CmsComponent $cmsComponent |
98
|
|
|
* @throws \Exception |
99
|
|
|
*/ |
100
|
|
|
protected function getBrickRoute($request, $cmsComponent) |
101
|
|
|
{ |
102
|
|
|
$cmsComponent->setParameter(CmsConstants::PARAMETER_SMALLEST_IMAGE, |
103
|
|
|
$cmsComponent->storage->getImageSet()->getSmallestImageSet()->slug); |
104
|
|
|
$cmsComponent->subTemplate = 'documents/brick'; |
105
|
|
|
$cmsComponent->setParameter(CmsConstants::PARAMETER_BRICK, |
106
|
|
|
$cmsComponent->storage->getBricks()->getBrickBySlug($request::$get[CmsConstants::GET_PARAMETER_SLUG])); |
107
|
|
|
$cmsComponent->setParameter(CmsConstants::PARAMETER_STATIC, |
108
|
|
|
$request::$get[CmsConstants::PARAMETER_STATIC] === 'true'); |
109
|
|
|
if (isset($request::$get[CmsConstants::PARAMETER_MY_BRICK_SLUG])) { |
110
|
|
|
$cmsComponent->setParameter(CmsConstants::PARAMETER_MY_BRICK_SLUG, |
111
|
|
|
$request::$get[CmsConstants::PARAMETER_MY_BRICK_SLUG]); |
112
|
|
|
} |
113
|
|
|
$result = new \stdClass(); |
114
|
|
|
$result->body = $cmsComponent->renderTemplate('documents/brick'); |
115
|
|
|
$result->rteList = isset($GLOBALS['rteList']) ? $GLOBALS['rteList'] : array(); |
116
|
|
|
ob_clean(); |
117
|
|
|
ResponseHeaders::add(ResponseHeaders::HEADER_CONTENT_TYPE, ResponseHeaders::HEADER_CONTENT_TYPE_CONTENT_APPLICATION_JSON); |
118
|
|
|
ResponseHeaders::sendAllHeaders(); |
119
|
|
|
die(json_encode($result)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param $request |
124
|
|
|
* @param CmsComponent $cmsComponent |
125
|
|
|
* @throws \Exception |
126
|
|
|
*/ |
127
|
|
|
protected function deleteDocumentRoute($request, $cmsComponent) |
128
|
|
|
{ |
129
|
|
|
$cmsComponent->storage->getDocuments()->deleteDocumentBySlug($request::$get[CmsConstants::GET_PARAMETER_SLUG]); |
130
|
|
|
$cmsComponent->storage->getActivityLog()->add('deleted document /' . $request::$get[CmsConstants::GET_PARAMETER_SLUG], |
131
|
|
|
'trash'); |
132
|
|
|
header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents?document-delete'); |
133
|
|
|
exit; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param $request |
138
|
|
|
* @param CmsComponent $cmsComponent |
139
|
|
|
* @throws \Exception |
140
|
|
|
*/ |
141
|
|
|
protected function publishDocumentRoute($request, $cmsComponent) |
142
|
|
|
{ |
143
|
|
|
$cmsComponent->storage->getDocuments()->publishDocumentBySlug($request::$get[CmsConstants::GET_PARAMETER_SLUG]); |
144
|
|
|
$this->clearCacheAndLogActivity($request, $cmsComponent); |
145
|
|
|
$this->doAfterPublishRedirect($request, $cmsComponent); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param $request |
150
|
|
|
* @param CmsComponent $cmsComponent |
151
|
|
|
* @throws \Exception |
152
|
|
|
*/ |
153
|
|
|
protected function unpublishDocumentRoute($request, $cmsComponent) |
154
|
|
|
{ |
155
|
|
|
$cmsComponent->storage->getDocuments()->unpublishDocumentBySlug($request::$get[CmsConstants::GET_PARAMETER_SLUG]); |
156
|
|
|
$this->clearCacheAndLogActivity($request, $cmsComponent, 'times-circle-o', 'unpublished'); |
157
|
|
|
$this->doAfterPublishRedirect($request, $cmsComponent, 'unpublished'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param CmsComponent $cmsComponent |
162
|
|
|
* @param Request $request |
163
|
|
|
* @throws \Exception |
164
|
|
|
*/ |
165
|
|
|
protected function overviewRouting($request, $cmsComponent) |
166
|
|
|
{ |
167
|
|
|
$cmsComponent->subTemplate = 'documents'; |
168
|
|
|
$cmsComponent->setParameter(CmsConstants::PARAMETER_DOCUMENTS, |
169
|
|
|
$cmsComponent->storage->getDocuments()->getDocumentsWithState()); |
170
|
|
|
$cmsComponent->setParameter(CmsConstants::PARAMETER_MAIN_NAV_CLASS, CmsConstants::PARAMETER_DOCUMENTS); |
171
|
|
|
|
172
|
|
|
$documentCount = $cmsComponent->storage->getDocuments()->getTotalDocumentCount(); |
173
|
|
|
$indexer = new Search($cmsComponent->storage); |
174
|
|
|
$indexedDocuments = $indexer->getIndexedDocuments(); |
175
|
|
|
$cmsComponent->setParameter(CmsConstants::PARAMETER_SEARCH_NEEDS_UPDATE, $documentCount !== $indexedDocuments); |
176
|
|
|
|
177
|
|
|
$this->handleInfoMessages($cmsComponent, $request); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param CmsComponent $cmsComponent |
182
|
|
|
* @param Request $request |
183
|
|
|
*/ |
184
|
|
|
private function handleInfoMessages($cmsComponent, $request) |
185
|
|
|
{ |
186
|
|
|
if (isset($_GET['not-found'])) { |
187
|
|
|
$cmsComponent->setParameter('infoMessage', 'Document could not be found. It might have been removed.'); |
188
|
|
|
$cmsComponent->setParameter('infoMessageClass', 'error'); |
189
|
|
|
} elseif (isset($_GET['published'])) { |
190
|
|
|
$cmsComponent->setParameter('infoMessage', |
191
|
|
|
'<i class="fa fa-check-circle-o"></i> Document ' . $_GET['published'] . ' published'); |
192
|
|
|
} elseif (isset($_GET['unpublished'])) { |
193
|
|
|
$cmsComponent->setParameter('infoMessage', |
194
|
|
|
'<i class="fa fa-times-circle-o"></i> Document ' . $_GET['unpublished'] . ' unpublished'); |
195
|
|
|
} elseif (isset($_GET['folder-delete'])) { |
196
|
|
|
$cmsComponent->setParameter('infoMessage', '<i class="fa fa-trash"></i> Folder deleted'); |
197
|
|
|
} elseif (isset($_GET['document-delete'])) { |
198
|
|
|
$cmsComponent->setParameter('infoMessage', '<i class="fa fa-trash"></i> Document deleted'); |
199
|
|
|
} elseif (isset($_GET['no-document-types'])) { |
200
|
|
|
$documentTypesLink = $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/configuration/document-types/new'; |
201
|
|
|
$cmsComponent->setParameter('infoMessage', |
202
|
|
|
'<i class="fa fa-exclamation-circle"></i> No document types defined yet. Please do so first, <a href="' . $documentTypesLink . '">here</a>.'); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param $request |
208
|
|
|
* @param CmsComponent $cmsComponent |
209
|
|
|
* @param string $param |
210
|
|
|
*/ |
211
|
|
|
private function doAfterPublishRedirect($request, $cmsComponent, $param = 'published') |
212
|
|
|
{ |
213
|
|
|
if ($cmsComponent->autoUpdateSearchIndex) { |
214
|
|
|
header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/search/update-index?returnUrl=' . urlencode($request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents?' . $param . '=' . urlencode($request::$get[CmsConstants::GET_PARAMETER_SLUG]))); |
215
|
|
|
} else { |
216
|
|
|
header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents?' . $param . '=' . urlencode($request::$get[CmsConstants::GET_PARAMETER_SLUG])); |
217
|
|
|
} |
218
|
|
|
exit; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param $request |
223
|
|
|
* @param CmsComponent $cmsComponent |
224
|
|
|
* @param string $icon |
225
|
|
|
* @param string $activity |
226
|
|
|
*/ |
227
|
|
|
private function clearCacheAndLogActivity( |
228
|
|
|
$request, |
229
|
|
|
$cmsComponent, |
230
|
|
|
$icon = 'check-circle-o', |
231
|
|
|
$activity = 'published' |
232
|
|
|
) |
233
|
|
|
{ |
234
|
|
|
Cache::getInstance()->clearCache(); |
235
|
|
|
$path = $request::$get[CmsConstants::GET_PARAMETER_SLUG]; |
236
|
|
|
$docLink = $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents/edit-document?slug=' . $path; |
237
|
|
|
$cmsComponent->storage->getActivityLog()->add($activity . ' document <a href="' . $docLink . '">' . $request::$get[CmsConstants::GET_PARAMETER_SLUG] . '</a>', |
238
|
|
|
$icon); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param $request |
243
|
|
|
* @param CmsComponent $cmsComponent |
244
|
|
|
* @throws \Exception |
245
|
|
|
*/ |
246
|
|
|
private function createNewDocument($request, $cmsComponent) |
247
|
|
|
{ |
248
|
|
|
$path = substr($cmsComponent->storage->getDocuments()->addDocument($request::$post), 1); |
249
|
|
|
$docLink = $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents/edit-document?slug=' . $path; |
250
|
|
|
$cmsComponent->storage->getActivityLog()->add('created document <a href="' . $docLink . '">' . $request::$post[CmsConstants::POST_PARAMETER_TITLE] . '</a> in path ' . $request::$get[CmsConstants::GET_PARAMETER_PATH], |
251
|
|
|
'plus'); |
252
|
|
|
if (isset($request::$post[CmsConstants::PARAMETER_SAVE_AND_PUBLISH])) { |
253
|
|
|
header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents/publish-document?slug=' . $path); |
254
|
|
|
} else { |
255
|
|
|
header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents'); |
256
|
|
|
} |
257
|
|
|
exit; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @param Request $request |
262
|
|
|
* @param CmsComponent $cmsComponent |
263
|
|
|
*/ |
264
|
|
|
private function putDocumentTypeOnRequest($request, $cmsComponent) |
265
|
|
|
{ |
266
|
|
|
$documentType = $cmsComponent->storage->getDocumentTypes()->getDocumentTypeBySlug($request::$get[CmsConstants::PARAMETER_DOCUMENT_TYPE], |
267
|
|
|
true); |
268
|
|
|
if ($documentType === null) { |
269
|
|
|
$documentTypes = $cmsComponent->storage->getDocumentTypes()->getDocumentTypes(); |
270
|
|
|
$cmsComponent->setParameter(CmsConstants::PARAMETER_DOCUMENT_TYPES, $documentTypes); |
271
|
|
|
} |
272
|
|
|
$cmsComponent->setParameter(CmsConstants::PARAMETER_DOCUMENT_TYPE, $documentType); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @param $request |
277
|
|
|
* @param CmsComponent $cmsComponent |
278
|
|
|
* @param $documentTypes |
279
|
|
|
*/ |
280
|
|
|
private function checkDocumentType($request, $cmsComponent, $documentTypes) |
281
|
|
|
{ |
282
|
|
|
$docTypesCount = count($documentTypes); |
283
|
|
|
if ($docTypesCount < 1) { |
284
|
|
|
header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents?no-document-types'); |
285
|
|
|
exit; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
if ($docTypesCount === 1) { |
289
|
|
|
header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents/new-document?path=' . urlencode($_GET['path']) . '&documentType=' . $documentTypes[0]->slug); |
290
|
|
|
exit; |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @param Request $request |
296
|
|
|
* @param CmsComponent $cmsComponent |
297
|
|
|
* @throws \Exception |
298
|
|
|
*/ |
299
|
|
|
private function newDocumentRoute($request, $cmsComponent) |
300
|
|
|
{ |
301
|
|
|
if (isset($request::$post[CmsConstants::POST_PARAMETER_TITLE], $request::$get[CmsConstants::PARAMETER_DOCUMENT_TYPE], $request::$get[CmsConstants::GET_PARAMETER_PATH])) { |
302
|
|
|
$this->createNewDocument($request, $cmsComponent); |
303
|
|
|
} |
304
|
|
|
$this->putDocumentTypeOnRequest($request, $cmsComponent); |
305
|
|
|
$cmsComponent->setParameter(CmsConstants::PARAMETER_BRICKS, |
306
|
|
|
$cmsComponent->storage->getBricks()->getBricks()); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @param Request $request |
311
|
|
|
* @param CmsComponent $cmsComponent |
312
|
|
|
*/ |
313
|
|
|
private function selectDocumentTypesRoute($request, $cmsComponent) |
314
|
|
|
{ |
315
|
|
|
$documentTypes = $cmsComponent->storage->getDocumentTypes()->getDocumentTypes(); |
316
|
|
|
$this->checkDocumentType($request, $cmsComponent, $documentTypes); |
317
|
|
|
$cmsComponent->setParameter(CmsConstants::PARAMETER_DOCUMENT_TYPES, $documentTypes); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @param Request $request |
322
|
|
|
* @param CmsComponent $cmsComponent |
323
|
|
|
* @throws \Exception |
324
|
|
|
*/ |
325
|
|
|
protected function saveDocument($request, $cmsComponent) |
326
|
|
|
{ |
327
|
|
|
$path = substr($cmsComponent->storage->getDocuments()->saveDocument($request::$post), 1); |
328
|
|
|
$docLink = $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents/edit-document?slug=' . $path; |
329
|
|
|
$cmsComponent->storage->getActivityLog()->add('edited document <a href="' . $docLink . '">' . $request::$post[CmsConstants::POST_PARAMETER_TITLE] . '</a> in path /' . $request::$get[CmsConstants::GET_PARAMETER_SLUG], |
330
|
|
|
'pencil'); |
331
|
|
|
if (isset($request::$post[CmsConstants::PARAMETER_SAVE_AND_PUBLISH])) { |
332
|
|
|
header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents/publish-document?slug=' . $path); |
333
|
|
|
} else { |
334
|
|
|
header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents'); |
335
|
|
|
} |
336
|
|
|
exit; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @param CmsComponent $cmsComponent |
341
|
|
|
* @param Document $document |
342
|
|
|
*/ |
343
|
|
|
protected function setDocumentTypeParameter($cmsComponent, $document) |
344
|
|
|
{ |
345
|
|
|
$documentType = $cmsComponent->storage->getDocumentTypes()->getDocumentTypeBySlug($document->documentTypeSlug, |
346
|
|
|
true); |
347
|
|
|
if ($documentType === null) { |
348
|
|
|
$documentTypes = $cmsComponent->storage->getDocumentTypes()->getDocumentTypes(); |
349
|
|
|
$cmsComponent->setParameter(CmsConstants::PARAMETER_DOCUMENT_TYPES, $documentTypes); |
350
|
|
|
} |
351
|
|
|
$cmsComponent->setParameter(CmsConstants::PARAMETER_DOCUMENT_TYPE, $documentType); |
352
|
|
|
} |
353
|
|
|
} |