1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Menu; 25use Fisharebest\Webtrees\Services\HtmlService; 26use Fisharebest\Webtrees\Services\TreeService; 27use Fisharebest\Webtrees\Tree; 28use Illuminate\Database\Capsule\Manager as DB; 29use Illuminate\Database\Query\Builder; 30use Illuminate\Support\Collection; 31use Psr\Http\Message\ResponseInterface; 32use Psr\Http\Message\ServerRequestInterface; 33 34use function assert; 35use function redirect; 36use function route; 37 38/** 39 * Class FrequentlyAskedQuestionsModule 40 */ 41class FrequentlyAskedQuestionsModule extends AbstractModule implements ModuleConfigInterface, ModuleMenuInterface 42{ 43 use ModuleConfigTrait; 44 use ModuleMenuTrait; 45 46 /** @var HtmlService */ 47 private $html_service; 48 49 /** @var TreeService */ 50 private $tree_service; 51 52 /** 53 * FrequentlyAskedQuestionsModule constructor. 54 * 55 * @param HtmlService $html_service 56 * @param TreeService $tree_service 57 */ 58 public function __construct(HtmlService $html_service, TreeService $tree_service) 59 { 60 $this->html_service = $html_service; 61 $this->tree_service = $tree_service; 62 } 63 64 /** 65 * How should this module be identified in the control panel, etc.? 66 * 67 * @return string 68 */ 69 public function title(): string 70 { 71 /* I18N: Name of a module. Abbreviation for “Frequently Asked Questions” */ 72 return I18N::translate('FAQ'); 73 } 74 75 /** 76 * A sentence describing what this module does. 77 * 78 * @return string 79 */ 80 public function description(): string 81 { 82 /* I18N: Description of the “FAQ” module */ 83 return I18N::translate('A list of frequently asked questions and answers.'); 84 } 85 86 /** 87 * The default position for this menu. It can be changed in the control panel. 88 * 89 * @return int 90 */ 91 public function defaultMenuOrder(): int 92 { 93 return 8; 94 } 95 96 /** 97 * A menu, to be added to the main application menu. 98 * 99 * @param Tree $tree 100 * 101 * @return Menu|null 102 */ 103 public function getMenu(Tree $tree): ?Menu 104 { 105 if ($this->faqsExist($tree, I18N::languageTag())) { 106 return new Menu($this->title(), route('module', [ 107 'module' => $this->name(), 108 'action' => 'Show', 109 'tree' => $tree->name(), 110 ]), 'menu-faq'); 111 } 112 113 return null; 114 } 115 116 /** 117 * @param ServerRequestInterface $request 118 * 119 * @return ResponseInterface 120 */ 121 public function getAdminAction(ServerRequestInterface $request): ResponseInterface 122 { 123 $this->layout = 'layouts/administration'; 124 125 // This module can't run without a tree 126 $tree = $request->getAttribute('tree'); 127 128 if (!$tree instanceof Tree) { 129 $tree = $this->tree_service->all()->first(); 130 if ($tree instanceof Tree) { 131 return redirect(route('module', ['module' => $this->name(), 'action' => 'Admin', 'tree' => $tree->name()])); 132 } 133 134 return redirect(route(ControlPanel::class)); 135 } 136 137 $faqs = $this->faqsForTree($tree); 138 139 $min_block_order = DB::table('block') 140 ->where('module_name', '=', $this->name()) 141 ->where(static function (Builder $query) use ($tree): void { 142 $query 143 ->whereNull('gedcom_id') 144 ->orWhere('gedcom_id', '=', $tree->id()); 145 }) 146 ->min('block_order'); 147 148 $max_block_order = DB::table('block') 149 ->where('module_name', '=', $this->name()) 150 ->where(static function (Builder $query) use ($tree): void { 151 $query 152 ->whereNull('gedcom_id') 153 ->orWhere('gedcom_id', '=', $tree->id()); 154 }) 155 ->max('block_order'); 156 157 $title = I18N::translate('Frequently asked questions') . ' — ' . $tree->title(); 158 159 return $this->viewResponse('modules/faq/config', [ 160 'action' => route('module', ['module' => $this->name(), 'action' => 'Admin']), 161 'faqs' => $faqs, 162 'max_block_order' => $max_block_order, 163 'min_block_order' => $min_block_order, 164 'module' => $this->name(), 165 'title' => $title, 166 'tree' => $tree, 167 'tree_names' => $this->tree_service->titles(), 168 ]); 169 } 170 171 /** 172 * @param ServerRequestInterface $request 173 * 174 * @return ResponseInterface 175 */ 176 public function postAdminAction(ServerRequestInterface $request): ResponseInterface 177 { 178 $params = (array) $request->getParsedBody(); 179 180 return redirect(route('module', [ 181 'module' => $this->name(), 182 'action' => 'Admin', 183 'tree' => $params['tree'] ?? '', 184 ])); 185 } 186 187 /** 188 * @param ServerRequestInterface $request 189 * 190 * @return ResponseInterface 191 */ 192 public function postAdminDeleteAction(ServerRequestInterface $request): ResponseInterface 193 { 194 $block_id = (int) $request->getQueryParams()['block_id']; 195 196 DB::table('block_setting')->where('block_id', '=', $block_id)->delete(); 197 198 DB::table('block')->where('block_id', '=', $block_id)->delete(); 199 200 $url = route('module', [ 201 'module' => $this->name(), 202 'action' => 'Admin', 203 ]); 204 205 return redirect($url); 206 } 207 208 /** 209 * @param ServerRequestInterface $request 210 * 211 * @return ResponseInterface 212 */ 213 public function postAdminMoveDownAction(ServerRequestInterface $request): ResponseInterface 214 { 215 $block_id = (int) $request->getQueryParams()['block_id']; 216 217 $block_order = DB::table('block') 218 ->where('block_id', '=', $block_id) 219 ->value('block_order'); 220 221 $swap_block = DB::table('block') 222 ->where('module_name', '=', $this->name()) 223 ->where('block_order', '>', $block_order) 224 ->orderBy('block_order', 'asc') 225 ->first(); 226 227 if ($block_order !== null && $swap_block !== null) { 228 DB::table('block') 229 ->where('block_id', '=', $block_id) 230 ->update([ 231 'block_order' => $swap_block->block_order, 232 ]); 233 234 DB::table('block') 235 ->where('block_id', '=', $swap_block->block_id) 236 ->update([ 237 'block_order' => $block_order, 238 ]); 239 } 240 241 return response(); 242 } 243 244 /** 245 * @param ServerRequestInterface $request 246 * 247 * @return ResponseInterface 248 */ 249 public function postAdminMoveUpAction(ServerRequestInterface $request): ResponseInterface 250 { 251 $block_id = (int) $request->getQueryParams()['block_id']; 252 253 $block_order = DB::table('block') 254 ->where('block_id', '=', $block_id) 255 ->value('block_order'); 256 257 $swap_block = DB::table('block') 258 ->where('module_name', '=', $this->name()) 259 ->where('block_order', '<', $block_order) 260 ->orderBy('block_order', 'desc') 261 ->first(); 262 263 if ($block_order !== null && $swap_block !== null) { 264 DB::table('block') 265 ->where('block_id', '=', $block_id) 266 ->update([ 267 'block_order' => $swap_block->block_order, 268 ]); 269 270 DB::table('block') 271 ->where('block_id', '=', $swap_block->block_id) 272 ->update([ 273 'block_order' => $block_order, 274 ]); 275 } 276 277 return response(); 278 } 279 280 /** 281 * @param ServerRequestInterface $request 282 * 283 * @return ResponseInterface 284 */ 285 public function getAdminEditAction(ServerRequestInterface $request): ResponseInterface 286 { 287 $this->layout = 'layouts/administration'; 288 289 $block_id = (int) ($request->getQueryParams()['block_id'] ?? 0); 290 291 if ($block_id === 0) { 292 // Creating a new faq 293 $header = ''; 294 $body = ''; 295 $gedcom_id = null; 296 $block_order = 1 + (int) DB::table('block')->where('module_name', '=', $this->name())->max('block_order'); 297 298 $languages = []; 299 300 $title = I18N::translate('Add an FAQ'); 301 } else { 302 // Editing an existing faq 303 $header = $this->getBlockSetting($block_id, 'header'); 304 $body = $this->getBlockSetting($block_id, 'faqbody'); 305 $gedcom_id = DB::table('block')->where('block_id', '=', $block_id)->value('gedcom_id'); 306 $block_order = DB::table('block')->where('block_id', '=', $block_id)->value('block_order'); 307 308 $languages = explode(',', $this->getBlockSetting($block_id, 'languages')); 309 310 $title = I18N::translate('Edit the FAQ'); 311 } 312 313 $gedcom_ids = $this->tree_service->all() 314 ->mapWithKeys(static function (Tree $tree): array { 315 return [$tree->id() => $tree->title()]; 316 }) 317 ->all(); 318 319 $gedcom_ids = ['' => I18N::translate('All')] + $gedcom_ids; 320 321 return $this->viewResponse('modules/faq/edit', [ 322 'block_id' => $block_id, 323 'block_order' => $block_order, 324 'header' => $header, 325 'body' => $body, 326 'languages' => $languages, 327 'title' => $title, 328 'gedcom_id' => $gedcom_id, 329 'gedcom_ids' => $gedcom_ids, 330 ]); 331 } 332 333 /** 334 * @param ServerRequestInterface $request 335 * 336 * @return ResponseInterface 337 */ 338 public function postAdminEditAction(ServerRequestInterface $request): ResponseInterface 339 { 340 $block_id = (int) ($request->getQueryParams()['block_id'] ?? 0); 341 342 $params = (array) $request->getParsedBody(); 343 344 $body = $params['body']; 345 $header = $params['header']; 346 $languages = $params['languages'] ?? []; 347 $gedcom_id = $params['gedcom_id']; 348 $block_order = (int) $params['block_order']; 349 350 if ($gedcom_id === '') { 351 $gedcom_id = null; 352 } 353 354 $body = $this->html_service->sanitize($body); 355 $header = $this->html_service->sanitize($header); 356 357 if ($block_id !== 0) { 358 DB::table('block') 359 ->where('block_id', '=', $block_id) 360 ->update([ 361 'gedcom_id' => $gedcom_id, 362 'block_order' => $block_order, 363 ]); 364 } else { 365 DB::table('block')->insert([ 366 'gedcom_id' => $gedcom_id, 367 'module_name' => $this->name(), 368 'block_order' => $block_order, 369 ]); 370 371 $block_id = (int) DB::connection()->getPdo()->lastInsertId(); 372 } 373 374 $this->setBlockSetting($block_id, 'faqbody', $body); 375 $this->setBlockSetting($block_id, 'header', $header); 376 $this->setBlockSetting($block_id, 'languages', implode(',', $languages)); 377 378 $url = route('module', [ 379 'module' => $this->name(), 380 'action' => 'Admin', 381 ]); 382 383 return redirect($url); 384 } 385 386 /** 387 * @param ServerRequestInterface $request 388 * 389 * @return ResponseInterface 390 */ 391 public function getShowAction(ServerRequestInterface $request): ResponseInterface 392 { 393 $tree = $request->getAttribute('tree'); 394 assert($tree instanceof Tree); 395 396 // Filter foreign languages. 397 $faqs = $this->faqsForTree($tree) 398 ->filter(static function (object $faq): bool { 399 return $faq->languages === '' || in_array(I18N::languageTag(), explode(',', $faq->languages), true); 400 }); 401 402 return $this->viewResponse('modules/faq/show', [ 403 'faqs' => $faqs, 404 'title' => I18N::translate('Frequently asked questions'), 405 'tree' => $tree, 406 ]); 407 } 408 409 /** 410 * @param Tree $tree 411 * 412 * @return Collection<object> 413 */ 414 private function faqsForTree(Tree $tree): Collection 415 { 416 return DB::table('block') 417 ->join('block_setting AS bs1', 'bs1.block_id', '=', 'block.block_id') 418 ->join('block_setting AS bs2', 'bs2.block_id', '=', 'block.block_id') 419 ->join('block_setting AS bs3', 'bs3.block_id', '=', 'block.block_id') 420 ->where('module_name', '=', $this->name()) 421 ->where('bs1.setting_name', '=', 'header') 422 ->where('bs2.setting_name', '=', 'faqbody') 423 ->where('bs3.setting_name', '=', 'languages') 424 ->where(static function (Builder $query) use ($tree): void { 425 $query 426 ->whereNull('gedcom_id') 427 ->orWhere('gedcom_id', '=', $tree->id()); 428 }) 429 ->orderBy('block_order') 430 ->select(['block.block_id', 'block_order', 'gedcom_id', 'bs1.setting_value AS header', 'bs2.setting_value AS faqbody', 'bs3.setting_value AS languages']) 431 ->get(); 432 } 433 434 /** 435 * @param Tree $tree 436 * @param string $language 437 * 438 * @return bool 439 */ 440 private function faqsExist(Tree $tree, string $language): bool 441 { 442 return DB::table('block') 443 ->join('block_setting', 'block_setting.block_id', '=', 'block.block_id') 444 ->where('module_name', '=', $this->name()) 445 ->where('setting_name', '=', 'languages') 446 ->where(static function (Builder $query) use ($tree): void { 447 $query 448 ->whereNull('gedcom_id') 449 ->orWhere('gedcom_id', '=', $tree->id()); 450 }) 451 ->select(['setting_value AS languages']) 452 ->get() 453 ->filter(static function (object $faq) use ($language): bool { 454 return $faq->languages === '' || in_array($language, explode(',', $faq->languages), true); 455 }) 456 ->isNotEmpty(); 457 } 458} 459