xref: /webtrees/app/Module/OnThisDayModule.php (revision 1001c3a8c3f3447dd95f8a9481ce18ff581bd6da)
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\Webtrees\Auth;
19use Fisharebest\Webtrees\Filter;
20use Fisharebest\Webtrees\Functions\FunctionsDb;
21use Fisharebest\Webtrees\GedcomTag;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Tree;
24
25/**
26 * Class OnThisDayModule
27 */
28class OnThisDayModule extends AbstractModule implements ModuleBlockInterface {
29	// All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN
30	const ALL_EVENTS = [
31		'ADOP',
32		'ANUL',
33		'BAPM',
34		'BARM',
35		'BASM',
36		'BIRT',
37		'BLES',
38		'BURI',
39		'CHR',
40		'CHRA',
41		'CONF',
42		'CREM',
43		'DEAT',
44		'DIV',
45		'DIVF',
46		'EMIG',
47		'ENGA',
48		'FCOM',
49		'GRAD',
50		'IMMI',
51		'MARB',
52		'MARC',
53		'MARL',
54		'MARR',
55		'MARS',
56		'NATU',
57		'ORDN',
58		'PROB',
59		'RETI',
60		'WILL',
61	];
62
63	const DEFAULT_EVENTS = [
64		'BIRT',
65		'MARR',
66		'DEAT',
67	];
68
69	/**
70	 * How should this module be labelled on tabs, menus, etc.?
71	 *
72	 * @return string
73	 */
74	public function getTitle() {
75		return /* I18N: Name of a module */ I18N::translate('On this day');
76	}
77
78	/**
79	 * A sentence describing what this module does.
80	 *
81	 * @return string
82	 */
83	public function getDescription() {
84		return /* I18N: Description of the “On this day” module */ I18N::translate('A list of the anniversaries that occur today.');
85	}
86
87	/**
88	 * Generate the HTML content of this block.
89	 *
90	 * @param Tree     $tree
91	 * @param int      $block_id
92	 * @param bool     $template
93	 * @param string[] $cfg
94	 *
95	 * @return string
96	 */
97	public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string {
98		global $ctype;
99
100		$default_events = implode(',', self::DEFAULT_EVENTS);
101
102		$filter    = (bool) $this->getBlockSetting($block_id, 'filter', '1');
103		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
104		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
105		$events    = $this->getBlockSetting($block_id, 'events', $default_events);
106
107		extract($cfg, EXTR_OVERWRITE);
108
109		$event_array = explode(',', $events);
110
111		// If we are only showing living individuals, then we don't need to search for DEAT events.
112		if ($filter) {
113			$death_events = explode('|', WT_EVENTS_DEAT);
114			$event_array = array_diff($event_array, $death_events);
115		}
116
117		$events_filter = implode('|', $event_array);
118
119		$startjd = WT_CLIENT_JD;
120		$endjd   = WT_CLIENT_JD;
121
122		$facts = FunctionsDb::getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $tree);
123
124		if (empty($facts)) {
125			$content = view('blocks/events-empty', [
126				'message' => I18N::translate('No events exist for today.')
127			]);
128		} else {
129			$content = view('blocks/events-' . $infoStyle, [
130					'facts'   => $facts,
131				]);
132		}
133
134		if ($template) {
135			if ($ctype === 'gedcom' && Auth::isManager($tree)) {
136				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]);
137			} elseif ($ctype === 'user' && Auth::check()) {
138				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]);
139			} else {
140				$config_url = '';
141			}
142
143			return view('blocks/template', [
144				'block'      => str_replace('_', '-', $this->getName()),
145				'id'         => $block_id,
146				'config_url' => $config_url,
147				'title'      => $this->getTitle(),
148				'content'    => $content,
149			]);
150		} else {
151			return $content;
152		}
153	}
154
155	/** {@inheritdoc} */
156	public function loadAjax(): bool {
157		return true;
158	}
159
160	/** {@inheritdoc} */
161	public function isUserBlock(): bool {
162		return true;
163	}
164
165	/** {@inheritdoc} */
166	public function isGedcomBlock(): bool {
167		return true;
168	}
169
170	/**
171	 * An HTML form to edit block settings
172	 *
173	 * @param Tree $tree
174	 * @param int  $block_id
175	 *
176	 * @return void
177	 */
178	public function configureBlock(Tree $tree, int $block_id) {
179		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
180			$this->setBlockSetting($block_id, 'filter', Filter::postBool('filter'));
181			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
182			$this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'alpha|anniv', 'alpha'));
183			$this->setBlockSetting($block_id, 'events', implode(',', Filter::postArray('events')));
184
185			return;
186		}
187
188		$default_events = implode(',', self::DEFAULT_EVENTS);
189
190		$filter    = $this->getBlockSetting($block_id, 'filter', '1');
191		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
192		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
193		$events    = $this->getBlockSetting($block_id, 'events', $default_events);
194
195		$event_array = explode(',', $events);
196
197		$all_events = [];
198		foreach (self::ALL_EVENTS as $event) {
199			$all_events[$event] = GedcomTag::getLabel($event);
200		}
201
202		$info_styles = [
203			'list'  => /* I18N: An option in a list-box */ I18N::translate('list'),
204			'table' => /* I18N: An option in a list-box */ I18N::translate('table'),
205		];
206
207		$sort_styles = [
208			'alpha' => /* I18N: An option in a list-box */ I18N::translate('sort by name'),
209			'anniv' => /* I18N: An option in a list-box */ I18N::translate('sort by date'),
210		];
211
212		echo view('blocks/on-this-day-config', [
213			'all_events'  => $all_events,
214			'event_array' => $event_array,
215			'filter'      => $filter,
216			'infoStyle'   => $infoStyle,
217			'info_styles' => $info_styles,
218			'sortStyle'   => $sortStyle,
219			'sort_styles' => $sort_styles,
220		]);
221	}
222}
223