xref: /webtrees/app/Module/UpcomingAnniversariesModule.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 UpcomingAnniversariesModule
29 */
30class UpcomingAnniversariesModule extends AbstractModule implements ModuleBlockInterface {
31	/**
32	 * How should this module be labelled on tabs, menus, etc.?
33	 *
34	 * @return string
35	 */
36	public function getTitle() {
37		return /* I18N: Name of a module */ I18N::translate('Upcoming events');
38	}
39
40	/**
41	 * A sentence describing what this module does.
42	 *
43	 * @return string
44	 */
45	public function getDescription() {
46		return /* I18N: Description of the “Upcoming events” module */ I18N::translate('A list of the anniversaries that will occur in the near future.');
47	}
48
49	/**
50	 * Generate the HTML content of this block.
51	 *
52	 * @param int      $block_id
53	 * @param bool     $template
54	 * @param string[] $cfg
55	 *
56	 * @return string
57	 */
58	public function getBlock($block_id, $template = true, $cfg = []) {
59		global $ctype, $WT_TREE;
60
61		$days      = $this->getBlockSetting($block_id, 'days', '7');
62		$filter    = $this->getBlockSetting($block_id, 'filter', '1');
63		$onlyBDM   = $this->getBlockSetting($block_id, 'onlyBDM', '0');
64		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
65		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
66
67		foreach (['days', 'filter', 'onlyBDM', 'infoStyle', 'sortStyle'] as $name) {
68			if (array_key_exists($name, $cfg)) {
69				$$name = $cfg[$name];
70			}
71		}
72
73		$startjd = WT_CLIENT_JD + 1;
74		$endjd   = WT_CLIENT_JD + $days;
75
76		$content = '';
77		switch ($infoStyle) {
78		case 'list':
79			// Output style 1:  Old format, no visible tables, much smaller text. Better suited to right side of page.
80			$content .= FunctionsPrintLists::eventsList($startjd, $endjd, $onlyBDM ? 'BIRT MARR DEAT' : '', $filter, $sortStyle);
81			break;
82		case 'table':
83			// Style 2: New format, tables, big text, etc. Not too good on right side of page
84			ob_start();
85			$content .= FunctionsPrintLists::eventsTable($startjd, $endjd, $onlyBDM ? 'BIRT MARR DEAT' : '', $filter, $sortStyle);
86			$content .= ob_get_clean();
87			break;
88		}
89
90		if ($template) {
91			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
92				$config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
93			} else {
94				$config_url = '';
95			}
96
97			return View::make('blocks/template', [
98				'block'      => str_replace('_', '-', $this->getName()),
99				'id'         => $block_id,
100				'config_url' => $config_url,
101				'title'      => $this->getTitle(),
102				'content'    => $content,
103			]);
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		}
137
138		$days      = $this->getBlockSetting($block_id, 'days', '7');
139		$filter    = $this->getBlockSetting($block_id, 'filter', '1');
140		$onlyBDM   = $this->getBlockSetting($block_id, 'onlyBDM', '0');
141		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
142		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
143
144		?>
145		<div class="form-group row">
146			<label class="col-sm-3 col-form-label" for="days">
147				<?= I18N::translate('Number of days to show') ?>
148			</label>
149			<div class="col-sm-9">
150				<input type="text" name="days" id="days" size="2" value="<?= $days ?>">
151				<?= I18N::plural('maximum %s day', 'maximum %s days', 30, I18N::number(30)) ?>
152			</div>
153		</div>
154
155		<div class="form-group row">
156			<label class="col-sm-3 col-form-label" for="filter">
157				<?= I18N::translate('Show only events of living individuals') ?>
158			</label>
159			<div class="col-sm-9">
160				<?= Bootstrap4::radioButtons('filter', FunctionsEdit::optionsNoYes(), $filter, true) ?>
161			</div>
162		</div>
163
164		<div class="form-group row">
165			<label class="col-sm-3 col-form-label" for="onlyBDM">
166				<?= I18N::translate('Show only births, deaths, and marriages') ?>
167			</label>
168			<div class="col-sm-9">
169				<?= Bootstrap4::radioButtons('onlyBDM', FunctionsEdit::optionsNoYes(), $onlyBDM, true) ?>
170			</div>
171		</div>
172
173		<div class="form-group row">
174			<label class="col-sm-3 col-form-label" for="infoStyle">
175				<?= I18N::translate('Presentation style') ?>
176			</label>
177			<div class="col-sm-9">
178				<?= Bootstrap4::select(['list' => I18N::translate('list'), 'table' => I18N::translate('table')], $infoStyle, ['id' => 'infoStyle', 'name' => 'infoStyle']) ?>
179			</div>
180		</div>
181
182		<div class="form-group row">
183			<label class="col-sm-3 col-form-label" for="sortStyle">
184				<?= I18N::translate('Sort order') ?>
185			</label>
186			<div class="col-sm-9">
187				<?= Bootstrap4::select([/* I18N: An option in a list-box */ 'alpha' => I18N::translate('sort by name'), /* I18N: An option in a list-box */ 'anniv' => I18N::translate('sort by date')], $sortStyle, ['id' => 'sortStyle', 'name' => 'sortStyle']) ?>
188			</div>
189		</div>
190		<?php
191	}
192}
193