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