TYPO3 CMS  TYPO3_8-7
DocumentController.php
Go to the documentation of this file.
1 <?php
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 
32 
37 {
42 
47 
51  protected $languageUtility;
52 
57 
63  protected $defaultViewObjectName = BackendTemplateView::class;
64 
70  protected $view;
71 
77  protected function initializeView(ViewInterface $view)
78  {
79  if ($view instanceof BackendTemplateView) {
81  parent::initializeView($view);
82  $view->getModuleTemplate()->getDocHeaderComponent()->setMetaInformation([]);
83  $uriBuilder = $this->objectManager->get(UriBuilder::class);
84  $uriBuilder->setRequest($this->request);
85 
86  $this->view->getModuleTemplate()->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Documentation/Main');
87  $menu = $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
88  $menu->setIdentifier('DocumentationModuleMenu');
89 
90  $isListActive = $this->request->getControllerActionName() === 'list' ? true : false;
91  $uri = $uriBuilder->reset()->uriFor('list', [], 'Document');
92  $listMenuItem = $menu->makeMenuItem()
93  ->setTitle($this->getLanguageService()
94  ->sL('LLL:EXT:documentation/Resources/Private/Language/locallang.xlf:showDocumentation'))
95  ->setHref($uri)
96  ->setActive($isListActive);
97  $menu->addMenuItem($listMenuItem);
98 
99  if ($this->getBackendUser()->isAdmin()) {
100  $isDownloadActive = $this->request->getControllerActionName() ===
101  'download' ? true : false;
102  $uri =
103  $uriBuilder->reset()->uriFor('download', [], 'Document');
104  $downloadMenuItem = $menu->makeMenuItem()
105  ->setTitle($this->getLanguageService()
106  ->sL('LLL:EXT:documentation/Resources/Private/Language/locallang.xlf:downloadDocumentation'))
107  ->setHref($uri)
108  ->setActive($isDownloadActive);
109  $menu->addMenuItem($downloadMenuItem);
110  }
111 
112  $this->view->getModuleTemplate()->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
113  $this->view->getModuleTemplate()->setFlashMessageQueue($this->controllerContext->getFlashMessageQueue());
114  }
115  }
116 
121  {
122  $this->documentRepository = $documentRepository;
123  }
124 
129  {
130  $this->documentationService = $documentationService;
131  }
132 
137  {
138  $this->languageUtility = $languageUtility;
139  }
140 
145  {
146  $this->signalSlotDispatcher = $signalSlotDispatcher;
147  }
148 
152  public function listAction()
153  {
154  $this->view->getModuleTemplate()->getDocHeaderComponent()->setMetaInformation([]);
155 
156  $documents = $this->getDocuments();
157 
158  // Filter documents to be shown for current user
159  $hideDocuments = $this->getBackendUser()->getTSConfigVal('mod.help_DocumentationDocumentation.documents.hide');
160  $hideDocuments = GeneralUtility::trimExplode(',', $hideDocuments, true);
161  if (!empty($hideDocuments)) {
162  $documents = array_diff_key($documents, array_flip($hideDocuments));
163  }
164  $showDocuments = $this->getBackendUser()->getTSConfigVal('mod.help_DocumentationDocumentation.documents.show');
165  $showDocuments = GeneralUtility::trimExplode(',', $showDocuments, true);
166  if (!empty($showDocuments)) {
167  $documents = array_intersect_key($documents, array_flip($showDocuments));
168  }
169 
170  $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
171  $pageRenderer->addInlineLanguageLabelFile('EXT:documentation/Resources/Private/Language/locallang.xlf');
172  $pageRenderer->loadRequireJsModule('TYPO3/CMS/Documentation/Main');
173 
174  $this->view->assign('documents', $documents);
175  }
176 
184  public function deleteAction(ServerRequestInterface $request, ResponseInterface $response)
185  {
186  $basePath = 'typo3conf/Documentation/';
187  $packageKey = $request->getParsedBody();
188  $isDirDeleted = GeneralUtility::rmdir(PATH_site . $basePath . $packageKey['documentationKey'], true);
189  if (!$isDirDeleted) {
190  $this->addFlashMessage(LocalizationUtility::translate('deleteFailed', 'Documentation'), '', FlashMessage::ERROR);
191  }
192 
193  $response->getBody()->write(json_encode($isDirDeleted));
194  return $response;
195  }
196 
203  public function getDocuments()
204  {
205  $language = $this->languageUtility->getDocumentationLanguage();
206  $documents = $this->documentRepository->findByLanguage($language);
207 
208  $documents = $this->emitAfterInitializeDocumentsSignal($language, $documents);
209 
210  return $documents;
211  }
212 
220  protected function emitAfterInitializeDocumentsSignal($language, array $documents)
221  {
222  $this->signalSlotDispatcher->dispatch(
223  __CLASS__,
224  'afterInitializeDocuments',
225  [
226  $language,
227  &$documents,
228  ]
229  );
230  return $documents;
231  }
232 
236  public function downloadAction()
237  {
238  // This action is reserved for admin users. Redirect to default view if not.
239  if (!$this->getBackendUser()->isAdmin()) {
240  $this->redirect('list');
241  }
242 
243  // Retrieve the list of official documents
244  $documents = $this->documentationService->getOfficialDocuments();
245 
246  // Merge with the list of local extensions
247  $extensions = $this->documentationService->getLocalExtensions();
248  $allDocuments = array_merge($documents, $extensions);
249 
250  $this->view->assign('documents', $allDocuments);
251  }
252 
260  public function fetchAction($url, $key, $version = null)
261  {
262  // This action is reserved for admin users. Redirect to default view if not.
263  if (!$this->getBackendUser()->isAdmin()) {
264  $this->redirect('list');
265  }
266 
267  $language = $this->languageUtility->getDocumentationLanguage();
268  try {
269  $result = $this->documentationService->fetchNearestDocument($url, $key, $version ?: 'latest', $language);
270  if ($result) {
271  $this->addFlashMessage(
273  'downloadSucceeded',
274  'documentation'
275  ),
276  '',
278  );
279  } else {
280  $this->addFlashMessage(
282  'downloadFailedNoArchive',
283  'documentation'
284  ),
286  'downloadFailed',
287  'documentation'
288  ),
290  );
291  }
292  } catch (\Exception $e) {
293  $this->addFlashMessage(
295  'downloadFailedDetails',
296  'documentation',
297  [
298  $key,
299  $e->getMessage(),
300  $e->getCode()
301  ]
302  ),
304  'downloadFailed',
305  'documentation'
306  ),
308  );
309  }
310  $this->redirect('download');
311  }
312 
318  protected function getBackendUser()
319  {
320  return $GLOBALS['BE_USER'];
321  }
322 
328  protected function getLanguageService()
329  {
330  return $GLOBALS['LANG'];
331  }
332 }
injectDocumentationService(DocumentationService $documentationService)
static translate($key, $extensionName=null, $arguments=null)
redirect($actionName, $controllerName=null, $extensionName=null, array $arguments=null, $pageUid=null, $delay=0, $statusCode=303)
injectSignalSlotDispatcher(Dispatcher $signalSlotDispatcher)
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
static makeInstance($className,... $constructorArguments)
deleteAction(ServerRequestInterface $request, ResponseInterface $response)
static rmdir($path, $removeNonEmpty=false)
injectDocumentRepository(DocumentRepository $documentRepository)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
addFlashMessage($messageBody, $messageTitle='', $severity=\TYPO3\CMS\Core\Messaging\AbstractMessage::OK, $storeInSession=true)