xref: /webtrees/app/Module/OnThisDayModule.php (revision 241f182934a11b1e3bd3e3fb316f3f3f2c05b074)
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 = []) {
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() {
104		return true;
105	}
106
107	/** {@inheritdoc} */
108	public function isUserBlock() {
109		return true;
110	}
111
112	/** {@inheritdoc} */
113	public function isGedcomBlock() {
114		return true;
115	}
116
117	/**
118	 * An HTML form to edit block settings
119	 *
120	 * @param int $block_id
121	 */
122	public function configureBlock($block_id) {
123		if (Filter::postBool('save') && Filter::checkCsrf()) {
124			$this->setBlockSetting($block_id, 'filter', Filter::postBool('filter'));
125			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
126			$this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'alpha|anniv', 'alpha'));
127		}
128
129		$filter    = $this->getBlockSetting($block_id, 'filter', '1');
130		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
131		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
132
133		?>
134		<div class="form-group row">
135			<label class="col-sm-3 col-form-label" for="filter">
136				<?= /* I18N: Label for a configuration option */ I18N::translate('Show only events of living individuals') ?>
137			</label>
138			<div class="col-sm-9">
139				<?= Bootstrap4::radioButtons('filter', FunctionsEdit::optionsNoYes(), $filter, true) ?>
140			</div>
141		</div>
142
143		<div class="form-group row">
144			<label class="col-sm-3 col-form-label" for="infoStyle">
145				<?= /* I18N: Label for a configuration option */ I18N::translate('Presentation style') ?>
146			</label>
147			<div class="col-sm-9">
148				<?= Bootstrap4::select(['list' => I18N::translate('list'), 'table' => I18N::translate('table')], $infoStyle, ['id' => 'infoStyle', 'name' => 'infoStyle']) ?>
149			</div>
150		</div>
151
152		<div class="form-group row">
153			<label class="col-sm-3 col-form-label" for="sortStyle">
154				<?= /* I18N: Label for a configuration option */ I18N::translate('Sort order') ?>
155			</label>
156			<div class="col-sm-9">
157				<?= 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']) ?>
158			</div>
159		</div>
160		<?php
161	}
162}
163