Passed
Push — develop ( 4d9250...cd27e2 )
by Jens
05:47
created

DocumentRouting::documentNewRoute()   C

Complexity

Conditions 7
Paths 3

Size

Total Lines 35
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

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