1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Module; 19 20use Fisharebest\ExtCalendar\JewishCalendar; 21use Fisharebest\Webtrees\Auth; 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 Symfony\Component\HttpFoundation\Request; 30 31/** 32 * Class YahrzeitModule 33 */ 34class YahrzeitModule extends AbstractModule implements ModuleBlockInterface 35{ 36 use ModuleBlockTrait; 37 38 // Default values for new blocks. 39 private const DEFAULT_CALENDAR = 'jewish'; 40 private const DEFAULT_DAYS = '7'; 41 private const DEFAULT_STYLE = 'table'; 42 43 // Can show this number of days into the future. 44 private const MAX_DAYS = 30; 45 46 /** 47 * How should this module be identified in the control panel, etc.? 48 * 49 * @return string 50 */ 51 public function title(): string 52 { 53 /* I18N: Name of a module. Yahrzeiten (the plural of Yahrzeit) are special anniversaries of deaths in the Hebrew faith/calendar. */ 54 return I18N::translate('Yahrzeiten'); 55 } 56 57 /** 58 * A sentence describing what this module does. 59 * 60 * @return string 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 $ctype 74 * @param string[] $cfg 75 * 76 * @return string 77 */ 78 public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): 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($cfg, EXTR_OVERWRITE); 87 88 $jewish_calendar = new JewishCalendar(); 89 $startjd = Carbon::now()->julianDay(); 90 $endjd = $startjd + $days - 1; 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 = []; 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 // ...then adjust DEAT dates (but not _YART) 102 if ($fact->getTag() === 'DEAT') { 103 $today = new JewishDate($jd); 104 $hd = $fact->date()->minimumDate(); 105 $hd1 = new JewishDate($hd); 106 $hd1->year += 1; 107 $hd1->setJdFromYmd(); 108 // Special rules. See http://www.hebcal.com/help/anniv.html 109 // Everything else is taken care of by our standard anniversary rules. 110 if ($hd->day == 30 && $hd->month == 2 && $hd->year != 0 && $hd1->daysInMonth() < 30) { 111 // 30 CSH - Last day in CSH 112 $jd = $jewish_calendar->ymdToJd($today->year, 3, 1) - 1; 113 } elseif ($hd->day == 30 && $hd->month == 3 && $hd->year != 0 && $hd1->daysInMonth() < 30) { 114 // 30 KSL - Last day in KSL 115 $jd = $jewish_calendar->ymdToJd($today->year, 4, 1) - 1; 116 } elseif ($hd->day == 30 && $hd->month == 6 && $hd->year != 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) { 117 // 30 ADR - Last day in SHV 118 $jd = $jewish_calendar->ymdToJd($today->year, 6, 1) - 1; 119 } 120 } 121 122 // Filter adjusted dates to our date range 123 if ($jd >= $startjd && $jd < $startjd + $days) { 124 // upcomming yahrzeit dates 125 switch ($calendar) { 126 case 'gregorian': 127 $yahrzeit_date = new GregorianDate($jd); 128 break; 129 case 'jewish': 130 default: 131 $yahrzeit_date = new JewishDate($jd); 132 break; 133 } 134 $yahrzeit_date = new Date($yahrzeit_date->format('%@ %A %O %E')); 135 136 $yahrzeits[] = (object) [ 137 'individual' => $fact->record(), 138 'fact_date' => $fact->date(), 139 'fact' => $fact, 140 'jd' => $jd, 141 'yahrzeit_date' => $yahrzeit_date, 142 ]; 143 } 144 } 145 } 146 } 147 148 switch ($infoStyle) { 149 case 'list': 150 $content = view('modules/yahrzeit/list', [ 151 'yahrzeits' => $yahrzeits, 152 ]); 153 break; 154 case 'table': 155 default: 156 $content = view('modules/yahrzeit/table', [ 157 'yahrzeits' => $yahrzeits, 158 ]); 159 break; 160 } 161 162 if ($ctype !== '') { 163 if ($ctype === 'gedcom' && Auth::isManager($tree)) { 164 $config_url = route('tree-page-block-edit', [ 165 'block_id' => $block_id, 166 'ged' => $tree->name(), 167 ]); 168 } elseif ($ctype === 'user' && Auth::check()) { 169 $config_url = route('user-page-block-edit', [ 170 'block_id' => $block_id, 171 'ged' => $tree->name(), 172 ]); 173 } else { 174 $config_url = ''; 175 } 176 177 return view('modules/block-template', [ 178 'block' => str_replace('_', '-', $this->name()), 179 'id' => $block_id, 180 'config_url' => $config_url, 181 'title' => $this->title(), 182 'content' => $content, 183 ]); 184 } 185 186 return $content; 187 } 188 189 /** {@inheritdoc} */ 190 public function loadAjax(): bool 191 { 192 return true; 193 } 194 195 /** {@inheritdoc} */ 196 public function isUserBlock(): bool 197 { 198 return true; 199 } 200 201 /** {@inheritdoc} */ 202 public function isTreeBlock(): bool 203 { 204 return true; 205 } 206 207 /** 208 * Update the configuration for a block. 209 * 210 * @param Request $request 211 * @param int $block_id 212 * 213 * @return void 214 */ 215 public function saveBlockConfiguration(Request $request, int $block_id) 216 { 217 $this->setBlockSetting($block_id, 'days', $request->get('days', self::DEFAULT_DAYS)); 218 $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE)); 219 $this->setBlockSetting($block_id, 'calendar', $request->get('calendar', self::DEFAULT_CALENDAR)); 220 } 221 222 /** 223 * An HTML form to edit block settings 224 * 225 * @param Tree $tree 226 * @param int $block_id 227 * 228 * @return void 229 */ 230 public function editBlockConfiguration(Tree $tree, int $block_id) 231 { 232 $calendar = $this->getBlockSetting($block_id, 'calendar', 'jewish'); 233 $days = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS); 234 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table'); 235 236 $styles = [ 237 /* I18N: An option in a list-box */ 238 'list' => I18N::translate('list'), 239 /* I18N: An option in a list-box */ 240 'table' => I18N::translate('table'), 241 ]; 242 243 $calendars = [ 244 'jewish' => I18N::translate('Jewish'), 245 'gregorian' => I18N::translate('Gregorian'), 246 ]; 247 248 echo view('modules/yahrzeit/config', [ 249 'calendar' => $calendar, 250 'calendars' => $calendars, 251 'days' => $days, 252 'infoStyle' => $infoStyle, 253 'max_days' => self::MAX_DAYS, 254 'styles' => $styles, 255 ]); 256 } 257} 258