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