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; 30*e24053e5SGreg Roachuse Illuminate\Support\Collection; 311e7a7a28SGreg Roachuse Illuminate\Support\Str; 326ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 338c2e8227SGreg Roach 348c2e8227SGreg Roach/** 358c2e8227SGreg Roach * Class YahrzeitModule 368c2e8227SGreg Roach */ 3737eb8894SGreg Roachclass YahrzeitModule extends AbstractModule implements ModuleBlockInterface 38c1010edaSGreg Roach{ 3949a243cbSGreg Roach use ModuleBlockTrait; 4049a243cbSGreg Roach 41c385536dSGreg Roach // Default values for new blocks. 4216d6367aSGreg Roach private const DEFAULT_CALENDAR = 'jewish'; 4316d6367aSGreg Roach private const DEFAULT_DAYS = '7'; 4416d6367aSGreg Roach private const DEFAULT_STYLE = 'table'; 45c385536dSGreg Roach 46c385536dSGreg Roach // Can show this number of days into the future. 4716d6367aSGreg Roach private const MAX_DAYS = 30; 48c385536dSGreg Roach 49961ec755SGreg Roach /** 500cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 51961ec755SGreg Roach * 52961ec755SGreg Roach * @return string 53961ec755SGreg Roach */ 5449a243cbSGreg Roach public function title(): string 55c1010edaSGreg Roach { 56bbb76c12SGreg Roach /* I18N: Name of a module. Yahrzeiten (the plural of Yahrzeit) are special anniversaries of deaths in the Hebrew faith/calendar. */ 57bbb76c12SGreg Roach return I18N::translate('Yahrzeiten'); 588c2e8227SGreg Roach } 598c2e8227SGreg Roach 60961ec755SGreg Roach /** 61961ec755SGreg Roach * A sentence describing what this module does. 62961ec755SGreg Roach * 63961ec755SGreg Roach * @return string 64961ec755SGreg Roach */ 6549a243cbSGreg Roach public function description(): string 66c1010edaSGreg Roach { 67bbb76c12SGreg Roach /* I18N: Description of the “Yahrzeiten” module. A “Hebrew death” is a death where the date is recorded in the Hebrew calendar. */ 68bbb76c12SGreg Roach return I18N::translate('A list of the Hebrew death anniversaries that will occur in the near future.'); 698c2e8227SGreg Roach } 708c2e8227SGreg Roach 7176692c8bSGreg Roach /** 7276692c8bSGreg Roach * Generate the HTML content of this block. 7376692c8bSGreg Roach * 74e490cd80SGreg Roach * @param Tree $tree 7576692c8bSGreg Roach * @param int $block_id 763caaa4d2SGreg Roach * @param string $context 773caaa4d2SGreg Roach * @param string[] $config 7876692c8bSGreg Roach * 7976692c8bSGreg Roach * @return string 8076692c8bSGreg Roach */ 813caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 82c1010edaSGreg Roach { 83f0a11419SGreg Roach $calendar_service = new CalendarService(); 84f0a11419SGreg Roach 85e490cd80SGreg Roach $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 86c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 87c385536dSGreg Roach $calendar = $this->getBlockSetting($block_id, 'calendar', self::DEFAULT_CALENDAR); 888c2e8227SGreg Roach 893caaa4d2SGreg Roach extract($config, EXTR_OVERWRITE); 908c2e8227SGreg Roach 9159f2f229SGreg Roach $jewish_calendar = new JewishCalendar(); 924459dc9aSGreg Roach $startjd = Carbon::now()->julianDay(); 93269fd10dSGreg Roach $endjd = $startjd + $days - 1; 948c2e8227SGreg Roach 958c2e8227SGreg Roach // The standard anniversary rules cover most of the Yahrzeit rules, we just 968c2e8227SGreg Roach // need to handle a few special cases. 9789f721acSGreg Roach // Fetch normal anniversaries, with an extra day before/after 98*e24053e5SGreg Roach $yahrzeits = new Collection(); 998c2e8227SGreg Roach for ($jd = $startjd - 1; $jd <= $endjd + $days; ++$jd) { 100f0a11419SGreg Roach foreach ($calendar_service->getAnniversaryEvents($jd, 'DEAT _YART', $tree) as $fact) { 1018c2e8227SGreg Roach // Exact hebrew dates only 1022decada7SGreg Roach $date = $fact->date(); 1038c2e8227SGreg Roach if ($date->minimumDate() instanceof JewishDate && $date->minimumJulianDay() === $date->maximumJulianDay()) { 10489f721acSGreg Roach // ...then adjust DEAT dates (but not _YART) 10589f721acSGreg Roach if ($fact->getTag() === 'DEAT') { 10689f721acSGreg Roach $today = new JewishDate($jd); 1072decada7SGreg Roach $hd = $fact->date()->minimumDate(); 1088c2e8227SGreg Roach $hd1 = new JewishDate($hd); 109e364afe4SGreg Roach ++$hd1->year; 1108c2e8227SGreg Roach $hd1->setJdFromYmd(); 1118c2e8227SGreg Roach // Special rules. See http://www.hebcal.com/help/anniv.html 1128c2e8227SGreg Roach // Everything else is taken care of by our standard anniversary rules. 1134a83f5d7SGreg Roach if ($hd->day == 30 && $hd->month == 2 && $hd->year != 0 && $hd1->daysInMonth() < 30) { 1148c2e8227SGreg Roach // 30 CSH - Last day in CSH 1154a83f5d7SGreg Roach $jd = $jewish_calendar->ymdToJd($today->year, 3, 1) - 1; 1164a83f5d7SGreg Roach } elseif ($hd->day == 30 && $hd->month == 3 && $hd->year != 0 && $hd1->daysInMonth() < 30) { 1178c2e8227SGreg Roach // 30 KSL - Last day in KSL 1184a83f5d7SGreg Roach $jd = $jewish_calendar->ymdToJd($today->year, 4, 1) - 1; 1194a83f5d7SGreg Roach } elseif ($hd->day == 30 && $hd->month == 6 && $hd->year != 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) { 1208c2e8227SGreg Roach // 30 ADR - Last day in SHV 1214a83f5d7SGreg Roach $jd = $jewish_calendar->ymdToJd($today->year, 6, 1) - 1; 12289f721acSGreg Roach } 12389f721acSGreg Roach } 12489f721acSGreg Roach 12589f721acSGreg Roach // Filter adjusted dates to our date range 12689f721acSGreg Roach if ($jd >= $startjd && $jd < $startjd + $days) { 12789f721acSGreg Roach // upcomming yahrzeit dates 12889f721acSGreg Roach switch ($calendar) { 12989f721acSGreg Roach case 'gregorian': 13089f721acSGreg Roach $yahrzeit_date = new GregorianDate($jd); 13189f721acSGreg Roach break; 13289f721acSGreg Roach case 'jewish': 13389f721acSGreg Roach default: 13489f721acSGreg Roach $yahrzeit_date = new JewishDate($jd); 13589f721acSGreg Roach break; 13689f721acSGreg Roach } 13789f721acSGreg Roach $yahrzeit_date = new Date($yahrzeit_date->format('%@ %A %O %E')); 13889f721acSGreg Roach 139*e24053e5SGreg Roach $yahrzeits->add((object) [ 140e7766c08SGreg Roach 'individual' => $fact->record(), 1412decada7SGreg Roach 'fact_date' => $fact->date(), 14289f721acSGreg Roach 'fact' => $fact, 14389f721acSGreg Roach 'jd' => $jd, 14489f721acSGreg Roach 'yahrzeit_date' => $yahrzeit_date, 145*e24053e5SGreg Roach ]); 14689f721acSGreg Roach } 1478c2e8227SGreg Roach } 1488c2e8227SGreg Roach } 1498c2e8227SGreg Roach } 1508c2e8227SGreg Roach 1518c2e8227SGreg Roach switch ($infoStyle) { 1528c2e8227SGreg Roach case 'list': 153147e99aaSGreg Roach $content = view('modules/yahrzeit/list', [ 154*e24053e5SGreg Roach 'id' => $block_id, 155*e24053e5SGreg Roach 'limit' => 10, 15689f721acSGreg Roach 'yahrzeits' => $yahrzeits, 15789f721acSGreg Roach ]); 1588c2e8227SGreg Roach break; 1598c2e8227SGreg Roach case 'table': 1608c2e8227SGreg Roach default: 161147e99aaSGreg Roach $content = view('modules/yahrzeit/table', [ 162*e24053e5SGreg Roach 'limit' => 10, 16389f721acSGreg Roach 'yahrzeits' => $yahrzeits, 16489f721acSGreg Roach ]); 1658c2e8227SGreg Roach break; 1668c2e8227SGreg Roach } 1678c2e8227SGreg Roach 1683caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 169147e99aaSGreg Roach return view('modules/block-template', [ 1701e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1719c6524dcSGreg Roach 'id' => $block_id, 1723caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 17349a243cbSGreg Roach 'title' => $this->title(), 1749c6524dcSGreg Roach 'content' => $content, 1759c6524dcSGreg Roach ]); 1768c2e8227SGreg Roach } 177b2ce94c6SRico Sonntag 178b2ce94c6SRico Sonntag return $content; 1798c2e8227SGreg Roach } 1808c2e8227SGreg Roach 1813caaa4d2SGreg Roach /** 1823caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1833caaa4d2SGreg Roach * 1843caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 1853caaa4d2SGreg Roach * 1863caaa4d2SGreg Roach * @return bool 1873caaa4d2SGreg Roach */ 188c1010edaSGreg Roach public function loadAjax(): bool 189c1010edaSGreg Roach { 1908c2e8227SGreg Roach return true; 1918c2e8227SGreg Roach } 1928c2e8227SGreg Roach 1933caaa4d2SGreg Roach /** 1943caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 1953caaa4d2SGreg Roach * 1963caaa4d2SGreg Roach * @return bool 1973caaa4d2SGreg Roach */ 198c1010edaSGreg Roach public function isUserBlock(): bool 199c1010edaSGreg Roach { 2008c2e8227SGreg Roach return true; 2018c2e8227SGreg Roach } 2028c2e8227SGreg Roach 2033caaa4d2SGreg Roach /** 2043caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2053caaa4d2SGreg Roach * 2063caaa4d2SGreg Roach * @return bool 2073caaa4d2SGreg Roach */ 20863276d8fSGreg Roach public function isTreeBlock(): bool 209c1010edaSGreg Roach { 2108c2e8227SGreg Roach return true; 2118c2e8227SGreg Roach } 2128c2e8227SGreg Roach 21376692c8bSGreg Roach /** 214a45f9889SGreg Roach * Update the configuration for a block. 215a45f9889SGreg Roach * 2166ccdf4f0SGreg Roach * @param ServerRequestInterface $request 217a45f9889SGreg Roach * @param int $block_id 218a45f9889SGreg Roach * 219a45f9889SGreg Roach * @return void 220a45f9889SGreg Roach */ 2216ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 222a45f9889SGreg Roach { 223b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 224c9e6b699SGreg Roach 225b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'days', $params['days'] ?? self::DEFAULT_DAYS); 226b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $params['infoStyle'] ?? self::DEFAULT_STYLE); 227b46c87bdSGreg Roach $this->setBlockSetting($block_id, 'calendar', $params['calendar'] ?? self::DEFAULT_CALENDAR); 228a45f9889SGreg Roach } 229a45f9889SGreg Roach 230a45f9889SGreg Roach /** 23176692c8bSGreg Roach * An HTML form to edit block settings 23276692c8bSGreg Roach * 233e490cd80SGreg Roach * @param Tree $tree 23476692c8bSGreg Roach * @param int $block_id 235a9430be8SGreg Roach * 2363caaa4d2SGreg Roach * @return string 23776692c8bSGreg Roach */ 2383caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 239c1010edaSGreg Roach { 240e2a378d3SGreg Roach $calendar = $this->getBlockSetting($block_id, 'calendar', 'jewish'); 241147e99aaSGreg Roach $days = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 242c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table'); 2438c2e8227SGreg Roach 244c385536dSGreg Roach $styles = [ 245bbb76c12SGreg Roach /* I18N: An option in a list-box */ 246bbb76c12SGreg Roach 'list' => I18N::translate('list'), 247bbb76c12SGreg Roach /* I18N: An option in a list-box */ 248bbb76c12SGreg Roach 'table' => I18N::translate('table'), 249c385536dSGreg Roach ]; 2508c2e8227SGreg Roach 251c385536dSGreg Roach $calendars = [ 252c385536dSGreg Roach 'jewish' => I18N::translate('Jewish'), 253c385536dSGreg Roach 'gregorian' => I18N::translate('Gregorian'), 254c385536dSGreg Roach ]; 2558c2e8227SGreg Roach 2563caaa4d2SGreg Roach return view('modules/yahrzeit/config', [ 257c385536dSGreg Roach 'calendar' => $calendar, 258c385536dSGreg Roach 'calendars' => $calendars, 259c385536dSGreg Roach 'days' => $days, 260c385536dSGreg Roach 'infoStyle' => $infoStyle, 261c385536dSGreg Roach 'max_days' => self::MAX_DAYS, 262c385536dSGreg Roach 'styles' => $styles, 263c385536dSGreg Roach ]); 2648c2e8227SGreg Roach } 2658c2e8227SGreg Roach} 266