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; 266ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 276ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 288de50a4eSGreg Roachuse stdClass; 298c2e8227SGreg Roach 308c2e8227SGreg Roach/** 318c2e8227SGreg Roach * Class FrequentlyAskedQuestionsModule 328c2e8227SGreg Roach */ 3337eb8894SGreg Roachclass FrequentlyAskedQuestionsModule extends AbstractModule implements ModuleConfigInterface, ModuleMenuInterface 34c1010edaSGreg Roach{ 3549a243cbSGreg Roach use ModuleConfigTrait; 3649a243cbSGreg Roach use ModuleMenuTrait; 3749a243cbSGreg Roach 38961ec755SGreg Roach /** 390cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 40961ec755SGreg Roach * 41961ec755SGreg Roach * @return string 42961ec755SGreg Roach */ 4349a243cbSGreg Roach public function title(): string 44c1010edaSGreg Roach { 45bbb76c12SGreg Roach /* I18N: Name of a module. Abbreviation for “Frequently Asked Questions” */ 46bbb76c12SGreg Roach return I18N::translate('FAQ'); 478c2e8227SGreg Roach } 488c2e8227SGreg Roach 49961ec755SGreg Roach /** 50961ec755SGreg Roach * A sentence describing what this module does. 51961ec755SGreg Roach * 52961ec755SGreg Roach * @return string 53961ec755SGreg Roach */ 5449a243cbSGreg Roach public function description(): string 55c1010edaSGreg Roach { 56bbb76c12SGreg Roach /* I18N: Description of the “FAQ” module */ 57bbb76c12SGreg Roach return I18N::translate('A list of frequently asked questions and answers.'); 588c2e8227SGreg Roach } 598c2e8227SGreg Roach 6076692c8bSGreg Roach /** 6149a243cbSGreg Roach * The default position for this menu. It can be changed in the control panel. 620ee13198SGreg Roach * 630ee13198SGreg Roach * @return int 640ee13198SGreg Roach */ 658f53f488SRico Sonntag public function defaultMenuOrder(): int 66c1010edaSGreg Roach { 67353b36abSGreg Roach return 8; 688c2e8227SGreg Roach } 698c2e8227SGreg Roach 700ee13198SGreg Roach /** 710ee13198SGreg Roach * A menu, to be added to the main application menu. 720ee13198SGreg Roach * 73aee13b6dSGreg Roach * @param Tree $tree 74aee13b6dSGreg Roach * 750ee13198SGreg Roach * @return Menu|null 760ee13198SGreg Roach */ 7746295629SGreg Roach public function getMenu(Tree $tree): ?Menu 78c1010edaSGreg Roach { 7977654037SGreg Roach if ($this->faqsExist($tree, WT_LOCALE)) { 8049a243cbSGreg Roach return new Menu($this->title(), route('module', [ 8126684e68SGreg Roach 'module' => $this->name(), 82c1010edaSGreg Roach 'action' => 'Show', 83aa6f03bbSGreg Roach 'ged' => $tree->name(), 84c1010edaSGreg Roach ]), 'menu-help'); 858c2e8227SGreg Roach } 86b2ce94c6SRico Sonntag 87b2ce94c6SRico Sonntag return null; 888c2e8227SGreg Roach } 89aee13b6dSGreg Roach 90aee13b6dSGreg Roach /** 91b6db7c1fSGreg Roach * @param Tree $tree 92aee13b6dSGreg Roach * 936ccdf4f0SGreg Roach * @return ResponseInterface 94aee13b6dSGreg Roach */ 956ccdf4f0SGreg Roach public function getAdminAction(Tree $tree): ResponseInterface 96c1010edaSGreg Roach { 97aee13b6dSGreg Roach $this->layout = 'layouts/administration'; 98aee13b6dSGreg Roach 9926348dcdSGreg Roach $faqs = $this->faqsForTree($tree); 100aee13b6dSGreg Roach 10177654037SGreg Roach $min_block_order = DB::table('block') 10226684e68SGreg Roach ->where('module_name', '=', $this->name()) 1030b5fd0a6SGreg Roach ->where(static function (Builder $query) use ($tree): void { 10477654037SGreg Roach $query 10577654037SGreg Roach ->whereNull('gedcom_id') 10677654037SGreg Roach ->orWhere('gedcom_id', '=', $tree->id()); 10777654037SGreg Roach }) 10877654037SGreg Roach ->min('block_order'); 109aee13b6dSGreg Roach 11077654037SGreg Roach $max_block_order = DB::table('block') 11126684e68SGreg Roach ->where('module_name', '=', $this->name()) 1120b5fd0a6SGreg Roach ->where(static function (Builder $query) use ($tree): void { 11377654037SGreg Roach $query 11477654037SGreg Roach ->whereNull('gedcom_id') 11577654037SGreg Roach ->orWhere('gedcom_id', '=', $tree->id()); 11677654037SGreg Roach }) 11777654037SGreg Roach ->max('block_order'); 118aee13b6dSGreg Roach 119cc13d6d8SGreg Roach $title = I18N::translate('Frequently asked questions') . ' — ' . $tree->title(); 120aee13b6dSGreg Roach 121aee13b6dSGreg Roach return $this->viewResponse('modules/faq/config', [ 122aee13b6dSGreg Roach 'faqs' => $faqs, 123aee13b6dSGreg Roach 'max_block_order' => $max_block_order, 124aee13b6dSGreg Roach 'min_block_order' => $min_block_order, 125aee13b6dSGreg Roach 'title' => $title, 126aee13b6dSGreg Roach 'tree' => $tree, 127aee13b6dSGreg Roach 'tree_names' => Tree::getNameList(), 128aee13b6dSGreg Roach ]); 129aee13b6dSGreg Roach } 130aee13b6dSGreg Roach 131aee13b6dSGreg Roach /** 1326ccdf4f0SGreg Roach * @param ServerRequestInterface $request 133b6db7c1fSGreg Roach * @param Tree $tree 134aee13b6dSGreg Roach * 1356ccdf4f0SGreg Roach * @return ResponseInterface 136aee13b6dSGreg Roach */ 1376ccdf4f0SGreg Roach public function postAdminDeleteAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 138c1010edaSGreg Roach { 139aee13b6dSGreg Roach $block_id = (int) $request->get('block_id'); 140aee13b6dSGreg Roach 14177654037SGreg Roach DB::table('block_setting')->where('block_id', '=', $block_id)->delete(); 142aee13b6dSGreg Roach 14377654037SGreg Roach DB::table('block')->where('block_id', '=', $block_id)->delete(); 144aee13b6dSGreg Roach 145c1010edaSGreg Roach $url = route('module', [ 14626684e68SGreg Roach 'module' => $this->name(), 147c1010edaSGreg Roach 'action' => 'Admin', 148aa6f03bbSGreg Roach 'ged' => $tree->name(), 149c1010edaSGreg Roach ]); 150aee13b6dSGreg Roach 1516ccdf4f0SGreg Roach return redirect($url); 152aee13b6dSGreg Roach } 153aee13b6dSGreg Roach 154aee13b6dSGreg Roach /** 1556ccdf4f0SGreg Roach * @param ServerRequestInterface $request 156b6db7c1fSGreg Roach * @param Tree $tree 157aee13b6dSGreg Roach * 1586ccdf4f0SGreg Roach * @return ResponseInterface 159aee13b6dSGreg Roach */ 1606ccdf4f0SGreg Roach public function postAdminMoveDownAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 161c1010edaSGreg Roach { 162aee13b6dSGreg Roach $block_id = (int) $request->get('block_id'); 163aee13b6dSGreg Roach 16477654037SGreg Roach $block_order = DB::table('block') 16577654037SGreg Roach ->where('block_id', '=', $block_id) 16677654037SGreg Roach ->value('block_order'); 167aee13b6dSGreg Roach 16877654037SGreg Roach $swap_block = DB::table('block') 16926684e68SGreg Roach ->where('module_name', '=', $this->name()) 1700b5fd0a6SGreg Roach ->where('block_order', '=', static function (Builder $query) use ($block_order): void { 17177654037SGreg Roach $query 17277654037SGreg Roach ->from('block') 17326684e68SGreg Roach ->where('module_name', '=', $this->name()) 17477654037SGreg Roach ->where('block_order', '>', $block_order) 17577654037SGreg Roach ->select(DB::raw('MIN(block_order)')); 17677654037SGreg Roach }) 17777654037SGreg Roach ->select(['block_order', 'block_id']) 17877654037SGreg Roach ->first(); 179aee13b6dSGreg Roach 180aee13b6dSGreg Roach if ($swap_block !== null) { 18177654037SGreg Roach DB::table('block') 18277654037SGreg Roach ->where('block_id', '=', $block_id) 18377654037SGreg Roach ->update([ 184aee13b6dSGreg Roach 'block_order' => $swap_block->block_order, 185aee13b6dSGreg Roach ]); 18677654037SGreg Roach 18777654037SGreg Roach DB::table('block') 18877654037SGreg Roach ->where('block_id', '=', $swap_block->block_id) 18977654037SGreg Roach ->update([ 190aee13b6dSGreg Roach 'block_order' => $block_order, 191aee13b6dSGreg Roach ]); 192aee13b6dSGreg Roach } 193aee13b6dSGreg Roach 194c1010edaSGreg Roach $url = route('module', [ 19526684e68SGreg Roach 'module' => $this->name(), 196c1010edaSGreg Roach 'action' => 'Admin', 197aa6f03bbSGreg Roach 'ged' => $tree->name(), 198c1010edaSGreg Roach ]); 199aee13b6dSGreg Roach 2006ccdf4f0SGreg Roach return redirect($url); 201aee13b6dSGreg Roach } 202aee13b6dSGreg Roach 203aee13b6dSGreg Roach /** 2046ccdf4f0SGreg Roach * @param ServerRequestInterface $request 205b6db7c1fSGreg Roach * @param Tree $tree 206aee13b6dSGreg Roach * 2076ccdf4f0SGreg Roach * @return ResponseInterface 208aee13b6dSGreg Roach */ 2096ccdf4f0SGreg Roach public function postAdminMoveUpAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 210c1010edaSGreg Roach { 211aee13b6dSGreg Roach $block_id = (int) $request->get('block_id'); 212aee13b6dSGreg Roach 21377654037SGreg Roach $block_order = DB::table('block') 21477654037SGreg Roach ->where('block_id', '=', $block_id) 21577654037SGreg Roach ->value('block_order'); 216aee13b6dSGreg Roach 21777654037SGreg Roach $swap_block = DB::table('block') 21826684e68SGreg Roach ->where('module_name', '=', $this->name()) 2190b5fd0a6SGreg Roach ->where('block_order', '=', static function (Builder $query) use ($block_order): void { 22077654037SGreg Roach $query 22177654037SGreg Roach ->from('block') 22226684e68SGreg Roach ->where('module_name', '=', $this->name()) 22377654037SGreg Roach ->where('block_order', '<', $block_order) 22477654037SGreg Roach ->select(DB::raw('MAX(block_order)')); 22577654037SGreg Roach }) 22677654037SGreg Roach ->select(['block_order', 'block_id']) 22777654037SGreg Roach ->first(); 228aee13b6dSGreg Roach 229aee13b6dSGreg Roach if ($swap_block !== null) { 23077654037SGreg Roach DB::table('block') 23177654037SGreg Roach ->where('block_id', '=', $block_id) 23277654037SGreg Roach ->update([ 233aee13b6dSGreg Roach 'block_order' => $swap_block->block_order, 234aee13b6dSGreg Roach ]); 23577654037SGreg Roach 23677654037SGreg Roach DB::table('block') 23777654037SGreg Roach ->where('block_id', '=', $swap_block->block_id) 23877654037SGreg Roach ->update([ 239aee13b6dSGreg Roach 'block_order' => $block_order, 240aee13b6dSGreg Roach ]); 241aee13b6dSGreg Roach } 242aee13b6dSGreg Roach 243c1010edaSGreg Roach $url = route('module', [ 24426684e68SGreg Roach 'module' => $this->name(), 245c1010edaSGreg Roach 'action' => 'Admin', 246aa6f03bbSGreg Roach 'ged' => $tree->name(), 247c1010edaSGreg Roach ]); 248aee13b6dSGreg Roach 2496ccdf4f0SGreg Roach return redirect($url); 250aee13b6dSGreg Roach } 251aee13b6dSGreg Roach 252aee13b6dSGreg Roach /** 2536ccdf4f0SGreg Roach * @param ServerRequestInterface $request 254b6db7c1fSGreg Roach * @param Tree $tree 255aee13b6dSGreg Roach * 2566ccdf4f0SGreg Roach * @return ResponseInterface 257aee13b6dSGreg Roach */ 2586ccdf4f0SGreg Roach public function getAdminEditAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 259c1010edaSGreg Roach { 260aee13b6dSGreg Roach $this->layout = 'layouts/administration'; 261aee13b6dSGreg Roach 262aee13b6dSGreg Roach $block_id = (int) $request->get('block_id'); 263aee13b6dSGreg Roach 264aee13b6dSGreg Roach if ($block_id === 0) { 265aee13b6dSGreg Roach // Creating a new faq 266aee13b6dSGreg Roach $header = ''; 267aee13b6dSGreg Roach $faqbody = ''; 26877654037SGreg Roach 26977654037SGreg Roach $block_order = 1 + (int) DB::table('block') 27026684e68SGreg Roach ->where('module_name', '=', $this->name()) 27177654037SGreg Roach ->max('block_order'); 27277654037SGreg Roach 273aee13b6dSGreg Roach $languages = []; 274aee13b6dSGreg Roach 275aee13b6dSGreg Roach $title = I18N::translate('Add an FAQ'); 276aee13b6dSGreg Roach } else { 277aee13b6dSGreg Roach // Editing an existing faq 278aee13b6dSGreg Roach $header = $this->getBlockSetting($block_id, 'header'); 279aee13b6dSGreg Roach $faqbody = $this->getBlockSetting($block_id, 'faqbody'); 28077654037SGreg Roach 28177654037SGreg Roach $block_order = DB::table('block') 28277654037SGreg Roach ->where('block_id', '=', $block_id) 28377654037SGreg Roach ->value('block_order'); 28477654037SGreg Roach 285aee13b6dSGreg Roach $languages = explode(',', $this->getBlockSetting($block_id, 'languages')); 286aee13b6dSGreg Roach 287aee13b6dSGreg Roach $title = I18N::translate('Edit the FAQ'); 288aee13b6dSGreg Roach } 289aee13b6dSGreg Roach 290b6c326d8SGreg Roach $tree_names = ['' => I18N::translate('All')] + Tree::getIdList(); 291b6c326d8SGreg Roach 292aee13b6dSGreg Roach return $this->viewResponse('modules/faq/edit', [ 293aee13b6dSGreg Roach 'block_id' => $block_id, 294aee13b6dSGreg Roach 'block_order' => $block_order, 295aee13b6dSGreg Roach 'header' => $header, 296aee13b6dSGreg Roach 'faqbody' => $faqbody, 297aee13b6dSGreg Roach 'languages' => $languages, 298aee13b6dSGreg Roach 'title' => $title, 299aee13b6dSGreg Roach 'tree' => $tree, 300b6c326d8SGreg Roach 'tree_names' => $tree_names, 301aee13b6dSGreg Roach ]); 302aee13b6dSGreg Roach } 303aee13b6dSGreg Roach 304aee13b6dSGreg Roach /** 3056ccdf4f0SGreg Roach * @param ServerRequestInterface $request 306b6db7c1fSGreg Roach * @param Tree $tree 307aee13b6dSGreg Roach * 3086ccdf4f0SGreg Roach * @return ResponseInterface 309aee13b6dSGreg Roach */ 3106ccdf4f0SGreg Roach public function postAdminEditAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 311c1010edaSGreg Roach { 312aee13b6dSGreg Roach $block_id = (int) $request->get('block_id'); 313aee13b6dSGreg Roach $faqbody = $request->get('faqbody', ''); 314aee13b6dSGreg Roach $header = $request->get('header', ''); 315aee13b6dSGreg Roach $languages = $request->get('languages', []); 31677654037SGreg Roach $gedcom_id = (int) $request->get('gedcom_id') ?: null; 31777654037SGreg Roach $block_order = (int) $request->get('block_order'); 318aee13b6dSGreg Roach 319aee13b6dSGreg Roach if ($block_id !== 0) { 32077654037SGreg Roach DB::table('block') 32177654037SGreg Roach ->where('block_id', '=', $block_id) 32277654037SGreg Roach ->update([ 32377654037SGreg Roach 'gedcom_id' => $gedcom_id, 32477654037SGreg Roach 'block_order' => $block_order, 325aee13b6dSGreg Roach ]); 326aee13b6dSGreg Roach } else { 32777654037SGreg Roach DB::table('block')->insert([ 32877654037SGreg Roach 'gedcom_id' => $gedcom_id, 32926684e68SGreg Roach 'module_name' => $this->name(), 33077654037SGreg Roach 'block_order' => $block_order, 331aee13b6dSGreg Roach ]); 332aee13b6dSGreg Roach 33377654037SGreg Roach $block_id = (int) DB::connection()->getPdo()->lastInsertId(); 334aee13b6dSGreg Roach } 335aee13b6dSGreg Roach 336aee13b6dSGreg Roach $this->setBlockSetting($block_id, 'faqbody', $faqbody); 337aee13b6dSGreg Roach $this->setBlockSetting($block_id, 'header', $header); 338aee13b6dSGreg Roach $this->setBlockSetting($block_id, 'languages', implode(',', $languages)); 339aee13b6dSGreg Roach 340c1010edaSGreg Roach $url = route('module', [ 34126684e68SGreg Roach 'module' => $this->name(), 342c1010edaSGreg Roach 'action' => 'Admin', 343aa6f03bbSGreg Roach 'ged' => $tree->name(), 344c1010edaSGreg Roach ]); 345aee13b6dSGreg Roach 3466ccdf4f0SGreg Roach return redirect($url); 347aee13b6dSGreg Roach } 348aee13b6dSGreg Roach 349b998dbceSGreg Roach /** 350b6db7c1fSGreg Roach * @param Tree $tree 351b998dbceSGreg Roach * 3526ccdf4f0SGreg Roach * @return ResponseInterface 353b998dbceSGreg Roach */ 3546ccdf4f0SGreg Roach public function getShowAction(Tree $tree): ResponseInterface 355c1010edaSGreg Roach { 3568de50a4eSGreg Roach // Filter foreign languages. 35777654037SGreg Roach $faqs = $this->faqsForTree($tree) 3580b5fd0a6SGreg Roach ->filter(static function (stdClass $faq): bool { 359*22d65e5aSGreg Roach return $faq->languages === '' || in_array(WT_LOCALE, explode(',', $faq->languages), true); 3608de50a4eSGreg Roach }); 361aee13b6dSGreg Roach 362aee13b6dSGreg Roach return $this->viewResponse('modules/faq/show', [ 3638de50a4eSGreg Roach 'faqs' => $faqs, 364aee13b6dSGreg Roach 'title' => I18N::translate('Frequently asked questions'), 3658de50a4eSGreg Roach 'tree' => $tree, 366aee13b6dSGreg Roach ]); 367aee13b6dSGreg Roach } 36877654037SGreg Roach 36977654037SGreg Roach /** 37077654037SGreg Roach * @param Tree $tree 37177654037SGreg Roach * 37277654037SGreg Roach * @return Collection 37377654037SGreg Roach */ 37477654037SGreg Roach private function faqsForTree(Tree $tree): Collection 37577654037SGreg Roach { 37677654037SGreg Roach return DB::table('block') 37777654037SGreg Roach ->join('block_setting AS bs1', 'bs1.block_id', '=', 'block.block_id') 37877654037SGreg Roach ->join('block_setting AS bs2', 'bs2.block_id', '=', 'block.block_id') 37977654037SGreg Roach ->join('block_setting AS bs3', 'bs3.block_id', '=', 'block.block_id') 38026684e68SGreg Roach ->where('module_name', '=', $this->name()) 38177654037SGreg Roach ->where('bs1.setting_name', '=', 'header') 38277654037SGreg Roach ->where('bs2.setting_name', '=', 'faqbody') 38377654037SGreg Roach ->where('bs3.setting_name', '=', 'languages') 3840b5fd0a6SGreg Roach ->where(static function (Builder $query) use ($tree): void { 38577654037SGreg Roach $query 38677654037SGreg Roach ->whereNull('gedcom_id') 38777654037SGreg Roach ->orWhere('gedcom_id', '=', $tree->id()); 38877654037SGreg Roach }) 38977654037SGreg Roach ->orderBy('block_order') 39077654037SGreg Roach ->select(['block.block_id', 'block_order', 'gedcom_id', 'bs1.setting_value AS header', 'bs2.setting_value AS faqbody', 'bs3.setting_value AS languages']) 39177654037SGreg Roach ->get(); 39277654037SGreg Roach } 39377654037SGreg Roach 39477654037SGreg Roach /** 39577654037SGreg Roach * @param Tree $tree 39677654037SGreg Roach * @param string $language 39777654037SGreg Roach * 39877654037SGreg Roach * @return bool 39977654037SGreg Roach */ 40077654037SGreg Roach private function faqsExist(Tree $tree, string $language): bool 40177654037SGreg Roach { 40277654037SGreg Roach return DB::table('block') 40377654037SGreg Roach ->join('block_setting', 'block_setting.block_id', '=', 'block.block_id') 40426684e68SGreg Roach ->where('module_name', '=', $this->name()) 40577654037SGreg Roach ->where('setting_name', '=', 'languages') 4060b5fd0a6SGreg Roach ->where(static function (Builder $query) use ($tree): void { 40777654037SGreg Roach $query 40877654037SGreg Roach ->whereNull('gedcom_id') 40977654037SGreg Roach ->orWhere('gedcom_id', '=', $tree->id()); 41077654037SGreg Roach }) 41177654037SGreg Roach ->select(['setting_value AS languages']) 41277654037SGreg Roach ->get() 4130b5fd0a6SGreg Roach ->filter(static function (stdClass $faq) use ($language): bool { 4140b5fd0a6SGreg Roach return $faq->languages === '' || in_array($language, explode(',', $faq->languages), true); 41577654037SGreg Roach }) 41677654037SGreg Roach ->isNotEmpty(); 41777654037SGreg Roach } 4188c2e8227SGreg Roach} 419