xref: /webtrees/app/Module/OnThisDayModule.php (revision 0280f44a4e2eb005a63073ee255873ce56267276)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
207ce208beSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDb;
2112664b30SGreg Roachuse Fisharebest\Webtrees\GedcomTag;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
238c2e8227SGreg Roach
248c2e8227SGreg Roach/**
258c2e8227SGreg Roach * Class OnThisDayModule
268c2e8227SGreg Roach */
27e2a378d3SGreg Roachclass OnThisDayModule extends AbstractModule implements ModuleBlockInterface {
2812664b30SGreg Roach	// All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN
2912664b30SGreg Roach	const ALL_EVENTS = [
3012664b30SGreg Roach		'ADOP',
3112664b30SGreg Roach		'ANUL',
3212664b30SGreg Roach		'BAPM',
3312664b30SGreg Roach		'BARM',
3412664b30SGreg Roach		'BASM',
3512664b30SGreg Roach		'BIRT',
3612664b30SGreg Roach		'BLES',
3712664b30SGreg Roach		'BURI',
3812664b30SGreg Roach		'CHR',
3912664b30SGreg Roach		'CHRA',
4012664b30SGreg Roach		'CONF',
4112664b30SGreg Roach		'CREM',
4212664b30SGreg Roach		'DEAT',
4312664b30SGreg Roach		'DIV',
4412664b30SGreg Roach		'DIVF',
4512664b30SGreg Roach		'EMIG',
4612664b30SGreg Roach		'ENGA',
4712664b30SGreg Roach		'FCOM',
4812664b30SGreg Roach		'GRAD',
4912664b30SGreg Roach		'IMMI',
5012664b30SGreg Roach		'MARB',
5112664b30SGreg Roach		'MARC',
5212664b30SGreg Roach		'MARL',
5312664b30SGreg Roach		'MARR',
5412664b30SGreg Roach		'MARS',
5512664b30SGreg Roach		'NATU',
5612664b30SGreg Roach		'ORDN',
5712664b30SGreg Roach		'PROB',
5812664b30SGreg Roach		'RETI',
5912664b30SGreg Roach		'WILL',
6012664b30SGreg Roach	];
6112664b30SGreg Roach
6212664b30SGreg Roach	const DEFAULT_EVENTS = [
6312664b30SGreg Roach		'BIRT',
6412664b30SGreg Roach		'MARR',
6512664b30SGreg Roach		'DEAT',
6612664b30SGreg Roach	];
6712664b30SGreg Roach
6812664b30SGreg Roach	/**
6912664b30SGreg Roach	 * How should this module be labelled on tabs, menus, etc.?
7012664b30SGreg Roach	 *
7112664b30SGreg Roach	 * @return string
7212664b30SGreg Roach	 */
738c2e8227SGreg Roach	public function getTitle() {
748c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('On this day');
758c2e8227SGreg Roach	}
768c2e8227SGreg Roach
7712664b30SGreg Roach	/**
7812664b30SGreg Roach	 * A sentence describing what this module does.
7912664b30SGreg Roach	 *
8012664b30SGreg Roach	 * @return string
8112664b30SGreg Roach	 */
828c2e8227SGreg Roach	public function getDescription() {
838c2e8227SGreg Roach		return /* I18N: Description of the “On this day” module */ I18N::translate('A list of the anniversaries that occur today.');
848c2e8227SGreg Roach	}
858c2e8227SGreg Roach
8676692c8bSGreg Roach	/**
8776692c8bSGreg Roach	 * Generate the HTML content of this block.
8876692c8bSGreg Roach	 *
8976692c8bSGreg Roach	 * @param int      $block_id
9076692c8bSGreg Roach	 * @param bool     $template
91727f238cSGreg Roach	 * @param string[] $cfg
9276692c8bSGreg Roach	 *
9376692c8bSGreg Roach	 * @return string
9476692c8bSGreg Roach	 */
95a9430be8SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []): string {
964b9ff166SGreg Roach		global $ctype, $WT_TREE;
978c2e8227SGreg Roach
9812664b30SGreg Roach		$default_events = implode(',', self::DEFAULT_EVENTS);
9912664b30SGreg Roach
10012664b30SGreg Roach		$filter    = (bool) $this->getBlockSetting($block_id, 'filter', '1');
101e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
102e2a378d3SGreg Roach		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
10312664b30SGreg Roach		$events    = $this->getBlockSetting($block_id, 'events', $default_events);
1048c2e8227SGreg Roach
105c385536dSGreg Roach		extract($cfg, EXTR_OVERWRITE);
1068c2e8227SGreg Roach
10712664b30SGreg Roach		$event_array = explode(',', $events);
10812664b30SGreg Roach
10912664b30SGreg Roach		// If we are only showing living individuals, then we don't need to search for DEAT events.
11012664b30SGreg Roach		if ($filter) {
11112664b30SGreg Roach			$death_events = explode('|', WT_EVENTS_DEAT);
11212664b30SGreg Roach			$event_array = array_diff($event_array, $death_events);
11312664b30SGreg Roach		}
11412664b30SGreg Roach
11512664b30SGreg Roach		$events_filter = implode('|', $event_array);
11612664b30SGreg Roach
11712664b30SGreg Roach		$startjd = WT_CLIENT_JD;
11812664b30SGreg Roach		$endjd   = WT_CLIENT_JD;
11912664b30SGreg Roach
1200eb072a2Smakitso		$facts = FunctionsDb::getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $WT_TREE);
12112664b30SGreg Roach
12212664b30SGreg Roach		if (empty($facts)) {
123*0280f44aSGreg Roach			$content = view('blocks/events-empty', [
124*0280f44aSGreg Roach				'message' => I18N::translate('No events exist for today.')
125*0280f44aSGreg Roach			]);
126d8189b6aSDavid Drury		} else {
127d8189b6aSDavid Drury			$content = view('blocks/events-' . $infoStyle, [
128d8189b6aSDavid Drury					'facts'   => $facts,
129*0280f44aSGreg Roach				]);
130*0280f44aSGreg Roach		}
1318c2e8227SGreg Roach
1328c2e8227SGreg Roach		if ($template) {
133397e599aSGreg Roach			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE)) {
134397e599aSGreg Roach				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
135397e599aSGreg Roach			} elseif ($ctype === 'user' && Auth::check()) {
136397e599aSGreg Roach				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
1379c6524dcSGreg Roach			} else {
1389c6524dcSGreg Roach				$config_url = '';
1399c6524dcSGreg Roach			}
1409c6524dcSGreg Roach
141225e381fSGreg Roach			return view('blocks/template', [
1429c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
1439c6524dcSGreg Roach				'id'         => $block_id,
1449c6524dcSGreg Roach				'config_url' => $config_url,
1459c6524dcSGreg Roach				'title'      => $this->getTitle(),
1469c6524dcSGreg Roach				'content'    => $content,
14712664b30SGreg Roach			]);
1488c2e8227SGreg Roach		} else {
1498c2e8227SGreg Roach			return $content;
1508c2e8227SGreg Roach		}
1518c2e8227SGreg Roach	}
1528c2e8227SGreg Roach
1538c2e8227SGreg Roach	/** {@inheritdoc} */
154a9430be8SGreg Roach	public function loadAjax(): bool {
1558c2e8227SGreg Roach		return true;
1568c2e8227SGreg Roach	}
1578c2e8227SGreg Roach
1588c2e8227SGreg Roach	/** {@inheritdoc} */
159a9430be8SGreg Roach	public function isUserBlock(): bool {
1608c2e8227SGreg Roach		return true;
1618c2e8227SGreg Roach	}
1628c2e8227SGreg Roach
1638c2e8227SGreg Roach	/** {@inheritdoc} */
164a9430be8SGreg Roach	public function isGedcomBlock(): bool {
1658c2e8227SGreg Roach		return true;
1668c2e8227SGreg Roach	}
1678c2e8227SGreg Roach
16876692c8bSGreg Roach	/**
16976692c8bSGreg Roach	 * An HTML form to edit block settings
17076692c8bSGreg Roach	 *
17176692c8bSGreg Roach	 * @param int $block_id
172a9430be8SGreg Roach	 *
173a9430be8SGreg Roach	 * @return void
17476692c8bSGreg Roach	 */
175be9a728cSGreg Roach	public function configureBlock($block_id) {
176c385536dSGreg Roach		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
177e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'filter', Filter::postBool('filter'));
178e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
179e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'alpha|anniv', 'alpha'));
18012664b30SGreg Roach			$this->setBlockSetting($block_id, 'events', implode(',', Filter::postArray('events')));
181c385536dSGreg Roach
182c385536dSGreg Roach			return;
1838c2e8227SGreg Roach		}
1848c2e8227SGreg Roach
18512664b30SGreg Roach		$default_events = implode(',', self::DEFAULT_EVENTS);
18612664b30SGreg Roach
187e2a378d3SGreg Roach		$filter    = $this->getBlockSetting($block_id, 'filter', '1');
188e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
189e2a378d3SGreg Roach		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
19012664b30SGreg Roach		$events    = $this->getBlockSetting($block_id, 'events', $default_events);
1918c2e8227SGreg Roach
19212664b30SGreg Roach		$event_array = explode(',', $events);
19312664b30SGreg Roach
19412664b30SGreg Roach		$all_events = [];
19512664b30SGreg Roach		foreach (self::ALL_EVENTS as $event) {
19612664b30SGreg Roach			$all_events[$event] = GedcomTag::getLabel($event);
19712664b30SGreg Roach		}
19812664b30SGreg Roach
199c385536dSGreg Roach		$info_styles = [
200c385536dSGreg Roach			'list'  => /* I18N: An option in a list-box */ I18N::translate('list'),
201c385536dSGreg Roach			'table' => /* I18N: An option in a list-box */ I18N::translate('table'),
202c385536dSGreg Roach		];
2038c2e8227SGreg Roach
204c385536dSGreg Roach		$sort_styles = [
205c385536dSGreg Roach			'alpha' => /* I18N: An option in a list-box */ I18N::translate('sort by name'),
206c385536dSGreg Roach			'anniv' => /* I18N: An option in a list-box */ I18N::translate('sort by date'),
207c385536dSGreg Roach		];
208d8189b6aSDavid Drury
209c385536dSGreg Roach		echo view('blocks/on-this-day-config', [
210c385536dSGreg Roach			'all_events'  => $all_events,
211c385536dSGreg Roach			'event_array' => $event_array,
212c385536dSGreg Roach			'filter'      => $filter,
213c385536dSGreg Roach			'infoStyle'   => $infoStyle,
214c385536dSGreg Roach			'info_styles' => $info_styles,
215c385536dSGreg Roach			'sortStyle'   => $sortStyle,
216c385536dSGreg Roach			'sort_styles' => $sort_styles,
217c385536dSGreg Roach		]);
2188c2e8227SGreg Roach	}
2198c2e8227SGreg Roach}
220