xref: /webtrees/app/Module/FrequentlyAskedQuestionsModule.php (revision 1062a1429914c995339f502856821457aa975a5a)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
4*1062a142SGreg 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\Controller\PageController;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
233d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit;
24047f239bSGreg Roachuse Fisharebest\Webtrees\Html;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Menu;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Module;
280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
298c2e8227SGreg Roach
308c2e8227SGreg Roach/**
318c2e8227SGreg Roach * Class FrequentlyAskedQuestionsModule
328c2e8227SGreg Roach */
33e2a378d3SGreg Roachclass FrequentlyAskedQuestionsModule extends AbstractModule implements ModuleMenuInterface, ModuleConfigInterface {
348c2e8227SGreg Roach	/** {@inheritdoc} */
358c2e8227SGreg Roach	public function getTitle() {
368c2e8227SGreg Roach		return /* I18N: Name of a module. Abbreviation for “Frequently Asked Questions” */ I18N::translate('FAQ');
378c2e8227SGreg Roach	}
388c2e8227SGreg Roach
398c2e8227SGreg Roach	/** {@inheritdoc} */
408c2e8227SGreg Roach	public function getDescription() {
418c2e8227SGreg Roach		return /* I18N: Description of the “FAQ” module */ I18N::translate('A list of frequently asked questions and answers.');
428c2e8227SGreg Roach	}
438c2e8227SGreg Roach
4476692c8bSGreg Roach	/**
4576692c8bSGreg Roach	 * This is a general purpose hook, allowing modules to respond to routes
4676692c8bSGreg Roach	 * of the form module.php?mod=FOO&mod_action=BAR
4776692c8bSGreg Roach	 *
4876692c8bSGreg Roach	 * @param string $mod_action
4976692c8bSGreg Roach	 */
508c2e8227SGreg Roach	public function modAction($mod_action) {
518c2e8227SGreg Roach		switch ($mod_action) {
528c2e8227SGreg Roach			case 'admin_config':
538c2e8227SGreg Roach				$this->config();
548c2e8227SGreg Roach				break;
558c2e8227SGreg Roach			case 'admin_delete':
56b1d70009SGreg Roach				if (Auth::isAdmin()) {
578c2e8227SGreg Roach					$this->delete();
58b1d70009SGreg Roach				}
59bff8dc60SGreg Roach				header('Location: module.php?mod=faq&mod_action=admin_config');
608c2e8227SGreg Roach				break;
618c2e8227SGreg Roach			case 'admin_edit':
628c2e8227SGreg Roach				$this->edit();
638c2e8227SGreg Roach				break;
64b1d70009SGreg Roach			case 'admin_edit_save':
65b1d70009SGreg Roach				if (Auth::isAdmin()) {
66b1d70009SGreg Roach					$this->editSave();
67b1d70009SGreg Roach				}
68bff8dc60SGreg Roach				header('Location: module.php?mod=faq&mod_action=admin_config');
69b1d70009SGreg Roach				break;
708c2e8227SGreg Roach			case 'admin_movedown':
71b1d70009SGreg Roach				if (Auth::isAdmin()) {
728c2e8227SGreg Roach					$this->movedown();
73b1d70009SGreg Roach				}
74bff8dc60SGreg Roach				header('Location: module.php?mod=faq&mod_action=admin_config');
758c2e8227SGreg Roach				break;
768c2e8227SGreg Roach			case 'admin_moveup':
77b1d70009SGreg Roach				if (Auth::isAdmin()) {
788c2e8227SGreg Roach					$this->moveup();
79b1d70009SGreg Roach				}
80bff8dc60SGreg Roach				header('Location: module.php?mod=faq&mod_action=admin_config');
818c2e8227SGreg Roach				break;
828c2e8227SGreg Roach			case 'show':
838c2e8227SGreg Roach				$this->show();
848c2e8227SGreg Roach				break;
858c2e8227SGreg Roach			default:
868c2e8227SGreg Roach				http_response_code(404);
878c2e8227SGreg Roach		}
888c2e8227SGreg Roach	}
898c2e8227SGreg Roach
908c2e8227SGreg Roach	/** {@inheritdoc} */
918c2e8227SGreg Roach	public function getConfigLink() {
92b1b85189SGreg Roach		return Html::url('module.php', [
93b1b85189SGreg Roach			'mod'        => $this->getName(),
94b1b85189SGreg Roach			'mod_action' => 'admin_config',
95b1b85189SGreg Roach		]);
968c2e8227SGreg Roach	}
978c2e8227SGreg Roach
988c2e8227SGreg Roach	/**
998c2e8227SGreg Roach	 * Action from the configuration page
1008c2e8227SGreg Roach	 */
101b1d70009SGreg Roach	private function editSave() {
102b1d70009SGreg Roach		if (Filter::checkCsrf()) {
1038c2e8227SGreg Roach			$block_id = Filter::postInteger('block_id');
1048c2e8227SGreg Roach			if ($block_id) {
1058c2e8227SGreg Roach				Database::prepare(
1068c2e8227SGreg Roach					"UPDATE `##block` SET gedcom_id = NULLIF(:tree_id, '0'), block_order = :block_order WHERE block_id = :block_id"
10713abd6f3SGreg Roach				)->execute([
1088c2e8227SGreg Roach					'tree_id'     => Filter::postInteger('gedcom_id'),
1098c2e8227SGreg Roach					'block_order' => Filter::postInteger('block_order'),
110cbc1590aSGreg Roach					'block_id'    => $block_id,
11113abd6f3SGreg Roach				]);
1128c2e8227SGreg Roach			} else {
1138c2e8227SGreg Roach				Database::prepare(
1148c2e8227SGreg Roach					"INSERT INTO `##block` (gedcom_id, module_name, block_order) VALUES (NULLIF(:tree_id, '0'), :module_name, :block_order)"
11513abd6f3SGreg Roach				)->execute([
1168c2e8227SGreg Roach					'tree_id'     => Filter::postInteger('gedcom_id'),
1178c2e8227SGreg Roach					'module_name' => $this->getName(),
1188c2e8227SGreg Roach					'block_order' => Filter::postInteger('block_order'),
11913abd6f3SGreg Roach				]);
1208c2e8227SGreg Roach				$block_id = Database::getInstance()->lastInsertId();
1218c2e8227SGreg Roach			}
122e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'header', Filter::post('header'));
123e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'faqbody', Filter::post('faqbody'));
1248c2e8227SGreg Roach
125c999a340SGreg Roach			$languages = Filter::postArray('lang');
126e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'languages', implode(',', $languages));
127b1d70009SGreg Roach		}
128b1d70009SGreg Roach	}
129b1d70009SGreg Roach
130b1d70009SGreg Roach	/**
131b1d70009SGreg Roach	 * Action from the configuration page
132b1d70009SGreg Roach	 */
133b1d70009SGreg Roach	private function edit() {
134b1d70009SGreg Roach		global $WT_TREE;
135b1d70009SGreg Roach
1368c2e8227SGreg Roach		$controller = new PageController;
137b1d70009SGreg Roach		$controller->restrictAccess(Auth::isAdmin());
138b1d70009SGreg Roach
139b1d70009SGreg Roach		$block_id = Filter::getInteger('block_id');
1408c2e8227SGreg Roach		if ($block_id) {
141b1783ef7SGreg Roach			$controller->setPageTitle(/* I18N: FAQ = “Frequently Asked Question” */ I18N::translate('Edit the FAQ'));
142e2a378d3SGreg Roach			$header      = $this->getBlockSetting($block_id, 'header');
143e2a378d3SGreg Roach			$faqbody     = $this->getBlockSetting($block_id, 'faqbody');
1448c2e8227SGreg Roach			$block_order = Database::prepare(
1458c2e8227SGreg Roach				"SELECT block_order FROM `##block` WHERE block_id = :block_id"
14613abd6f3SGreg Roach			)->execute(['block_id' => $block_id])->fetchOne();
1478c2e8227SGreg Roach			$gedcom_id = Database::prepare(
1488c2e8227SGreg Roach				"SELECT gedcom_id FROM `##block` WHERE block_id = :block_id"
14913abd6f3SGreg Roach			)->execute(['block_id' => $block_id])->fetchOne();
1508c2e8227SGreg Roach		} else {
151b1783ef7SGreg Roach			$controller->setPageTitle(/* I18N: FAQ = “Frequently Asked Question” */ I18N::translate('Add an FAQ'));
1528c2e8227SGreg Roach			$header      = '';
1538c2e8227SGreg Roach			$faqbody     = '';
1548c2e8227SGreg Roach			$block_order = Database::prepare(
1558c2e8227SGreg Roach				"SELECT IFNULL(MAX(block_order)+1, 0) FROM `##block` WHERE module_name = :module_name"
15613abd6f3SGreg Roach			)->execute(['module_name' => $this->getName()])->fetchOne();
15724ec66ceSGreg Roach			$gedcom_id = $WT_TREE->getTreeId();
1588c2e8227SGreg Roach		}
1598c2e8227SGreg Roach		$controller->pageHeader();
1608c2e8227SGreg Roach		if (Module::getModuleByName('ckeditor')) {
1618c2e8227SGreg Roach			CkeditorModule::enableEditor($controller);
1628c2e8227SGreg Roach		}
1638c2e8227SGreg Roach
16415d603e7SGreg Roach		echo Bootstrap4::breadcrumbs([
1651f3fb95cSGreg Roach			route('admin-control-panel')                                      => I18N::translate('Control panel'),
1661f3fb95cSGreg Roach			route('admin-modules')                                            => I18N::translate('Module administration'),
16715d603e7SGreg Roach			'module.php?mod=' . $this->getName() . '&mod_action=admin_config' => $this->getTitle(),
16815d603e7SGreg Roach		], $controller->getPageTitle());
1698c2e8227SGreg Roach		?>
1704d6c0a77SGreg Roach
17115d603e7SGreg Roach		<h1><?= $controller->getPageTitle() ?></h1>
1724d6c0a77SGreg Roach
17315d603e7SGreg Roach		<form name="faq" class="form-horizontal" method="post" action="module.php?mod=<?= $this->getName() ?>&amp;mod_action=admin_edit_save">
17415d603e7SGreg Roach		<?= Filter::getCsrf() ?>
17515d603e7SGreg Roach		<input type="hidden" name="block_id" value="<?= $block_id ?>">
17615d603e7SGreg Roach
17715d603e7SGreg Roach		<div class="row form-group">
17815d603e7SGreg Roach			<label for="header" class="col-sm-3 col-form-label">
179564ae2d7SGreg Roach				<?= I18N::translate('Question') ?>
1804d6c0a77SGreg Roach			</label>
1814d6c0a77SGreg Roach
1824d6c0a77SGreg Roach			<div class="col-sm-9">
1834d6c0a77SGreg Roach				<input type="text" class="form-control" name="header" id="header"
184d53324c9SGreg Roach				       value="<?= e($header) ?>">
1854d6c0a77SGreg Roach			</div>
1864d6c0a77SGreg Roach		</div>
1874d6c0a77SGreg Roach
18815d603e7SGreg Roach		<div class="row form-group">
18915d603e7SGreg Roach			<label for="faqbody" class="col-sm-3 col-form-label">
190564ae2d7SGreg Roach				<?= I18N::translate('Answer') ?>
1914d6c0a77SGreg Roach			</label>
1924d6c0a77SGreg Roach
1934d6c0a77SGreg Roach			<div class="col-sm-9">
194d53324c9SGreg Roach				<textarea name="faqbody" id="faqbody" class="form-control html-edit" rows="10"><?= e($faqbody) ?></textarea>
1954d6c0a77SGreg Roach			</div>
1964d6c0a77SGreg Roach		</div>
1974d6c0a77SGreg Roach
19815d603e7SGreg Roach		<div class="row form-group">
19915d603e7SGreg Roach			<label for="xref" class="col-sm-3 col-form-label">
200564ae2d7SGreg Roach				<?= /* I18N: Label for a configuration option */ I18N::translate('Show this block for which languages') ?>
2014d6c0a77SGreg Roach			</label>
2024d6c0a77SGreg Roach
2034d6c0a77SGreg Roach			<div class="col-sm-9">
204564ae2d7SGreg Roach				<?= FunctionsEdit::editLanguageCheckboxes('lang', explode(',', $this->getBlockSetting($block_id, 'languages'))) ?>
2054d6c0a77SGreg Roach			</div>
2064d6c0a77SGreg Roach		</div>
2074d6c0a77SGreg Roach
20815d603e7SGreg Roach		<div class="row form-group">
20915d603e7SGreg Roach			<label for="block_order" class="col-sm-3 col-form-label">
210564ae2d7SGreg Roach				<?= I18N::translate('Sort order') ?>
2114d6c0a77SGreg Roach			</label>
2124d6c0a77SGreg Roach
2134d6c0a77SGreg Roach			<div class="col-sm-9">
214564ae2d7SGreg Roach				<input type="text" name="block_order" id="block_order" class="form-control" value="<?= $block_order ?>">
2154d6c0a77SGreg Roach			</div>
2164d6c0a77SGreg Roach		</div>
2174d6c0a77SGreg Roach
21815d603e7SGreg Roach		<div class="row form-group">
21915d603e7SGreg Roach			<label for="gedcom_id" class="col-sm-3 col-form-label">
220564ae2d7SGreg Roach				<?= I18N::translate('Family tree') ?>
2214d6c0a77SGreg Roach			</label>
2224d6c0a77SGreg Roach
2234d6c0a77SGreg Roach			<div class="col-sm-9">
22415d603e7SGreg Roach				<?= Bootstrap4::select(['' => I18N::translate('All')] + Tree::getIdList(), $gedcom_id, ['id' => 'gedcom_id', 'name' => 'gedcom_id']) ?>
2254d6c0a77SGreg Roach				<p class="small text-muted">
226564ae2d7SGreg Roach					<?= /* I18N: FAQ = “Frequently Asked Question” */ I18N::translate('An FAQ can be displayed on just one of the family trees, or on all the family trees.') ?>
2274d6c0a77SGreg Roach				</p>
2284d6c0a77SGreg Roach			</div>
2294d6c0a77SGreg Roach		</div>
2304d6c0a77SGreg Roach
23115d603e7SGreg Roach		<div class="row form-group">
23215d603e7SGreg Roach			<div class="offset-sm-3 col-sm-9">
2334d6c0a77SGreg Roach				<button type="submit" class="btn btn-primary">
2348108dc50SGreg Roach					<i class="fas fa-check"></i>
235564ae2d7SGreg Roach					<?= I18N::translate('save') ?>
2364d6c0a77SGreg Roach				</button>
2374d6c0a77SGreg Roach			</div>
2384d6c0a77SGreg Roach		</div>
2394d6c0a77SGreg Roach
2404d6c0a77SGreg Roach	</form>
2418c2e8227SGreg Roach	<?php
2428c2e8227SGreg Roach	}
2438c2e8227SGreg Roach
2448c2e8227SGreg Roach	/**
245b1783ef7SGreg Roach	 * Delete an FAQ.
2468c2e8227SGreg Roach	 */
2478c2e8227SGreg Roach	private function delete() {
2488c2e8227SGreg Roach		$block_id = Filter::getInteger('block_id');
2498c2e8227SGreg Roach
2508c2e8227SGreg Roach		Database::prepare(
2518c2e8227SGreg Roach			"DELETE FROM `##block_setting` WHERE block_id = :block_id"
25213abd6f3SGreg Roach		)->execute(['block_id' => $block_id]);
2538c2e8227SGreg Roach
2548c2e8227SGreg Roach		Database::prepare(
2558c2e8227SGreg Roach			"DELETE FROM `##block` WHERE block_id = :block_id"
25613abd6f3SGreg Roach		)->execute(['block_id' => $block_id]);
2578c2e8227SGreg Roach	}
2588c2e8227SGreg Roach
2598c2e8227SGreg Roach	/**
260b1783ef7SGreg Roach	 * Move an FAQ up the list.
2618c2e8227SGreg Roach	 */
2628c2e8227SGreg Roach	private function moveup() {
2638c2e8227SGreg Roach		$block_id = Filter::getInteger('block_id');
2648c2e8227SGreg Roach
2658c2e8227SGreg Roach		$block_order = Database::prepare(
2668c2e8227SGreg Roach			"SELECT block_order FROM `##block` WHERE block_id = :block_id"
26713abd6f3SGreg Roach		)->execute(['block_id' => $block_id])->fetchOne();
2688c2e8227SGreg Roach
2698c2e8227SGreg Roach		$swap_block = Database::prepare(
2708c2e8227SGreg Roach			"SELECT block_order, block_id" .
2718c2e8227SGreg Roach			" FROM `##block`" .
2728c2e8227SGreg Roach			" WHERE block_order = (" .
2738c2e8227SGreg Roach			"  SELECT MAX(block_order) FROM `##block` WHERE block_order < :block_order AND module_name = :module_name_1" .
2748c2e8227SGreg Roach			" ) AND module_name = :module_name_2" .
2758c2e8227SGreg Roach			" LIMIT 1"
27613abd6f3SGreg Roach		)->execute([
2778c2e8227SGreg Roach			'block_order'   => $block_order,
2788c2e8227SGreg Roach			'module_name_1' => $this->getName(),
279cbc1590aSGreg Roach			'module_name_2' => $this->getName(),
28013abd6f3SGreg Roach		])->fetchOneRow();
2818c2e8227SGreg Roach		if ($swap_block) {
2828c2e8227SGreg Roach			Database::prepare(
2838c2e8227SGreg Roach				"UPDATE `##block` SET block_order = :block_order WHERE block_id = :block_id"
28413abd6f3SGreg Roach			)->execute([
2858c2e8227SGreg Roach				'block_order' => $swap_block->block_order,
2868c2e8227SGreg Roach				'block_id'    => $block_id,
28713abd6f3SGreg Roach			]);
2888c2e8227SGreg Roach			Database::prepare(
2898c2e8227SGreg Roach				"UPDATE `##block` SET block_order = :block_order WHERE block_id = :block_id"
29013abd6f3SGreg Roach			)->execute([
2918c2e8227SGreg Roach				'block_order' => $block_order,
2928c2e8227SGreg Roach				'block_id'    => $swap_block->block_id,
29313abd6f3SGreg Roach			]);
2948c2e8227SGreg Roach		}
2958c2e8227SGreg Roach	}
2968c2e8227SGreg Roach
2978c2e8227SGreg Roach	/**
298b1783ef7SGreg Roach	 * Move an FAQ down the list.
2998c2e8227SGreg Roach	 */
3008c2e8227SGreg Roach	private function movedown() {
3018c2e8227SGreg Roach		$block_id = Filter::get('block_id');
3028c2e8227SGreg Roach
3038c2e8227SGreg Roach		$block_order = Database::prepare(
3048c2e8227SGreg Roach			"SELECT block_order FROM `##block` WHERE block_id = :block_id"
30513abd6f3SGreg Roach		)->execute([
3068c2e8227SGreg Roach			'block_id' => $block_id,
30713abd6f3SGreg Roach		])->fetchOne();
3088c2e8227SGreg Roach
3098c2e8227SGreg Roach		$swap_block = Database::prepare(
3108c2e8227SGreg Roach			"SELECT block_order, block_id" .
3118c2e8227SGreg Roach			" FROM `##block`" .
3128c2e8227SGreg Roach			" WHERE block_order=(" .
3138c2e8227SGreg Roach			"  SELECT MIN(block_order) FROM `##block` WHERE block_order > :block_order AND module_name = :module_name_1" .
3148c2e8227SGreg Roach			" ) AND module_name = :module_name_2" .
3158c2e8227SGreg Roach			" LIMIT 1"
31613abd6f3SGreg Roach		)->execute([
3178c2e8227SGreg Roach			'block_order'   => $block_order,
3188c2e8227SGreg Roach			'module_name_1' => $this->getName(),
3198c2e8227SGreg Roach			'module_name_2' => $this->getName(),
32013abd6f3SGreg Roach			])->fetchOneRow();
3218c2e8227SGreg Roach		if ($swap_block) {
3228c2e8227SGreg Roach			Database::prepare(
3238c2e8227SGreg Roach				"UPDATE `##block` SET block_order = :block_order WHERE block_id = :block_id"
32413abd6f3SGreg Roach			)->execute([
3258c2e8227SGreg Roach				'block_order' => $swap_block->block_order,
3268c2e8227SGreg Roach				'block_id'    => $block_id,
32713abd6f3SGreg Roach			]);
3288c2e8227SGreg Roach			Database::prepare(
3298c2e8227SGreg Roach				"UPDATE `##block` SET block_order = :block_order WHERE block_id = :block_id"
33013abd6f3SGreg Roach			)->execute([
3318c2e8227SGreg Roach				'block_order' => $block_order,
3328c2e8227SGreg Roach				'block_id'    => $swap_block->block_id,
33313abd6f3SGreg Roach			]);
3348c2e8227SGreg Roach		}
3358c2e8227SGreg Roach	}
3368c2e8227SGreg Roach
3378c2e8227SGreg Roach	/**
3388c2e8227SGreg Roach	 * Show a list of FAQs
3398c2e8227SGreg Roach	 */
3408c2e8227SGreg Roach	private function show() {
3414b9ff166SGreg Roach		global $controller, $WT_TREE;
3424b9ff166SGreg Roach
3438c2e8227SGreg Roach		$controller = new PageController;
3448c2e8227SGreg Roach		$controller
3458c2e8227SGreg Roach			->setPageTitle(I18N::translate('Frequently asked questions'))
3468c2e8227SGreg Roach			->pageHeader();
3478c2e8227SGreg Roach
3488c2e8227SGreg Roach		$faqs = Database::prepare(
3498c2e8227SGreg Roach			"SELECT block_id, bs1.setting_value AS header, bs2.setting_value AS body, bs3.setting_value AS languages" .
3508c2e8227SGreg Roach			" FROM `##block` b" .
3518c2e8227SGreg Roach			" JOIN `##block_setting` bs1 USING (block_id)" .
3528c2e8227SGreg Roach			" JOIN `##block_setting` bs2 USING (block_id)" .
3538c2e8227SGreg Roach			" JOIN `##block_setting` bs3 USING (block_id)" .
3548c2e8227SGreg Roach			" WHERE module_name = :module_name" .
3558c2e8227SGreg Roach			" AND bs1.setting_name = 'header'" .
3568c2e8227SGreg Roach			" AND bs2.setting_name = 'faqbody'" .
3578c2e8227SGreg Roach			" AND bs3.setting_name = 'languages'" .
3588c2e8227SGreg Roach			" AND IFNULL(gedcom_id, :tree_id_1) = :tree_id_2" .
3598c2e8227SGreg Roach			" ORDER BY block_order"
36013abd6f3SGreg Roach		)->execute([
3618c2e8227SGreg Roach			'module_name' => $this->getName(),
36224ec66ceSGreg Roach			'tree_id_1'   => $WT_TREE->getTreeId(),
36324ec66ceSGreg Roach			'tree_id_2'   => $WT_TREE->getTreeId(),
36413abd6f3SGreg Roach		])->fetchAll();
3658c2e8227SGreg Roach
36615d603e7SGreg Roach		echo '<h2 class="wt-page-title">', I18N::translate('Frequently asked questions');
3674b9ff166SGreg Roach		if (Auth::isManager($WT_TREE)) {
3682a277e58SGreg Roach			echo ' — <a href="module.php?mod=', $this->getName(), '&amp;mod_action=admin_config">', I18N::translate('edit'), '</a>';
3698c2e8227SGreg Roach		}
3702a277e58SGreg Roach		echo '</h2>';
3718c2e8227SGreg Roach		$row_count = 0;
3728c2e8227SGreg Roach		echo '<table class="faq">';
3738c2e8227SGreg Roach		// List of titles
3748c2e8227SGreg Roach		foreach ($faqs as $id => $faq) {
3758c2e8227SGreg Roach			if (!$faq->languages || in_array(WT_LOCALE, explode(',', $faq->languages))) {
3768c2e8227SGreg Roach				$row_color = ($row_count % 2) ? 'odd' : 'even';
3778c2e8227SGreg Roach				// NOTE: Print the header of the current item
3788c2e8227SGreg Roach				echo '<tr class="', $row_color, '"><td style="padding: 5px;">';
3798c2e8227SGreg Roach				echo '<a href="#faq', $id, '">', $faq->header, '</a>';
3808c2e8227SGreg Roach				echo '</td></tr>';
3818c2e8227SGreg Roach				$row_count++;
3828c2e8227SGreg Roach			}
3838c2e8227SGreg Roach		}
3848c2e8227SGreg Roach		echo '</table><hr>';
3858c2e8227SGreg Roach		// Detailed entries
3868c2e8227SGreg Roach		foreach ($faqs as $id => $faq) {
3878c2e8227SGreg Roach			if (!$faq->languages || in_array(WT_LOCALE, explode(',', $faq->languages))) {
3888c2e8227SGreg Roach				echo '<div class="faq_title" id="faq', $id, '">', $faq->header;
3898c2e8227SGreg Roach				echo '<div class="faq_top faq_italic">';
3908c2e8227SGreg Roach				echo '<a href="#content">', I18N::translate('back to top'), '</a>';
3918c2e8227SGreg Roach				echo '</div>';
3928c2e8227SGreg Roach				echo '</div>';
3938c2e8227SGreg Roach				echo '<div class="faq_body">', substr($faq->body, 0, 1) == '<' ? $faq->body : nl2br($faq->body, false), '</div>';
3948c2e8227SGreg Roach				echo '<hr>';
3958c2e8227SGreg Roach			}
3968c2e8227SGreg Roach		}
3978c2e8227SGreg Roach	}
3988c2e8227SGreg Roach
3998c2e8227SGreg Roach	/**
4008c2e8227SGreg Roach	 * Provide a form to manage the FAQs.
4018c2e8227SGreg Roach	 */
4028c2e8227SGreg Roach	private function config() {
40324ec66ceSGreg Roach		global $WT_TREE;
40424ec66ceSGreg Roach
4058c2e8227SGreg Roach		$controller = new PageController;
4068c2e8227SGreg Roach		$controller
407da3772f8SJustCarmen			->restrictAccess(Auth::isAdmin())
4088c2e8227SGreg Roach			->setPageTitle(I18N::translate('Frequently asked questions'))
4098c2e8227SGreg Roach			->pageHeader();
4108c2e8227SGreg Roach
4118c2e8227SGreg Roach		$faqs = Database::prepare(
4128c2e8227SGreg Roach			"SELECT block_id, block_order, gedcom_id, bs1.setting_value AS header, bs2.setting_value AS faqbody" .
4138c2e8227SGreg Roach			" FROM `##block` b" .
4148c2e8227SGreg Roach			" JOIN `##block_setting` bs1 USING (block_id)" .
4158c2e8227SGreg Roach			" JOIN `##block_setting` bs2 USING (block_id)" .
4168c2e8227SGreg Roach			" WHERE module_name = :module_name" .
4178c2e8227SGreg Roach			" AND bs1.setting_name = 'header'" .
4188c2e8227SGreg Roach			" AND bs2.setting_name = 'faqbody'" .
4198c2e8227SGreg Roach			" AND IFNULL(gedcom_id, :tree_id_1) = :tree_id_2" .
4208c2e8227SGreg Roach			" ORDER BY block_order"
42113abd6f3SGreg Roach		)->execute([
4228c2e8227SGreg Roach			'module_name' => $this->getName(),
42324ec66ceSGreg Roach			'tree_id_1'   => $WT_TREE->getTreeId(),
42424ec66ceSGreg Roach			'tree_id_2'   => $WT_TREE->getTreeId(),
42513abd6f3SGreg Roach			])->fetchAll();
4268c2e8227SGreg Roach
4278c2e8227SGreg Roach		$min_block_order = Database::prepare(
428ee928960SGreg Roach			"SELECT MIN(block_order) FROM `##block` WHERE module_name = 'faq' AND (gedcom_id = :tree_id OR gedcom_id IS NULL)"
42913abd6f3SGreg Roach		)->execute([
4304d6c0a77SGreg Roach			'tree_id' => $WT_TREE->getTreeId(),
43113abd6f3SGreg Roach		])->fetchOne();
4328c2e8227SGreg Roach
4338c2e8227SGreg Roach		$max_block_order = Database::prepare(
434ee928960SGreg Roach			"SELECT MAX(block_order) FROM `##block` WHERE module_name = 'faq' AND (gedcom_id = :tree_id OR gedcom_id IS NULL)"
43513abd6f3SGreg Roach		)->execute([
4364d6c0a77SGreg Roach			'tree_id' => $WT_TREE->getTreeId(),
43713abd6f3SGreg Roach		])->fetchOne();
4388c2e8227SGreg Roach
43915d603e7SGreg Roach		echo Bootstrap4::breadcrumbs([
4401f3fb95cSGreg Roach			route('admin-control-panel') => I18N::translate('Control panel'),
4411f3fb95cSGreg Roach			route('admin-modules')       => I18N::translate('Module administration'),
44215d603e7SGreg Roach		], $controller->getPageTitle());
4438c2e8227SGreg Roach		?>
44415d603e7SGreg Roach
44515d603e7SGreg Roach		<h1><?= $controller->getPageTitle() ?></h1>
4468c2e8227SGreg Roach		<p>
447564ae2d7SGreg Roach			<?= /* I18N: FAQ = “Frequently Asked Question” */ I18N::translate('FAQs are lists of questions and answers, which allow you to explain the site’s rules, policies, and procedures to your visitors. Questions are typically concerned with privacy, copyright, user-accounts, unsuitable content, requirement for source-citations, etc.') ?>
448564ae2d7SGreg Roach			<?= I18N::translate('You may use HTML to format the answer and to add links to other websites.') ?>
4498c2e8227SGreg Roach		</p>
450b1783ef7SGreg Roach
451b1783ef7SGreg Roach		<p>
45207231e86SGreg Roach			<form class="form form-inline">
45307231e86SGreg Roach				<label for="ged" class="sr-only">
454564ae2d7SGreg Roach					<?= I18N::translate('Family tree') ?>
45507231e86SGreg Roach				</label>
456564ae2d7SGreg Roach				<input type="hidden" name="mod" value="<?=  $this->getName() ?>">
45707231e86SGreg Roach				<input type="hidden" name="mod_action" value="admin_config">
45815d603e7SGreg Roach				<?= Bootstrap4::select(Tree::getNameList(), $WT_TREE->getName(), ['id' => 'ged', 'name' => 'ged']) ?>
459564ae2d7SGreg Roach				<input type="submit" class="btn btn-primary" value="<?= I18N::translate('show') ?>">
46007231e86SGreg Roach			</form>
461b1783ef7SGreg Roach		</p>
46207231e86SGreg Roach
46307231e86SGreg Roach		<p>
464564ae2d7SGreg Roach			<a href="module.php?mod=<?= $this->getName() ?>&amp;mod_action=admin_edit" class="btn btn-default">
4658108dc50SGreg Roach				<i class="fas fa-plus"></i>
466564ae2d7SGreg Roach				<?= /* I18N: FAQ = “Frequently Asked Question” */ I18N::translate('Add an FAQ') ?>
46707231e86SGreg Roach			</a>
46807231e86SGreg Roach		</p>
46907231e86SGreg Roach
4708c2e8227SGreg Roach		<?php
4714d6c0a77SGreg Roach		echo '<table class="table table-bordered">';
4728c2e8227SGreg Roach		foreach ($faqs as $faq) {
4738c2e8227SGreg Roach			// NOTE: Print the position of the current item
4748c2e8227SGreg Roach			echo '<tr class="faq_edit_pos"><td>';
4754d6c0a77SGreg Roach			echo I18N::translate('#%s', $faq->block_order + 1), ' ';
4763a7d762dSGreg Roach			if ($faq->gedcom_id === null) {
4778c2e8227SGreg Roach				echo I18N::translate('All');
4788c2e8227SGreg Roach			} else {
479f306ecacSDavid Drury				echo $WT_TREE->getTitleHtml();
4808c2e8227SGreg Roach			}
4818c2e8227SGreg Roach			echo '</td>';
4828c2e8227SGreg Roach			// NOTE: Print the edit options of the current item
4838c2e8227SGreg Roach			echo '<td>';
4848c2e8227SGreg Roach			if ($faq->block_order == $min_block_order) {
4858c2e8227SGreg Roach				echo '&nbsp;';
4868c2e8227SGreg Roach			} else {
4878108dc50SGreg Roach				echo '<a href="module.php?mod=', $this->getName(), '&amp;mod_action=admin_moveup&amp;block_id=', $faq->block_id, '"><i class="fas fa-arrow-up"></i></i> ', I18N::translate('Move up'), '</a>';
4888c2e8227SGreg Roach			}
4898c2e8227SGreg Roach			echo '</td><td>';
4908c2e8227SGreg Roach			if ($faq->block_order == $max_block_order) {
4918c2e8227SGreg Roach				echo '&nbsp;';
4928c2e8227SGreg Roach			} else {
4938108dc50SGreg Roach				echo '<a href="module.php?mod=', $this->getName(), '&amp;mod_action=admin_movedown&amp;block_id=', $faq->block_id, '"><i class="fas fa-arrow-down"></i></i> ', I18N::translate('Move down'), '</a>';
4948c2e8227SGreg Roach			}
4958c2e8227SGreg Roach			echo '</td><td>';
4961f1d2668SGreg Roach			echo '<a href="module.php?mod=', $this->getName(), '&amp;mod_action=admin_edit&amp;block_id=', $faq->block_id, '"><i class="fas fa-pencil-alt"></i> ', I18N::translate('Edit'), '</a>';
4978c2e8227SGreg Roach			echo '</td><td>';
498d53324c9SGreg Roach			echo '<a href="module.php?mod=', $this->getName(), '&amp;mod_action=admin_delete&amp;block_id=', $faq->block_id, '" onclick="return confirm(\'', I18N::translate('Are you sure you want to delete “%s”?', e($faq->header)), '\');"><i class="fas fa-trash-alt"></i> ', I18N::translate('Delete'), '</a>';
4998c2e8227SGreg Roach			echo '</td></tr>';
5008c2e8227SGreg Roach			// NOTE: Print the title text of the current item
5018c2e8227SGreg Roach			echo '<tr><td colspan="5">';
5028c2e8227SGreg Roach			echo '<div class="faq_edit_item">';
5038c2e8227SGreg Roach			echo '<div class="faq_edit_title">', $faq->header, '</div>';
5048c2e8227SGreg Roach			// NOTE: Print the body text of the current item
5058c2e8227SGreg Roach			echo '<div class="faq_edit_content">', substr($faq->faqbody, 0, 1) == '<' ? $faq->faqbody : nl2br($faq->faqbody, false), '</div></div></td></tr>';
5068c2e8227SGreg Roach		}
5078c2e8227SGreg Roach		echo '</table>';
5088c2e8227SGreg Roach	}
5098c2e8227SGreg Roach
5100ee13198SGreg Roach	/**
5110ee13198SGreg Roach	 * The user can re-order menus. Until they do, they are shown in this order.
5120ee13198SGreg Roach	 *
5130ee13198SGreg Roach	 * @return int
5140ee13198SGreg Roach	 */
5158c2e8227SGreg Roach	public function defaultMenuOrder() {
5168c2e8227SGreg Roach		return 40;
5178c2e8227SGreg Roach	}
5188c2e8227SGreg Roach
5190ee13198SGreg Roach	/**
5200ee13198SGreg Roach	 * A menu, to be added to the main application menu.
5210ee13198SGreg Roach	 *
5220ee13198SGreg Roach	 * @return Menu|null
5230ee13198SGreg Roach	 */
5248c2e8227SGreg Roach	public function getMenu() {
52524ec66ceSGreg Roach		global $WT_TREE;
52624ec66ceSGreg Roach
5278c2e8227SGreg Roach		$faqs = Database::prepare(
5282b6675d7SGreg Roach			"SELECT block_id FROM `##block`" .
5292b6675d7SGreg Roach			" JOIN `##block_setting` USING (block_id)" .
5302b6675d7SGreg Roach			" WHERE module_name = :module_name AND IFNULL(gedcom_id, :tree_id_1) = :tree_id_2" .
5312b6675d7SGreg Roach			" AND setting_name='languages' AND (setting_value LIKE CONCAT('%', :locale, '%') OR setting_value='')"
53213abd6f3SGreg Roach		)->execute([
5338c2e8227SGreg Roach			'module_name' => $this->getName(),
53424ec66ceSGreg Roach			'tree_id_1'   => $WT_TREE->getTreeId(),
53524ec66ceSGreg Roach			'tree_id_2'   => $WT_TREE->getTreeId(),
5362b6675d7SGreg Roach			'locale'      => WT_LOCALE,
53713abd6f3SGreg Roach		])->fetchAll();
5388c2e8227SGreg Roach
53938a9583bSGreg Roach		if ($faqs) {
540b1783ef7SGreg Roach			return new Menu($this->getTitle(), 'module.php?mod=faq&amp;mod_action=show', 'menu-help');
54138a9583bSGreg Roach		} else {
5428c2e8227SGreg Roach			return null;
5438c2e8227SGreg Roach		}
5448c2e8227SGreg Roach	}
5458c2e8227SGreg Roach}
546