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