xref: /webtrees/app/Module/UpcomingAnniversariesModule.php (revision be9a728c03bbf624813113eeca03830a7825a5b1)
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;
1915d603e7SGreg Roachuse Fisharebest\Webtrees\Bootstrap4;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit;
223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintLists;
238cbbfdceSGreg Roachuse Fisharebest\Webtrees\Html;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
259c6524dcSGreg Roachuse Fisharebest\Webtrees\View;
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	 */
58a9430be8SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []): string {
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
6715d603e7SGreg 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		$content = '';
778c2e8227SGreg Roach		switch ($infoStyle) {
788c2e8227SGreg Roach			case 'list':
798c2e8227SGreg Roach				// Output style 1:  Old format, no visible tables, much smaller text. Better suited to right side of page.
803d7a8a4cSGreg Roach				$content .= FunctionsPrintLists::eventsList($startjd, $endjd, $onlyBDM ? 'BIRT MARR DEAT' : '', $filter, $sortStyle);
818c2e8227SGreg Roach				break;
828c2e8227SGreg Roach			case 'table':
838c2e8227SGreg Roach				// Style 2: New format, tables, big text, etc. Not too good on right side of page
848c2e8227SGreg Roach				ob_start();
853d7a8a4cSGreg Roach				$content .= FunctionsPrintLists::eventsTable($startjd, $endjd, $onlyBDM ? 'BIRT MARR DEAT' : '', $filter, $sortStyle);
868c2e8227SGreg Roach				$content .= ob_get_clean();
878c2e8227SGreg Roach				break;
888c2e8227SGreg Roach		}
898c2e8227SGreg Roach
908c2e8227SGreg Roach		if ($template) {
918cbbfdceSGreg Roach			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
928cbbfdceSGreg Roach				$config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
938cbbfdceSGreg Roach			} else {
948cbbfdceSGreg Roach				$config_url = '';
958cbbfdceSGreg Roach			}
968cbbfdceSGreg Roach
979c6524dcSGreg Roach			return View::make('blocks/template', [
989c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
999c6524dcSGreg Roach				'id'         => $block_id,
1008cbbfdceSGreg Roach				'config_url' => $config_url,
1019c6524dcSGreg Roach				'title'      => $this->getTitle(),
1029c6524dcSGreg Roach				'content'    => $content,
1039c6524dcSGreg Roach			]);
1048c2e8227SGreg Roach		} else {
1058c2e8227SGreg Roach			return $content;
1068c2e8227SGreg Roach		}
1078c2e8227SGreg Roach	}
1088c2e8227SGreg Roach
1098c2e8227SGreg Roach	/** {@inheritdoc} */
110a9430be8SGreg Roach	public function loadAjax(): bool {
1118c2e8227SGreg Roach		return true;
1128c2e8227SGreg Roach	}
1138c2e8227SGreg Roach
1148c2e8227SGreg Roach	/** {@inheritdoc} */
115a9430be8SGreg Roach	public function isUserBlock(): bool {
1168c2e8227SGreg Roach		return true;
1178c2e8227SGreg Roach	}
1188c2e8227SGreg Roach
1198c2e8227SGreg Roach	/** {@inheritdoc} */
120a9430be8SGreg Roach	public function isGedcomBlock(): bool {
1218c2e8227SGreg Roach		return true;
1228c2e8227SGreg Roach	}
1238c2e8227SGreg Roach
12476692c8bSGreg Roach	/**
12576692c8bSGreg Roach	 * An HTML form to edit block settings
12676692c8bSGreg Roach	 *
12776692c8bSGreg Roach	 * @param int $block_id
128a9430be8SGreg Roach	 *
129a9430be8SGreg Roach	 * @return void
13076692c8bSGreg Roach	 */
131*be9a728cSGreg Roach	public function configureBlock($block_id) {
1328c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
133e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, 30, 7));
134e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'filter', Filter::postBool('filter'));
135e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'onlyBDM', Filter::postBool('onlyBDM'));
136e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
137e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'alpha|anniv', 'alpha'));
1388c2e8227SGreg Roach		}
1398c2e8227SGreg Roach
140e2a378d3SGreg Roach		$days      = $this->getBlockSetting($block_id, 'days', '7');
141e2a378d3SGreg Roach		$filter    = $this->getBlockSetting($block_id, 'filter', '1');
142e2a378d3SGreg Roach		$onlyBDM   = $this->getBlockSetting($block_id, 'onlyBDM', '0');
143e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
144e2a378d3SGreg Roach		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
1458c2e8227SGreg Roach
14615d603e7SGreg Roach		?>
14715d603e7SGreg Roach		<div class="form-group row">
14815d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="days">
14915d603e7SGreg Roach				<?= I18N::translate('Number of days to show') ?>
15015d603e7SGreg Roach			</label>
15115d603e7SGreg Roach			<div class="col-sm-9">
15215d603e7SGreg Roach				<input type="text" name="days" id="days" size="2" value="<?= $days ?>">
15315d603e7SGreg Roach				<?= I18N::plural('maximum %s day', 'maximum %s days', 30, I18N::number(30)) ?>
15415d603e7SGreg Roach			</div>
15515d603e7SGreg Roach		</div>
1568c2e8227SGreg Roach
15715d603e7SGreg Roach		<div class="form-group row">
15815d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="filter">
15915d603e7SGreg Roach				<?= I18N::translate('Show only events of living individuals') ?>
16015d603e7SGreg Roach			</label>
16115d603e7SGreg Roach			<div class="col-sm-9">
16215d603e7SGreg Roach				<?= Bootstrap4::radioButtons('filter', FunctionsEdit::optionsNoYes(), $filter, true) ?>
16315d603e7SGreg Roach			</div>
16415d603e7SGreg Roach		</div>
1658c2e8227SGreg Roach
16615d603e7SGreg Roach		<div class="form-group row">
16715d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="onlyBDM">
16815d603e7SGreg Roach				<?= I18N::translate('Show only births, deaths, and marriages') ?>
16915d603e7SGreg Roach			</label>
17015d603e7SGreg Roach			<div class="col-sm-9">
17115d603e7SGreg Roach				<?= Bootstrap4::radioButtons('onlyBDM', FunctionsEdit::optionsNoYes(), $onlyBDM, true) ?>
17215d603e7SGreg Roach			</div>
17315d603e7SGreg Roach		</div>
1748c2e8227SGreg Roach
17515d603e7SGreg Roach		<div class="form-group row">
17615d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="infoStyle">
17715d603e7SGreg Roach				<?= I18N::translate('Presentation style') ?>
17815d603e7SGreg Roach			</label>
17915d603e7SGreg Roach			<div class="col-sm-9">
18015d603e7SGreg Roach				<?= Bootstrap4::select(['list' => I18N::translate('list'), 'table' => I18N::translate('table')], $infoStyle, ['id' => 'infoStyle', 'name' => 'infoStyle']) ?>
18115d603e7SGreg Roach			</div>
18215d603e7SGreg Roach		</div>
1838c2e8227SGreg Roach
18415d603e7SGreg Roach		<div class="form-group row">
18515d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="sortStyle">
18615d603e7SGreg Roach				<?= I18N::translate('Sort order') ?>
18715d603e7SGreg Roach			</label>
18815d603e7SGreg Roach			<div class="col-sm-9">
18915d603e7SGreg 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']) ?>
19015d603e7SGreg Roach			</div>
19115d603e7SGreg Roach		</div>
19215d603e7SGreg Roach		<?php
1938c2e8227SGreg Roach	}
1948c2e8227SGreg Roach}
195