1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2017 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, $controller, $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) || $ctype === 'user' && Auth::check()) { 142 $config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 143 } else { 144 $config_url = ''; 145 } 146 147 return view('blocks/template', [ 148 'block' => str_replace('_', '-', $this->getName()), 149 'id' => $block_id, 150 'config_url' => $config_url, 151 'title' => $this->getTitle(), 152 'content' => $content, 153 ]); 154 } else { 155 return $content; 156 } 157 } 158 159 /** {@inheritdoc} */ 160 public function loadAjax(): bool { 161 return true; 162 } 163 164 /** {@inheritdoc} */ 165 public function isUserBlock(): bool { 166 return true; 167 } 168 169 /** {@inheritdoc} */ 170 public function isGedcomBlock(): bool { 171 return true; 172 } 173 174 /** 175 * An HTML form to edit block settings 176 * 177 * @param int $block_id 178 * 179 * @return void 180 */ 181 public function configureBlock($block_id): void { 182 if (Filter::postBool('save') && Filter::checkCsrf()) { 183 $this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, 30, 7)); 184 $this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table')); 185 $this->setBlockSetting($block_id, 'calendar', Filter::post('calendar', 'jewish|gregorian', 'jewish')); 186 } 187 188 $days = $this->getBlockSetting($block_id, 'days', '7'); 189 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table'); 190 $calendar = $this->getBlockSetting($block_id, 'calendar', 'jewish'); 191 192 echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="days">'; 193 echo I18N::translate('Number of days to show'); 194 echo '</label><div class="col-sm-9">'; 195 echo '<input type="text" name="days" size="2" value="' . $days . '">'; 196 echo ' <em>', I18N::plural('maximum %s day', 'maximum %s days', 30, I18N::number(30)), '</em>'; 197 echo '</div></div>'; 198 199 echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="infoStyle">'; 200 echo I18N::translate('Presentation style'); 201 echo '</label><div class="col-sm-9">'; 202 echo Bootstrap4::select(['list' => I18N::translate('list'), 'table' => I18N::translate('table')], $infoStyle, ['id' => 'infoStyle', 'name' => 'infoStyle']); 203 echo '</div></div>'; 204 205 echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="calendar">'; 206 echo I18N::translate('Calendar'); 207 echo '</label><div class="col-sm-9">'; 208 echo Bootstrap4::select(['jewish' => I18N::translate('Jewish'), 'gregorian' => I18N::translate('Gregorian')], $calendar, ['id' => 'calendar', 'name' => 'calendar']); 209 echo '</div></div>'; 210 } 211} 212