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