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\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 'module.php?mod=' . $this->getName() . '&mod_action=admin_config'; 74 } 75 76 /** {@inheritdoc} */ 77 public function defaultTabOrder() { 78 return 55; 79 } 80 81 /** {@inheritdoc} */ 82 public function getTabContent() { 83 global $controller, $WT_TREE; 84 85 $block_ids = 86 Database::prepare( 87 "SELECT block_id" . 88 " FROM `##block`" . 89 " WHERE module_name=?" . 90 " AND xref=?" . 91 " AND gedcom_id=?" 92 )->execute([ 93 $this->getName(), 94 $controller->record->getXref(), 95 $controller->record->getTree()->getTreeId(), 96 ])->fetchOneColumn(); 97 98 $html = ''; 99 foreach ($block_ids as $block_id) { 100 // Only show this block for certain languages 101 $languages = $this->getBlockSetting($block_id, 'languages'); 102 if (!$languages || in_array(WT_LOCALE, explode(',', $languages))) { 103 $html .= '<div class="story_title descriptionbox center rela">' . $this->getBlockSetting($block_id, 'title') . '</div>'; 104 $html .= '<div class="story_body optionbox">' . $this->getBlockSetting($block_id, 'story_body') . '</div>'; 105 if (Auth::isEditor($WT_TREE)) { 106 $html .= '<div class="story_edit"><a href="module.php?mod=' . $this->getName() . '&mod_action=admin_edit&block_id=' . $block_id . '">'; 107 $html .= I18N::translate('Edit the story') . '</a></div>'; 108 } 109 } 110 } 111 if (Auth::isManager($WT_TREE) && !$html) { 112 $html .= '<div class="news_title center">' . $this->getTitle() . '</div>'; 113 $html .= '<div><a href="module.php?mod=' . $this->getName() . '&mod_action=admin_edit&xref=' . $controller->record->getXref() . '">'; 114 $html .= I18N::translate('Add a story') . '</a></div><br>'; 115 } 116 117 return $html; 118 } 119 120 /** {@inheritdoc} */ 121 public function hasTabContent() { 122 return $this->getTabContent() != ''; 123 } 124 125 /** {@inheritdoc} */ 126 public function isGrayedOut() { 127 global $controller; 128 129 $count_of_stories = 130 Database::prepare( 131 "SELECT COUNT(block_id)" . 132 " FROM `##block`" . 133 " WHERE module_name=?" . 134 " AND xref=?" . 135 " AND gedcom_id=?" 136 )->execute([ 137 $this->getName(), 138 $controller->record->getXref(), 139 $controller->record->getTree()->getTreeId(), 140 ])->fetchOne(); 141 142 return $count_of_stories == 0; 143 } 144 145 /** {@inheritdoc} */ 146 public function canLoadAjax() { 147 return false; 148 } 149 150 /** {@inheritdoc} */ 151 public function getPreLoadContent() { 152 return ''; 153 } 154 155 /** 156 * Show and process a form to edit a story. 157 */ 158 private function edit() { 159 global $WT_TREE; 160 161 if (Auth::isEditor($WT_TREE)) { 162 if (Filter::postBool('save') && Filter::checkCsrf()) { 163 $block_id = Filter::postInteger('block_id'); 164 if ($block_id) { 165 Database::prepare( 166 "UPDATE `##block` SET gedcom_id=?, xref=? WHERE block_id=?" 167 )->execute([Filter::postInteger('gedcom_id'), Filter::post('xref', WT_REGEX_XREF), $block_id]); 168 } else { 169 Database::prepare( 170 "INSERT INTO `##block` (gedcom_id, xref, module_name, block_order) VALUES (?, ?, ?, ?)" 171 )->execute([ 172 Filter::postInteger('gedcom_id'), 173 Filter::post('xref', WT_REGEX_XREF), 174 $this->getName(), 175 0, 176 ]); 177 $block_id = Database::getInstance()->lastInsertId(); 178 } 179 $this->setBlockSetting($block_id, 'title', Filter::post('title')); 180 $this->setBlockSetting($block_id, 'story_body', Filter::post('story_body')); 181 $languages = Filter::postArray('lang'); 182 $this->setBlockSetting($block_id, 'languages', implode(',', $languages)); 183 $this->config(); 184 } else { 185 $block_id = Filter::getInteger('block_id'); 186 187 $controller = new PageController; 188 if ($block_id) { 189 $controller->setPageTitle(I18N::translate('Edit the story')); 190 $title = $this->getBlockSetting($block_id, 'title'); 191 $story_body = $this->getBlockSetting($block_id, 'story_body'); 192 $xref = Database::prepare( 193 "SELECT xref FROM `##block` WHERE block_id=?" 194 )->execute([$block_id])->fetchOne(); 195 } else { 196 $controller->setPageTitle(I18N::translate('Add a story')); 197 $title = ''; 198 $story_body = ''; 199 $xref = Filter::get('xref', WT_REGEX_XREF); 200 } 201 $controller->pageHeader(); 202 if (Module::getModuleByName('ckeditor')) { 203 CkeditorModule::enableEditor($controller); 204 } 205 206 $individual = Individual::getInstance($xref, $WT_TREE); 207 208 echo Bootstrap4::breadcrumbs([ 209 'admin.php' => I18N::translate('Control panel'), 210 'admin_modules.php' => I18N::translate('Module administration'), 211 'module.php?mod=' . $this->getName() . '&mod_action=admin_config' => $this->getTitle(), 212 ], $controller->getPageTitle()); 213 ?> 214 215 <h1><?= $controller->getPageTitle() ?></h1> 216 217 <form class="form-horizontal" method="post" action="module.php?mod=<?= $this->getName() ?>&mod_action=admin_edit"> 218 <?= Filter::getCsrf() ?> 219 <input type="hidden" name="save" value="1"> 220 <input type="hidden" name="block_id" value="<?= $block_id ?>"> 221 <input type="hidden" name="gedcom_id" value="<?= $WT_TREE->getTreeId() ?>"> 222 223 <div class="row form-group"> 224 <label for="title" class="col-sm-3 col-form-label"> 225 <?= I18N::translate('Story title') ?> 226 </label> 227 <div class="col-sm-9"> 228 <input type="text" class="form-control" name="title" id="title" value="<?= Html::escape($title) ?>"> 229 </div> 230 </div> 231 232 <div class="row form-group"> 233 <label for="story_body" class="col-sm-3 col-form-label"> 234 <?= I18N::translate('Story') ?> 235 </label> 236 <div class="col-sm-9"> 237 <textarea name="story_body" id="story_body" class="html-edit form-control" rows="10"><?= Html::escape($story_body) ?></textarea> 238 </div> 239 </div> 240 241 <div class="row form-group"> 242 <label for="xref" class="col-sm-3 col-form-label"> 243 <?= I18N::translate('Individual') ?> 244 </label> 245 <div class="col-sm-9"> 246 <input data-autocomplete-type="INDI" type="text" name="xref" id="xref" size="4" value="<?= $xref ?>"> 247 <?php if ($individual): ?> 248 <?= $individual->formatList('span') ?> 249 <?php endif ?> 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="fa 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 'admin.php' => I18N::translate('Control panel'), 341 'admin_modules.php' => 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="fa fa-plus"></i> 360 <?= I18N::translate('Add a story') ?> 361 </a> 362 </p> 363 364 <table class="table table-bordered table-condensed"> 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 <?= Html::escape($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="<?= $individual->getHtmlUrl() ?>#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="fa fa-pencil"></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”?', Html::escape($this->getBlockSetting($story->block_id, 'title'))) ?>');" 398 > 399 <i class="fa fa-trash"></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="' . $indi->getHtmlUrl() . '#tab-stories">' . $story_title . '</a></td><td><a href="' . $indi->getHtmlUrl() . '#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