1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 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 */ 16namespace Fisharebest\Webtrees\Module; 17 18use Fisharebest\Webtrees\Auth; 19use Fisharebest\Webtrees\Bootstrap4; 20use Fisharebest\Webtrees\Controller\PageController; 21use Fisharebest\Webtrees\Database; 22use Fisharebest\Webtrees\Filter; 23use Fisharebest\Webtrees\Functions\FunctionsEdit; 24use Fisharebest\Webtrees\Html; 25use Fisharebest\Webtrees\I18N; 26use Fisharebest\Webtrees\Individual; 27use Fisharebest\Webtrees\Menu; 28use Fisharebest\Webtrees\Module; 29use Fisharebest\Webtrees\Tree; 30 31/** 32 * Class StoriesModule 33 */ 34class StoriesModule extends AbstractModule implements ModuleTabInterface, ModuleConfigInterface, ModuleMenuInterface { 35 /** {@inheritdoc} */ 36 public function getTitle() { 37 return /* I18N: Name of a module */ I18N::translate('Stories'); 38 } 39 40 /** {@inheritdoc} */ 41 public function getDescription() { 42 return /* I18N: Description of the “Stories” module */ I18N::translate('Add narrative stories to individuals in the family tree.'); 43 } 44 45 /** 46 * This is a general purpose hook, allowing modules to respond to routes 47 * of the form module.php?mod=FOO&mod_action=BAR 48 * 49 * @param string $mod_action 50 */ 51 public function modAction($mod_action) { 52 switch ($mod_action) { 53 case 'admin_edit': 54 $this->edit(); 55 break; 56 case 'admin_delete': 57 $this->delete(); 58 $this->config(); 59 break; 60 case 'admin_config': 61 $this->config(); 62 break; 63 case 'show_list': 64 $this->showList(); 65 break; 66 default: 67 http_response_code(404); 68 } 69 } 70 71 /** {@inheritdoc} */ 72 public function getConfigLink() { 73 return Html::url('module.php', [ 74 'mod' => $this->getName(), 75 'mod_action' => 'admin_config', 76 ]); 77 } 78 79 /** {@inheritdoc} */ 80 public function defaultTabOrder() { 81 return 55; 82 } 83 84 /** {@inheritdoc} */ 85 public function getTabContent() { 86 global $controller, $WT_TREE; 87 88 $block_ids = 89 Database::prepare( 90 "SELECT block_id" . 91 " FROM `##block`" . 92 " WHERE module_name=?" . 93 " AND xref=?" . 94 " AND gedcom_id=?" 95 )->execute([ 96 $this->getName(), 97 $controller->record->getXref(), 98 $controller->record->getTree()->getTreeId(), 99 ])->fetchOneColumn(); 100 101 $html = ''; 102 foreach ($block_ids as $block_id) { 103 // Only show this block for certain languages 104 $languages = $this->getBlockSetting($block_id, 'languages'); 105 if (!$languages || in_array(WT_LOCALE, explode(',', $languages))) { 106 $html .= '<div class="story_title descriptionbox center rela">' . $this->getBlockSetting($block_id, 'title') . '</div>'; 107 $html .= '<div class="story_body optionbox">' . $this->getBlockSetting($block_id, 'story_body') . '</div>'; 108 if (Auth::isEditor($WT_TREE)) { 109 $html .= '<div class="story_edit"><a href="module.php?mod=' . $this->getName() . '&mod_action=admin_edit&block_id=' . $block_id . '">'; 110 $html .= I18N::translate('Edit the story') . '</a></div>'; 111 } 112 } 113 } 114 if (Auth::isManager($WT_TREE) && !$html) { 115 $html .= '<div class="news_title center">' . $this->getTitle() . '</div>'; 116 $html .= '<div><a href="module.php?mod=' . $this->getName() . '&mod_action=admin_edit&xref=' . $controller->record->getXref() . '">'; 117 $html .= I18N::translate('Add a story') . '</a></div><br>'; 118 } 119 120 return $html; 121 } 122 123 /** {@inheritdoc} */ 124 public function hasTabContent() { 125 return $this->getTabContent() != ''; 126 } 127 128 /** {@inheritdoc} */ 129 public function isGrayedOut() { 130 global $controller; 131 132 $count_of_stories = 133 Database::prepare( 134 "SELECT COUNT(block_id)" . 135 " FROM `##block`" . 136 " WHERE module_name=?" . 137 " AND xref=?" . 138 " AND gedcom_id=?" 139 )->execute([ 140 $this->getName(), 141 $controller->record->getXref(), 142 $controller->record->getTree()->getTreeId(), 143 ])->fetchOne(); 144 145 return $count_of_stories == 0; 146 } 147 148 /** {@inheritdoc} */ 149 public function canLoadAjax() { 150 return false; 151 } 152 153 /** {@inheritdoc} */ 154 public function getPreLoadContent() { 155 return ''; 156 } 157 158 /** 159 * Show and process a form to edit a story. 160 */ 161 private function edit() { 162 global $WT_TREE; 163 164 if (Auth::isEditor($WT_TREE)) { 165 if (Filter::postBool('save') && Filter::checkCsrf()) { 166 $block_id = Filter::postInteger('block_id'); 167 if ($block_id) { 168 Database::prepare( 169 "UPDATE `##block` SET gedcom_id=?, xref=? WHERE block_id=?" 170 )->execute([Filter::postInteger('gedcom_id'), Filter::post('xref', WT_REGEX_XREF), $block_id]); 171 } else { 172 Database::prepare( 173 "INSERT INTO `##block` (gedcom_id, xref, module_name, block_order) VALUES (?, ?, ?, ?)" 174 )->execute([ 175 Filter::postInteger('gedcom_id'), 176 Filter::post('xref', WT_REGEX_XREF), 177 $this->getName(), 178 0, 179 ]); 180 $block_id = Database::getInstance()->lastInsertId(); 181 } 182 $this->setBlockSetting($block_id, 'title', Filter::post('title')); 183 $this->setBlockSetting($block_id, 'story_body', Filter::post('story_body')); 184 $languages = Filter::postArray('lang'); 185 $this->setBlockSetting($block_id, 'languages', implode(',', $languages)); 186 $this->config(); 187 } else { 188 $block_id = Filter::getInteger('block_id'); 189 190 $controller = new PageController; 191 if ($block_id) { 192 $controller->setPageTitle(I18N::translate('Edit the story')); 193 $title = $this->getBlockSetting($block_id, 'title'); 194 $story_body = $this->getBlockSetting($block_id, 'story_body'); 195 $xref = Database::prepare( 196 "SELECT xref FROM `##block` WHERE block_id=?" 197 )->execute([$block_id])->fetchOne(); 198 } else { 199 $controller->setPageTitle(I18N::translate('Add a story')); 200 $title = ''; 201 $story_body = ''; 202 $xref = Filter::get('xref', WT_REGEX_XREF); 203 } 204 $controller->pageHeader(); 205 if (Module::getModuleByName('ckeditor')) { 206 CkeditorModule::enableEditor($controller); 207 } 208 209 $individual = Individual::getInstance($xref, $WT_TREE); 210 211 echo Bootstrap4::breadcrumbs([ 212 route('admin-control-panel') => I18N::translate('Control panel'), 213 route('admin-modules') => I18N::translate('Module administration'), 214 'module.php?mod=' . $this->getName() . '&mod_action=admin_config' => $this->getTitle(), 215 ], $controller->getPageTitle()); 216 ?> 217 218 <h1><?= $controller->getPageTitle() ?></h1> 219 220 <form class="form-horizontal" method="post" action="module.php?mod=<?= $this->getName() ?>&mod_action=admin_edit"> 221 <?= Filter::getCsrf() ?> 222 <input type="hidden" name="save" value="1"> 223 <input type="hidden" name="block_id" value="<?= $block_id ?>"> 224 <input type="hidden" name="gedcom_id" value="<?= $WT_TREE->getTreeId() ?>"> 225 226 <div class="row form-group"> 227 <label for="title" class="col-sm-3 col-form-label"> 228 <?= I18N::translate('Story title') ?> 229 </label> 230 <div class="col-sm-9"> 231 <input type="text" class="form-control" name="title" id="title" value="<?= e($title) ?>"> 232 </div> 233 </div> 234 235 <div class="row form-group"> 236 <label for="story_body" class="col-sm-3 col-form-label"> 237 <?= I18N::translate('Story') ?> 238 </label> 239 <div class="col-sm-9"> 240 <textarea name="story_body" id="story_body" class="html-edit form-control" rows="10"><?= e($story_body) ?></textarea> 241 </div> 242 </div> 243 244 <div class="row form-group"> 245 <label for="xref" class="col-sm-3 col-form-label"> 246 <?= I18N::translate('Individual') ?> 247 </label> 248 <div class="col-sm-9"> 249 <?= FunctionsEdit::formControlIndividual($individual, ['id' => 'xref', 'name' => 'xref']) ?> 250 </div> 251 </div> 252 253 <div class="row form-group"> 254 <label for="xref" class="col-sm-3 col-form-label"> 255 <?= I18N::translate('Show this block for which languages') ?> 256 </label> 257 <div class="col-sm-9"> 258 <?= FunctionsEdit::editLanguageCheckboxes('lang', explode(',', $this->getBlockSetting($block_id, 'languages'))) ?> 259 </div> 260 </div> 261 262 <div class="row form-group"> 263 <div class="offset-sm-3 col-sm-9"> 264 <button type="submit" class="btn btn-primary"> 265 <i class="fas fa-check"></i> 266 <?= I18N::translate('save') ?> 267 </button> 268 </div> 269 </div> 270 271 </form> 272 <?php 273 } 274 } else { 275 header('Location: index.php'); 276 } 277 } 278 279 /** 280 * Respond to a request to delete a story. 281 */ 282 private function delete() { 283 global $WT_TREE; 284 285 if (Auth::isEditor($WT_TREE)) { 286 $block_id = Filter::getInteger('block_id'); 287 288 Database::prepare( 289 "DELETE FROM `##block_setting` WHERE block_id=?" 290 )->execute([$block_id]); 291 292 Database::prepare( 293 "DELETE FROM `##block` WHERE block_id=?" 294 )->execute([$block_id]); 295 } else { 296 header('Location: index.php'); 297 exit; 298 } 299 } 300 301 /** 302 * The admin view - list, create, edit, delete stories. 303 */ 304 private function config() { 305 global $WT_TREE; 306 307 $controller = new PageController; 308 $controller 309 ->restrictAccess(Auth::isAdmin()) 310 ->setPageTitle($this->getTitle()) 311 ->pageHeader() 312 ->addInlineJavascript(' 313 $("#story_table").dataTable({ 314 ' . I18N::datatablesI18N() . ', 315 autoWidth: false, 316 paging: true, 317 pagingType: "full_numbers", 318 lengthChange: true, 319 filter: true, 320 info: true, 321 sorting: [[0,"asc"]], 322 columns: [ 323 /* 0-name */ null, 324 /* 1-NAME */ null, 325 /* 2-NAME */ { sortable:false }, 326 /* 3-NAME */ { sortable:false } 327 ] 328 }); 329 '); 330 331 $stories = Database::prepare( 332 "SELECT block_id, xref" . 333 " FROM `##block` b" . 334 " WHERE module_name=?" . 335 " AND gedcom_id=?" . 336 " ORDER BY xref" 337 )->execute([$this->getName(), $WT_TREE->getTreeId()])->fetchAll(); 338 339 echo Bootstrap4::breadcrumbs([ 340 route('admin-control-panel') => I18N::translate('Control panel'), 341 route('admin-modules') => I18N::translate('Module administration'), 342 ], $controller->getPageTitle()); 343 ?> 344 345 <h1><?= $controller->getPageTitle() ?></h1> 346 347 <form class="form form-inline"> 348 <label for="ged" class="sr-only"> 349 <?= I18N::translate('Family tree') ?> 350 </label> 351 <input type="hidden" name="mod" value="<?= $this->getName() ?>"> 352 <input type="hidden" name="mod_action" value="admin_config"> 353 <?= Bootstrap4::select(Tree::getNameList(), $WT_TREE->getName(), ['id' => 'ged', 'name' => 'ged']) ?> 354 <input type="submit" class="btn btn-primary" value="<?= I18N::translate('show') ?>"> 355 </form> 356 357 <p> 358 <a href="module.php?mod=<?= $this->getName() ?>&mod_action=admin_edit" class="btn btn-default"> 359 <i class="fas fa-plus"></i> 360 <?= I18N::translate('Add a story') ?> 361 </a> 362 </p> 363 364 <table class="table table-bordered table-sm"> 365 <thead> 366 <tr> 367 <th><?= I18N::translate('Story title') ?></th> 368 <th><?= I18N::translate('Individual') ?></th> 369 <th><?= I18N::translate('Edit') ?></th> 370 <th><?= I18N::translate('Delete') ?></th> 371 </tr> 372 </thead> 373 <tbody> 374 <?php foreach ($stories as $story): ?> 375 <tr> 376 <td> 377 <?= e($this->getBlockSetting($story->block_id, 'title')) ?> 378 </td> 379 <td> 380 <?php $individual = Individual::getInstance($story->xref, $WT_TREE) ?> 381 <?php if ($individual): ?> 382 <a href="<?= e($individual->url()) ?>#tab-stories"> 383 <?= $individual->getFullName() ?> 384 </a> 385 <?php else: ?> 386 <?= $story->xref ?> 387 <?php endif ?> 388 </td> 389 <td> 390 <a href="module.php?mod=<?= $this->getName() ?>&mod_action=admin_edit&block_id=<?= $story->block_id ?>"> 391 <i class="fas fa-pencil-alt"></i> <?= I18N::translate('Edit') ?> 392 </a> 393 </td> 394 <td> 395 <a 396 href="module.php?mod=<?= $this->getName() ?>&mod_action=admin_delete&block_id=<?= $story->block_id ?>" 397 onclick="return confirm('<?= I18N::translate('Are you sure you want to delete “%s”?', e($this->getBlockSetting($story->block_id, 'title'))) ?>');" 398 > 399 <i class="fas fa-trash-alt"></i> <?= I18N::translate('Delete') ?> 400 </a> 401 </td> 402 </tr> 403 <?php endforeach ?> 404 </tbody> 405 </table> 406 <?php 407 } 408 409 /** 410 * Show the list of stories 411 */ 412 private function showList() { 413 global $controller, $WT_TREE; 414 415 $controller = new PageController; 416 $controller 417 ->setPageTitle($this->getTitle()) 418 ->pageHeader() 419 ->addInlineJavascript(' 420 $("#story_table").dataTable({ 421 dom: \'<"H"pf<"dt-clear">irl>t<"F"pl>\', 422 ' . I18N::datatablesI18N() . ', 423 autoWidth: false, 424 paging: true, 425 pagingType: "full_numbers", 426 lengthChange: true, 427 filter: true, 428 info: true, 429 sorting: [[0,"asc"]], 430 columns: [ 431 /* 0-name */ null, 432 /* 1-NAME */ null 433 ] 434 }); 435 '); 436 437 $stories = Database::prepare( 438 "SELECT block_id, xref" . 439 " FROM `##block` b" . 440 " WHERE module_name=?" . 441 " AND gedcom_id=?" . 442 " ORDER BY xref" 443 )->execute([$this->getName(), $WT_TREE->getTreeId()])->fetchAll(); 444 445 echo '<h2 class="wt-page-title">', I18N::translate('Stories'), '</h2>'; 446 if (count($stories) > 0) { 447 echo '<table id="story_table" class="width100">'; 448 echo '<thead><tr> 449 <th>', I18N::translate('Story title'), '</th> 450 <th>', I18N::translate('Individual'), '</th> 451 </tr></thead> 452 <tbody>'; 453 foreach ($stories as $story) { 454 $indi = Individual::getInstance($story->xref, $WT_TREE); 455 $story_title = $this->getBlockSetting($story->block_id, 'title'); 456 $languages = $this->getBlockSetting($story->block_id, 'languages'); 457 if (!$languages || in_array(WT_LOCALE, explode(',', $languages))) { 458 if ($indi) { 459 if ($indi->canShow()) { 460 echo '<tr><td><a href="' . e($indi->url()) . '#tab-stories">' . $story_title . '</a></td><td><a href="' . e($indi->url()) . '#tab-stories">' . $indi->getFullName() . '</a></td></tr>'; 461 } 462 } else { 463 echo '<tr><td>', $story_title, '</td><td class="error">', $story->xref, '</td></tr>'; 464 } 465 } 466 } 467 echo '</tbody></table>'; 468 } 469 } 470 471 /** 472 * The user can re-order menus. Until they do, they are shown in this order. 473 * 474 * @return int 475 */ 476 public function defaultMenuOrder() { 477 return 30; 478 } 479 480 /** 481 * What is the default access level for this module? 482 * 483 * Some modules are aimed at admins or managers, and are not generally shown to users. 484 * 485 * @return int 486 */ 487 public function defaultAccessLevel() { 488 return Auth::PRIV_HIDE; 489 } 490 491 /** 492 * A menu, to be added to the main application menu. 493 * 494 * @return Menu|null 495 */ 496 public function getMenu() { 497 $menu = new Menu($this->getTitle(), 'module.php?mod=' . $this->getName() . '&mod_action=show_list', 'menu-story'); 498 499 return $menu; 500 } 501} 502