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