xref: /webtrees/app/Module/OnThisDayModule.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\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('modules/todays_events/empty');
126		} elseif ($infoStyle === 'list') {
127			$content = view('modules/todays_events/list', [
128				'facts' => $facts,
129			]);
130		} else {
131			$content = view('modules/todays_events/table', [
132				'facts' => $facts,
133			]);
134		}
135
136		if ($template) {
137			if ($ctype === 'gedcom' && Auth::isManager($tree)) {
138				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]);
139			} elseif ($ctype === 'user' && Auth::check()) {
140				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]);
141			} else {
142				$config_url = '';
143			}
144
145			return view('modules/block-template', [
146				'block'      => str_replace('_', '-', $this->getName()),
147				'id'         => $block_id,
148				'config_url' => $config_url,
149				'title'      => $this->getTitle(),
150				'content'    => $content,
151			]);
152		} else {
153			return $content;
154		}
155	}
156
157	/** {@inheritdoc} */
158	public function loadAjax(): bool {
159		return true;
160	}
161
162	/** {@inheritdoc} */
163	public function isUserBlock(): bool {
164		return true;
165	}
166
167	/** {@inheritdoc} */
168	public function isGedcomBlock(): bool {
169		return true;
170	}
171
172	/**
173	 * An HTML form to edit block settings
174	 *
175	 * @param Tree $tree
176	 * @param int  $block_id
177	 *
178	 * @return void
179	 */
180	public function configureBlock(Tree $tree, int $block_id) {
181		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
182			$this->setBlockSetting($block_id, 'filter', Filter::postBool('filter'));
183			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
184			$this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'alpha|anniv', 'alpha'));
185			$this->setBlockSetting($block_id, 'events', implode(',', Filter::postArray('events')));
186
187			return;
188		}
189
190		$default_events = implode(',', self::DEFAULT_EVENTS);
191
192		$filter    = $this->getBlockSetting($block_id, 'filter', '1');
193		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
194		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
195		$events    = $this->getBlockSetting($block_id, 'events', $default_events);
196
197		$event_array = explode(',', $events);
198
199		$all_events = [];
200		foreach (self::ALL_EVENTS as $event) {
201			$all_events[$event] = GedcomTag::getLabel($event);
202		}
203
204		$info_styles = [
205			'list'  => /* I18N: An option in a list-box */ I18N::translate('list'),
206			'table' => /* I18N: An option in a list-box */ I18N::translate('table'),
207		];
208
209		$sort_styles = [
210			'alpha' => /* I18N: An option in a list-box */ I18N::translate('sort by name'),
211			'anniv' => /* I18N: An option in a list-box */ I18N::translate('sort by date'),
212		];
213
214		echo view('modules/todays_events/config', [
215			'all_events'  => $all_events,
216			'event_array' => $event_array,
217			'filter'      => $filter,
218			'infoStyle'   => $infoStyle,
219			'info_styles' => $info_styles,
220			'sortStyle'   => $sortStyle,
221			'sort_styles' => $sort_styles,
222		]);
223	}
224}
225