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 'module' => $this->name(), 226 'stories' => $stories, 227 'title' => $this->title() . ' — ' . $tree->title(), 228 'tree' => $tree, 229 'tree_names' => Tree::getNameList(), 230 ]); 231 } 232 233 /** 234 * @param ServerRequestInterface $request 235 * 236 * @return ResponseInterface 237 */ 238 public function getAdminEditAction(ServerRequestInterface $request): ResponseInterface 239 { 240 $this->layout = 'layouts/administration'; 241 242 $tree = $request->getAttribute('tree'); 243 $block_id = (int) ($request->getQueryParams()['block_id'] ?? 0); 244 245 if ($block_id === 0) { 246 // Creating a new story 247 $individual = null; 248 $story_title = ''; 249 $story_body = ''; 250 $languages = []; 251 252 $title = I18N::translate('Add a story') . ' — ' . e($tree->title()); 253 } else { 254 // Editing an existing story 255 $xref = (string) DB::table('block') 256 ->where('block_id', '=', $block_id) 257 ->value('xref'); 258 259 $individual = Individual::getInstance($xref, $tree); 260 $story_title = $this->getBlockSetting($block_id, 'title'); 261 $story_body = $this->getBlockSetting($block_id, 'story_body'); 262 $languages = explode(',', $this->getBlockSetting($block_id, 'languages')); 263 264 $title = I18N::translate('Edit the story') . ' — ' . e($tree->title()); 265 } 266 267 return $this->viewResponse('modules/stories/edit', [ 268 'block_id' => $block_id, 269 'languages' => $languages, 270 'story_body' => $story_body, 271 'story_title' => $story_title, 272 'title' => $title, 273 'tree' => $tree, 274 'individual' => $individual, 275 ]); 276 } 277 278 /** 279 * @param ServerRequestInterface $request 280 * 281 * @return ResponseInterface 282 */ 283 public function postAdminEditAction(ServerRequestInterface $request): ResponseInterface 284 { 285 $tree = $request->getAttribute('tree'); 286 $block_id = (int) ($request->getQueryParams()['block_id'] ?? 0); 287 288 $params = $request->getParsedBody(); 289 290 $xref = $params['xref']; 291 $story_body = $params['story_body']; 292 $story_title = $params['story_title']; 293 $languages = $params['languages'] ?? []; 294 295 $story_body = $this->html_service->sanitize($story_body); 296 $story_title = $this->html_service->sanitize($story_title); 297 298 if ($block_id !== 0) { 299 DB::table('block') 300 ->where('block_id', '=', $block_id) 301 ->update([ 302 'gedcom_id' => $tree->id(), 303 'xref' => $xref, 304 ]); 305 } else { 306 DB::table('block')->insert([ 307 'gedcom_id' => $tree->id(), 308 'xref' => $xref, 309 'module_name' => $this->name(), 310 'block_order' => 0, 311 ]); 312 313 $block_id = (int) DB::connection()->getPdo()->lastInsertId(); 314 } 315 316 $this->setBlockSetting($block_id, 'story_body', $story_body); 317 $this->setBlockSetting($block_id, 'title', $story_title); 318 $this->setBlockSetting($block_id, 'languages', implode(',', $languages)); 319 320 $url = route('module', [ 321 'module' => $this->name(), 322 'action' => 'Admin', 323 'ged' => $tree->name(), 324 ]); 325 326 return redirect($url); 327 } 328 329 /** 330 * @param ServerRequestInterface $request 331 * 332 * @return ResponseInterface 333 */ 334 public function postAdminDeleteAction(ServerRequestInterface $request): ResponseInterface 335 { 336 $tree = $request->getAttribute('tree'); 337 $block_id = $request->getQueryParams()['block_id']; 338 339 DB::table('block_setting') 340 ->where('block_id', '=', $block_id) 341 ->delete(); 342 343 DB::table('block') 344 ->where('block_id', '=', $block_id) 345 ->delete(); 346 347 $url = route('module', [ 348 'module' => $this->name(), 349 'action' => 'Admin', 350 'ged' => $tree->name(), 351 ]); 352 353 return redirect($url); 354 } 355 356 /** 357 * @param ServerRequestInterface $request 358 * 359 * @return ResponseInterface 360 */ 361 public function getShowListAction(ServerRequestInterface $request): ResponseInterface 362 { 363 $tree = $request->getAttribute('tree'); 364 365 $stories = DB::table('block') 366 ->where('module_name', '=', $this->name()) 367 ->where('gedcom_id', '=', $tree->id()) 368 ->get() 369 ->map(function (stdClass $story) use ($tree): stdClass { 370 $block_id = (int) $story->block_id; 371 372 $story->individual = Individual::getInstance($story->xref, $tree); 373 $story->title = $this->getBlockSetting($block_id, 'title'); 374 $story->languages = $this->getBlockSetting($block_id, 'languages'); 375 376 return $story; 377 })->filter(static function (stdClass $story): bool { 378 // Filter non-existant and private individuals. 379 return $story->individual instanceof Individual && $story->individual->canShow(); 380 })->filter(static function (stdClass $story): bool { 381 // Filter foreign languages. 382 return $story->languages === '' || in_array(WT_LOCALE, explode(',', $story->languages), true); 383 }); 384 385 return $this->viewResponse('modules/stories/list', [ 386 'stories' => $stories, 387 'title' => $this->title(), 388 ]); 389 } 390} 391