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