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