xref: /webtrees/app/Http/RequestHandlers/CalendarPage.php (revision e93a8df2f8d797005750082cc3766c0e80799688)
1b00cb080SGreg Roach<?php
2b00cb080SGreg Roach
3b00cb080SGreg Roach/**
4b00cb080SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6b00cb080SGreg Roach * This program is free software: you can redistribute it and/or modify
7b00cb080SGreg Roach * it under the terms of the GNU General Public License as published by
8b00cb080SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9b00cb080SGreg Roach * (at your option) any later version.
10b00cb080SGreg Roach * This program is distributed in the hope that it will be useful,
11b00cb080SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12b00cb080SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13b00cb080SGreg Roach * GNU General Public License for more details.
14b00cb080SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16b00cb080SGreg Roach */
17b00cb080SGreg Roach
18b00cb080SGreg Roachdeclare(strict_types=1);
19b00cb080SGreg Roach
20b00cb080SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21b00cb080SGreg Roach
22b00cb080SGreg Roachuse Fisharebest\Webtrees\Date;
23b00cb080SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
24b00cb080SGreg Roachuse Fisharebest\Webtrees\I18N;
25b00cb080SGreg Roachuse Fisharebest\Webtrees\Services\CalendarService;
26b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
27b00cb080SGreg Roachuse Psr\Http\Message\ResponseInterface;
28b00cb080SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
29b00cb080SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
30b00cb080SGreg Roach
31b00cb080SGreg Roach/**
32b00cb080SGreg Roach * Show anniversaries for events in a given day/month/year.
33b00cb080SGreg Roach */
34b00cb080SGreg Roachclass CalendarPage implements RequestHandlerInterface
35b00cb080SGreg Roach{
36b00cb080SGreg Roach    use ViewResponseTrait;
37b00cb080SGreg Roach
38c4943cffSGreg Roach    private CalendarService $calendar_service;
39b00cb080SGreg Roach
40b00cb080SGreg Roach    /**
41b00cb080SGreg Roach     * @param CalendarService $calendar_service
42b00cb080SGreg Roach     */
434a9a6095SGreg Roach    public function __construct(CalendarService $calendar_service)
44b00cb080SGreg Roach    {
45b00cb080SGreg Roach        $this->calendar_service = $calendar_service;
46b00cb080SGreg Roach    }
47b00cb080SGreg Roach
48b00cb080SGreg Roach    /**
49b00cb080SGreg Roach     * A form to request the page parameters.
50b00cb080SGreg Roach     *
51b00cb080SGreg Roach     * @param ServerRequestInterface $request
52b00cb080SGreg Roach     *
53b00cb080SGreg Roach     * @return ResponseInterface
54b00cb080SGreg Roach     */
55b00cb080SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
56b00cb080SGreg Roach    {
57b55cbc6bSGreg Roach        $tree     = Validator::attributes($request)->tree();
58110d87e5SGreg Roach        $view     = Validator::attributes($request)->isInArray(['day', 'month', 'year'])->string('view');
59748dbe15SGreg Roach        $cal      = Validator::queryParams($request)->string('cal', '');
60748dbe15SGreg Roach        $day      = Validator::queryParams($request)->string('day', '');
61748dbe15SGreg Roach        $month    = Validator::queryParams($request)->string('month', '');
62748dbe15SGreg Roach        $year     = Validator::queryParams($request)->string('year', '');
63748dbe15SGreg Roach        $filterev = Validator::queryParams($request)->string('filterev', 'BIRT-MARR-DEAT');
64748dbe15SGreg Roach        $filterof = Validator::queryParams($request)->string('filterof', 'all');
65748dbe15SGreg Roach        $filtersx = Validator::queryParams($request)->string('filtersx', '');
66b00cb080SGreg Roach
67b00cb080SGreg Roach        if ($cal . $day . $month . $year === '') {
68b00cb080SGreg Roach            // No date specified? Use the most likely calendar
694a9a6095SGreg Roach            $cal = I18N::language()->calendar()->gedcomCalendarEscape();
70b00cb080SGreg Roach        }
71b00cb080SGreg Roach
72b00cb080SGreg Roach        // need BC to parse date
73e2308a2aSGreg Roach        if (str_starts_with($year, '-')) {
74e2308a2aSGreg Roach            $year = substr($year, 1) . ' B.C.';
75b00cb080SGreg Roach        }
76e7825935SGreg Roach        $ged_date = new Date($cal . ' ' . $day . ' ' . $month . ' ' . $year);
77b00cb080SGreg Roach        // need negative year for year entry field.
78b00cb080SGreg Roach        $year     = $ged_date->minimumDate()->year;
79b00cb080SGreg Roach        $cal_date = $ged_date->minimumDate();
80b00cb080SGreg Roach
81b00cb080SGreg Roach        // Fill in any missing bits with todays date
82b00cb080SGreg Roach        $today = $cal_date->today();
83b00cb080SGreg Roach        if ($cal_date->day === 0) {
84b00cb080SGreg Roach            $cal_date->day = $today->day;
85b00cb080SGreg Roach        }
86b00cb080SGreg Roach        if ($cal_date->month === 0) {
87b00cb080SGreg Roach            $cal_date->month = $today->month;
88b00cb080SGreg Roach        }
89b00cb080SGreg Roach        if ($cal_date->year === 0) {
90b00cb080SGreg Roach            $cal_date->year = $today->year;
91b00cb080SGreg Roach        }
92b00cb080SGreg Roach
93b00cb080SGreg Roach        $cal_date->setJdFromYmd();
94b00cb080SGreg Roach
95b00cb080SGreg Roach        if ($year === 0) {
96b00cb080SGreg Roach            $year = $cal_date->year;
97b00cb080SGreg Roach        }
98b00cb080SGreg Roach
99b00cb080SGreg Roach        // Extract values from date
100b00cb080SGreg Roach        $days_in_month = $cal_date->daysInMonth();
101b00cb080SGreg Roach        $cal_month     = $cal_date->format('%O');
102b00cb080SGreg Roach        $today_month   = $today->format('%O');
103b00cb080SGreg Roach
104b00cb080SGreg Roach        // Invalid dates? Go to monthly view, where they'll be found.
105b00cb080SGreg Roach        if ($cal_date->day > $days_in_month && $view === 'day') {
106b00cb080SGreg Roach            $view = 'month';
107b00cb080SGreg Roach        }
108b00cb080SGreg Roach
109b00cb080SGreg Roach        $title = I18N::translate('Anniversary calendar');
110b00cb080SGreg Roach
111b00cb080SGreg Roach        switch ($view) {
112b00cb080SGreg Roach            case 'day':
11366ecd017SGreg Roach                $title = I18N::translate('On this day…') . ' ' . $ged_date->display($tree);
114b00cb080SGreg Roach                break;
115b00cb080SGreg Roach            case 'month':
11666ecd017SGreg Roach                $title = I18N::translate('In this month…') . ' ' . $ged_date->display($tree, '%F %Y');
117b00cb080SGreg Roach                break;
118b00cb080SGreg Roach            case 'year':
11966ecd017SGreg Roach                $title = I18N::translate('In this year…') . ' ' . $ged_date->display($tree, '%Y');
120b00cb080SGreg Roach                break;
121b00cb080SGreg Roach        }
122b00cb080SGreg Roach
123b00cb080SGreg Roach        return $this->viewResponse('calendar-page', [
124b00cb080SGreg Roach            'cal'           => $cal,
125b00cb080SGreg Roach            'cal_date'      => $cal_date,
126b00cb080SGreg Roach            'cal_month'     => $cal_month,
127b00cb080SGreg Roach            'day'           => $day,
128b00cb080SGreg Roach            'days_in_month' => $days_in_month,
129b00cb080SGreg Roach            'filterev'      => $filterev,
130b00cb080SGreg Roach            'filterof'      => $filterof,
131b00cb080SGreg Roach            'filtersx'      => $filtersx,
132b00cb080SGreg Roach            'month'         => $month,
133b00cb080SGreg Roach            'months'        => $this->calendar_service->calendarMonthsInYear($cal, $year),
134b00cb080SGreg Roach            'title'         => $title,
135b00cb080SGreg Roach            'today'         => $today,
136b00cb080SGreg Roach            'today_month'   => $today_month,
137b00cb080SGreg Roach            'tree'          => $tree,
138b00cb080SGreg Roach            'view'          => $view,
139b00cb080SGreg Roach            'year'          => $year,
140b00cb080SGreg Roach        ]);
141b00cb080SGreg Roach    }
142b00cb080SGreg Roach}
143