xref: /webtrees/app/Module/YahrzeitModule.php (revision 577e6b297534e9d4914dae637e58e8baf7848590)
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 Ramsey\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 = []) {
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
61		foreach (['days', 'infoStyle', 'calendar'] as $name) {
62			if (array_key_exists($name, $cfg)) {
63				$$name = $cfg[$name];
64			}
65		}
66
67		$startjd = WT_CLIENT_JD;
68		$endjd   = WT_CLIENT_JD + $days - 1;
69
70		$id    = $this->getName() . $block_id;
71		$class = $this->getName() . '_block';
72		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
73			$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> ';
74		} else {
75			$title = '';
76		}
77		$title .= $this->getTitle();
78
79		$content = '';
80		// The standard anniversary rules cover most of the Yahrzeit rules, we just
81		// need to handle a few special cases.
82		// Fetch normal anniversaries...
83		$yahrzeits = [];
84		for ($jd = $startjd - 1; $jd <= $endjd + $days; ++$jd) {
85			foreach (FunctionsDb::getAnniversaryEvents($jd, 'DEAT _YART', $WT_TREE) as $fact) {
86				// Exact hebrew dates only
87				$date = $fact->getDate();
88				if ($date->minimumDate() instanceof JewishDate && $date->minimumJulianDay() === $date->maximumJulianDay()) {
89					$fact->jd    = $jd;
90					$yahrzeits[] = $fact;
91				}
92			}
93		}
94
95		// ...then adjust dates
96		$jewish_calendar = new JewishCalendar;
97
98		foreach ($yahrzeits as $yahrzeit) {
99			if ($yahrzeit->getTag() === 'DEAT') {
100				$today = new JewishDate($yahrzeit->jd);
101				$hd    = $yahrzeit->getDate()->minimumDate();
102				$hd1   = new JewishDate($hd);
103				$hd1->y += 1;
104				$hd1->setJdFromYmd();
105				// Special rules. See http://www.hebcal.com/help/anniv.html
106				// Everything else is taken care of by our standard anniversary rules.
107				if ($hd->d == 30 && $hd->m == 2 && $hd->y != 0 && $hd1->daysInMonth() < 30) {
108					// 30 CSH - Last day in CSH
109					$yahrzeit->jd = $jewish_calendar->ymdToJd($today->y, 3, 1) - 1;
110				} elseif ($hd->d == 30 && $hd->m == 3 && $hd->y != 0 && $hd1->daysInMonth() < 30) {
111					// 30 KSL - Last day in KSL
112					$yahrzeit->jd = $jewish_calendar->ymdToJd($today->y, 4, 1) - 1;
113				} elseif ($hd->d == 30 && $hd->m == 6 && $hd->y != 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) {
114					// 30 ADR - Last day in SHV
115					$yahrzeit->jd = $jewish_calendar->ymdToJd($today->y, 6, 1) - 1;
116				}
117			}
118		}
119
120		switch ($infoStyle) {
121		case 'list':
122			foreach ($yahrzeits as $yahrzeit) {
123				if ($yahrzeit->jd >= $startjd && $yahrzeit->jd < $startjd + $days) {
124					$ind = $yahrzeit->getParent();
125					$content .= '<a href="' . $ind->getHtmlUrl() . '" class="list_item name2">' . $ind->getFullName() . '</a>' . $ind->getSexImage();
126					$content .= '<div class="indent">';
127					$content .= $yahrzeit->getDate()->display(true);
128					$content .= ', ' . I18N::translate('%s year anniversary', $yahrzeit->anniv);
129					$content .= '</div>';
130				}
131			}
132			break;
133		case 'table':
134		default:
135			$table_id = Uuid::uuid4(); // table requires a unique ID
136			$controller
137				->addInlineJavascript('
138					$("#' . $table_id . '").dataTable({
139						dom: \'t\',
140						' . I18N::datatablesI18N() . ',
141						autoWidth: false,
142						paginate: false,
143						lengthChange: false,
144						filter: false,
145						info: true,
146						sorting: [[5,"asc"]],
147						columns: [
148							/* 0-name */ { dataSort: 1 },
149							/* 1-NAME */ { visible: false },
150							/* 2-date */ { dataSort: 3 },
151							/* 3-DATE */ { visible: false },
152							/* 4-Aniv */ { class: "center"},
153							/* 5-yart */ { dataSort: 6 },
154							/* 6-YART */ { visible: false }
155						]
156					});
157					$("#' . $table_id . '").css("visibility", "visible");
158					$(".loading-image").css("display", "none");
159				');
160			$content = '';
161			$content .= '<div class="loading-image">&nbsp;</div>';
162			$content .= '<table id="' . $table_id . '" class="width100" style="visibility:hidden;">';
163			$content .= '<thead><tr>';
164			$content .= '<th>' . GedcomTag::getLabel('NAME') . '</th>';
165			$content .= '<th>' . GedcomTag::getLabel('NAME') . '</th>';
166			$content .= '<th>' . I18N::translate('Death') . '</th>';
167			$content .= '<th>DEAT</th>';
168			$content .= '<th><i class="icon-reminder" title="' . I18N::translate('Anniversary') . '"></i></th>';
169			$content .= '<th>' . GedcomTag::getLabel('_YART') . '</th>';
170			$content .= '<th>_YART</th>';
171			$content .= '</tr></thead><tbody>';
172
173			foreach ($yahrzeits as $yahrzeit) {
174				if ($yahrzeit->jd >= $startjd && $yahrzeit->jd < $startjd + $days) {
175					$content .= '<tr>';
176					$ind = $yahrzeit->getParent();
177					// Individual name(s)
178					$name = $ind->getFullName();
179					$url  = $ind->getHtmlUrl();
180					$content .= '<td>';
181					$content .= '<a href="' . $url . '">' . $name . '</a>';
182					$content .= $ind->getSexImage();
183					$addname = $ind->getAddName();
184					if ($addname) {
185						$content .= '<br><a href="' . $url . '">' . $addname . '</a>';
186					}
187					$content .= '</td>';
188					$content .= '<td>' . $ind->getSortName() . '</td>';
189
190					// death/yahrzeit event date
191					$content .= '<td>' . $yahrzeit->getDate()->display() . '</td>';
192					$content .= '<td>' . $yahrzeit->getDate()->julianDay() . '</td>'; // sortable date
193
194					// Anniversary
195					$content .= '<td>' . $yahrzeit->anniv . '</td>';
196
197					// upcomming yahrzeit dates
198					switch ($calendar) {
199					case 'gregorian':
200						$today = new GregorianDate($yahrzeit->jd);
201						break;
202					case 'jewish':
203					default:
204						$today = new JewishDate($yahrzeit->jd);
205						break;
206					}
207					$td = new Date($today->format('%@ %A %O %E'));
208					$content .= '<td>' . $td->display() . '</td>';
209					$content .= '<td>' . $td->julianDay() . '</td>'; // sortable date
210
211					$content .= '</tr>';
212				}
213			}
214			$content .= '</tbody></table>';
215
216			break;
217		}
218
219		if ($template) {
220			return Theme::theme()->formatBlock($id, $title, $class, $content);
221		} else {
222			return $content;
223		}
224	}
225
226	/** {@inheritdoc} */
227	public function loadAjax() {
228		return true;
229	}
230
231	/** {@inheritdoc} */
232	public function isUserBlock() {
233		return true;
234	}
235
236	/** {@inheritdoc} */
237	public function isGedcomBlock() {
238		return true;
239	}
240
241	/**
242	 * An HTML form to edit block settings
243	 *
244	 * @param int $block_id
245	 */
246	public function configureBlock($block_id) {
247		if (Filter::postBool('save') && Filter::checkCsrf()) {
248			$this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, 30, 7));
249			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
250			$this->setBlockSetting($block_id, 'calendar', Filter::post('calendar', 'jewish|gregorian', 'jewish'));
251		}
252
253		$days      = $this->getBlockSetting($block_id, 'days', '7');
254		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
255		$calendar  = $this->getBlockSetting($block_id, 'calendar', 'jewish');
256
257		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="days">';
258		echo I18N::translate('Number of days to show');
259		echo '</label><div class="col-sm-9">';
260		echo '<input type="text" name="days" size="2" value="' . $days . '">';
261		echo ' <em>', I18N::plural('maximum %s day', 'maximum %s days', 30, I18N::number(30)), '</em>';
262		echo '</div></div>';
263
264		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="infoStyle">';
265		echo I18N::translate('Presentation style');
266		echo '</label><div class="col-sm-9">';
267		echo Bootstrap4::select(['list' => I18N::translate('list'), 'table' => I18N::translate('table')], $infoStyle, ['id' => 'infoStyle', 'name' => 'infoStyle']);
268		echo '</div></div>';
269
270		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="calendar">';
271		echo I18N::translate('Calendar');
272		echo '</label><div class="col-sm-9">';
273		echo Bootstrap4::select(['jewish'    => I18N::translate('Jewish'), 'gregorian' => I18N::translate('Gregorian')], $calendar, ['id' => 'calendar', 'name' => 'calendar']);
274		echo '</div></div>';
275	}
276}
277