xref: /webtrees/app/Module/UpcomingAnniversariesModule.php (revision 225e381f36d59b49c8cdac0060465fa5af2fc308)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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;
258c2e8227SGreg Roach
268c2e8227SGreg Roach/**
278c2e8227SGreg Roach * Class UpcomingAnniversariesModule
288c2e8227SGreg Roach */
29e2a378d3SGreg Roachclass UpcomingAnniversariesModule extends AbstractModule implements ModuleBlockInterface {
3076692c8bSGreg Roach	/**
3176692c8bSGreg Roach	 * How should this module be labelled on tabs, menus, etc.?
3276692c8bSGreg Roach	 *
3376692c8bSGreg Roach	 * @return string
3476692c8bSGreg Roach	 */
358c2e8227SGreg Roach	public function getTitle() {
368c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('Upcoming events');
378c2e8227SGreg Roach	}
388c2e8227SGreg Roach
3976692c8bSGreg Roach	/**
4076692c8bSGreg Roach	 * A sentence describing what this module does.
4176692c8bSGreg Roach	 *
4276692c8bSGreg Roach	 * @return string
4376692c8bSGreg Roach	 */
448c2e8227SGreg Roach	public function getDescription() {
458c2e8227SGreg Roach		return /* I18N: Description of the “Upcoming events” module */ I18N::translate('A list of the anniversaries that will occur in the near future.');
468c2e8227SGreg Roach	}
478c2e8227SGreg Roach
4876692c8bSGreg Roach	/**
4976692c8bSGreg Roach	 * Generate the HTML content of this block.
5076692c8bSGreg Roach	 *
5176692c8bSGreg Roach	 * @param int      $block_id
5276692c8bSGreg Roach	 * @param bool     $template
53727f238cSGreg Roach	 * @param string[] $cfg
5476692c8bSGreg Roach	 *
5576692c8bSGreg Roach	 * @return string
5676692c8bSGreg Roach	 */
57a9430be8SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []): string {
584b9ff166SGreg Roach		global $ctype, $WT_TREE;
598c2e8227SGreg Roach
60e2a378d3SGreg Roach		$days      = $this->getBlockSetting($block_id, 'days', '7');
61e2a378d3SGreg Roach		$filter    = $this->getBlockSetting($block_id, 'filter', '1');
62e2a378d3SGreg Roach		$onlyBDM   = $this->getBlockSetting($block_id, 'onlyBDM', '0');
63e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
64e2a378d3SGreg Roach		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
658c2e8227SGreg Roach
6615d603e7SGreg Roach		foreach (['days', 'filter', 'onlyBDM', 'infoStyle', 'sortStyle'] as $name) {
678c2e8227SGreg Roach			if (array_key_exists($name, $cfg)) {
688c2e8227SGreg Roach				$$name = $cfg[$name];
698c2e8227SGreg Roach			}
708c2e8227SGreg Roach		}
718c2e8227SGreg Roach
728c2e8227SGreg Roach		$startjd = WT_CLIENT_JD + 1;
738c2e8227SGreg Roach		$endjd   = WT_CLIENT_JD + $days;
748c2e8227SGreg Roach
758c2e8227SGreg Roach		$content = '';
768c2e8227SGreg Roach		switch ($infoStyle) {
778c2e8227SGreg Roach			case 'list':
788c2e8227SGreg Roach				// Output style 1:  Old format, no visible tables, much smaller text. Better suited to right side of page.
793d7a8a4cSGreg Roach				$content .= FunctionsPrintLists::eventsList($startjd, $endjd, $onlyBDM ? 'BIRT MARR DEAT' : '', $filter, $sortStyle);
808c2e8227SGreg Roach				break;
818c2e8227SGreg Roach			case 'table':
828c2e8227SGreg Roach				// Style 2: New format, tables, big text, etc. Not too good on right side of page
838c2e8227SGreg Roach				ob_start();
843d7a8a4cSGreg Roach				$content .= FunctionsPrintLists::eventsTable($startjd, $endjd, $onlyBDM ? 'BIRT MARR DEAT' : '', $filter, $sortStyle);
858c2e8227SGreg Roach				$content .= ob_get_clean();
868c2e8227SGreg Roach				break;
878c2e8227SGreg Roach		}
888c2e8227SGreg Roach
898c2e8227SGreg Roach		if ($template) {
908cbbfdceSGreg Roach			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
918cbbfdceSGreg Roach				$config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
928cbbfdceSGreg Roach			} else {
938cbbfdceSGreg Roach				$config_url = '';
948cbbfdceSGreg Roach			}
958cbbfdceSGreg Roach
96*225e381fSGreg Roach			return view('blocks/template', [
979c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
989c6524dcSGreg Roach				'id'         => $block_id,
998cbbfdceSGreg Roach				'config_url' => $config_url,
1009c6524dcSGreg Roach				'title'      => $this->getTitle(),
1019c6524dcSGreg Roach				'content'    => $content,
1029c6524dcSGreg Roach			]);
1038c2e8227SGreg Roach		} else {
1048c2e8227SGreg Roach			return $content;
1058c2e8227SGreg Roach		}
1068c2e8227SGreg Roach	}
1078c2e8227SGreg Roach
1088c2e8227SGreg Roach	/** {@inheritdoc} */
109a9430be8SGreg Roach	public function loadAjax(): bool {
1108c2e8227SGreg Roach		return true;
1118c2e8227SGreg Roach	}
1128c2e8227SGreg Roach
1138c2e8227SGreg Roach	/** {@inheritdoc} */
114a9430be8SGreg Roach	public function isUserBlock(): bool {
1158c2e8227SGreg Roach		return true;
1168c2e8227SGreg Roach	}
1178c2e8227SGreg Roach
1188c2e8227SGreg Roach	/** {@inheritdoc} */
119a9430be8SGreg Roach	public function isGedcomBlock(): bool {
1208c2e8227SGreg Roach		return true;
1218c2e8227SGreg Roach	}
1228c2e8227SGreg Roach
12376692c8bSGreg Roach	/**
12476692c8bSGreg Roach	 * An HTML form to edit block settings
12576692c8bSGreg Roach	 *
12676692c8bSGreg Roach	 * @param int $block_id
127a9430be8SGreg Roach	 *
128a9430be8SGreg Roach	 * @return void
12976692c8bSGreg Roach	 */
130be9a728cSGreg Roach	public function configureBlock($block_id) {
1318c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
132e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'days', Filter::postInteger('days', 1, 30, 7));
133e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'filter', Filter::postBool('filter'));
134e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'onlyBDM', Filter::postBool('onlyBDM'));
135e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
136e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'sortStyle', Filter::post('sortStyle', 'alpha|anniv', 'alpha'));
1378c2e8227SGreg Roach		}
1388c2e8227SGreg Roach
139e2a378d3SGreg Roach		$days      = $this->getBlockSetting($block_id, 'days', '7');
140e2a378d3SGreg Roach		$filter    = $this->getBlockSetting($block_id, 'filter', '1');
141e2a378d3SGreg Roach		$onlyBDM   = $this->getBlockSetting($block_id, 'onlyBDM', '0');
142e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
143e2a378d3SGreg Roach		$sortStyle = $this->getBlockSetting($block_id, 'sortStyle', 'alpha');
1448c2e8227SGreg Roach
14515d603e7SGreg Roach		?>
14615d603e7SGreg Roach		<div class="form-group row">
14715d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="days">
14815d603e7SGreg Roach				<?= I18N::translate('Number of days to show') ?>
14915d603e7SGreg Roach			</label>
15015d603e7SGreg Roach			<div class="col-sm-9">
15115d603e7SGreg Roach				<input type="text" name="days" id="days" size="2" value="<?= $days ?>">
15215d603e7SGreg Roach				<?= I18N::plural('maximum %s day', 'maximum %s days', 30, I18N::number(30)) ?>
15315d603e7SGreg Roach			</div>
15415d603e7SGreg Roach		</div>
1558c2e8227SGreg Roach
15615d603e7SGreg Roach		<div class="form-group row">
15715d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="filter">
15815d603e7SGreg Roach				<?= I18N::translate('Show only events of living individuals') ?>
15915d603e7SGreg Roach			</label>
16015d603e7SGreg Roach			<div class="col-sm-9">
16115d603e7SGreg Roach				<?= Bootstrap4::radioButtons('filter', FunctionsEdit::optionsNoYes(), $filter, true) ?>
16215d603e7SGreg Roach			</div>
16315d603e7SGreg Roach		</div>
1648c2e8227SGreg Roach
16515d603e7SGreg Roach		<div class="form-group row">
16615d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="onlyBDM">
16715d603e7SGreg Roach				<?= I18N::translate('Show only births, deaths, and marriages') ?>
16815d603e7SGreg Roach			</label>
16915d603e7SGreg Roach			<div class="col-sm-9">
17015d603e7SGreg Roach				<?= Bootstrap4::radioButtons('onlyBDM', FunctionsEdit::optionsNoYes(), $onlyBDM, true) ?>
17115d603e7SGreg Roach			</div>
17215d603e7SGreg Roach		</div>
1738c2e8227SGreg Roach
17415d603e7SGreg Roach		<div class="form-group row">
17515d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="infoStyle">
17615d603e7SGreg Roach				<?= I18N::translate('Presentation style') ?>
17715d603e7SGreg Roach			</label>
17815d603e7SGreg Roach			<div class="col-sm-9">
17915d603e7SGreg Roach				<?= Bootstrap4::select(['list' => I18N::translate('list'), 'table' => I18N::translate('table')], $infoStyle, ['id' => 'infoStyle', 'name' => 'infoStyle']) ?>
18015d603e7SGreg Roach			</div>
18115d603e7SGreg Roach		</div>
1828c2e8227SGreg Roach
18315d603e7SGreg Roach		<div class="form-group row">
18415d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="sortStyle">
18515d603e7SGreg Roach				<?= I18N::translate('Sort order') ?>
18615d603e7SGreg Roach			</label>
18715d603e7SGreg Roach			<div class="col-sm-9">
18815d603e7SGreg 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']) ?>
18915d603e7SGreg Roach			</div>
19015d603e7SGreg Roach		</div>
19115d603e7SGreg Roach		<?php
1928c2e8227SGreg Roach	}
1938c2e8227SGreg Roach}
194