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