xref: /webtrees/app/Module/FrequentlyAskedQuestionsModule.php (revision a45f98897789fc9ff88705eb09ae5f037bf49c10)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Menu;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
228de50a4eSGreg Roachuse stdClass;
23aee13b6dSGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
24aee13b6dSGreg Roachuse Symfony\Component\HttpFoundation\Request;
25aee13b6dSGreg Roachuse Symfony\Component\HttpFoundation\Response;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class FrequentlyAskedQuestionsModule
298c2e8227SGreg Roach */
30c1010edaSGreg Roachclass FrequentlyAskedQuestionsModule extends AbstractModule implements ModuleMenuInterface, ModuleConfigInterface
31c1010edaSGreg Roach{
328c2e8227SGreg Roach    /** {@inheritdoc} */
33c1010edaSGreg Roach    public function getTitle()
34c1010edaSGreg Roach    {
35bbb76c12SGreg Roach        /* I18N: Name of a module. Abbreviation for “Frequently Asked Questions” */
36bbb76c12SGreg Roach        return I18N::translate('FAQ');
378c2e8227SGreg Roach    }
388c2e8227SGreg Roach
398c2e8227SGreg Roach    /** {@inheritdoc} */
40c1010edaSGreg Roach    public function getDescription()
41c1010edaSGreg Roach    {
42bbb76c12SGreg Roach        /* I18N: Description of the “FAQ” module */
43bbb76c12SGreg Roach        return I18N::translate('A list of frequently asked questions and answers.');
448c2e8227SGreg Roach    }
458c2e8227SGreg Roach
4676692c8bSGreg Roach    /**
47aee13b6dSGreg Roach     * The URL to a page where the user can modify the configuration of this module.
4876692c8bSGreg Roach     *
49aee13b6dSGreg Roach     * @return string
5076692c8bSGreg Roach     */
51c1010edaSGreg Roach    public function getConfigLink()
52c1010edaSGreg Roach    {
53c1010edaSGreg Roach        return route('module', [
54c1010edaSGreg Roach            'module' => $this->getName(),
55c1010edaSGreg Roach            'action' => 'Admin',
56c1010edaSGreg Roach        ]);
578c2e8227SGreg Roach    }
588c2e8227SGreg Roach
598c2e8227SGreg Roach    /**
600ee13198SGreg Roach     * The user can re-order menus. Until they do, they are shown in this order.
610ee13198SGreg Roach     *
620ee13198SGreg Roach     * @return int
630ee13198SGreg Roach     */
64c1010edaSGreg Roach    public function defaultMenuOrder()
65c1010edaSGreg Roach    {
668c2e8227SGreg Roach        return 40;
678c2e8227SGreg Roach    }
688c2e8227SGreg Roach
690ee13198SGreg Roach    /**
700ee13198SGreg Roach     * A menu, to be added to the main application menu.
710ee13198SGreg Roach     *
72aee13b6dSGreg Roach     * @param Tree $tree
73aee13b6dSGreg Roach     *
740ee13198SGreg Roach     * @return Menu|null
750ee13198SGreg Roach     */
76c1010edaSGreg Roach    public function getMenu(Tree $tree)
77c1010edaSGreg Roach    {
788c2e8227SGreg Roach        $faqs = Database::prepare(
792b6675d7SGreg Roach            "SELECT block_id FROM `##block`" .
802b6675d7SGreg Roach            " JOIN `##block_setting` USING (block_id)" .
812b6675d7SGreg Roach            " WHERE module_name = :module_name AND IFNULL(gedcom_id, :tree_id_1) = :tree_id_2" .
822b6675d7SGreg Roach            " AND setting_name='languages' AND (setting_value LIKE CONCAT('%', :locale, '%') OR setting_value='')"
8313abd6f3SGreg Roach        )->execute([
848c2e8227SGreg Roach            'module_name' => $this->getName(),
85aee13b6dSGreg Roach            'tree_id_1'   => $tree->getTreeId(),
86aee13b6dSGreg Roach            'tree_id_2'   => $tree->getTreeId(),
872b6675d7SGreg Roach            'locale'      => WT_LOCALE,
8813abd6f3SGreg Roach        ])->fetchAll();
898c2e8227SGreg Roach
9038a9583bSGreg Roach        if ($faqs) {
91c1010edaSGreg Roach            return new Menu($this->getTitle(), route('module', [
92c1010edaSGreg Roach                'module' => 'faq',
93c1010edaSGreg Roach                'action' => 'Show',
94c1010edaSGreg Roach                'ged'    => $tree->getName(),
95c1010edaSGreg Roach            ]), 'menu-help');
9638a9583bSGreg Roach        } else {
978c2e8227SGreg Roach            return null;
988c2e8227SGreg Roach        }
998c2e8227SGreg Roach    }
100aee13b6dSGreg Roach
101aee13b6dSGreg Roach    /**
102b6db7c1fSGreg Roach     * @param Tree $tree
103aee13b6dSGreg Roach     *
104aee13b6dSGreg Roach     * @return Response
105aee13b6dSGreg Roach     */
106b6db7c1fSGreg Roach    public function getAdminAction(Tree $tree): Response
107c1010edaSGreg Roach    {
108aee13b6dSGreg Roach        $this->layout = 'layouts/administration';
109aee13b6dSGreg Roach
110aee13b6dSGreg Roach        $faqs = Database::prepare(
111aee13b6dSGreg Roach            "SELECT block_id, block_order, gedcom_id, bs1.setting_value AS header, bs2.setting_value AS faqbody" .
112aee13b6dSGreg Roach            " FROM `##block` b" .
113aee13b6dSGreg Roach            " JOIN `##block_setting` bs1 USING (block_id)" .
114aee13b6dSGreg Roach            " JOIN `##block_setting` bs2 USING (block_id)" .
115aee13b6dSGreg Roach            " WHERE module_name = :module_name" .
116aee13b6dSGreg Roach            " AND bs1.setting_name = 'header'" .
117aee13b6dSGreg Roach            " AND bs2.setting_name = 'faqbody'" .
118aee13b6dSGreg Roach            " AND IFNULL(gedcom_id, :tree_id_1) = :tree_id_2" .
119aee13b6dSGreg Roach            " ORDER BY block_order"
120aee13b6dSGreg Roach        )->execute([
121aee13b6dSGreg Roach            'module_name' => $this->getName(),
122aee13b6dSGreg Roach            'tree_id_1'   => $tree->getTreeId(),
123aee13b6dSGreg Roach            'tree_id_2'   => $tree->getTreeId(),
124aee13b6dSGreg Roach        ])->fetchAll();
125aee13b6dSGreg Roach
126aee13b6dSGreg Roach        $min_block_order = Database::prepare(
127aee13b6dSGreg Roach            "SELECT MIN(block_order) FROM `##block` WHERE module_name = 'faq' AND (gedcom_id = :tree_id OR gedcom_id IS NULL)"
128aee13b6dSGreg Roach        )->execute([
129aee13b6dSGreg Roach            'tree_id' => $tree->getTreeId(),
130aee13b6dSGreg Roach        ])->fetchOne();
131aee13b6dSGreg Roach
132aee13b6dSGreg Roach        $max_block_order = Database::prepare(
133aee13b6dSGreg Roach            "SELECT MAX(block_order) FROM `##block` WHERE module_name = 'faq' AND (gedcom_id = :tree_id OR gedcom_id IS NULL)"
134aee13b6dSGreg Roach        )->execute([
135aee13b6dSGreg Roach            'tree_id' => $tree->getTreeId(),
136aee13b6dSGreg Roach        ])->fetchOne();
137aee13b6dSGreg Roach
138aee13b6dSGreg Roach        $title = I18N::translate('Frequently asked questions') . ' — ' . $tree->getTitle();
139aee13b6dSGreg Roach
140aee13b6dSGreg Roach        return $this->viewResponse('modules/faq/config', [
141aee13b6dSGreg Roach            'faqs'            => $faqs,
142aee13b6dSGreg Roach            'max_block_order' => $max_block_order,
143aee13b6dSGreg Roach            'min_block_order' => $min_block_order,
144aee13b6dSGreg Roach            'title'           => $title,
145aee13b6dSGreg Roach            'tree'            => $tree,
146aee13b6dSGreg Roach            'tree_names'      => Tree::getNameList(),
147aee13b6dSGreg Roach        ]);
148aee13b6dSGreg Roach    }
149aee13b6dSGreg Roach
150aee13b6dSGreg Roach    /**
151aee13b6dSGreg Roach     * @param Request $request
152b6db7c1fSGreg Roach     * @param Tree    $tree
153aee13b6dSGreg Roach     *
154aee13b6dSGreg Roach     * @return RedirectResponse
155aee13b6dSGreg Roach     */
156b6db7c1fSGreg Roach    public function postAdminDeleteAction(Request $request, Tree $tree): RedirectResponse
157c1010edaSGreg Roach    {
158aee13b6dSGreg Roach        $block_id = (int)$request->get('block_id');
159aee13b6dSGreg Roach
160aee13b6dSGreg Roach        Database::prepare(
161aee13b6dSGreg Roach            "DELETE FROM `##block_setting` WHERE block_id = :block_id"
162aee13b6dSGreg Roach        )->execute(['block_id' => $block_id]);
163aee13b6dSGreg Roach
164aee13b6dSGreg Roach        Database::prepare(
165aee13b6dSGreg Roach            "DELETE FROM `##block` WHERE block_id = :block_id"
166aee13b6dSGreg Roach        )->execute(['block_id' => $block_id]);
167aee13b6dSGreg Roach
168aee13b6dSGreg Roach
169c1010edaSGreg Roach        $url = route('module', [
170c1010edaSGreg Roach            'module' => 'faq',
171c1010edaSGreg Roach            'action' => 'Admin',
172c1010edaSGreg Roach            'ged'    => $tree->getName(),
173c1010edaSGreg Roach        ]);
174aee13b6dSGreg Roach
175aee13b6dSGreg Roach        return new RedirectResponse($url);
176aee13b6dSGreg Roach    }
177aee13b6dSGreg Roach
178aee13b6dSGreg Roach    /**
179aee13b6dSGreg Roach     * @param Request $request
180b6db7c1fSGreg Roach     * @param Tree    $tree
181aee13b6dSGreg Roach     *
182aee13b6dSGreg Roach     * @return RedirectResponse
183aee13b6dSGreg Roach     */
184b6db7c1fSGreg Roach    public function postAdminMoveDownAction(Request $request, Tree $tree): RedirectResponse
185c1010edaSGreg Roach    {
186aee13b6dSGreg Roach        $block_id = (int)$request->get('block_id');
187aee13b6dSGreg Roach
188aee13b6dSGreg Roach        $block_order = Database::prepare(
189aee13b6dSGreg Roach            "SELECT block_order FROM `##block` WHERE block_id = :block_id"
190aee13b6dSGreg Roach        )->execute([
191aee13b6dSGreg Roach            'block_id' => $block_id,
192aee13b6dSGreg Roach        ])->fetchOne();
193aee13b6dSGreg Roach
194aee13b6dSGreg Roach        $swap_block = Database::prepare(
195aee13b6dSGreg Roach            "SELECT block_order, block_id" .
196aee13b6dSGreg Roach            " FROM `##block`" .
197aee13b6dSGreg Roach            " WHERE block_order=(" .
198aee13b6dSGreg Roach            "  SELECT MIN(block_order) FROM `##block` WHERE block_order > :block_order AND module_name = :module_name_1" .
199aee13b6dSGreg Roach            " ) AND module_name = :module_name_2" .
200aee13b6dSGreg Roach            " LIMIT 1"
201aee13b6dSGreg Roach        )->execute([
202aee13b6dSGreg Roach            'block_order'   => $block_order,
203aee13b6dSGreg Roach            'module_name_1' => $this->getName(),
204aee13b6dSGreg Roach            'module_name_2' => $this->getName(),
205aee13b6dSGreg Roach        ])->fetchOneRow();
206aee13b6dSGreg Roach
207aee13b6dSGreg Roach        if ($swap_block !== null) {
208aee13b6dSGreg Roach            Database::prepare(
209aee13b6dSGreg Roach                "UPDATE `##block` SET block_order = :block_order WHERE block_id = :block_id"
210aee13b6dSGreg Roach            )->execute([
211aee13b6dSGreg Roach                'block_order' => $swap_block->block_order,
212aee13b6dSGreg Roach                'block_id'    => $block_id,
213aee13b6dSGreg Roach            ]);
214aee13b6dSGreg Roach            Database::prepare(
215aee13b6dSGreg Roach                "UPDATE `##block` SET block_order = :block_order WHERE block_id = :block_id"
216aee13b6dSGreg Roach            )->execute([
217aee13b6dSGreg Roach                'block_order' => $block_order,
218aee13b6dSGreg Roach                'block_id'    => $swap_block->block_id,
219aee13b6dSGreg Roach            ]);
220aee13b6dSGreg Roach        }
221aee13b6dSGreg Roach
222c1010edaSGreg Roach        $url = route('module', [
223c1010edaSGreg Roach            'module' => 'faq',
224c1010edaSGreg Roach            'action' => 'Admin',
225c1010edaSGreg Roach            'ged'    => $tree->getName(),
226c1010edaSGreg Roach        ]);
227aee13b6dSGreg Roach
228aee13b6dSGreg Roach        return new RedirectResponse($url);
229aee13b6dSGreg Roach    }
230aee13b6dSGreg Roach
231aee13b6dSGreg Roach    /**
232aee13b6dSGreg Roach     * @param Request $request
233b6db7c1fSGreg Roach     * @param Tree    $tree
234aee13b6dSGreg Roach     *
235aee13b6dSGreg Roach     * @return RedirectResponse
236aee13b6dSGreg Roach     */
237b6db7c1fSGreg Roach    public function postAdminMoveUpAction(Request $request, Tree $tree): RedirectResponse
238c1010edaSGreg Roach    {
239aee13b6dSGreg Roach        $block_id = (int)$request->get('block_id');
240aee13b6dSGreg Roach
241aee13b6dSGreg Roach        $block_order = Database::prepare(
242aee13b6dSGreg Roach            "SELECT block_order FROM `##block` WHERE block_id = :block_id"
243aee13b6dSGreg Roach        )->execute([
244aee13b6dSGreg Roach            'block_id' => $block_id,
245aee13b6dSGreg Roach        ])->fetchOne();
246aee13b6dSGreg Roach
247aee13b6dSGreg Roach        $swap_block = Database::prepare(
248aee13b6dSGreg Roach            "SELECT block_order, block_id" .
249aee13b6dSGreg Roach            " FROM `##block`" .
250aee13b6dSGreg Roach            " WHERE block_order = (" .
251aee13b6dSGreg Roach            "  SELECT MAX(block_order) FROM `##block` WHERE block_order < :block_order AND module_name = :module_name_1" .
252aee13b6dSGreg Roach            " ) AND module_name = :module_name_2" .
253aee13b6dSGreg Roach            " LIMIT 1"
254aee13b6dSGreg Roach        )->execute([
255aee13b6dSGreg Roach            'block_order'   => $block_order,
256aee13b6dSGreg Roach            'module_name_1' => $this->getName(),
257aee13b6dSGreg Roach            'module_name_2' => $this->getName(),
258aee13b6dSGreg Roach        ])->fetchOneRow();
259aee13b6dSGreg Roach
260aee13b6dSGreg Roach        if ($swap_block !== null) {
261aee13b6dSGreg Roach            Database::prepare(
262aee13b6dSGreg Roach                "UPDATE `##block` SET block_order = :block_order WHERE block_id = :block_id"
263aee13b6dSGreg Roach            )->execute([
264aee13b6dSGreg Roach                'block_order' => $swap_block->block_order,
265aee13b6dSGreg Roach                'block_id'    => $block_id,
266aee13b6dSGreg Roach            ]);
267aee13b6dSGreg Roach            Database::prepare(
268aee13b6dSGreg Roach                "UPDATE `##block` SET block_order = :block_order WHERE block_id = :block_id"
269aee13b6dSGreg Roach            )->execute([
270aee13b6dSGreg Roach                'block_order' => $block_order,
271aee13b6dSGreg Roach                'block_id'    => $swap_block->block_id,
272aee13b6dSGreg Roach            ]);
273aee13b6dSGreg Roach        }
274aee13b6dSGreg Roach
275c1010edaSGreg Roach        $url = route('module', [
276c1010edaSGreg Roach            'module' => 'faq',
277c1010edaSGreg Roach            'action' => 'Admin',
278c1010edaSGreg Roach            'ged'    => $tree->getName(),
279c1010edaSGreg Roach        ]);
280aee13b6dSGreg Roach
281aee13b6dSGreg Roach        return new RedirectResponse($url);
282aee13b6dSGreg Roach    }
283aee13b6dSGreg Roach
284aee13b6dSGreg Roach    /**
285aee13b6dSGreg Roach     * @param Request $request
286b6db7c1fSGreg Roach     * @param Tree    $tree
287aee13b6dSGreg Roach     *
288aee13b6dSGreg Roach     * @return Response
289aee13b6dSGreg Roach     */
290b6db7c1fSGreg Roach    public function getAdminEditAction(Request $request, Tree $tree): Response
291c1010edaSGreg Roach    {
292aee13b6dSGreg Roach        $this->layout = 'layouts/administration';
293aee13b6dSGreg Roach
294aee13b6dSGreg Roach        $block_id = (int)$request->get('block_id');
295aee13b6dSGreg Roach
296aee13b6dSGreg Roach        if ($block_id === 0) {
297aee13b6dSGreg Roach            // Creating a new faq
298aee13b6dSGreg Roach            $header      = '';
299aee13b6dSGreg Roach            $faqbody     = '';
300aee13b6dSGreg Roach            $block_order = Database::prepare(
301aee13b6dSGreg Roach                "SELECT IFNULL(MAX(block_order)+1, 0) FROM `##block` WHERE module_name = :module_name"
302aee13b6dSGreg Roach            )->execute([
303aee13b6dSGreg Roach                'module_name' => $this->getName(),
304aee13b6dSGreg Roach            ])->fetchOne();
305aee13b6dSGreg Roach            $languages   = [];
306aee13b6dSGreg Roach
307aee13b6dSGreg Roach            $title = I18N::translate('Add an FAQ');
308aee13b6dSGreg Roach        } else {
309aee13b6dSGreg Roach            // Editing an existing faq
310aee13b6dSGreg Roach            $header      = $this->getBlockSetting($block_id, 'header');
311aee13b6dSGreg Roach            $faqbody     = $this->getBlockSetting($block_id, 'faqbody');
312aee13b6dSGreg Roach            $block_order = Database::prepare(
313aee13b6dSGreg Roach                "SELECT block_order FROM `##block` WHERE block_id = :block_id"
314aee13b6dSGreg Roach            )->execute(['block_id' => $block_id])->fetchOne();
315aee13b6dSGreg Roach            $languages   = explode(',', $this->getBlockSetting($block_id, 'languages'));
316aee13b6dSGreg Roach
317aee13b6dSGreg Roach            $title = I18N::translate('Edit the FAQ');
318aee13b6dSGreg Roach        }
319aee13b6dSGreg Roach
320aee13b6dSGreg Roach        // @TODO enable CKEDITOR
321aee13b6dSGreg Roach
322aee13b6dSGreg Roach        return $this->viewResponse('modules/faq/edit', [
323aee13b6dSGreg Roach            'block_id'    => $block_id,
324aee13b6dSGreg Roach            'block_order' => $block_order,
325aee13b6dSGreg Roach            'header'      => $header,
326aee13b6dSGreg Roach            'faqbody'     => $faqbody,
327aee13b6dSGreg Roach            'languages'   => $languages,
328aee13b6dSGreg Roach            'title'       => $title,
329aee13b6dSGreg Roach            'tree'        => $tree,
330aee13b6dSGreg Roach            'tree_names'  => Tree::getNameList(),
331aee13b6dSGreg Roach        ]);
332aee13b6dSGreg Roach    }
333aee13b6dSGreg Roach
334aee13b6dSGreg Roach    /**
335aee13b6dSGreg Roach     * @param Request $request
336b6db7c1fSGreg Roach     * @param Tree    $tree
337aee13b6dSGreg Roach     *
338aee13b6dSGreg Roach     * @return RedirectResponse
339aee13b6dSGreg Roach     */
340b6db7c1fSGreg Roach    public function postAdminEditAction(Request $request, Tree $tree): RedirectResponse
341c1010edaSGreg Roach    {
342aee13b6dSGreg Roach        $block_id  = (int)$request->get('block_id');
343aee13b6dSGreg Roach        $faqbody   = $request->get('faqbody', '');
344aee13b6dSGreg Roach        $header    = $request->get('header', '');
345aee13b6dSGreg Roach        $languages = $request->get('languages', []);
346aee13b6dSGreg Roach
347aee13b6dSGreg Roach        if ($block_id !== 0) {
348aee13b6dSGreg Roach            Database::prepare(
349aee13b6dSGreg Roach                "UPDATE `##block` SET gedcom_id = NULLIF(:tree_id, '0'), block_order = :block_order WHERE block_id = :block_id"
350aee13b6dSGreg Roach            )->execute([
351*a45f9889SGreg Roach                'tree_id'     => (int) $request->get('gedcom_id'),
352*a45f9889SGreg Roach                'block_order' => (int) $request->get('block_order'),
353aee13b6dSGreg Roach                'block_id'    => $block_id,
354aee13b6dSGreg Roach            ]);
355aee13b6dSGreg Roach        } else {
356aee13b6dSGreg Roach            Database::prepare(
357aee13b6dSGreg Roach                "INSERT INTO `##block` (gedcom_id, module_name, block_order) VALUES (NULLIF(:tree_id, '0'), :module_name, :block_order)"
358aee13b6dSGreg Roach            )->execute([
359*a45f9889SGreg Roach                'tree_id'     => (int) $request->get('gedcom_id'),
360aee13b6dSGreg Roach                'module_name' => $this->getName(),
361*a45f9889SGreg Roach                'block_order' => (int) $request->get('block_order'),
362aee13b6dSGreg Roach            ]);
363aee13b6dSGreg Roach
364aee13b6dSGreg Roach            $block_id = Database::getInstance()->lastInsertId();
365aee13b6dSGreg Roach        }
366aee13b6dSGreg Roach
367aee13b6dSGreg Roach        $this->setBlockSetting($block_id, 'faqbody', $faqbody);
368aee13b6dSGreg Roach        $this->setBlockSetting($block_id, 'header', $header);
369aee13b6dSGreg Roach        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
370aee13b6dSGreg Roach
371c1010edaSGreg Roach        $url = route('module', [
372c1010edaSGreg Roach            'module' => 'faq',
373c1010edaSGreg Roach            'action' => 'Admin',
374c1010edaSGreg Roach            'ged'    => $tree->getName(),
375c1010edaSGreg Roach        ]);
376aee13b6dSGreg Roach
377aee13b6dSGreg Roach        return new RedirectResponse($url);
378aee13b6dSGreg Roach    }
379aee13b6dSGreg Roach
380b998dbceSGreg Roach    /**
381b998dbceSGreg Roach     * @param Request $request
382b6db7c1fSGreg Roach     * @param Tree    $tree
383b998dbceSGreg Roach     *
384b998dbceSGreg Roach     * @return Response
385b998dbceSGreg Roach     */
386b6db7c1fSGreg Roach    public function getShowAction(Request $request, Tree $tree): Response
387c1010edaSGreg Roach    {
388aee13b6dSGreg Roach        $faqs = Database::prepare(
389aee13b6dSGreg Roach            "SELECT block_id, bs1.setting_value AS header, bs2.setting_value AS body, bs3.setting_value AS languages" .
390aee13b6dSGreg Roach            " FROM `##block` b" .
391aee13b6dSGreg Roach            " JOIN `##block_setting` bs1 USING (block_id)" .
392aee13b6dSGreg Roach            " JOIN `##block_setting` bs2 USING (block_id)" .
393aee13b6dSGreg Roach            " JOIN `##block_setting` bs3 USING (block_id)" .
394aee13b6dSGreg Roach            " WHERE module_name = :module_name" .
395aee13b6dSGreg Roach            " AND bs1.setting_name = 'header'" .
396aee13b6dSGreg Roach            " AND bs2.setting_name = 'faqbody'" .
397aee13b6dSGreg Roach            " AND bs3.setting_name = 'languages'" .
398aee13b6dSGreg Roach            " AND IFNULL(gedcom_id, :tree_id_1) = :tree_id_2" .
399aee13b6dSGreg Roach            " ORDER BY block_order"
400aee13b6dSGreg Roach        )->execute([
401aee13b6dSGreg Roach            'module_name' => $this->getName(),
402aee13b6dSGreg Roach            'tree_id_1'   => $tree->getTreeId(),
403aee13b6dSGreg Roach            'tree_id_2'   => $tree->getTreeId(),
404aee13b6dSGreg Roach        ])->fetchAll();
405aee13b6dSGreg Roach
4068de50a4eSGreg Roach        // Filter foreign languages.
407492c7072SGreg Roach        $faqs = array_filter($faqs, function (stdClass $faq): bool {
4088de50a4eSGreg Roach            return $faq->languages === '' || in_array(WT_LOCALE, explode(',', $faq->languages));
4098de50a4eSGreg Roach        });
410aee13b6dSGreg Roach
411aee13b6dSGreg Roach        return $this->viewResponse('modules/faq/show', [
4128de50a4eSGreg Roach            'faqs'  => $faqs,
413aee13b6dSGreg Roach            'title' => I18N::translate('Frequently asked questions'),
4148de50a4eSGreg Roach            'tree'  => $tree,
415aee13b6dSGreg Roach        ]);
416aee13b6dSGreg Roach    }
4178c2e8227SGreg Roach}
418