xref: /webtrees/app/Module/YahrzeitModule.php (revision cbc1590a8c715aa2d88bd745610b899587bd9563)
1<?php
2namespace Fisharebest\Webtrees;
3
4/**
5 * webtrees: online genealogy
6 * Copyright (C) 2015 webtrees development team
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19use Fisharebest\ExtCalendar\JewishCalendar;
20use Rhumsaa\Uuid\Uuid;
21
22/**
23 * Class YahrzeitModule
24 */
25class YahrzeitModule extends AbstractModule implements ModuleBlockInterface {
26	/** {@inheritdoc} */
27	public function getTitle() {
28		return /* I18N: Name of a module.  Yahrzeiten (the plural of Yahrzeit) are special anniversaries of deaths in the Hebrew faith/calendar. */ I18N::translate('Yahrzeiten');
29	}
30
31	/** {@inheritdoc} */
32	public function getDescription() {
33		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.');
34	}
35
36	/** {@inheritdoc} */
37	public function getBlock($block_id, $template = true, $cfg = null) {
38		global $ctype, $controller, $WT_TREE;
39
40		$days      = $this->getBlockSetting($block_id, 'days', '7');
41		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
42		$calendar  = $this->getBlockSetting($block_id, 'calendar', 'jewish');
43		$block     = $this->getBlockSetting($block_id, 'block', '1');
44
45		if ($cfg) {
46			foreach (array('days', 'infoStyle', 'block') as $name) {
47				if (array_key_exists($name, $cfg)) {
48					$$name = $cfg[$name];
49				}
50			}
51		}
52
53		$startjd = WT_CLIENT_JD;
54		$endjd   = WT_CLIENT_JD + $days - 1;
55
56		$id    = $this->getName() . $block_id;
57		$class = $this->getName() . '_block';
58		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
59			$title = '<a class="icon-admin" title="' . I18N::translate('Configure') . '" href="block_edit.php?block_id=' . $block_id . '&amp;ged=' . $WT_TREE->getNameHtml() . '&amp;ctype=' . $ctype . '"></a>';
60		} else {
61			$title = '';
62		}
63		$title .= $this->getTitle();
64
65		$content = '';
66		// The standard anniversary rules cover most of the Yahrzeit rules, we just
67		// need to handle a few special cases.
68		// Fetch normal anniversaries...
69		$yahrzeits = array();
70		for ($jd = $startjd - 1; $jd <= $endjd + $days; ++$jd) {
71			foreach (get_anniversary_events($jd, 'DEAT _YART', $WT_TREE) as $fact) {
72				// Exact hebrew dates only
73				$date = $fact->getDate();
74				if ($date->minimumDate() instanceof JewishDate && $date->minimumJulianDay() === $date->maximumJulianDay()) {
75					$fact->jd    = $jd;
76					$yahrzeits[] = $fact;
77				}
78			}
79		}
80
81		// ...then adjust dates
82		$jewish_calendar = new JewishCalendar;
83
84		foreach ($yahrzeits as $yahrzeit) {
85			if ($yahrzeit->getTag() === 'DEAT') {
86				$today = new JewishDate($yahrzeit->jd);
87				$hd    = $yahrzeit->getDate()->minimumDate();
88				$hd1   = new JewishDate($hd);
89				$hd1->y += 1;
90				$hd1->setJdFromYmd();
91				// Special rules.  See http://www.hebcal.com/help/anniv.html
92				// Everything else is taken care of by our standard anniversary rules.
93				if ($hd->d == 30 && $hd->m == 2 && $hd->y != 0 && $hd1->daysInMonth() < 30) {
94					// 30 CSH - Last day in CSH
95					$yahrzeit->jd = $jewish_calendar->ymdToJd($today->y, 3, 1) - 1;
96				} elseif ($hd->d == 30 && $hd->m == 3 && $hd->y != 0 && $hd1->daysInMonth() < 30) {
97					// 30 KSL - Last day in KSL
98					$yahrzeit->jd = $jewish_calendar->ymdToJd($today->y, 4, 1) - 1;
99				} elseif ($hd->d == 30 && $hd->m == 6 && $hd->y != 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) {
100					// 30 ADR - Last day in SHV
101					$yahrzeit->jd = $jewish_calendar->ymdToJd($today->y, 6, 1) - 1;
102				}
103			}
104		}
105
106		switch ($infoStyle) {
107		case 'list':
108			foreach ($yahrzeits as $yahrzeit) {
109				if ($yahrzeit->jd >= $startjd && $yahrzeit->jd < $startjd + $days) {
110					$ind = $yahrzeit->getParent();
111					$content .= "<a href=\"" . $ind->getHtmlUrl() . "\" class=\"list_item name2\">" . $ind->getFullName() . "</a>" . $ind->getSexImage();
112					$content .= "<div class=\"indent\">";
113					$content .= $yahrzeit->getDate()->display(true);
114					$content .= ', ' . I18N::translate('%s year anniversary', $yahrzeit->anniv);
115					$content .= "</div>";
116				}
117			}
118			break;
119		case 'table':
120		default:
121			$table_id = Uuid::uuid4(); // table requires a unique ID
122			$controller
123				->addExternalJavascript(WT_JQUERY_DATATABLES_JS_URL)
124				->addInlineJavascript('
125					jQuery("#' . $table_id . '").dataTable({
126						dom: \'t\',
127						' . I18N::datatablesI18N() . ',
128						autoWidth: false,
129						paginate: false,
130						lengthChange: false,
131						filter: false,
132						info: true,
133						jQueryUI: true,
134						sorting: [[5,"asc"]],
135						columns: [
136							/* 0-name */ { dataSort: 1 },
137							/* 1-NAME */ { visible: false },
138							/* 2-date */ { dataSort: 3 },
139							/* 3-DATE */ { visible: false },
140							/* 4-Aniv */ { class: "center"},
141							/* 5-yart */ { dataSort: 6 },
142							/* 6-YART */ { visible: false }
143						]
144					});
145					jQuery("#' . $table_id . '").css("visibility", "visible");
146					jQuery(".loading-image").css("display", "none");
147				');
148			$content = '';
149			$content .= '<div class="loading-image">&nbsp;</div>';
150			$content .= '<table id="' . $table_id . '" class="width100" style="visibility:hidden;">';
151			$content .= '<thead><tr>';
152			$content .= '<th>' . GedcomTag::getLabel('NAME') . '</th>';
153			$content .= '<th>' . GedcomTag::getLabel('NAME') . '</th>';
154			$content .= '<th>' . GedcomTag::getLabel('DEAT') . '</th>';
155			$content .= '<th>DEAT</th>';
156			$content .= '<th><i class="icon-reminder" title="' . I18N::translate('Anniversary') . '"></i></th>';
157			$content .= '<th>' . GedcomTag::getLabel('_YART') . '</th>';
158			$content .= '<th>_YART</th>';
159			$content .= '</tr></thead><tbody>';
160
161			foreach ($yahrzeits as $yahrzeit) {
162				if ($yahrzeit->jd >= $startjd && $yahrzeit->jd < $startjd + $days) {
163					$content .= '<tr>';
164					$ind = $yahrzeit->getParent();
165					// Individual name(s)
166					$name = $ind->getFullName();
167					$url  = $ind->getHtmlUrl();
168					$content .= '<td>';
169					$content .= '<a href="' . $url . '">' . $name . '</a>';
170					$content .= $ind->getSexImage();
171					$addname = $ind->getAddName();
172					if ($addname) {
173						$content .= '<br><a href="' . $url . '">' . $addname . '</a>';
174					}
175					$content .= '</td>';
176					$content .= '<td>' . $ind->getSortName() . '</td>';
177
178					// death/yahrzeit event date
179					$content .= '<td>' . $yahrzeit->getDate()->display() . '</td>';
180					$content .= '<td>' . $yahrzeit->getDate()->julianDay() . '</td>'; // sortable date
181
182					// Anniversary
183					$content .= '<td>' . $yahrzeit->anniv . '</td>';
184
185					// upcomming yahrzeit dates
186					switch ($calendar) {
187					case 'gregorian':
188						$today = new GregorianDate($yahrzeit->jd);
189						break;
190					case 'jewish':
191					default:
192						$today = new JewishDate($yahrzeit->jd);
193						break;
194					}
195					$td = new Date($today->format('%@ %A %O %E'));
196					$content .= '<td>' . $td->display() . '</td>';
197					$content .= '<td>' . $td->julianDay() . '</td>'; // sortable date
198
199					$content .= '</tr>';
200				}
201			}
202			$content .= '</tbody></table>';
203
204			break;
205		}
206
207		if ($template) {
208			if ($block) {
209				$class .= ' small_inner_block';
210			}
211
212			return Theme::theme()->formatBlock($id, $title, $class, $content);
213		} else {
214			return $content;
215		}
216	}
217
218	/** {@inheritdoc} */
219	public function loadAjax() {
220		return true;
221	}
222
223	/** {@inheritdoc} */
224	public function isUserBlock() {
225		return true;
226	}
227
228	/** {@inheritdoc} */
229	public function isGedcomBlock() {
230		return true;
231	}
232
233	/** {@inheritdoc} */
234	public function configureBlock($block_id) {
235		if (Filter::postBool('save') && Filter::checkCsrf()) {
236			$this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, 30, 7));
237			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
238			$this->setBlockSetting($block_id, 'calendar', Filter::post('calendar', 'jewish|gregorian', 'jewish'));
239			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
240		}
241
242		$days      = $this->getBlockSetting($block_id, 'days', '7');
243		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
244		$calendar  = $this->getBlockSetting($block_id, 'calendar', 'jewish');
245		$block     = $this->getBlockSetting($block_id, 'block', '1');
246
247		echo '<tr><td class="descriptionbox wrap width33">';
248		echo I18N::translate('Number of days to show');
249		echo '</td><td class="optionbox">';
250		echo '<input type="text" name="days" size="2" value="' . $days . '">';
251		echo ' <em>', I18N::plural('maximum %s day', 'maximum %s days', 30, I18N::number(30)), '</em>';
252		echo '</td></tr>';
253
254		echo '<tr><td class="descriptionbox wrap width33">';
255		echo I18N::translate('Presentation style');
256		echo '</td><td class="optionbox">';
257		echo select_edit_control('infoStyle', array('list' => I18N::translate('list'), 'table' => I18N::translate('table')), null, $infoStyle, '');
258		echo '</td></tr>';
259
260		echo '<tr><td class="descriptionbox wrap width33">';
261		echo I18N::translate('Calendar');
262		echo '</td><td class="optionbox">';
263		echo select_edit_control('calendar', array(
264			'jewish'    => /* I18N: The Hebrew/Jewish calendar */ I18N::translate('Jewish'),
265			'gregorian' => /* I18N: The gregorian calendar */ I18N::translate('Gregorian'),
266		), null, $calendar, '');
267		echo '</td></tr>';
268
269		echo '<tr><td class="descriptionbox wrap width33">';
270		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
271		echo '</td><td class="optionbox">';
272		echo edit_field_yes_no('block', $block);
273		echo '</td></tr>';
274	}
275}
276