[TASK] Re-work/simplify copyright header in PHP files - Part 2
[Packages/TYPO3.CMS.git] / typo3 / sysext / documentation / Classes / Controller / DocumentController.php
1 <?php
2 namespace TYPO3\CMS\Documentation\Controller;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * Main controller of the Documentation module.
21 *
22 * @author Andrea Schmuttermair <spam@schmutt.de>
23 */
24 class DocumentController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
25
26 /**
27 * documentRepository
28 *
29 * @var \TYPO3\CMS\Documentation\Domain\Repository\DocumentRepository
30 * @inject
31 */
32 protected $documentRepository;
33
34 /**
35 * @var \TYPO3\CMS\Documentation\Service\DocumentationService
36 * @inject
37 */
38 protected $documentationService;
39
40 /**
41 * languageUtility
42 *
43 * @var \TYPO3\CMS\Documentation\Utility\LanguageUtility
44 * @inject
45 */
46 protected $languageUtility;
47
48 /**
49 * Signal Slot dispatcher
50 *
51 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
52 * @inject
53 */
54 protected $signalSlotDispatcher;
55
56 /**
57 * Lists the available documents.
58 *
59 * @return void
60 */
61 public function listAction() {
62 $documents = $this->getDocuments();
63
64 // Filter documents to be shown for current user
65 $hideDocuments = $this->getBackendUser()->getTSConfigVal('mod.help_DocumentationDocumentation.documents.hide');
66 $hideDocuments = GeneralUtility::trimExplode(',', $hideDocuments, TRUE);
67 if (count($hideDocuments) > 0) {
68 $documents = array_diff_key($documents, array_flip($hideDocuments));
69 }
70 $showDocuments = $this->getBackendUser()->getTSConfigVal('mod.help_DocumentationDocumentation.documents.show');
71 $showDocuments = GeneralUtility::trimExplode(',', $showDocuments, TRUE);
72 if (count($showDocuments) > 0) {
73 $documents = array_intersect_key($documents, array_flip($showDocuments));
74 }
75
76 $this->view->assign('documents', $documents);
77 }
78
79 /**
80 * Returns available documents.
81 *
82 * @return \TYPO3\CMS\Documentation\Domain\Model\Document[]
83 * @api
84 */
85 public function getDocuments() {
86 $language = $this->languageUtility->getDocumentationLanguage();
87 $documents = $this->documentRepository->findByLanguage($language);
88
89 $documents = $this->emitAfterInitializeDocumentsSignal($language, $documents);
90
91 return $documents;
92 }
93
94 /**
95 * Emits a signal after the documents are initialized
96 *
97 * @param string $language
98 * @param \TYPO3\CMS\Documentation\Domain\Model\Document[] $documents
99 * @return \TYPO3\CMS\Documentation\Domain\Model\Document[]
100 */
101 protected function emitAfterInitializeDocumentsSignal($language, array $documents) {
102 $signalArguments = $this->signalSlotDispatcher->dispatch(
103 __CLASS__,
104 'afterInitializeDocuments',
105 array(
106 'language' => $language,
107 'documents' => $documents,
108 )
109 );
110 return $signalArguments[1];
111 }
112
113 /**
114 * Shows documents to be downloaded/fetched from a remote location.
115 *
116 * @return void
117 */
118 public function downloadAction() {
119 // This action is reserved for admin users. Redirect to default view if not.
120 if (!$this->getBackendUser()->isAdmin()) {
121 $this->redirect('list');
122 }
123
124 // Retrieve the list of official documents
125 $documents = $this->documentationService->getOfficialDocuments();
126
127 // Merge with the list of local extensions
128 $extensions = $this->documentationService->getLocalExtensions();
129 $allDocuments = array_merge($documents, $extensions);
130
131 $this->view->assign('documents', $allDocuments);
132 }
133
134 /**
135 * Fetches a document from a remote URL.
136 *
137 * @param string $url
138 * @param string $key
139 * @param string $version
140 * @return void
141 */
142 public function fetchAction($url, $key, $version = NULL) {
143 // This action is reserved for admin users. Redirect to default view if not.
144 if (!$this->getBackendUser()->isAdmin()) {
145 $this->redirect('list');
146 }
147
148 $language = $this->languageUtility->getDocumentationLanguage();
149 try {
150 $result = $this->documentationService->fetchNearestDocument($url, $key, $version ?: 'latest', $language);
151
152 if ($result) {
153 $this->controllerContext->getFlashMessageQueue()->enqueue(
154 GeneralUtility::makeInstance(
155 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
156 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
157 'downloadSucceeded',
158 'documentation'
159 ),
160 '',
161 \TYPO3\CMS\Core\Messaging\AbstractMessage::OK,
162 TRUE
163 )
164 );
165 } else {
166 $this->controllerContext->getFlashMessageQueue()->enqueue(
167 GeneralUtility::makeInstance(
168 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
169 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
170 'downloadFailedNoArchive',
171 'documentation'
172 ),
173 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
174 'downloadFailed',
175 'documentation'
176 ),
177 \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR,
178 TRUE
179 )
180 );
181 }
182 } catch (\Exception $e) {
183 $this->controllerContext->getFlashMessageQueue()->enqueue(
184 GeneralUtility::makeInstance(
185 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
186 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
187 'downloadFailedDetails',
188 'documentation',
189 array(
190 $key,
191 $e->getMessage(),
192 $e->getCode()
193 )
194 ),
195 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
196 'downloadFailed',
197 'documentation'
198 ),
199 \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR,
200 TRUE
201 )
202 );
203 }
204 $this->redirect('download');
205 }
206
207 /**
208 * Get backend user
209 *
210 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
211 */
212 protected function getBackendUser() {
213 return $GLOBALS['BE_USER'];
214 }
215
216 }