xref: /webtrees/app/Module/FrequentlyAskedQuestionsModule.php (revision 6bdf767435631ad1dc27ec1ffd855d43dbdce907)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Controller\PageController;
20use Fisharebest\Webtrees\Database;
21use Fisharebest\Webtrees\Filter;
22use Fisharebest\Webtrees\Functions\FunctionsEdit;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Menu;
25use Fisharebest\Webtrees\Module;
26use Fisharebest\Webtrees\Tree;
27
28/**
29 * Class FrequentlyAskedQuestionsModule
30 */
31class FrequentlyAskedQuestionsModule extends AbstractModule implements ModuleMenuInterface, ModuleConfigInterface {
32	/** {@inheritdoc} */
33	public function getTitle() {
34		return /* I18N: Name of a module. Abbreviation for “Frequently Asked Questions” */ I18N::translate('FAQ');
35	}
36
37	/** {@inheritdoc} */
38	public function getDescription() {
39		return /* I18N: Description of the “FAQ” module */ I18N::translate('A list of frequently asked questions and answers.');
40	}
41
42	/**
43	 * This is a general purpose hook, allowing modules to respond to routes
44	 * of the form module.php?mod=FOO&mod_action=BAR
45	 *
46	 * @param string $mod_action
47	 */
48	public function modAction($mod_action) {
49		switch ($mod_action) {
50		case 'admin_config':
51			$this->config();
52			break;
53		case 'admin_delete':
54			if (Auth::isAdmin()) {
55				$this->delete();
56			}
57			header('Location: ' . WT_BASE_URL . 'module.php?mod=faq&mod_action=admin_config');
58			break;
59		case 'admin_edit':
60			$this->edit();
61			break;
62		case 'admin_edit_save':
63			if (Auth::isAdmin()) {
64				$this->editSave();
65			}
66			header('Location: ' . WT_BASE_URL . 'module.php?mod=faq&mod_action=admin_config');
67			break;
68		case 'admin_movedown':
69			if (Auth::isAdmin()) {
70				$this->movedown();
71			}
72			header('Location: ' . WT_BASE_URL . 'module.php?mod=faq&mod_action=admin_config');
73			break;
74		case 'admin_moveup':
75			if (Auth::isAdmin()) {
76				$this->moveup();
77			}
78			header('Location: ' . WT_BASE_URL . 'module.php?mod=faq&mod_action=admin_config');
79			break;
80		case 'show':
81			$this->show();
82			break;
83		default:
84			http_response_code(404);
85		}
86	}
87
88	/** {@inheritdoc} */
89	public function getConfigLink() {
90		return 'module.php?mod=' . $this->getName() . '&amp;mod_action=admin_config';
91	}
92
93	/**
94	 * Action from the configuration page
95	 */
96	private function editSave() {
97		if (Filter::checkCsrf()) {
98			$block_id = Filter::postInteger('block_id');
99			if ($block_id) {
100				Database::prepare(
101					"UPDATE `##block` SET gedcom_id = NULLIF(:tree_id, '0'), block_order = :block_order WHERE block_id = :block_id"
102				)->execute([
103					'tree_id'     => Filter::postInteger('gedcom_id'),
104					'block_order' => Filter::postInteger('block_order'),
105					'block_id'    => $block_id,
106				]);
107			} else {
108				Database::prepare(
109					"INSERT INTO `##block` (gedcom_id, module_name, block_order) VALUES (NULLIF(:tree_id, '0'), :module_name, :block_order)"
110				)->execute([
111					'tree_id'     => Filter::postInteger('gedcom_id'),
112					'module_name' => $this->getName(),
113					'block_order' => Filter::postInteger('block_order'),
114				]);
115				$block_id = Database::getInstance()->lastInsertId();
116			}
117			$this->setBlockSetting($block_id, 'header', Filter::post('header'));
118			$this->setBlockSetting($block_id, 'faqbody', Filter::post('faqbody'));
119
120			$languages = Filter::postArray('lang');
121			$this->setBlockSetting($block_id, 'languages', implode(',', $languages));
122		}
123	}
124
125	/**
126	 * Action from the configuration page
127	 */
128	private function edit() {
129		global $WT_TREE;
130
131		$controller = new PageController;
132		$controller->restrictAccess(Auth::isAdmin());
133
134			$block_id = Filter::getInteger('block_id');
135		if ($block_id) {
136			$controller->setPageTitle(/* I18N: FAQ = “Frequently Asked Question” */ I18N::translate('Edit the FAQ'));
137			$header      = $this->getBlockSetting($block_id, 'header');
138			$faqbody     = $this->getBlockSetting($block_id, 'faqbody');
139			$block_order = Database::prepare(
140				"SELECT block_order FROM `##block` WHERE block_id = :block_id"
141			)->execute(['block_id' => $block_id])->fetchOne();
142			$gedcom_id   = Database::prepare(
143				"SELECT gedcom_id FROM `##block` WHERE block_id = :block_id"
144			)->execute(['block_id' => $block_id])->fetchOne();
145		} else {
146			$controller->setPageTitle(/* I18N: FAQ = “Frequently Asked Question” */ I18N::translate('Add an FAQ'));
147			$header      = '';
148			$faqbody     = '';
149			$block_order = Database::prepare(
150				"SELECT IFNULL(MAX(block_order)+1, 0) FROM `##block` WHERE module_name = :module_name"
151			)->execute(['module_name' => $this->getName()])->fetchOne();
152			$gedcom_id   = $WT_TREE->getTreeId();
153		}
154		$controller->pageHeader();
155		if (Module::getModuleByName('ckeditor')) {
156			CkeditorModule::enableEditor($controller);
157		}
158
159		?>
160		<ol class="breadcrumb small">
161			<li><a href="admin.php"><?php echo I18N::translate('Control panel'); ?></a></li>
162			<li><a href="admin_modules.php"><?php echo I18N::translate('Module administration'); ?></a></li>
163			<li><a href="module.php?mod=<?php echo $this->getName(); ?>&mod_action=admin_config"><?php echo I18N::translate('Frequently asked questions'); ?></a>
164			</li>
165			<li class="active"><?php echo $controller->getPageTitle(); ?></li>
166		</ol>
167		<h1><?php echo $controller->getPageTitle(); ?></h1>
168
169		<form name="faq" class="form-horizontal" method="post" action="module.php?mod=<?php echo $this->getName(); ?>&amp;mod_action=admin_edit_save">
170		<?php echo Filter::getCsrf(); ?>
171		<input type="hidden" name="block_id" value="<?php echo $block_id; ?>">
172
173		<div class="form-group">
174			<label for="header" class="col-sm-3 control-label">
175				<?= I18N::translate('Question') ?>
176			</label>
177
178			<div class="col-sm-9">
179				<input type="text" class="form-control" name="header" id="header"
180				       value="<?= Filter::escapeHtml($header) ?>">
181			</div>
182		</div>
183
184		<div class="form-group">
185			<label for="faqbody" class="col-sm-3 control-label">
186				<?= I18N::translate('Answer') ?>
187			</label>
188
189			<div class="col-sm-9">
190				<textarea name="faqbody" id="faqbody" class="form-control html-edit" rows="10"><?= Filter::escapeHtml($faqbody) ?></textarea>
191			</div>
192		</div>
193
194		<div class="form-group">
195			<label for="xref" class="col-sm-3 control-label">
196				<?= /* I18N: Label for a configuration option */ I18N::translate('Show this block for which languages') ?>
197			</label>
198
199			<div class="col-sm-9">
200				<?= FunctionsEdit::editLanguageCheckboxes('lang', explode(',', $this->getBlockSetting($block_id, 'languages'))) ?>
201			</div>
202		</div>
203
204		<div class="form-group">
205			<label for="block_order" class="col-sm-3 control-label">
206				<?= I18N::translate('Sort order') ?>
207			</label>
208
209			<div class="col-sm-9">
210				<input type="text" name="block_order" id="block_order" class="form-control" value="<?= $block_order ?>">
211			</div>
212		</div>
213
214		<div class="form-group">
215			<label for="gedcom_id" class="col-sm-3 control-label">
216				<?= I18N::translate('Family tree') ?>
217			</label>
218
219			<div class="col-sm-9">
220				<?php echo FunctionsEdit::selectEditControl('gedcom_id', Tree::getIdList(), I18N::translate('All'), $gedcom_id, 'class="form-control"'); ?>
221				<p class="small text-muted">
222					<?= /* 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.') ?>
223				</p>
224			</div>
225		</div>
226
227		<div class="form-group">
228			<div class="col-sm-offset-3 col-sm-9">
229				<button type="submit" class="btn btn-primary">
230					<i class="fa fa-check"></i>
231					<?= I18N::translate('save') ?>
232				</button>
233			</div>
234		</div>
235
236	</form>
237	<?php
238	}
239
240	/**
241	 * Delete an FAQ.
242	 */
243	private function delete() {
244		$block_id = Filter::getInteger('block_id');
245
246		Database::prepare(
247			"DELETE FROM `##block_setting` WHERE block_id = :block_id"
248		)->execute(['block_id' => $block_id]);
249
250		Database::prepare(
251			"DELETE FROM `##block` WHERE block_id = :block_id"
252		)->execute(['block_id' => $block_id]);
253	}
254
255	/**
256	 * Move an FAQ up the list.
257	 */
258	private function moveup() {
259		$block_id = Filter::getInteger('block_id');
260
261		$block_order = Database::prepare(
262			"SELECT block_order FROM `##block` WHERE block_id = :block_id"
263		)->execute(['block_id' => $block_id])->fetchOne();
264
265		$swap_block = Database::prepare(
266			"SELECT block_order, block_id" .
267			" FROM `##block`" .
268			" WHERE block_order = (" .
269			"  SELECT MAX(block_order) FROM `##block` WHERE block_order < :block_order AND module_name = :module_name_1" .
270			" ) AND module_name = :module_name_2" .
271			" LIMIT 1"
272		)->execute([
273			'block_order'   => $block_order,
274			'module_name_1' => $this->getName(),
275			'module_name_2' => $this->getName(),
276		])->fetchOneRow();
277		if ($swap_block) {
278			Database::prepare(
279				"UPDATE `##block` SET block_order = :block_order WHERE block_id = :block_id"
280			)->execute([
281				'block_order' => $swap_block->block_order,
282				'block_id'    => $block_id,
283			]);
284			Database::prepare(
285				"UPDATE `##block` SET block_order = :block_order WHERE block_id = :block_id"
286			)->execute([
287				'block_order' => $block_order,
288				'block_id'    => $swap_block->block_id,
289			]);
290		}
291	}
292
293	/**
294	 * Move an FAQ down the list.
295	 */
296	private function movedown() {
297		$block_id = Filter::get('block_id');
298
299		$block_order = Database::prepare(
300			"SELECT block_order FROM `##block` WHERE block_id = :block_id"
301		)->execute([
302			'block_id' => $block_id,
303		])->fetchOne();
304
305		$swap_block = Database::prepare(
306			"SELECT block_order, block_id" .
307			" FROM `##block`" .
308			" WHERE block_order=(" .
309			"  SELECT MIN(block_order) FROM `##block` WHERE block_order > :block_order AND module_name = :module_name_1" .
310			" ) AND module_name = :module_name_2" .
311			" LIMIT 1"
312		)->execute([
313			'block_order'   => $block_order,
314			'module_name_1' => $this->getName(),
315			'module_name_2' => $this->getName(),
316			])->fetchOneRow();
317		if ($swap_block) {
318			Database::prepare(
319				"UPDATE `##block` SET block_order = :block_order WHERE block_id = :block_id"
320			)->execute([
321				'block_order' => $swap_block->block_order,
322				'block_id'    => $block_id,
323			]);
324			Database::prepare(
325				"UPDATE `##block` SET block_order = :block_order WHERE block_id = :block_id"
326			)->execute([
327				'block_order' => $block_order,
328				'block_id'    => $swap_block->block_id,
329			]);
330		}
331	}
332
333	/**
334	 * Show a list of FAQs
335	 */
336	private function show() {
337		global $controller, $WT_TREE;
338
339		$controller = new PageController;
340		$controller
341			->setPageTitle(I18N::translate('Frequently asked questions'))
342			->pageHeader();
343
344		$faqs = Database::prepare(
345			"SELECT block_id, bs1.setting_value AS header, bs2.setting_value AS body, bs3.setting_value AS languages" .
346			" FROM `##block` b" .
347			" JOIN `##block_setting` bs1 USING (block_id)" .
348			" JOIN `##block_setting` bs2 USING (block_id)" .
349			" JOIN `##block_setting` bs3 USING (block_id)" .
350			" WHERE module_name = :module_name" .
351			" AND bs1.setting_name = 'header'" .
352			" AND bs2.setting_name = 'faqbody'" .
353			" AND bs3.setting_name = 'languages'" .
354			" AND IFNULL(gedcom_id, :tree_id_1) = :tree_id_2" .
355			" ORDER BY block_order"
356		)->execute([
357			'module_name' => $this->getName(),
358			'tree_id_1'   => $WT_TREE->getTreeId(),
359			'tree_id_2'   => $WT_TREE->getTreeId(),
360		])->fetchAll();
361
362		echo '<h2 class="center">', I18N::translate('Frequently asked questions');
363		if (Auth::isManager($WT_TREE)) {
364			echo ' — <a href="module.php?mod=', $this->getName(), '&amp;mod_action=admin_config">', I18N::translate('edit'), '</a>';
365		}
366		echo '</h2>';
367		$row_count = 0;
368		echo '<table class="faq">';
369		// List of titles
370		foreach ($faqs as $id => $faq) {
371			if (!$faq->languages || in_array(WT_LOCALE, explode(',', $faq->languages))) {
372				$row_color = ($row_count % 2) ? 'odd' : 'even';
373				// NOTE: Print the header of the current item
374				echo '<tr class="', $row_color, '"><td style="padding: 5px;">';
375				echo '<a href="#faq', $id, '">', $faq->header, '</a>';
376				echo '</td></tr>';
377				$row_count++;
378			}
379		}
380		echo '</table><hr>';
381		// Detailed entries
382		foreach ($faqs as $id => $faq) {
383			if (!$faq->languages || in_array(WT_LOCALE, explode(',', $faq->languages))) {
384				echo '<div class="faq_title" id="faq', $id, '">', $faq->header;
385				echo '<div class="faq_top faq_italic">';
386				echo '<a href="#content">', I18N::translate('back to top'), '</a>';
387				echo '</div>';
388				echo '</div>';
389				echo '<div class="faq_body">', substr($faq->body, 0, 1) == '<' ? $faq->body : nl2br($faq->body, false), '</div>';
390				echo '<hr>';
391			}
392		}
393	}
394
395	/**
396	 * Provide a form to manage the FAQs.
397	 */
398	private function config() {
399		global $WT_TREE;
400
401		$controller = new PageController;
402		$controller
403			->restrictAccess(Auth::isAdmin())
404			->setPageTitle(I18N::translate('Frequently asked questions'))
405			->pageHeader();
406
407		$faqs = Database::prepare(
408			"SELECT block_id, block_order, gedcom_id, bs1.setting_value AS header, bs2.setting_value AS faqbody" .
409			" FROM `##block` b" .
410			" JOIN `##block_setting` bs1 USING (block_id)" .
411			" JOIN `##block_setting` bs2 USING (block_id)" .
412			" WHERE module_name = :module_name" .
413			" AND bs1.setting_name = 'header'" .
414			" AND bs2.setting_name = 'faqbody'" .
415			" AND IFNULL(gedcom_id, :tree_id_1) = :tree_id_2" .
416			" ORDER BY block_order"
417		)->execute([
418			'module_name' => $this->getName(),
419			'tree_id_1'   => $WT_TREE->getTreeId(),
420			'tree_id_2'   => $WT_TREE->getTreeId(),
421			])->fetchAll();
422
423		$min_block_order = Database::prepare(
424			"SELECT MIN(block_order) FROM `##block` WHERE module_name = 'faq' AND (gedcom_id = :tree_id OR gedcom_id IS NULL)"
425		)->execute([
426			'tree_id' => $WT_TREE->getTreeId(),
427		])->fetchOne();
428
429		$max_block_order = Database::prepare(
430			"SELECT MAX(block_order) FROM `##block` WHERE module_name = 'faq' AND (gedcom_id = :tree_id OR gedcom_id IS NULL)"
431		)->execute([
432			'tree_id' => $WT_TREE->getTreeId(),
433		])->fetchOne();
434
435		?>
436		<ol class="breadcrumb small">
437			<li><a href="admin.php"><?php echo I18N::translate('Control panel'); ?></a></li>
438			<li><a href="admin_modules.php"><?php echo I18N::translate('Module administration'); ?></a></li>
439			<li class="active"><?php echo $controller->getPageTitle(); ?></li>
440		</ol>
441		<h2><?php echo $controller->getPageTitle(); ?></h2>
442		<p>
443			<?= /* 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.') ?>
444			<?= I18N::translate('You may use HTML to format the answer and to add links to other websites.') ?>
445		</p>
446
447		<p>
448			<form class="form form-inline">
449				<label for="ged" class="sr-only">
450					<?= I18N::translate('Family tree') ?>
451				</label>
452				<input type="hidden" name="mod" value="<?=  $this->getName() ?>">
453				<input type="hidden" name="mod_action" value="admin_config">
454				<?php echo FunctionsEdit::selectEditControl('ged', Tree::getNameList(), null, $WT_TREE->getName(), 'class="form-control"'); ?>
455				<input type="submit" class="btn btn-primary" value="<?= I18N::translate('show') ?>">
456			</form>
457		</p>
458
459		<p>
460			<a href="module.php?mod=<?= $this->getName() ?>&amp;mod_action=admin_edit" class="btn btn-default">
461				<i class="fa fa-plus"></i>
462				<?= /* I18N: FAQ = “Frequently Asked Question” */ I18N::translate('Add an FAQ') ?>
463			</a>
464		</p>
465
466		<?php
467		echo '<table class="table table-bordered">';
468		foreach ($faqs as $faq) {
469			// NOTE: Print the position of the current item
470			echo '<tr class="faq_edit_pos"><td>';
471			echo I18N::translate('#%s', $faq->block_order + 1), ' ';
472			if ($faq->gedcom_id === null) {
473				echo I18N::translate('All');
474			} else {
475				echo $WT_TREE->getTitleHtml();
476			}
477			echo '</td>';
478			// NOTE: Print the edit options of the current item
479			echo '<td>';
480			if ($faq->block_order == $min_block_order) {
481				echo '&nbsp;';
482			} else {
483				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>';
484			}
485			echo '</td><td>';
486			if ($faq->block_order == $max_block_order) {
487				echo '&nbsp;';
488			} else {
489				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>';
490			}
491			echo '</td><td>';
492			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>';
493			echo '</td><td>';
494			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>';
495			echo '</td></tr>';
496			// NOTE: Print the title text of the current item
497			echo '<tr><td colspan="5">';
498			echo '<div class="faq_edit_item">';
499			echo '<div class="faq_edit_title">', $faq->header, '</div>';
500			// NOTE: Print the body text of the current item
501			echo '<div class="faq_edit_content">', substr($faq->faqbody, 0, 1) == '<' ? $faq->faqbody : nl2br($faq->faqbody, false), '</div></div></td></tr>';
502		}
503		echo '</table>';
504	}
505
506	/**
507	 * The user can re-order menus. Until they do, they are shown in this order.
508	 *
509	 * @return int
510	 */
511	public function defaultMenuOrder() {
512		return 40;
513	}
514
515	/**
516	 * A menu, to be added to the main application menu.
517	 *
518	 * @return Menu|null
519	 */
520	public function getMenu() {
521		global $WT_TREE;
522
523		$faqs = Database::prepare(
524			"SELECT block_id FROM `##block`" .
525			" JOIN `##block_setting` USING (block_id)" .
526			" WHERE module_name = :module_name AND IFNULL(gedcom_id, :tree_id_1) = :tree_id_2" .
527			" AND setting_name='languages' AND (setting_value LIKE CONCAT('%', :locale, '%') OR setting_value='')"
528		)->execute([
529			'module_name' => $this->getName(),
530			'tree_id_1'   => $WT_TREE->getTreeId(),
531			'tree_id_2'   => $WT_TREE->getTreeId(),
532			'locale'      => WT_LOCALE,
533		])->fetchAll();
534
535		if ($faqs) {
536			return new Menu($this->getTitle(), 'module.php?mod=faq&amp;mod_action=show', 'menu-help');
537		} else {
538			return null;
539		}
540
541	}
542}
543