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; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 25*f0a11419SGreg Roachuse Fisharebest\Webtrees\Services\CalendarService; 26e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 278c2e8227SGreg Roach 288c2e8227SGreg Roach/** 298c2e8227SGreg Roach * Class YahrzeitModule 308c2e8227SGreg Roach */ 31c1010edaSGreg Roachclass YahrzeitModule extends AbstractModule implements ModuleBlockInterface 32c1010edaSGreg Roach{ 33c385536dSGreg Roach // Default values for new blocks. 34c385536dSGreg Roach const DEFAULT_CALENDAR = 'jewish'; 35c385536dSGreg Roach const DEFAULT_DAYS = 7; 36c385536dSGreg Roach const DEFAULT_STYLE = 'table'; 37c385536dSGreg Roach 38c385536dSGreg Roach // Can show this number of days into the future. 39c385536dSGreg Roach const MAX_DAYS = 30; 40c385536dSGreg Roach 418c2e8227SGreg Roach /** {@inheritdoc} */ 42c1010edaSGreg Roach public function getTitle() 43c1010edaSGreg Roach { 44bbb76c12SGreg Roach /* I18N: Name of a module. Yahrzeiten (the plural of Yahrzeit) are special anniversaries of deaths in the Hebrew faith/calendar. */ 45bbb76c12SGreg Roach return I18N::translate('Yahrzeiten'); 468c2e8227SGreg Roach } 478c2e8227SGreg Roach 488c2e8227SGreg Roach /** {@inheritdoc} */ 49c1010edaSGreg Roach public function getDescription() 50c1010edaSGreg Roach { 51bbb76c12SGreg Roach /* I18N: Description of the “Yahrzeiten” module. A “Hebrew death” is a death where the date is recorded in the Hebrew calendar. */ 52bbb76c12SGreg Roach return I18N::translate('A list of the Hebrew death anniversaries that will occur in the near future.'); 538c2e8227SGreg Roach } 548c2e8227SGreg Roach 5576692c8bSGreg Roach /** 5676692c8bSGreg Roach * Generate the HTML content of this block. 5776692c8bSGreg Roach * 58e490cd80SGreg Roach * @param Tree $tree 5976692c8bSGreg Roach * @param int $block_id 6076692c8bSGreg Roach * @param bool $template 61727f238cSGreg Roach * @param string[] $cfg 6276692c8bSGreg Roach * 6376692c8bSGreg Roach * @return string 6476692c8bSGreg Roach */ 65c1010edaSGreg Roach public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 66c1010edaSGreg Roach { 67e490cd80SGreg Roach global $ctype; 688c2e8227SGreg Roach 69*f0a11419SGreg Roach $calendar_service = new CalendarService(); 70*f0a11419SGreg Roach 71e490cd80SGreg Roach $days = (int)$this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 72c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 73c385536dSGreg Roach $calendar = $this->getBlockSetting($block_id, 'calendar', self::DEFAULT_CALENDAR); 748c2e8227SGreg Roach 75c385536dSGreg Roach extract($cfg, EXTR_OVERWRITE); 768c2e8227SGreg Roach 7759f2f229SGreg Roach $jewish_calendar = new JewishCalendar(); 788c2e8227SGreg Roach $startjd = WT_CLIENT_JD; 798c2e8227SGreg Roach $endjd = WT_CLIENT_JD + $days - 1; 808c2e8227SGreg Roach 818c2e8227SGreg Roach // The standard anniversary rules cover most of the Yahrzeit rules, we just 828c2e8227SGreg Roach // need to handle a few special cases. 8389f721acSGreg Roach // Fetch normal anniversaries, with an extra day before/after 8413abd6f3SGreg Roach $yahrzeits = []; 858c2e8227SGreg Roach for ($jd = $startjd - 1; $jd <= $endjd + $days; ++$jd) { 86*f0a11419SGreg Roach foreach ($calendar_service->getAnniversaryEvents($jd, 'DEAT _YART', $tree) as $fact) { 878c2e8227SGreg Roach // Exact hebrew dates only 888c2e8227SGreg Roach $date = $fact->getDate(); 898c2e8227SGreg Roach if ($date->minimumDate() instanceof JewishDate && $date->minimumJulianDay() === $date->maximumJulianDay()) { 9089f721acSGreg Roach // ...then adjust DEAT dates (but not _YART) 9189f721acSGreg Roach if ($fact->getTag() === 'DEAT') { 9289f721acSGreg Roach $today = new JewishDate($jd); 9389f721acSGreg Roach $hd = $fact->getDate()->minimumDate(); 948c2e8227SGreg Roach $hd1 = new JewishDate($hd); 958c2e8227SGreg Roach $hd1->y += 1; 968c2e8227SGreg Roach $hd1->setJdFromYmd(); 978c2e8227SGreg Roach // Special rules. See http://www.hebcal.com/help/anniv.html 988c2e8227SGreg Roach // Everything else is taken care of by our standard anniversary rules. 998c2e8227SGreg Roach if ($hd->d == 30 && $hd->m == 2 && $hd->y != 0 && $hd1->daysInMonth() < 30) { 1008c2e8227SGreg Roach // 30 CSH - Last day in CSH 10189f721acSGreg Roach $jd = $jewish_calendar->ymdToJd($today->y, 3, 1) - 1; 1028c2e8227SGreg Roach } elseif ($hd->d == 30 && $hd->m == 3 && $hd->y != 0 && $hd1->daysInMonth() < 30) { 1038c2e8227SGreg Roach // 30 KSL - Last day in KSL 10489f721acSGreg Roach $jd = $jewish_calendar->ymdToJd($today->y, 4, 1) - 1; 1058c2e8227SGreg Roach } elseif ($hd->d == 30 && $hd->m == 6 && $hd->y != 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) { 1068c2e8227SGreg Roach // 30 ADR - Last day in SHV 10789f721acSGreg Roach $jd = $jewish_calendar->ymdToJd($today->y, 6, 1) - 1; 10889f721acSGreg Roach } 10989f721acSGreg Roach } 11089f721acSGreg Roach 11189f721acSGreg Roach // Filter adjusted dates to our date range 11289f721acSGreg Roach if ($jd >= $startjd && $jd < $startjd + $days) { 11389f721acSGreg Roach // upcomming yahrzeit dates 11489f721acSGreg Roach switch ($calendar) { 11589f721acSGreg Roach case 'gregorian': 11689f721acSGreg Roach $yahrzeit_date = new GregorianDate($jd); 11789f721acSGreg Roach break; 11889f721acSGreg Roach case 'jewish': 11989f721acSGreg Roach default: 12089f721acSGreg Roach $yahrzeit_date = new JewishDate($jd); 12189f721acSGreg Roach break; 12289f721acSGreg Roach } 12389f721acSGreg Roach $yahrzeit_date = new Date($yahrzeit_date->format('%@ %A %O %E')); 12489f721acSGreg Roach 12589f721acSGreg Roach $yahrzeits[] = (object)[ 12689f721acSGreg Roach 'individual' => $fact->getParent(), 12789f721acSGreg Roach 'fact_date' => $fact->getDate(), 12889f721acSGreg Roach 'fact' => $fact, 12989f721acSGreg Roach 'jd' => $jd, 13089f721acSGreg Roach 'yahrzeit_date' => $yahrzeit_date, 13189f721acSGreg Roach ]; 13289f721acSGreg Roach } 1338c2e8227SGreg Roach } 1348c2e8227SGreg Roach } 1358c2e8227SGreg Roach } 1368c2e8227SGreg Roach 1378c2e8227SGreg Roach switch ($infoStyle) { 1388c2e8227SGreg Roach case 'list': 139147e99aaSGreg Roach $content = view('modules/yahrzeit/list', [ 14089f721acSGreg Roach 'yahrzeits' => $yahrzeits, 14189f721acSGreg Roach ]); 1428c2e8227SGreg Roach break; 1438c2e8227SGreg Roach case 'table': 1448c2e8227SGreg Roach default: 145147e99aaSGreg Roach $content = view('modules/yahrzeit/table', [ 14689f721acSGreg Roach 'yahrzeits' => $yahrzeits, 14789f721acSGreg Roach ]); 1488c2e8227SGreg Roach break; 1498c2e8227SGreg Roach } 1508c2e8227SGreg Roach 1518c2e8227SGreg Roach if ($template) { 152e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 153c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 154c1010edaSGreg Roach 'block_id' => $block_id, 155c1010edaSGreg Roach 'ged' => $tree->getName(), 156c1010edaSGreg Roach ]); 157397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 158c1010edaSGreg Roach $config_url = route('user-page-block-edit', [ 159c1010edaSGreg Roach 'block_id' => $block_id, 160c1010edaSGreg Roach 'ged' => $tree->getName(), 161c1010edaSGreg Roach ]); 1628cbbfdceSGreg Roach } else { 1638cbbfdceSGreg Roach $config_url = ''; 1648cbbfdceSGreg Roach } 1658cbbfdceSGreg Roach 166147e99aaSGreg Roach return view('modules/block-template', [ 1679c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1689c6524dcSGreg Roach 'id' => $block_id, 169f7f4b984SGreg Roach 'config_url' => $config_url, 1709c6524dcSGreg Roach 'title' => $this->getTitle(), 1719c6524dcSGreg Roach 'content' => $content, 1729c6524dcSGreg Roach ]); 1738c2e8227SGreg Roach } else { 1748c2e8227SGreg Roach return $content; 1758c2e8227SGreg Roach } 1768c2e8227SGreg Roach } 1778c2e8227SGreg Roach 1788c2e8227SGreg Roach /** {@inheritdoc} */ 179c1010edaSGreg Roach public function loadAjax(): bool 180c1010edaSGreg Roach { 1818c2e8227SGreg Roach return true; 1828c2e8227SGreg Roach } 1838c2e8227SGreg Roach 1848c2e8227SGreg Roach /** {@inheritdoc} */ 185c1010edaSGreg Roach public function isUserBlock(): bool 186c1010edaSGreg Roach { 1878c2e8227SGreg Roach return true; 1888c2e8227SGreg Roach } 1898c2e8227SGreg Roach 1908c2e8227SGreg Roach /** {@inheritdoc} */ 191c1010edaSGreg Roach public function isGedcomBlock(): bool 192c1010edaSGreg Roach { 1938c2e8227SGreg Roach return true; 1948c2e8227SGreg Roach } 1958c2e8227SGreg Roach 19676692c8bSGreg Roach /** 19776692c8bSGreg Roach * An HTML form to edit block settings 19876692c8bSGreg Roach * 199e490cd80SGreg Roach * @param Tree $tree 20076692c8bSGreg Roach * @param int $block_id 201a9430be8SGreg Roach * 202a9430be8SGreg Roach * @return void 20376692c8bSGreg Roach */ 204c1010edaSGreg Roach public function configureBlock(Tree $tree, int $block_id) 205c1010edaSGreg Roach { 206c385536dSGreg Roach if ($_SERVER['REQUEST_METHOD'] === 'POST') { 207c385536dSGreg Roach $this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, self::MAX_DAYS, self::DEFAULT_DAYS)); 208c385536dSGreg Roach $this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', self::DEFAULT_STYLE)); 209c385536dSGreg Roach $this->setBlockSetting($block_id, 'calendar', Filter::post('calendar', 'jewish|gregorian', self::DEFAULT_CALENDAR)); 210c385536dSGreg Roach 211c385536dSGreg Roach return; 2128c2e8227SGreg Roach } 2138c2e8227SGreg Roach 214e2a378d3SGreg Roach $calendar = $this->getBlockSetting($block_id, 'calendar', 'jewish'); 215147e99aaSGreg Roach $days = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 216c385536dSGreg Roach $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table'); 2178c2e8227SGreg Roach 218c385536dSGreg Roach $styles = [ 219bbb76c12SGreg Roach /* I18N: An option in a list-box */ 220bbb76c12SGreg Roach 'list' => I18N::translate('list'), 221bbb76c12SGreg Roach /* I18N: An option in a list-box */ 222bbb76c12SGreg Roach 'table' => I18N::translate('table'), 223c385536dSGreg Roach ]; 2248c2e8227SGreg Roach 225c385536dSGreg Roach $calendars = [ 226c385536dSGreg Roach 'jewish' => I18N::translate('Jewish'), 227c385536dSGreg Roach 'gregorian' => I18N::translate('Gregorian'), 228c385536dSGreg Roach ]; 2298c2e8227SGreg Roach 230147e99aaSGreg Roach echo view('modules/yahrzeit/config', [ 231c385536dSGreg Roach 'calendar' => $calendar, 232c385536dSGreg Roach 'calendars' => $calendars, 233c385536dSGreg Roach 'days' => $days, 234c385536dSGreg Roach 'infoStyle' => $infoStyle, 235c385536dSGreg Roach 'max_days' => self::MAX_DAYS, 236c385536dSGreg Roach 'styles' => $styles, 237c385536dSGreg Roach ]); 2388c2e8227SGreg Roach } 2398c2e8227SGreg Roach} 240