xref: /webtrees/app/Module/YahrzeitModule.php (revision bba27599b02e0b6f90f62348bb4fa32bd74056d0)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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\I18N;
26
27/**
28 * Class YahrzeitModule
29 */
30class YahrzeitModule extends AbstractModule implements ModuleBlockInterface {
31	// Default values for new blocks.
32	const DEFAULT_CALENDAR = 'jewish';
33	const DEFAULT_DAYS     = 7;
34	const DEFAULT_STYLE    = 'table';
35
36	// Can show this number of days into the future.
37	const MAX_DAYS = 30;
38
39	/** {@inheritdoc} */
40	public function getTitle() {
41		return /* I18N: Name of a module. Yahrzeiten (the plural of Yahrzeit) are special anniversaries of deaths in the Hebrew faith/calendar. */ I18N::translate('Yahrzeiten');
42	}
43
44	/** {@inheritdoc} */
45	public function getDescription() {
46		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.');
47	}
48
49	/**
50	 * Generate the HTML content of this block.
51	 *
52	 * @param int      $block_id
53	 * @param bool     $template
54	 * @param string[] $cfg
55	 *
56	 * @return string
57	 */
58	public function getBlock($block_id, $template = true, $cfg = []): string {
59		global $ctype, $WT_TREE;
60
61		$days      = $this->getBlockSetting($block_id, 'days', self::DEFAULT_DAYS);
62		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE);
63		$calendar  = $this->getBlockSetting($block_id, 'calendar', self::DEFAULT_CALENDAR);
64
65		extract($cfg, EXTR_OVERWRITE);
66
67		$jewish_calendar = new JewishCalendar;
68		$startjd         = WT_CLIENT_JD;
69		$endjd           = WT_CLIENT_JD + $days - 1;
70
71		// The standard anniversary rules cover most of the Yahrzeit rules, we just
72		// need to handle a few special cases.
73		// Fetch normal anniversaries, with an extra day before/after
74		$yahrzeits = [];
75		for ($jd = $startjd - 1; $jd <= $endjd + $days; ++$jd) {
76			foreach (FunctionsDb::getAnniversaryEvents($jd, 'DEAT _YART', $WT_TREE) as $fact) {
77				// Exact hebrew dates only
78				$date = $fact->getDate();
79				if ($date->minimumDate() instanceof JewishDate && $date->minimumJulianDay() === $date->maximumJulianDay()) {
80
81					// ...then adjust DEAT dates (but not _YART)
82					if ($fact->getTag() === 'DEAT') {
83						$today = new JewishDate($jd);
84						$hd    = $fact->getDate()->minimumDate();
85						$hd1   = new JewishDate($hd);
86						$hd1->y += 1;
87						$hd1->setJdFromYmd();
88						// Special rules. See http://www.hebcal.com/help/anniv.html
89						// Everything else is taken care of by our standard anniversary rules.
90						if ($hd->d == 30 && $hd->m == 2 && $hd->y != 0 && $hd1->daysInMonth() < 30) {
91							// 30 CSH - Last day in CSH
92							$jd = $jewish_calendar->ymdToJd($today->y, 3, 1) - 1;
93						} elseif ($hd->d == 30 && $hd->m == 3 && $hd->y != 0 && $hd1->daysInMonth() < 30) {
94							// 30 KSL - Last day in KSL
95							$jd = $jewish_calendar->ymdToJd($today->y, 4, 1) - 1;
96						} elseif ($hd->d == 30 && $hd->m == 6 && $hd->y != 0 && $today->daysInMonth() < 30 && !$today->isLeapYear()) {
97							// 30 ADR - Last day in SHV
98							$jd = $jewish_calendar->ymdToJd($today->y, 6, 1) - 1;
99						}
100					}
101
102					// Filter adjusted dates to our date range
103					if ($jd >= $startjd && $jd < $startjd + $days) {
104						// upcomming yahrzeit dates
105						switch ($calendar) {
106							case 'gregorian':
107								$yahrzeit_date = new GregorianDate($jd);
108								break;
109							case 'jewish':
110							default:
111								$yahrzeit_date = new JewishDate($jd);
112								break;
113						}
114						$yahrzeit_date = new Date($yahrzeit_date->format('%@ %A %O %E'));
115
116						$yahrzeits[] = (object) [
117							'individual' => $fact->getParent(),
118							'fact_date'  => $fact->getDate(),
119							'fact'       => $fact,
120							'jd'            => $jd,
121							'yahrzeit_date' => $yahrzeit_date,
122						];
123					}
124				}
125			}
126		}
127
128		switch ($infoStyle) {
129			case 'list':
130				$content = view('blocks/yahrzeit-list', [
131					'yahrzeits' => $yahrzeits,
132				]);
133				break;
134			case 'table':
135			default:
136				$content = view('blocks/yahrzeit-table', [
137					'yahrzeits' => $yahrzeits,
138				]);
139			break;
140		}
141
142		if ($template) {
143			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE)) {
144				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
145			} elseif ($ctype === 'user' && Auth::check()) {
146				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
147			} else {
148				$config_url = '';
149			}
150
151			return view('blocks/template', [
152				'block'      => str_replace('_', '-', $this->getName()),
153				'id'         => $block_id,
154				'config_url' => $config_url,
155				'title'      => $this->getTitle(),
156				'content'    => $content,
157			]);
158		} else {
159			return $content;
160		}
161	}
162
163	/** {@inheritdoc} */
164	public function loadAjax(): bool {
165		return true;
166	}
167
168	/** {@inheritdoc} */
169	public function isUserBlock(): bool {
170		return true;
171	}
172
173	/** {@inheritdoc} */
174	public function isGedcomBlock(): bool {
175		return true;
176	}
177
178	/**
179	 * An HTML form to edit block settings
180	 *
181	 * @param int $block_id
182	 *
183	 * @return void
184	 */
185	public function configureBlock($block_id) {
186		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
187			$this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, self::MAX_DAYS, self::DEFAULT_DAYS));
188			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', self::DEFAULT_STYLE));
189			$this->setBlockSetting($block_id, 'calendar', Filter::post('calendar', 'jewish|gregorian', self::DEFAULT_CALENDAR));
190
191			return;
192		}
193
194		$calendar  = $this->getBlockSetting($block_id, 'calendar', 'jewish');
195		$days      = $this->getBlockSetting($block_id, 'days', 'self::DEFAULT_DAYS');
196		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
197
198		$styles = [
199			'list'  => /* I18N: An option in a list-box */ I18N::translate('list'),
200			'table' => /* I18N: An option in a list-box */ I18N::translate('table'),
201		];
202
203		$calendars = [
204			'jewish'    => I18N::translate('Jewish'),
205			'gregorian' => I18N::translate('Gregorian'),
206		];
207
208		echo view('blocks/yahrzeit-config', [
209			'calendar'  => $calendar,
210			'calendars' => $calendars,
211			'days'      => $days,
212			'infoStyle' => $infoStyle,
213			'max_days'  => self::MAX_DAYS,
214			'styles'    => $styles,
215		]);
216	}
217}
218