18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 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 */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1976692c8bSGreg Roach 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Menu; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree; 2377654037SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2477654037SGreg Roachuse Illuminate\Database\Query\Builder; 2577654037SGreg Roachuse Illuminate\Support\Collection; 268de50a4eSGreg Roachuse stdClass; 27aee13b6dSGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse; 28aee13b6dSGreg Roachuse Symfony\Component\HttpFoundation\Request; 29aee13b6dSGreg Roachuse Symfony\Component\HttpFoundation\Response; 308c2e8227SGreg Roach 318c2e8227SGreg Roach/** 328c2e8227SGreg Roach * Class FrequentlyAskedQuestionsModule 338c2e8227SGreg Roach */ 3437eb8894SGreg Roachclass FrequentlyAskedQuestionsModule extends AbstractModule implements ModuleConfigInterface, ModuleMenuInterface 35c1010edaSGreg Roach{ 3649a243cbSGreg Roach use ModuleConfigTrait; 3749a243cbSGreg Roach use ModuleMenuTrait; 3849a243cbSGreg Roach 39961ec755SGreg Roach /** 400cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 41961ec755SGreg Roach * 42961ec755SGreg Roach * @return string 43961ec755SGreg Roach */ 4449a243cbSGreg Roach public function title(): string 45c1010edaSGreg Roach { 46bbb76c12SGreg Roach /* I18N: Name of a module. Abbreviation for “Frequently Asked Questions” */ 47bbb76c12SGreg Roach return I18N::translate('FAQ'); 488c2e8227SGreg Roach } 498c2e8227SGreg Roach 50961ec755SGreg Roach /** 51961ec755SGreg Roach * A sentence describing what this module does. 52961ec755SGreg Roach * 53961ec755SGreg Roach * @return string 54961ec755SGreg Roach */ 5549a243cbSGreg Roach public function description(): string 56c1010edaSGreg Roach { 57bbb76c12SGreg Roach /* I18N: Description of the “FAQ” module */ 58bbb76c12SGreg Roach return I18N::translate('A list of frequently asked questions and answers.'); 598c2e8227SGreg Roach } 608c2e8227SGreg Roach 6176692c8bSGreg Roach /** 6249a243cbSGreg Roach * The default position for this menu. It can be changed in the control panel. 630ee13198SGreg Roach * 640ee13198SGreg Roach * @return int 650ee13198SGreg Roach */ 668f53f488SRico Sonntag public function defaultMenuOrder(): int 67c1010edaSGreg Roach { 68353b36abSGreg Roach return 8; 698c2e8227SGreg Roach } 708c2e8227SGreg Roach 710ee13198SGreg Roach /** 720ee13198SGreg Roach * A menu, to be added to the main application menu. 730ee13198SGreg Roach * 74aee13b6dSGreg Roach * @param Tree $tree 75aee13b6dSGreg Roach * 760ee13198SGreg Roach * @return Menu|null 770ee13198SGreg Roach */ 7846295629SGreg Roach public function getMenu(Tree $tree): ?Menu 79c1010edaSGreg Roach { 8077654037SGreg Roach if ($this->faqsExist($tree, WT_LOCALE)) { 8149a243cbSGreg Roach return new Menu($this->title(), route('module', [ 8226684e68SGreg Roach 'module' => $this->name(), 83c1010edaSGreg Roach 'action' => 'Show', 84aa6f03bbSGreg Roach 'ged' => $tree->name(), 85c1010edaSGreg Roach ]), 'menu-help'); 868c2e8227SGreg Roach } 87b2ce94c6SRico Sonntag 88b2ce94c6SRico Sonntag return null; 898c2e8227SGreg Roach } 90aee13b6dSGreg Roach 91aee13b6dSGreg Roach /** 92b6db7c1fSGreg Roach * @param Tree $tree 93aee13b6dSGreg Roach * 94aee13b6dSGreg Roach * @return Response 95aee13b6dSGreg Roach */ 96b6db7c1fSGreg Roach public function getAdminAction(Tree $tree): Response 97c1010edaSGreg Roach { 98aee13b6dSGreg Roach $this->layout = 'layouts/administration'; 99aee13b6dSGreg Roach 10026348dcdSGreg Roach $faqs = $this->faqsForTree($tree); 101aee13b6dSGreg Roach 10277654037SGreg Roach $min_block_order = DB::table('block') 10326684e68SGreg Roach ->where('module_name', '=', $this->name()) 10477654037SGreg Roach ->where(function (Builder $query) use ($tree): void { 10577654037SGreg Roach $query 10677654037SGreg Roach ->whereNull('gedcom_id') 10777654037SGreg Roach ->orWhere('gedcom_id', '=', $tree->id()); 10877654037SGreg Roach }) 10977654037SGreg Roach ->min('block_order'); 110aee13b6dSGreg Roach 11177654037SGreg Roach $max_block_order = DB::table('block') 11226684e68SGreg Roach ->where('module_name', '=', $this->name()) 11377654037SGreg Roach ->where(function (Builder $query) use ($tree): void { 11477654037SGreg Roach $query 11577654037SGreg Roach ->whereNull('gedcom_id') 11677654037SGreg Roach ->orWhere('gedcom_id', '=', $tree->id()); 11777654037SGreg Roach }) 11877654037SGreg Roach ->max('block_order'); 119aee13b6dSGreg Roach 120cc13d6d8SGreg Roach $title = I18N::translate('Frequently asked questions') . ' — ' . $tree->title(); 121aee13b6dSGreg Roach 122aee13b6dSGreg Roach return $this->viewResponse('modules/faq/config', [ 123aee13b6dSGreg Roach 'faqs' => $faqs, 124aee13b6dSGreg Roach 'max_block_order' => $max_block_order, 125aee13b6dSGreg Roach 'min_block_order' => $min_block_order, 126aee13b6dSGreg Roach 'title' => $title, 127aee13b6dSGreg Roach 'tree' => $tree, 128aee13b6dSGreg Roach 'tree_names' => Tree::getNameList(), 129aee13b6dSGreg Roach ]); 130aee13b6dSGreg Roach } 131aee13b6dSGreg Roach 132aee13b6dSGreg Roach /** 133aee13b6dSGreg Roach * @param Request $request 134b6db7c1fSGreg Roach * @param Tree $tree 135aee13b6dSGreg Roach * 136aee13b6dSGreg Roach * @return RedirectResponse 137aee13b6dSGreg Roach */ 138b6db7c1fSGreg Roach public function postAdminDeleteAction(Request $request, Tree $tree): RedirectResponse 139c1010edaSGreg Roach { 140aee13b6dSGreg Roach $block_id = (int) $request->get('block_id'); 141aee13b6dSGreg Roach 14277654037SGreg Roach DB::table('block_setting')->where('block_id', '=', $block_id)->delete(); 143aee13b6dSGreg Roach 14477654037SGreg Roach DB::table('block')->where('block_id', '=', $block_id)->delete(); 145aee13b6dSGreg Roach 146c1010edaSGreg Roach $url = route('module', [ 14726684e68SGreg Roach 'module' => $this->name(), 148c1010edaSGreg Roach 'action' => 'Admin', 149aa6f03bbSGreg Roach 'ged' => $tree->name(), 150c1010edaSGreg Roach ]); 151aee13b6dSGreg Roach 152aee13b6dSGreg Roach return new RedirectResponse($url); 153aee13b6dSGreg Roach } 154aee13b6dSGreg Roach 155aee13b6dSGreg Roach /** 156aee13b6dSGreg Roach * @param Request $request 157b6db7c1fSGreg Roach * @param Tree $tree 158aee13b6dSGreg Roach * 159aee13b6dSGreg Roach * @return RedirectResponse 160aee13b6dSGreg Roach */ 161b6db7c1fSGreg Roach public function postAdminMoveDownAction(Request $request, Tree $tree): RedirectResponse 162c1010edaSGreg Roach { 163aee13b6dSGreg Roach $block_id = (int) $request->get('block_id'); 164aee13b6dSGreg Roach 16577654037SGreg Roach $block_order = DB::table('block') 16677654037SGreg Roach ->where('block_id', '=', $block_id) 16777654037SGreg Roach ->value('block_order'); 168aee13b6dSGreg Roach 16977654037SGreg Roach $swap_block = DB::table('block') 17026684e68SGreg Roach ->where('module_name', '=', $this->name()) 17177654037SGreg Roach ->where('block_order', '=', function (Builder $query) use ($block_order): void { 17277654037SGreg Roach $query 17377654037SGreg Roach ->from('block') 17426684e68SGreg Roach ->where('module_name', '=', $this->name()) 17577654037SGreg Roach ->where('block_order', '>', $block_order) 17677654037SGreg Roach ->select(DB::raw('MIN(block_order)')); 17777654037SGreg Roach }) 17877654037SGreg Roach ->select(['block_order', 'block_id']) 17977654037SGreg Roach ->first(); 180aee13b6dSGreg Roach 181aee13b6dSGreg Roach if ($swap_block !== null) { 18277654037SGreg Roach DB::table('block') 18377654037SGreg Roach ->where('block_id', '=', $block_id) 18477654037SGreg Roach ->update([ 185aee13b6dSGreg Roach 'block_order' => $swap_block->block_order, 186aee13b6dSGreg Roach ]); 18777654037SGreg Roach 18877654037SGreg Roach DB::table('block') 18977654037SGreg Roach ->where('block_id', '=', $swap_block->block_id) 19077654037SGreg Roach ->update([ 191aee13b6dSGreg Roach 'block_order' => $block_order, 192aee13b6dSGreg Roach ]); 193aee13b6dSGreg Roach } 194aee13b6dSGreg Roach 195c1010edaSGreg Roach $url = route('module', [ 19626684e68SGreg Roach 'module' => $this->name(), 197c1010edaSGreg Roach 'action' => 'Admin', 198aa6f03bbSGreg Roach 'ged' => $tree->name(), 199c1010edaSGreg Roach ]); 200aee13b6dSGreg Roach 201aee13b6dSGreg Roach return new RedirectResponse($url); 202aee13b6dSGreg Roach } 203aee13b6dSGreg Roach 204aee13b6dSGreg Roach /** 205aee13b6dSGreg Roach * @param Request $request 206b6db7c1fSGreg Roach * @param Tree $tree 207aee13b6dSGreg Roach * 208aee13b6dSGreg Roach * @return RedirectResponse 209aee13b6dSGreg Roach */ 210b6db7c1fSGreg Roach public function postAdminMoveUpAction(Request $request, Tree $tree): RedirectResponse 211c1010edaSGreg Roach { 212aee13b6dSGreg Roach $block_id = (int) $request->get('block_id'); 213aee13b6dSGreg Roach 21477654037SGreg Roach $block_order = DB::table('block') 21577654037SGreg Roach ->where('block_id', '=', $block_id) 21677654037SGreg Roach ->value('block_order'); 217aee13b6dSGreg Roach 21877654037SGreg Roach $swap_block = DB::table('block') 21926684e68SGreg Roach ->where('module_name', '=', $this->name()) 22077654037SGreg Roach ->where('block_order', '=', function (Builder $query) use ($block_order): void { 22177654037SGreg Roach $query 22277654037SGreg Roach ->from('block') 22326684e68SGreg Roach ->where('module_name', '=', $this->name()) 22477654037SGreg Roach ->where('block_order', '<', $block_order) 22577654037SGreg Roach ->select(DB::raw('MAX(block_order)')); 22677654037SGreg Roach }) 22777654037SGreg Roach ->select(['block_order', 'block_id']) 22877654037SGreg Roach ->first(); 229aee13b6dSGreg Roach 230aee13b6dSGreg Roach if ($swap_block !== null) { 23177654037SGreg Roach DB::table('block') 23277654037SGreg Roach ->where('block_id', '=', $block_id) 23377654037SGreg Roach ->update([ 234aee13b6dSGreg Roach 'block_order' => $swap_block->block_order, 235aee13b6dSGreg Roach ]); 23677654037SGreg Roach 23777654037SGreg Roach DB::table('block') 23877654037SGreg Roach ->where('block_id', '=', $swap_block->block_id) 23977654037SGreg Roach ->update([ 240aee13b6dSGreg Roach 'block_order' => $block_order, 241aee13b6dSGreg Roach ]); 242aee13b6dSGreg Roach } 243aee13b6dSGreg Roach 244c1010edaSGreg Roach $url = route('module', [ 24526684e68SGreg Roach 'module' => $this->name(), 246c1010edaSGreg Roach 'action' => 'Admin', 247aa6f03bbSGreg Roach 'ged' => $tree->name(), 248c1010edaSGreg Roach ]); 249aee13b6dSGreg Roach 250aee13b6dSGreg Roach return new RedirectResponse($url); 251aee13b6dSGreg Roach } 252aee13b6dSGreg Roach 253aee13b6dSGreg Roach /** 254aee13b6dSGreg Roach * @param Request $request 255b6db7c1fSGreg Roach * @param Tree $tree 256aee13b6dSGreg Roach * 257aee13b6dSGreg Roach * @return Response 258aee13b6dSGreg Roach */ 259b6db7c1fSGreg Roach public function getAdminEditAction(Request $request, Tree $tree): Response 260c1010edaSGreg Roach { 261aee13b6dSGreg Roach $this->layout = 'layouts/administration'; 262aee13b6dSGreg Roach 263aee13b6dSGreg Roach $block_id = (int) $request->get('block_id'); 264aee13b6dSGreg Roach 265aee13b6dSGreg Roach if ($block_id === 0) { 266aee13b6dSGreg Roach // Creating a new faq 267aee13b6dSGreg Roach $header = ''; 268aee13b6dSGreg Roach $faqbody = ''; 26977654037SGreg Roach 27077654037SGreg Roach $block_order = 1 + (int) DB::table('block') 27126684e68SGreg Roach ->where('module_name', '=', $this->name()) 27277654037SGreg Roach ->max('block_order'); 27377654037SGreg Roach 274aee13b6dSGreg Roach $languages = []; 275aee13b6dSGreg Roach 276aee13b6dSGreg Roach $title = I18N::translate('Add an FAQ'); 277aee13b6dSGreg Roach } else { 278aee13b6dSGreg Roach // Editing an existing faq 279aee13b6dSGreg Roach $header = $this->getBlockSetting($block_id, 'header'); 280aee13b6dSGreg Roach $faqbody = $this->getBlockSetting($block_id, 'faqbody'); 28177654037SGreg Roach 28277654037SGreg Roach $block_order = DB::table('block') 28377654037SGreg Roach ->where('block_id', '=', $block_id) 28477654037SGreg Roach ->value('block_order'); 28577654037SGreg Roach 286aee13b6dSGreg Roach $languages = explode(',', $this->getBlockSetting($block_id, 'languages')); 287aee13b6dSGreg Roach 288aee13b6dSGreg Roach $title = I18N::translate('Edit the FAQ'); 289aee13b6dSGreg Roach } 290aee13b6dSGreg Roach 291*b6c326d8SGreg Roach $tree_names = ['' => I18N::translate('All')] + Tree::getIdList(); 292*b6c326d8SGreg Roach 293aee13b6dSGreg Roach return $this->viewResponse('modules/faq/edit', [ 294aee13b6dSGreg Roach 'block_id' => $block_id, 295aee13b6dSGreg Roach 'block_order' => $block_order, 296aee13b6dSGreg Roach 'header' => $header, 297aee13b6dSGreg Roach 'faqbody' => $faqbody, 298aee13b6dSGreg Roach 'languages' => $languages, 299aee13b6dSGreg Roach 'title' => $title, 300aee13b6dSGreg Roach 'tree' => $tree, 301*b6c326d8SGreg Roach 'tree_names' => $tree_names, 302aee13b6dSGreg Roach ]); 303aee13b6dSGreg Roach } 304aee13b6dSGreg Roach 305aee13b6dSGreg Roach /** 306aee13b6dSGreg Roach * @param Request $request 307b6db7c1fSGreg Roach * @param Tree $tree 308aee13b6dSGreg Roach * 309aee13b6dSGreg Roach * @return RedirectResponse 310aee13b6dSGreg Roach */ 311b6db7c1fSGreg Roach public function postAdminEditAction(Request $request, Tree $tree): RedirectResponse 312c1010edaSGreg Roach { 313aee13b6dSGreg Roach $block_id = (int) $request->get('block_id'); 314aee13b6dSGreg Roach $faqbody = $request->get('faqbody', ''); 315aee13b6dSGreg Roach $header = $request->get('header', ''); 316aee13b6dSGreg Roach $languages = $request->get('languages', []); 31777654037SGreg Roach $gedcom_id = (int) $request->get('gedcom_id') ?: null; 31877654037SGreg Roach $block_order = (int) $request->get('block_order'); 319aee13b6dSGreg Roach 320aee13b6dSGreg Roach if ($block_id !== 0) { 32177654037SGreg Roach DB::table('block') 32277654037SGreg Roach ->where('block_id', '=', $block_id) 32377654037SGreg Roach ->update([ 32477654037SGreg Roach 'gedcom_id' => $gedcom_id, 32577654037SGreg Roach 'block_order' => $block_order, 326aee13b6dSGreg Roach ]); 327aee13b6dSGreg Roach } else { 32877654037SGreg Roach DB::table('block')->insert([ 32977654037SGreg Roach 'gedcom_id' => $gedcom_id, 33026684e68SGreg Roach 'module_name' => $this->name(), 33177654037SGreg Roach 'block_order' => $block_order, 332aee13b6dSGreg Roach ]); 333aee13b6dSGreg Roach 33477654037SGreg Roach $block_id = (int) DB::connection()->getPdo()->lastInsertId(); 335aee13b6dSGreg Roach } 336aee13b6dSGreg Roach 337aee13b6dSGreg Roach $this->setBlockSetting($block_id, 'faqbody', $faqbody); 338aee13b6dSGreg Roach $this->setBlockSetting($block_id, 'header', $header); 339aee13b6dSGreg Roach $this->setBlockSetting($block_id, 'languages', implode(',', $languages)); 340aee13b6dSGreg Roach 341c1010edaSGreg Roach $url = route('module', [ 34226684e68SGreg Roach 'module' => $this->name(), 343c1010edaSGreg Roach 'action' => 'Admin', 344aa6f03bbSGreg Roach 'ged' => $tree->name(), 345c1010edaSGreg Roach ]); 346aee13b6dSGreg Roach 347aee13b6dSGreg Roach return new RedirectResponse($url); 348aee13b6dSGreg Roach } 349aee13b6dSGreg Roach 350b998dbceSGreg Roach /** 351b6db7c1fSGreg Roach * @param Tree $tree 352b998dbceSGreg Roach * 353b998dbceSGreg Roach * @return Response 354b998dbceSGreg Roach */ 35538f9e88cSGreg Roach public function getShowAction(Tree $tree): Response 356c1010edaSGreg Roach { 3578de50a4eSGreg Roach // Filter foreign languages. 35877654037SGreg Roach $faqs = $this->faqsForTree($tree) 35977654037SGreg Roach ->filter(function (stdClass $faq): bool { 3608de50a4eSGreg Roach return $faq->languages === '' || in_array(WT_LOCALE, explode(',', $faq->languages)); 3618de50a4eSGreg Roach }); 362aee13b6dSGreg Roach 363aee13b6dSGreg Roach return $this->viewResponse('modules/faq/show', [ 3648de50a4eSGreg Roach 'faqs' => $faqs, 365aee13b6dSGreg Roach 'title' => I18N::translate('Frequently asked questions'), 3668de50a4eSGreg Roach 'tree' => $tree, 367aee13b6dSGreg Roach ]); 368aee13b6dSGreg Roach } 36977654037SGreg Roach 37077654037SGreg Roach /** 37177654037SGreg Roach * @param Tree $tree 37277654037SGreg Roach * 37377654037SGreg Roach * @return Collection 37477654037SGreg Roach */ 37577654037SGreg Roach private function faqsForTree(Tree $tree): Collection 37677654037SGreg Roach { 37777654037SGreg Roach return DB::table('block') 37877654037SGreg Roach ->join('block_setting AS bs1', 'bs1.block_id', '=', 'block.block_id') 37977654037SGreg Roach ->join('block_setting AS bs2', 'bs2.block_id', '=', 'block.block_id') 38077654037SGreg Roach ->join('block_setting AS bs3', 'bs3.block_id', '=', 'block.block_id') 38126684e68SGreg Roach ->where('module_name', '=', $this->name()) 38277654037SGreg Roach ->where('bs1.setting_name', '=', 'header') 38377654037SGreg Roach ->where('bs2.setting_name', '=', 'faqbody') 38477654037SGreg Roach ->where('bs3.setting_name', '=', 'languages') 38577654037SGreg Roach ->where(function (Builder $query) use ($tree): void { 38677654037SGreg Roach $query 38777654037SGreg Roach ->whereNull('gedcom_id') 38877654037SGreg Roach ->orWhere('gedcom_id', '=', $tree->id()); 38977654037SGreg Roach }) 39077654037SGreg Roach ->orderBy('block_order') 39177654037SGreg Roach ->select(['block.block_id', 'block_order', 'gedcom_id', 'bs1.setting_value AS header', 'bs2.setting_value AS faqbody', 'bs3.setting_value AS languages']) 39277654037SGreg Roach ->get(); 39377654037SGreg Roach } 39477654037SGreg Roach 39577654037SGreg Roach /** 39677654037SGreg Roach * @param Tree $tree 39777654037SGreg Roach * @param string $language 39877654037SGreg Roach * 39977654037SGreg Roach * @return bool 40077654037SGreg Roach */ 40177654037SGreg Roach private function faqsExist(Tree $tree, string $language): bool 40277654037SGreg Roach { 40377654037SGreg Roach return DB::table('block') 40477654037SGreg Roach ->join('block_setting', 'block_setting.block_id', '=', 'block.block_id') 40526684e68SGreg Roach ->where('module_name', '=', $this->name()) 40677654037SGreg Roach ->where('setting_name', '=', 'languages') 40777654037SGreg Roach ->where(function (Builder $query) use ($tree): void { 40877654037SGreg Roach $query 40977654037SGreg Roach ->whereNull('gedcom_id') 41077654037SGreg Roach ->orWhere('gedcom_id', '=', $tree->id()); 41177654037SGreg Roach }) 41277654037SGreg Roach ->select(['setting_value AS languages']) 41377654037SGreg Roach ->get() 41477654037SGreg Roach ->filter(function (stdClass $faq) use ($language): bool { 41577654037SGreg Roach return $faq->languages === '' || in_array($language, explode(',', $faq->languages)); 41677654037SGreg Roach }) 41777654037SGreg Roach ->isNotEmpty(); 41877654037SGreg Roach } 4198c2e8227SGreg Roach} 420