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