xref: /webtrees/app/Module/YahrzeitModule.php (revision 241f182934a11b1e3bd3e3fb316f3f3f2c05b074)
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				');
151			$content = '';
152			$content .= '<table id="' . $table_id . '" class="width100" style="visibility:hidden;">';
153			$content .= '<thead><tr>';
154			$content .= '<th>' . GedcomTag::getLabel('NAME') . '</th>';
155			$content .= '<th>' . GedcomTag::getLabel('NAME') . '</th>';
156			$content .= '<th>' . I18N::translate('Death') . '</th>';
157			$content .= '<th>DEAT</th>';
158			$content .= '<th><i class="icon-reminder" title="' . I18N::translate('Anniversary') . '"></i></th>';
159			$content .= '<th>' . GedcomTag::getLabel('_YART') . '</th>';
160			$content .= '<th>_YART</th>';
161			$content .= '</tr></thead><tbody>';
162
163			foreach ($yahrzeits as $yahrzeit) {
164				if ($yahrzeit->jd >= $startjd && $yahrzeit->jd < $startjd + $days) {
165					$content .= '<tr>';
166					$ind = $yahrzeit->getParent();
167					// Individual name(s)
168					$name = $ind->getFullName();
169					$url  = $ind->getHtmlUrl();
170					$content .= '<td>';
171					$content .= '<a href="' . $url . '">' . $name . '</a>';
172					$content .= $ind->getSexImage();
173					$addname = $ind->getAddName();
174					if ($addname) {
175						$content .= '<br><a href="' . $url . '">' . $addname . '</a>';
176					}
177					$content .= '</td>';
178					$content .= '<td>' . $ind->getSortName() . '</td>';
179
180					// death/yahrzeit event date
181					$content .= '<td>' . $yahrzeit->getDate()->display() . '</td>';
182					$content .= '<td>' . $yahrzeit->getDate()->julianDay() . '</td>'; // sortable date
183
184					// Anniversary
185					$content .= '<td>' . $yahrzeit->anniv . '</td>';
186
187					// upcomming yahrzeit dates
188					switch ($calendar) {
189					case 'gregorian':
190						$today = new GregorianDate($yahrzeit->jd);
191						break;
192					case 'jewish':
193					default:
194						$today = new JewishDate($yahrzeit->jd);
195						break;
196					}
197					$td = new Date($today->format('%@ %A %O %E'));
198					$content .= '<td>' . $td->display() . '</td>';
199					$content .= '<td>' . $td->julianDay() . '</td>'; // sortable date
200
201					$content .= '</tr>';
202				}
203			}
204			$content .= '</tbody></table>';
205
206			break;
207		}
208
209		if ($template) {
210			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
211				$config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
212			} else {
213				$config_url = '';
214			}
215
216			return View::make('blocks/template', [
217				'block'      => str_replace('_', '-', $this->getName()),
218				'id'         => $block_id,
219				'config_url' => $config_url,
220				'title'      => $this->getTitle(),
221				'content'    => $content,
222			]);
223		} else {
224			return $content;
225		}
226	}
227
228	/** {@inheritdoc} */
229	public function loadAjax() {
230		return true;
231	}
232
233	/** {@inheritdoc} */
234	public function isUserBlock() {
235		return true;
236	}
237
238	/** {@inheritdoc} */
239	public function isGedcomBlock() {
240		return true;
241	}
242
243	/**
244	 * An HTML form to edit block settings
245	 *
246	 * @param int $block_id
247	 */
248	public function configureBlock($block_id) {
249		if (Filter::postBool('save') && Filter::checkCsrf()) {
250			$this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, 30, 7));
251			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
252			$this->setBlockSetting($block_id, 'calendar', Filter::post('calendar', 'jewish|gregorian', 'jewish'));
253		}
254
255		$days      = $this->getBlockSetting($block_id, 'days', '7');
256		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
257		$calendar  = $this->getBlockSetting($block_id, 'calendar', 'jewish');
258
259		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="days">';
260		echo I18N::translate('Number of days to show');
261		echo '</label><div class="col-sm-9">';
262		echo '<input type="text" name="days" size="2" value="' . $days . '">';
263		echo ' <em>', I18N::plural('maximum %s day', 'maximum %s days', 30, I18N::number(30)), '</em>';
264		echo '</div></div>';
265
266		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="infoStyle">';
267		echo I18N::translate('Presentation style');
268		echo '</label><div class="col-sm-9">';
269		echo Bootstrap4::select(['list' => I18N::translate('list'), 'table' => I18N::translate('table')], $infoStyle, ['id' => 'infoStyle', 'name' => 'infoStyle']);
270		echo '</div></div>';
271
272		echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="calendar">';
273		echo I18N::translate('Calendar');
274		echo '</label><div class="col-sm-9">';
275		echo Bootstrap4::select(['jewish'    => I18N::translate('Jewish'), 'gregorian' => I18N::translate('Gregorian')], $calendar, ['id' => 'calendar', 'name' => 'calendar']);
276		echo '</div></div>';
277	}
278}
279