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