1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; 24use Fisharebest\Webtrees\Registry; 25use Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel; 26use Fisharebest\Webtrees\I18N; 27use Fisharebest\Webtrees\Individual; 28use Fisharebest\Webtrees\Menu; 29use Fisharebest\Webtrees\Services\HtmlService; 30use Fisharebest\Webtrees\Services\TreeService; 31use Fisharebest\Webtrees\Tree; 32use Illuminate\Database\Capsule\Manager as DB; 33use Psr\Http\Message\ResponseInterface; 34use Psr\Http\Message\ServerRequestInterface; 35 36use function assert; 37use function is_string; 38use function redirect; 39use function route; 40 41/** 42 * Class StoriesModule 43 */ 44class StoriesModule extends AbstractModule implements ModuleConfigInterface, ModuleMenuInterface, ModuleTabInterface 45{ 46 use ModuleTabTrait; 47 use ModuleConfigTrait; 48 use ModuleMenuTrait; 49 50 private HtmlService $html_service; 51 52 private TreeService $tree_service; 53 54 /** 55 * StoriesModule constructor. 56 * 57 * @param HtmlService $html_service 58 * @param TreeService $tree_service 59 */ 60 public function __construct(HtmlService $html_service, TreeService $tree_service) 61 { 62 $this->html_service = $html_service; 63 $this->tree_service = $tree_service; 64 } 65 66 /** @var int The default access level for this module. It can be changed in the control panel. */ 67 protected int $access_level = Auth::PRIV_HIDE; 68 69 /** 70 * A sentence describing what this module does. 71 * 72 * @return string 73 */ 74 public function description(): string 75 { 76 /* I18N: Description of the “Stories” module */ 77 return I18N::translate('Add narrative stories to individuals in the family tree.'); 78 } 79 80 /** 81 * The default position for this menu. It can be changed in the control panel. 82 * 83 * @return int 84 */ 85 public function defaultMenuOrder(): int 86 { 87 return 7; 88 } 89 90 /** 91 * The default position for this tab. It can be changed in the control panel. 92 * 93 * @return int 94 */ 95 public function defaultTabOrder(): int 96 { 97 return 9; 98 } 99 100 /** 101 * Generate the HTML content of this tab. 102 * 103 * @param Individual $individual 104 * 105 * @return string 106 */ 107 public function getTabContent(Individual $individual): string 108 { 109 return view('modules/stories/tab', [ 110 'is_admin' => Auth::isAdmin(), 111 'individual' => $individual, 112 'stories' => $this->getStoriesForIndividual($individual), 113 'tree' => $individual->tree(), 114 ]); 115 } 116 117 /** 118 * @param Individual $individual 119 * 120 * @return array<object> 121 */ 122 private function getStoriesForIndividual(Individual $individual): array 123 { 124 $block_ids = DB::table('block') 125 ->where('module_name', '=', $this->name()) 126 ->where('xref', '=', $individual->xref()) 127 ->where('gedcom_id', '=', $individual->tree()->id()) 128 ->pluck('block_id'); 129 130 $stories = []; 131 foreach ($block_ids as $block_id) { 132 $block_id = (int) $block_id; 133 134 // Only show this block for certain languages 135 $languages = $this->getBlockSetting($block_id, 'languages'); 136 if ($languages === '' || in_array(I18N::languageTag(), explode(',', $languages), true)) { 137 $stories[] = (object) [ 138 'block_id' => $block_id, 139 'title' => $this->getBlockSetting($block_id, 'title'), 140 'story_body' => $this->getBlockSetting($block_id, 'story_body'), 141 ]; 142 } 143 } 144 145 return $stories; 146 } 147 148 /** 149 * Is this tab empty? If so, we don't always need to display it. 150 * 151 * @param Individual $individual 152 * 153 * @return bool 154 */ 155 public function hasTabContent(Individual $individual): bool 156 { 157 return Auth::isManager($individual->tree()) || $this->getStoriesForIndividual($individual) !== []; 158 } 159 160 /** 161 * A greyed out tab has no actual content, but may perhaps have 162 * options to create content. 163 * 164 * @param Individual $individual 165 * 166 * @return bool 167 */ 168 public function isGrayedOut(Individual $individual): bool 169 { 170 return $this->getStoriesForIndividual($individual) === []; 171 } 172 173 /** 174 * Can this tab load asynchronously? 175 * 176 * @return bool 177 */ 178 public function canLoadAjax(): bool 179 { 180 return false; 181 } 182 183 /** 184 * A menu, to be added to the main application menu. 185 * 186 * @param Tree $tree 187 * 188 * @return Menu|null 189 */ 190 public function getMenu(Tree $tree): ?Menu 191 { 192 return new Menu($this->title(), route('module', [ 193 'module' => $this->name(), 194 'action' => 'ShowList', 195 'tree' => $tree->name(), 196 ]), 'menu-story'); 197 } 198 199 /** 200 * How should this module be identified in the control panel, etc.? 201 * 202 * @return string 203 */ 204 public function title(): string 205 { 206 /* I18N: Name of a module */ 207 return I18N::translate('Stories'); 208 } 209 210 /** 211 * @param ServerRequestInterface $request 212 * 213 * @return ResponseInterface 214 */ 215 public function getAdminAction(ServerRequestInterface $request): ResponseInterface 216 { 217 $this->layout = 'layouts/administration'; 218 219 // This module can't run without a tree 220 $tree = $request->getAttribute('tree'); 221 222 if (!$tree instanceof Tree) { 223 $tree = $this->tree_service->all()->first(); 224 if ($tree instanceof Tree) { 225 return redirect(route('module', ['module' => $this->name(), 'action' => 'Admin', 'tree' => $tree->name()])); 226 } 227 228 return redirect(route(ControlPanel::class)); 229 } 230 231 $stories = DB::table('block') 232 ->where('module_name', '=', $this->name()) 233 ->where('gedcom_id', '=', $tree->id()) 234 ->orderBy('xref') 235 ->get(); 236 237 foreach ($stories as $story) { 238 $block_id = (int) $story->block_id; 239 $xref = (string) $story->xref; 240 241 $story->individual = Registry::individualFactory()->make($xref, $tree); 242 $story->title = $this->getBlockSetting($block_id, 'title'); 243 $story->languages = $this->getBlockSetting($block_id, 'languages'); 244 } 245 246 $tree_names = $this->tree_service->all()->map(static function (Tree $tree): string { 247 return $tree->title(); 248 }); 249 250 return $this->viewResponse('modules/stories/config', [ 251 'module' => $this->name(), 252 'stories' => $stories, 253 'title' => $this->title() . ' — ' . $tree->title(), 254 'tree' => $tree, 255 'tree_names' => $tree_names, 256 ]); 257 } 258 259 /** 260 * @param ServerRequestInterface $request 261 * 262 * @return ResponseInterface 263 */ 264 public function postAdminAction(ServerRequestInterface $request): ResponseInterface 265 { 266 $params = (array) $request->getParsedBody(); 267 268 return redirect(route('module', [ 269 'module' => $this->name(), 270 'action' => 'Admin', 271 'tree' => $params['tree'] ?? '', 272 ])); 273 } 274 275 /** 276 * @param ServerRequestInterface $request 277 * 278 * @return ResponseInterface 279 */ 280 public function getAdminEditAction(ServerRequestInterface $request): ResponseInterface 281 { 282 $this->layout = 'layouts/administration'; 283 284 $tree = $request->getAttribute('tree'); 285 assert($tree instanceof Tree); 286 287 $block_id = (int) ($request->getQueryParams()['block_id'] ?? 0); 288 289 $url = $request->getQueryParams()['url'] ?? ''; 290 291 if ($block_id === 0) { 292 // Creating a new story 293 $story_title = ''; 294 $story_body = ''; 295 $languages = []; 296 $xref = $request->getQueryParams()['xref'] ?? ''; 297 $title = I18N::translate('Add a story') . ' — ' . e($tree->title()); 298 } else { 299 // Editing an existing story 300 $xref = (string) DB::table('block') 301 ->where('block_id', '=', $block_id) 302 ->value('xref'); 303 304 // Record was deleted before we could read it? 305 if (!is_string($xref)) { 306 throw new HttpNotFoundException(I18N::translate('%s does not exist.', 'block_id:' . $block_id)); 307 } 308 309 $story_title = $this->getBlockSetting($block_id, 'title'); 310 $story_body = $this->getBlockSetting($block_id, 'story_body'); 311 $languages = explode(',', $this->getBlockSetting($block_id, 'languages')); 312 $title = I18N::translate('Edit the story') . ' — ' . e($tree->title()); 313 } 314 315 $individual = Registry::individualFactory()->make($xref, $tree); 316 317 return $this->viewResponse('modules/stories/edit', [ 318 'block_id' => $block_id, 319 'languages' => $languages, 320 'story_body' => $story_body, 321 'story_title' => $story_title, 322 'title' => $title, 323 'tree' => $tree, 324 'url' => $url, 325 'individual' => $individual, 326 ]); 327 } 328 329 /** 330 * @param ServerRequestInterface $request 331 * 332 * @return ResponseInterface 333 */ 334 public function postAdminEditAction(ServerRequestInterface $request): ResponseInterface 335 { 336 $tree = $request->getAttribute('tree'); 337 assert($tree instanceof Tree); 338 339 $block_id = (int) ($request->getQueryParams()['block_id'] ?? 0); 340 341 $params = (array) $request->getParsedBody(); 342 343 $xref = $params['xref']; 344 $story_body = $params['story_body']; 345 $story_title = $params['story_title']; 346 $languages = $params['languages'] ?? []; 347 $url = $params['url'] ?? ''; 348 349 $story_body = $this->html_service->sanitize($story_body); 350 351 if ($block_id !== 0) { 352 DB::table('block') 353 ->where('block_id', '=', $block_id) 354 ->update([ 355 'gedcom_id' => $tree->id(), 356 'xref' => $xref, 357 ]); 358 } else { 359 DB::table('block')->insert([ 360 'gedcom_id' => $tree->id(), 361 'xref' => $xref, 362 'module_name' => $this->name(), 363 'block_order' => 0, 364 ]); 365 366 $block_id = (int) DB::connection()->getPdo()->lastInsertId(); 367 } 368 369 $this->setBlockSetting($block_id, 'story_body', $story_body); 370 $this->setBlockSetting($block_id, 'title', $story_title); 371 $this->setBlockSetting($block_id, 'languages', implode(',', $languages)); 372 373 $url = $url ?: route('module', [ 374 'module' => $this->name(), 375 'action' => 'Admin', 376 'tree' => $tree->name(), 377 ]); 378 379 return redirect($url); 380 } 381 382 /** 383 * @param ServerRequestInterface $request 384 * 385 * @return ResponseInterface 386 */ 387 public function postAdminDeleteAction(ServerRequestInterface $request): ResponseInterface 388 { 389 $tree = $request->getAttribute('tree'); 390 assert($tree instanceof Tree); 391 392 $block_id = $request->getQueryParams()['block_id']; 393 394 DB::table('block_setting') 395 ->where('block_id', '=', $block_id) 396 ->delete(); 397 398 DB::table('block') 399 ->where('block_id', '=', $block_id) 400 ->delete(); 401 402 $url = route('module', [ 403 'module' => $this->name(), 404 'action' => 'Admin', 405 'tree' => $tree->name(), 406 ]); 407 408 return redirect($url); 409 } 410 411 /** 412 * @param ServerRequestInterface $request 413 * 414 * @return ResponseInterface 415 */ 416 public function getShowListAction(ServerRequestInterface $request): ResponseInterface 417 { 418 $tree = $request->getAttribute('tree'); 419 assert($tree instanceof Tree); 420 421 $stories = DB::table('block') 422 ->where('module_name', '=', $this->name()) 423 ->where('gedcom_id', '=', $tree->id()) 424 ->get() 425 ->map(function (object $story) use ($tree): object { 426 $block_id = (int) $story->block_id; 427 $xref = (string) $story->xref; 428 429 $story->individual = Registry::individualFactory()->make($xref, $tree); 430 $story->title = $this->getBlockSetting($block_id, 'title'); 431 $story->languages = $this->getBlockSetting($block_id, 'languages'); 432 433 return $story; 434 })->filter(static function (object $story): bool { 435 // Filter non-existent and private individuals. 436 return $story->individual instanceof Individual && $story->individual->canShow(); 437 })->filter(static function (object $story): bool { 438 // Filter foreign languages. 439 return $story->languages === '' || in_array(I18N::languageTag(), explode(',', $story->languages), true); 440 }); 441 442 return $this->viewResponse('modules/stories/list', [ 443 'stories' => $stories, 444 'title' => $this->title(), 445 'tree' => $tree, 446 ]); 447 } 448} 449