Completed
Push — master ( 6e5577...d893d2 )
by Tim
02:49
created

CalendarController::detailAction()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.439
c 0
b 0
f 0
cc 6
eloc 15
nc 10
nop 1
1
<?php
2
/**
3
 * Calendar
4
 *
5
 * @author  Tim Lochmüller
6
 */
7
8
namespace HDNET\Calendarize\Controller;
9
10
use HDNET\Calendarize\Domain\Model\Index;
11
use HDNET\Calendarize\Register;
12
use HDNET\Calendarize\Utility\DateTimeUtility;
13
use HDNET\Calendarize\Utility\TranslateUtility;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
use TYPO3\CMS\Core\Utility\MathUtility;
16
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
17
use TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter;
18
19
/**
20
 * Calendar
21
 */
22
class CalendarController extends AbstractController
23
{
24
    /**
25
     * Init all actions
26
     */
27
    public function initializeAction()
28
    {
29
        parent::initializeAction();
30
        $this->indexRepository->setIndexTypes(GeneralUtility::trimExplode(',', $this->settings['configuration']));
31
        $this->indexRepository->setContentRecord($this->configurationManager->getContentObject()->data);
32
        if (isset($this->settings['sorting'])) {
33
            $this->indexRepository->setDefaultSortingDirection($this->settings['sorting']);
34
        }
35
36
        if (isset($this->arguments['startDate'])) {
37
            $this->arguments['startDate']->getPropertyMappingConfiguration()
38
                ->setTypeConverterOption(
39
                    DateTimeConverter::class,
40
                    DateTimeConverter::CONFIGURATION_DATE_FORMAT,
41
                    $this->settings['dateFormat']
42
                );
43
        }
44
        if (isset($this->arguments['endDate'])) {
45
            $this->arguments['endDate']->getPropertyMappingConfiguration()
46
                ->setTypeConverterOption(
47
                    DateTimeConverter::class,
48
                    DateTimeConverter::CONFIGURATION_DATE_FORMAT,
49
                    $this->settings['dateFormat']
50
                );
51
        }
52
    }
53
54
    /**
55
     * Latest action
56
     *
57
     * @param \HDNET\Calendarize\Domain\Model\Index $index
58
     * @param \DateTime $startDate
59
     * @param \DateTime $endDate
60
     * @param array $customSearch *
61
     * @param int $year
62
     * @param int $month
63
     * @param int $week
64
     *
65
     * @ignorevalidation $startDate
66
     * @ignorevalidation $endDate
67
     * @ignorevalidation $customSearch
68
     *
69
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
70
     */
71
    public function latestAction(
72
        Index $index = null,
73
        \DateTime $startDate = null,
74
        \DateTime $endDate = null,
75
        array $customSearch = [],
76
        $year = null,
77
        $month = null,
78
        $week = null
79
    ) {
80
        $this->listAction($index, $startDate, $endDate, $customSearch, $year, $month, $week);
81
    }
82
83
    /**
84
     * Result action
85
     *
86
     * @param \HDNET\Calendarize\Domain\Model\Index $index
87
     * @param \DateTime $startDate
88
     * @param \DateTime $endDate
89
     * @param array $customSearch *
90
     * @param int $year
91
     * @param int $month
92
     * @param int $week
93
     *
94
     * @ignorevalidation $startDate
95
     * @ignorevalidation $endDate
96
     * @ignorevalidation $customSearch
97
     *
98
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
99
     */
100
    public function resultAction(
101
        Index $index = null,
102
        \DateTime $startDate = null,
103
        \DateTime $endDate = null,
104
        array $customSearch = [],
105
        $year = null,
106
        $month = null,
107
        $week = null
108
    ) {
109
        $this->listAction($index, $startDate, $endDate, $customSearch, $year, $month, $week);
110
    }
111
112
    /**
113
     * List action
114
     *
115
     * @param \HDNET\Calendarize\Domain\Model\Index $index
116
     * @param \DateTime $startDate
117
     * @param \DateTime $endDate
118
     * @param array $customSearch *
119
     * @param int $year
120
     * @param int $month
121
     * @param int $day
122
     * @param int $week
123
     *
124
     * @ignorevalidation $startDate
125
     * @ignorevalidation $endDate
126
     * @ignorevalidation $customSearch
127
     *
128
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
129
     */
130
    public function listAction(
131
        Index $index = null,
132
        \DateTime $startDate = null,
133
        \DateTime $endDate = null,
134
        array $customSearch = [],
135
        $year = null,
136
        $month = null,
137
        $day = null,
138
        $week = null
139
    ) {
140
        $this->checkStaticTemplateIsIncluded();
141
        if (($index instanceof Index) && in_array('detail', $this->getAllowedActions())) {
142
            $this->forward('detail');
143
        }
144
145
        $searchMode = false;
146
        if ($startDate || $endDate || !empty($customSearch)) {
147
            $searchMode = true;
148
            $indices = $this->indexRepository->findBySearch($startDate, $endDate, $customSearch);
149
        } elseif (MathUtility::canBeInterpretedAsInteger($year) && MathUtility::canBeInterpretedAsInteger($month) && MathUtility::canBeInterpretedAsInteger($day)) {
150
            $indices = $this->indexRepository->findDay($year, $month, $day);
151
        } elseif (MathUtility::canBeInterpretedAsInteger($year) && MathUtility::canBeInterpretedAsInteger($month)) {
152
            $indices = $this->indexRepository->findMonth($year, $month);
153
        } elseif (MathUtility::canBeInterpretedAsInteger($year) && MathUtility::canBeInterpretedAsInteger($week)) {
154
            $indices = $this->indexRepository->findWeek($year, $week);
155
        } elseif (MathUtility::canBeInterpretedAsInteger($year)) {
156
            $indices = $this->indexRepository->findYear($year);
157
        } else {
158
            $overrideStartDate = (int)$this->settings['overrideStartdate'];
159
            $overrideEndDate = (int)$this->settings['overrideEnddate'];
160
            $indices = $this->indexRepository->findList(
161
                (int)$this->settings['limit'],
162
                $this->settings['listStartTime'],
163
                (int)$this->settings['listStartTimeOffsetHours'],
164
                $overrideStartDate,
165
                $overrideEndDate
166
            );
167
        }
168
169
        $this->slotExtendedAssignMultiple([
170
            'indices' => $indices,
171
            'searchMode' => $searchMode
172
        ], __CLASS__, __FUNCTION__);
173
    }
174
175
    /**
176
     * Year action
177
     *
178
     * @param int $year
179
     *
180
     * @return void
181
     */
182
    public function yearAction($year = null)
183
    {
184
        $date = DateTimeUtility::normalizeDateTime(1, 1, $year);
185
186
        $this->slotExtendedAssignMultiple([
187
            'indices' => $this->indexRepository->findYear($date->format('Y')),
188
            'date' => $date
189
        ], __CLASS__, __FUNCTION__);
190
    }
191
192
    /**
193
     * Month action
194
     *
195
     * @param int $year
196
     * @param int $month
197
     * @param int $day
198
     *
199
     * @return void
200
     */
201
    public function monthAction($year = null, $month = null, $day = null)
202
    {
203
        $date = DateTimeUtility::normalizeDateTime($day, $month, $year);
204
205
        $this->slotExtendedAssignMultiple([
206
            'date' => $date,
207
            'indices' => $this->indexRepository->findMonth($date->format('Y'), $date->format('n')),
208
        ], __CLASS__, __FUNCTION__);
209
    }
210
211
    /**
212
     * Week action
213
     *
214
     * @param int $year
215
     * @param int $week
216
     *
217
     * @return void
218
     */
219
    public function weekAction($year = null, $week = null)
220
    {
221
        $now = DateTimeUtility::getNow();
222
        if ($year === null) {
223
            $year = $now->format('Y');
224
        }
225
        if ($week === null) {
226
            $week = $now->format('W');
227
        }
228
        $firstDay = DateTimeUtility::convertWeekYear2DayMonthYear($week, $year);
229
        $firstDay->setTime(0, 0, 0);
230
231
        $weekConfiguration = [
232
            '+0 day' => 2,
233
            '+1 days' => 2,
234
            '+2 days' => 2,
235
            '+3 days' => 2,
236
            '+4 days' => 2,
237
            '+5 days' => 1,
238
            '+6 days' => 1
239
        ];
240
241
        $this->slotExtendedAssignMultiple([
242
            'firstDay' => $firstDay,
243
            'indices' => $this->indexRepository->findWeek($year, $week),
244
            'weekConfiguration' => $weekConfiguration,
245
        ], __CLASS__, __FUNCTION__);
246
    }
247
248
    /**
249
     * Day action
250
     *
251
     * @param int $year
252
     * @param int $month
253
     * @param int $day
254
     *
255
     * @return void
256
     */
257
    public function dayAction($year = null, $month = null, $day = null)
258
    {
259
        $date = DateTimeUtility::normalizeDateTime($day, $month, $year);
260
        $date->modify('+12 hours');
261
262
        $previous = clone $date;
263
        $previous->modify('-1 day');
264
265
        $next = clone $date;
266
        $next->modify('+1 day');
267
268
        $this->slotExtendedAssignMultiple([
269
            'indices' => $this->indexRepository->findDay($date->format('Y'), $date->format('n'), $date->format('j')),
270
            'today' => $date,
271
            'previous' => $previous,
272
            'next' => $next,
273
        ], __CLASS__, __FUNCTION__);
274
    }
275
276
    /**
277
     * Detail action
278
     *
279
     * @param \HDNET\Calendarize\Domain\Model\Index $index
280
     *
281
     * @return string
282
     */
283
    public function detailAction(Index $index = null)
284
    {
285
        if ($index === null) {
286
            // handle fallback for "strange language settings"
287
            if ($this->request->hasArgument('index')) {
288
                $indexId = (int)$this->request->getArgument('index');
289
                if ($indexId > 0) {
290
                    $index = $this->indexRepository->findByUid($indexId);
291
                }
292
            }
293
294
            if ($index === null) {
295
                if (!MathUtility::canBeInterpretedAsInteger($this->settings['listPid'])) {
296
                    return TranslateUtility::get('noEventDetailView');
297
                }
298
                $this->redirect('list', null, null, [], null, $this->settings['listPid'], 301);
299
            }
300
        }
301
302
        $this->slotExtendedAssignMultiple([
303
            'index' => $index,
304
            'domain' => GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY')
305
        ], __CLASS__, __FUNCTION__);
306
307
        return $this->view->render();
308
    }
309
310
    /**
311
     * Render the search view
312
     *
313
     * @param \DateTime $startDate
314
     * @param \DateTime $endDate
315
     * @param array $customSearch
316
     *
317
     * @ignorevalidation $startDate
318
     * @ignorevalidation $endDate
319
     * @ignorevalidation $customSearch
320
     */
321
    public function searchAction(\DateTime $startDate = null, \DateTime $endDate = null, array $customSearch = [])
322
    {
323
        $baseDate = DateTimeUtility::getNow();
324
        if (!($startDate instanceof \DateTime)) {
325
            $startDate = clone $baseDate;
326
        }
327
        if (!($endDate instanceof \DateTime)) {
328
            $baseDate->modify('+1 month');
329
            $endDate = $baseDate;
330
        }
331
332
        $this->slotExtendedAssignMultiple([
333
            'startDate' => $startDate,
334
            'endDate' => $endDate,
335
            'customSearch' => $customSearch,
336
            'configurations' => $this->getCurrentConfigurations()
337
        ], __CLASS__, __FUNCTION__);
338
    }
339
340
    /**
341
     * Get the allowed actions
342
     *
343
     * @return array
344
     */
345
    protected function getAllowedActions()
346
    {
347
        $configuration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
348
        $allowedActions = [];
349
        foreach ($configuration['controllerConfiguration'] as $controllerName => $controllerActions) {
350
            $allowedActions[$controllerName] = $controllerActions['actions'];
351
        }
352
        return isset($allowedActions['Calendar']) ? $allowedActions['Calendar'] : [];
353
    }
354
355
    /**
356
     * Get the current configurations
357
     *
358
     * @return array
359
     */
360
    protected function getCurrentConfigurations()
361
    {
362
        $configurations = GeneralUtility::trimExplode(',', $this->settings['configuration'], true);
363
        $return = [];
364
        foreach (Register::getRegister() as $key => $configuration) {
365
            if (in_array($key, $configurations)) {
366
                $return[] = $configuration;
367
            }
368
        }
369
        return $return;
370
    }
371
}
372