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