NoticesController.php 9.38 KB
Newer Older
Dainis Abols's avatar
Dainis Abols committed
1
2
3
4
5
6
7
8
9
10
11
<?php

namespace Lu\LuNotices\Controller;

use Lu\LuNotices\Domain\Model\Notices;
use Lu\LuNotices\Domain\Model\Topics;
use Lu\LuNotices\Domain\Repository\NoticesRepository;
use Lu\LuNotices\Domain\Repository\TopicsRepository;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\DataHandling\DataHandler;
12
use TYPO3\CMS\Core\Resource\FileRepository;
Dainis Abols's avatar
Dainis Abols committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

/**
 * Class NoticesController
 *
 * @owner   University of Latvia
 * @author  Maija Zviedre <maija.zviedre@lu.lv>
 * @author  Dainis Abols <dainis.abols@lu.lv>
 * @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
     */
212
    private function getFilesByNotice($noticeId): array
Dainis Abols's avatar
Dainis Abols committed
213
214
    {
        // Pre-set values
215
216
        $noticesTable   = 'tx_lunotices_domain_model_notices';
        $fileRepository = GeneralUtility::makeInstance(FileRepository::class);
Dainis Abols's avatar
Dainis Abols committed
217

Dainis Abols's avatar
Dainis Abols committed
218
        return $fileRepository->findByRelation($noticesTable, 'files', $noticeId);
Dainis Abols's avatar
Dainis Abols committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
    }

    /**
     *  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;
    }
}