summaryrefslogtreecommitdiff
path: root/app/Module/FrequentlyAskedQuestionsModule.php
blob: 1e9351fc7bfc7fcd418ba49c2d10b1fcfe94e4ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
/**
 * webtrees: online genealogy
 * Copyright (C) 2018 webtrees development team
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
namespace Fisharebest\Webtrees\Module;

use Fisharebest\Webtrees\Database;
use Fisharebest\Webtrees\Filter;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Menu;
use Fisharebest\Webtrees\Tree;
use stdClass;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * Class FrequentlyAskedQuestionsModule
 */
class FrequentlyAskedQuestionsModule extends AbstractModule implements ModuleMenuInterface, ModuleConfigInterface {
	/** {@inheritdoc} */
	public function getTitle() {
		return /* I18N: Name of a module. Abbreviation for “Frequently Asked Questions” */
			I18N::translate('FAQ');
	}

	/** {@inheritdoc} */
	public function getDescription() {
		return /* I18N: Description of the “FAQ” module */
			I18N::translate('A list of frequently asked questions and answers.');
	}

	/**
	 * The URL to a page where the user can modify the configuration of this module.
	 *
	 * @return string
	 */
	public function getConfigLink() {
		return route('module', ['module' => $this->getName(), 'action' => 'Admin']);
	}

	/**
	 * The user can re-order menus. Until they do, they are shown in this order.
	 *
	 * @return int
	 */
	public function defaultMenuOrder() {
		return 40;
	}

	/**
	 * A menu, to be added to the main application menu.
	 *
	 * @param Tree $tree
	 *
	 * @return Menu|null
	 */
	public function getMenu(Tree $tree) {
		$faqs = Database::prepare(
			"SELECT block_id FROM `##block`" .
			" JOIN `##block_setting` USING (block_id)" .
			" WHERE module_name = :module_name AND IFNULL(gedcom_id, :tree_id_1) = :tree_id_2" .
			" AND setting_name='languages' AND (setting_value LIKE CONCAT('%', :locale, '%') OR setting_value='')"
		)->execute([
			'module_name' => $this->getName(),
			'tree_id_1'   => $tree->getTreeId(),
			'tree_id_2'   => $tree->getTreeId(),
			'locale'      => WT_LOCALE,
		])->fetchAll();

		if ($faqs) {
			return new Menu($this->getTitle(), route('module', ['module' => 'faq', 'action' => 'Show', 'ged' => $tree->getName()]), 'menu-help');
		} else {
			return null;
		}
	}

	/**
	 * @param Request $request
	 *
	 * @return Response
	 */
	public function getAdminAction(Request $request): Response {
		/** @var Tree $tree */
		$tree = $request->attributes->get('tree');

		$this->layout = 'layouts/administration';

		$faqs = Database::prepare(
			"SELECT block_id, block_order, gedcom_id, bs1.setting_value AS header, bs2.setting_value AS faqbody" .
			" FROM `##block` b" .
			" JOIN `##block_setting` bs1 USING (block_id)" .
			" JOIN `##block_setting` bs2 USING (block_id)" .
			" WHERE module_name = :module_name" .
			" AND bs1.setting_name = 'header'" .
			" AND bs2.setting_name = 'faqbody'" .
			" AND IFNULL(gedcom_id, :tree_id_1) = :tree_id_2" .
			" ORDER BY block_order"
		)->execute([
			'module_name' => $this->getName(),
			'tree_id_1'   => $tree->getTreeId(),
			'tree_id_2'   => $tree->getTreeId(),
		])->fetchAll();

		$min_block_order = Database::prepare(
			"SELECT MIN(block_order) FROM `##block` WHERE module_name = 'faq' AND (gedcom_id = :tree_id OR gedcom_id IS NULL)"
		)->execute([
			'tree_id' => $tree->getTreeId(),
		])->fetchOne();

		$max_block_order = Database::prepare(
			"SELECT MAX(block_order) FROM `##block` WHERE module_name = 'faq' AND (gedcom_id = :tree_id OR gedcom_id IS NULL)"
		)->execute([
			'tree_id' => $tree->getTreeId(),
		])->fetchOne();

		$title = I18N::translate('Frequently asked questions') . ' — ' . $tree->getTitle();

		return $this->viewResponse('modules/faq/config', [
			'faqs'            => $faqs,
			'max_block_order' => $max_block_order,
			'min_block_order' => $min_block_order,
			'title'           => $title,
			'tree'            => $tree,
			'tree_names'      => Tree::getNameList(),
		]);
	}

	/**
	 * @param Request $request
	 *
	 * @return RedirectResponse
	 */
	public function postAdminDeleteAction(Request $request): RedirectResponse {
		/** @var Tree $tree */
		$tree = $request->attributes->get('tree');

		$block_id = (int) $request->get('block_id');

		Database::prepare(
			"DELETE FROM `##block_setting` WHERE block_id = :block_id"
		)->execute(['block_id' => $block_id]);

		Database::prepare(
			"DELETE FROM `##block` WHERE block_id = :block_id"
		)->execute(['block_id' => $block_id]);


		$url = route('module', ['module' => 'faq', 'action' => 'Admin', 'ged' => $tree->getName()]);

		return new RedirectResponse($url);
	}

	/**
	 * @param Request $request
	 *
	 * @return RedirectResponse
	 */
	public function postAdminMoveDownAction(Request $request): RedirectResponse {
		/** @var Tree $tree */
		$tree = $request->attributes->get('tree');

		$block_id = (int) $request->get('block_id');

		$block_order = Database::prepare(
			"SELECT block_order FROM `##block` WHERE block_id = :block_id"
		)->execute([
			'block_id' => $block_id,
		])->fetchOne();

		$swap_block = Database::prepare(
			"SELECT block_order, block_id" .
			" FROM `##block`" .
			" WHERE block_order=(" .
			"  SELECT MIN(block_order) FROM `##block` WHERE block_order > :block_order AND module_name = :module_name_1" .
			" ) AND module_name = :module_name_2" .
			" LIMIT 1"
		)->execute([
			'block_order'   => $block_order,
			'module_name_1' => $this->getName(),
			'module_name_2' => $this->getName(),
		])->fetchOneRow();

		if ($swap_block !== null) {
			Database::prepare(
				"UPDATE `##block` SET block_order = :block_order WHERE block_id = :block_id"
			)->execute([
				'block_order' => $swap_block->block_order,
				'block_id'    => $block_id,
			]);
			Database::prepare(
				"UPDATE `##block` SET block_order = :block_order WHERE block_id = :block_id"
			)->execute([
				'block_order' => $block_order,
				'block_id'    => $swap_block->block_id,
			]);
		}

		$url = route('module', ['module' => 'faq', 'action' => 'Admin', 'ged' => $tree->getName()]);

		return new RedirectResponse($url);
	}

	/**
	 * @param Request $request
	 *
	 * @return RedirectResponse
	 */
	public function postAdminMoveUpAction(Request $request): RedirectResponse {
		/** @var Tree $tree */
		$tree = $request->attributes->get('tree');

		$block_id = (int) $request->get('block_id');

		$block_order = Database::prepare(
			"SELECT block_order FROM `##block` WHERE block_id = :block_id"
		)->execute([
			'block_id' => $block_id,
			])->fetchOne();

		$swap_block = Database::prepare(
			"SELECT block_order, block_id" .
			" FROM `##block`" .
			" WHERE block_order = (" .
			"  SELECT MAX(block_order) FROM `##block` WHERE block_order < :block_order AND module_name = :module_name_1" .
			" ) AND module_name = :module_name_2" .
			" LIMIT 1"
		)->execute([
			'block_order'   => $block_order,
			'module_name_1' => $this->getName(),
			'module_name_2' => $this->getName(),
		])->fetchOneRow();

		if ($swap_block !== null) {
			Database::prepare(
				"UPDATE `##block` SET block_order = :block_order WHERE block_id = :block_id"
			)->execute([
				'block_order' => $swap_block->block_order,
				'block_id'    => $block_id,
			]);
			Database::prepare(
				"UPDATE `##block` SET block_order = :block_order WHERE block_id = :block_id"
			)->execute([
				'block_order' => $block_order,
				'block_id'    => $swap_block->block_id,
			]);
		}

		$url = route('module', ['module' => 'faq', 'action' => 'Admin', 'ged' => $tree->getName()]);

		return new RedirectResponse($url);
	}

	/**
	 * @param Request $request
	 *
	 * @return Response
	 */
	public function getAdminEditAction(Request $request): Response {
		/** @var Tree $tree */
		$tree = $request->attributes->get('tree');

		$this->layout = 'layouts/administration';

		$block_id = (int) $request->get('block_id');

		if ($block_id === 0) {
			// Creating a new faq
			$header      = '';
			$faqbody     = '';
			$block_order = Database::prepare(
				"SELECT IFNULL(MAX(block_order)+1, 0) FROM `##block` WHERE module_name = :module_name"
			)->execute([
				'module_name' => $this->getName(),
			])->fetchOne();
			$languages = [];

			$title = I18N::translate('Add an FAQ');
		} else {
			// Editing an existing faq
			$header      = $this->getBlockSetting($block_id, 'header');
			$faqbody     = $this->getBlockSetting($block_id, 'faqbody');
			$block_order = Database::prepare(
				"SELECT block_order FROM `##block` WHERE block_id = :block_id"
			)->execute(['block_id' => $block_id])->fetchOne();
			$languages = explode(',', $this->getBlockSetting($block_id, 'languages'));

			$title = I18N::translate('Edit the FAQ');
		}

		// @TODO enable CKEDITOR

		return $this->viewResponse('modules/faq/edit', [
			'block_id'    => $block_id,
			'block_order' => $block_order,
			'header'      => $header,
			'faqbody'     => $faqbody,
			'languages'   => $languages,
			'title'       => $title,
			'tree'        => $tree,
			'tree_names' => Tree::getNameList(),
		]);
	}

	/**
	 * @param Request $request
	 *
	 * @return RedirectResponse
	 */
	public function postAdminEditAction(Request $request): RedirectResponse {
		/** @var Tree $tree */
		$tree = $request->attributes->get('tree');

		$block_id  = (int) $request->get('block_id');
		$faqbody   = $request->get('faqbody', '');
		$header    = $request->get('header', '');
		$languages = $request->get('languages', []);

		if ($block_id !== 0) {
			Database::prepare(
				"UPDATE `##block` SET gedcom_id = NULLIF(:tree_id, '0'), block_order = :block_order WHERE block_id = :block_id"
			)->execute([
				'tree_id'     => Filter::postInteger('gedcom_id'),
				'block_order' => Filter::postInteger('block_order'),
				'block_id'    => $block_id,
			]);
		} else {
			Database::prepare(
				"INSERT INTO `##block` (gedcom_id, module_name, block_order) VALUES (NULLIF(:tree_id, '0'), :module_name, :block_order)"
			)->execute([
				'tree_id'     => Filter::postInteger('gedcom_id'),
				'module_name' => $this->getName(),
				'block_order' => Filter::postInteger('block_order'),
			]);

			$block_id = Database::getInstance()->lastInsertId();
		}

		$this->setBlockSetting($block_id, 'faqbody', $faqbody);
		$this->setBlockSetting($block_id, 'header', $header);
		$this->setBlockSetting($block_id, 'languages', implode(',', $languages));

		$url = route('module', ['module' => 'faq', 'action' => 'Admin', 'ged' => $tree->getName()]);

		return new RedirectResponse($url);
	}

	/**
	 * @param Request $request
	 *
	 * @return Response
	 */
	public function getShowAction(Request $request): Response {
		/** @var Tree $tree */
		$tree = $request->attributes->get('tree');

		$faqs = Database::prepare(
			"SELECT block_id, bs1.setting_value AS header, bs2.setting_value AS body, bs3.setting_value AS languages" .
			" FROM `##block` b" .
			" JOIN `##block_setting` bs1 USING (block_id)" .
			" JOIN `##block_setting` bs2 USING (block_id)" .
			" JOIN `##block_setting` bs3 USING (block_id)" .
			" WHERE module_name = :module_name" .
			" AND bs1.setting_name = 'header'" .
			" AND bs2.setting_name = 'faqbody'" .
			" AND bs3.setting_name = 'languages'" .
			" AND IFNULL(gedcom_id, :tree_id_1) = :tree_id_2" .
			" ORDER BY block_order"
		)->execute([
			'module_name' => $this->getName(),
			'tree_id_1'   => $tree->getTreeId(),
			'tree_id_2'   => $tree->getTreeId(),
		])->fetchAll();

		// Filter foreign languages.
		$faqs = array_filter($faqs, function (stdClass $faq) {
			return $faq->languages === '' || in_array(WT_LOCALE, explode(',', $faq->languages));
		});

		return $this->viewResponse('modules/faq/show', [
			'faqs' => $faqs,
			'title' => I18N::translate('Frequently asked questions'),
			'tree' => $tree,
		]);
	}
}