1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Individual; 26use Fisharebest\Webtrees\Menu; 27use Fisharebest\Webtrees\Services\HtmlService; 28use Fisharebest\Webtrees\Services\TreeService; 29use Fisharebest\Webtrees\Tree; 30use Illuminate\Database\Capsule\Manager as DB; 31use InvalidArgumentException; 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 * BatchUpdateModule 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 ]); 115 } 116 117 /** 118 * @param Individual $individual 119 * 120 * @return stdClass[] 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(WT_LOCALE, 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 $menu = new Menu($this->title(), route('module', [ 193 'module' => $this->name(), 194 'action' => 'ShowList', 195 'tree' => $tree->name(), 196 ]), 'menu-story'); 197 198 return $menu; 199 } 200 201 /** 202 * How should this module be identified in the control panel, etc.? 203 * 204 * @return string 205 */ 206 public function title(): string 207 { 208 /* I18N: Name of a module */ 209 return I18N::translate('Stories'); 210 } 211 212 /** 213 * @param ServerRequestInterface $request 214 * 215 * @return ResponseInterface 216 */ 217 public function getAdminAction(ServerRequestInterface $request): ResponseInterface 218 { 219 $this->layout = 'layouts/administration'; 220 221 // This module can't run without a tree 222 $tree = $request->getAttribute('tree'); 223 224 if (!$tree instanceof Tree) { 225 $tree = $this->tree_service->all()->first(); 226 if ($tree instanceof Tree) { 227 return redirect(route('module', ['module' => $this->name(), 'action' => 'Admin', 'tree' => $tree->name()])); 228 } 229 230 return redirect(route(ControlPanel::class)); 231 } 232 233 $stories = DB::table('block') 234 ->where('module_name', '=', $this->name()) 235 ->where('gedcom_id', '=', $tree->id()) 236 ->orderBy('xref') 237 ->get(); 238 239 foreach ($stories as $story) { 240 $block_id = (int) $story->block_id; 241 242 $story->individual = Individual::getInstance($story->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 return redirect(route('module', [ 268 'module' => $this->name(), 269 'action' => 'Admin', 270 'tree' => $request->getParsedBody()['tree'] ?? '', 271 ])); 272 } 273 274 /** 275 * @param ServerRequestInterface $request 276 * 277 * @return ResponseInterface 278 */ 279 public function getAdminEditAction(ServerRequestInterface $request): ResponseInterface 280 { 281 $this->layout = 'layouts/administration'; 282 283 $tree = $request->getAttribute('tree'); 284 $block_id = (int) ($request->getQueryParams()['block_id'] ?? 0); 285 286 if ($block_id === 0) { 287 // Creating a new story 288 $individual = null; 289 $story_title = ''; 290 $story_body = ''; 291 $languages = []; 292 293 $title = I18N::translate('Add a story') . ' — ' . e($tree->title()); 294 } else { 295 // Editing an existing story 296 $xref = (string) DB::table('block') 297 ->where('block_id', '=', $block_id) 298 ->value('xref'); 299 300 $individual = Individual::getInstance($xref, $tree); 301 $story_title = $this->getBlockSetting($block_id, 'title'); 302 $story_body = $this->getBlockSetting($block_id, 'story_body'); 303 $languages = explode(',', $this->getBlockSetting($block_id, 'languages')); 304 305 $title = I18N::translate('Edit the story') . ' — ' . e($tree->title()); 306 } 307 308 return $this->viewResponse('modules/stories/edit', [ 309 'block_id' => $block_id, 310 'languages' => $languages, 311 'story_body' => $story_body, 312 'story_title' => $story_title, 313 'title' => $title, 314 'tree' => $tree, 315 'individual' => $individual, 316 ]); 317 } 318 319 /** 320 * @param ServerRequestInterface $request 321 * 322 * @return ResponseInterface 323 */ 324 public function postAdminEditAction(ServerRequestInterface $request): ResponseInterface 325 { 326 $tree = $request->getAttribute('tree'); 327 $block_id = (int) ($request->getQueryParams()['block_id'] ?? 0); 328 329 $params = $request->getParsedBody(); 330 331 $xref = $params['xref']; 332 $story_body = $params['story_body']; 333 $story_title = $params['story_title']; 334 $languages = $params['languages'] ?? []; 335 336 $story_body = $this->html_service->sanitize($story_body); 337 $story_title = $this->html_service->sanitize($story_title); 338 339 if ($block_id !== 0) { 340 DB::table('block') 341 ->where('block_id', '=', $block_id) 342 ->update([ 343 'gedcom_id' => $tree->id(), 344 'xref' => $xref, 345 ]); 346 } else { 347 DB::table('block')->insert([ 348 'gedcom_id' => $tree->id(), 349 'xref' => $xref, 350 'module_name' => $this->name(), 351 'block_order' => 0, 352 ]); 353 354 $block_id = (int) DB::connection()->getPdo()->lastInsertId(); 355 } 356 357 $this->setBlockSetting($block_id, 'story_body', $story_body); 358 $this->setBlockSetting($block_id, 'title', $story_title); 359 $this->setBlockSetting($block_id, 'languages', implode(',', $languages)); 360 361 $url = route('module', [ 362 'module' => $this->name(), 363 'action' => 'Admin', 364 'tree' => $tree->name(), 365 ]); 366 367 return redirect($url); 368 } 369 370 /** 371 * @param ServerRequestInterface $request 372 * 373 * @return ResponseInterface 374 */ 375 public function postAdminDeleteAction(ServerRequestInterface $request): ResponseInterface 376 { 377 $tree = $request->getAttribute('tree'); 378 $block_id = $request->getQueryParams()['block_id']; 379 380 DB::table('block_setting') 381 ->where('block_id', '=', $block_id) 382 ->delete(); 383 384 DB::table('block') 385 ->where('block_id', '=', $block_id) 386 ->delete(); 387 388 $url = route('module', [ 389 'module' => $this->name(), 390 'action' => 'Admin', 391 'tree' => $tree->name(), 392 ]); 393 394 return redirect($url); 395 } 396 397 /** 398 * @param ServerRequestInterface $request 399 * 400 * @return ResponseInterface 401 */ 402 public function getShowListAction(ServerRequestInterface $request): ResponseInterface 403 { 404 $tree = $request->getAttribute('tree'); 405 assert($tree instanceof Tree, new InvalidArgumentException()); 406 407 $stories = DB::table('block') 408 ->where('module_name', '=', $this->name()) 409 ->where('gedcom_id', '=', $tree->id()) 410 ->get() 411 ->map(function (stdClass $story) use ($tree): stdClass { 412 $block_id = (int) $story->block_id; 413 414 $story->individual = Individual::getInstance($story->xref, $tree); 415 $story->title = $this->getBlockSetting($block_id, 'title'); 416 $story->languages = $this->getBlockSetting($block_id, 'languages'); 417 418 return $story; 419 })->filter(static function (stdClass $story): bool { 420 // Filter non-existant and private individuals. 421 return $story->individual instanceof Individual && $story->individual->canShow(); 422 })->filter(static function (stdClass $story): bool { 423 // Filter foreign languages. 424 return $story->languages === '' || in_array(WT_LOCALE, explode(',', $story->languages), true); 425 }); 426 427 return $this->viewResponse('modules/stories/list', [ 428 'stories' => $stories, 429 'title' => $this->title(), 430 ]); 431 } 432} 433