18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 98c2e8227SGreg Roach * (at your option) any later version. 108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138c2e8227SGreg Roach * GNU General Public License for more details. 148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 168c2e8227SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 218c2e8227SGreg Roach 228c2e8227SGreg Roachuse Fisharebest\ExtCalendar\JewishCalendar; 234459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate; 270e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 28f0a11419SGreg Roachuse Fisharebest\Webtrees\Services\CalendarService; 29e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 301e7a7a28SGreg Roachuse Illuminate\Support\Str; 316ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 328c2e8227SGreg Roach 338c2e8227SGreg Roach/** 348c2e8227SGreg Roach * Class YahrzeitModule 358c2e8227SGreg Roach */ 3637eb8894SGreg Roachclass YahrzeitModule extends AbstractModule implements ModuleBlockInterface 37c1010edaSGreg Roach{ 3849a243cbSGreg Roach use ModuleBlockTrait; 3949a243cbSGreg Roach 40c385536dSGreg Roach // Default values for new blocks. 4116d6367aSGreg Roach private const DEFAULT_CALENDAR = 'jewish'; 4216d6367aSGreg Roach private const DEFAULT_DAYS = '7'; 4316d6367aSGreg Roach private const DEFAULT_STYLE = 'table'; 44c385536dSGreg Roach 45c385536dSGreg Roach // Can show this number of days into the future. 4616d6367aSGreg Roach private const MAX_DAYS = 30; 47c385536dSGreg Roach 48961ec755SGreg Roach /** 490cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 50961ec755SGreg Roach * 51961ec755SGreg Roach * @return string 52961ec755SGreg Roach */ 5349a243cbSGreg Roach public function title(): string 54c1010edaSGreg Roach { 55bbb76c12SGreg Roach /* I18N: Name of a module. Yahrzeiten (the plural of Yahrzeit) are special anniversaries of deaths in the Hebrew faith/calendar. */ 56bbb76c12SGreg Roach return I18N::translate('Yahrzeiten'); 578c2e8227SGreg Roach } 588c2e8227SGreg Roach 59961ec755SGreg Roach /** 60961ec755SGreg Roach * A sentence describing what this module does. 61961ec755SGreg Roach * 62961ec755SGreg Roach * @return string 63961ec755SGreg Roach */ 6449a243cbSGreg Roach public function description(): string 65c1010edaSGreg Roach { 66bbb76c12SGreg Roach /* I18N: Description of the “Yahrzeiten” module. A “Hebrew death” is a death where the date is recorded in the Hebrew calendar. */ 67bbb76c12SGreg Roach return I18N::translate('A list of the Hebrew death anniversaries that will occur in the near future.'); 688c2e8227SGreg Roach } 698c2e8227SGreg Roach 7076692c8bSGreg Roach /** 7176692c8bSGreg Roach * Generate the HTML content of this block. 7276692c8bSGreg Roach * 73e490cd80SGreg Roach * @param Tree $tree 7476692c8bSGreg Roach * @param int $block_id 753caaa4d2SGreg Roach * @param string $context 763caaa4d2SGreg Roach * @param string[] $config 7776692c8bSGreg Roach * 7876692c8bSGreg Roach * @return string 7976692c8bSGreg Roach */ 803caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 81c1010edaSGreg Roach { 82f0a11419SGreg Roach $calendar_service = new CalendarService(); 83f0a11419SGreg Roach 84e490cd80SGreg Roach $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 85c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 86c385536dSGreg Roach $calendar = $this->getBlockSetting($block_id, 'calendar', self::DEFAULT_CALENDAR); 878c2e8227SGreg Roach 883caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 898c2e8227SGreg Roach 9059f2f229SGreg Roach $jewish_calendar = new JewishCalendar(); 914459dc9aSGreg Roach $startjd = Carbon::now()->julianDay(); 92269fd10dSGreg Roach $endjd = $startjd + $days - 1; 938c2e8227SGreg Roach 948c2e8227SGreg Roach // The standard anniversary rules cover most of the Yahrzeit rules, we just 958c2e8227SGreg Roach // need to handle a few special cases. 9689f721acSGreg Roach // Fetch normal anniversaries, with an extra day before/after 9713abd6f3SGreg Roach $yahrzeits = []; 988c2e8227SGreg Roach for ($jd = $startjd - 1; $jd <= $endjd + $days; ++$jd) { 99f0a11419SGreg Roach foreach ($calendar_service->getAnniversaryEvents($jd, 'DEAT _YART', $tree) as $fact) { 1008c2e8227SGreg Roach // Exact hebrew dates only 1012decada7SGreg Roach $date = $fact->date(); 1028c2e8227SGreg Roach if ($date->minimumDate() instanceof JewishDate && $date->minimumJulianDay() === $date->maximumJulianDay()) { 10389f721acSGreg Roach // ...then adjust DEAT dates (but not _YART) 10489f721acSGreg Roach if ($fact->getTag() === 'DEAT') { 10589f721acSGreg Roach $today = new JewishDate($jd); 1062decada7SGreg Roach $hd = $fact->date()->minimumDate(); 1078c2e8227SGreg Roach $hd1 = new JewishDate($hd); 108e364afe4SGreg Roach ++$hd1->year; 1098c2e8227SGreg Roach $hd1->setJdFromYmd(); 1108c2e8227SGreg Roach // Special rules. See http://www.hebcal.com/help/anniv.html 1118c2e8227SGreg Roach // Everything else is taken care of by our standard anniversary rules. 1124a83f5d7SGreg Roach if ($hd->day == 30 && $hd->month == 2 && $hd->year != 0 && $hd1->daysInMonth() < 30) { 1138c2e8227SGreg Roach // 30 CSH - Last day in CSH 1144a83f5d7SGreg Roach $jd = $jewish_calendar->ymdToJd($today->year, 3, 1) - 1; 1154a83f5d7SGreg Roach } elseif ($hd->day == 30 && $hd->month == 3 && $hd->year != 0 && $hd1->daysInMonth() < 30) { 1168c2e8227SGreg Roach // 30 KSL - Last day in KSL 1174a83f5d7SGreg Roach $jd = $jewish_calendar->ymdToJd($today->year, 4, 1) - 1; 1184a83f5d7SGreg Roach } elseif ($hd->day == 30 && $hd->month == 6 && $hd->year != 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) { 1198c2e8227SGreg Roach // 30 ADR - Last day in SHV 1204a83f5d7SGreg Roach $jd = $jewish_calendar->ymdToJd($today->year, 6, 1) - 1; 12189f721acSGreg Roach } 12289f721acSGreg Roach } 12389f721acSGreg Roach 12489f721acSGreg Roach // Filter adjusted dates to our date range 12589f721acSGreg Roach if ($jd >= $startjd && $jd < $startjd + $days) { 12689f721acSGreg Roach // upcomming yahrzeit dates 12789f721acSGreg Roach switch ($calendar) { 12889f721acSGreg Roach case 'gregorian': 12989f721acSGreg Roach $yahrzeit_date = new GregorianDate($jd); 13089f721acSGreg Roach break; 13189f721acSGreg Roach case 'jewish': 13289f721acSGreg Roach default: 13389f721acSGreg Roach $yahrzeit_date = new JewishDate($jd); 13489f721acSGreg Roach break; 13589f721acSGreg Roach } 13689f721acSGreg Roach $yahrzeit_date = new Date($yahrzeit_date->format('%@ %A %O %E')); 13789f721acSGreg Roach 13889f721acSGreg Roach $yahrzeits[] = (object) [ 139e7766c08SGreg Roach 'individual' => $fact->record(), 1402decada7SGreg Roach 'fact_date' => $fact->date(), 14189f721acSGreg Roach 'fact' => $fact, 14289f721acSGreg Roach 'jd' => $jd, 14389f721acSGreg Roach 'yahrzeit_date' => $yahrzeit_date, 14489f721acSGreg Roach ]; 14589f721acSGreg Roach } 1468c2e8227SGreg Roach } 1478c2e8227SGreg Roach } 1488c2e8227SGreg Roach } 1498c2e8227SGreg Roach 1508c2e8227SGreg Roach switch ($infoStyle) { 1518c2e8227SGreg Roach case 'list': 152147e99aaSGreg Roach $content = view('modules/yahrzeit/list', [ 15389f721acSGreg Roach 'yahrzeits' => $yahrzeits, 15489f721acSGreg Roach ]); 1558c2e8227SGreg Roach break; 1568c2e8227SGreg Roach case 'table': 1578c2e8227SGreg Roach default: 158147e99aaSGreg Roach $content = view('modules/yahrzeit/table', [ 15989f721acSGreg Roach 'yahrzeits' => $yahrzeits, 16089f721acSGreg Roach ]); 1618c2e8227SGreg Roach break; 1628c2e8227SGreg Roach } 1638c2e8227SGreg Roach 1643caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 165147e99aaSGreg Roach return view('modules/block-template', [ 1661e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1679c6524dcSGreg Roach 'id' => $block_id, 1683caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 16949a243cbSGreg Roach 'title' => $this->title(), 1709c6524dcSGreg Roach 'content' => $content, 1719c6524dcSGreg Roach ]); 1728c2e8227SGreg Roach } 173b2ce94c6SRico Sonntag 174b2ce94c6SRico Sonntag return $content; 1758c2e8227SGreg Roach } 1768c2e8227SGreg Roach 1773caaa4d2SGreg Roach /** 1783caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1793caaa4d2SGreg Roach * 1803caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 1813caaa4d2SGreg Roach * 1823caaa4d2SGreg Roach * @return bool 1833caaa4d2SGreg Roach */ 184c1010edaSGreg Roach public function loadAjax(): bool 185c1010edaSGreg Roach { 1868c2e8227SGreg Roach return true; 1878c2e8227SGreg Roach } 1888c2e8227SGreg Roach 1893caaa4d2SGreg Roach /** 1903caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 1913caaa4d2SGreg Roach * 1923caaa4d2SGreg Roach * @return bool 1933caaa4d2SGreg Roach */ 194c1010edaSGreg Roach public function isUserBlock(): bool 195c1010edaSGreg Roach { 1968c2e8227SGreg Roach return true; 1978c2e8227SGreg Roach } 1988c2e8227SGreg Roach 1993caaa4d2SGreg Roach /** 2003caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2013caaa4d2SGreg Roach * 2023caaa4d2SGreg Roach * @return bool 2033caaa4d2SGreg Roach */ 20463276d8fSGreg Roach public function isTreeBlock(): bool 205c1010edaSGreg Roach { 2068c2e8227SGreg Roach return true; 2078c2e8227SGreg Roach } 2088c2e8227SGreg Roach 20976692c8bSGreg Roach /** 210a45f9889SGreg Roach * Update the configuration for a block. 211a45f9889SGreg Roach * 2126ccdf4f0SGreg Roach * @param ServerRequestInterface $request 213a45f9889SGreg Roach * @param int $block_id 214a45f9889SGreg Roach * 215a45f9889SGreg Roach * @return void 216a45f9889SGreg Roach */ 2176ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 218a45f9889SGreg Roach { 219*b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 220c9e6b699SGreg Roach 221*b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'days', $params['days'] ?? self::DEFAULT_DAYS); 222*b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle'] ?? self::DEFAULT_STYLE); 223*b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'calendar', $params['calendar'] ?? self::DEFAULT_CALENDAR); 224a45f9889SGreg Roach } 225a45f9889SGreg Roach 226a45f9889SGreg Roach /** 22776692c8bSGreg Roach * An HTML form to edit block settings 22876692c8bSGreg Roach * 229e490cd80SGreg Roach * @param Tree $tree 23076692c8bSGreg Roach * @param int $block_id 231a9430be8SGreg Roach * 2323caaa4d2SGreg Roach * @return string 23376692c8bSGreg Roach */ 2343caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 235c1010edaSGreg Roach { 236e2a378d3SGreg Roach $calendar = $this->getBlockSetting($block_id, 'calendar', 'jewish'); 237147e99aaSGreg Roach $days = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 238c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table'); 2398c2e8227SGreg Roach 240c385536dSGreg Roach $styles = [ 241bbb76c12SGreg Roach /* I18N: An option in a list-box */ 242bbb76c12SGreg Roach 'list' => I18N::translate('list'), 243bbb76c12SGreg Roach /* I18N: An option in a list-box */ 244bbb76c12SGreg Roach 'table' => I18N::translate('table'), 245c385536dSGreg Roach ]; 2468c2e8227SGreg Roach 247c385536dSGreg Roach $calendars = [ 248c385536dSGreg Roach 'jewish' => I18N::translate('Jewish'), 249c385536dSGreg Roach 'gregorian' => I18N::translate('Gregorian'), 250c385536dSGreg Roach ]; 2518c2e8227SGreg Roach 2523caaa4d2SGreg Roach return view('modules/yahrzeit/config', [ 253c385536dSGreg Roach 'calendar' => $calendar, 254c385536dSGreg Roach 'calendars' => $calendars, 255c385536dSGreg Roach 'days' => $days, 256c385536dSGreg Roach 'infoStyle' => $infoStyle, 257c385536dSGreg Roach 'max_days' => self::MAX_DAYS, 258c385536dSGreg Roach 'styles' => $styles, 259c385536dSGreg Roach ]); 2608c2e8227SGreg Roach } 2618c2e8227SGreg Roach} 262