xref: /webtrees/app/Module/UpcomingAnniversariesModule.php (revision 15d603e7c7c15d20f055d3d9c38d6b133453c5be)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
46bdf7674SGreg Roach * Copyright (C) 2017 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
19*15d603e7SGreg Roachuse Fisharebest\Webtrees\Bootstrap4;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
21*15d603e7SGreg Roachuse Fisharebest\Webtrees\FontAwesome;
223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit;
233d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintLists;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Theme;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class UpcomingAnniversariesModule
298c2e8227SGreg Roach */
30e2a378d3SGreg Roachclass UpcomingAnniversariesModule extends AbstractModule implements ModuleBlockInterface {
3176692c8bSGreg Roach	/**
3276692c8bSGreg Roach	 * How should this module be labelled on tabs, menus, etc.?
3376692c8bSGreg Roach	 *
3476692c8bSGreg Roach	 * @return string
3576692c8bSGreg Roach	 */
368c2e8227SGreg Roach	public function getTitle() {
378c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('Upcoming events');
388c2e8227SGreg Roach	}
398c2e8227SGreg Roach
4076692c8bSGreg Roach	/**
4176692c8bSGreg Roach	 * A sentence describing what this module does.
4276692c8bSGreg Roach	 *
4376692c8bSGreg Roach	 * @return string
4476692c8bSGreg Roach	 */
458c2e8227SGreg Roach	public function getDescription() {
468c2e8227SGreg Roach		return /* I18N: Description of the “Upcoming events” module */ I18N::translate('A list of the anniversaries that will occur in the near future.');
478c2e8227SGreg Roach	}
488c2e8227SGreg Roach
4976692c8bSGreg Roach	/**
5076692c8bSGreg Roach	 * Generate the HTML content of this block.
5176692c8bSGreg Roach	 *
5276692c8bSGreg Roach	 * @param int      $block_id
5376692c8bSGreg Roach	 * @param bool     $template
54727f238cSGreg Roach	 * @param string[] $cfg
5576692c8bSGreg Roach	 *
5676692c8bSGreg Roach	 * @return string
5776692c8bSGreg Roach	 */
5813abd6f3SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []) {
594b9ff166SGreg Roach		global $ctype, $WT_TREE;
608c2e8227SGreg Roach
61e2a378d3SGreg Roach		$days      = $this->getBlockSetting($block_id, 'days', '7');
62e2a378d3SGreg Roach		$filter    = $this->getBlockSetting($block_id, 'filter', '1');
63e2a378d3SGreg Roach		$onlyBDM   = $this->getBlockSetting($block_id, 'onlyBDM', '0');
64e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
65e2a378d3SGreg Roach		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
668c2e8227SGreg Roach
67*15d603e7SGreg Roach		foreach (['days', 'filter', 'onlyBDM', 'infoStyle', 'sortStyle'] as $name) {
688c2e8227SGreg Roach			if (array_key_exists($name, $cfg)) {
698c2e8227SGreg Roach				$$name = $cfg[$name];
708c2e8227SGreg Roach			}
718c2e8227SGreg Roach		}
728c2e8227SGreg Roach
738c2e8227SGreg Roach		$startjd = WT_CLIENT_JD + 1;
748c2e8227SGreg Roach		$endjd   = WT_CLIENT_JD + $days;
758c2e8227SGreg Roach
768c2e8227SGreg Roach		$id    = $this->getName() . $block_id;
778c2e8227SGreg Roach		$class = $this->getName() . '_block';
784b9ff166SGreg Roach		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
79*15d603e7SGreg Roach			$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]) . ' ';
808c2e8227SGreg Roach		} else {
818c2e8227SGreg Roach			$title = '';
828c2e8227SGreg Roach		}
838c2e8227SGreg Roach		$title .= $this->getTitle();
848c2e8227SGreg Roach
858c2e8227SGreg Roach		$content = '';
868c2e8227SGreg Roach		switch ($infoStyle) {
878c2e8227SGreg Roach		case 'list':
888c2e8227SGreg Roach			// Output style 1:  Old format, no visible tables, much smaller text. Better suited to right side of page.
893d7a8a4cSGreg Roach			$content .= FunctionsPrintLists::eventsList($startjd, $endjd, $onlyBDM ? 'BIRT MARR DEAT' : '', $filter, $sortStyle);
908c2e8227SGreg Roach			break;
918c2e8227SGreg Roach		case 'table':
928c2e8227SGreg Roach			// Style 2: New format, tables, big text, etc. Not too good on right side of page
938c2e8227SGreg Roach			ob_start();
943d7a8a4cSGreg Roach			$content .= FunctionsPrintLists::eventsTable($startjd, $endjd, $onlyBDM ? 'BIRT MARR DEAT' : '', $filter, $sortStyle);
958c2e8227SGreg Roach			$content .= ob_get_clean();
968c2e8227SGreg Roach			break;
978c2e8227SGreg Roach		}
988c2e8227SGreg Roach
998c2e8227SGreg Roach		if ($template) {
1008c2e8227SGreg Roach			return Theme::theme()->formatBlock($id, $title, $class, $content);
1018c2e8227SGreg Roach		} else {
1028c2e8227SGreg Roach			return $content;
1038c2e8227SGreg Roach		}
1048c2e8227SGreg Roach	}
1058c2e8227SGreg Roach
1068c2e8227SGreg Roach	/** {@inheritdoc} */
1078c2e8227SGreg Roach	public function loadAjax() {
1088c2e8227SGreg Roach		return true;
1098c2e8227SGreg Roach	}
1108c2e8227SGreg Roach
1118c2e8227SGreg Roach	/** {@inheritdoc} */
1128c2e8227SGreg Roach	public function isUserBlock() {
1138c2e8227SGreg Roach		return true;
1148c2e8227SGreg Roach	}
1158c2e8227SGreg Roach
1168c2e8227SGreg Roach	/** {@inheritdoc} */
1178c2e8227SGreg Roach	public function isGedcomBlock() {
1188c2e8227SGreg Roach		return true;
1198c2e8227SGreg Roach	}
1208c2e8227SGreg Roach
12176692c8bSGreg Roach	/**
12276692c8bSGreg Roach	 * An HTML form to edit block settings
12376692c8bSGreg Roach	 *
12476692c8bSGreg Roach	 * @param int $block_id
12576692c8bSGreg Roach	 */
1268c2e8227SGreg Roach	public function configureBlock($block_id) {
1278c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
128e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, 30, 7));
129e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'filter', Filter::postBool('filter'));
130e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'onlyBDM', Filter::postBool('onlyBDM'));
131e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
132e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'alpha|anniv', 'alpha'));
1338c2e8227SGreg Roach		}
1348c2e8227SGreg Roach
135e2a378d3SGreg Roach		$days      = $this->getBlockSetting($block_id, 'days', '7');
136e2a378d3SGreg Roach		$filter    = $this->getBlockSetting($block_id, 'filter', '1');
137e2a378d3SGreg Roach		$onlyBDM   = $this->getBlockSetting($block_id, 'onlyBDM', '0');
138e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
139e2a378d3SGreg Roach		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
1408c2e8227SGreg Roach
141*15d603e7SGreg Roach		?>
142*15d603e7SGreg Roach		<div class="form-group row">
143*15d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="days">
144*15d603e7SGreg Roach				<?= I18N::translate('Number of days to show') ?>
145*15d603e7SGreg Roach			</label>
146*15d603e7SGreg Roach			<div class="col-sm-9">
147*15d603e7SGreg Roach				<input type="text" name="days" id="days" size="2" value="<?= $days ?>">
148*15d603e7SGreg Roach				<?= I18N::plural('maximum %s day', 'maximum %s days', 30, I18N::number(30)) ?>
149*15d603e7SGreg Roach			</div>
150*15d603e7SGreg Roach		</div>
1518c2e8227SGreg Roach
152*15d603e7SGreg Roach		<div class="form-group row">
153*15d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="filter">
154*15d603e7SGreg Roach				<?= I18N::translate('Show only events of living individuals') ?>
155*15d603e7SGreg Roach			</label>
156*15d603e7SGreg Roach			<div class="col-sm-9">
157*15d603e7SGreg Roach				<?= Bootstrap4::radioButtons('filter', FunctionsEdit::optionsNoYes(), $filter, true) ?>
158*15d603e7SGreg Roach			</div>
159*15d603e7SGreg Roach		</div>
1608c2e8227SGreg Roach
161*15d603e7SGreg Roach		<div class="form-group row">
162*15d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="onlyBDM">
163*15d603e7SGreg Roach				<?= I18N::translate('Show only births, deaths, and marriages') ?>
164*15d603e7SGreg Roach			</label>
165*15d603e7SGreg Roach			<div class="col-sm-9">
166*15d603e7SGreg Roach				<?= Bootstrap4::radioButtons('onlyBDM', FunctionsEdit::optionsNoYes(), $onlyBDM, true) ?>
167*15d603e7SGreg Roach			</div>
168*15d603e7SGreg Roach		</div>
1698c2e8227SGreg Roach
170*15d603e7SGreg Roach		<div class="form-group row">
171*15d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="infoStyle">
172*15d603e7SGreg Roach				<?= I18N::translate('Presentation style') ?>
173*15d603e7SGreg Roach			</label>
174*15d603e7SGreg Roach			<div class="col-sm-9">
175*15d603e7SGreg Roach				<?= Bootstrap4::select(['list' => I18N::translate('list'), 'table' => I18N::translate('table')], $infoStyle, ['id' => 'infoStyle', 'name' => 'infoStyle']) ?>
176*15d603e7SGreg Roach			</div>
177*15d603e7SGreg Roach		</div>
1788c2e8227SGreg Roach
179*15d603e7SGreg Roach		<div class="form-group row">
180*15d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="sortStyle">
181*15d603e7SGreg Roach				<?= I18N::translate('Sort order') ?>
182*15d603e7SGreg Roach			</label>
183*15d603e7SGreg Roach			<div class="col-sm-9">
184*15d603e7SGreg Roach				<?= 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*15d603e7SGreg Roach			</div>
186*15d603e7SGreg Roach		</div>
187*15d603e7SGreg Roach		<?php
1888c2e8227SGreg Roach	}
1898c2e8227SGreg Roach}
190