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