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 */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 178c2e8227SGreg Roach 188c2e8227SGreg Roachuse Fisharebest\ExtCalendar\JewishCalendar; 190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date; 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter; 243d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDb; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 268c2e8227SGreg Roach 278c2e8227SGreg Roach/** 288c2e8227SGreg Roach * Class YahrzeitModule 298c2e8227SGreg Roach */ 30e2a378d3SGreg Roachclass YahrzeitModule extends AbstractModule implements ModuleBlockInterface { 31*c385536dSGreg Roach // Default values for new blocks. 32*c385536dSGreg Roach const DEFAULT_CALENDAR = 'jewish'; 33*c385536dSGreg Roach const DEFAULT_DAYS = 7; 34*c385536dSGreg Roach const DEFAULT_STYLE = 'table'; 35*c385536dSGreg Roach 36*c385536dSGreg Roach // Can show this number of days into the future. 37*c385536dSGreg Roach const MAX_DAYS = 30; 38*c385536dSGreg Roach 398c2e8227SGreg Roach /** {@inheritdoc} */ 408c2e8227SGreg Roach public function getTitle() { 418c2e8227SGreg Roach return /* I18N: Name of a module. Yahrzeiten (the plural of Yahrzeit) are special anniversaries of deaths in the Hebrew faith/calendar. */ I18N::translate('Yahrzeiten'); 428c2e8227SGreg Roach } 438c2e8227SGreg Roach 448c2e8227SGreg Roach /** {@inheritdoc} */ 458c2e8227SGreg Roach public function getDescription() { 468c2e8227SGreg Roach return /* I18N: Description of the “Yahrzeiten” module. A “Hebrew death” is a death where the date is recorded in the Hebrew calendar. */ I18N::translate('A list of the Hebrew death anniversaries that will occur in the near future.'); 478c2e8227SGreg Roach } 488c2e8227SGreg Roach 4976692c8bSGreg Roach /** 5076692c8bSGreg Roach * Generate the HTML content of this block. 5176692c8bSGreg Roach * 5276692c8bSGreg Roach * @param int $block_id 5376692c8bSGreg Roach * @param bool $template 54727f238cSGreg Roach * @param string[] $cfg 5576692c8bSGreg Roach * 5676692c8bSGreg Roach * @return string 5776692c8bSGreg Roach */ 58a9430be8SGreg Roach public function getBlock($block_id, $template = true, $cfg = []): string { 5960084bb2SGreg Roach global $ctype, $WT_TREE; 608c2e8227SGreg Roach 61*c385536dSGreg Roach $days = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 62*c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 63*c385536dSGreg Roach $calendar = $this->getBlockSetting($block_id, 'calendar', self::DEFAULT_CALENDAR); 648c2e8227SGreg Roach 65*c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 668c2e8227SGreg Roach 6789f721acSGreg Roach $jewish_calendar = new JewishCalendar; 688c2e8227SGreg Roach $startjd = WT_CLIENT_JD; 698c2e8227SGreg Roach $endjd = WT_CLIENT_JD + $days - 1; 708c2e8227SGreg Roach 718c2e8227SGreg Roach // The standard anniversary rules cover most of the Yahrzeit rules, we just 728c2e8227SGreg Roach // need to handle a few special cases. 7389f721acSGreg Roach // Fetch normal anniversaries, with an extra day before/after 7413abd6f3SGreg Roach $yahrzeits = []; 758c2e8227SGreg Roach for ($jd = $startjd - 1; $jd <= $endjd + $days; ++$jd) { 763d7a8a4cSGreg Roach foreach (FunctionsDb::getAnniversaryEvents($jd, 'DEAT _YART', $WT_TREE) as $fact) { 778c2e8227SGreg Roach // Exact hebrew dates only 788c2e8227SGreg Roach $date = $fact->getDate(); 798c2e8227SGreg Roach if ($date->minimumDate() instanceof JewishDate && $date->minimumJulianDay() === $date->maximumJulianDay()) { 808c2e8227SGreg Roach 8189f721acSGreg Roach // ...then adjust DEAT dates (but not _YART) 8289f721acSGreg Roach if ($fact->getTag() === 'DEAT') { 8389f721acSGreg Roach $today = new JewishDate($jd); 8489f721acSGreg Roach $hd = $fact->getDate()->minimumDate(); 858c2e8227SGreg Roach $hd1 = new JewishDate($hd); 868c2e8227SGreg Roach $hd1->y += 1; 878c2e8227SGreg Roach $hd1->setJdFromYmd(); 888c2e8227SGreg Roach // Special rules. See http://www.hebcal.com/help/anniv.html 898c2e8227SGreg Roach // Everything else is taken care of by our standard anniversary rules. 908c2e8227SGreg Roach if ($hd->d == 30 && $hd->m == 2 && $hd->y != 0 && $hd1->daysInMonth() < 30) { 918c2e8227SGreg Roach // 30 CSH - Last day in CSH 9289f721acSGreg Roach $jd = $jewish_calendar->ymdToJd($today->y, 3, 1) - 1; 938c2e8227SGreg Roach } elseif ($hd->d == 30 && $hd->m == 3 && $hd->y != 0 && $hd1->daysInMonth() < 30) { 948c2e8227SGreg Roach // 30 KSL - Last day in KSL 9589f721acSGreg Roach $jd = $jewish_calendar->ymdToJd($today->y, 4, 1) - 1; 968c2e8227SGreg Roach } elseif ($hd->d == 30 && $hd->m == 6 && $hd->y != 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) { 978c2e8227SGreg Roach // 30 ADR - Last day in SHV 9889f721acSGreg Roach $jd = $jewish_calendar->ymdToJd($today->y, 6, 1) - 1; 9989f721acSGreg Roach } 10089f721acSGreg Roach } 10189f721acSGreg Roach 10289f721acSGreg Roach // Filter adjusted dates to our date range 10389f721acSGreg Roach if ($jd >= $startjd && $jd < $startjd + $days) { 10489f721acSGreg Roach // upcomming yahrzeit dates 10589f721acSGreg Roach switch ($calendar) { 10689f721acSGreg Roach case 'gregorian': 10789f721acSGreg Roach $yahrzeit_date = new GregorianDate($jd); 10889f721acSGreg Roach break; 10989f721acSGreg Roach case 'jewish': 11089f721acSGreg Roach default: 11189f721acSGreg Roach $yahrzeit_date = new JewishDate($jd); 11289f721acSGreg Roach break; 11389f721acSGreg Roach } 11489f721acSGreg Roach $yahrzeit_date = new Date($yahrzeit_date->format('%@ %A %O %E')); 11589f721acSGreg Roach 11689f721acSGreg Roach $yahrzeits[] = (object) [ 11789f721acSGreg Roach 'individual' => $fact->getParent(), 11889f721acSGreg Roach 'fact_date' => $fact->getDate(), 11989f721acSGreg Roach 'fact' => $fact, 12089f721acSGreg Roach 'jd' => $jd, 12189f721acSGreg Roach 'yahrzeit_date' => $yahrzeit_date, 12289f721acSGreg Roach ]; 12389f721acSGreg Roach } 1248c2e8227SGreg Roach } 1258c2e8227SGreg Roach } 1268c2e8227SGreg Roach } 1278c2e8227SGreg Roach 1288c2e8227SGreg Roach switch ($infoStyle) { 1298c2e8227SGreg Roach case 'list': 13089f721acSGreg Roach $content = view('blocks/yahrzeit-list', [ 13189f721acSGreg Roach 'yahrzeits' => $yahrzeits, 13289f721acSGreg Roach ]); 1338c2e8227SGreg Roach break; 1348c2e8227SGreg Roach case 'table': 1358c2e8227SGreg Roach default: 13689f721acSGreg Roach $content = view('blocks/yahrzeit-table', [ 13789f721acSGreg Roach 'yahrzeits' => $yahrzeits, 13889f721acSGreg Roach ]); 1398c2e8227SGreg Roach break; 1408c2e8227SGreg Roach } 1418c2e8227SGreg Roach 1428c2e8227SGreg Roach if ($template) { 143397e599aSGreg Roach if ($ctype === 'gedcom' && Auth::isManager($WT_TREE)) { 144397e599aSGreg Roach $config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 145397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 146397e599aSGreg Roach $config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 1478cbbfdceSGreg Roach } else { 1488cbbfdceSGreg Roach $config_url = ''; 1498cbbfdceSGreg Roach } 1508cbbfdceSGreg Roach 15189f721acSGreg Roach return view('blocks/template', [ 1529c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1539c6524dcSGreg Roach 'id' => $block_id, 154f7f4b984SGreg Roach 'config_url' => $config_url, 1559c6524dcSGreg Roach 'title' => $this->getTitle(), 1569c6524dcSGreg Roach 'content' => $content, 1579c6524dcSGreg Roach ]); 1588c2e8227SGreg Roach } else { 1598c2e8227SGreg Roach return $content; 1608c2e8227SGreg Roach } 1618c2e8227SGreg Roach } 1628c2e8227SGreg Roach 1638c2e8227SGreg Roach /** {@inheritdoc} */ 164a9430be8SGreg Roach public function loadAjax(): bool { 1658c2e8227SGreg Roach return true; 1668c2e8227SGreg Roach } 1678c2e8227SGreg Roach 1688c2e8227SGreg Roach /** {@inheritdoc} */ 169a9430be8SGreg Roach public function isUserBlock(): bool { 1708c2e8227SGreg Roach return true; 1718c2e8227SGreg Roach } 1728c2e8227SGreg Roach 1738c2e8227SGreg Roach /** {@inheritdoc} */ 174a9430be8SGreg Roach public function isGedcomBlock(): bool { 1758c2e8227SGreg Roach return true; 1768c2e8227SGreg Roach } 1778c2e8227SGreg Roach 17876692c8bSGreg Roach /** 17976692c8bSGreg Roach * An HTML form to edit block settings 18076692c8bSGreg Roach * 18176692c8bSGreg Roach * @param int $block_id 182a9430be8SGreg Roach * 183a9430be8SGreg Roach * @return void 18476692c8bSGreg Roach */ 185be9a728cSGreg Roach public function configureBlock($block_id) { 186*c385536dSGreg Roach if ($_SERVER['REQUEST_METHOD'] === 'POST') { 187*c385536dSGreg Roach $this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, self::MAX_DAYS, self::DEFAULT_DAYS)); 188*c385536dSGreg Roach $this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', self::DEFAULT_STYLE)); 189*c385536dSGreg Roach $this->setBlockSetting($block_id, 'calendar', Filter::post('calendar', 'jewish|gregorian', self::DEFAULT_CALENDAR)); 190*c385536dSGreg Roach 191*c385536dSGreg Roach return; 1928c2e8227SGreg Roach } 1938c2e8227SGreg Roach 194e2a378d3SGreg Roach $calendar = $this->getBlockSetting($block_id, 'calendar', 'jewish'); 195*c385536dSGreg Roach $days = $this->getBlockSetting($block_id, 'days', 'self::DEFAULT_DAYS'); 196*c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table'); 1978c2e8227SGreg Roach 198*c385536dSGreg Roach $styles = [ 199*c385536dSGreg Roach 'list' => /* I18N: An option in a list-box */ I18N::translate('list'), 200*c385536dSGreg Roach 'table' => /* I18N: An option in a list-box */ I18N::translate('table'), 201*c385536dSGreg Roach ]; 2028c2e8227SGreg Roach 203*c385536dSGreg Roach $calendars = [ 204*c385536dSGreg Roach 'jewish' => I18N::translate('Jewish'), 205*c385536dSGreg Roach 'gregorian' => I18N::translate('Gregorian'), 206*c385536dSGreg Roach ]; 2078c2e8227SGreg Roach 208*c385536dSGreg Roach echo view('blocks/yahrzeit-config', [ 209*c385536dSGreg Roach 'calendar' => $calendar, 210*c385536dSGreg Roach 'calendars' => $calendars, 211*c385536dSGreg Roach 'days' => $days, 212*c385536dSGreg Roach 'infoStyle' => $infoStyle, 213*c385536dSGreg Roach 'max_days' => self::MAX_DAYS, 214*c385536dSGreg Roach 'styles' => $styles, 215*c385536dSGreg Roach ]); 2168c2e8227SGreg Roach } 2178c2e8227SGreg Roach} 218