. */ namespace Fisharebest\Webtrees\Module; use Fisharebest\Webtrees\Auth; use Fisharebest\Webtrees\Bootstrap4; use Fisharebest\Webtrees\Controller\PageController; use Fisharebest\Webtrees\Database; use Fisharebest\Webtrees\Filter; use Fisharebest\Webtrees\Functions\FunctionsEdit; use Fisharebest\Webtrees\Html; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Individual; use Fisharebest\Webtrees\Menu; use Fisharebest\Webtrees\Module; use Fisharebest\Webtrees\Tree; use stdClass; /** * Class StoriesModule */ class StoriesModule extends AbstractModule implements ModuleTabInterface, ModuleConfigInterface, ModuleMenuInterface { /** {@inheritdoc} */ public function getTitle() { return /* I18N: Name of a module */ I18N::translate('Stories'); } /** {@inheritdoc} */ public function getDescription() { return /* I18N: Description of the “Stories” module */ I18N::translate('Add narrative stories to individuals in the family tree.'); } /** * This is a general purpose hook, allowing modules to respond to routes * of the form module.php?mod=FOO&mod_action=BAR * * @param string $mod_action */ public function modAction($mod_action) { switch ($mod_action) { case 'admin_edit': $this->edit(); break; case 'admin_delete': $this->delete(); $this->config(); break; case 'admin_config': $this->config(); break; case 'show_list': $this->showList(); break; default: http_response_code(404); } } /** {@inheritdoc} */ public function getConfigLink() { return Html::url('module.php', [ 'mod' => $this->getName(), 'mod_action' => 'admin_config', ]); } /** {@inheritdoc} */ public function defaultTabOrder() { return 55; } /** {@inheritdoc} */ public function getTabContent(Individual $individual) { return view('tabs/stories', [ 'is_editor' => Auth::isEditor($individual->getTree()), 'is_manager' => Auth::isManager($individual->getTree()), 'individual' => $individual, 'stories' => $this->getStoriesForIndividual($individual), ]); } /** {@inheritdoc} */ public function hasTabContent(Individual $individual) { return Auth::isManager($individual->getTree() )|| !empty($this->getStoriesForIndividual($individual)); } /** {@inheritdoc} */ public function isGrayedOut(Individual $individual) { return !empty($this->getStoriesForIndividual($individual)); } /** {@inheritdoc} */ public function canLoadAjax() { return false; } /** * @param Individual $individual * * @return stdClass[] */ private function getStoriesForIndividual(Individual $individual): array { $block_ids = Database::prepare( "SELECT SQL_CACHE block_id" . " FROM `##block`" . " WHERE module_name = :module_name" . " AND xref = :xref" . " AND gedcom_id = :tree_id" )->execute([ 'module_name' => $this->getName(), 'xref' => $individual->getXref(), 'tree_id' => $individual->getTree()->getTreeId(), ])->fetchOneColumn(); $stories = []; foreach ($block_ids as $block_id) { // Only show this block for certain languages $languages = $this->getBlockSetting($block_id, 'languages', ''); if ($languages === '' || in_array(WT_LOCALE, explode(',', $languages))) { $stories[] = (object) [ 'block_id' => $block_id, 'title' => $this->getBlockSetting($block_id, 'title'), 'body' => $this->getBlockSetting($block_id, 'story_body'), ]; } } return $stories; } /** * Show and process a form to edit a story. */ private function edit() { global $WT_TREE; if (Auth::isEditor($WT_TREE)) { if (Filter::postBool('save') && Filter::checkCsrf()) { $block_id = Filter::postInteger('block_id'); if ($block_id) { Database::prepare( "UPDATE `##block` SET gedcom_id=?, xref=? WHERE block_id=?" )->execute([Filter::postInteger('gedcom_id'), Filter::post('xref', WT_REGEX_XREF), $block_id]); } else { Database::prepare( "INSERT INTO `##block` (gedcom_id, xref, module_name, block_order) VALUES (?, ?, ?, ?)" )->execute([ Filter::postInteger('gedcom_id'), Filter::post('xref', WT_REGEX_XREF), $this->getName(), 0, ]); $block_id = Database::getInstance()->lastInsertId(); } $this->setBlockSetting($block_id, 'title', Filter::post('title')); $this->setBlockSetting($block_id, 'story_body', Filter::post('story_body')); $languages = Filter::postArray('lang'); $this->setBlockSetting($block_id, 'languages', implode(',', $languages)); $this->config(); } else { $block_id = Filter::getInteger('block_id'); $controller = new PageController; if ($block_id) { $controller->setPageTitle(I18N::translate('Edit the story')); $title = $this->getBlockSetting($block_id, 'title'); $story_body = $this->getBlockSetting($block_id, 'story_body'); $xref = Database::prepare( "SELECT xref FROM `##block` WHERE block_id=?" )->execute([$block_id])->fetchOne(); } else { $controller->setPageTitle(I18N::translate('Add a story')); $title = ''; $story_body = ''; $xref = Filter::get('xref', WT_REGEX_XREF); } $controller->pageHeader(); if (Module::getModuleByName('ckeditor')) { CkeditorModule::enableEditor($controller); } $individual = Individual::getInstance($xref, $WT_TREE); echo Bootstrap4::breadcrumbs([ route('admin-control-panel') => I18N::translate('Control panel'), route('admin-modules') => I18N::translate('Module administration'), 'module.php?mod=' . $this->getName() . '&mod_action=admin_config' => $this->getTitle(), ], $controller->getPageTitle()); ?>
= I18N::translate('Add a story') ?>
= I18N::translate('Story title') ?> | = I18N::translate('Individual') ?> | = I18N::translate('Edit') ?> | = I18N::translate('Delete') ?> |
---|---|---|---|
= e($this->getBlockSetting($story->block_id, 'title')) ?> | xref, $WT_TREE) ?> = $individual->getFullName() ?> = $story->xref ?> | = I18N::translate('Edit') ?> | = I18N::translate('Delete') ?> |
', I18N::translate('Story title'), ' | ', I18N::translate('Individual'), ' |
---|---|
' . $story_title . ' | ' . $indi->getFullName() . ' |
', $story_title, ' | ', $story->xref, ' |