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