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