xref: /webtrees/app/Module/FrequentlyAskedQuestionsModule.php (revision da3772f85950756f9d294284c449c6923e3bcf84)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48c2e8227SGreg Roach * Copyright (C) 2015 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;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Controller\PageController;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Menu;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Module;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class FrequentlyAskedQuestionsModule
308c2e8227SGreg Roach */
31e2a378d3SGreg Roachclass FrequentlyAskedQuestionsModule extends AbstractModule implements ModuleMenuInterface, ModuleConfigInterface {
328c2e8227SGreg Roach	/** {@inheritdoc} */
338c2e8227SGreg Roach	public function getTitle() {
348c2e8227SGreg Roach		return /* I18N: Name of a module.  Abbreviation for “Frequently Asked Questions” */ I18N::translate('FAQ');
358c2e8227SGreg Roach	}
368c2e8227SGreg Roach
378c2e8227SGreg Roach	/** {@inheritdoc} */
388c2e8227SGreg Roach	public function getDescription() {
398c2e8227SGreg Roach		return /* I18N: Description of the “FAQ” module */ I18N::translate('A list of frequently asked questions and answers.');
408c2e8227SGreg Roach	}
418c2e8227SGreg Roach
4276692c8bSGreg Roach	/**
4376692c8bSGreg Roach	 * This is a general purpose hook, allowing modules to respond to routes
4476692c8bSGreg Roach	 * of the form module.php?mod=FOO&mod_action=BAR
4576692c8bSGreg Roach	 *
4676692c8bSGreg Roach	 * @param string $mod_action
4776692c8bSGreg Roach	 */
488c2e8227SGreg Roach	public function modAction($mod_action) {
498c2e8227SGreg Roach		switch ($mod_action) {
508c2e8227SGreg Roach		case 'admin_config':
518c2e8227SGreg Roach			$this->config();
528c2e8227SGreg Roach			break;
538c2e8227SGreg Roach		case 'admin_delete':
548c2e8227SGreg Roach			$this->delete();
558c2e8227SGreg Roach			$this->config();
568c2e8227SGreg Roach			break;
578c2e8227SGreg Roach		case 'admin_edit':
588c2e8227SGreg Roach			$this->edit();
598c2e8227SGreg Roach			break;
608c2e8227SGreg Roach		case 'admin_movedown':
618c2e8227SGreg Roach			$this->movedown();
628c2e8227SGreg Roach			$this->config();
638c2e8227SGreg Roach			break;
648c2e8227SGreg Roach		case 'admin_moveup':
658c2e8227SGreg Roach			$this->moveup();
668c2e8227SGreg Roach			$this->config();
678c2e8227SGreg Roach			break;
688c2e8227SGreg Roach		case 'show':
698c2e8227SGreg Roach			$this->show();
708c2e8227SGreg Roach			break;
718c2e8227SGreg Roach		default:
728c2e8227SGreg Roach			http_response_code(404);
738c2e8227SGreg Roach		}
748c2e8227SGreg Roach	}
758c2e8227SGreg Roach
768c2e8227SGreg Roach	/** {@inheritdoc} */
778c2e8227SGreg Roach	public function getConfigLink() {
788c2e8227SGreg Roach		return 'module.php?mod=' . $this->getName() . '&amp;mod_action=admin_config';
798c2e8227SGreg Roach	}
808c2e8227SGreg Roach
818c2e8227SGreg Roach	/**
828c2e8227SGreg Roach	 * Action from the configuration page
838c2e8227SGreg Roach	 */
848c2e8227SGreg Roach	private function edit() {
8524ec66ceSGreg Roach		global $WT_TREE;
8624ec66ceSGreg Roach
878c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
888c2e8227SGreg Roach			$block_id = Filter::postInteger('block_id');
898c2e8227SGreg Roach			if ($block_id) {
908c2e8227SGreg Roach				Database::prepare(
918c2e8227SGreg Roach					"UPDATE `##block` SET gedcom_id = NULLIF(:tree_id, '0'), block_order = :block_order WHERE block_id = :block_id"
928c2e8227SGreg Roach				)->execute(array(
938c2e8227SGreg Roach					'tree_id'     => Filter::postInteger('gedcom_id'),
948c2e8227SGreg Roach					'block_order' => Filter::postInteger('block_order'),
95cbc1590aSGreg Roach					'block_id'    => $block_id,
968c2e8227SGreg Roach				));
978c2e8227SGreg Roach			} else {
988c2e8227SGreg Roach				Database::prepare(
998c2e8227SGreg Roach					"INSERT INTO `##block` (gedcom_id, module_name, block_order) VALUES (NULLIF(:tree_id, '0'), :module_name, :block_order)"
1008c2e8227SGreg Roach				)->execute(array(
1018c2e8227SGreg Roach					'tree_id'     => Filter::postInteger('gedcom_id'),
1028c2e8227SGreg Roach					'module_name' => $this->getName(),
1038c2e8227SGreg Roach					'block_order' => Filter::postInteger('block_order'),
1048c2e8227SGreg Roach				));
1058c2e8227SGreg Roach				$block_id = Database::getInstance()->lastInsertId();
1068c2e8227SGreg Roach			}
107e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'header', Filter::post('header'));
108e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'faqbody', Filter::post('faqbody'));
1098c2e8227SGreg Roach
110c999a340SGreg Roach			$languages = Filter::postArray('lang');
111e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'languages', implode(',', $languages));
1128c2e8227SGreg Roach			$this->config();
1138c2e8227SGreg Roach		} else {
1148c2e8227SGreg Roach			$block_id   = Filter::getInteger('block_id');
1158c2e8227SGreg Roach			$controller = new PageController;
1168c2e8227SGreg Roach			if ($block_id) {
1178c2e8227SGreg Roach				$controller->setPageTitle(I18N::translate('Edit FAQ item'));
118e2a378d3SGreg Roach				$header      = $this->getBlockSetting($block_id, 'header');
119e2a378d3SGreg Roach				$faqbody     = $this->getBlockSetting($block_id, 'faqbody');
1208c2e8227SGreg Roach				$block_order = Database::prepare(
1218c2e8227SGreg Roach					"SELECT block_order FROM `##block` WHERE block_id = :block_id"
1228c2e8227SGreg Roach				)->execute(array('block_id' => $block_id))->fetchOne();
1238c2e8227SGreg Roach				$gedcom_id   = Database::prepare(
1248c2e8227SGreg Roach					"SELECT gedcom_id FROM `##block` WHERE block_id = :block_id"
1258c2e8227SGreg Roach				)->execute(array('block_id' => $block_id))->fetchOne();
1268c2e8227SGreg Roach			} else {
1278c2e8227SGreg Roach				$controller->setPageTitle(I18N::translate('Add an FAQ item'));
1288c2e8227SGreg Roach				$header      = '';
1298c2e8227SGreg Roach				$faqbody     = '';
1308c2e8227SGreg Roach				$block_order = Database::prepare(
1318c2e8227SGreg Roach					"SELECT IFNULL(MAX(block_order)+1, 0) FROM `##block` WHERE module_name = :module_name"
1328c2e8227SGreg Roach				)->execute(array('module_name' => $this->getName()))->fetchOne();
13324ec66ceSGreg Roach				$gedcom_id   = $WT_TREE->getTreeId();
1348c2e8227SGreg Roach			}
1358c2e8227SGreg Roach			$controller->pageHeader();
1368c2e8227SGreg Roach			if (Module::getModuleByName('ckeditor')) {
1378c2e8227SGreg Roach				CkeditorModule::enableEditor($controller);
1388c2e8227SGreg Roach			}
1398c2e8227SGreg Roach
1408c2e8227SGreg Roach			?>
1418c2e8227SGreg Roach			<ol class="breadcrumb small">
1428c2e8227SGreg Roach				<li><a href="admin.php"><?php echo I18N::translate('Control panel'); ?></a></li>
1438c2e8227SGreg Roach				<li><a href="admin_modules.php"><?php echo I18N::translate('Module administration'); ?></a></li>
1444d6c0a77SGreg Roach				<li><a
1454d6c0a77SGreg Roach						href="module.php?mod=<?php echo $this->getName(); ?>&mod_action=admin_config"><?php echo I18N::translate('Frequently asked questions'); ?></a>
1464d6c0a77SGreg Roach				</li>
1478c2e8227SGreg Roach				<li class="active"><?php echo $controller->getPageTitle(); ?></li>
1488c2e8227SGreg Roach			</ol>
1494d6c0a77SGreg Roach			<h1><?php echo $controller->getPageTitle(); ?></h1>
1504d6c0a77SGreg Roach
1514d6c0a77SGreg Roach			<form name="faq" class="form-horizontal" method="post" action="module.php?mod=<?php echo $this->getName(); ?>&amp;mod_action=admin_edit">
1524d6c0a77SGreg Roach			<?php echo Filter::getCsrf(); ?>
1534d6c0a77SGreg Roach			<input type="hidden" name="save" value="1">
1544d6c0a77SGreg Roach			<input type="hidden" name="block_id" value="<?php echo $block_id; ?>">
1554d6c0a77SGreg Roach
1564d6c0a77SGreg Roach			<div class="form-group">
1574d6c0a77SGreg Roach				<label for="header" class="col-sm-3 control-label">
1584d6c0a77SGreg Roach					<?php echo I18N::translate('Question'); ?>
1594d6c0a77SGreg Roach				</label>
1604d6c0a77SGreg Roach
1614d6c0a77SGreg Roach				<div class="col-sm-9">
1624d6c0a77SGreg Roach					<input type="text" class="form-control" name="header" id="header"
1634d6c0a77SGreg Roach					       value="<?php echo Filter::escapeHtml($header); ?>">
1644d6c0a77SGreg Roach				</div>
1654d6c0a77SGreg Roach			</div>
1664d6c0a77SGreg Roach
1674d6c0a77SGreg Roach			<div class="form-group">
1684d6c0a77SGreg Roach				<label for="faqbody" class="col-sm-3 control-label">
1694d6c0a77SGreg Roach					<?php echo I18N::translate('Answer'); ?>
1704d6c0a77SGreg Roach				</label>
1714d6c0a77SGreg Roach
1724d6c0a77SGreg Roach				<div class="col-sm-9">
1734d6c0a77SGreg Roach					<textarea name="faqbody" id="faqbody" class="form-control"
1744d6c0a77SGreg Roach					          rows="10"><?php echo Filter::escapeHtml($faqbody); ?></textarea>
1754d6c0a77SGreg Roach				</div>
1764d6c0a77SGreg Roach			</div>
1774d6c0a77SGreg Roach
1784d6c0a77SGreg Roach			<div class="form-group">
1794d6c0a77SGreg Roach				<label for="xref" class="col-sm-3 control-label">
1804d6c0a77SGreg Roach					<?php echo I18N::translate('Show this block for which languages?'); ?>
1814d6c0a77SGreg Roach				</label>
1824d6c0a77SGreg Roach
1834d6c0a77SGreg Roach				<div class="col-sm-9">
1843d7a8a4cSGreg Roach					<?php echo FunctionsEdit::editLanguageCheckboxes('lang', explode(',', $this->getBlockSetting($block_id, 'languages'))); ?>
1854d6c0a77SGreg Roach				</div>
1864d6c0a77SGreg Roach			</div>
1874d6c0a77SGreg Roach
1884d6c0a77SGreg Roach			<div class="form-group">
1894d6c0a77SGreg Roach				<label for="block_order" class="col-sm-3 control-label">
1904d6c0a77SGreg Roach					<?php echo I18N::translate('FAQ position'); ?>
1914d6c0a77SGreg Roach				</label>
1924d6c0a77SGreg Roach
1934d6c0a77SGreg Roach				<div class="col-sm-9">
1944d6c0a77SGreg Roach					<input type="text" name="block_order" id="block_order" class="form-control" value="<?php echo $block_order; ?>">
1954d6c0a77SGreg Roach				</div>
1964d6c0a77SGreg Roach			</div>
1974d6c0a77SGreg Roach
1984d6c0a77SGreg Roach			<div class="form-group">
1994d6c0a77SGreg Roach				<label for="gedcom_id" class="col-sm-3 control-label">
2004d6c0a77SGreg Roach					<?php echo I18N::translate('FAQ visibility'); ?>
2014d6c0a77SGreg Roach				</label>
2024d6c0a77SGreg Roach
2034d6c0a77SGreg Roach				<div class="col-sm-9">
2043d7a8a4cSGreg Roach					<?php echo FunctionsEdit::selectEditControl('gedcom_id', Tree::getIdList(), I18N::translate('All'), $gedcom_id, 'class="form-control"'); ?>
2054d6c0a77SGreg Roach					<p class="small text-muted">
2064d6c0a77SGreg Roach						<?php echo I18N::translate('A FAQ item can be displayed on just one of the family trees, or on all the family trees.'); ?>
2074d6c0a77SGreg Roach					</p>
2084d6c0a77SGreg Roach				</div>
2094d6c0a77SGreg Roach			</div>
2104d6c0a77SGreg Roach
2114d6c0a77SGreg Roach			<div class="form-group">
2124d6c0a77SGreg Roach				<div class="col-sm-offset-3 col-sm-9">
2134d6c0a77SGreg Roach					<button type="submit" class="btn btn-primary">
2144d6c0a77SGreg Roach						<i class="fa fa-check"></i>
2154d6c0a77SGreg Roach						<?php echo I18N::translate('save'); ?>
2164d6c0a77SGreg Roach					</button>
2174d6c0a77SGreg Roach				</div>
2184d6c0a77SGreg Roach			</div>
2194d6c0a77SGreg Roach
2204d6c0a77SGreg Roach		</form>
2218c2e8227SGreg Roach		<?php
2228c2e8227SGreg Roach		}
2238c2e8227SGreg Roach	}
2248c2e8227SGreg Roach
2258c2e8227SGreg Roach	/**
2268c2e8227SGreg Roach	 * Respond to a request to delete a FAQ.
2278c2e8227SGreg Roach	 */
2288c2e8227SGreg Roach	private function delete() {
2298c2e8227SGreg Roach		$block_id = Filter::getInteger('block_id');
2308c2e8227SGreg Roach
2318c2e8227SGreg Roach		Database::prepare(
2328c2e8227SGreg Roach			"DELETE FROM `##block_setting` WHERE block_id = :block_id"
2338c2e8227SGreg Roach		)->execute(array('block_id' => $block_id));
2348c2e8227SGreg Roach
2358c2e8227SGreg Roach		Database::prepare(
2368c2e8227SGreg Roach			"DELETE FROM `##block` WHERE block_id = :block_id"
2378c2e8227SGreg Roach		)->execute(array('block_id' => $block_id));
2388c2e8227SGreg Roach	}
2398c2e8227SGreg Roach
2408c2e8227SGreg Roach	/**
2418c2e8227SGreg Roach	 * Respond to a request to move a FAQ up the list.
2428c2e8227SGreg Roach	 */
2438c2e8227SGreg Roach	private function moveup() {
2448c2e8227SGreg Roach		$block_id = Filter::getInteger('block_id');
2458c2e8227SGreg Roach
2468c2e8227SGreg Roach		$block_order = Database::prepare(
2478c2e8227SGreg Roach			"SELECT block_order FROM `##block` WHERE block_id = :block_id"
2488c2e8227SGreg Roach		)->execute(array('block_id' => $block_id))->fetchOne();
2498c2e8227SGreg Roach
2508c2e8227SGreg Roach		$swap_block = Database::prepare(
2518c2e8227SGreg Roach			"SELECT block_order, block_id" .
2528c2e8227SGreg Roach			" FROM `##block`" .
2538c2e8227SGreg Roach			" WHERE block_order = (" .
2548c2e8227SGreg Roach			"  SELECT MAX(block_order) FROM `##block` WHERE block_order < :block_order AND module_name = :module_name_1" .
2558c2e8227SGreg Roach			" ) AND module_name = :module_name_2" .
2568c2e8227SGreg Roach			" LIMIT 1"
2578c2e8227SGreg Roach		)->execute(array(
2588c2e8227SGreg Roach			'block_order'   => $block_order,
2598c2e8227SGreg Roach			'module_name_1' => $this->getName(),
260cbc1590aSGreg Roach			'module_name_2' => $this->getName(),
2618c2e8227SGreg Roach		))->fetchOneRow();
2628c2e8227SGreg Roach		if ($swap_block) {
2638c2e8227SGreg Roach			Database::prepare(
2648c2e8227SGreg Roach				"UPDATE `##block` SET block_order = :block_order WHERE block_id = :block_id"
2658c2e8227SGreg Roach			)->execute(array(
2668c2e8227SGreg Roach				'block_order' => $swap_block->block_order,
2678c2e8227SGreg Roach				'block_id'    => $block_id,
2688c2e8227SGreg Roach			));
2698c2e8227SGreg Roach			Database::prepare(
2708c2e8227SGreg Roach				"UPDATE `##block` SET block_order = :block_order WHERE block_id = :block_id"
2718c2e8227SGreg Roach			)->execute(array(
2728c2e8227SGreg Roach				'block_order' => $block_order,
2738c2e8227SGreg Roach				'block_id'    => $swap_block->block_id,
2748c2e8227SGreg Roach			));
2758c2e8227SGreg Roach		}
2768c2e8227SGreg Roach	}
2778c2e8227SGreg Roach
2788c2e8227SGreg Roach	/**
2798c2e8227SGreg Roach	 * Respond to a request to move a FAQ down the list.
2808c2e8227SGreg Roach	 */
2818c2e8227SGreg Roach	private function movedown() {
2828c2e8227SGreg Roach		$block_id = Filter::get('block_id');
2838c2e8227SGreg Roach
2848c2e8227SGreg Roach		$block_order = Database::prepare(
2858c2e8227SGreg Roach			"SELECT block_order FROM `##block` WHERE block_id = :block_id"
2868c2e8227SGreg Roach		)->execute(array(
2878c2e8227SGreg Roach			'block_id' => $block_id,
2888c2e8227SGreg Roach		))->fetchOne();
2898c2e8227SGreg Roach
2908c2e8227SGreg Roach		$swap_block = Database::prepare(
2918c2e8227SGreg Roach			"SELECT block_order, block_id" .
2928c2e8227SGreg Roach			" FROM `##block`" .
2938c2e8227SGreg Roach			" WHERE block_order=(" .
2948c2e8227SGreg Roach			"  SELECT MIN(block_order) FROM `##block` WHERE block_order > :block_order AND module_name = :module_name_1" .
2958c2e8227SGreg Roach			" ) AND module_name = :module_name_2" .
2968c2e8227SGreg Roach			" LIMIT 1"
2978c2e8227SGreg Roach		)->execute(array(
2988c2e8227SGreg Roach			'block_order'   => $block_order,
2998c2e8227SGreg Roach			'module_name_1' => $this->getName(),
3008c2e8227SGreg Roach			'module_name_2' => $this->getName(),
3018c2e8227SGreg Roach			))->fetchOneRow();
3028c2e8227SGreg Roach		if ($swap_block) {
3038c2e8227SGreg Roach			Database::prepare(
3048c2e8227SGreg Roach				"UPDATE `##block` SET block_order = :block_order WHERE block_id = :block_id"
3058c2e8227SGreg Roach			)->execute(array(
3068c2e8227SGreg Roach				'block_order' => $swap_block->block_order,
3078c2e8227SGreg Roach				'block_id'    => $block_id,
3088c2e8227SGreg Roach			));
3098c2e8227SGreg Roach			Database::prepare(
3108c2e8227SGreg Roach				"UPDATE `##block` SET block_order = :block_order WHERE block_id = :block_id"
3118c2e8227SGreg Roach			)->execute(array(
3128c2e8227SGreg Roach				'block_order' => $block_order,
3138c2e8227SGreg Roach				'block_id'    => $swap_block->block_id,
3148c2e8227SGreg Roach			));
3158c2e8227SGreg Roach		}
3168c2e8227SGreg Roach	}
3178c2e8227SGreg Roach
3188c2e8227SGreg Roach	/**
3198c2e8227SGreg Roach	 * Show a list of FAQs
3208c2e8227SGreg Roach	 */
3218c2e8227SGreg Roach	private function show() {
3224b9ff166SGreg Roach		global $controller, $WT_TREE;
3234b9ff166SGreg Roach
3248c2e8227SGreg Roach		$controller = new PageController;
3258c2e8227SGreg Roach		$controller
3268c2e8227SGreg Roach			->setPageTitle(I18N::translate('Frequently asked questions'))
3278c2e8227SGreg Roach			->pageHeader();
3288c2e8227SGreg Roach
3298c2e8227SGreg Roach		$faqs = Database::prepare(
3308c2e8227SGreg Roach			"SELECT block_id, bs1.setting_value AS header, bs2.setting_value AS body, bs3.setting_value AS languages" .
3318c2e8227SGreg Roach			" FROM `##block` b" .
3328c2e8227SGreg Roach			" JOIN `##block_setting` bs1 USING (block_id)" .
3338c2e8227SGreg Roach			" JOIN `##block_setting` bs2 USING (block_id)" .
3348c2e8227SGreg Roach			" JOIN `##block_setting` bs3 USING (block_id)" .
3358c2e8227SGreg Roach			" WHERE module_name = :module_name" .
3368c2e8227SGreg Roach			" AND bs1.setting_name = 'header'" .
3378c2e8227SGreg Roach			" AND bs2.setting_name = 'faqbody'" .
3388c2e8227SGreg Roach			" AND bs3.setting_name = 'languages'" .
3398c2e8227SGreg Roach			" AND IFNULL(gedcom_id, :tree_id_1) = :tree_id_2" .
3408c2e8227SGreg Roach			" ORDER BY block_order"
3418c2e8227SGreg Roach		)->execute(array(
3428c2e8227SGreg Roach			'module_name' => $this->getName(),
34324ec66ceSGreg Roach			'tree_id_1'   => $WT_TREE->getTreeId(),
34424ec66ceSGreg Roach			'tree_id_2'   => $WT_TREE->getTreeId(),
3458c2e8227SGreg Roach		))->fetchAll();
3468c2e8227SGreg Roach
3478c2e8227SGreg Roach		// Define your colors for the alternating rows
3488c2e8227SGreg Roach		echo '<h2 class="center">', I18N::translate('Frequently asked questions'), '</h2>';
3498c2e8227SGreg Roach		// Instructions
35077e70a22SGreg Roach		echo '<div class="faq_italic">', I18N::translate('Click on a title to go straight to it, or scroll down to read them all.');
3514b9ff166SGreg Roach		if (Auth::isManager($WT_TREE)) {
3528c2e8227SGreg Roach			echo '<div class="faq_edit"><a href="module.php?mod=', $this->getName(), '&amp;mod_action=admin_config">', I18N::translate('Click here to add, edit, or delete'), '</a></div>';
3538c2e8227SGreg Roach		}
3548c2e8227SGreg Roach		echo '</div>';
3558c2e8227SGreg Roach		$row_count = 0;
3568c2e8227SGreg Roach		echo '<table class="faq">';
3578c2e8227SGreg Roach		// List of titles
3588c2e8227SGreg Roach		foreach ($faqs as $id => $faq) {
3598c2e8227SGreg Roach			if (!$faq->languages || in_array(WT_LOCALE, explode(',', $faq->languages))) {
3608c2e8227SGreg Roach				$row_color = ($row_count % 2) ? 'odd' : 'even';
3618c2e8227SGreg Roach				// NOTE: Print the header of the current item
3628c2e8227SGreg Roach				echo '<tr class="', $row_color, '"><td style="padding: 5px;">';
3638c2e8227SGreg Roach				echo '<a href="#faq', $id, '">', $faq->header, '</a>';
3648c2e8227SGreg Roach				echo '</td></tr>';
3658c2e8227SGreg Roach				$row_count++;
3668c2e8227SGreg Roach			}
3678c2e8227SGreg Roach		}
3688c2e8227SGreg Roach		echo '</table><hr>';
3698c2e8227SGreg Roach		// Detailed entries
3708c2e8227SGreg Roach		foreach ($faqs as $id => $faq) {
3718c2e8227SGreg Roach			if (!$faq->languages || in_array(WT_LOCALE, explode(',', $faq->languages))) {
3728c2e8227SGreg Roach				echo '<div class="faq_title" id="faq', $id, '">', $faq->header;
3738c2e8227SGreg Roach				echo '<div class="faq_top faq_italic">';
3748c2e8227SGreg Roach				echo '<a href="#content">', I18N::translate('back to top'), '</a>';
3758c2e8227SGreg Roach				echo '</div>';
3768c2e8227SGreg Roach				echo '</div>';
3778c2e8227SGreg Roach				echo '<div class="faq_body">', substr($faq->body, 0, 1) == '<' ? $faq->body : nl2br($faq->body, false), '</div>';
3788c2e8227SGreg Roach				echo '<hr>';
3798c2e8227SGreg Roach			}
3808c2e8227SGreg Roach		}
3818c2e8227SGreg Roach	}
3828c2e8227SGreg Roach
3838c2e8227SGreg Roach	/**
3848c2e8227SGreg Roach	 * Provide a form to manage the FAQs.
3858c2e8227SGreg Roach	 */
3868c2e8227SGreg Roach	private function config() {
38724ec66ceSGreg Roach		global $WT_TREE;
38824ec66ceSGreg Roach
3898c2e8227SGreg Roach		$controller = new PageController;
3908c2e8227SGreg Roach		$controller
391*da3772f8SJustCarmen			->restrictAccess(Auth::isAdmin())
3928c2e8227SGreg Roach			->setPageTitle(I18N::translate('Frequently asked questions'))
3938c2e8227SGreg Roach			->pageHeader();
3948c2e8227SGreg Roach
3958c2e8227SGreg Roach		$faqs = Database::prepare(
3968c2e8227SGreg Roach			"SELECT block_id, block_order, gedcom_id, bs1.setting_value AS header, bs2.setting_value AS faqbody" .
3978c2e8227SGreg Roach			" FROM `##block` b" .
3988c2e8227SGreg Roach			" JOIN `##block_setting` bs1 USING (block_id)" .
3998c2e8227SGreg Roach			" JOIN `##block_setting` bs2 USING (block_id)" .
4008c2e8227SGreg Roach			" WHERE module_name = :module_name" .
4018c2e8227SGreg Roach			" AND bs1.setting_name = 'header'" .
4028c2e8227SGreg Roach			" AND bs2.setting_name = 'faqbody'" .
4038c2e8227SGreg Roach			" AND IFNULL(gedcom_id, :tree_id_1) = :tree_id_2" .
4048c2e8227SGreg Roach			" ORDER BY block_order"
4058c2e8227SGreg Roach		)->execute(array(
4068c2e8227SGreg Roach			'module_name' => $this->getName(),
40724ec66ceSGreg Roach			'tree_id_1'   => $WT_TREE->getTreeId(),
40824ec66ceSGreg Roach			'tree_id_2'   => $WT_TREE->getTreeId(),
4098c2e8227SGreg Roach			))->fetchAll();
4108c2e8227SGreg Roach
4118c2e8227SGreg Roach		$min_block_order = Database::prepare(
412ee928960SGreg Roach			"SELECT MIN(block_order) FROM `##block` WHERE module_name = 'faq' AND (gedcom_id = :tree_id OR gedcom_id IS NULL)"
4134d6c0a77SGreg Roach		)->execute(array(
4144d6c0a77SGreg Roach			'tree_id' => $WT_TREE->getTreeId(),
4154d6c0a77SGreg Roach		))->fetchOne();
4168c2e8227SGreg Roach
4178c2e8227SGreg Roach		$max_block_order = Database::prepare(
418ee928960SGreg Roach			"SELECT MAX(block_order) FROM `##block` WHERE module_name = 'faq' AND (gedcom_id = :tree_id OR gedcom_id IS NULL)"
4194d6c0a77SGreg Roach		)->execute(array(
4204d6c0a77SGreg Roach			'tree_id' => $WT_TREE->getTreeId(),
4214d6c0a77SGreg Roach		))->fetchOne();
4228c2e8227SGreg Roach
4238c2e8227SGreg Roach		?>
4248c2e8227SGreg Roach		<ol class="breadcrumb small">
4258c2e8227SGreg Roach			<li><a href="admin.php"><?php echo I18N::translate('Control panel'); ?></a></li>
4268c2e8227SGreg Roach			<li><a href="admin_modules.php"><?php echo I18N::translate('Module administration'); ?></a></li>
4278c2e8227SGreg Roach			<li class="active"><?php echo $controller->getPageTitle(); ?></li>
4288c2e8227SGreg Roach		</ol>
4298c2e8227SGreg Roach		<h2><?php echo $controller->getPageTitle(); ?></h2>
4308c2e8227SGreg Roach		<p>
4318c2e8227SGreg Roach			<?php echo 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.'); ?>
4328c2e8227SGreg Roach			<?php echo I18N::translate('You may use HTML to format the answer and to add links to other websites.'); ?>
4338c2e8227SGreg Roach		</p>
43407231e86SGreg Roach		<form class="form form-inline">
43507231e86SGreg Roach			<label for="ged" class="sr-only">
43607231e86SGreg Roach				<?php echo I18N::translate('Family tree'); ?>
43707231e86SGreg Roach			</label>
43807231e86SGreg Roach			<input type="hidden" name="mod" value="<?php echo  $this->getName(); ?>">
43907231e86SGreg Roach			<input type="hidden" name="mod_action" value="admin_config">
44007231e86SGreg Roach			<?php echo FunctionsEdit::selectEditControl('ged', Tree::getNameList(), null, $WT_TREE->getName(), 'class="form-control"'); ?>
44107231e86SGreg Roach			<input type="submit" class="btn btn-primary" value="<?php echo I18N::translate('show'); ?>">
44207231e86SGreg Roach		</form>
44307231e86SGreg Roach
44407231e86SGreg Roach		<p>
44507231e86SGreg Roach			<a href="module.php?mod=<?php echo $this->getName(); ?>&amp;mod_action=admin_edit" class="btn btn-default">
44607231e86SGreg Roach				<i class="fa fa-plus"></i>
44707231e86SGreg Roach				<?php echo I18N::translate('Add an FAQ item'); ?>
44807231e86SGreg Roach			</a>
44907231e86SGreg Roach		</p>
45007231e86SGreg Roach
4518c2e8227SGreg Roach		<?php
4524d6c0a77SGreg Roach		echo '<table class="table table-bordered">';
4538c2e8227SGreg Roach		if (empty($faqs)) {
4548c2e8227SGreg Roach			echo '<tr><td class="error center" colspan="5">', I18N::translate('The FAQ list is empty.'), '</td></tr></table>';
4558c2e8227SGreg Roach		} else {
4568c2e8227SGreg Roach			foreach ($faqs as $faq) {
4578c2e8227SGreg Roach				// NOTE: Print the position of the current item
4588c2e8227SGreg Roach				echo '<tr class="faq_edit_pos"><td>';
4594d6c0a77SGreg Roach				echo I18N::translate('#%s', $faq->block_order + 1), ' ';
4603a7d762dSGreg Roach				if ($faq->gedcom_id === null) {
4618c2e8227SGreg Roach					echo I18N::translate('All');
4628c2e8227SGreg Roach				} else {
463f306ecacSDavid Drury					echo $WT_TREE->getTitleHtml();
4648c2e8227SGreg Roach				}
4658c2e8227SGreg Roach				echo '</td>';
4668c2e8227SGreg Roach				// NOTE: Print the edit options of the current item
4678c2e8227SGreg Roach				echo '<td>';
4688c2e8227SGreg Roach				if ($faq->block_order == $min_block_order) {
4698c2e8227SGreg Roach					echo '&nbsp;';
4708c2e8227SGreg Roach				} else {
4714d6c0a77SGreg Roach					echo '<a href="module.php?mod=', $this->getName(), '&amp;mod_action=admin_moveup&amp;block_id=', $faq->block_id, '"><i class="fa fa-arrow-up"></i></i> ', I18N::translate('Move up'), '</a>';
4728c2e8227SGreg Roach				}
4738c2e8227SGreg Roach				echo '</td><td>';
4748c2e8227SGreg Roach				if ($faq->block_order == $max_block_order) {
4758c2e8227SGreg Roach					echo '&nbsp;';
4768c2e8227SGreg Roach				} else {
4774d6c0a77SGreg Roach					echo '<a href="module.php?mod=', $this->getName(), '&amp;mod_action=admin_movedown&amp;block_id=', $faq->block_id, '"><i class="fa fa-arrow-down"></i></i> ', I18N::translate('Move down'), '</a>';
4788c2e8227SGreg Roach				}
4798c2e8227SGreg Roach				echo '</td><td>';
4804d6c0a77SGreg Roach				echo '<a href="module.php?mod=', $this->getName(), '&amp;mod_action=admin_edit&amp;block_id=', $faq->block_id, '"><i class="fa fa-pencil"></i> ', I18N::translate('Edit'), '</a>';
4818c2e8227SGreg Roach				echo '</td><td>';
482727fa558SGreg 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”?', Filter::escapeHtml($faq->header)), '\');"><i class="fa fa-trash"></i> ', I18N::translate('Delete'), '</a>';
4838c2e8227SGreg Roach				echo '</td></tr>';
4848c2e8227SGreg Roach				// NOTE: Print the title text of the current item
4858c2e8227SGreg Roach				echo '<tr><td colspan="5">';
4868c2e8227SGreg Roach				echo '<div class="faq_edit_item">';
4878c2e8227SGreg Roach				echo '<div class="faq_edit_title">', $faq->header, '</div>';
4888c2e8227SGreg Roach				// NOTE: Print the body text of the current item
4898c2e8227SGreg Roach				echo '<div class="faq_edit_content">', substr($faq->faqbody, 0, 1) == '<' ? $faq->faqbody : nl2br($faq->faqbody, false), '</div></div></td></tr>';
4908c2e8227SGreg Roach			}
4918c2e8227SGreg Roach			echo '</table>';
4928c2e8227SGreg Roach		}
4938c2e8227SGreg Roach	}
4948c2e8227SGreg Roach
4950ee13198SGreg Roach	/**
4960ee13198SGreg Roach	 * The user can re-order menus.  Until they do, they are shown in this order.
4970ee13198SGreg Roach	 *
4980ee13198SGreg Roach	 * @return int
4990ee13198SGreg Roach	 */
5008c2e8227SGreg Roach	public function defaultMenuOrder() {
5018c2e8227SGreg Roach		return 40;
5028c2e8227SGreg Roach	}
5038c2e8227SGreg Roach
5040ee13198SGreg Roach	/**
5050ee13198SGreg Roach	 * A menu, to be added to the main application menu.
5060ee13198SGreg Roach	 *
5070ee13198SGreg Roach	 * @return Menu|null
5080ee13198SGreg Roach	 */
5098c2e8227SGreg Roach	public function getMenu() {
51024ec66ceSGreg Roach		global $WT_TREE;
51124ec66ceSGreg Roach
5128c2e8227SGreg Roach		$faqs = Database::prepare(
5138c2e8227SGreg Roach			"SELECT block_id FROM `##block` WHERE module_name = :module_name AND IFNULL(gedcom_id, :tree_id_1) = :tree_id_2"
5148c2e8227SGreg Roach		)->execute(array(
5158c2e8227SGreg Roach			'module_name' => $this->getName(),
51624ec66ceSGreg Roach			'tree_id_1'   => $WT_TREE->getTreeId(),
51724ec66ceSGreg Roach			'tree_id_2'   => $WT_TREE->getTreeId(),
5188c2e8227SGreg Roach		))->fetchAll();
5198c2e8227SGreg Roach
52038a9583bSGreg Roach		if ($faqs) {
52138a9583bSGreg Roach			return new Menu(I18N::translate('FAQ'), 'module.php?mod=faq&amp;mod_action=show', 'menu-help');
52238a9583bSGreg Roach		} else {
5238c2e8227SGreg Roach			return null;
5248c2e8227SGreg Roach		}
5258c2e8227SGreg Roach
5268c2e8227SGreg Roach	}
5278c2e8227SGreg Roach}
528