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\Database; 20use Fisharebest\Webtrees\I18N; 21use Fisharebest\Webtrees\Individual; 22use Fisharebest\Webtrees\Menu; 23use Fisharebest\Webtrees\Tree; 24use stdClass; 25use Symfony\Component\HttpFoundation\RedirectResponse; 26use Symfony\Component\HttpFoundation\Request; 27use Symfony\Component\HttpFoundation\Response; 28 29/** 30 * Class StoriesModule 31 */ 32class StoriesModule extends AbstractModule implements ModuleTabInterface, ModuleConfigInterface, ModuleMenuInterface 33{ 34 /** {@inheritdoc} */ 35 public function getTitle() 36 { 37 return /* I18N: Name of a module */ 38 I18N::translate('Stories'); 39 } 40 41 /** {@inheritdoc} */ 42 public function getDescription() 43 { 44 return /* I18N: Description of the “Stories” module */ 45 I18N::translate('Add narrative stories to individuals in the family tree.'); 46 } 47 48 /** 49 * The URL to a page where the user can modify the configuration of this module. 50 * 51 * @return string 52 */ 53 public function getConfigLink() 54 { 55 return route('module', [ 56 'module' => $this->getName(), 57 'action' => 'Admin', 58 ]); 59 } 60 61 /** {@inheritdoc} */ 62 public function defaultTabOrder() 63 { 64 return 55; 65 } 66 67 /** {@inheritdoc} */ 68 public function getTabContent(Individual $individual) 69 { 70 return view('modules/stories/tab', [ 71 'is_admin' => Auth::isAdmin(), 72 'individual' => $individual, 73 'stories' => $this->getStoriesForIndividual($individual), 74 ]); 75 } 76 77 /** {@inheritdoc} */ 78 public function hasTabContent(Individual $individual) 79 { 80 return Auth::isManager($individual->getTree()) || !empty($this->getStoriesForIndividual($individual)); 81 } 82 83 /** {@inheritdoc} */ 84 public function isGrayedOut(Individual $individual) 85 { 86 return !empty($this->getStoriesForIndividual($individual)); 87 } 88 89 /** {@inheritdoc} */ 90 public function canLoadAjax() 91 { 92 return false; 93 } 94 95 /** 96 * @param Individual $individual 97 * 98 * @return stdClass[] 99 */ 100 private function getStoriesForIndividual(Individual $individual): array 101 { 102 $block_ids = 103 Database::prepare( 104 "SELECT block_id" . 105 " FROM `##block`" . 106 " WHERE module_name = :module_name" . 107 " AND xref = :xref" . 108 " AND gedcom_id = :tree_id" 109 )->execute([ 110 'module_name' => $this->getName(), 111 'xref' => $individual->getXref(), 112 'tree_id' => $individual->getTree()->getTreeId(), 113 ])->fetchOneColumn(); 114 115 $stories = []; 116 foreach ($block_ids as $block_id) { 117 // Only show this block for certain languages 118 $languages = $this->getBlockSetting($block_id, 'languages', ''); 119 if ($languages === '' || in_array(WT_LOCALE, explode(',', $languages))) { 120 $stories[] = (object)[ 121 'block_id' => $block_id, 122 'title' => $this->getBlockSetting($block_id, 'title'), 123 'story_body' => $this->getBlockSetting($block_id, 'story_body'), 124 ]; 125 } 126 } 127 128 return $stories; 129 } 130 131 /** 132 * The user can re-order menus. Until they do, they are shown in this order. 133 * 134 * @return int 135 */ 136 public function defaultMenuOrder() 137 { 138 return 30; 139 } 140 141 /** 142 * What is the default access level for this module? 143 * 144 * Some modules are aimed at admins or managers, and are not generally shown to users. 145 * 146 * @return int 147 */ 148 public function defaultAccessLevel() 149 { 150 return Auth::PRIV_HIDE; 151 } 152 153 /** 154 * A menu, to be added to the main application menu. 155 * 156 * @param Tree $tree 157 * 158 * @return Menu|null 159 */ 160 public function getMenu(Tree $tree) 161 { 162 $menu = new Menu($this->getTitle(), route('module', [ 163 'module' => $this->getName(), 164 'action' => 'ShowList', 165 ]), 'menu-story'); 166 167 return $menu; 168 } 169 170 /** 171 * @param Tree $tree 172 * 173 * @return Response 174 */ 175 public function getAdminAction(Tree $tree): Response 176 { 177 $this->layout = 'layouts/administration'; 178 179 $stories = Database::prepare( 180 "SELECT block_id, xref, gedcom_id" . 181 " FROM `##block` b" . 182 " WHERE module_name = :module_name" . 183 " AND gedcom_id = :tree_id" . 184 " ORDER BY gedcom_id, xref" 185 )->execute([ 186 'tree_id' => $tree->getTreeId(), 187 'module_name' => $this->getName(), 188 ])->fetchAll(); 189 190 foreach ($stories as $story) { 191 $story->individual = Individual::getInstance($story->xref, $tree); 192 $story->title = $this->getBlockSetting($story->block_id, 'title'); 193 $story->languages = $this->getBlockSetting($story->block_id, 'languages'); 194 } 195 196 return $this->viewResponse('modules/stories/config', [ 197 'stories' => $stories, 198 'title' => $this->getTitle() . ' — ' . $tree->getTitle(), 199 'tree' => $tree, 200 'tree_names' => Tree::getNameList(), 201 ]); 202 } 203 204 /** 205 * @param Request $request 206 * @param Tree $tree 207 * 208 * @return Response 209 */ 210 public function getAdminEditAction(Request $request, Tree $tree): Response 211 { 212 $this->layout = 'layouts/administration'; 213 214 $block_id = (int)$request->get('block_id'); 215 216 if ($block_id === 0) { 217 // Creating a new story 218 $individual = Individual::getInstance($request->get('xref', ''), $tree); 219 $story_title = ''; 220 $story_body = ''; 221 $languages = []; 222 223 $title = I18N::translate('Add a story') . ' — ' . e($tree->getTitle()); 224 } else { 225 // Editing an existing story 226 $xref = Database::prepare( 227 "SELECT xref FROM `##block` WHERE block_id = :block_id" 228 )->execute([ 229 'block_id' => $block_id, 230 ])->fetchOne(); 231 232 $individual = Individual::getInstance($xref, $tree); 233 $story_title = $this->getBlockSetting($block_id, 'title', ''); 234 $story_body = $this->getBlockSetting($block_id, 'story_body', ''); 235 $languages = explode(',', $this->getBlockSetting($block_id, 'languages')); 236 237 $title = I18N::translate('Edit the story') . ' — ' . e($tree->getTitle()); 238 } 239 240 return $this->viewResponse('modules/stories/edit', [ 241 'block_id' => $block_id, 242 'languages' => $languages, 243 'story_body' => $story_body, 244 'story_title' => $story_title, 245 'title' => $title, 246 'tree' => $tree, 247 'individual' => $individual, 248 ]); 249 } 250 251 /** 252 * @param Request $request 253 * @param Tree $tree 254 * 255 * @return RedirectResponse 256 */ 257 public function postAdminEditAction(Request $request, Tree $tree): RedirectResponse 258 { 259 $block_id = (int)$request->get('block_id'); 260 $xref = $request->get('xref', ''); 261 $story_body = $request->get('story_body', ''); 262 $story_title = $request->get('story_title', ''); 263 $languages = $request->get('languages', []); 264 265 if ($block_id !== 0) { 266 Database::prepare( 267 "UPDATE `##block` SET gedcom_id = :tree_id, xref = :xref WHERE block_id = :block_id" 268 )->execute([ 269 'tree_id' => $tree->getTreeId(), 270 'xref' => $xref, 271 'block_id' => $block_id, 272 ]); 273 } else { 274 Database::prepare( 275 "INSERT INTO `##block` (gedcom_id, xref, module_name, block_order) VALUES (:tree_id, :xref, 'stories', 0)" 276 )->execute([ 277 'tree_id' => $tree->getTreeId(), 278 'xref' => $xref, 279 ]); 280 281 $block_id = Database::getInstance()->lastInsertId(); 282 } 283 284 $this->setBlockSetting($block_id, 'story_body', $story_body); 285 $this->setBlockSetting($block_id, 'title', $story_title); 286 $this->setBlockSetting($block_id, 'languages', implode(',', $languages)); 287 288 $url = route('module', [ 289 'module' => 'stories', 290 'action' => 'Admin', 291 'ged' => $tree->getName(), 292 ]); 293 294 return new RedirectResponse($url); 295 } 296 297 /** 298 * @param Request $request 299 * @param Tree $tree 300 * 301 * @return Response 302 */ 303 public function postAdminDeleteAction(Request $request, Tree $tree): Response 304 { 305 $block_id = (int)$request->get('block_id'); 306 307 Database::prepare( 308 "DELETE FROM `##block_setting` WHERE block_id = :block_id" 309 )->execute([ 310 'block_id' => $block_id, 311 ]); 312 313 Database::prepare( 314 "DELETE FROM `##block` WHERE block_id = :block_id" 315 )->execute([ 316 'block_id' => $block_id, 317 ]); 318 319 $url = route('module', [ 320 'module' => 'stories', 321 'action' => 'Admin', 322 'ged' => $tree->getName(), 323 ]); 324 325 return new RedirectResponse($url); 326 } 327 328 /** 329 * @param Tree $tree 330 * 331 * @return Response 332 */ 333 public function getShowListAction(Tree $tree): Response 334 { 335 $stories = Database::prepare( 336 "SELECT block_id, xref" . 337 " FROM `##block` b" . 338 " WHERE module_name = :module_name" . 339 " AND gedcom_id = :tree_id" . 340 " ORDER BY xref" 341 )->execute([ 342 'module_name' => $this->getName(), 343 'tree_id' => $tree->getTreeId(), 344 ])->fetchAll(); 345 346 foreach ($stories as $story) { 347 $story->individual = Individual::getInstance($story->xref, $tree); 348 $story->title = $this->getBlockSetting($story->block_id, 'title'); 349 $story->languages = $this->getBlockSetting($story->block_id, 'languages'); 350 } 351 352 // Filter non-existant and private individuals. 353 $stories = array_filter($stories, function (stdClass $story) { 354 return $story->individual !== null && $story->individual->canShow(); 355 }); 356 357 // Filter foreign languages. 358 $stories = array_filter($stories, function (stdClass $story) { 359 return $story->languages === '' || in_array(WT_LOCALE, explode(',', $story->languages)); 360 }); 361 362 return $this->viewResponse('modules/stories/list', [ 363 'stories' => $stories, 364 'title' => $this->getTitle(), 365 ]); 366 } 367} 368