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