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