xref: /webtrees/app/Module/OnThisDayModule.php (revision 9797fe2e6d6eb3d34559dcf546f47c58bc50777b)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2016 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\FunctionsEdit;
21use Fisharebest\Webtrees\Functions\FunctionsPrintLists;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Theme;
24
25/**
26 * Class OnThisDayModule
27 */
28class OnThisDayModule extends AbstractModule implements ModuleBlockInterface {
29	/** {@inheritdoc} */
30	public function getTitle() {
31		return /* I18N: Name of a module */ I18N::translate('On this day');
32	}
33
34	/** {@inheritdoc} */
35	public function getDescription() {
36		return /* I18N: Description of the “On this day” module */ I18N::translate('A list of the anniversaries that occur today.');
37	}
38
39	/**
40	 * Generate the HTML content of this block.
41	 *
42	 * @param int      $block_id
43	 * @param bool     $template
44	 * @param string[] $cfg
45	 *
46	 * @return string
47	 */
48	public function getBlock($block_id, $template = true, $cfg = array()) {
49		global $ctype, $WT_TREE;
50
51		$filter    = $this->getBlockSetting($block_id, 'filter', '1');
52		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
53		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
54		$block     = $this->getBlockSetting($block_id, 'block', '1');
55
56		foreach (array('filter', 'infoStyle', 'sortStyle', 'block') as $name) {
57			if (array_key_exists($name, $cfg)) {
58				$$name = $cfg[$name];
59			}
60		}
61
62		$todayjd = WT_CLIENT_JD;
63
64		$id    = $this->getName() . $block_id;
65		$class = $this->getName() . '_block';
66		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
67			$title = '<a class="icon-admin" title="' . I18N::translate('Preferences') . '" href="block_edit.php?block_id=' . $block_id . '&amp;ged=' . $WT_TREE->getNameHtml() . '&amp;ctype=' . $ctype . '"></a>';
68		} else {
69			$title = '';
70		}
71		$title .= $this->getTitle();
72
73		$content = '';
74
75		// If we are only showing living individuals, then we don't need to search for DEAT events.
76		$tags = $filter ? 'BIRT MARR' : 'BIRT MARR DEAT';
77
78		switch ($infoStyle) {
79		case 'list':
80			// Output style 1:  Old format, no visible tables, much smaller text. Better suited to right side of page.
81			$content .= FunctionsPrintLists::eventsList($todayjd, $todayjd, $tags, $filter, $sortStyle);
82			break;
83		case 'table':
84			// Style 2: New format, tables, big text, etc. Not too good on right side of page
85			ob_start();
86			$content .= FunctionsPrintLists::eventsTable($todayjd, $todayjd, $tags, $filter, $sortStyle);
87			$content .= ob_get_clean();
88			break;
89		}
90
91		if ($template) {
92			if ($block) {
93				$class .= ' small_inner_block';
94			}
95
96			return Theme::theme()->formatBlock($id, $title, $class, $content);
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			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
128		}
129
130		$filter    = $this->getBlockSetting($block_id, 'filter', '1');
131		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
132		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
133		$block     = $this->getBlockSetting($block_id, 'block', '1');
134
135		echo '<tr><td class="descriptionbox wrap width33">';
136		echo /* I18N: Label for a configuration option */ I18N::translate('Show only events of living individuals');
137		echo '</td><td class="optionbox">';
138		echo FunctionsEdit::editFieldYesNo('filter', $filter);
139		echo '</td></tr>';
140
141		echo '<tr><td class="descriptionbox wrap width33">';
142		echo /* I18N: Label for a configuration option */ I18N::translate('Presentation style');
143		echo '</td><td class="optionbox">';
144		echo FunctionsEdit::selectEditControl('infoStyle', array('list' => I18N::translate('list'), 'table' => I18N::translate('table')), null, $infoStyle, '');
145		echo '</td></tr>';
146
147		echo '<tr><td class="descriptionbox wrap width33">';
148		echo /* I18N: Label for a configuration option */ I18N::translate('Sort order');
149		echo '</td><td class="optionbox">';
150		echo FunctionsEdit::selectEditControl('sortStyle', array(
151			/* I18N: An option in a list-box */ 'alpha' => I18N::translate('sort by name'),
152			/* I18N: An option in a list-box */ 'anniv' => I18N::translate('sort by date'),
153		), null, $sortStyle, '');
154		echo '</td></tr>';
155
156		echo '<tr><td class="descriptionbox wrap width33">';
157		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
158		echo '</td><td class="optionbox">';
159		echo FunctionsEdit::editFieldYesNo('block', $block);
160		echo '</td></tr>';
161	}
162}
163