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