Passed
Push — develop ( cd27e2...ec33de )
by Jens
07:45
created

DocumentRouting::documentNewRoute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 3
b 0
f 0
nc 3
nop 2
dl 0
loc 15
rs 9.4285
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\components\cms\document\FolderRouting;
13
use CloudControl\Cms\components\CmsComponent;
14
use CloudControl\Cms\search\Search;
15
use CloudControl\Cms\storage\Cache;
16
use CloudControl\Cms\storage\entities\Document;
17
18
class DocumentRouting implements CmsRouting
19
{
20
    /**
21
     * DocumentRouting constructor.
22
     * @param $request
23
     * @param $relativeCmsUri
24
     * @param CmsComponent $cmsComponent
25
     * @throws \Exception
26
     */
27
    public function __construct(Request $request, $relativeCmsUri, CmsComponent $cmsComponent)
28
    {
29
        if ($relativeCmsUri == '/documents') {
30
            $this->overviewRouting($cmsComponent, $request);
31
        }
32
        $this->documentRouting($request, $relativeCmsUri, $cmsComponent);
33
        new FolderRouting($request, $relativeCmsUri, $cmsComponent);
34
    }
35
36
37
    /**
38
     * @param Request $request
39
     * @param string $relativeCmsUri
40
     * @param CmsComponent $cmsComponent
41
     * @throws \Exception
42
     */
43
    private function documentRouting($request, $relativeCmsUri, $cmsComponent)
44
    {
45
        if ($relativeCmsUri == '/documents/new-document' && isset($request::$get[CmsConstants::GET_PARAMETER_PATH])) {
46
            $this->documentNewRoute($request, $cmsComponent);
47
        } elseif (isset($request::$get[CmsConstants::GET_PARAMETER_SLUG])) {
48
            switch ($relativeCmsUri) {
49
                case '/documents/edit-document': $this->editDocumentRoute($request, $cmsComponent); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
50
                case '/documents/get-brick': $this->getBrickRoute($request, $cmsComponent); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
51
                case '/documents/delete-document': $this->deleteDocumentRoute($request, $cmsComponent); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
52
                case '/documents/publish-document': $this->publishDocumentRoute($request, $cmsComponent); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
53
                case '/documents/unpublish-document': $this->unpublishDocumentRoute($request, $cmsComponent); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
54
            }
55
        }
56
    }
57
58
    /**
59
     * @param $request
60
     * @param CmsComponent $cmsComponent
61
     *
62
     * @throws \Exception
63
     */
64
    private function documentNewRoute($request, $cmsComponent)
65
    {
66
        $cmsComponent->subTemplate = 'documents/document-form';
67
        $cmsComponent->setParameter(CmsConstants::PARAMETER_MAIN_NAV_CLASS, CmsConstants::PARAMETER_DOCUMENTS);
68
        $cmsComponent->setParameter(CmsConstants::PARAMETER_SMALLEST_IMAGE, $cmsComponent->storage->getImageSet()->getSmallestImageSet()->slug);
69
        if (isset($request::$get[CmsConstants::PARAMETER_DOCUMENT_TYPE])) {
70
            if (isset($request::$post[CmsConstants::POST_PARAMETER_TITLE], $request::$get[CmsConstants::PARAMETER_DOCUMENT_TYPE], $request::$get[CmsConstants::GET_PARAMETER_PATH])) {
71
                $this->createNewDocument($request, $cmsComponent);
72
            }
73
            $this->putDocumentTypeOnRequest($request, $cmsComponent);
74
            $cmsComponent->setParameter(CmsConstants::PARAMETER_BRICKS, $cmsComponent->storage->getBricks()->getBricks());
75
        } else {
76
            $documentTypes = $cmsComponent->storage->getDocumentTypes()->getDocumentTypes();
77
            $this->checkDocumentType($request, $cmsComponent, $documentTypes);
78
            $cmsComponent->setParameter(CmsConstants::PARAMETER_DOCUMENT_TYPES, $documentTypes);
79
        }
80
    }
81
82
    /**
83
     * @param $request
84
     * @param CmsComponent $cmsComponent
85
     * @throws \Exception
86
     */
87
    private function editDocumentRoute($request, $cmsComponent)
88
    {
89
        $cmsComponent->subTemplate = 'documents/document-form';
90
        $cmsComponent->setParameter(CmsConstants::PARAMETER_MAIN_NAV_CLASS, CmsConstants::PARAMETER_DOCUMENTS);
91
        $cmsComponent->setParameter(CmsConstants::PARAMETER_SMALLEST_IMAGE, $cmsComponent->storage->getImageSet()->getSmallestImageSet()->slug);
92
        if (isset($request::$post[CmsConstants::POST_PARAMETER_TITLE], $request::$get[CmsConstants::GET_PARAMETER_SLUG])) {
93
            $path = substr($cmsComponent->storage->getDocuments()->saveDocument($request::$post), 1);
94
            $docLink = $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents/edit-document?slug=' . $path;
95
            $cmsComponent->storage->getActivityLog()->add('edited document <a href="' . $docLink . '">' . $request::$post[CmsConstants::POST_PARAMETER_TITLE] . '</a> in path /' . $request::$get[CmsConstants::GET_PARAMETER_SLUG], 'pencil');
96
            if (isset($request::$post[CmsConstants::PARAMETER_SAVE_AND_PUBLISH])) {
97
                header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents/publish-document?slug=' . $path);
98
            } else {
99
                header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents');
100
            }
101
            exit;
102
        }
103
        $document = $cmsComponent->storage->getDocuments()->getDocumentBySlug($request::$get[CmsConstants::GET_PARAMETER_SLUG], 'unpublished');
104
        $cmsComponent->setParameter(CmsConstants::PARAMETER_DOCUMENT, $document);
105
106
        $request::$get[CmsConstants::GET_PARAMETER_PATH] = $request::$get[CmsConstants::GET_PARAMETER_SLUG];
107
        if ($document instanceof Document) {
108
            $documentType = $cmsComponent->storage->getDocumentTypes()->getDocumentTypeBySlug($document->documentTypeSlug, true);
109
            if ($documentType === null) {
110
                $documentTypes = $cmsComponent->storage->getDocumentTypes()->getDocumentTypes();
111
                $cmsComponent->setParameter(CmsConstants::PARAMETER_DOCUMENT_TYPES, $documentTypes);
112
            }
113
            $cmsComponent->setParameter(CmsConstants::PARAMETER_DOCUMENT_TYPE, $documentType);
114
        } else {
115
            header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents?not-found');
116
            exit;
117
        }
118
119
        $cmsComponent->setParameter(CmsConstants::PARAMETER_BRICKS, $cmsComponent->storage->getBricks()->getBricks());
120
    }
121
122
    /**
123
     * @param $request
124
     * @param CmsComponent $cmsComponent
125
     * @throws \Exception
126
     */
127
    private function getBrickRoute($request, $cmsComponent)
128
    {
129
        $cmsComponent->setParameter(CmsConstants::PARAMETER_SMALLEST_IMAGE, $cmsComponent->storage->getImageSet()->getSmallestImageSet()->slug);
130
        $cmsComponent->subTemplate = 'documents/brick';
131
        $cmsComponent->setParameter(CmsConstants::PARAMETER_BRICK, $cmsComponent->storage->getBricks()->getBrickBySlug($request::$get[CmsConstants::GET_PARAMETER_SLUG]));
132
        $cmsComponent->setParameter(CmsConstants::PARAMETER_STATIC, $request::$get[CmsConstants::PARAMETER_STATIC] === 'true');
133
        if (isset($request::$get[CmsConstants::PARAMETER_MY_BRICK_SLUG])) {
134
            $cmsComponent->setParameter(CmsConstants::PARAMETER_MY_BRICK_SLUG, $request::$get[CmsConstants::PARAMETER_MY_BRICK_SLUG]);
135
        }
136
        $result = new \stdClass();
137
        $result->body = $cmsComponent->renderTemplate('documents/brick');
138
        $result->rteList = isset($GLOBALS['rteList']) ? $GLOBALS['rteList'] : array();
139
        ob_clean();
140
        header(CmsConstants::CONTENT_TYPE_APPLICATION_JSON);
141
        die(json_encode($result));
142
    }
143
144
    /**
145
     * @param $request
146
     * @param CmsComponent $cmsComponent
147
     */
148
    private function deleteDocumentRoute($request, $cmsComponent)
149
    {
150
        $cmsComponent->storage->getDocuments()->deleteDocumentBySlug($request::$get[CmsConstants::GET_PARAMETER_SLUG]);
151
        $cmsComponent->storage->getActivityLog()->add('deleted document /' . $request::$get[CmsConstants::GET_PARAMETER_SLUG], 'trash');
152
        header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents?document-delete');
153
        exit;
154
    }
155
156
    /**
157
     * @param $request
158
     * @param CmsComponent $cmsComponent
159
     */
160
    private function publishDocumentRoute($request, $cmsComponent)
161
    {
162
        $cmsComponent->storage->getDocuments()->publishDocumentBySlug($request::$get[CmsConstants::GET_PARAMETER_SLUG]);
163
        $this->clearCacheAndLogActivity($request, $cmsComponent);
164
        $this->doAfterPublishRedirect($request, $cmsComponent);
165
    }
166
167
    /**
168
     * @param $request
169
     * @param CmsComponent $cmsComponent
170
     */
171
    private function unpublishDocumentRoute($request, $cmsComponent)
172
    {
173
        $cmsComponent->storage->getDocuments()->unpublishDocumentBySlug($request::$get[CmsConstants::GET_PARAMETER_SLUG]);
174
        $this->clearCacheAndLogActivity($request, $cmsComponent, 'times-circle-o', 'unpublished');
175
        $this->doAfterPublishRedirect($request, $cmsComponent, 'unpublished');
176
    }
177
178
    /**
179
     * @param CmsComponent $cmsComponent
180
     * @param Request $request
181
     * @throws \Exception
182
     */
183
    private function overviewRouting($cmsComponent, $request)
184
    {
185
        $cmsComponent->subTemplate = 'documents';
186
        $cmsComponent->setParameter(CmsConstants::PARAMETER_DOCUMENTS, $cmsComponent->storage->getDocuments()->getDocumentsWithState());
187
        $cmsComponent->setParameter(CmsConstants::PARAMETER_MAIN_NAV_CLASS, CmsConstants::PARAMETER_DOCUMENTS);
188
189
        $documentCount = $cmsComponent->storage->getDocuments()->getTotalDocumentCount();
190
        $indexer = new Search($cmsComponent->storage);
191
        $indexedDocuments = $indexer->getIndexedDocuments();
192
        $cmsComponent->setParameter(CmsConstants::PARAMETER_SEARCH_NEEDS_UPDATE, $documentCount !== $indexedDocuments);
193
194
        $this->handleInfoMessages($cmsComponent, $request);
195
    }
196
197
    /**
198
     * @param CmsComponent $cmsComponent
199
     * @param Request $request
200
     */
201
    private function handleInfoMessages($cmsComponent, $request)
202
    {
203
        if (isset($_GET['not-found'])) {
204
            $cmsComponent->setParameter('infoMessage', 'Document could not be found. It might have been removed.');
205
            $cmsComponent->setParameter('infoMessageClass', 'error');
206
        } elseif (isset($_GET['published'])) {
207
            $cmsComponent->setParameter('infoMessage', '<i class="fa fa-check-circle-o"></i> Document ' . $_GET['published'] . ' published');
208
        } elseif (isset($_GET['unpublished'])) {
209
            $cmsComponent->setParameter('infoMessage', '<i class="fa fa-times-circle-o"></i> Document ' . $_GET['unpublished'] . ' unpublished');
210
        } elseif (isset($_GET['folder-delete'])) {
211
            $cmsComponent->setParameter('infoMessage', '<i class="fa fa-trash"></i> Folder deleted');
212
        } elseif (isset($_GET['document-delete'])) {
213
            $cmsComponent->setParameter('infoMessage', '<i class="fa fa-trash"></i> Document deleted');
214
        } elseif (isset($_GET['no-document-types'])) {
215
            $documentTypesLink = $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/configuration/document-types/new';
216
            $cmsComponent->setParameter('infoMessage', '<i class="fa fa-exclamation-circle"></i> No document types defined yet. Please do so first, <a href="' . $documentTypesLink . '">here</a>.');
217
        }
218
    }
219
220
    /**
221
     * @param $request
222
     * @param $cmsComponent
223
     * @param string $param
224
     */
225
    private function doAfterPublishRedirect($request, $cmsComponent, $param = 'published')
226
    {
227
        if ($cmsComponent->autoUpdateSearchIndex) {
228
            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])));
229
        } else {
230
            header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents?' . $param . '=' . urlencode($request::$get[CmsConstants::GET_PARAMETER_SLUG]));
231
        }
232
        exit;
233
    }
234
235
    /**
236
     * @param $request
237
     * @param $cmsComponent
238
     * @param string $icon
239
     * @param string $activity
240
     */
241
    private function clearCacheAndLogActivity($request, $cmsComponent, $icon = 'check-circle-o', $activity = 'published')
242
    {
243
        Cache::getInstance()->clearCache();
244
        $path = $request::$get[CmsConstants::GET_PARAMETER_SLUG];
245
        $docLink = $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents/edit-document?slug=' . $path;
246
        $cmsComponent->storage->getActivityLog()->add($activity. ' document <a href="' . $docLink . '">' . $request::$get[CmsConstants::GET_PARAMETER_SLUG] . '</a>', $icon);
247
    }
248
249
    /**
250
     * @param $request
251
     * @param $cmsComponent
252
     */
253
    private function createNewDocument($request, $cmsComponent)
254
    {
255
        $path = substr($cmsComponent->storage->getDocuments()->addDocument($request::$post), 1);
256
        $docLink = $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents/edit-document?slug=' . $path;
257
        $cmsComponent->storage->getActivityLog()->add('created document <a href="' . $docLink . '">' . $request::$post[CmsConstants::POST_PARAMETER_TITLE] . '</a> in path ' . $request::$get[CmsConstants::GET_PARAMETER_PATH], 'plus');
258
        if (isset($request::$post[CmsConstants::PARAMETER_SAVE_AND_PUBLISH])) {
259
            header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents/publish-document?slug=' . $path);
260
        } else {
261
            header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents');
262
        }
263
        exit;
264
    }
265
266
    /**
267
     * @param $request
268
     * @param $cmsComponent
269
     */
270
    private function putDocumentTypeOnRequest($request, $cmsComponent)
271
    {
272
        $documentType = $cmsComponent->storage->getDocumentTypes()->getDocumentTypeBySlug($request::$get[CmsConstants::PARAMETER_DOCUMENT_TYPE], true);
273
        if ($documentType === null) {
274
            $documentTypes = $cmsComponent->storage->getDocumentTypes()->getDocumentTypes();
275
            $cmsComponent->setParameter(CmsConstants::PARAMETER_DOCUMENT_TYPES, $documentTypes);
276
        }
277
        $cmsComponent->setParameter(CmsConstants::PARAMETER_DOCUMENT_TYPE, $documentType);
278
    }
279
280
    /**
281
     * @param $request
282
     * @param $cmsComponent
283
     * @param $documentTypes
284
     */
285
    private function checkDocumentType($request, $cmsComponent, $documentTypes)
286
    {
287
        $docTypesCount = count($documentTypes);
288
        if ($docTypesCount < 1) {
289
            header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents?no-document-types');
290
            exit;
291
        } elseif ($docTypesCount == 1) {
292
            header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsConstants::PARAMETER_CMS_PREFIX) . '/documents/new-document?path=' . urlencode($_GET['path']) . '&documentType=' . $documentTypes[0]->slug);
293
            exit;
294
        }
295
    }
296
}