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