* @author Dainis Abols * @version 3.0.4 * @since 30.07.2018 * * @package Lu\LuNotices\Controller */ class NoticesController extends ActionController { /** * Topics repository * * @var \Lu\LuNotices\Domain\Repository\TopicsRepository */ private $topicsRepository; /** * Notices repository * * @var \Lu\LuNotices\Domain\Repository\NoticesRepository */ private $noticesRepository; /** * Current page * * @var int */ public $currentPage; /** * Inject the notices repository * * @param \Lu\LuNotices\Domain\Repository\NoticesRepository */ public function injectNoticesRepository(NoticesRepository $noticesRepository) { $this->noticesRepository = $noticesRepository; } /** * Inject the topics repository * * @param \Lu\LuNotices\Domain\Repository\TopicsRepository */ public function injectTopicsRepository(TopicsRepository $topicsRepository) { $this->topicsRepository = $topicsRepository; } /** * List Action * See all active topics with optional limited notices width summaries. */ public function listAction() { // Pre-set data $data = []; $allTopics = [0]; $dateFrom = null; $dateTill = null; $UriBuilder = $this->controllerContext->getUriBuilder()->reset()->setCreateAbsoluteUri(true); $user = $GLOBALS['TSFE']->fe_user; $groupBy = $this->settings['GroupBy']; $currentDate = $this->request->hasArgument('currentDate') ? $this->request->getArgument('currentDate') : null; // Fetch topics $topics = $this->topicsRepository->findAll(); $setLimit = (int)$this->settings['NoticesPerTopic']; // Add filter options, if date set if ($groupBy == 'date') { $dateFrom = $dateTill = $currentDate; } // Filter topics for frontend user groups foreach ($topics as $topic) { if (in_array($topic->getFeGroup(), $user->groupData['uid'])) { $allTopics[] = $topicId = $topic->getId(); $data[$topicId]['topic'] = $topic; $data[$topicId]['url'] = $UriBuilder->uriFor('single', ['topic_id' => $topicId, 'currentDate' => $currentDate]); } } // Get dates if ($groupBy == 'date') { $dates = Notices::getNoticeDates($allTopics, $currentDate); // Check if current date is provided if (!$currentDate) { $dateFrom = $dateTill = $currentDate = $dates[0]['date_formatted']; $dates[0]['selected'] = true; } $this->view->assign('dates', $dates); } // Fill topics with notices foreach ($topics as $topic) { if (in_array($topic->getFeGroup(), $user->groupData['uid'])) { $allTopics[] = $topicId = $topic->getId(); $data[$topicId]['topic'] = $topic; $data[$topicId]['url'] = $UriBuilder->uriFor('single', ['topic_id' => $topicId, 'currentDate' => $currentDate]); $topicNotices = $this->noticesRepository->findTopicNotices($topic->getId(), $setLimit, 0, $groupBy, $dateFrom, $dateTill); $data[$topicId]['notices'] = $topicNotices; // Get files for each notice foreach ($data[$topicId]['notices'] as $key => $item) { $files = $this->cleanNoticeFileResponse($this->getFilesByNotice($item->getUid())); if ($files) { $data[$topicId]['notices'][$key]->fileArray = $files; } } } } $this->view->assign('data', $data); } /** * Single Action * See all notices per topic with notices summaries */ public function singleAction() { // Pre-set variables $data = []; $groupBy = $this->settings['GroupBy']; $currentDate = $this->request->hasArgument('currentDate') ? $this->request->getArgument('currentDate') : null; // Fetch request $request = $this->request->getArguments(); $topicId = $this->request->hasArgument('topic_id') ? $this->request->getArgument('topic_id') : 0; // Add filter options, if date set if ($groupBy == 'date') { $dateFrom = $dateTill = $currentDate; } // Fetch topic and its notices $topic = $this->topicsRepository->findByUid($topicId); $getNotices = $this->noticesRepository->findTopicNotices($topicId, 0, 0, $groupBy, $dateFrom, $dateTill)->count(); // NoticesPerTopicSingle - set Notices per page for TopicSingeView $setLimit = (int)$this->settings['NoticesPerTopicSingle']; $currentPage = isset($request['@widget_0']['currentPage']) ? $request['@widget_0']['currentPage'] * $setLimit - 2 : 0; $topicNotices = $this->noticesRepository->findTopicNotices($topic->getId(), $setLimit, $currentPage, $groupBy, $dateFrom, $dateTill); // Set data values $data['range'] = range(1, $getNotices); $data['itemsPerPage'] = $setLimit; $data['maximumNumberOfLinks'] = 10; // Don't show pagination if only one page if (count($data['range']) < $data['itemsPerPage']) { $data['hidePagination'] = true; } $this->view->assign('data', $data); $this->view->assign('language', $this->language); $this->view->assign('notices', $topicNotices); $this->view->assign('topic', $topic); } /** * Details Action * See content details * * @return void * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException */ public function detailsAction() { // Fetch request $noticeId = $this->request->hasArgument('uid') ? $this->request->getArgument('uid') : 0; $notice = $this->noticesRepository->findByUid($noticeId); $files = $this->getFilesByNotice($noticeId); $this->view->assign('topic', $this->topicsRepository->findByUid($notice->getTopicId())); $this->view->assign('language', $this->language); $this->view->assign('notice', $notice); $this->view->assign('files', $files); } /** * Retrieve files by notice ID * * @param $noticeId * * @return array */ private function getFilesByNotice($noticeId): array { // Pre-set values $noticesTable = 'tx_lunotices_domain_model_notices'; $fileRepository = GeneralUtility::makeInstance(FileRepository::class); return $fileRepository->findByRelation($noticesTable, 'files', $noticeId); } /** * Strips excess info about files * * @param array $files * * @return array */ private function cleanNoticeFileResponse(array $files): array { $data = []; foreach ($files as $item) { $storageConf = $item->getStorage()->getConfiguration(); $data[] = [ 'name' => end(explode("/", $item->getIdentifier())), 'path' => $storageConf['basePath'].$item->getIdentifier(), 'type' => substr($item->getExtension(), 0, 3), // Use only first 3 letters (docx, xlsx hack while no other icons available, might change in v8)! ]; } return $data; } /** * Retrieves user category mounts * * @param $fields * * @return array */ public function renderTopicFields(&$fields) { $row = $fields['row']; $pid = $row['pid']; $dataHandler = new DataHandler(); $resolvedPid = $dataHandler->resolvePid('tx_lunotices_domain_model_notices', $pid); $userMount = self::getBeUserCatMounts($resolvedPid); $fields['items'] = Topics::getAllowedTopics($userMount, $resolvedPid); return $fields; } /** * @param int $pid * * @return array */ public function getBeUserCatMounts($pid = 0) { global $BE_USER; $catMounts = []; if ($BE_USER->user['admin']) { $catMounts = Topics::getTopicIds($pid); } else { if (is_array($BE_USER->userGroups)) { foreach ($BE_USER->userGroups as $group) { if ($group['lu_notices_topicmounts']) { $catMounts[] = $group['lu_notices_topicmounts']; } } } if ($BE_USER->user['lu_notices_topicmounts']) { $catMounts[] = $BE_USER->user['lu_notices_topicmounts']; } } return $catMounts; } }