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