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