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