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