xref: /webtrees/app/Module/YahrzeitModule.php (revision 9c6524dcf4555157077b94309ec75cc0781acd78)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
46bdf7674SGreg Roach * Copyright (C) 2017 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
178c2e8227SGreg Roach
188c2e8227SGreg Roachuse Fisharebest\ExtCalendar\JewishCalendar;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
2015d603e7SGreg Roachuse Fisharebest\Webtrees\Bootstrap4;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
253d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDb;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomTag;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Theme;
29*9c6524dcSGreg Roachuse Fisharebest\Webtrees\View;
30577e6b29SGreg Roachuse Ramsey\Uuid\Uuid;
318c2e8227SGreg Roach
328c2e8227SGreg Roach/**
338c2e8227SGreg Roach * Class YahrzeitModule
348c2e8227SGreg Roach */
35e2a378d3SGreg Roachclass YahrzeitModule extends AbstractModule implements ModuleBlockInterface {
368c2e8227SGreg Roach	/** {@inheritdoc} */
378c2e8227SGreg Roach	public function getTitle() {
388c2e8227SGreg Roach		return /* I18N: Name of a module. Yahrzeiten (the plural of Yahrzeit) are special anniversaries of deaths in the Hebrew faith/calendar. */ I18N::translate('Yahrzeiten');
398c2e8227SGreg Roach	}
408c2e8227SGreg Roach
418c2e8227SGreg Roach	/** {@inheritdoc} */
428c2e8227SGreg Roach	public function getDescription() {
438c2e8227SGreg Roach		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.');
448c2e8227SGreg Roach	}
458c2e8227SGreg Roach
4676692c8bSGreg Roach	/**
4776692c8bSGreg Roach	 * Generate the HTML content of this block.
4876692c8bSGreg Roach	 *
4976692c8bSGreg Roach	 * @param int      $block_id
5076692c8bSGreg Roach	 * @param bool     $template
51727f238cSGreg Roach	 * @param string[] $cfg
5276692c8bSGreg Roach	 *
5376692c8bSGreg Roach	 * @return string
5476692c8bSGreg Roach	 */
5513abd6f3SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []) {
564b9ff166SGreg Roach		global $ctype, $controller, $WT_TREE;
578c2e8227SGreg Roach
58e2a378d3SGreg Roach		$days      = $this->getBlockSetting($block_id, 'days', '7');
59e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
60e2a378d3SGreg Roach		$calendar  = $this->getBlockSetting($block_id, 'calendar', 'jewish');
618c2e8227SGreg Roach
6215d603e7SGreg Roach		foreach (['days', 'infoStyle', 'calendar'] as $name) {
638c2e8227SGreg Roach			if (array_key_exists($name, $cfg)) {
648c2e8227SGreg Roach				$$name = $cfg[$name];
658c2e8227SGreg Roach			}
668c2e8227SGreg Roach		}
678c2e8227SGreg Roach
688c2e8227SGreg Roach		$startjd = WT_CLIENT_JD;
698c2e8227SGreg Roach		$endjd   = WT_CLIENT_JD + $days - 1;
708c2e8227SGreg Roach
718c2e8227SGreg Roach		$id    = $this->getName() . $block_id;
728c2e8227SGreg Roach		$class = $this->getName() . '_block';
734b9ff166SGreg Roach		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
7415d603e7SGreg Roach			$title = '<a class="fa fa-cog" href="block_edit.php?block_id=' . $block_id . '&amp;ged=' . $WT_TREE->getNameHtml() . '&amp;ctype=' . $ctype . '">' . I18N::translate('Preferences') . '</a> ';
758c2e8227SGreg Roach		} else {
768c2e8227SGreg Roach			$title = '';
778c2e8227SGreg Roach		}
788c2e8227SGreg Roach		$title .= $this->getTitle();
798c2e8227SGreg Roach
808c2e8227SGreg Roach		$content = '';
818c2e8227SGreg Roach		// The standard anniversary rules cover most of the Yahrzeit rules, we just
828c2e8227SGreg Roach		// need to handle a few special cases.
838c2e8227SGreg Roach		// Fetch normal anniversaries...
8413abd6f3SGreg Roach		$yahrzeits = [];
858c2e8227SGreg Roach		for ($jd = $startjd - 1; $jd <= $endjd + $days; ++$jd) {
863d7a8a4cSGreg Roach			foreach (FunctionsDb::getAnniversaryEvents($jd, 'DEAT _YART', $WT_TREE) as $fact) {
878c2e8227SGreg Roach				// Exact hebrew dates only
888c2e8227SGreg Roach				$date = $fact->getDate();
898c2e8227SGreg Roach				if ($date->minimumDate() instanceof JewishDate && $date->minimumJulianDay() === $date->maximumJulianDay()) {
908c2e8227SGreg Roach					$fact->jd    = $jd;
918c2e8227SGreg Roach					$yahrzeits[] = $fact;
928c2e8227SGreg Roach				}
938c2e8227SGreg Roach			}
948c2e8227SGreg Roach		}
958c2e8227SGreg Roach
968c2e8227SGreg Roach		// ...then adjust dates
978c2e8227SGreg Roach		$jewish_calendar = new JewishCalendar;
988c2e8227SGreg Roach
998c2e8227SGreg Roach		foreach ($yahrzeits as $yahrzeit) {
1008c2e8227SGreg Roach			if ($yahrzeit->getTag() === 'DEAT') {
1018c2e8227SGreg Roach				$today = new JewishDate($yahrzeit->jd);
1028a22d202SGreg Roach				$hd    = $yahrzeit->getDate()->minimumDate();
1038c2e8227SGreg Roach				$hd1   = new JewishDate($hd);
1048c2e8227SGreg Roach				$hd1->y += 1;
1058c2e8227SGreg Roach				$hd1->setJdFromYmd();
1068c2e8227SGreg Roach				// Special rules. See http://www.hebcal.com/help/anniv.html
1078c2e8227SGreg Roach				// Everything else is taken care of by our standard anniversary rules.
1088c2e8227SGreg Roach				if ($hd->d == 30 && $hd->m == 2 && $hd->y != 0 && $hd1->daysInMonth() < 30) {
1098c2e8227SGreg Roach					// 30 CSH - Last day in CSH
1108c2e8227SGreg Roach					$yahrzeit->jd = $jewish_calendar->ymdToJd($today->y, 3, 1) - 1;
1118c2e8227SGreg Roach				} elseif ($hd->d == 30 && $hd->m == 3 && $hd->y != 0 && $hd1->daysInMonth() < 30) {
1128c2e8227SGreg Roach					// 30 KSL - Last day in KSL
1138c2e8227SGreg Roach					$yahrzeit->jd = $jewish_calendar->ymdToJd($today->y, 4, 1) - 1;
1148c2e8227SGreg Roach				} elseif ($hd->d == 30 && $hd->m == 6 && $hd->y != 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) {
1158c2e8227SGreg Roach					// 30 ADR - Last day in SHV
1168c2e8227SGreg Roach					$yahrzeit->jd = $jewish_calendar->ymdToJd($today->y, 6, 1) - 1;
1178c2e8227SGreg Roach				}
1188c2e8227SGreg Roach			}
1198c2e8227SGreg Roach		}
1208c2e8227SGreg Roach
1218c2e8227SGreg Roach		switch ($infoStyle) {
1228c2e8227SGreg Roach		case 'list':
1238c2e8227SGreg Roach			foreach ($yahrzeits as $yahrzeit) {
1248c2e8227SGreg Roach				if ($yahrzeit->jd >= $startjd && $yahrzeit->jd < $startjd + $days) {
1258c2e8227SGreg Roach					$ind = $yahrzeit->getParent();
1267a6ee1acSGreg Roach					$content .= '<a href="' . $ind->getHtmlUrl() . '" class="list_item name2">' . $ind->getFullName() . '</a>' . $ind->getSexImage();
1277a6ee1acSGreg Roach					$content .= '<div class="indent">';
1288c2e8227SGreg Roach					$content .= $yahrzeit->getDate()->display(true);
1298c2e8227SGreg Roach					$content .= ', ' . I18N::translate('%s year anniversary', $yahrzeit->anniv);
1307a6ee1acSGreg Roach					$content .= '</div>';
1318c2e8227SGreg Roach				}
1328c2e8227SGreg Roach			}
1338c2e8227SGreg Roach			break;
1348c2e8227SGreg Roach		case 'table':
1358c2e8227SGreg Roach		default:
1368c2e8227SGreg Roach			$table_id = Uuid::uuid4(); // table requires a unique ID
1378c2e8227SGreg Roach			$controller
1388c2e8227SGreg Roach				->addInlineJavascript('
13915d603e7SGreg Roach					$("#' . $table_id . '").dataTable({
1408c2e8227SGreg Roach						dom: \'t\',
1418c2e8227SGreg Roach						' . I18N::datatablesI18N() . ',
1428c2e8227SGreg Roach						autoWidth: false,
1438c2e8227SGreg Roach						paginate: false,
1448c2e8227SGreg Roach						lengthChange: false,
1458c2e8227SGreg Roach						filter: false,
1468c2e8227SGreg Roach						info: true,
1478c2e8227SGreg Roach						sorting: [[5,"asc"]],
1488c2e8227SGreg Roach						columns: [
1498c2e8227SGreg Roach							/* 0-name */ { dataSort: 1 },
1508c2e8227SGreg Roach							/* 1-NAME */ { visible: false },
1518c2e8227SGreg Roach							/* 2-date */ { dataSort: 3 },
1528c2e8227SGreg Roach							/* 3-DATE */ { visible: false },
1538c2e8227SGreg Roach							/* 4-Aniv */ { class: "center"},
1548c2e8227SGreg Roach							/* 5-yart */ { dataSort: 6 },
1558c2e8227SGreg Roach							/* 6-YART */ { visible: false }
1568c2e8227SGreg Roach						]
1578c2e8227SGreg Roach					});
15815d603e7SGreg Roach					$("#' . $table_id . '").css("visibility", "visible");
15915d603e7SGreg Roach					$(".loading-image").css("display", "none");
1608c2e8227SGreg Roach				');
1618c2e8227SGreg Roach			$content = '';
1628c2e8227SGreg Roach			$content .= '<div class="loading-image">&nbsp;</div>';
1638c2e8227SGreg Roach			$content .= '<table id="' . $table_id . '" class="width100" style="visibility:hidden;">';
1648c2e8227SGreg Roach			$content .= '<thead><tr>';
165764a01d9SGreg Roach			$content .= '<th>' . GedcomTag::getLabel('NAME') . '</th>';
166764a01d9SGreg Roach			$content .= '<th>' . GedcomTag::getLabel('NAME') . '</th>';
16715d603e7SGreg Roach			$content .= '<th>' . I18N::translate('Death') . '</th>';
1688c2e8227SGreg Roach			$content .= '<th>DEAT</th>';
1698c2e8227SGreg Roach			$content .= '<th><i class="icon-reminder" title="' . I18N::translate('Anniversary') . '"></i></th>';
170764a01d9SGreg Roach			$content .= '<th>' . GedcomTag::getLabel('_YART') . '</th>';
1718c2e8227SGreg Roach			$content .= '<th>_YART</th>';
1728c2e8227SGreg Roach			$content .= '</tr></thead><tbody>';
1738c2e8227SGreg Roach
1748c2e8227SGreg Roach			foreach ($yahrzeits as $yahrzeit) {
1758c2e8227SGreg Roach				if ($yahrzeit->jd >= $startjd && $yahrzeit->jd < $startjd + $days) {
1768c2e8227SGreg Roach					$content .= '<tr>';
1778c2e8227SGreg Roach					$ind = $yahrzeit->getParent();
1788c2e8227SGreg Roach					// Individual name(s)
1798c2e8227SGreg Roach					$name = $ind->getFullName();
1808c2e8227SGreg Roach					$url  = $ind->getHtmlUrl();
1818c2e8227SGreg Roach					$content .= '<td>';
1828c2e8227SGreg Roach					$content .= '<a href="' . $url . '">' . $name . '</a>';
1838c2e8227SGreg Roach					$content .= $ind->getSexImage();
1848c2e8227SGreg Roach					$addname = $ind->getAddName();
1858c2e8227SGreg Roach					if ($addname) {
1868c2e8227SGreg Roach						$content .= '<br><a href="' . $url . '">' . $addname . '</a>';
1878c2e8227SGreg Roach					}
1888c2e8227SGreg Roach					$content .= '</td>';
1898c2e8227SGreg Roach					$content .= '<td>' . $ind->getSortName() . '</td>';
1908c2e8227SGreg Roach
1918c2e8227SGreg Roach					// death/yahrzeit event date
1928c2e8227SGreg Roach					$content .= '<td>' . $yahrzeit->getDate()->display() . '</td>';
1938c2e8227SGreg Roach					$content .= '<td>' . $yahrzeit->getDate()->julianDay() . '</td>'; // sortable date
1948c2e8227SGreg Roach
1958c2e8227SGreg Roach					// Anniversary
1968c2e8227SGreg Roach					$content .= '<td>' . $yahrzeit->anniv . '</td>';
1978c2e8227SGreg Roach
1988c2e8227SGreg Roach					// upcomming yahrzeit dates
1998c2e8227SGreg Roach					switch ($calendar) {
2008c2e8227SGreg Roach					case 'gregorian':
2018c2e8227SGreg Roach						$today = new GregorianDate($yahrzeit->jd);
2028c2e8227SGreg Roach						break;
2038c2e8227SGreg Roach					case 'jewish':
2048c2e8227SGreg Roach					default:
2058c2e8227SGreg Roach						$today = new JewishDate($yahrzeit->jd);
2068c2e8227SGreg Roach						break;
2078c2e8227SGreg Roach					}
2088c2e8227SGreg Roach					$td = new Date($today->format('%@ %A %O %E'));
2098c2e8227SGreg Roach					$content .= '<td>' . $td->display() . '</td>';
2108c2e8227SGreg Roach					$content .= '<td>' . $td->julianDay() . '</td>'; // sortable date
2118c2e8227SGreg Roach
2128c2e8227SGreg Roach					$content .= '</tr>';
2138c2e8227SGreg Roach				}
2148c2e8227SGreg Roach			}
2158c2e8227SGreg Roach			$content .= '</tbody></table>';
2168c2e8227SGreg Roach
2178c2e8227SGreg Roach			break;
2188c2e8227SGreg Roach		}
2198c2e8227SGreg Roach
2208c2e8227SGreg Roach		if ($template) {
221*9c6524dcSGreg Roach			return View::make('blocks/template', [
222*9c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
223*9c6524dcSGreg Roach				'id'         => $block_id,
224*9c6524dcSGreg Roach				'config_url' => '',
225*9c6524dcSGreg Roach				'title'      => $this->getTitle(),
226*9c6524dcSGreg Roach				'content'    => $content,
227*9c6524dcSGreg Roach			]);
2288c2e8227SGreg Roach		} else {
2298c2e8227SGreg Roach			return $content;
2308c2e8227SGreg Roach		}
2318c2e8227SGreg Roach	}
2328c2e8227SGreg Roach
2338c2e8227SGreg Roach	/** {@inheritdoc} */
2348c2e8227SGreg Roach	public function loadAjax() {
2358c2e8227SGreg Roach		return true;
2368c2e8227SGreg Roach	}
2378c2e8227SGreg Roach
2388c2e8227SGreg Roach	/** {@inheritdoc} */
2398c2e8227SGreg Roach	public function isUserBlock() {
2408c2e8227SGreg Roach		return true;
2418c2e8227SGreg Roach	}
2428c2e8227SGreg Roach
2438c2e8227SGreg Roach	/** {@inheritdoc} */
2448c2e8227SGreg Roach	public function isGedcomBlock() {
2458c2e8227SGreg Roach		return true;
2468c2e8227SGreg Roach	}
2478c2e8227SGreg Roach
24876692c8bSGreg Roach	/**
24976692c8bSGreg Roach	 * An HTML form to edit block settings
25076692c8bSGreg Roach	 *
25176692c8bSGreg Roach	 * @param int $block_id
25276692c8bSGreg Roach	 */
2538c2e8227SGreg Roach	public function configureBlock($block_id) {
2548c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
255e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, 30, 7));
256e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
257e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'calendar', Filter::post('calendar', 'jewish|gregorian', 'jewish'));
2588c2e8227SGreg Roach		}
2598c2e8227SGreg Roach
260e2a378d3SGreg Roach		$days      = $this->getBlockSetting($block_id, 'days', '7');
261e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
262e2a378d3SGreg Roach		$calendar  = $this->getBlockSetting($block_id, 'calendar', 'jewish');
2638c2e8227SGreg Roach
26415d603e7SGreg Roach		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="days">';
2658c2e8227SGreg Roach		echo I18N::translate('Number of days to show');
26615d603e7SGreg Roach		echo '</label><div class="col-sm-9">';
2678c2e8227SGreg Roach		echo '<input type="text" name="days" size="2" value="' . $days . '">';
268def7396fSGreg Roach		echo ' <em>', I18N::plural('maximum %s day', 'maximum %s days', 30, I18N::number(30)), '</em>';
26915d603e7SGreg Roach		echo '</div></div>';
2708c2e8227SGreg Roach
27115d603e7SGreg Roach		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="infoStyle">';
2728c2e8227SGreg Roach		echo I18N::translate('Presentation style');
27315d603e7SGreg Roach		echo '</label><div class="col-sm-9">';
27415d603e7SGreg Roach		echo Bootstrap4::select(['list' => I18N::translate('list'), 'table' => I18N::translate('table')], $infoStyle, ['id' => 'infoStyle', 'name' => 'infoStyle']);
27515d603e7SGreg Roach		echo '</div></div>';
2768c2e8227SGreg Roach
27715d603e7SGreg Roach		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="calendar">';
2788c2e8227SGreg Roach		echo I18N::translate('Calendar');
27915d603e7SGreg Roach		echo '</label><div class="col-sm-9">';
28015d603e7SGreg Roach		echo Bootstrap4::select(['jewish'    => I18N::translate('Jewish'), 'gregorian' => I18N::translate('Gregorian')], $calendar, ['id' => 'calendar', 'name' => 'calendar']);
28115d603e7SGreg Roach		echo '</div></div>';
2828c2e8227SGreg Roach	}
2838c2e8227SGreg Roach}
284