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