Passed
Push — master ( 1a2f46...8e46a2 )
by Jens
04:11
created

DocumentRouting   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 235
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
dl 0
loc 235
rs 8.6
c 3
b 1
f 0
wmc 37

12 Methods

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