xref: /webtrees/app/Module/OnThisDayModule.php (revision 3e983931fdde6db78f1490364106d7d46e77dea7)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 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\Bootstrap4;
20use Fisharebest\Webtrees\Filter;
21use Fisharebest\Webtrees\Functions\FunctionsEdit;
22use Fisharebest\Webtrees\Functions\FunctionsPrintLists;
23use Fisharebest\Webtrees\Html;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\View;
26
27/**
28 * Class OnThisDayModule
29 */
30class OnThisDayModule extends AbstractModule implements ModuleBlockInterface {
31	/** {@inheritdoc} */
32	public function getTitle() {
33		return /* I18N: Name of a module */ I18N::translate('On this day');
34	}
35
36	/** {@inheritdoc} */
37	public function getDescription() {
38		return /* I18N: Description of the “On this day” module */ I18N::translate('A list of the anniversaries that occur today.');
39	}
40
41	/**
42	 * Generate the HTML content of this block.
43	 *
44	 * @param int      $block_id
45	 * @param bool     $template
46	 * @param string[] $cfg
47	 *
48	 * @return string
49	 */
50	public function getBlock($block_id, $template = true, $cfg = []): string {
51		global $ctype, $WT_TREE;
52
53		$filter    = $this->getBlockSetting($block_id, 'filter', '1');
54		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
55		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
56
57		foreach (['filter', 'infoStyle', 'sortStyle'] as $name) {
58			if (array_key_exists($name, $cfg)) {
59				$$name = $cfg[$name];
60			}
61		}
62
63		$todayjd = WT_CLIENT_JD;
64
65		$content = '';
66
67		// If we are only showing living individuals, then we don't need to search for DEAT events.
68		$tags = $filter ? 'BIRT MARR' : 'BIRT MARR DEAT';
69
70		switch ($infoStyle) {
71			case 'list':
72				// Output style 1:  Old format, no visible tables, much smaller text. Better suited to right side of page.
73				$content .= FunctionsPrintLists::eventsList($todayjd, $todayjd, $tags, $filter, $sortStyle);
74				break;
75			case 'table':
76				// Style 2: New format, tables, big text, etc. Not too good on right side of page
77				ob_start();
78				$content .= FunctionsPrintLists::eventsTable($todayjd, $todayjd, $tags, $filter, $sortStyle);
79				$content .= ob_get_clean();
80				break;
81		}
82
83		if ($template) {
84			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
85				$config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
86			} else {
87				$config_url = '';
88			}
89
90			return View::make('blocks/template', [
91				'block'      => str_replace('_', '-', $this->getName()),
92				'id'         => $block_id,
93				'config_url' => $config_url,
94				'title'      => $this->getTitle(),
95				'content'    => $content,
96			]);
97		} else {
98			return $content;
99		}
100	}
101
102	/** {@inheritdoc} */
103	public function loadAjax(): bool {
104		return true;
105	}
106
107	/** {@inheritdoc} */
108	public function isUserBlock(): bool {
109		return true;
110	}
111
112	/** {@inheritdoc} */
113	public function isGedcomBlock(): bool {
114		return true;
115	}
116
117	/**
118	 * An HTML form to edit block settings
119	 *
120	 * @param int $block_id
121	 *
122	 * @return void
123	 */
124	public function configureBlock($block_id): void {
125		if (Filter::postBool('save') && Filter::checkCsrf()) {
126			$this->setBlockSetting($block_id, 'filter', Filter::postBool('filter'));
127			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
128			$this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'alpha|anniv', 'alpha'));
129		}
130
131		$filter    = $this->getBlockSetting($block_id, 'filter', '1');
132		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
133		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
134
135		?>
136		<div class="form-group row">
137			<label class="col-sm-3 col-form-label" for="filter">
138				<?= /* I18N: Label for a configuration option */ I18N::translate('Show only events of living individuals') ?>
139			</label>
140			<div class="col-sm-9">
141				<?= Bootstrap4::radioButtons('filter', FunctionsEdit::optionsNoYes(), $filter, true) ?>
142			</div>
143		</div>
144
145		<div class="form-group row">
146			<label class="col-sm-3 col-form-label" for="infoStyle">
147				<?= /* I18N: Label for a configuration option */ I18N::translate('Presentation style') ?>
148			</label>
149			<div class="col-sm-9">
150				<?= Bootstrap4::select(['list' => I18N::translate('list'), 'table' => I18N::translate('table')], $infoStyle, ['id' => 'infoStyle', 'name' => 'infoStyle']) ?>
151			</div>
152		</div>
153
154		<div class="form-group row">
155			<label class="col-sm-3 col-form-label" for="sortStyle">
156				<?= /* I18N: Label for a configuration option */ I18N::translate('Sort order') ?>
157			</label>
158			<div class="col-sm-9">
159				<?= 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']) ?>
160			</div>
161		</div>
162		<?php
163	}
164}
165