xref: /webtrees/app/Module/OnThisDayModule.php (revision 397e599a3131684b8a335ebfddcfe63828b9f51b)
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;
1915d603e7SGreg Roachuse Fisharebest\Webtrees\Bootstrap4;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
21d8189b6aSDavid Druryuse Fisharebest\Webtrees\Functions\FunctionsDB;
223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit;
2312664b30SGreg Roachuse Fisharebest\Webtrees\GedcomTag;
249c6524dcSGreg Roachuse Fisharebest\Webtrees\Html;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class OnThisDayModule
298c2e8227SGreg Roach */
30e2a378d3SGreg Roachclass OnThisDayModule extends AbstractModule implements ModuleBlockInterface {
3112664b30SGreg Roach	// All standard GEDCOM 5.5.1 events except CENS, RESI and EVEN
3212664b30SGreg Roach	const ALL_EVENTS = [
3312664b30SGreg Roach		'ADOP',
3412664b30SGreg Roach		'ANUL',
3512664b30SGreg Roach		'BAPM',
3612664b30SGreg Roach		'BARM',
3712664b30SGreg Roach		'BASM',
3812664b30SGreg Roach		'BIRT',
3912664b30SGreg Roach		'BLES',
4012664b30SGreg Roach		'BURI',
4112664b30SGreg Roach		'CHR',
4212664b30SGreg Roach		'CHRA',
4312664b30SGreg Roach		'CONF',
4412664b30SGreg Roach		'CREM',
4512664b30SGreg Roach		'DEAT',
4612664b30SGreg Roach		'DIV',
4712664b30SGreg Roach		'DIVF',
4812664b30SGreg Roach		'EMIG',
4912664b30SGreg Roach		'ENGA',
5012664b30SGreg Roach		'FCOM',
5112664b30SGreg Roach		'GRAD',
5212664b30SGreg Roach		'IMMI',
5312664b30SGreg Roach		'MARB',
5412664b30SGreg Roach		'MARC',
5512664b30SGreg Roach		'MARL',
5612664b30SGreg Roach		'MARR',
5712664b30SGreg Roach		'MARS',
5812664b30SGreg Roach		'NATU',
5912664b30SGreg Roach		'ORDN',
6012664b30SGreg Roach		'PROB',
6112664b30SGreg Roach		'RETI',
6212664b30SGreg Roach		'WILL',
6312664b30SGreg Roach	];
6412664b30SGreg Roach
6512664b30SGreg Roach	const DEFAULT_EVENTS = [
6612664b30SGreg Roach		'BIRT',
6712664b30SGreg Roach		'MARR',
6812664b30SGreg Roach		'DEAT',
6912664b30SGreg Roach	];
7012664b30SGreg Roach
7112664b30SGreg Roach	/**
7212664b30SGreg Roach	 * How should this module be labelled on tabs, menus, etc.?
7312664b30SGreg Roach	 *
7412664b30SGreg Roach	 * @return string
7512664b30SGreg Roach	 */
768c2e8227SGreg Roach	public function getTitle() {
778c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('On this day');
788c2e8227SGreg Roach	}
798c2e8227SGreg Roach
8012664b30SGreg Roach	/**
8112664b30SGreg Roach	 * A sentence describing what this module does.
8212664b30SGreg Roach	 *
8312664b30SGreg Roach	 * @return string
8412664b30SGreg Roach	 */
858c2e8227SGreg Roach	public function getDescription() {
868c2e8227SGreg Roach		return /* I18N: Description of the “On this day” module */ I18N::translate('A list of the anniversaries that occur today.');
878c2e8227SGreg Roach	}
888c2e8227SGreg Roach
8976692c8bSGreg Roach	/**
9076692c8bSGreg Roach	 * Generate the HTML content of this block.
9176692c8bSGreg Roach	 *
9276692c8bSGreg Roach	 * @param int      $block_id
9376692c8bSGreg Roach	 * @param bool     $template
94727f238cSGreg Roach	 * @param string[] $cfg
9576692c8bSGreg Roach	 *
9676692c8bSGreg Roach	 * @return string
9776692c8bSGreg Roach	 */
98a9430be8SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []): string {
994b9ff166SGreg Roach		global $ctype, $WT_TREE;
1008c2e8227SGreg Roach
10112664b30SGreg Roach		$default_events = implode(',', self::DEFAULT_EVENTS);
10212664b30SGreg Roach
10312664b30SGreg Roach		$filter    = (bool) $this->getBlockSetting($block_id, 'filter', '1');
104e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
105e2a378d3SGreg Roach		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
10612664b30SGreg Roach		$events    = $this->getBlockSetting($block_id, 'events', $default_events);
1078c2e8227SGreg Roach
10812664b30SGreg Roach		foreach (['events', 'filter', 'infoStyle', 'sortStyle'] as $name) {
1098c2e8227SGreg Roach			if (array_key_exists($name, $cfg)) {
1108c2e8227SGreg Roach				$$name = $cfg[$name];
1118c2e8227SGreg Roach			}
1128c2e8227SGreg Roach		}
1138c2e8227SGreg Roach
11412664b30SGreg Roach		$event_array = explode(',', $events);
11512664b30SGreg Roach
11612664b30SGreg Roach		// If we are only showing living individuals, then we don't need to search for DEAT events.
11712664b30SGreg Roach		if ($filter) {
11812664b30SGreg Roach			$death_events = explode('|', WT_EVENTS_DEAT);
11912664b30SGreg Roach			$event_array = array_diff($event_array, $death_events);
12012664b30SGreg Roach		}
12112664b30SGreg Roach
12212664b30SGreg Roach		$events_filter = implode('|', $event_array);
12312664b30SGreg Roach
12412664b30SGreg Roach		$startjd = WT_CLIENT_JD;
12512664b30SGreg Roach		$endjd   = WT_CLIENT_JD;
12612664b30SGreg Roach
12712664b30SGreg Roach		$facts = FunctionsDB::getEventsList($startjd, $endjd, $events_filter, $filter, $sortStyle, $WT_TREE);
12812664b30SGreg Roach
129d8189b6aSDavid Drury		$summary = '';
1308c2e8227SGreg Roach
13112664b30SGreg Roach		if (empty($facts)) {
132d8189b6aSDavid Drury			if ($filter) {
133d8189b6aSDavid Drury				$summary = I18N::translate('No events for living individuals exist for today.');
134d8189b6aSDavid Drury			} else {
135d8189b6aSDavid Drury				$summary = I18N::translate('No events exist for today.');
136d8189b6aSDavid Drury			}
137d8189b6aSDavid Drury		}
138d8189b6aSDavid Drury
139d8189b6aSDavid Drury		$content = view('blocks/events-' . $infoStyle, [
140d8189b6aSDavid Drury				'facts'   => $facts,
141d8189b6aSDavid Drury				'summary' => $summary,
142d8189b6aSDavid Drury			]
143d8189b6aSDavid Drury		);
1448c2e8227SGreg Roach
1458c2e8227SGreg Roach		if ($template) {
146*397e599aSGreg Roach			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE)) {
147*397e599aSGreg Roach				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
148*397e599aSGreg Roach			} elseif ($ctype === 'user' && Auth::check()) {
149*397e599aSGreg Roach				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
1509c6524dcSGreg Roach			} else {
1519c6524dcSGreg Roach				$config_url = '';
1529c6524dcSGreg Roach			}
1539c6524dcSGreg Roach
154225e381fSGreg Roach			return view('blocks/template', [
1559c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
1569c6524dcSGreg Roach				'id'         => $block_id,
1579c6524dcSGreg Roach				'config_url' => $config_url,
1589c6524dcSGreg Roach				'title'      => $this->getTitle(),
1599c6524dcSGreg Roach				'content'    => $content,
16012664b30SGreg Roach			]);
1618c2e8227SGreg Roach		} else {
1628c2e8227SGreg Roach			return $content;
1638c2e8227SGreg Roach		}
1648c2e8227SGreg Roach	}
1658c2e8227SGreg Roach
1668c2e8227SGreg Roach	/** {@inheritdoc} */
167a9430be8SGreg Roach	public function loadAjax(): bool {
1688c2e8227SGreg Roach		return true;
1698c2e8227SGreg Roach	}
1708c2e8227SGreg Roach
1718c2e8227SGreg Roach	/** {@inheritdoc} */
172a9430be8SGreg Roach	public function isUserBlock(): bool {
1738c2e8227SGreg Roach		return true;
1748c2e8227SGreg Roach	}
1758c2e8227SGreg Roach
1768c2e8227SGreg Roach	/** {@inheritdoc} */
177a9430be8SGreg Roach	public function isGedcomBlock(): bool {
1788c2e8227SGreg Roach		return true;
1798c2e8227SGreg Roach	}
1808c2e8227SGreg Roach
18176692c8bSGreg Roach	/**
18276692c8bSGreg Roach	 * An HTML form to edit block settings
18376692c8bSGreg Roach	 *
18476692c8bSGreg Roach	 * @param int $block_id
185a9430be8SGreg Roach	 *
186a9430be8SGreg Roach	 * @return void
18776692c8bSGreg Roach	 */
188be9a728cSGreg Roach	public function configureBlock($block_id) {
1898c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
190e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'filter', Filter::postBool('filter'));
191e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
192e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'alpha|anniv', 'alpha'));
19312664b30SGreg Roach			$this->setBlockSetting($block_id, 'events', implode(',', Filter::postArray('events')));
1948c2e8227SGreg Roach		}
1958c2e8227SGreg Roach
19612664b30SGreg Roach		$default_events = implode(',', self::DEFAULT_EVENTS);
19712664b30SGreg Roach
198e2a378d3SGreg Roach		$filter    = $this->getBlockSetting($block_id, 'filter', '1');
199e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
200e2a378d3SGreg Roach		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
20112664b30SGreg Roach		$events    = $this->getBlockSetting($block_id, 'events', $default_events);
2028c2e8227SGreg Roach
20312664b30SGreg Roach		$event_array = explode(',', $events);
20412664b30SGreg Roach
20512664b30SGreg Roach		$all_events = [];
20612664b30SGreg Roach		foreach (self::ALL_EVENTS as $event) {
20712664b30SGreg Roach			$all_events[$event] = GedcomTag::getLabel($event);
20812664b30SGreg Roach		}
20915d603e7SGreg Roach		?>
21012664b30SGreg Roach
21115d603e7SGreg Roach		<div class="form-group row">
21215d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="filter">
21312664b30SGreg Roach				<?= I18N::translate('Show only events of living individuals') ?>
21415d603e7SGreg Roach			</label>
21515d603e7SGreg Roach			<div class="col-sm-9">
21615d603e7SGreg Roach				<?= Bootstrap4::radioButtons('filter', FunctionsEdit::optionsNoYes(), $filter, true) ?>
21715d603e7SGreg Roach			</div>
21815d603e7SGreg Roach		</div>
2198c2e8227SGreg Roach
22015d603e7SGreg Roach		<div class="form-group row">
22112664b30SGreg Roach			<label class="col-sm-3 col-form-label" for="events">
22212664b30SGreg Roach				<?= I18N::translate('Events') ?>
223d8189b6aSDavid Drury			</label>
224d8189b6aSDavid Drury			<div class="col-sm-9">
22512664b30SGreg Roach				<?= Bootstrap4::multiSelect($all_events, $event_array, ['id' => 'events', 'name' => 'events[]', 'class' => 'select2']) ?>
226d8189b6aSDavid Drury			</div>
227d8189b6aSDavid Drury		</div>
228d8189b6aSDavid Drury
229d8189b6aSDavid Drury		<div class="form-group row">
23015d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="infoStyle">
23115d603e7SGreg Roach				<?= /* I18N: Label for a configuration option */ I18N::translate('Presentation style') ?>
23215d603e7SGreg Roach			</label>
23315d603e7SGreg Roach			<div class="col-sm-9">
23412664b30SGreg Roach				<?= Bootstrap4::select(['list' => I18N::translate('list'), 'table' => I18N::translate('table')], $infoStyle, ['id' => 'infoStyle', 'name' => 'infoStyle']) ?>
23515d603e7SGreg Roach			</div>
23615d603e7SGreg Roach		</div>
2378c2e8227SGreg Roach
23815d603e7SGreg Roach		<div class="form-group row">
23915d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="sortStyle">
24015d603e7SGreg Roach				<?= /* I18N: Label for a configuration option */ I18N::translate('Sort order') ?>
24115d603e7SGreg Roach			</label>
24215d603e7SGreg Roach			<div class="col-sm-9">
24312664b30SGreg Roach				<?= Bootstrap4::select(['alpha' => /* I18N: An option in a list-box */ I18N::translate('sort by name'), 'anniv' => /* I18N: An option in a list-box */ I18N::translate('sort by date')], $sortStyle, ['id' => 'sortStyle', 'name' => 'sortStyle']) ?>
24415d603e7SGreg Roach			</div>
24515d603e7SGreg Roach		</div>
24615d603e7SGreg Roach		<?php
2478c2e8227SGreg Roach	}
2488c2e8227SGreg Roach}
249