xref: /webtrees/app/Module/UpcomingAnniversariesModule.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 UpcomingAnniversariesModule
27 */
28class UpcomingAnniversariesModule extends AbstractModule implements ModuleBlockInterface {
29	/**
30	 * How should this module be labelled on tabs, menus, etc.?
31	 *
32	 * @return string
33	 */
34	public function getTitle() {
35		return /* I18N: Name of a module */ I18N::translate('Upcoming events');
36	}
37
38	/**
39	 * A sentence describing what this module does.
40	 *
41	 * @return string
42	 */
43	public function getDescription() {
44		return /* I18N: Description of the “Upcoming events” module */ I18N::translate('A list of the anniversaries that will occur in the near future.');
45	}
46
47	/**
48	 * Generate the HTML content of this block.
49	 *
50	 * @param int      $block_id
51	 * @param bool     $template
52	 * @param string[] $cfg
53	 *
54	 * @return string
55	 */
56	public function getBlock($block_id, $template = true, $cfg = array()) {
57		global $ctype, $WT_TREE;
58
59		$days      = $this->getBlockSetting($block_id, 'days', '7');
60		$filter    = $this->getBlockSetting($block_id, 'filter', '1');
61		$onlyBDM   = $this->getBlockSetting($block_id, 'onlyBDM', '0');
62		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
63		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
64		$block     = $this->getBlockSetting($block_id, 'block', '1');
65
66		foreach (array('days', 'filter', 'onlyBDM', 'infoStyle', 'sortStyle', 'block') as $name) {
67			if (array_key_exists($name, $cfg)) {
68				$$name = $cfg[$name];
69			}
70		}
71
72		$startjd = WT_CLIENT_JD + 1;
73		$endjd   = WT_CLIENT_JD + $days;
74
75		$id    = $this->getName() . $block_id;
76		$class = $this->getName() . '_block';
77		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
78			$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>';
79		} else {
80			$title = '';
81		}
82		$title .= $this->getTitle();
83
84		$content = '';
85		switch ($infoStyle) {
86		case 'list':
87			// Output style 1:  Old format, no visible tables, much smaller text. Better suited to right side of page.
88			$content .= FunctionsPrintLists::eventsList($startjd, $endjd, $onlyBDM ? 'BIRT MARR DEAT' : '', $filter, $sortStyle);
89			break;
90		case 'table':
91			// Style 2: New format, tables, big text, etc. Not too good on right side of page
92			ob_start();
93			$content .= FunctionsPrintLists::eventsTable($startjd, $endjd, $onlyBDM ? 'BIRT MARR DEAT' : '', $filter, $sortStyle);
94			$content .= ob_get_clean();
95			break;
96		}
97
98		if ($template) {
99			if ($block) {
100				$class .= ' small_inner_block';
101			}
102
103			return Theme::theme()->formatBlock($id, $title, $class, $content);
104		} else {
105			return $content;
106		}
107	}
108
109	/** {@inheritdoc} */
110	public function loadAjax() {
111		return true;
112	}
113
114	/** {@inheritdoc} */
115	public function isUserBlock() {
116		return true;
117	}
118
119	/** {@inheritdoc} */
120	public function isGedcomBlock() {
121		return true;
122	}
123
124	/**
125	 * An HTML form to edit block settings
126	 *
127	 * @param int $block_id
128	 */
129	public function configureBlock($block_id) {
130		if (Filter::postBool('save') && Filter::checkCsrf()) {
131			$this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, 30, 7));
132			$this->setBlockSetting($block_id, 'filter', Filter::postBool('filter'));
133			$this->setBlockSetting($block_id, 'onlyBDM', Filter::postBool('onlyBDM'));
134			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
135			$this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'alpha|anniv', 'alpha'));
136			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
137		}
138
139		$days      = $this->getBlockSetting($block_id, 'days', '7');
140		$filter    = $this->getBlockSetting($block_id, 'filter', '1');
141		$onlyBDM   = $this->getBlockSetting($block_id, 'onlyBDM', '0');
142		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
143		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
144		$block     = $this->getBlockSetting($block_id, 'block', '1');
145
146		echo '<tr><td class="descriptionbox wrap width33">';
147		echo I18N::translate('Number of days to show');
148		echo '</td><td class="optionbox">';
149		echo '<input type="text" name="days" size="2" value="', $days, '">';
150		echo ' <em>', I18N::plural('maximum %s day', 'maximum %s days', 30, I18N::number(30)), '</em>';
151		echo '</td></tr>';
152
153		echo '<tr><td class="descriptionbox wrap width33">';
154		echo I18N::translate('Show only events of living individuals');
155		echo '</td><td class="optionbox">';
156		echo FunctionsEdit::editFieldYesNo('filter', $filter);
157		echo '</td></tr>';
158
159		echo '<tr><td class="descriptionbox wrap width33">';
160		echo I18N::translate('Show only births, deaths, and marriages');
161		echo '</td><td class="optionbox">';
162		echo FunctionsEdit::editFieldYesNo('onlyBDM', $onlyBDM);
163		echo '</td></tr>';
164
165		echo '<tr><td class="descriptionbox wrap width33">';
166		echo I18N::translate('Presentation style');
167		echo '</td><td class="optionbox">';
168		echo FunctionsEdit::selectEditControl('infoStyle', array('list' => I18N::translate('list'), 'table' => I18N::translate('table')), null, $infoStyle, '');
169		echo '</td></tr>';
170
171		echo '<tr><td class="descriptionbox wrap width33">';
172		echo I18N::translate('Sort order');
173		echo '</td><td class="optionbox">';
174		echo FunctionsEdit::selectEditControl('sortStyle', array(
175			/* I18N: An option in a list-box */ 'alpha' => I18N::translate('sort by name'),
176			/* I18N: An option in a list-box */ 'anniv' => I18N::translate('sort by date'),
177		), null, $sortStyle, '');
178		echo '</td></tr>';
179
180		echo '<tr><td class="descriptionbox wrap width33">';
181		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
182		echo '</td><td class="optionbox">';
183		echo FunctionsEdit::editFieldYesNo('block', $block);
184		echo '</td></tr>';
185	}
186}
187