xref: /webtrees/app/Module/FrequentlyAskedQuestionsModule.php (revision b55cbc6b43247e8b2ad14af6f6d24dc6747195ff)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
221d1f373cSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Menu;
2550d6f48cSGreg Roachuse Fisharebest\Webtrees\Services\HtmlService;
261d1f373cSGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
28*b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
2977654037SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
3077654037SGreg Roachuse Illuminate\Database\Query\Builder;
3177654037SGreg Roachuse Illuminate\Support\Collection;
326ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
336ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
34f3874e19SGreg Roach
351d1f373cSGreg Roachuse function redirect;
361d1f373cSGreg Roachuse function route;
378c2e8227SGreg Roach
388c2e8227SGreg Roach/**
398c2e8227SGreg Roach * Class FrequentlyAskedQuestionsModule
408c2e8227SGreg Roach */
4137eb8894SGreg Roachclass FrequentlyAskedQuestionsModule extends AbstractModule implements ModuleConfigInterface, ModuleMenuInterface
42c1010edaSGreg Roach{
4349a243cbSGreg Roach    use ModuleConfigTrait;
4449a243cbSGreg Roach    use ModuleMenuTrait;
4549a243cbSGreg Roach
4643f2f523SGreg Roach    private HtmlService $html_service;
4750d6f48cSGreg Roach
4843f2f523SGreg Roach    private TreeService $tree_service;
491d1f373cSGreg Roach
5050d6f48cSGreg Roach    /**
51ce42304aSGreg Roach     * FrequentlyAskedQuestionsModule constructor.
5250d6f48cSGreg Roach     *
5350d6f48cSGreg Roach     * @param HtmlService $html_service
541d1f373cSGreg Roach     * @param TreeService $tree_service
5550d6f48cSGreg Roach     */
561d1f373cSGreg Roach    public function __construct(HtmlService $html_service, TreeService $tree_service)
5750d6f48cSGreg Roach    {
5850d6f48cSGreg Roach        $this->html_service = $html_service;
591d1f373cSGreg Roach        $this->tree_service = $tree_service;
6050d6f48cSGreg Roach    }
6150d6f48cSGreg Roach
62961ec755SGreg Roach    /**
630cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
64961ec755SGreg Roach     *
65961ec755SGreg Roach     * @return string
66961ec755SGreg Roach     */
6749a243cbSGreg Roach    public function title(): string
68c1010edaSGreg Roach    {
69bbb76c12SGreg Roach        /* I18N: Name of a module. Abbreviation for “Frequently Asked Questions” */
70bbb76c12SGreg Roach        return I18N::translate('FAQ');
718c2e8227SGreg Roach    }
728c2e8227SGreg Roach
73961ec755SGreg Roach    /**
74961ec755SGreg Roach     * A sentence describing what this module does.
75961ec755SGreg Roach     *
76961ec755SGreg Roach     * @return string
77961ec755SGreg Roach     */
7849a243cbSGreg Roach    public function description(): string
79c1010edaSGreg Roach    {
80bbb76c12SGreg Roach        /* I18N: Description of the “FAQ” module */
81bbb76c12SGreg Roach        return I18N::translate('A list of frequently asked questions and answers.');
828c2e8227SGreg Roach    }
838c2e8227SGreg Roach
8476692c8bSGreg Roach    /**
8549a243cbSGreg Roach     * The default position for this menu.  It can be changed in the control panel.
860ee13198SGreg Roach     *
870ee13198SGreg Roach     * @return int
880ee13198SGreg Roach     */
898f53f488SRico Sonntag    public function defaultMenuOrder(): int
90c1010edaSGreg Roach    {
91353b36abSGreg Roach        return 8;
928c2e8227SGreg Roach    }
938c2e8227SGreg Roach
940ee13198SGreg Roach    /**
950ee13198SGreg Roach     * A menu, to be added to the main application menu.
960ee13198SGreg Roach     *
97aee13b6dSGreg Roach     * @param Tree $tree
98aee13b6dSGreg Roach     *
990ee13198SGreg Roach     * @return Menu|null
1000ee13198SGreg Roach     */
10146295629SGreg Roach    public function getMenu(Tree $tree): ?Menu
102c1010edaSGreg Roach    {
10365cf5706SGreg Roach        if ($this->faqsExist($tree, I18N::languageTag())) {
10449a243cbSGreg Roach            return new Menu($this->title(), route('module', [
10526684e68SGreg Roach                'module' => $this->name(),
106c1010edaSGreg Roach                'action' => 'Show',
1079022ab66SGreg Roach                'tree'   => $tree->name(),
10808362db4SGreg Roach            ]), 'menu-faq');
1098c2e8227SGreg Roach        }
110b2ce94c6SRico Sonntag
111b2ce94c6SRico Sonntag        return null;
1128c2e8227SGreg Roach    }
113aee13b6dSGreg Roach
114aee13b6dSGreg Roach    /**
11557ab2231SGreg Roach     * @param ServerRequestInterface $request
116aee13b6dSGreg Roach     *
1176ccdf4f0SGreg Roach     * @return ResponseInterface
118aee13b6dSGreg Roach     */
11957ab2231SGreg Roach    public function getAdminAction(ServerRequestInterface $request): ResponseInterface
120c1010edaSGreg Roach    {
121aee13b6dSGreg Roach        $this->layout = 'layouts/administration';
122aee13b6dSGreg Roach
1231d1f373cSGreg Roach        // This module can't run without a tree
124*b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->treeOptional();
125e218f363SGreg Roach
1261d1f373cSGreg Roach        if (!$tree instanceof Tree) {
127e218f363SGreg Roach            $tree = $this->tree_service->all()->first();
128e218f363SGreg Roach            if ($tree instanceof Tree) {
129e218f363SGreg Roach                return redirect(route('module', ['module' => $this->name(), 'action' => 'Admin', 'tree' => $tree->name()]));
130e218f363SGreg Roach            }
131e218f363SGreg Roach
1321d1f373cSGreg Roach            return redirect(route(ControlPanel::class));
1331d1f373cSGreg Roach        }
1345229eadeSGreg Roach
13526348dcdSGreg Roach        $faqs = $this->faqsForTree($tree);
136aee13b6dSGreg Roach
13777654037SGreg Roach        $min_block_order = DB::table('block')
13826684e68SGreg Roach            ->where('module_name', '=', $this->name())
1390b5fd0a6SGreg Roach            ->where(static function (Builder $query) use ($tree): void {
14077654037SGreg Roach                $query
14177654037SGreg Roach                    ->whereNull('gedcom_id')
14277654037SGreg Roach                    ->orWhere('gedcom_id', '=', $tree->id());
14377654037SGreg Roach            })
14477654037SGreg Roach            ->min('block_order');
145aee13b6dSGreg Roach
14677654037SGreg Roach        $max_block_order = DB::table('block')
14726684e68SGreg Roach            ->where('module_name', '=', $this->name())
1480b5fd0a6SGreg Roach            ->where(static function (Builder $query) use ($tree): void {
14977654037SGreg Roach                $query
15077654037SGreg Roach                    ->whereNull('gedcom_id')
15177654037SGreg Roach                    ->orWhere('gedcom_id', '=', $tree->id());
15277654037SGreg Roach            })
15377654037SGreg Roach            ->max('block_order');
154aee13b6dSGreg Roach
155cc13d6d8SGreg Roach        $title = I18N::translate('Frequently asked questions') . ' — ' . $tree->title();
156aee13b6dSGreg Roach
157aee13b6dSGreg Roach        return $this->viewResponse('modules/faq/config', [
15883615acfSGreg Roach            'action'          => route('module', ['module' => $this->name(), 'action' => 'Admin']),
159aee13b6dSGreg Roach            'faqs'            => $faqs,
160aee13b6dSGreg Roach            'max_block_order' => $max_block_order,
161aee13b6dSGreg Roach            'min_block_order' => $min_block_order,
16271378461SGreg Roach            'module'          => $this->name(),
163aee13b6dSGreg Roach            'title'           => $title,
164aee13b6dSGreg Roach            'tree'            => $tree,
1651e653452SGreg Roach            'tree_names'      => $this->tree_service->titles(),
166aee13b6dSGreg Roach        ]);
167aee13b6dSGreg Roach    }
168aee13b6dSGreg Roach
169aee13b6dSGreg Roach    /**
1706ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
171aee13b6dSGreg Roach     *
1726ccdf4f0SGreg Roach     * @return ResponseInterface
173aee13b6dSGreg Roach     */
174e218f363SGreg Roach    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
175e218f363SGreg Roach    {
176b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
177b46c87bdSGreg Roach
178e218f363SGreg Roach        return redirect(route('module', [
179e218f363SGreg Roach            'module' => $this->name(),
180e218f363SGreg Roach            'action' => 'Admin',
181b46c87bdSGreg Roach            'tree'   => $params['tree'] ?? '',
182e218f363SGreg Roach        ]));
183e218f363SGreg Roach    }
184e218f363SGreg Roach
185e218f363SGreg Roach    /**
186e218f363SGreg Roach     * @param ServerRequestInterface $request
187e218f363SGreg Roach     *
188e218f363SGreg Roach     * @return ResponseInterface
189e218f363SGreg Roach     */
19057ab2231SGreg Roach    public function postAdminDeleteAction(ServerRequestInterface $request): ResponseInterface
191c1010edaSGreg Roach    {
192eb235819SGreg Roach        $block_id = (int) $request->getQueryParams()['block_id'];
193aee13b6dSGreg Roach
19477654037SGreg Roach        DB::table('block_setting')->where('block_id', '=', $block_id)->delete();
195aee13b6dSGreg Roach
19677654037SGreg Roach        DB::table('block')->where('block_id', '=', $block_id)->delete();
197aee13b6dSGreg Roach
198c1010edaSGreg Roach        $url = route('module', [
19926684e68SGreg Roach            'module' => $this->name(),
200c1010edaSGreg Roach            'action' => 'Admin',
201c1010edaSGreg Roach        ]);
202aee13b6dSGreg Roach
2036ccdf4f0SGreg Roach        return redirect($url);
204aee13b6dSGreg Roach    }
205aee13b6dSGreg Roach
206aee13b6dSGreg Roach    /**
2076ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
208aee13b6dSGreg Roach     *
2096ccdf4f0SGreg Roach     * @return ResponseInterface
210aee13b6dSGreg Roach     */
21157ab2231SGreg Roach    public function postAdminMoveDownAction(ServerRequestInterface $request): ResponseInterface
212c1010edaSGreg Roach    {
213eb235819SGreg Roach        $block_id = (int) $request->getQueryParams()['block_id'];
214aee13b6dSGreg Roach
21577654037SGreg Roach        $block_order = DB::table('block')
21677654037SGreg Roach            ->where('block_id', '=', $block_id)
21777654037SGreg Roach            ->value('block_order');
218aee13b6dSGreg Roach
21977654037SGreg Roach        $swap_block = DB::table('block')
22026684e68SGreg Roach            ->where('module_name', '=', $this->name())
22177654037SGreg Roach            ->where('block_order', '>', $block_order)
222a9866bf2SGreg Roach            ->orderBy('block_order')
22377654037SGreg Roach            ->first();
224aee13b6dSGreg Roach
2251d1f373cSGreg Roach        if ($block_order !== null && $swap_block !== null) {
22677654037SGreg Roach            DB::table('block')
22777654037SGreg Roach                ->where('block_id', '=', $block_id)
22877654037SGreg Roach                ->update([
229aee13b6dSGreg Roach                    'block_order' => $swap_block->block_order,
230aee13b6dSGreg Roach                ]);
23177654037SGreg Roach
23277654037SGreg Roach            DB::table('block')
23377654037SGreg Roach                ->where('block_id', '=', $swap_block->block_id)
23477654037SGreg Roach                ->update([
235aee13b6dSGreg Roach                    'block_order' => $block_order,
236aee13b6dSGreg Roach                ]);
237aee13b6dSGreg Roach        }
238aee13b6dSGreg Roach
2391d1f373cSGreg Roach        return response();
240aee13b6dSGreg Roach    }
241aee13b6dSGreg Roach
242aee13b6dSGreg Roach    /**
2436ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
244aee13b6dSGreg Roach     *
2456ccdf4f0SGreg Roach     * @return ResponseInterface
246aee13b6dSGreg Roach     */
24757ab2231SGreg Roach    public function postAdminMoveUpAction(ServerRequestInterface $request): ResponseInterface
248c1010edaSGreg Roach    {
249eb235819SGreg Roach        $block_id = (int) $request->getQueryParams()['block_id'];
250aee13b6dSGreg Roach
25177654037SGreg Roach        $block_order = DB::table('block')
25277654037SGreg Roach            ->where('block_id', '=', $block_id)
25377654037SGreg Roach            ->value('block_order');
254aee13b6dSGreg Roach
25577654037SGreg Roach        $swap_block = DB::table('block')
25626684e68SGreg Roach            ->where('module_name', '=', $this->name())
25777654037SGreg Roach            ->where('block_order', '<', $block_order)
2581d1f373cSGreg Roach            ->orderBy('block_order', 'desc')
25977654037SGreg Roach            ->first();
260aee13b6dSGreg Roach
2611d1f373cSGreg Roach        if ($block_order !== null && $swap_block !== null) {
26277654037SGreg Roach            DB::table('block')
26377654037SGreg Roach                ->where('block_id', '=', $block_id)
26477654037SGreg Roach                ->update([
265aee13b6dSGreg Roach                    'block_order' => $swap_block->block_order,
266aee13b6dSGreg Roach                ]);
26777654037SGreg Roach
26877654037SGreg Roach            DB::table('block')
26977654037SGreg Roach                ->where('block_id', '=', $swap_block->block_id)
27077654037SGreg Roach                ->update([
271aee13b6dSGreg Roach                    'block_order' => $block_order,
272aee13b6dSGreg Roach                ]);
273aee13b6dSGreg Roach        }
274aee13b6dSGreg Roach
2751d1f373cSGreg Roach        return response();
276aee13b6dSGreg Roach    }
277aee13b6dSGreg Roach
278aee13b6dSGreg Roach    /**
2796ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
280aee13b6dSGreg Roach     *
2816ccdf4f0SGreg Roach     * @return ResponseInterface
282aee13b6dSGreg Roach     */
28357ab2231SGreg Roach    public function getAdminEditAction(ServerRequestInterface $request): ResponseInterface
284c1010edaSGreg Roach    {
285aee13b6dSGreg Roach        $this->layout = 'layouts/administration';
286aee13b6dSGreg Roach
287eb235819SGreg Roach        $block_id = (int) ($request->getQueryParams()['block_id'] ?? 0);
288aee13b6dSGreg Roach
289aee13b6dSGreg Roach        if ($block_id === 0) {
290aee13b6dSGreg Roach            // Creating a new faq
291aee13b6dSGreg Roach            $header      = '';
2921e653452SGreg Roach            $body        = '';
2931e653452SGreg Roach            $gedcom_id   = null;
2941e653452SGreg Roach            $block_order = 1 + (int) DB::table('block')->where('module_name', '=', $this->name())->max('block_order');
29577654037SGreg Roach
296aee13b6dSGreg Roach            $languages = [];
297aee13b6dSGreg Roach
298aee13b6dSGreg Roach            $title = I18N::translate('Add an FAQ');
299aee13b6dSGreg Roach        } else {
300aee13b6dSGreg Roach            // Editing an existing faq
301aee13b6dSGreg Roach            $header      = $this->getBlockSetting($block_id, 'header');
3021e653452SGreg Roach            $body        = $this->getBlockSetting($block_id, 'faqbody');
3031e653452SGreg Roach            $gedcom_id   = DB::table('block')->where('block_id', '=', $block_id)->value('gedcom_id');
3041e653452SGreg Roach            $block_order = DB::table('block')->where('block_id', '=', $block_id)->value('block_order');
30577654037SGreg Roach
306aee13b6dSGreg Roach            $languages = explode(',', $this->getBlockSetting($block_id, 'languages'));
307aee13b6dSGreg Roach
308aee13b6dSGreg Roach            $title = I18N::translate('Edit the FAQ');
309aee13b6dSGreg Roach        }
310aee13b6dSGreg Roach
3111e653452SGreg Roach        $gedcom_ids = $this->tree_service->all()
3121e653452SGreg Roach            ->mapWithKeys(static function (Tree $tree): array {
3131e653452SGreg Roach                return [$tree->id() => $tree->title()];
3141e653452SGreg Roach            })
3151e653452SGreg Roach            ->all();
3161e653452SGreg Roach
3171e653452SGreg Roach        $gedcom_ids = ['' => I18N::translate('All')] + $gedcom_ids;
318b6c326d8SGreg Roach
319aee13b6dSGreg Roach        return $this->viewResponse('modules/faq/edit', [
320aee13b6dSGreg Roach            'block_id'    => $block_id,
321aee13b6dSGreg Roach            'block_order' => $block_order,
322aee13b6dSGreg Roach            'header'      => $header,
3231e653452SGreg Roach            'body'        => $body,
324aee13b6dSGreg Roach            'languages'   => $languages,
325aee13b6dSGreg Roach            'title'       => $title,
3261e653452SGreg Roach            'gedcom_id'   => $gedcom_id,
3271e653452SGreg Roach            'gedcom_ids'  => $gedcom_ids,
328aee13b6dSGreg Roach        ]);
329aee13b6dSGreg Roach    }
330aee13b6dSGreg Roach
331aee13b6dSGreg Roach    /**
3326ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
333aee13b6dSGreg Roach     *
3346ccdf4f0SGreg Roach     * @return ResponseInterface
335aee13b6dSGreg Roach     */
33657ab2231SGreg Roach    public function postAdminEditAction(ServerRequestInterface $request): ResponseInterface
337c1010edaSGreg Roach    {
338eb235819SGreg Roach        $block_id = (int) ($request->getQueryParams()['block_id'] ?? 0);
339eb235819SGreg Roach
340b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
341eb235819SGreg Roach
3421e653452SGreg Roach        $body        = $params['body'];
343eb235819SGreg Roach        $header      = $params['header'];
344eb235819SGreg Roach        $languages   = $params['languages'] ?? [];
3451e653452SGreg Roach        $gedcom_id   = $params['gedcom_id'];
346f118193aSGreg Roach        $block_order = (int) $params['block_order'];
347aee13b6dSGreg Roach
3481e653452SGreg Roach        if ($gedcom_id === '') {
3491e653452SGreg Roach            $gedcom_id = null;
3501e653452SGreg Roach        }
3511e653452SGreg Roach
3521e653452SGreg Roach        $body    = $this->html_service->sanitize($body);
35350d6f48cSGreg Roach        $header  = $this->html_service->sanitize($header);
35450d6f48cSGreg Roach
355aee13b6dSGreg Roach        if ($block_id !== 0) {
35677654037SGreg Roach            DB::table('block')
35777654037SGreg Roach                ->where('block_id', '=', $block_id)
35877654037SGreg Roach                ->update([
35977654037SGreg Roach                    'gedcom_id'   => $gedcom_id,
36077654037SGreg Roach                    'block_order' => $block_order,
361aee13b6dSGreg Roach                ]);
362aee13b6dSGreg Roach        } else {
36377654037SGreg Roach            DB::table('block')->insert([
36477654037SGreg Roach                'gedcom_id'   => $gedcom_id,
36526684e68SGreg Roach                'module_name' => $this->name(),
36677654037SGreg Roach                'block_order' => $block_order,
367aee13b6dSGreg Roach            ]);
368aee13b6dSGreg Roach
36977654037SGreg Roach            $block_id = (int) DB::connection()->getPdo()->lastInsertId();
370aee13b6dSGreg Roach        }
371aee13b6dSGreg Roach
3721e653452SGreg Roach        $this->setBlockSetting($block_id, 'faqbody', $body);
373aee13b6dSGreg Roach        $this->setBlockSetting($block_id, 'header', $header);
374aee13b6dSGreg Roach        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
375aee13b6dSGreg Roach
376c1010edaSGreg Roach        $url = route('module', [
37726684e68SGreg Roach            'module' => $this->name(),
378c1010edaSGreg Roach            'action' => 'Admin',
379c1010edaSGreg Roach        ]);
380aee13b6dSGreg Roach
3816ccdf4f0SGreg Roach        return redirect($url);
382aee13b6dSGreg Roach    }
383aee13b6dSGreg Roach
384b998dbceSGreg Roach    /**
38557ab2231SGreg Roach     * @param ServerRequestInterface $request
386b998dbceSGreg Roach     *
3876ccdf4f0SGreg Roach     * @return ResponseInterface
388b998dbceSGreg Roach     */
38957ab2231SGreg Roach    public function getShowAction(ServerRequestInterface $request): ResponseInterface
390c1010edaSGreg Roach    {
391*b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
39257ab2231SGreg Roach
3938de50a4eSGreg Roach        // Filter foreign languages.
39477654037SGreg Roach        $faqs = $this->faqsForTree($tree)
395f70bcff5SGreg Roach            ->filter(static function (object $faq): bool {
39665cf5706SGreg Roach                return $faq->languages === '' || in_array(I18N::languageTag(), explode(',', $faq->languages), true);
3978de50a4eSGreg Roach            });
398aee13b6dSGreg Roach
399aee13b6dSGreg Roach        return $this->viewResponse('modules/faq/show', [
4008de50a4eSGreg Roach            'faqs'  => $faqs,
401aee13b6dSGreg Roach            'title' => I18N::translate('Frequently asked questions'),
4028de50a4eSGreg Roach            'tree'  => $tree,
403aee13b6dSGreg Roach        ]);
404aee13b6dSGreg Roach    }
40577654037SGreg Roach
40677654037SGreg Roach    /**
40777654037SGreg Roach     * @param Tree $tree
40877654037SGreg Roach     *
40936779af1SGreg Roach     * @return Collection<int,object>
41077654037SGreg Roach     */
41177654037SGreg Roach    private function faqsForTree(Tree $tree): Collection
41277654037SGreg Roach    {
41377654037SGreg Roach        return DB::table('block')
41477654037SGreg Roach            ->join('block_setting AS bs1', 'bs1.block_id', '=', 'block.block_id')
41577654037SGreg Roach            ->join('block_setting AS bs2', 'bs2.block_id', '=', 'block.block_id')
41677654037SGreg Roach            ->join('block_setting AS bs3', 'bs3.block_id', '=', 'block.block_id')
41726684e68SGreg Roach            ->where('module_name', '=', $this->name())
41877654037SGreg Roach            ->where('bs1.setting_name', '=', 'header')
41977654037SGreg Roach            ->where('bs2.setting_name', '=', 'faqbody')
42077654037SGreg Roach            ->where('bs3.setting_name', '=', 'languages')
4210b5fd0a6SGreg Roach            ->where(static function (Builder $query) use ($tree): void {
42277654037SGreg Roach                $query
42377654037SGreg Roach                    ->whereNull('gedcom_id')
42477654037SGreg Roach                    ->orWhere('gedcom_id', '=', $tree->id());
42577654037SGreg Roach            })
42677654037SGreg Roach            ->orderBy('block_order')
42777654037SGreg Roach            ->select(['block.block_id', 'block_order', 'gedcom_id', 'bs1.setting_value AS header', 'bs2.setting_value AS faqbody', 'bs3.setting_value AS languages'])
42877654037SGreg Roach            ->get();
42977654037SGreg Roach    }
43077654037SGreg Roach
43177654037SGreg Roach    /**
43277654037SGreg Roach     * @param Tree   $tree
43377654037SGreg Roach     * @param string $language
43477654037SGreg Roach     *
43577654037SGreg Roach     * @return bool
43677654037SGreg Roach     */
43777654037SGreg Roach    private function faqsExist(Tree $tree, string $language): bool
43877654037SGreg Roach    {
43977654037SGreg Roach        return DB::table('block')
44077654037SGreg Roach            ->join('block_setting', 'block_setting.block_id', '=', 'block.block_id')
44126684e68SGreg Roach            ->where('module_name', '=', $this->name())
44277654037SGreg Roach            ->where('setting_name', '=', 'languages')
4430b5fd0a6SGreg Roach            ->where(static function (Builder $query) use ($tree): void {
44477654037SGreg Roach                $query
44577654037SGreg Roach                    ->whereNull('gedcom_id')
44677654037SGreg Roach                    ->orWhere('gedcom_id', '=', $tree->id());
44777654037SGreg Roach            })
44877654037SGreg Roach            ->select(['setting_value AS languages'])
44977654037SGreg Roach            ->get()
450f70bcff5SGreg Roach            ->filter(static function (object $faq) use ($language): bool {
4510b5fd0a6SGreg Roach                return $faq->languages === '' || in_array($language, explode(',', $faq->languages), true);
45277654037SGreg Roach            })
45377654037SGreg Roach            ->isNotEmpty();
45477654037SGreg Roach    }
4558c2e8227SGreg Roach}
456