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