1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\ExtCalendar\JewishCalendar; 23use Fisharebest\Webtrees\Date; 24use Fisharebest\Webtrees\Date\GregorianDate; 25use Fisharebest\Webtrees\Date\JewishDate; 26use Fisharebest\Webtrees\I18N; 27use Fisharebest\Webtrees\Registry; 28use Fisharebest\Webtrees\Services\CalendarService; 29use Fisharebest\Webtrees\Tree; 30use Fisharebest\Webtrees\Validator; 31use Illuminate\Support\Collection; 32use Illuminate\Support\Str; 33use Psr\Http\Message\ServerRequestInterface; 34 35use function extract; 36use function view; 37 38use const EXTR_OVERWRITE; 39 40/** 41 * Class YahrzeitModule 42 */ 43class YahrzeitModule extends AbstractModule implements ModuleBlockInterface 44{ 45 use ModuleBlockTrait; 46 47 // Default values for new blocks. 48 private const DEFAULT_CALENDAR = 'jewish'; 49 private const DEFAULT_DAYS = '7'; 50 private const DEFAULT_STYLE = 'table'; 51 52 // Can show this number of days into the future. 53 private const MAX_DAYS = 30; 54 55 // Pagination 56 private const LIMIT_LOW = 10; 57 private const LIMIT_HIGH = 20; 58 59 /** 60 * How should this module be identified in the control panel, etc.? 61 * 62 * @return string 63 */ 64 public function title(): string 65 { 66 /* I18N: Name of a module. Yahrzeiten (the plural of Yahrzeit) are special anniversaries of deaths in the Hebrew faith/calendar. */ 67 return I18N::translate('Yahrzeiten'); 68 } 69 70 public function description(): string 71 { 72 /* I18N: Description of the “Yahrzeiten” module. A “Hebrew death” is a death where the date is recorded in the Hebrew calendar. */ 73 return I18N::translate('A list of the Hebrew death anniversaries that will occur in the near future.'); 74 } 75 76 /** 77 * Generate the HTML content of this block. 78 * 79 * @param Tree $tree 80 * @param int $block_id 81 * @param string $context 82 * @param array<string,string> $config 83 * 84 * @return string 85 */ 86 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 87 { 88 $calendar_service = new CalendarService(); 89 90 $days = (int) $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 91 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 92 $calendar = $this->getBlockSetting($block_id, 'calendar', self::DEFAULT_CALENDAR); 93 94 extract($config, EXTR_OVERWRITE); 95 96 $jewish_calendar = new JewishCalendar(); 97 $startjd = Registry::timestampFactory()->now()->julianDay(); 98 $endjd = Registry::timestampFactory()->now()->addDays($days - 1)->julianDay(); 99 100 // The standard anniversary rules cover most of the Yahrzeit rules, we just 101 // need to handle a few special cases. 102 // Fetch normal anniversaries, with an extra day before/after 103 $yahrzeits = new Collection(); 104 for ($jd = $startjd - 1; $jd <= $endjd + $days; ++$jd) { 105 foreach ($calendar_service->getAnniversaryEvents($jd, 'DEAT _YART', $tree) as $fact) { 106 // Exact hebrew dates only 107 $date = $fact->date(); 108 if ($date->minimumDate() instanceof JewishDate && $date->minimumJulianDay() === $date->maximumJulianDay()) { 109 $jd_yahrtzeit = $jd; 110 // ...then adjust DEAT dates (but not _YART) 111 if ($fact->tag() === 'INDI:DEAT') { 112 $today = new JewishDate($jd); 113 $hd = $fact->date()->minimumDate(); 114 $hd1 = new JewishDate($hd); 115 ++$hd1->year; 116 $hd1->setJdFromYmd(); 117 // Special rules. See https://www.hebcal.com/help/anniv.html 118 // Everything else is taken care of by our standard anniversary rules. 119 if ($hd->day === 30 && $hd->month === 2 && $hd->year !== 0 && $hd1->daysInMonth() < 30) { 120 // 30 CSH - Last day in CSH 121 $jd_yahrtzeit = $jewish_calendar->ymdToJd($today->year, 3, 1) - 1; 122 } elseif ($hd->day === 30 && $hd->month === 3 && $hd->year !== 0 && $hd1->daysInMonth() < 30) { 123 // 30 KSL - Last day in KSL 124 $jd_yahrtzeit = $jewish_calendar->ymdToJd($today->year, 4, 1) - 1; 125 } elseif ($hd->day === 30 && $hd->month === 6 && $hd->year !== 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) { 126 // 30 ADR - Last day in SHV 127 $jd_yahrtzeit = $jewish_calendar->ymdToJd($today->year, 6, 1) - 1; 128 } 129 } 130 131 // Filter adjusted dates to our date range 132 if ($jd_yahrtzeit >= $startjd && $jd_yahrtzeit < $startjd + $days) { 133 // upcoming yahrzeit dates 134 switch ($calendar) { 135 case 'gregorian': 136 $yahrzeit_calendar_date = new GregorianDate($jd_yahrtzeit); 137 break; 138 case 'jewish': 139 default: 140 $yahrzeit_calendar_date = new JewishDate($jd_yahrtzeit); 141 break; 142 } 143 $yahrzeit_date = new Date($yahrzeit_calendar_date->format('%@ %A %O %E')); 144 145 $yahrzeits->add((object) [ 146 'individual' => $fact->record(), 147 'fact_date' => $fact->date(), 148 'fact' => $fact, 149 'yahrzeit_date' => $yahrzeit_date, 150 ]); 151 } 152 } 153 } 154 } 155 156 switch ($infoStyle) { 157 case 'list': 158 $content = view('modules/yahrzeit/list', [ 159 'id' => $block_id, 160 'limit_low' => self::LIMIT_LOW, 161 'limit_high' => self::LIMIT_HIGH, 162 'yahrzeits' => $yahrzeits, 163 ]); 164 break; 165 case 'table': 166 default: 167 $content = view('modules/yahrzeit/table', [ 168 'limit_low' => self::LIMIT_LOW, 169 'limit_high' => self::LIMIT_HIGH, 170 'yahrzeits' => $yahrzeits, 171 ]); 172 break; 173 } 174 175 if ($context !== self::CONTEXT_EMBED) { 176 return view('modules/block-template', [ 177 'block' => Str::kebab($this->name()), 178 'id' => $block_id, 179 'config_url' => $this->configUrl($tree, $context, $block_id), 180 'title' => $this->title(), 181 'content' => $content, 182 ]); 183 } 184 185 return $content; 186 } 187 188 /** 189 * Should this block load asynchronously using AJAX? 190 * 191 * Simple blocks are faster in-line, more complex ones can be loaded later. 192 * 193 * @return bool 194 */ 195 public function loadAjax(): bool 196 { 197 return true; 198 } 199 200 /** 201 * Can this block be shown on the user’s home page? 202 * 203 * @return bool 204 */ 205 public function isUserBlock(): bool 206 { 207 return true; 208 } 209 210 /** 211 * Can this block be shown on the tree’s home page? 212 * 213 * @return bool 214 */ 215 public function isTreeBlock(): bool 216 { 217 return true; 218 } 219 220 /** 221 * Update the configuration for a block. 222 * 223 * @param ServerRequestInterface $request 224 * @param int $block_id 225 * 226 * @return void 227 */ 228 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 229 { 230 $days = Validator::parsedBody($request)->string('days', self::DEFAULT_DAYS); 231 $info_style = Validator::parsedBody($request)->string('infoStyle', self::DEFAULT_STYLE); 232 $calendar = Validator::parsedBody($request)->string('calendar', self::DEFAULT_CALENDAR); 233 234 $this->setBlockSetting($block_id, 'days', $days); 235 $this->setBlockSetting($block_id, 'infoStyle', $info_style); 236 $this->setBlockSetting($block_id, 'calendar', $calendar); 237 } 238 239 /** 240 * An HTML form to edit block settings 241 * 242 * @param Tree $tree 243 * @param int $block_id 244 * 245 * @return string 246 */ 247 public function editBlockConfiguration(Tree $tree, int $block_id): string 248 { 249 $calendar = $this->getBlockSetting($block_id, 'calendar', 'jewish'); 250 $days = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 251 $info_style = $this->getBlockSetting($block_id, 'infoStyle', 'table'); 252 253 $styles = [ 254 /* I18N: An option in a list-box */ 255 'list' => I18N::translate('list'), 256 /* I18N: An option in a list-box */ 257 'table' => I18N::translate('table'), 258 ]; 259 260 $calendars = [ 261 'jewish' => I18N::translate('Jewish'), 262 'gregorian' => I18N::translate('Gregorian'), 263 ]; 264 265 return view('modules/yahrzeit/config', [ 266 'calendar' => $calendar, 267 'calendars' => $calendars, 268 'days' => $days, 269 'info_style' => $info_style, 270 'max_days' => self::MAX_DAYS, 271 'styles' => $styles, 272 ]); 273 } 274} 275