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