xref: /webtrees/app/Module/UpcomingAnniversariesModule.php (revision faa002109662e488bd31ab20b489dc4c67a3bd56)
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\FunctionsDB;
22use Fisharebest\Webtrees\Functions\FunctionsEdit;
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    = (bool) $this->getBlockSetting($block_id, 'filter', '1');
62		$onlyBDM   = (bool) $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		$tags    = $onlyBDM ? 'BIRT MARR DEAT' : '';
75		$summary = '';
76
77		$facts = FunctionsDB::getEventsList($startjd, $endjd, $tags, $filter, $sortStyle, $WT_TREE);
78
79		if (count($facts) === 0) {
80			if ($filter) {
81				if ($endjd == $startjd) {
82					$summary = I18N::translate('No events for living individuals exist for tomorrow.');
83				} else {
84					// I18N: translation for %s==1 is unused; it is translated separately as “tomorrow”
85					$summary = I18N::plural('No events for living people exist for the next %s day.', 'No events for living people exist for the next %s days.', $endjd - $startjd + 1, I18N::number($endjd - $startjd + 1));
86				}
87			} else {
88				if ($endjd == $startjd) {
89					$summary = I18N::translate('No events exist for tomorrow.');
90				} else {
91					// I18N: translation for %s==1 is unused; it is translated separately as “tomorrow”
92					$summary = I18N::plural('No events exist for the next %s day.', 'No events exist for the next %s days.', $endjd - $startjd + 1, I18N::number($endjd - $startjd + 1));
93				}
94			}
95		}
96
97		$content = view('blocks/events-' . $infoStyle, [
98				'facts'   => $facts,
99				'summary' => $summary,
100			]
101		);
102
103		if ($template) {
104			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
105				$config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
106			} else {
107				$config_url = '';
108			}
109
110			return view('blocks/template', [
111				'block'      => str_replace('_', '-', $this->getName()),
112				'id'         => $block_id,
113				'config_url' => $config_url,
114				'title'      => $this->getTitle(),
115				'content'    => $content,
116			]);
117		} else {
118			return $content;
119		}
120	}
121
122	/** {@inheritdoc} */
123	public function loadAjax(): bool {
124		return true;
125	}
126
127	/** {@inheritdoc} */
128	public function isUserBlock(): bool {
129		return true;
130	}
131
132	/** {@inheritdoc} */
133	public function isGedcomBlock(): bool {
134		return true;
135	}
136
137	/**
138	 * An HTML form to edit block settings
139	 *
140	 * @param int $block_id
141	 *
142	 * @return void
143	 */
144	public function configureBlock($block_id) {
145		if (Filter::postBool('save') && Filter::checkCsrf()) {
146			$this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, 30, 7));
147			$this->setBlockSetting($block_id, 'filter', Filter::postBool('filter'));
148			$this->setBlockSetting($block_id, 'onlyBDM', Filter::postBool('onlyBDM'));
149			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
150			$this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'alpha|anniv', 'alpha'));
151		}
152
153		$days      = $this->getBlockSetting($block_id, 'days', '7');
154		$filter    = $this->getBlockSetting($block_id, 'filter', '1');
155		$onlyBDM   = $this->getBlockSetting($block_id, 'onlyBDM', '0');
156		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
157		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
158
159		?>
160		<div class="form-group row">
161			<label class="col-sm-3 col-form-label" for="days">
162				<?= I18N::translate('Number of days to show') ?>
163			</label>
164			<div class="col-sm-9">
165				<input type="text" name="days" id="days" size="2" value="<?= $days ?>">
166				<?= I18N::plural('maximum %s day', 'maximum %s days', 30, I18N::number(30)) ?>
167			</div>
168		</div>
169
170		<div class="form-group row">
171			<label class="col-sm-3 col-form-label" for="filter">
172				<?= I18N::translate('Show only events of living individuals') ?>
173			</label>
174			<div class="col-sm-9">
175				<?= Bootstrap4::radioButtons('filter', FunctionsEdit::optionsNoYes(), $filter, true) ?>
176			</div>
177		</div>
178
179		<div class="form-group row">
180			<label class="col-sm-3 col-form-label" for="onlyBDM">
181				<?= I18N::translate('Show only births, deaths, and marriages') ?>
182			</label>
183			<div class="col-sm-9">
184				<?= Bootstrap4::radioButtons('onlyBDM', FunctionsEdit::optionsNoYes(), $onlyBDM, true) ?>
185			</div>
186		</div>
187
188		<div class="form-group row">
189			<label class="col-sm-3 col-form-label" for="infoStyle">
190				<?= I18N::translate('Presentation style') ?>
191			</label>
192			<div class="col-sm-9">
193				<?= Bootstrap4::select(['list' => I18N::translate('list'), 'table' => I18N::translate('table')], $infoStyle, ['id' => 'infoStyle', 'name' => 'infoStyle']) ?>
194			</div>
195		</div>
196
197		<div class="form-group row">
198			<label class="col-sm-3 col-form-label" for="sortStyle">
199				<?= I18N::translate('Sort order') ?>
200			</label>
201			<div class="col-sm-9">
202				<?= 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']) ?>
203			</div>
204		</div>
205		<?php
206	}
207}
208