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