xref: /webtrees/app/Http/RequestHandlers/CalendarPage.php (revision 7dcc0dd1f98a779ae8ed3c9c0b53d14ead31a778)
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\Http\ViewResponseTrait;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Services\CalendarService;
26use Fisharebest\Webtrees\Validator;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
29use Psr\Http\Server\RequestHandlerInterface;
30
31/**
32 * Show anniversaries for events in a given day/month/year.
33 */
34class CalendarPage implements RequestHandlerInterface
35{
36    use ViewResponseTrait;
37
38    private CalendarService $calendar_service;
39
40    /**
41     * @param CalendarService $calendar_service
42     */
43    public function __construct(CalendarService $calendar_service)
44    {
45        $this->calendar_service = $calendar_service;
46    }
47
48    /**
49     * A form to request the page parameters.
50     *
51     * @param ServerRequestInterface $request
52     *
53     * @return ResponseInterface
54     */
55    public function handle(ServerRequestInterface $request): ResponseInterface
56    {
57        $tree     = Validator::attributes($request)->tree();
58        $view     = Validator::attributes($request)->isInArray(['day', 'month', 'year'])->string('view');
59        $cal      = Validator::queryParams($request)->string('cal', '');
60        $day      = Validator::queryParams($request)->string('day', '');
61        $month    = Validator::queryParams($request)->string('month', '');
62        $year     = Validator::queryParams($request)->string('year', '');
63        $filterev = Validator::queryParams($request)->string('filterev', 'BIRT-MARR-DEAT');
64        $filterof = Validator::queryParams($request)->string('filterof', 'all');
65        $filtersx = Validator::queryParams($request)->string('filtersx', '');
66
67        if ($cal . $day . $month . $year === '') {
68            // No date specified? Use the most likely calendar
69            $cal = I18N::language()->calendar()->gedcomCalendarEscape();
70        }
71
72        // need BC to parse date
73        if (str_starts_with($year, '-')) {
74            $year = substr($year, 1) . ' B.C.';
75        }
76        $ged_date = new Date($cal . ' ' . $day . ' ' . $month . ' ' . $year);
77        // need negative year for year entry field.
78        $year     = $ged_date->minimumDate()->year;
79        $cal_date = $ged_date->minimumDate();
80
81        // Fill in any missing bits with todays date
82        $today = $cal_date->today();
83        if ($cal_date->day === 0) {
84            $cal_date->day = $today->day;
85        }
86        if ($cal_date->month === 0) {
87            $cal_date->month = $today->month;
88        }
89        if ($cal_date->year === 0) {
90            $cal_date->year = $today->year;
91        }
92
93        $cal_date->setJdFromYmd();
94
95        if ($year === 0) {
96            $year = $cal_date->year;
97        }
98
99        // Extract values from date
100        $days_in_month = $cal_date->daysInMonth();
101        $cal_month     = $cal_date->format('%O');
102        $today_month   = $today->format('%O');
103
104        // Invalid dates? Go to monthly view, where they'll be found.
105        if ($cal_date->day > $days_in_month && $view === 'day') {
106            $view = 'month';
107        }
108
109        $title = I18N::translate('Anniversary calendar');
110
111        switch ($view) {
112            case 'day':
113                $title = I18N::translate('On this day…') . ' ' . $ged_date->display($tree);
114                break;
115            case 'month':
116                $title = I18N::translate('In this month…') . ' ' . $ged_date->display($tree, '%F %Y');
117                break;
118            case 'year':
119                $title = I18N::translate('In this year…') . ' ' . $ged_date->display($tree, '%Y');
120                break;
121        }
122
123        return $this->viewResponse('calendar-page', [
124            'cal'           => $cal,
125            'cal_date'      => $cal_date,
126            'cal_month'     => $cal_month,
127            'day'           => $day,
128            'days_in_month' => $days_in_month,
129            'filterev'      => $filterev,
130            'filterof'      => $filterof,
131            'filtersx'      => $filtersx,
132            'month'         => $month,
133            'months'        => $this->calendar_service->calendarMonthsInYear($cal, $year),
134            'title'         => $title,
135            'today'         => $today,
136            'today_month'   => $today_month,
137            'tree'          => $tree,
138            'view'          => $view,
139            'year'          => $year,
140        ]);
141    }
142}
143