xref: /webtrees/app/Module/FrequentlyAskedQuestionsModule.php (revision 3976b4703df669696105ed6b024b96d433c8fbdb)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Module;
20
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Menu;
23use Fisharebest\Webtrees\Services\HtmlService;
24use Fisharebest\Webtrees\Tree;
25use Illuminate\Database\Capsule\Manager as DB;
26use Illuminate\Database\Query\Builder;
27use Illuminate\Support\Collection;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30use stdClass;
31
32/**
33 * Class FrequentlyAskedQuestionsModule
34 */
35class FrequentlyAskedQuestionsModule extends AbstractModule implements ModuleConfigInterface, ModuleMenuInterface
36{
37    use ModuleConfigTrait;
38    use ModuleMenuTrait;
39
40    /** @var HtmlService */
41    private $html_service;
42
43    /**
44     * HtmlBlockModule bootstrap.
45     *
46     * @param HtmlService $html_service
47     */
48    public function boot(HtmlService $html_service)
49    {
50        $this->html_service = $html_service;
51    }
52
53    /**
54     * How should this module be identified in the control panel, etc.?
55     *
56     * @return string
57     */
58    public function title(): string
59    {
60        /* I18N: Name of a module. Abbreviation for “Frequently Asked Questions” */
61        return I18N::translate('FAQ');
62    }
63
64    /**
65     * A sentence describing what this module does.
66     *
67     * @return string
68     */
69    public function description(): string
70    {
71        /* I18N: Description of the “FAQ” module */
72        return I18N::translate('A list of frequently asked questions and answers.');
73    }
74
75    /**
76     * The default position for this menu.  It can be changed in the control panel.
77     *
78     * @return int
79     */
80    public function defaultMenuOrder(): int
81    {
82        return 8;
83    }
84
85    /**
86     * A menu, to be added to the main application menu.
87     *
88     * @param Tree $tree
89     *
90     * @return Menu|null
91     */
92    public function getMenu(Tree $tree): ?Menu
93    {
94        if ($this->faqsExist($tree, WT_LOCALE)) {
95            return new Menu($this->title(), route('module', [
96                'module' => $this->name(),
97                'action' => 'Show',
98                'ged'    => $tree->name(),
99            ]), 'menu-help');
100        }
101
102        return null;
103    }
104
105    /**
106     * @param ServerRequestInterface $request
107     *
108     * @return ResponseInterface
109     */
110    public function getAdminAction(ServerRequestInterface $request): ResponseInterface
111    {
112        $this->layout = 'layouts/administration';
113
114        $tree = $request->getAttribute('tree');
115        $faqs = $this->faqsForTree($tree);
116
117        $min_block_order = DB::table('block')
118            ->where('module_name', '=', $this->name())
119            ->where(static function (Builder $query) use ($tree): void {
120                $query
121                    ->whereNull('gedcom_id')
122                    ->orWhere('gedcom_id', '=', $tree->id());
123            })
124            ->min('block_order');
125
126        $max_block_order = DB::table('block')
127            ->where('module_name', '=', $this->name())
128            ->where(static function (Builder $query) use ($tree): void {
129                $query
130                    ->whereNull('gedcom_id')
131                    ->orWhere('gedcom_id', '=', $tree->id());
132            })
133            ->max('block_order');
134
135        $title = I18N::translate('Frequently asked questions') . ' — ' . $tree->title();
136
137        return $this->viewResponse('modules/faq/config', [
138            'faqs'            => $faqs,
139            'max_block_order' => $max_block_order,
140            'min_block_order' => $min_block_order,
141            'title'           => $title,
142            'tree'            => $tree,
143            'tree_names'      => Tree::getNameList(),
144        ]);
145    }
146
147    /**
148     * @param ServerRequestInterface $request
149     *
150     * @return ResponseInterface
151     */
152    public function postAdminDeleteAction(ServerRequestInterface $request): ResponseInterface
153    {
154        $tree     = $request->getAttribute('tree');
155        $block_id = (int) $request->getQueryParams()['block_id'];
156
157        DB::table('block_setting')->where('block_id', '=', $block_id)->delete();
158
159        DB::table('block')->where('block_id', '=', $block_id)->delete();
160
161        $url = route('module', [
162            'module' => $this->name(),
163            'action' => 'Admin',
164            'ged'    => $tree->name(),
165        ]);
166
167        return redirect($url);
168    }
169
170    /**
171     * @param ServerRequestInterface $request
172     *
173     * @return ResponseInterface
174     */
175    public function postAdminMoveDownAction(ServerRequestInterface $request): ResponseInterface
176    {
177        $tree     = $request->getAttribute('tree');
178        $block_id = (int) $request->getQueryParams()['block_id'];
179
180        $block_order = DB::table('block')
181            ->where('block_id', '=', $block_id)
182            ->value('block_order');
183
184        $swap_block = DB::table('block')
185            ->where('module_name', '=', $this->name())
186            ->where('block_order', '=', function (Builder $query) use ($block_order): void {
187                $query
188                    ->from('block')
189                    ->where('module_name', '=', $this->name())
190                    ->where('block_order', '>', $block_order)
191                    ->min('block_order');
192            })
193            ->select(['block_order', 'block_id'])
194            ->first();
195
196        if ($swap_block !== null) {
197            DB::table('block')
198                ->where('block_id', '=', $block_id)
199                ->update([
200                    'block_order' => $swap_block->block_order,
201                ]);
202
203            DB::table('block')
204                ->where('block_id', '=', $swap_block->block_id)
205                ->update([
206                    'block_order' => $block_order,
207                ]);
208        }
209
210        $url = route('module', [
211            'module' => $this->name(),
212            'action' => 'Admin',
213            'ged'    => $tree->name(),
214        ]);
215
216        return redirect($url);
217    }
218
219    /**
220     * @param ServerRequestInterface $request
221     *
222     * @return ResponseInterface
223     */
224    public function postAdminMoveUpAction(ServerRequestInterface $request): ResponseInterface
225    {
226        $tree     = $request->getAttribute('tree');
227        $block_id = (int) $request->getQueryParams()['block_id'];
228
229        $block_order = DB::table('block')
230            ->where('block_id', '=', $block_id)
231            ->value('block_order');
232
233        $swap_block = DB::table('block')
234            ->where('module_name', '=', $this->name())
235            ->where('block_order', '=', function (Builder $query) use ($block_order): void {
236                $query
237                    ->from('block')
238                    ->where('module_name', '=', $this->name())
239                    ->where('block_order', '<', $block_order)
240                    ->max('block_order');
241            })
242            ->select(['block_order', 'block_id'])
243            ->first();
244
245        if ($swap_block !== null) {
246            DB::table('block')
247                ->where('block_id', '=', $block_id)
248                ->update([
249                    'block_order' => $swap_block->block_order,
250                ]);
251
252            DB::table('block')
253                ->where('block_id', '=', $swap_block->block_id)
254                ->update([
255                    'block_order' => $block_order,
256                ]);
257        }
258
259        $url = route('module', [
260            'module' => $this->name(),
261            'action' => 'Admin',
262            'ged'    => $tree->name(),
263        ]);
264
265        return redirect($url);
266    }
267
268    /**
269     * @param ServerRequestInterface $request
270     *
271     * @return ResponseInterface
272     */
273    public function getAdminEditAction(ServerRequestInterface $request): ResponseInterface
274    {
275        $this->layout = 'layouts/administration';
276
277        $tree     = $request->getAttribute('tree');
278        $block_id = (int) ($request->getQueryParams()['block_id'] ?? 0);
279
280        if ($block_id === 0) {
281            // Creating a new faq
282            $header  = '';
283            $faqbody = '';
284
285            $block_order = 1 + (int) DB::table('block')
286                    ->where('module_name', '=', $this->name())
287                    ->max('block_order');
288
289            $languages = [];
290
291            $title = I18N::translate('Add an FAQ');
292        } else {
293            // Editing an existing faq
294            $header  = $this->getBlockSetting($block_id, 'header');
295            $faqbody = $this->getBlockSetting($block_id, 'faqbody');
296
297            $block_order = DB::table('block')
298                ->where('block_id', '=', $block_id)
299                ->value('block_order');
300
301            $languages = explode(',', $this->getBlockSetting($block_id, 'languages'));
302
303            $title = I18N::translate('Edit the FAQ');
304        }
305
306        $tree_names = ['' => I18N::translate('All')] + Tree::getIdList();
307
308        return $this->viewResponse('modules/faq/edit', [
309            'block_id'    => $block_id,
310            'block_order' => $block_order,
311            'header'      => $header,
312            'faqbody'     => $faqbody,
313            'languages'   => $languages,
314            'title'       => $title,
315            'tree'        => $tree,
316            'tree_names'  => $tree_names,
317        ]);
318    }
319
320    /**
321     * @param ServerRequestInterface $request
322     *
323     * @return ResponseInterface
324     */
325    public function postAdminEditAction(ServerRequestInterface $request): ResponseInterface
326    {
327        $tree     = $request->getAttribute('tree');
328        $block_id = (int) ($request->getQueryParams()['block_id'] ?? 0);
329
330        $params = $request->getParsedBody();
331
332        $faqbody     = $params['faqbody'];
333        $header      = $params['header'];
334        $languages   = $params['languages'] ?? [];
335        $gedcom_id   = (int) $params['gedcom_id'] ?: null;
336        $block_order = (int) $params['block_order'];
337
338        $faqbody = $this->html_service->sanitize($faqbody);
339        $header  = $this->html_service->sanitize($header);
340
341        if ($block_id !== 0) {
342            DB::table('block')
343                ->where('block_id', '=', $block_id)
344                ->update([
345                    'gedcom_id'   => $gedcom_id,
346                    'block_order' => $block_order,
347                ]);
348        } else {
349            DB::table('block')->insert([
350                'gedcom_id'   => $gedcom_id,
351                'module_name' => $this->name(),
352                'block_order' => $block_order,
353            ]);
354
355            $block_id = (int) DB::connection()->getPdo()->lastInsertId();
356        }
357
358        $this->setBlockSetting($block_id, 'faqbody', $faqbody);
359        $this->setBlockSetting($block_id, 'header', $header);
360        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
361
362        $url = route('module', [
363            'module' => $this->name(),
364            'action' => 'Admin',
365            'ged'    => $tree->name(),
366        ]);
367
368        return redirect($url);
369    }
370
371    /**
372     * @param ServerRequestInterface $request
373     *
374     * @return ResponseInterface
375     */
376    public function getShowAction(ServerRequestInterface $request): ResponseInterface
377    {
378        $tree = $request->getAttribute('tree');
379
380        // Filter foreign languages.
381        $faqs = $this->faqsForTree($tree)
382            ->filter(static function (stdClass $faq): bool {
383                return $faq->languages === '' || in_array(WT_LOCALE, explode(',', $faq->languages), true);
384            });
385
386        return $this->viewResponse('modules/faq/show', [
387            'faqs'  => $faqs,
388            'title' => I18N::translate('Frequently asked questions'),
389            'tree'  => $tree,
390        ]);
391    }
392
393    /**
394     * @param Tree $tree
395     *
396     * @return Collection
397     */
398    private function faqsForTree(Tree $tree): Collection
399    {
400        return DB::table('block')
401            ->join('block_setting AS bs1', 'bs1.block_id', '=', 'block.block_id')
402            ->join('block_setting AS bs2', 'bs2.block_id', '=', 'block.block_id')
403            ->join('block_setting AS bs3', 'bs3.block_id', '=', 'block.block_id')
404            ->where('module_name', '=', $this->name())
405            ->where('bs1.setting_name', '=', 'header')
406            ->where('bs2.setting_name', '=', 'faqbody')
407            ->where('bs3.setting_name', '=', 'languages')
408            ->where(static function (Builder $query) use ($tree): void {
409                $query
410                    ->whereNull('gedcom_id')
411                    ->orWhere('gedcom_id', '=', $tree->id());
412            })
413            ->orderBy('block_order')
414            ->select(['block.block_id', 'block_order', 'gedcom_id', 'bs1.setting_value AS header', 'bs2.setting_value AS faqbody', 'bs3.setting_value AS languages'])
415            ->get();
416    }
417
418    /**
419     * @param Tree   $tree
420     * @param string $language
421     *
422     * @return bool
423     */
424    private function faqsExist(Tree $tree, string $language): bool
425    {
426        return DB::table('block')
427            ->join('block_setting', 'block_setting.block_id', '=', 'block.block_id')
428            ->where('module_name', '=', $this->name())
429            ->where('setting_name', '=', 'languages')
430            ->where(static function (Builder $query) use ($tree): void {
431                $query
432                    ->whereNull('gedcom_id')
433                    ->orWhere('gedcom_id', '=', $tree->id());
434            })
435            ->select(['setting_value AS languages'])
436            ->get()
437            ->filter(static function (stdClass $faq) use ($language): bool {
438                return $faq->languages === '' || in_array($language, explode(',', $faq->languages), true);
439            })
440            ->isNotEmpty();
441    }
442}
443