xref: /webtrees/app/Http/RequestHandlers/CalendarEvents.php (revision 54f9ed18c67402425839d8ad7a6ffd425b6361b8)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Date;
23use Fisharebest\Webtrees\Date\FrenchDate;
24use Fisharebest\Webtrees\Date\GregorianDate;
25use Fisharebest\Webtrees\Date\HijriDate;
26use Fisharebest\Webtrees\Date\JalaliDate;
27use Fisharebest\Webtrees\Date\JewishDate;
28use Fisharebest\Webtrees\Date\JulianDate;
29use Fisharebest\Webtrees\Fact;
30use Fisharebest\Webtrees\Family;
31use Fisharebest\Webtrees\I18N;
32use Fisharebest\Webtrees\Individual;
33use Fisharebest\Webtrees\Registry;
34use Fisharebest\Webtrees\Services\CalendarService;
35use Fisharebest\Webtrees\Tree;
36use Fisharebest\Webtrees\Validator;
37use Illuminate\Support\Collection;
38use Psr\Http\Message\ResponseInterface;
39use Psr\Http\Message\ServerRequestInterface;
40use Psr\Http\Server\RequestHandlerInterface;
41
42use function count;
43use function e;
44use function explode;
45use function get_class;
46use function ob_get_clean;
47use function ob_start;
48use function range;
49use function response;
50use function view;
51
52/**
53 * Show anniversaries for events in a given day/month/year.
54 */
55class CalendarEvents implements RequestHandlerInterface
56{
57    private CalendarService $calendar_service;
58
59    /**
60     * @param CalendarService $calendar_service
61     */
62    public function __construct(CalendarService $calendar_service)
63    {
64        $this->calendar_service = $calendar_service;
65    }
66
67    /**
68     * Show anniversaries that occurred on a given day/month/year.
69     *
70     * @param ServerRequestInterface $request
71     *
72     * @return ResponseInterface
73     */
74    public function handle(ServerRequestInterface $request): ResponseInterface
75    {
76        $tree     = Validator::attributes($request)->tree();
77        $view     = Validator::attributes($request)->isInArray(['day', 'month', 'year'])->string('view');
78        $cal      = Validator::queryParams($request)->string('cal');
79        $day      = Validator::queryParams($request)->string('day');
80        $month    = Validator::queryParams($request)->string('month');
81        $year     = Validator::queryParams($request)->string('year');
82        $filterev = Validator::queryParams($request)->string('filterev');
83        $filterof = Validator::queryParams($request)->string('filterof');
84        $filtersx = Validator::queryParams($request)->string('filtersx');
85
86        $ged_date = new Date($cal . ' ' . $day . ' ' . $month . ' ' . $year);
87        $cal_date = $ged_date->minimumDate();
88        $today    = $cal_date->today();
89
90        $days_in_month = $cal_date->daysInMonth();
91        $days_in_week  = $cal_date->daysInWeek();
92
93        $CALENDAR_FORMAT = $tree->getPreference('CALENDAR_FORMAT');
94
95        // Day and year share the same layout.
96        if ($view !== 'month') {
97            if ($view === 'day') {
98                $anniversary_facts = $this->calendar_service->getAnniversaryEvents($cal_date->minimumJulianDay(), $filterev, $tree, $filterof, $filtersx);
99            } else {
100                $ged_year          = new Date($cal . ' ' . $year);
101                $anniversary_facts = $this->calendar_service->getCalendarEvents($ged_year->minimumJulianDay(), $ged_year->maximumJulianDay(), $filterev, $tree, $filterof, $filtersx);
102            }
103
104            $anniversaries = Collection::make($anniversary_facts)
105                ->unique()
106                ->sort(static fn (Fact $x, Fact $y): int => $x->date()->minimumJulianDay() <=> $y->date()->minimumJulianDay());
107
108            $family_anniversaries = $anniversaries->filter(static fn (Fact $f): bool => $f->record() instanceof Family);
109
110            $individual_anniversaries = $anniversaries->filter(static fn (Fact $f): bool => $f->record() instanceof Individual);
111
112            return response(view('calendar-list', [
113                'family_anniversaries'     => $family_anniversaries,
114                'individual_anniversaries' => $individual_anniversaries,
115            ]));
116        }
117
118        $found_facts = [];
119
120        $cal_date->day = 0;
121        $cal_date->setJdFromYmd();
122        // Make a separate list for each day. Unspecified/invalid days go in day 0.
123        for ($d = 0; $d <= $days_in_month; ++$d) {
124            $found_facts[$d] = [];
125        }
126        // Fetch events for each day
127        $jds = range($cal_date->minimumJulianDay(), $cal_date->maximumJulianDay());
128
129        foreach ($jds as $jd) {
130            foreach ($this->calendar_service->getAnniversaryEvents($jd, $filterev, $tree, $filterof, $filtersx) as $fact) {
131                $tmp = $fact->date()->minimumDate();
132                if ($tmp->day >= 1 && $tmp->day <= $tmp->daysInMonth()) {
133                    // If the day is valid (for its own calendar), display it in the
134                    // anniversary day (for the display calendar).
135                    $found_facts[$jd - $cal_date->minimumJulianDay() + 1][] = $fact;
136                } else {
137                    // Otherwise, display it in the "Day not set" box.
138                    $found_facts[0][] = $fact;
139                }
140            }
141        }
142
143        $cal_facts = [];
144
145        foreach ($found_facts as $d => $facts) {
146            $cal_facts[$d] = [];
147            foreach ($facts as $fact) {
148                $xref = $fact->record()->xref();
149                $text = $fact->label() . ' — ' . $fact->date()->display($tree);
150                if ($fact->anniv > 0) {
151                    $text .= ' (' . I18N::translate('%s year anniversary', I18N::number($fact->anniv)) . ')';
152                }
153                if (empty($cal_facts[$d][$xref])) {
154                    $cal_facts[$d][$xref] = $text;
155                } else {
156                    $cal_facts[$d][$xref] .= '<br>' . $text;
157                }
158            }
159        }
160        // We use JD%7 = 0/Mon…6/Sun. Standard definitions use 0/Sun…6/Sat.
161        $week_start    = (I18N::locale()->territory()->firstDay() + 6) % 7;
162        $weekend_start = (I18N::locale()->territory()->weekendStart() + 6) % 7;
163        $weekend_end   = (I18N::locale()->territory()->weekendEnd() + 6) % 7;
164
165        // The French calendar has a 10-day week, which starts on primidi.
166        if ($days_in_week === 10) {
167            $week_start    = 0;
168            $weekend_start = -1;
169            $weekend_end   = -1;
170        }
171
172        ob_start();
173
174        echo '<table class="w-100 wt-calendar-month"><thead><tr>';
175        for ($week_day = 0; $week_day < $days_in_week; ++$week_day) {
176            $day_name = $cal_date->dayNames(($week_day + $week_start) % $days_in_week);
177            if ($week_day === $weekend_start || $week_day === $weekend_end) {
178                echo '<th class="wt-page-options-label weekend" width="', 100 / $days_in_week, '%">', $day_name, '</th>';
179            } else {
180                echo '<th class="wt-page-options-label" width="', 100 / $days_in_week, '%">', $day_name, '</th>';
181            }
182        }
183        echo '</tr>';
184        echo '</thead>';
185        echo '<tbody>';
186        // Print days 1 to n of the month, but extend to cover "empty" days before/after the month to make whole weeks.
187        // e.g. instead of 1 -> 30 (=30 days), we might have -1 -> 33 (=35 days)
188        $start_d = 1 - ($cal_date->minimumJulianDay() - $week_start) % $days_in_week;
189        $end_d   = $days_in_month + ($days_in_week - ($cal_date->maximumJulianDay() - $week_start + 1) % $days_in_week) % $days_in_week;
190        // Make sure that there is an empty box for any leap/missing days
191        if ($start_d === 1 && $end_d === $days_in_month && count($found_facts[0]) > 0) {
192            $end_d += $days_in_week;
193        }
194        for ($d = $start_d; $d <= $end_d; ++$d) {
195            if (($d + $cal_date->minimumJulianDay() - $week_start) % $days_in_week === 1) {
196                echo '<tr>';
197            }
198            echo '<td class="wt-page-options-value">';
199            if ($d < 1 || $d > $days_in_month) {
200                if (count($cal_facts[0]) > 0) {
201                    echo '<div class="cal_day">', I18N::translate('Day not set'), '</div>';
202                    echo '<div class="small" style="height: 180px; overflow: auto;">';
203                    echo $this->calendarListText($cal_facts[0], $tree);
204                    echo '</div>';
205                    $cal_facts[0] = [];
206                }
207            } else {
208                // Format the day number using the calendar
209                $tmp   = new Date($cal_date->format('%@ ' . $d . ' %O %E'));
210                $d_fmt = $tmp->minimumDate()->format('%j');
211                echo '<div class="d-flex d-flex justify-content-between">';
212                if ($d === $today->day && $cal_date->month === $today->month) {
213                    echo '<span class="cal_day current_day">', $d_fmt, '</span>';
214                } else {
215                    echo '<span class="cal_day">', $d_fmt, '</span>';
216                }
217                // Show a converted date
218                foreach (explode('_and_', $CALENDAR_FORMAT) as $convcal) {
219                    switch ($convcal) {
220                        case 'french':
221                            $alt_date = new FrenchDate($cal_date->minimumJulianDay() + $d - 1);
222                            break;
223                        case 'gregorian':
224                            $alt_date = new GregorianDate($cal_date->minimumJulianDay() + $d - 1);
225                            break;
226                        case 'jewish':
227                            $alt_date = new JewishDate($cal_date->minimumJulianDay() + $d - 1);
228                            break;
229                        case 'julian':
230                            $alt_date = new JulianDate($cal_date->minimumJulianDay() + $d - 1);
231                            break;
232                        case 'hijri':
233                            $alt_date = new HijriDate($cal_date->minimumJulianDay() + $d - 1);
234                            break;
235                        case 'jalali':
236                            $alt_date = new JalaliDate($cal_date->minimumJulianDay() + $d - 1);
237                            break;
238                        case 'none':
239                        default:
240                            $alt_date = $cal_date;
241                            break;
242                    }
243                    if (get_class($alt_date) !== get_class($cal_date) && $alt_date->inValidRange()) {
244                        echo '<span class="rtl_cal_day">' . $alt_date->format('%j %M') . '</span>';
245                        // Just show the first conversion
246                        break;
247                    }
248                }
249                echo '</div>';
250                echo '<div class="small" style="height: 180px; overflow: auto;">';
251                echo $this->calendarListText($cal_facts[$d], $tree);
252                echo '</div>';
253            }
254            echo '</td>';
255            if (($d + $cal_date->minimumJulianDay() - $week_start) % $days_in_week === 0) {
256                echo '</tr>';
257            }
258        }
259        echo '</tbody>';
260        echo '</table>';
261
262        return response(ob_get_clean());
263    }
264
265    /**
266     * Format a list of facts for display
267     *
268     * @param array<string> $list
269     * @param Tree          $tree
270     *
271     * @return string
272     */
273    private function calendarListText(array $list, Tree $tree): string
274    {
275        $html = '';
276
277        foreach ($list as $xref => $facts) {
278            $tmp = Registry::gedcomRecordFactory()->make((string) $xref, $tree);
279            $html .= '<a href="' . e($tmp->url()) . '">' . $tmp->fullName() . '</a> ';
280            $html .= '<div class="indent">' . $facts . '</div>';
281        }
282
283        return $html;
284    }
285}
286