xref: /webtrees/app/Http/RequestHandlers/CalendarPage.php (revision e99ac601cee7ced57ac8545ceba47ece1b15d883)
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\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        $view     = Validator::attributes($request)->isInArray(['day', 'month', 'year'])->string('view');
66        $cal      = $request->getQueryParams()['cal'] ?? '';
67        $day      = $request->getQueryParams()['day'] ?? '';
68        $month    = $request->getQueryParams()['month'] ?? '';
69        $year     = $request->getQueryParams()['year'] ?? '';
70        $filterev = $request->getQueryParams()['filterev'] ?? 'BIRT-MARR-DEAT';
71        $filterof = $request->getQueryParams()['filterof'] ?? 'all';
72        $filtersx = $request->getQueryParams()['filtersx'] ?? '';
73
74        if ($cal . $day . $month . $year === '') {
75            // No date specified? Use the most likely calendar
76            $cal = $this->localization_service->calendar(I18N::locale())->gedcomCalendarEscape();
77        }
78
79        // need BC to parse date
80        if (str_starts_with($year, '-')) {
81            $year = substr($year, 1) . ' B.C.';
82        }
83        $ged_date = new Date($cal . ' ' . $day . ' ' . $month . ' ' . $year);
84        // need negative year for year entry field.
85        $year     = $ged_date->minimumDate()->year;
86        $cal_date = $ged_date->minimumDate();
87
88        // Fill in any missing bits with todays date
89        $today = $cal_date->today();
90        if ($cal_date->day === 0) {
91            $cal_date->day = $today->day;
92        }
93        if ($cal_date->month === 0) {
94            $cal_date->month = $today->month;
95        }
96        if ($cal_date->year === 0) {
97            $cal_date->year = $today->year;
98        }
99
100        $cal_date->setJdFromYmd();
101
102        if ($year === 0) {
103            $year = $cal_date->year;
104        }
105
106        // Extract values from date
107        $days_in_month = $cal_date->daysInMonth();
108        $cal_month     = $cal_date->format('%O');
109        $today_month   = $today->format('%O');
110
111        // Invalid dates? Go to monthly view, where they'll be found.
112        if ($cal_date->day > $days_in_month && $view === 'day') {
113            $view = 'month';
114        }
115
116        $title = I18N::translate('Anniversary calendar');
117
118        switch ($view) {
119            case 'day':
120                $title = I18N::translate('On this day…') . ' ' . $ged_date->display($tree);
121                break;
122            case 'month':
123                $title = I18N::translate('In this month…') . ' ' . $ged_date->display($tree, '%F %Y');
124                break;
125            case 'year':
126                $title = I18N::translate('In this year…') . ' ' . $ged_date->display($tree, '%Y');
127                break;
128        }
129
130        return $this->viewResponse('calendar-page', [
131            'cal'           => $cal,
132            'cal_date'      => $cal_date,
133            'cal_month'     => $cal_month,
134            'day'           => $day,
135            'days_in_month' => $days_in_month,
136            'filterev'      => $filterev,
137            'filterof'      => $filterof,
138            'filtersx'      => $filtersx,
139            'month'         => $month,
140            'months'        => $this->calendar_service->calendarMonthsInYear($cal, $year),
141            'title'         => $title,
142            'today'         => $today,
143            'today_month'   => $today_month,
144            'tree'          => $tree,
145            'view'          => $view,
146            'year'          => $year,
147        ]);
148    }
149}
150