Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 137
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
OlzTermineUpcomingTile
0.00% covered (danger)
0.00%
0 / 137
0.00% covered (danger)
0.00%
0 / 7
210
0.00% covered (danger)
0.00%
0 / 1
 getRelevance
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHtml
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 1
30
 renderProgramList
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
20
 renderWeekendsList
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
2
 renderTrophyList
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
2
 renderUpcomingTrainingsList
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
 getNumberOfEntries
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3// =============================================================================
4// Zeigt eine Startseiten-Kachel mit den nächsten Terminen an.
5// =============================================================================
6
7namespace Olz\Startseite\Components\OlzTermineUpcomingTile;
8
9use Olz\Entity\Termine\TerminLabel;
10use Olz\Entity\Users\User;
11use Olz\Startseite\Components\AbstractOlzTile\AbstractOlzTile;
12
13class OlzTermineUpcomingTile extends AbstractOlzTile {
14    private ?\mysqli $db = null;
15    private ?int $this_year = null;
16
17    public function getRelevance(?User $user): float {
18        return 0.7;
19    }
20
21    public function getHtml(mixed $args): string {
22        $this->termineUtils()->loadTypeOptions();
23        $this->db = $this->dbUtils()->getDb();
24        $this->this_year = intval($this->dateUtils()->getCurrentDateInFormat('Y'));
25        $code_href = $this->envUtils()->getCodeHref();
26        $code_path = $this->envUtils()->getCodePath();
27        $today = $this->dateUtils()->getIsoToday();
28        $termin_label_repo = $this->entityManager()->getRepository(TerminLabel::class);
29
30        $termine_url = $this->termineUtils()->getUrl(['typ' => 'alle', 'datum' => 'bevorstehend']);
31
32        $out = <<<ZZZZZZZZZZ
33            <h3>
34                <a href='{$termine_url}&von=st-tr' class='header-link'>
35                    <img
36                        src='{$code_href}assets/icns/termine_type_all_20.svg'
37                        alt='Termine'
38                        class='header-link-icon text-icon'
39                    >
40                    Termine
41                </a>
42            </h3>
43            ZZZZZZZZZZ;
44
45        $out .= "<div class='filters'>".implode(' ', [
46            $this->renderProgramList(),
47            $this->renderWeekendsList(),
48            $this->renderTrophyList(),
49            $this->renderUpcomingTrainingsList(),
50        ])."</div>";
51
52        $out .= "<ul class='links'>";
53        $res = $this->db->query(<<<ZZZZZZZZZZ
54            SELECT
55                t.id,
56                t.start_date as date,
57                t.title as title,
58                (
59                    SELECT l.id
60                    FROM 
61                        termin_label_map tl
62                        JOIN termin_labels l ON (l.id = tl.label_id)
63                    WHERE tl.termin_id = t.id
64                    ORDER BY l.position ASC
65                    LIMIT 1
66                ) as label_id
67            FROM termine t
68            WHERE
69                t.deleted_at IS NULL
70                AND t.parent_termin_id IS NULL
71                AND t.start_date >= '{$today}'
72            ORDER BY t.start_date ASC
73            LIMIT 7
74            ZZZZZZZZZZ);
75        $index = 0;
76        // @phpstan-ignore-next-line
77        while ($row = $res->fetch_assoc()) {
78            $id = $row['id'];
79            // @phpstan-ignore-next-line
80            $date = $this->dateUtils()->compactDate($row['date']);
81            $title = $row['title'];
82            $label_id = $row['label_id'];
83            $label = $termin_label_repo->findOneBy(['id' => $label_id]);
84            $label_ident = $label?->getIdent();
85            $fallback_path = "{$code_path}assets/icns/termine_type_{$label_ident}_20.svg";
86            $fallback_href = is_file($fallback_path)
87                ? "{$code_href}assets/icns/termine_type_{$label_ident}_20.svg" : null;
88            $icon_href = $label?->getIcon() ? $label->getFileHref($label->getIcon()) : $fallback_href;
89            $icon_img = $icon_href ? "<img src='{$icon_href}' alt='' class='link-icon text-icon'>" : '';
90            $out .= <<<ZZZZZZZZZZ
91                    <li class='flex'>
92                        <a href='{$code_href}termine/{$id}?von=st-tr-{$index}'>
93                            {$icon_img}
94                            <b>{$date}</b>: {$title}
95                        </a>
96                    </li>
97                ZZZZZZZZZZ;
98            $index++;
99        }
100        $out .= "</ul>";
101
102        return $out;
103    }
104
105    protected function renderProgramList(): string {
106        $code_href = $this->envUtils()->getCodeHref();
107        $icon = "{$code_href}assets/icns/termine_type_programm_20.svg";
108        $icon_img = "<img src='{$icon}' alt='' class='link-icon'>";
109        $this_year = $this->this_year;
110        $next_year = $this->this_year + 1;
111        $imminent_filter = [
112            ...$this->termineUtils()->getDefaultFilter(),
113            'typ' => 'programm',
114            'datum' => 'bevorstehend',
115        ];
116        $this_year_filter = [
117            ...$this->termineUtils()->getDefaultFilter(),
118            'typ' => 'programm',
119            'datum' => strval($this_year),
120        ];
121        $num_imminent = $this->getNumberOfEntries($imminent_filter);
122        $out = '';
123        if ($num_imminent > 0) {
124            $num_this_year = $this->getNumberOfEntries($this_year_filter);
125            $serialized_filter = $this->termineUtils()->serialize($this_year_filter);
126            $out .= <<<ZZZZZZZZZZ
127                <a href='{$code_href}termine?filter={$serialized_filter}&von=st-tr' class='filter'>
128                    {$icon_img} Programm {$this_year}<span class='secondary'>({$num_this_year})</span>
129                </a>
130                ZZZZZZZZZZ;
131        }
132        $current_month = intval($this->dateUtils()->getCurrentDateInFormat('m'));
133        if ($current_month > 8) {
134            $next_year_filter = [
135                ...$this->termineUtils()->getDefaultFilter(),
136                'typ' => 'programm',
137                'datum' => strval($next_year),
138            ];
139            $num_next_year = $this->getNumberOfEntries($next_year_filter);
140
141            if ($num_next_year > 0) {
142                $serialized_filter = $this->termineUtils()->serialize($next_year_filter);
143                $out .= <<<ZZZZZZZZZZ
144                    <a href='{$code_href}termine?filter={$serialized_filter}&von=st-tr' class='filter'>
145                        {$icon_img} Programm {$next_year}<span class='secondary'>({$num_next_year})</span>
146                    </a>
147                    ZZZZZZZZZZ;
148            }
149        }
150        return $out;
151    }
152
153    protected function renderWeekendsList(): string {
154        $code_href = $this->envUtils()->getCodeHref();
155        $icon = "{$code_href}assets/icns/termine_type_weekend_20.svg";
156        $icon_img = "<img src='{$icon}' alt='' class='link-icon'>";
157        $imminent_filter = [
158            ...$this->termineUtils()->getDefaultFilter(),
159            'typ' => 'weekend',
160            'datum' => 'bevorstehend',
161        ];
162        $num_imminent = $this->getNumberOfEntries($imminent_filter);
163        $serialized_filter = $this->termineUtils()->serialize($imminent_filter);
164        return <<<ZZZZZZZZZZ
165            <a href='{$code_href}termine?filter={$serialized_filter}&von=st-tr' class='filter'>
166                {$icon_img} Weekends<span class='secondary'>({$num_imminent})</span>
167            </a>
168            ZZZZZZZZZZ;
169    }
170
171    protected function renderTrophyList(): string {
172        $code_href = $this->envUtils()->getCodeHref();
173        $icon = "{$code_href}assets/icns/termine_type_trophy_20.svg";
174        $icon_img = "<img src='{$icon}' alt='' class='link-icon'>";
175        $this_year = $this->this_year;
176        $this_year_filter = [
177            ...$this->termineUtils()->getDefaultFilter(),
178            'typ' => 'trophy',
179            'datum' => strval($this_year),
180        ];
181        $num_this_year = $this->getNumberOfEntries($this_year_filter);
182        $serialized_filter = $this->termineUtils()->serialize($this_year_filter);
183        return <<<ZZZZZZZZZZ
184            <a href='{$code_href}termine?filter={$serialized_filter}&von=st-tr' class='filter'>
185                {$icon_img} OLZ Trophy<span class='secondary'>({$num_this_year})</span>
186            </a>
187            ZZZZZZZZZZ;
188    }
189
190    protected function renderUpcomingTrainingsList(): string {
191        $code_href = $this->envUtils()->getCodeHref();
192        $icon = "{$code_href}assets/icns/termine_type_training_20.svg";
193        $icon_img = "<img src='{$icon}' alt='' class='link-icon'>";
194        $imminent_filter = [
195            ...$this->termineUtils()->getDefaultFilter(),
196            'typ' => 'training',
197            'datum' => 'bevorstehend',
198        ];
199        $serialized_filter = $this->termineUtils()->serialize($imminent_filter);
200        return <<<ZZZZZZZZZZ
201            <a href='{$code_href}termine?filter={$serialized_filter}&von=st-tr' class='filter'>
202                {$icon_img} Trainings
203            </a>
204            ZZZZZZZZZZ;
205    }
206
207    /** @param array{typ?: string, datum?: string, archiv?: string} $filter */
208    protected function getNumberOfEntries(array $filter): int {
209        $date_filter = $this->termineUtils()->getSqlDateRangeFilter($filter, 'c');
210        $type_filter = $this->termineUtils()->getSqlTypeFilter($filter, 'c');
211        $filter_sql = "({$date_filter}) AND ({$type_filter})";
212        $sql = <<<ZZZZZZZZZZ
213            SELECT * 
214            FROM (
215                SELECT
216                    t.id AS id,
217                    t.start_date AS start_date,
218                    t.end_date AS end_date,
219                    (
220                        SELECT GROUP_CONCAT(l.ident ORDER BY l.position ASC SEPARATOR ' ')
221                        FROM
222                            termin_label_map tl
223                            JOIN termin_labels l ON (l.id = tl.label_id)
224                        WHERE tl.termin_id = t.id
225                        GROUP BY t.id
226                    ) as typ
227                FROM termine t
228            ) AS c
229            WHERE {$filter_sql}
230            ZZZZZZZZZZ;
231        $res = $this->db?->query($sql);
232        // @phpstan-ignore-next-line
233        return $res->num_rows;
234    }
235}