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