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