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 { 139*eb235819SGreg Roach $block_id = (int) $request->getQueryParams()['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 { 162*eb235819SGreg Roach $block_id = (int) $request->getQueryParams()['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()) 170*eb235819SGreg Roach ->where('block_order', '=', 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 { 211*eb235819SGreg Roach $block_id = (int) $request->getQueryParams()['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 262*eb235819SGreg Roach $block_id = (int) ($request->getQueryParams()['block_id'] ?? 0); 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 { 312*eb235819SGreg Roach $block_id = (int) ($request->getQueryParams()['block_id'] ?? 0); 313*eb235819SGreg Roach 314*eb235819SGreg Roach $params = $request->getParsedBody(); 315*eb235819SGreg Roach 316*eb235819SGreg Roach $faqbody = $params['faqbody']; 317*eb235819SGreg Roach $header = $params['header']; 318*eb235819SGreg Roach $languages = $params['languages'] ?? []; 319*eb235819SGreg Roach $gedcom_id = (int) $params['gedcom_id'] ?: null; 320*eb235819SGreg Roach $block_order = (int) $params['block_order']; 321aee13b6dSGreg Roach 322aee13b6dSGreg Roach if ($block_id !== 0) { 32377654037SGreg Roach DB::table('block') 32477654037SGreg Roach ->where('block_id', '=', $block_id) 32577654037SGreg Roach ->update([ 32677654037SGreg Roach 'gedcom_id' => $gedcom_id, 32777654037SGreg Roach 'block_order' => $block_order, 328aee13b6dSGreg Roach ]); 329aee13b6dSGreg Roach } else { 33077654037SGreg Roach DB::table('block')->insert([ 33177654037SGreg Roach 'gedcom_id' => $gedcom_id, 33226684e68SGreg Roach 'module_name' => $this->name(), 33377654037SGreg Roach 'block_order' => $block_order, 334aee13b6dSGreg Roach ]); 335aee13b6dSGreg Roach 33677654037SGreg Roach $block_id = (int) DB::connection()->getPdo()->lastInsertId(); 337aee13b6dSGreg Roach } 338aee13b6dSGreg Roach 339aee13b6dSGreg Roach $this->setBlockSetting($block_id, 'faqbody', $faqbody); 340aee13b6dSGreg Roach $this->setBlockSetting($block_id, 'header', $header); 341aee13b6dSGreg Roach $this->setBlockSetting($block_id, 'languages', implode(',', $languages)); 342aee13b6dSGreg Roach 343c1010edaSGreg Roach $url = route('module', [ 34426684e68SGreg Roach 'module' => $this->name(), 345c1010edaSGreg Roach 'action' => 'Admin', 346aa6f03bbSGreg Roach 'ged' => $tree->name(), 347c1010edaSGreg Roach ]); 348aee13b6dSGreg Roach 3496ccdf4f0SGreg Roach return redirect($url); 350aee13b6dSGreg Roach } 351aee13b6dSGreg Roach 352b998dbceSGreg Roach /** 353b6db7c1fSGreg Roach * @param Tree $tree 354b998dbceSGreg Roach * 3556ccdf4f0SGreg Roach * @return ResponseInterface 356b998dbceSGreg Roach */ 3576ccdf4f0SGreg Roach public function getShowAction(Tree $tree): ResponseInterface 358c1010edaSGreg Roach { 3598de50a4eSGreg Roach // Filter foreign languages. 36077654037SGreg Roach $faqs = $this->faqsForTree($tree) 3610b5fd0a6SGreg Roach ->filter(static function (stdClass $faq): bool { 36222d65e5aSGreg Roach return $faq->languages === '' || in_array(WT_LOCALE, explode(',', $faq->languages), true); 3638de50a4eSGreg Roach }); 364aee13b6dSGreg Roach 365aee13b6dSGreg Roach return $this->viewResponse('modules/faq/show', [ 3668de50a4eSGreg Roach 'faqs' => $faqs, 367aee13b6dSGreg Roach 'title' => I18N::translate('Frequently asked questions'), 3688de50a4eSGreg Roach 'tree' => $tree, 369aee13b6dSGreg Roach ]); 370aee13b6dSGreg Roach } 37177654037SGreg Roach 37277654037SGreg Roach /** 37377654037SGreg Roach * @param Tree $tree 37477654037SGreg Roach * 37577654037SGreg Roach * @return Collection 37677654037SGreg Roach */ 37777654037SGreg Roach private function faqsForTree(Tree $tree): Collection 37877654037SGreg Roach { 37977654037SGreg Roach return DB::table('block') 38077654037SGreg Roach ->join('block_setting AS bs1', 'bs1.block_id', '=', 'block.block_id') 38177654037SGreg Roach ->join('block_setting AS bs2', 'bs2.block_id', '=', 'block.block_id') 38277654037SGreg Roach ->join('block_setting AS bs3', 'bs3.block_id', '=', 'block.block_id') 38326684e68SGreg Roach ->where('module_name', '=', $this->name()) 38477654037SGreg Roach ->where('bs1.setting_name', '=', 'header') 38577654037SGreg Roach ->where('bs2.setting_name', '=', 'faqbody') 38677654037SGreg Roach ->where('bs3.setting_name', '=', 'languages') 3870b5fd0a6SGreg Roach ->where(static function (Builder $query) use ($tree): void { 38877654037SGreg Roach $query 38977654037SGreg Roach ->whereNull('gedcom_id') 39077654037SGreg Roach ->orWhere('gedcom_id', '=', $tree->id()); 39177654037SGreg Roach }) 39277654037SGreg Roach ->orderBy('block_order') 39377654037SGreg Roach ->select(['block.block_id', 'block_order', 'gedcom_id', 'bs1.setting_value AS header', 'bs2.setting_value AS faqbody', 'bs3.setting_value AS languages']) 39477654037SGreg Roach ->get(); 39577654037SGreg Roach } 39677654037SGreg Roach 39777654037SGreg Roach /** 39877654037SGreg Roach * @param Tree $tree 39977654037SGreg Roach * @param string $language 40077654037SGreg Roach * 40177654037SGreg Roach * @return bool 40277654037SGreg Roach */ 40377654037SGreg Roach private function faqsExist(Tree $tree, string $language): bool 40477654037SGreg Roach { 40577654037SGreg Roach return DB::table('block') 40677654037SGreg Roach ->join('block_setting', 'block_setting.block_id', '=', 'block.block_id') 40726684e68SGreg Roach ->where('module_name', '=', $this->name()) 40877654037SGreg Roach ->where('setting_name', '=', 'languages') 4090b5fd0a6SGreg Roach ->where(static function (Builder $query) use ($tree): void { 41077654037SGreg Roach $query 41177654037SGreg Roach ->whereNull('gedcom_id') 41277654037SGreg Roach ->orWhere('gedcom_id', '=', $tree->id()); 41377654037SGreg Roach }) 41477654037SGreg Roach ->select(['setting_value AS languages']) 41577654037SGreg Roach ->get() 4160b5fd0a6SGreg Roach ->filter(static function (stdClass $faq) use ($language): bool { 4170b5fd0a6SGreg Roach return $faq->languages === '' || in_array($language, explode(',', $faq->languages), true); 41877654037SGreg Roach }) 41977654037SGreg Roach ->isNotEmpty(); 42077654037SGreg Roach } 4218c2e8227SGreg Roach} 422