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