18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 webtrees development team 58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 88c2e8227SGreg Roach * (at your option) any later version. 98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 128c2e8227SGreg Roach * GNU General Public License for more details. 138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 158c2e8227SGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 198c2e8227SGreg Roach 208c2e8227SGreg Roachuse Fisharebest\ExtCalendar\JewishCalendar; 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 26f0a11419SGreg Roachuse Fisharebest\Webtrees\Services\CalendarService; 27e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 28a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request; 298c2e8227SGreg Roach 308c2e8227SGreg Roach/** 318c2e8227SGreg Roach * Class YahrzeitModule 328c2e8227SGreg Roach */ 33c1010edaSGreg Roachclass YahrzeitModule extends AbstractModule implements ModuleBlockInterface 34c1010edaSGreg Roach{ 35c385536dSGreg Roach // Default values for new blocks. 36c385536dSGreg Roach const DEFAULT_CALENDAR = 'jewish'; 3755664801SGreg Roach const DEFAULT_DAYS = '7'; 38c385536dSGreg Roach const DEFAULT_STYLE = 'table'; 39c385536dSGreg Roach 40c385536dSGreg Roach // Can show this number of days into the future. 41c385536dSGreg Roach const MAX_DAYS = 30; 42c385536dSGreg Roach 438c2e8227SGreg Roach /** {@inheritdoc} */ 448f53f488SRico Sonntag public function getTitle(): string 45c1010edaSGreg Roach { 46bbb76c12SGreg Roach /* I18N: Name of a module. Yahrzeiten (the plural of Yahrzeit) are special anniversaries of deaths in the Hebrew faith/calendar. */ 47bbb76c12SGreg Roach return I18N::translate('Yahrzeiten'); 488c2e8227SGreg Roach } 498c2e8227SGreg Roach 508c2e8227SGreg Roach /** {@inheritdoc} */ 518f53f488SRico Sonntag public function getDescription(): string 52c1010edaSGreg Roach { 53bbb76c12SGreg Roach /* I18N: Description of the “Yahrzeiten” module. A “Hebrew death” is a death where the date is recorded in the Hebrew calendar. */ 54bbb76c12SGreg Roach return I18N::translate('A list of the Hebrew death anniversaries that will occur in the near future.'); 558c2e8227SGreg Roach } 568c2e8227SGreg Roach 5776692c8bSGreg Roach /** 5876692c8bSGreg Roach * Generate the HTML content of this block. 5976692c8bSGreg Roach * 60e490cd80SGreg Roach * @param Tree $tree 6176692c8bSGreg Roach * @param int $block_id 6276692c8bSGreg Roach * @param bool $template 63727f238cSGreg Roach * @param string[] $cfg 6476692c8bSGreg Roach * 6576692c8bSGreg Roach * @return string 6676692c8bSGreg Roach */ 67c1010edaSGreg Roach public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 68c1010edaSGreg Roach { 69e490cd80SGreg Roach global $ctype; 708c2e8227SGreg Roach 71f0a11419SGreg Roach $calendar_service = new CalendarService(); 72f0a11419SGreg Roach 73e490cd80SGreg Roach $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 74c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 75c385536dSGreg Roach $calendar = $this->getBlockSetting($block_id, 'calendar', self::DEFAULT_CALENDAR); 768c2e8227SGreg Roach 77c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 788c2e8227SGreg Roach 7959f2f229SGreg Roach $jewish_calendar = new JewishCalendar(); 808c2e8227SGreg Roach $startjd = WT_CLIENT_JD; 818c2e8227SGreg Roach $endjd = WT_CLIENT_JD + $days - 1; 828c2e8227SGreg Roach 838c2e8227SGreg Roach // The standard anniversary rules cover most of the Yahrzeit rules, we just 848c2e8227SGreg Roach // need to handle a few special cases. 8589f721acSGreg Roach // Fetch normal anniversaries, with an extra day before/after 8613abd6f3SGreg Roach $yahrzeits = []; 878c2e8227SGreg Roach for ($jd = $startjd - 1; $jd <= $endjd + $days; ++$jd) { 88f0a11419SGreg Roach foreach ($calendar_service->getAnniversaryEvents($jd, 'DEAT _YART', $tree) as $fact) { 898c2e8227SGreg Roach // Exact hebrew dates only 908c2e8227SGreg Roach $date = $fact->getDate(); 918c2e8227SGreg Roach if ($date->minimumDate() instanceof JewishDate && $date->minimumJulianDay() === $date->maximumJulianDay()) { 9289f721acSGreg Roach // ...then adjust DEAT dates (but not _YART) 9389f721acSGreg Roach if ($fact->getTag() === 'DEAT') { 9489f721acSGreg Roach $today = new JewishDate($jd); 9589f721acSGreg Roach $hd = $fact->getDate()->minimumDate(); 968c2e8227SGreg Roach $hd1 = new JewishDate($hd); 97*4a83f5d7SGreg Roach $hd1->year += 1; 988c2e8227SGreg Roach $hd1->setJdFromYmd(); 998c2e8227SGreg Roach // Special rules. See http://www.hebcal.com/help/anniv.html 1008c2e8227SGreg Roach // Everything else is taken care of by our standard anniversary rules. 101*4a83f5d7SGreg Roach if ($hd->day == 30 && $hd->month == 2 && $hd->year != 0 && $hd1->daysInMonth() < 30) { 1028c2e8227SGreg Roach // 30 CSH - Last day in CSH 103*4a83f5d7SGreg Roach $jd = $jewish_calendar->ymdToJd($today->year, 3, 1) - 1; 104*4a83f5d7SGreg Roach } elseif ($hd->day == 30 && $hd->month == 3 && $hd->year != 0 && $hd1->daysInMonth() < 30) { 1058c2e8227SGreg Roach // 30 KSL - Last day in KSL 106*4a83f5d7SGreg Roach $jd = $jewish_calendar->ymdToJd($today->year, 4, 1) - 1; 107*4a83f5d7SGreg Roach } elseif ($hd->day == 30 && $hd->month == 6 && $hd->year != 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) { 1088c2e8227SGreg Roach // 30 ADR - Last day in SHV 109*4a83f5d7SGreg Roach $jd = $jewish_calendar->ymdToJd($today->year, 6, 1) - 1; 11089f721acSGreg Roach } 11189f721acSGreg Roach } 11289f721acSGreg Roach 11389f721acSGreg Roach // Filter adjusted dates to our date range 11489f721acSGreg Roach if ($jd >= $startjd && $jd < $startjd + $days) { 11589f721acSGreg Roach // upcomming yahrzeit dates 11689f721acSGreg Roach switch ($calendar) { 11789f721acSGreg Roach case 'gregorian': 11889f721acSGreg Roach $yahrzeit_date = new GregorianDate($jd); 11989f721acSGreg Roach break; 12089f721acSGreg Roach case 'jewish': 12189f721acSGreg Roach default: 12289f721acSGreg Roach $yahrzeit_date = new JewishDate($jd); 12389f721acSGreg Roach break; 12489f721acSGreg Roach } 12589f721acSGreg Roach $yahrzeit_date = new Date($yahrzeit_date->format('%@ %A %O %E')); 12689f721acSGreg Roach 12789f721acSGreg Roach $yahrzeits[] = (object) [ 12889f721acSGreg Roach 'individual' => $fact->getParent(), 12989f721acSGreg Roach 'fact_date' => $fact->getDate(), 13089f721acSGreg Roach 'fact' => $fact, 13189f721acSGreg Roach 'jd' => $jd, 13289f721acSGreg Roach 'yahrzeit_date' => $yahrzeit_date, 13389f721acSGreg Roach ]; 13489f721acSGreg Roach } 1358c2e8227SGreg Roach } 1368c2e8227SGreg Roach } 1378c2e8227SGreg Roach } 1388c2e8227SGreg Roach 1398c2e8227SGreg Roach switch ($infoStyle) { 1408c2e8227SGreg Roach case 'list': 141147e99aaSGreg Roach $content = view('modules/yahrzeit/list', [ 14289f721acSGreg Roach 'yahrzeits' => $yahrzeits, 14389f721acSGreg Roach ]); 1448c2e8227SGreg Roach break; 1458c2e8227SGreg Roach case 'table': 1468c2e8227SGreg Roach default: 147147e99aaSGreg Roach $content = view('modules/yahrzeit/table', [ 14889f721acSGreg Roach 'yahrzeits' => $yahrzeits, 14989f721acSGreg Roach ]); 1508c2e8227SGreg Roach break; 1518c2e8227SGreg Roach } 1528c2e8227SGreg Roach 1538c2e8227SGreg Roach if ($template) { 154e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 155c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 156c1010edaSGreg Roach 'block_id' => $block_id, 157c1010edaSGreg Roach 'ged' => $tree->getName(), 158c1010edaSGreg Roach ]); 159397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 160c1010edaSGreg Roach $config_url = route('user-page-block-edit', [ 161c1010edaSGreg Roach 'block_id' => $block_id, 162c1010edaSGreg Roach 'ged' => $tree->getName(), 163c1010edaSGreg Roach ]); 1648cbbfdceSGreg Roach } else { 1658cbbfdceSGreg Roach $config_url = ''; 1668cbbfdceSGreg Roach } 1678cbbfdceSGreg Roach 168147e99aaSGreg Roach return view('modules/block-template', [ 1699c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1709c6524dcSGreg Roach 'id' => $block_id, 171f7f4b984SGreg Roach 'config_url' => $config_url, 1729c6524dcSGreg Roach 'title' => $this->getTitle(), 1739c6524dcSGreg Roach 'content' => $content, 1749c6524dcSGreg Roach ]); 1758c2e8227SGreg Roach } 176b2ce94c6SRico Sonntag 177b2ce94c6SRico Sonntag return $content; 1788c2e8227SGreg Roach } 1798c2e8227SGreg Roach 1808c2e8227SGreg Roach /** {@inheritdoc} */ 181c1010edaSGreg Roach public function loadAjax(): bool 182c1010edaSGreg Roach { 1838c2e8227SGreg Roach return true; 1848c2e8227SGreg Roach } 1858c2e8227SGreg Roach 1868c2e8227SGreg Roach /** {@inheritdoc} */ 187c1010edaSGreg Roach public function isUserBlock(): bool 188c1010edaSGreg Roach { 1898c2e8227SGreg Roach return true; 1908c2e8227SGreg Roach } 1918c2e8227SGreg Roach 1928c2e8227SGreg Roach /** {@inheritdoc} */ 193c1010edaSGreg Roach public function isGedcomBlock(): bool 194c1010edaSGreg Roach { 1958c2e8227SGreg Roach return true; 1968c2e8227SGreg Roach } 1978c2e8227SGreg Roach 19876692c8bSGreg Roach /** 199a45f9889SGreg Roach * Update the configuration for a block. 200a45f9889SGreg Roach * 201a45f9889SGreg Roach * @param Request $request 202a45f9889SGreg Roach * @param int $block_id 203a45f9889SGreg Roach * 204a45f9889SGreg Roach * @return void 205a45f9889SGreg Roach */ 206a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 207a45f9889SGreg Roach { 208a45f9889SGreg Roach $this->setBlockSetting($block_id, 'days', $request->get('days', self::DEFAULT_DAYS)); 209a45f9889SGreg Roach $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE)); 210a45f9889SGreg Roach $this->setBlockSetting($block_id, 'calendar', $request->get('calendar', self::DEFAULT_CALENDAR)); 211a45f9889SGreg Roach } 212a45f9889SGreg Roach 213a45f9889SGreg Roach /** 21476692c8bSGreg Roach * An HTML form to edit block settings 21576692c8bSGreg Roach * 216e490cd80SGreg Roach * @param Tree $tree 21776692c8bSGreg Roach * @param int $block_id 218a9430be8SGreg Roach * 219a9430be8SGreg Roach * @return void 22076692c8bSGreg Roach */ 221a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 222c1010edaSGreg Roach { 223e2a378d3SGreg Roach $calendar = $this->getBlockSetting($block_id, 'calendar', 'jewish'); 224147e99aaSGreg Roach $days = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 225c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table'); 2268c2e8227SGreg Roach 227c385536dSGreg Roach $styles = [ 228bbb76c12SGreg Roach /* I18N: An option in a list-box */ 229bbb76c12SGreg Roach 'list' => I18N::translate('list'), 230bbb76c12SGreg Roach /* I18N: An option in a list-box */ 231bbb76c12SGreg Roach 'table' => I18N::translate('table'), 232c385536dSGreg Roach ]; 2338c2e8227SGreg Roach 234c385536dSGreg Roach $calendars = [ 235c385536dSGreg Roach 'jewish' => I18N::translate('Jewish'), 236c385536dSGreg Roach 'gregorian' => I18N::translate('Gregorian'), 237c385536dSGreg Roach ]; 2388c2e8227SGreg Roach 239147e99aaSGreg Roach echo view('modules/yahrzeit/config', [ 240c385536dSGreg Roach 'calendar' => $calendar, 241c385536dSGreg Roach 'calendars' => $calendars, 242c385536dSGreg Roach 'days' => $days, 243c385536dSGreg Roach 'infoStyle' => $infoStyle, 244c385536dSGreg Roach 'max_days' => self::MAX_DAYS, 245c385536dSGreg Roach 'styles' => $styles, 246c385536dSGreg Roach ]); 2478c2e8227SGreg Roach } 2488c2e8227SGreg Roach} 249