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