xref: /webtrees/app/Module/UpcomingAnniversariesModule.php (revision ecf66805cdf435857ce4ed0eb577cfd1296f8989)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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 = []): string {
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(): bool {
111		return true;
112	}
113
114	/** {@inheritdoc} */
115	public function isUserBlock(): bool {
116		return true;
117	}
118
119	/** {@inheritdoc} */
120	public function isGedcomBlock(): bool {
121		return true;
122	}
123
124	/**
125	 * An HTML form to edit block settings
126	 *
127	 * @param int $block_id
128	 *
129	 * @return void
130	 */
131	public function configureBlock($block_id) {
132		if (Filter::postBool('save') && Filter::checkCsrf()) {
133			$this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, 30, 7));
134			$this->setBlockSetting($block_id, 'filter', Filter::postBool('filter'));
135			$this->setBlockSetting($block_id, 'onlyBDM', Filter::postBool('onlyBDM'));
136			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
137			$this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'alpha|anniv', 'alpha'));
138		}
139
140		$days      = $this->getBlockSetting($block_id, 'days', '7');
141		$filter    = $this->getBlockSetting($block_id, 'filter', '1');
142		$onlyBDM   = $this->getBlockSetting($block_id, 'onlyBDM', '0');
143		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
144		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
145
146		?>
147		<div class="form-group row">
148			<label class="col-sm-3 col-form-label" for="days">
149				<?= I18N::translate('Number of days to show') ?>
150			</label>
151			<div class="col-sm-9">
152				<input type="text" name="days" id="days" size="2" value="<?= $days ?>">
153				<?= I18N::plural('maximum %s day', 'maximum %s days', 30, I18N::number(30)) ?>
154			</div>
155		</div>
156
157		<div class="form-group row">
158			<label class="col-sm-3 col-form-label" for="filter">
159				<?= I18N::translate('Show only events of living individuals') ?>
160			</label>
161			<div class="col-sm-9">
162				<?= Bootstrap4::radioButtons('filter', FunctionsEdit::optionsNoYes(), $filter, true) ?>
163			</div>
164		</div>
165
166		<div class="form-group row">
167			<label class="col-sm-3 col-form-label" for="onlyBDM">
168				<?= I18N::translate('Show only births, deaths, and marriages') ?>
169			</label>
170			<div class="col-sm-9">
171				<?= Bootstrap4::radioButtons('onlyBDM', FunctionsEdit::optionsNoYes(), $onlyBDM, true) ?>
172			</div>
173		</div>
174
175		<div class="form-group row">
176			<label class="col-sm-3 col-form-label" for="infoStyle">
177				<?= I18N::translate('Presentation style') ?>
178			</label>
179			<div class="col-sm-9">
180				<?= Bootstrap4::select(['list' => I18N::translate('list'), 'table' => I18N::translate('table')], $infoStyle, ['id' => 'infoStyle', 'name' => 'infoStyle']) ?>
181			</div>
182		</div>
183
184		<div class="form-group row">
185			<label class="col-sm-3 col-form-label" for="sortStyle">
186				<?= I18N::translate('Sort order') ?>
187			</label>
188			<div class="col-sm-9">
189				<?= 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']) ?>
190			</div>
191		</div>
192		<?php
193	}
194}
195