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\Date\FrenchDate; 24b00cb080SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate; 25b00cb080SGreg Roachuse Fisharebest\Webtrees\Date\HijriDate; 26b00cb080SGreg Roachuse Fisharebest\Webtrees\Date\JalaliDate; 27b00cb080SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate; 28b00cb080SGreg Roachuse Fisharebest\Webtrees\Date\JulianDate; 29b00cb080SGreg Roachuse Fisharebest\Webtrees\Fact; 30b00cb080SGreg Roachuse Fisharebest\Webtrees\Family; 31b00cb080SGreg Roachuse Fisharebest\Webtrees\I18N; 32b00cb080SGreg Roachuse Fisharebest\Webtrees\Individual; 336b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 34b00cb080SGreg Roachuse Fisharebest\Webtrees\Services\CalendarService; 35b00cb080SGreg Roachuse Fisharebest\Webtrees\Tree; 36b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 37b00cb080SGreg Roachuse Illuminate\Support\Collection; 38b00cb080SGreg Roachuse Psr\Http\Message\ResponseInterface; 39b00cb080SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 40b00cb080SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 41b00cb080SGreg Roach 42b00cb080SGreg Roachuse function count; 43b00cb080SGreg Roachuse function e; 44b00cb080SGreg Roachuse function explode; 45b00cb080SGreg Roachuse function get_class; 46b00cb080SGreg Roachuse function ob_get_clean; 47b00cb080SGreg Roachuse function ob_start; 48b00cb080SGreg Roachuse function range; 49b00cb080SGreg Roachuse function response; 50b00cb080SGreg Roachuse function view; 51b00cb080SGreg Roach 52b00cb080SGreg Roach/** 53b00cb080SGreg Roach * Show anniversaries for events in a given day/month/year. 54b00cb080SGreg Roach */ 55b00cb080SGreg Roachclass CalendarEvents implements RequestHandlerInterface 56b00cb080SGreg Roach{ 57c4943cffSGreg Roach private CalendarService $calendar_service; 58b00cb080SGreg Roach 59b00cb080SGreg Roach /** 60b00cb080SGreg Roach * CalendarPage constructor. 61b00cb080SGreg Roach * 62b00cb080SGreg Roach * @param CalendarService $calendar_service 63b00cb080SGreg Roach */ 64b00cb080SGreg Roach public function __construct(CalendarService $calendar_service) 65b00cb080SGreg Roach { 66b00cb080SGreg Roach $this->calendar_service = $calendar_service; 67b00cb080SGreg Roach } 68b00cb080SGreg Roach 69b00cb080SGreg Roach /** 70fceda430SGreg Roach * Show anniversaries that occurred on a given day/month/year. 71b00cb080SGreg Roach * 72b00cb080SGreg Roach * @param ServerRequestInterface $request 73b00cb080SGreg Roach * 74b00cb080SGreg Roach * @return ResponseInterface 75b00cb080SGreg Roach */ 76b00cb080SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 77b00cb080SGreg Roach { 78b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 79*110d87e5SGreg Roach $view = Validator::attributes($request)->isInArray(['day', 'month', 'year'])->string('view'); 80b00cb080SGreg Roach $CALENDAR_FORMAT = $tree->getPreference('CALENDAR_FORMAT'); 81b00cb080SGreg Roach 82b00cb080SGreg Roach $cal = $request->getQueryParams()['cal'] ?? ''; 83b00cb080SGreg Roach $day = $request->getQueryParams()['day'] ?? ''; 84b00cb080SGreg Roach $month = $request->getQueryParams()['month'] ?? ''; 85b00cb080SGreg Roach $year = $request->getQueryParams()['year'] ?? ''; 86b00cb080SGreg Roach $filterev = $request->getQueryParams()['filterev'] ?? 'BIRT-MARR-DEAT'; 87b00cb080SGreg Roach $filterof = $request->getQueryParams()['filterof'] ?? 'all'; 88b00cb080SGreg Roach $filtersx = $request->getQueryParams()['filtersx'] ?? ''; 89b00cb080SGreg Roach 90b6ac34efSGreg Roach $ged_date = new Date($cal . ' ' . $day . ' ' . $month . ' ' . $year); 91b00cb080SGreg Roach $cal_date = $ged_date->minimumDate(); 92b00cb080SGreg Roach $today = $cal_date->today(); 93b00cb080SGreg Roach 94b00cb080SGreg Roach $days_in_month = $cal_date->daysInMonth(); 95b00cb080SGreg Roach $days_in_week = $cal_date->daysInWeek(); 96b00cb080SGreg Roach 97b00cb080SGreg Roach // Day and year share the same layout. 98b00cb080SGreg Roach if ($view !== 'month') { 99b00cb080SGreg Roach if ($view === 'day') { 100b00cb080SGreg Roach $anniversary_facts = $this->calendar_service->getAnniversaryEvents($cal_date->minimumJulianDay(), $filterev, $tree, $filterof, $filtersx); 101b00cb080SGreg Roach } else { 102b00cb080SGreg Roach $ged_year = new Date($cal . ' ' . $year); 103b00cb080SGreg Roach $anniversary_facts = $this->calendar_service->getCalendarEvents($ged_year->minimumJulianDay(), $ged_year->maximumJulianDay(), $filterev, $tree, $filterof, $filtersx); 104b00cb080SGreg Roach } 105b00cb080SGreg Roach 106b00cb080SGreg Roach $anniversaries = Collection::make($anniversary_facts) 107b00cb080SGreg Roach ->unique() 108b00cb080SGreg Roach ->sort(static function (Fact $x, Fact $y): int { 109b00cb080SGreg Roach return $x->date()->minimumJulianDay() <=> $y->date()->minimumJulianDay(); 110b00cb080SGreg Roach }); 111b00cb080SGreg Roach 112b00cb080SGreg Roach $family_anniversaries = $anniversaries->filter(static function (Fact $f): bool { 113b00cb080SGreg Roach return $f->record() instanceof Family; 114b00cb080SGreg Roach }); 115b00cb080SGreg Roach 116b00cb080SGreg Roach $individual_anniversaries = $anniversaries->filter(static function (Fact $f): bool { 117b00cb080SGreg Roach return $f->record() instanceof Individual; 118b00cb080SGreg Roach }); 119b00cb080SGreg Roach 120b00cb080SGreg Roach return response(view('calendar-list', [ 121b00cb080SGreg Roach 'family_anniversaries' => $family_anniversaries, 122b00cb080SGreg Roach 'individual_anniversaries' => $individual_anniversaries, 123b00cb080SGreg Roach ])); 124b00cb080SGreg Roach } 125b00cb080SGreg Roach 126b00cb080SGreg Roach $found_facts = []; 127b00cb080SGreg Roach 128b00cb080SGreg Roach $cal_date->day = 0; 129b00cb080SGreg Roach $cal_date->setJdFromYmd(); 130b00cb080SGreg Roach // Make a separate list for each day. Unspecified/invalid days go in day 0. 131b00cb080SGreg Roach for ($d = 0; $d <= $days_in_month; ++$d) { 132b00cb080SGreg Roach $found_facts[$d] = []; 133b00cb080SGreg Roach } 134b00cb080SGreg Roach // Fetch events for each day 135b00cb080SGreg Roach $jds = range($cal_date->minimumJulianDay(), $cal_date->maximumJulianDay()); 136b00cb080SGreg Roach 137b00cb080SGreg Roach foreach ($jds as $jd) { 138b00cb080SGreg Roach foreach ($this->calendar_service->getAnniversaryEvents($jd, $filterev, $tree, $filterof, $filtersx) as $fact) { 139b00cb080SGreg Roach $tmp = $fact->date()->minimumDate(); 140b00cb080SGreg Roach if ($tmp->day >= 1 && $tmp->day <= $tmp->daysInMonth()) { 141b00cb080SGreg Roach // If the day is valid (for its own calendar), display it in the 142b00cb080SGreg Roach // anniversary day (for the display calendar). 143b00cb080SGreg Roach $found_facts[$jd - $cal_date->minimumJulianDay() + 1][] = $fact; 144b00cb080SGreg Roach } else { 145b00cb080SGreg Roach // Otherwise, display it in the "Day not set" box. 146b00cb080SGreg Roach $found_facts[0][] = $fact; 147b00cb080SGreg Roach } 148b00cb080SGreg Roach } 149b00cb080SGreg Roach } 150b00cb080SGreg Roach 151b00cb080SGreg Roach $cal_facts = []; 152b00cb080SGreg Roach 153b00cb080SGreg Roach foreach ($found_facts as $d => $facts) { 154b00cb080SGreg Roach $cal_facts[$d] = []; 155b00cb080SGreg Roach foreach ($facts as $fact) { 156b00cb080SGreg Roach $xref = $fact->record()->xref(); 15766ecd017SGreg Roach $text = $fact->label() . ' — ' . $fact->date()->display($tree); 158b00cb080SGreg Roach if ($fact->anniv > 0) { 159bfed30e4SGreg Roach $text .= ' (' . I18N::translate('%s year anniversary', I18N::number($fact->anniv)) . ')'; 160b00cb080SGreg Roach } 161b00cb080SGreg Roach if (empty($cal_facts[$d][$xref])) { 162b00cb080SGreg Roach $cal_facts[$d][$xref] = $text; 163b00cb080SGreg Roach } else { 164b00cb080SGreg Roach $cal_facts[$d][$xref] .= '<br>' . $text; 165b00cb080SGreg Roach } 166b00cb080SGreg Roach } 167b00cb080SGreg Roach } 168b00cb080SGreg Roach // We use JD%7 = 0/Mon…6/Sun. Standard definitions use 0/Sun…6/Sat. 169b00cb080SGreg Roach $week_start = (I18N::locale()->territory()->firstDay() + 6) % 7; 170b00cb080SGreg Roach $weekend_start = (I18N::locale()->territory()->weekendStart() + 6) % 7; 171b00cb080SGreg Roach $weekend_end = (I18N::locale()->territory()->weekendEnd() + 6) % 7; 172bfed30e4SGreg Roach 173bfed30e4SGreg Roach // The French calendar has a 10-day week, which starts on primidi. 174b00cb080SGreg Roach if ($days_in_week === 10) { 175b00cb080SGreg Roach $week_start = 0; 176b00cb080SGreg Roach $weekend_start = -1; 177b00cb080SGreg Roach $weekend_end = -1; 178b00cb080SGreg Roach } 179b00cb080SGreg Roach 180b00cb080SGreg Roach ob_start(); 181b00cb080SGreg Roach 182789e127fSGreg Roach echo '<table class="w-100 wt-calendar-month"><thead><tr>'; 183b00cb080SGreg Roach for ($week_day = 0; $week_day < $days_in_week; ++$week_day) { 184b00cb080SGreg Roach $day_name = $cal_date->dayNames(($week_day + $week_start) % $days_in_week); 185b00cb080SGreg Roach if ($week_day === $weekend_start || $week_day === $weekend_end) { 186dd71ff6bSGreg Roach echo '<th class="wt-page-options-label weekend" width="', 100 / $days_in_week, '%">', $day_name, '</th>'; 187b00cb080SGreg Roach } else { 188dd71ff6bSGreg Roach echo '<th class="wt-page-options-label" width="', 100 / $days_in_week, '%">', $day_name, '</th>'; 189b00cb080SGreg Roach } 190b00cb080SGreg Roach } 191b00cb080SGreg Roach echo '</tr>'; 192b00cb080SGreg Roach echo '</thead>'; 193b00cb080SGreg Roach echo '<tbody>'; 194b00cb080SGreg Roach // Print days 1 to n of the month, but extend to cover "empty" days before/after the month to make whole weeks. 195b00cb080SGreg Roach // e.g. instead of 1 -> 30 (=30 days), we might have -1 -> 33 (=35 days) 196b00cb080SGreg Roach $start_d = 1 - ($cal_date->minimumJulianDay() - $week_start) % $days_in_week; 197b00cb080SGreg Roach $end_d = $days_in_month + ($days_in_week - ($cal_date->maximumJulianDay() - $week_start + 1) % $days_in_week) % $days_in_week; 198b00cb080SGreg Roach // Make sure that there is an empty box for any leap/missing days 199b00cb080SGreg Roach if ($start_d === 1 && $end_d === $days_in_month && count($found_facts[0]) > 0) { 200b00cb080SGreg Roach $end_d += $days_in_week; 201b00cb080SGreg Roach } 202b00cb080SGreg Roach for ($d = $start_d; $d <= $end_d; ++$d) { 203b00cb080SGreg Roach if (($d + $cal_date->minimumJulianDay() - $week_start) % $days_in_week === 1) { 204b00cb080SGreg Roach echo '<tr>'; 205b00cb080SGreg Roach } 206b00cb080SGreg Roach echo '<td class="wt-page-options-value">'; 207b00cb080SGreg Roach if ($d < 1 || $d > $days_in_month) { 208b00cb080SGreg Roach if (count($cal_facts[0]) > 0) { 209d8278044SGreg Roach echo '<div class="cal_day">', I18N::translate('Day not set'), '</div>'; 210b00cb080SGreg Roach echo '<div class="small" style="height: 180px; overflow: auto;">'; 2117b6e6159SGreg Roach echo $this->calendarListText($cal_facts[0], $tree); 212b00cb080SGreg Roach echo '</div>'; 213b00cb080SGreg Roach $cal_facts[0] = []; 214b00cb080SGreg Roach } 215b00cb080SGreg Roach } else { 216b00cb080SGreg Roach // Format the day number using the calendar 217e7825935SGreg Roach $tmp = new Date($cal_date->format('%@ ' . $d . ' %O %E')); 218b00cb080SGreg Roach $d_fmt = $tmp->minimumDate()->format('%j'); 219d8278044SGreg Roach echo '<div class="d-flex d-flex justify-content-between">'; 220b00cb080SGreg Roach if ($d === $today->day && $cal_date->month === $today->month) { 221b00cb080SGreg Roach echo '<span class="cal_day current_day">', $d_fmt, '</span>'; 222b00cb080SGreg Roach } else { 223b00cb080SGreg Roach echo '<span class="cal_day">', $d_fmt, '</span>'; 224b00cb080SGreg Roach } 225b00cb080SGreg Roach // Show a converted date 226b00cb080SGreg Roach foreach (explode('_and_', $CALENDAR_FORMAT) as $convcal) { 227b00cb080SGreg Roach switch ($convcal) { 228b00cb080SGreg Roach case 'french': 229b00cb080SGreg Roach $alt_date = new FrenchDate($cal_date->minimumJulianDay() + $d - 1); 230b00cb080SGreg Roach break; 231b00cb080SGreg Roach case 'gregorian': 232b00cb080SGreg Roach $alt_date = new GregorianDate($cal_date->minimumJulianDay() + $d - 1); 233b00cb080SGreg Roach break; 234b00cb080SGreg Roach case 'jewish': 235b00cb080SGreg Roach $alt_date = new JewishDate($cal_date->minimumJulianDay() + $d - 1); 236b00cb080SGreg Roach break; 237b00cb080SGreg Roach case 'julian': 238b00cb080SGreg Roach $alt_date = new JulianDate($cal_date->minimumJulianDay() + $d - 1); 239b00cb080SGreg Roach break; 240b00cb080SGreg Roach case 'hijri': 241b00cb080SGreg Roach $alt_date = new HijriDate($cal_date->minimumJulianDay() + $d - 1); 242b00cb080SGreg Roach break; 243b00cb080SGreg Roach case 'jalali': 244b00cb080SGreg Roach $alt_date = new JalaliDate($cal_date->minimumJulianDay() + $d - 1); 245b00cb080SGreg Roach break; 246b00cb080SGreg Roach case 'none': 247b00cb080SGreg Roach default: 248b00cb080SGreg Roach $alt_date = $cal_date; 249b00cb080SGreg Roach break; 250b00cb080SGreg Roach } 251b00cb080SGreg Roach if (get_class($alt_date) !== get_class($cal_date) && $alt_date->inValidRange()) { 252b00cb080SGreg Roach echo '<span class="rtl_cal_day">' . $alt_date->format('%j %M') . '</span>'; 253b00cb080SGreg Roach // Just show the first conversion 254b00cb080SGreg Roach break; 255b00cb080SGreg Roach } 256b00cb080SGreg Roach } 257d8278044SGreg Roach echo '</div>'; 258d8278044SGreg Roach echo '<div class="small" style="height: 180px; overflow: auto;">'; 2597b6e6159SGreg Roach echo $this->calendarListText($cal_facts[$d], $tree); 260b00cb080SGreg Roach echo '</div>'; 261b00cb080SGreg Roach } 262b00cb080SGreg Roach echo '</td>'; 263b00cb080SGreg Roach if (($d + $cal_date->minimumJulianDay() - $week_start) % $days_in_week === 0) { 264b00cb080SGreg Roach echo '</tr>'; 265b00cb080SGreg Roach } 266b00cb080SGreg Roach } 267b00cb080SGreg Roach echo '</tbody>'; 268b00cb080SGreg Roach echo '</table>'; 269b00cb080SGreg Roach 270b00cb080SGreg Roach return response(ob_get_clean()); 271b00cb080SGreg Roach } 272b00cb080SGreg Roach 273b00cb080SGreg Roach /** 274b00cb080SGreg Roach * Format a list of facts for display 275b00cb080SGreg Roach * 27609482a55SGreg Roach * @param array<string> $list 277b00cb080SGreg Roach * @param Tree $tree 278b00cb080SGreg Roach * 279b00cb080SGreg Roach * @return string 280b00cb080SGreg Roach */ 2817b6e6159SGreg Roach private function calendarListText(array $list, Tree $tree): string 282b00cb080SGreg Roach { 283b00cb080SGreg Roach $html = ''; 284b00cb080SGreg Roach 285b00cb080SGreg Roach foreach ($list as $xref => $facts) { 2866b9cb339SGreg Roach $tmp = Registry::gedcomRecordFactory()->make((string) $xref, $tree); 2877b6e6159SGreg Roach $html .= '<a href="' . e($tmp->url()) . '">' . $tmp->fullName() . '</a> '; 2887b6e6159SGreg Roach $html .= '<div class="indent">' . $facts . '</div>'; 289b00cb080SGreg Roach } 290b00cb080SGreg Roach 291b00cb080SGreg Roach return $html; 292b00cb080SGreg Roach } 293b00cb080SGreg Roach} 294