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