18c2e8227SGreg Roach<?php 2*3976b470SGreg 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 */ 17e7f56f2aSGreg Roachdeclare(strict_types=1); 18e7f56f2aSGreg Roach 1976692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 208c2e8227SGreg Roach 218c2e8227SGreg Roachuse Fisharebest\ExtCalendar\JewishCalendar; 224459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 27f0a11419SGreg Roachuse Fisharebest\Webtrees\Services\CalendarService; 28e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 291e7a7a28SGreg Roachuse Illuminate\Support\Str; 306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 318c2e8227SGreg Roach 328c2e8227SGreg Roach/** 338c2e8227SGreg Roach * Class YahrzeitModule 348c2e8227SGreg Roach */ 3537eb8894SGreg Roachclass YahrzeitModule extends AbstractModule implements ModuleBlockInterface 36c1010edaSGreg Roach{ 3749a243cbSGreg Roach use ModuleBlockTrait; 3849a243cbSGreg Roach 39c385536dSGreg Roach // Default values for new blocks. 4016d6367aSGreg Roach private const DEFAULT_CALENDAR = 'jewish'; 4116d6367aSGreg Roach private const DEFAULT_DAYS = '7'; 4216d6367aSGreg Roach private const DEFAULT_STYLE = 'table'; 43c385536dSGreg Roach 44c385536dSGreg Roach // Can show this number of days into the future. 4516d6367aSGreg Roach private const MAX_DAYS = 30; 46c385536dSGreg Roach 47961ec755SGreg Roach /** 480cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 49961ec755SGreg Roach * 50961ec755SGreg Roach * @return string 51961ec755SGreg Roach */ 5249a243cbSGreg Roach public function title(): string 53c1010edaSGreg Roach { 54bbb76c12SGreg Roach /* I18N: Name of a module. Yahrzeiten (the plural of Yahrzeit) are special anniversaries of deaths in the Hebrew faith/calendar. */ 55bbb76c12SGreg Roach return I18N::translate('Yahrzeiten'); 568c2e8227SGreg Roach } 578c2e8227SGreg Roach 58961ec755SGreg Roach /** 59961ec755SGreg Roach * A sentence describing what this module does. 60961ec755SGreg Roach * 61961ec755SGreg Roach * @return string 62961ec755SGreg Roach */ 6349a243cbSGreg Roach public function description(): string 64c1010edaSGreg Roach { 65bbb76c12SGreg Roach /* I18N: Description of the “Yahrzeiten” module. A “Hebrew death” is a death where the date is recorded in the Hebrew calendar. */ 66bbb76c12SGreg Roach return I18N::translate('A list of the Hebrew death anniversaries that will occur in the near future.'); 678c2e8227SGreg Roach } 688c2e8227SGreg Roach 6976692c8bSGreg Roach /** 7076692c8bSGreg Roach * Generate the HTML content of this block. 7176692c8bSGreg Roach * 72e490cd80SGreg Roach * @param Tree $tree 7376692c8bSGreg Roach * @param int $block_id 743caaa4d2SGreg Roach * @param string $context 753caaa4d2SGreg Roach * @param string[] $config 7676692c8bSGreg Roach * 7776692c8bSGreg Roach * @return string 7876692c8bSGreg Roach */ 793caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 80c1010edaSGreg Roach { 81f0a11419SGreg Roach $calendar_service = new CalendarService(); 82f0a11419SGreg Roach 83e490cd80SGreg Roach $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 84c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 85c385536dSGreg Roach $calendar = $this->getBlockSetting($block_id, 'calendar', self::DEFAULT_CALENDAR); 868c2e8227SGreg Roach 873caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 888c2e8227SGreg Roach 8959f2f229SGreg Roach $jewish_calendar = new JewishCalendar(); 904459dc9aSGreg Roach $startjd = Carbon::now()->julianDay(); 91269fd10dSGreg Roach $endjd = $startjd + $days - 1; 928c2e8227SGreg Roach 938c2e8227SGreg Roach // The standard anniversary rules cover most of the Yahrzeit rules, we just 948c2e8227SGreg Roach // need to handle a few special cases. 9589f721acSGreg Roach // Fetch normal anniversaries, with an extra day before/after 9613abd6f3SGreg Roach $yahrzeits = []; 978c2e8227SGreg Roach for ($jd = $startjd - 1; $jd <= $endjd + $days; ++$jd) { 98f0a11419SGreg Roach foreach ($calendar_service->getAnniversaryEvents($jd, 'DEAT _YART', $tree) as $fact) { 998c2e8227SGreg Roach // Exact hebrew dates only 1002decada7SGreg Roach $date = $fact->date(); 1018c2e8227SGreg Roach if ($date->minimumDate() instanceof JewishDate && $date->minimumJulianDay() === $date->maximumJulianDay()) { 10289f721acSGreg Roach // ...then adjust DEAT dates (but not _YART) 10389f721acSGreg Roach if ($fact->getTag() === 'DEAT') { 10489f721acSGreg Roach $today = new JewishDate($jd); 1052decada7SGreg Roach $hd = $fact->date()->minimumDate(); 1068c2e8227SGreg Roach $hd1 = new JewishDate($hd); 107e364afe4SGreg Roach ++$hd1->year; 1088c2e8227SGreg Roach $hd1->setJdFromYmd(); 1098c2e8227SGreg Roach // Special rules. See http://www.hebcal.com/help/anniv.html 1108c2e8227SGreg Roach // Everything else is taken care of by our standard anniversary rules. 1114a83f5d7SGreg Roach if ($hd->day == 30 && $hd->month == 2 && $hd->year != 0 && $hd1->daysInMonth() < 30) { 1128c2e8227SGreg Roach // 30 CSH - Last day in CSH 1134a83f5d7SGreg Roach $jd = $jewish_calendar->ymdToJd($today->year, 3, 1) - 1; 1144a83f5d7SGreg Roach } elseif ($hd->day == 30 && $hd->month == 3 && $hd->year != 0 && $hd1->daysInMonth() < 30) { 1158c2e8227SGreg Roach // 30 KSL - Last day in KSL 1164a83f5d7SGreg Roach $jd = $jewish_calendar->ymdToJd($today->year, 4, 1) - 1; 1174a83f5d7SGreg Roach } elseif ($hd->day == 30 && $hd->month == 6 && $hd->year != 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) { 1188c2e8227SGreg Roach // 30 ADR - Last day in SHV 1194a83f5d7SGreg Roach $jd = $jewish_calendar->ymdToJd($today->year, 6, 1) - 1; 12089f721acSGreg Roach } 12189f721acSGreg Roach } 12289f721acSGreg Roach 12389f721acSGreg Roach // Filter adjusted dates to our date range 12489f721acSGreg Roach if ($jd >= $startjd && $jd < $startjd + $days) { 12589f721acSGreg Roach // upcomming yahrzeit dates 12689f721acSGreg Roach switch ($calendar) { 12789f721acSGreg Roach case 'gregorian': 12889f721acSGreg Roach $yahrzeit_date = new GregorianDate($jd); 12989f721acSGreg Roach break; 13089f721acSGreg Roach case 'jewish': 13189f721acSGreg Roach default: 13289f721acSGreg Roach $yahrzeit_date = new JewishDate($jd); 13389f721acSGreg Roach break; 13489f721acSGreg Roach } 13589f721acSGreg Roach $yahrzeit_date = new Date($yahrzeit_date->format('%@ %A %O %E')); 13689f721acSGreg Roach 13789f721acSGreg Roach $yahrzeits[] = (object) [ 138e7766c08SGreg Roach 'individual' => $fact->record(), 1392decada7SGreg Roach 'fact_date' => $fact->date(), 14089f721acSGreg Roach 'fact' => $fact, 14189f721acSGreg Roach 'jd' => $jd, 14289f721acSGreg Roach 'yahrzeit_date' => $yahrzeit_date, 14389f721acSGreg Roach ]; 14489f721acSGreg Roach } 1458c2e8227SGreg Roach } 1468c2e8227SGreg Roach } 1478c2e8227SGreg Roach } 1488c2e8227SGreg Roach 1498c2e8227SGreg Roach switch ($infoStyle) { 1508c2e8227SGreg Roach case 'list': 151147e99aaSGreg Roach $content = view('modules/yahrzeit/list', [ 15289f721acSGreg Roach 'yahrzeits' => $yahrzeits, 15389f721acSGreg Roach ]); 1548c2e8227SGreg Roach break; 1558c2e8227SGreg Roach case 'table': 1568c2e8227SGreg Roach default: 157147e99aaSGreg Roach $content = view('modules/yahrzeit/table', [ 15889f721acSGreg Roach 'yahrzeits' => $yahrzeits, 15989f721acSGreg Roach ]); 1608c2e8227SGreg Roach break; 1618c2e8227SGreg Roach } 1628c2e8227SGreg Roach 1633caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 164147e99aaSGreg Roach return view('modules/block-template', [ 1651e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1669c6524dcSGreg Roach 'id' => $block_id, 1673caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 16849a243cbSGreg Roach 'title' => $this->title(), 1699c6524dcSGreg Roach 'content' => $content, 1709c6524dcSGreg Roach ]); 1718c2e8227SGreg Roach } 172b2ce94c6SRico Sonntag 173b2ce94c6SRico Sonntag return $content; 1748c2e8227SGreg Roach } 1758c2e8227SGreg Roach 1763caaa4d2SGreg Roach /** 1773caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1783caaa4d2SGreg Roach * 1793caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 1803caaa4d2SGreg Roach * 1813caaa4d2SGreg Roach * @return bool 1823caaa4d2SGreg Roach */ 183c1010edaSGreg Roach public function loadAjax(): bool 184c1010edaSGreg Roach { 1858c2e8227SGreg Roach return true; 1868c2e8227SGreg Roach } 1878c2e8227SGreg Roach 1883caaa4d2SGreg Roach /** 1893caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 1903caaa4d2SGreg Roach * 1913caaa4d2SGreg Roach * @return bool 1923caaa4d2SGreg Roach */ 193c1010edaSGreg Roach public function isUserBlock(): bool 194c1010edaSGreg Roach { 1958c2e8227SGreg Roach return true; 1968c2e8227SGreg Roach } 1978c2e8227SGreg Roach 1983caaa4d2SGreg Roach /** 1993caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2003caaa4d2SGreg Roach * 2013caaa4d2SGreg Roach * @return bool 2023caaa4d2SGreg Roach */ 20363276d8fSGreg Roach public function isTreeBlock(): bool 204c1010edaSGreg Roach { 2058c2e8227SGreg Roach return true; 2068c2e8227SGreg Roach } 2078c2e8227SGreg Roach 20876692c8bSGreg Roach /** 209a45f9889SGreg Roach * Update the configuration for a block. 210a45f9889SGreg Roach * 2116ccdf4f0SGreg Roach * @param ServerRequestInterface $request 212a45f9889SGreg Roach * @param int $block_id 213a45f9889SGreg Roach * 214a45f9889SGreg Roach * @return void 215a45f9889SGreg Roach */ 2166ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 217a45f9889SGreg Roach { 218c9e6b699SGreg Roach $settings = $request->getParsedBody(); 219c9e6b699SGreg Roach 220c9e6b699SGreg Roach $this->setBlockSetting($block_id, 'days', $settings['days'] ?? self::DEFAULT_DAYS); 221c9e6b699SGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $settings['infoStyle'] ?? self::DEFAULT_STYLE); 222c9e6b699SGreg Roach $this->setBlockSetting($block_id, 'calendar', $settings['calendar'] ?? self::DEFAULT_CALENDAR); 223a45f9889SGreg Roach } 224a45f9889SGreg Roach 225a45f9889SGreg Roach /** 22676692c8bSGreg Roach * An HTML form to edit block settings 22776692c8bSGreg Roach * 228e490cd80SGreg Roach * @param Tree $tree 22976692c8bSGreg Roach * @param int $block_id 230a9430be8SGreg Roach * 2313caaa4d2SGreg Roach * @return string 23276692c8bSGreg Roach */ 2333caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 234c1010edaSGreg Roach { 235e2a378d3SGreg Roach $calendar = $this->getBlockSetting($block_id, 'calendar', 'jewish'); 236147e99aaSGreg Roach $days = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 237c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table'); 2388c2e8227SGreg Roach 239c385536dSGreg Roach $styles = [ 240bbb76c12SGreg Roach /* I18N: An option in a list-box */ 241bbb76c12SGreg Roach 'list' => I18N::translate('list'), 242bbb76c12SGreg Roach /* I18N: An option in a list-box */ 243bbb76c12SGreg Roach 'table' => I18N::translate('table'), 244c385536dSGreg Roach ]; 2458c2e8227SGreg Roach 246c385536dSGreg Roach $calendars = [ 247c385536dSGreg Roach 'jewish' => I18N::translate('Jewish'), 248c385536dSGreg Roach 'gregorian' => I18N::translate('Gregorian'), 249c385536dSGreg Roach ]; 2508c2e8227SGreg Roach 2513caaa4d2SGreg Roach return view('modules/yahrzeit/config', [ 252c385536dSGreg Roach 'calendar' => $calendar, 253c385536dSGreg Roach 'calendars' => $calendars, 254c385536dSGreg Roach 'days' => $days, 255c385536dSGreg Roach 'infoStyle' => $infoStyle, 256c385536dSGreg Roach 'max_days' => self::MAX_DAYS, 257c385536dSGreg Roach 'styles' => $styles, 258c385536dSGreg Roach ]); 2598c2e8227SGreg Roach } 2608c2e8227SGreg Roach} 261