xref: /webtrees/app/Module/YahrzeitModule.php (revision 9c6524dcf4555157077b94309ec75cc0781acd78)
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\GedcomTag;
27use Fisharebest\Webtrees\I18N;
28use Fisharebest\Webtrees\Theme;
29use Fisharebest\Webtrees\View;
30use Ramsey\Uuid\Uuid;
31
32/**
33 * Class YahrzeitModule
34 */
35class YahrzeitModule extends AbstractModule implements ModuleBlockInterface {
36	/** {@inheritdoc} */
37	public function getTitle() {
38		return /* I18N: Name of a module. Yahrzeiten (the plural of Yahrzeit) are special anniversaries of deaths in the Hebrew faith/calendar. */ I18N::translate('Yahrzeiten');
39	}
40
41	/** {@inheritdoc} */
42	public function getDescription() {
43		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.');
44	}
45
46	/**
47	 * Generate the HTML content of this block.
48	 *
49	 * @param int      $block_id
50	 * @param bool     $template
51	 * @param string[] $cfg
52	 *
53	 * @return string
54	 */
55	public function getBlock($block_id, $template = true, $cfg = []) {
56		global $ctype, $controller, $WT_TREE;
57
58		$days      = $this->getBlockSetting($block_id, 'days', '7');
59		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
60		$calendar  = $this->getBlockSetting($block_id, 'calendar', 'jewish');
61
62		foreach (['days', 'infoStyle', 'calendar'] 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="fa fa-cog" href="block_edit.php?block_id=' . $block_id . '&amp;ged=' . $WT_TREE->getNameHtml() . '&amp;ctype=' . $ctype . '">' . I18N::translate('Preferences') . '</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 = [];
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				->addInlineJavascript('
139					$("#' . $table_id . '").dataTable({
140						dom: \'t\',
141						' . I18N::datatablesI18N() . ',
142						autoWidth: false,
143						paginate: false,
144						lengthChange: false,
145						filter: false,
146						info: true,
147						sorting: [[5,"asc"]],
148						columns: [
149							/* 0-name */ { dataSort: 1 },
150							/* 1-NAME */ { visible: false },
151							/* 2-date */ { dataSort: 3 },
152							/* 3-DATE */ { visible: false },
153							/* 4-Aniv */ { class: "center"},
154							/* 5-yart */ { dataSort: 6 },
155							/* 6-YART */ { visible: false }
156						]
157					});
158					$("#' . $table_id . '").css("visibility", "visible");
159					$(".loading-image").css("display", "none");
160				');
161			$content = '';
162			$content .= '<div class="loading-image">&nbsp;</div>';
163			$content .= '<table id="' . $table_id . '" class="width100" style="visibility:hidden;">';
164			$content .= '<thead><tr>';
165			$content .= '<th>' . GedcomTag::getLabel('NAME') . '</th>';
166			$content .= '<th>' . GedcomTag::getLabel('NAME') . '</th>';
167			$content .= '<th>' . I18N::translate('Death') . '</th>';
168			$content .= '<th>DEAT</th>';
169			$content .= '<th><i class="icon-reminder" title="' . I18N::translate('Anniversary') . '"></i></th>';
170			$content .= '<th>' . GedcomTag::getLabel('_YART') . '</th>';
171			$content .= '<th>_YART</th>';
172			$content .= '</tr></thead><tbody>';
173
174			foreach ($yahrzeits as $yahrzeit) {
175				if ($yahrzeit->jd >= $startjd && $yahrzeit->jd < $startjd + $days) {
176					$content .= '<tr>';
177					$ind = $yahrzeit->getParent();
178					// Individual name(s)
179					$name = $ind->getFullName();
180					$url  = $ind->getHtmlUrl();
181					$content .= '<td>';
182					$content .= '<a href="' . $url . '">' . $name . '</a>';
183					$content .= $ind->getSexImage();
184					$addname = $ind->getAddName();
185					if ($addname) {
186						$content .= '<br><a href="' . $url . '">' . $addname . '</a>';
187					}
188					$content .= '</td>';
189					$content .= '<td>' . $ind->getSortName() . '</td>';
190
191					// death/yahrzeit event date
192					$content .= '<td>' . $yahrzeit->getDate()->display() . '</td>';
193					$content .= '<td>' . $yahrzeit->getDate()->julianDay() . '</td>'; // sortable date
194
195					// Anniversary
196					$content .= '<td>' . $yahrzeit->anniv . '</td>';
197
198					// upcomming yahrzeit dates
199					switch ($calendar) {
200					case 'gregorian':
201						$today = new GregorianDate($yahrzeit->jd);
202						break;
203					case 'jewish':
204					default:
205						$today = new JewishDate($yahrzeit->jd);
206						break;
207					}
208					$td = new Date($today->format('%@ %A %O %E'));
209					$content .= '<td>' . $td->display() . '</td>';
210					$content .= '<td>' . $td->julianDay() . '</td>'; // sortable date
211
212					$content .= '</tr>';
213				}
214			}
215			$content .= '</tbody></table>';
216
217			break;
218		}
219
220		if ($template) {
221			return View::make('blocks/template', [
222				'block'      => str_replace('_', '-', $this->getName()),
223				'id'         => $block_id,
224				'config_url' => '',
225				'title'      => $this->getTitle(),
226				'content'    => $content,
227			]);
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		}
259
260		$days      = $this->getBlockSetting($block_id, 'days', '7');
261		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
262		$calendar  = $this->getBlockSetting($block_id, 'calendar', 'jewish');
263
264		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="days">';
265		echo I18N::translate('Number of days to show');
266		echo '</label><div class="col-sm-9">';
267		echo '<input type="text" name="days" size="2" value="' . $days . '">';
268		echo ' <em>', I18N::plural('maximum %s day', 'maximum %s days', 30, I18N::number(30)), '</em>';
269		echo '</div></div>';
270
271		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="infoStyle">';
272		echo I18N::translate('Presentation style');
273		echo '</label><div class="col-sm-9">';
274		echo Bootstrap4::select(['list' => I18N::translate('list'), 'table' => I18N::translate('table')], $infoStyle, ['id' => 'infoStyle', 'name' => 'infoStyle']);
275		echo '</div></div>';
276
277		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="calendar">';
278		echo I18N::translate('Calendar');
279		echo '</label><div class="col-sm-9">';
280		echo Bootstrap4::select(['jewish'    => I18N::translate('Jewish'), 'gregorian' => I18N::translate('Gregorian')], $calendar, ['id' => 'calendar', 'name' => 'calendar']);
281		echo '</div></div>';
282	}
283}
284