xref: /webtrees/app/Module/UpcomingAnniversariesModule.php (revision 530749aa3936273ddfb12c66ee2eafdac35cde61)
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\FontAwesome;
22use Fisharebest\Webtrees\Functions\FunctionsEdit;
23use Fisharebest\Webtrees\Functions\FunctionsPrintLists;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Theme;
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		$id    = $this->getName() . $block_id;
77		$class = $this->getName() . '_block';
78		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
79			$title = FontAwesome::linkIcon('preferences', I18N::translate('Preferences'), ['class' => 'btn btn-link', 'href' => 'block_edit.php?block_id=' . $block_id . '&ged=' . $WT_TREE->getNameHtml() . '&ctype=' . $ctype]) . ' ';
80		} else {
81			$title = '';
82		}
83		$title .= $this->getTitle();
84
85		$content = '';
86		switch ($infoStyle) {
87		case 'list':
88			// Output style 1:  Old format, no visible tables, much smaller text. Better suited to right side of page.
89			$content .= FunctionsPrintLists::eventsList($startjd, $endjd, $onlyBDM ? 'BIRT MARR DEAT' : '', $filter, $sortStyle);
90			break;
91		case 'table':
92			// Style 2: New format, tables, big text, etc. Not too good on right side of page
93			ob_start();
94			$content .= FunctionsPrintLists::eventsTable($startjd, $endjd, $onlyBDM ? 'BIRT MARR DEAT' : '', $filter, $sortStyle);
95			$content .= ob_get_clean();
96			break;
97		}
98
99		if ($template) {
100			return Theme::theme()->formatBlock($id, $title, $class, $content);
101		} else {
102			return $content;
103		}
104	}
105
106	/** {@inheritdoc} */
107	public function loadAjax() {
108		return true;
109	}
110
111	/** {@inheritdoc} */
112	public function isUserBlock() {
113		return true;
114	}
115
116	/** {@inheritdoc} */
117	public function isGedcomBlock() {
118		return true;
119	}
120
121	/**
122	 * An HTML form to edit block settings
123	 *
124	 * @param int $block_id
125	 */
126	public function configureBlock($block_id) {
127		if (Filter::postBool('save') && Filter::checkCsrf()) {
128			$this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, 30, 7));
129			$this->setBlockSetting($block_id, 'filter', Filter::postBool('filter'));
130			$this->setBlockSetting($block_id, 'onlyBDM', Filter::postBool('onlyBDM'));
131			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
132			$this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'alpha|anniv', 'alpha'));
133		}
134
135		$days      = $this->getBlockSetting($block_id, 'days', '7');
136		$filter    = $this->getBlockSetting($block_id, 'filter', '1');
137		$onlyBDM   = $this->getBlockSetting($block_id, 'onlyBDM', '0');
138		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
139		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
140
141		?>
142		<div class="form-group row">
143			<label class="col-sm-3 col-form-label" for="days">
144				<?= I18N::translate('Number of days to show') ?>
145			</label>
146			<div class="col-sm-9">
147				<input type="text" name="days" id="days" size="2" value="<?= $days ?>">
148				<?= I18N::plural('maximum %s day', 'maximum %s days', 30, I18N::number(30)) ?>
149			</div>
150		</div>
151
152		<div class="form-group row">
153			<label class="col-sm-3 col-form-label" for="filter">
154				<?= I18N::translate('Show only events of living individuals') ?>
155			</label>
156			<div class="col-sm-9">
157				<?= Bootstrap4::radioButtons('filter', FunctionsEdit::optionsNoYes(), $filter, true) ?>
158			</div>
159		</div>
160
161		<div class="form-group row">
162			<label class="col-sm-3 col-form-label" for="onlyBDM">
163				<?= I18N::translate('Show only births, deaths, and marriages') ?>
164			</label>
165			<div class="col-sm-9">
166				<?= Bootstrap4::radioButtons('onlyBDM', FunctionsEdit::optionsNoYes(), $onlyBDM, true) ?>
167			</div>
168		</div>
169
170		<div class="form-group row">
171			<label class="col-sm-3 col-form-label" for="infoStyle">
172				<?= I18N::translate('Presentation style') ?>
173			</label>
174			<div class="col-sm-9">
175				<?= Bootstrap4::select(['list' => I18N::translate('list'), 'table' => I18N::translate('table')], $infoStyle, ['id' => 'infoStyle', 'name' => 'infoStyle']) ?>
176			</div>
177		</div>
178
179		<div class="form-group row">
180			<label class="col-sm-3 col-form-label" for="sortStyle">
181				<?= I18N::translate('Sort order') ?>
182			</label>
183			<div class="col-sm-9">
184				<?= 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']) ?>
185			</div>
186		</div>
187		<?php
188	}
189}
190