[TASK] Use ModuleTemplate API for EXT:documentation
[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\Backend\View\BackendTemplateView;
18 use TYPO3\CMS\Core\Messaging\FlashMessage;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Documentation\Domain\Repository\DocumentRepository;
21 use TYPO3\CMS\Documentation\Service\DocumentationService;
22 use TYPO3\CMS\Documentation\Utility\LanguageUtility;
23 use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
24 use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
25 use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
26 use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
27 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
28 use TYPO3\CMS\Lang\LanguageService;
29
30 /**
31 * Main controller of the Documentation module.
32 */
33 class DocumentController extends ActionController {
34
35 /**
36 * @var DocumentRepository
37 */
38 protected $documentRepository;
39
40 /**
41 * @var DocumentationService
42 */
43 protected $documentationService;
44
45 /**
46 * @var LanguageUtility
47 */
48 protected $languageUtility;
49
50 /**
51 * @var Dispatcher
52 */
53 protected $signalSlotDispatcher;
54
55 /**
56 * Backend Template Container
57 *
58 * @var BackendTemplateView
59 */
60 protected $defaultViewObjectName = BackendTemplateView::class;
61
62 /**
63 * BackendTemplateContainer
64 *
65 * @var BackendTemplateView
66 */
67 protected $view;
68
69 /**
70 * Set up the doc header properly here
71 *
72 * @param ViewInterface $view
73 */
74 protected function initializeView(ViewInterface $view) {
75 /** @var BackendTemplateView $view */
76 parent::initializeView($view);
77 $view->getModuleTemplate()->getDocHeaderComponent()->setMetaInformation([]);
78 $uriBuilder = $this->objectManager->get(UriBuilder::class);
79 $uriBuilder->setRequest($this->request);
80
81 $this->view->getModuleTemplate()->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Documentation/Main');
82 $menu = $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
83 $menu->setIdentifier('DocumentationModuleMenu');
84
85 $isListActive = $this->request->getControllerActionName() === 'list' ? TRUE : FALSE;
86 $uri = $uriBuilder->reset()->uriFor('list', array(), 'Document');
87 $listMenuItem = $menu->makeMenuItem()
88 ->setTitle($this->getLanguageService()->sL('LLL:EXT:documentation/Resources/Private/Language/locallang.xlf:showDocumentation'))
89 ->setHref($uri)
90 ->setActive($isListActive);
91 $menu->addMenuItem($listMenuItem);
92
93 if ($this->getBackendUser()->isAdmin()) {
94 $isDownloadActive = $this->request->getControllerActionName() ===
95 'download' ? TRUE : FALSE;
96 $uri =
97 $uriBuilder->reset()->uriFor('download', array(), 'Document');
98 $downloadMenuItem = $menu->makeMenuItem()
99 ->setTitle($this->getLanguageService()->sL('LLL:EXT:documentation/Resources/Private/Language/locallang.xlf:downloadDocumentation'))
100 ->setHref($uri)
101 ->setActive($isDownloadActive);
102 $menu->addMenuItem($downloadMenuItem);
103 }
104
105 $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
106 }
107
108 /**
109 * @param DocumentRepository $documentRepository
110 */
111 public function injectDocumentRepository(DocumentRepository $documentRepository) {
112 $this->documentRepository = $documentRepository;
113 }
114
115 /**
116 * @param DocumentationService $documentationService
117 */
118 public function injectDocumentationService(DocumentationService $documentationService) {
119 $this->documentationService = $documentationService;
120 }
121
122 /**
123 * @param LanguageUtility $languageUtility
124 */
125 public function injectLanguageUtility(LanguageUtility $languageUtility) {
126 $this->languageUtility = $languageUtility;
127 }
128
129 /**
130 * @param Dispatcher $signalSlotDispatcher
131 */
132 public function injectSignalSlotDispatcher(Dispatcher $signalSlotDispatcher) {
133 $this->signalSlotDispatcher = $signalSlotDispatcher;
134 }
135
136 /**
137 * Lists the available documents.
138 *
139 * @return void
140 */
141 public function listAction() {
142 $this->view->getModuleTemplate()->getDocHeaderComponent()->setMetaInformation([]);
143
144 $documents = $this->getDocuments();
145
146 // Filter documents to be shown for current user
147 $hideDocuments = $this->getBackendUser()->getTSConfigVal('mod.help_DocumentationDocumentation.documents.hide');
148 $hideDocuments = GeneralUtility::trimExplode(',', $hideDocuments, TRUE);
149 if (!empty($hideDocuments)) {
150 $documents = array_diff_key($documents, array_flip($hideDocuments));
151 }
152 $showDocuments = $this->getBackendUser()->getTSConfigVal('mod.help_DocumentationDocumentation.documents.show');
153 $showDocuments = GeneralUtility::trimExplode(',', $showDocuments, TRUE);
154 if (!empty($showDocuments)) {
155 $documents = array_intersect_key($documents, array_flip($showDocuments));
156 }
157
158 $this->view->assign('documents', $documents);
159 }
160
161 /**
162 * Returns available documents.
163 *
164 * @return \TYPO3\CMS\Documentation\Domain\Model\Document[]
165 * @api
166 */
167 public function getDocuments() {
168 $language = $this->languageUtility->getDocumentationLanguage();
169 $documents = $this->documentRepository->findByLanguage($language);
170
171 $documents = $this->emitAfterInitializeDocumentsSignal($language, $documents);
172
173 return $documents;
174 }
175
176 /**
177 * Emits a signal after the documents are initialized
178 *
179 * @param string $language
180 * @param \TYPO3\CMS\Documentation\Domain\Model\Document[] $documents
181 * @return \TYPO3\CMS\Documentation\Domain\Model\Document[]
182 */
183 protected function emitAfterInitializeDocumentsSignal($language, array $documents) {
184 $this->signalSlotDispatcher->dispatch(
185 __CLASS__,
186 'afterInitializeDocuments',
187 array(
188 $language,
189 &$documents,
190 )
191 );
192 return $documents;
193 }
194
195 /**
196 * Shows documents to be downloaded/fetched from a remote location.
197 *
198 * @return void
199 */
200 public function downloadAction() {
201 // This action is reserved for admin users. Redirect to default view if not.
202 if (!$this->getBackendUser()->isAdmin()) {
203 $this->redirect('list');
204 }
205
206 // Retrieve the list of official documents
207 $documents = $this->documentationService->getOfficialDocuments();
208
209 // Merge with the list of local extensions
210 $extensions = $this->documentationService->getLocalExtensions();
211 $allDocuments = array_merge($documents, $extensions);
212
213 $this->view->assign('documents', $allDocuments);
214 }
215
216 /**
217 * Fetches a document from a remote URL.
218 *
219 * @param string $url
220 * @param string $key
221 * @param string $version
222 * @return void
223 */
224 public function fetchAction($url, $key, $version = NULL) {
225 // This action is reserved for admin users. Redirect to default view if not.
226 if (!$this->getBackendUser()->isAdmin()) {
227 $this->redirect('list');
228 }
229
230 $language = $this->languageUtility->getDocumentationLanguage();
231 try {
232 $result = $this->documentationService->fetchNearestDocument($url, $key, $version ?: 'latest', $language);
233 if ($result) {
234 $this->addFlashMessage(
235 LocalizationUtility::translate(
236 'downloadSucceeded',
237 'documentation'
238 ),
239 '',
240 FlashMessage::OK
241 );
242 } else {
243 $this->addFlashMessage(
244 LocalizationUtility::translate(
245 'downloadFailedNoArchive',
246 'documentation'
247 ),
248 LocalizationUtility::translate(
249 'downloadFailed',
250 'documentation'
251 ),
252 FlashMessage::ERROR
253 );
254 }
255 } catch (\Exception $e) {
256 $this->addFlashMessage(
257 LocalizationUtility::translate(
258 'downloadFailedDetails',
259 'documentation',
260 array(
261 $key,
262 $e->getMessage(),
263 $e->getCode()
264 )
265 ),
266 LocalizationUtility::translate(
267 'downloadFailed',
268 'documentation'
269 ),
270 FlashMessage::ERROR
271 );
272 }
273 $this->redirect('download');
274 }
275
276 /**
277 * Get backend user
278 *
279 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
280 */
281 protected function getBackendUser() {
282 return $GLOBALS['BE_USER'];
283 }
284
285 /**
286 * Returns the LanguageService
287 *
288 * @return LanguageService
289 */
290 protected function getLanguageService() {
291 return $GLOBALS['LANG'];
292 }
293
294 }