Completed
Pull Request — master (#397)
by
unknown
02:23
created

CalendarizeInfoElement   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 64
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 19 2
B getEventList() 0 31 7
1
<?php
2
declare(strict_types = 1);
3
namespace HDNET\Calendarize\Form\Element;
4
5
6
use HDNET\Calendarize\Service\IndexerService;
7
use HDNET\Calendarize\Utility\DateTimeUtility;
8
use HDNET\Calendarize\Utility\TranslateUtility;
9
use TYPO3\CMS\Backend\Form\Element\AbstractFormElement;
10
use TYPO3\CMS\Backend\Utility\BackendUtility;
11
use TYPO3\CMS\Core\Utility\DebugUtility;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
14
class CalendarizeInfoElement extends AbstractFormElement
0 ignored issues
show
Bug introduced by
There is one abstract method setLogger in this class; you could implement it, or declare this class as abstract.
Loading history...
15
{
16
17
    public function render()
18
    {
19
        $result = $this->initializeResultArray();
20
21
        $parameters = $this->data['parameterArray']['fieldConf']['config']['parameters'];
22
23
        $previewLimit = 10;
24
        if (isset($parameters['items'])) {
25
            $previewLimit = (int)$parameters['items'];
26
        }
27
28
        $indexService = GeneralUtility::makeInstance(IndexerService::class);
29
        $count = $indexService->getIndexCount($this->data['tableName'], $this->data['vanillaUid']);
30
        $next = $indexService->getNextEvents($this->data['tableName'], $this->data['vanillaUid'], $previewLimit);
31
        $content = \sprintf(TranslateUtility::get('previewLabel'), $count, $previewLimit) . $this->getEventList($next);
32
33
        $result['html'] = $content;
34
        return $result;
35
    }
36
37
38
    /**
39
     * Get event list.
40
     *
41
     * @param $events
42
     *
43
     * @return string
44
     */
45
    protected function getEventList($events)
46
    {
47
        $items = [];
48
        foreach ($events as $event) {
49
            if (!($event['start_date'] instanceof \DateTimeInterface)) {
50
                $event['start_date'] = new \DateTime($event['start_date']);
51
            }
52
            $startDate = \strftime(DateTimeUtility::FORMAT_DATE_BACKEND, (int)$event['start_date']->getTimestamp());
53
54
            if (!($event['end_date'] instanceof \DateTimeInterface)) {
55
                $event['end_date'] = new \DateTime($event['end_date']);
56
            }
57
            $endDate = \strftime(DateTimeUtility::FORMAT_DATE_BACKEND, (int)$event['end_date']->getTimestamp());
58
            $entry = $startDate . ' - ' . $endDate;
59
            if (!$event['all_day']) {
60
                $start = BackendUtility::time($event['start_time'] % DateTimeUtility::SECONDS_DAY, false);
61
                if ((bool)$event['open_end_time']) {
62
                    $end = '"' . TranslateUtility::get('openEndTime') . '"';
63
                } else {
64
                    $end = BackendUtility::time($event['end_time'] % DateTimeUtility::SECONDS_DAY, false);
65
                }
66
                $entry .= ' (' . $start . ' - ' . $end . ')';
67
            }
68
            $items[] = $entry;
69
        }
70
        if (!$items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
71
            $items[] = TranslateUtility::get('noEvents');
72
        }
73
74
        return '<ul><li>' . \implode('</li><li>', $items) . '</li></ul>';
75
    }
76
77
}
78