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\Fact; 24use Fisharebest\Webtrees\Gedcom; 25use Fisharebest\Webtrees\GedcomTag; 26use Fisharebest\Webtrees\Http\RequestHandlers\AccountEdit; 27use Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel; 28use Fisharebest\Webtrees\Http\RequestHandlers\HomePage; 29use Fisharebest\Webtrees\Http\RequestHandlers\LoginPage; 30use Fisharebest\Webtrees\Http\RequestHandlers\Logout; 31use Fisharebest\Webtrees\Http\RequestHandlers\PendingChanges; 32use Fisharebest\Webtrees\Http\RequestHandlers\SelectLanguage; 33use Fisharebest\Webtrees\Http\RequestHandlers\SelectTheme; 34use Fisharebest\Webtrees\I18N; 35use Fisharebest\Webtrees\Individual; 36use Fisharebest\Webtrees\Menu; 37use Fisharebest\Webtrees\Services\ModuleService; 38use Fisharebest\Webtrees\Tree; 39use Fisharebest\Webtrees\Webtrees; 40use Psr\Http\Message\ServerRequestInterface; 41 42use function app; 43use function route; 44 45/** 46 * Trait ModuleThemeTrait - default implementation of ModuleThemeInterface 47 */ 48trait ModuleThemeTrait 49{ 50 /** 51 * @return string 52 */ 53 abstract public function name(): string; 54 55 /** 56 * @return string 57 */ 58 abstract public function title(): string; 59 60 /** 61 * A sentence describing what this module does. 62 * 63 * @return string 64 */ 65 public function description(): string 66 { 67 return I18N::translate('Theme') . ' — ' . $this->title(); 68 } 69 70 /** 71 * Display an icon for this fact. 72 * 73 * @TODO use CSS for this 74 * 75 * @param Fact $fact 76 * 77 * @return string 78 */ 79 public function icon(Fact $fact): string 80 { 81 $asset = 'public/css/' . $this->name() . '/images/facts/' . $fact->getTag() . '.png'; 82 if (file_exists(Webtrees::ROOT_DIR . 'public' . $asset)) { 83 return '<img src="' . e(asset($asset)) . '" title="' . GedcomTag::getLabel($fact->getTag()) . '">'; 84 } 85 86 // Spacer image - for alignment - until we move to a sprite. 87 $asset = 'public/css/' . $this->name() . '/images/facts/NULL.png'; 88 if (file_exists(Webtrees::ROOT_DIR . 'public' . $asset)) { 89 return '<img src="' . e(asset($asset)) . '">'; 90 } 91 92 return ''; 93 } 94 95 /** 96 * Generate the facts, for display in charts. 97 * 98 * @param Individual $individual 99 * 100 * @return string 101 */ 102 public function individualBoxFacts(Individual $individual): string 103 { 104 $html = ''; 105 106 $opt_tags = preg_split('/\W/', $individual->tree()->getPreference('CHART_BOX_TAGS'), 0, PREG_SPLIT_NO_EMPTY); 107 // Show BIRT or equivalent event 108 foreach (Gedcom::BIRTH_EVENTS as $birttag) { 109 if (!in_array($birttag, $opt_tags, true)) { 110 $event = $individual->facts([$birttag])->first(); 111 if ($event instanceof Fact) { 112 $html .= $event->summary(); 113 break; 114 } 115 } 116 } 117 // Show optional events (before death) 118 foreach ($opt_tags as $key => $tag) { 119 if (!in_array($tag, Gedcom::DEATH_EVENTS, true)) { 120 $event = $individual->facts([$tag])->first(); 121 if ($event instanceof Fact) { 122 $html .= $event->summary(); 123 unset($opt_tags[$key]); 124 } 125 } 126 } 127 // Show DEAT or equivalent event 128 foreach (Gedcom::DEATH_EVENTS as $deattag) { 129 $event = $individual->facts([$deattag])->first(); 130 if ($event instanceof Fact) { 131 $html .= $event->summary(); 132 if (in_array($deattag, $opt_tags, true)) { 133 unset($opt_tags[array_search($deattag, $opt_tags, true)]); 134 } 135 break; 136 } 137 } 138 // Show remaining optional events (after death) 139 foreach ($opt_tags as $tag) { 140 $event = $individual->facts([$tag])->first(); 141 if ($event instanceof Fact) { 142 $html .= $event->summary(); 143 } 144 } 145 146 return $html; 147 } 148 149 /** 150 * Links, to show in chart boxes; 151 * 152 * @param Individual $individual 153 * 154 * @return Menu[] 155 */ 156 public function individualBoxMenu(Individual $individual): array 157 { 158 $menus = array_merge( 159 $this->individualBoxMenuCharts($individual), 160 $this->individualBoxMenuFamilyLinks($individual) 161 ); 162 163 return $menus; 164 } 165 166 /** 167 * Chart links, to show in chart boxes; 168 * 169 * @param Individual $individual 170 * 171 * @return Menu[] 172 */ 173 public function individualBoxMenuCharts(Individual $individual): array 174 { 175 $menus = []; 176 foreach (app(ModuleService::class)->findByComponent(ModuleChartInterface::class, $individual->tree(), Auth::user()) as $chart) { 177 $menu = $chart->chartBoxMenu($individual); 178 if ($menu) { 179 $menus[] = $menu; 180 } 181 } 182 183 usort($menus, static function (Menu $x, Menu $y): int { 184 return I18N::strcasecmp($x->getLabel(), $y->getLabel()); 185 }); 186 187 return $menus; 188 } 189 190 /** 191 * Family links, to show in chart boxes. 192 * 193 * @param Individual $individual 194 * 195 * @return Menu[] 196 */ 197 public function individualBoxMenuFamilyLinks(Individual $individual): array 198 { 199 $menus = []; 200 201 foreach ($individual->spouseFamilies() as $family) { 202 $menus[] = new Menu('<strong>' . I18N::translate('Family with spouse') . '</strong>', $family->url()); 203 $spouse = $family->spouse($individual); 204 if ($spouse && $spouse->canShowName()) { 205 $menus[] = new Menu($spouse->fullName(), $spouse->url()); 206 } 207 foreach ($family->children() as $child) { 208 if ($child->canShowName()) { 209 $menus[] = new Menu($child->fullName(), $child->url()); 210 } 211 } 212 } 213 214 return $menus; 215 } 216 217 /** 218 * Generate a menu item to change the blocks on the current tree/user page. 219 * 220 * @param Tree $tree 221 * 222 * @return Menu|null 223 */ 224 public function menuChangeBlocks(Tree $tree): ?Menu 225 { 226 /** @var ServerRequestInterface $request */ 227 $request = app(ServerRequestInterface::class); 228 229 $route = $request->getAttribute('route'); 230 231 if (Auth::check() && $route === 'user-page') { 232 return new Menu(I18N::translate('Customize this page'), route('user-page-edit', ['tree' => $tree->name()]), 'menu-change-blocks'); 233 } 234 235 if (Auth::isManager($tree) && $route === 'tree-page') { 236 return new Menu(I18N::translate('Customize this page'), route('tree-page-edit', ['tree' => $tree->name()]), 'menu-change-blocks'); 237 } 238 239 return null; 240 } 241 242 /** 243 * Generate a menu item for the control panel. 244 * 245 * @param Tree $tree 246 * 247 * @return Menu|null 248 */ 249 public function menuControlPanel(Tree $tree): ?Menu 250 { 251 if (Auth::isAdmin()) { 252 return new Menu(I18N::translate('Control panel'), route(ControlPanel::class), 'menu-admin'); 253 } 254 255 if (Auth::isManager($tree)) { 256 return new Menu(I18N::translate('Control panel'), route('manage-trees'), 'menu-admin'); 257 } 258 259 return null; 260 } 261 262 /** 263 * A menu to show a list of available languages. 264 * 265 * @return Menu|null 266 */ 267 public function menuLanguages(): ?Menu 268 { 269 $menu = new Menu(I18N::translate('Language'), '#', 'menu-language'); 270 271 foreach (I18N::activeLocales() as $locale) { 272 $language_tag = $locale->languageTag(); 273 $class = 'menu-language-' . $language_tag . (WT_LOCALE === $language_tag ? ' active' : ''); 274 $menu->addSubmenu(new Menu($locale->endonym(), '#', $class, [ 275 'data-post-url' => route(SelectLanguage::class, ['language' => $language_tag]), 276 ])); 277 } 278 279 if (count($menu->getSubmenus()) > 1) { 280 return $menu; 281 } 282 283 return null; 284 } 285 286 /** 287 * A login menu option (or null if we are already logged in). 288 * 289 * @return Menu|null 290 */ 291 public function menuLogin(): ?Menu 292 { 293 if (Auth::check()) { 294 return null; 295 } 296 297 $request = app(ServerRequestInterface::class); 298 299 // Return to this page after login... 300 $redirect = (string) $request->getUri(); 301 302 // ...but switch from the tree-page to the user-page 303 if ($request->getAttribute('route') === 'tree-page') { 304 $tree = $request->getAttribute('tree'); 305 assert($tree instanceof Tree); 306 $redirect = route('user-page', ['tree' => $tree->name()]); 307 } 308 309 // Stay on the same tree page 310 $tree = $request->getAttribute('tree'); 311 $url = route(LoginPage::class, ['tree' => $tree instanceof Tree ? $tree->name() : null, 'url' => $redirect]); 312 313 return new Menu(I18N::translate('Sign in'), $url, 'menu-login', ['rel' => 'nofollow']); 314 } 315 316 /** 317 * A logout menu option (or null if we are already logged out). 318 * 319 * @return Menu|null 320 */ 321 public function menuLogout(): ?Menu 322 { 323 if (Auth::check()) { 324 $parameters = [ 325 'data-post-url' => route(Logout::class), 326 'data-reload-url' => route(HomePage::class), 327 ]; 328 329 return new Menu(I18N::translate('Sign out'), '#', 'menu-logout', $parameters); 330 } 331 332 return null; 333 } 334 335 /** 336 * A link to allow users to edit their account settings. 337 * 338 * @param Tree|null $tree 339 * 340 * @return Menu 341 */ 342 public function menuMyAccount(?Tree $tree): Menu 343 { 344 $url = route(AccountEdit::class, ['tree' => $tree instanceof Tree ? $tree->name() : null]); 345 346 return new Menu(I18N::translate('My account'), $url); 347 } 348 349 /** 350 * A link to the user's individual record (individual.php). 351 * 352 * @param Tree $tree 353 * 354 * @return Menu|null 355 */ 356 public function menuMyIndividualRecord(Tree $tree): ?Menu 357 { 358 $record = Individual::getInstance($tree->getUserPreference(Auth::user(), 'gedcomid'), $tree); 359 360 if ($record) { 361 return new Menu(I18N::translate('My individual record'), $record->url(), 'menu-myrecord'); 362 } 363 364 return null; 365 } 366 367 /** 368 * A link to the user's personal home page. 369 * 370 * @param Tree $tree 371 * 372 * @return Menu 373 */ 374 public function menuMyPage(Tree $tree): Menu 375 { 376 return new Menu(I18N::translate('My page'), route('user-page', ['tree' => $tree->name()]), 'menu-mypage'); 377 } 378 379 /** 380 * A menu for the user's personal pages. 381 * 382 * @param Tree|null $tree 383 * 384 * @return Menu|null 385 */ 386 public function menuMyPages(?Tree $tree): ?Menu 387 { 388 if (Auth::id()) { 389 if ($tree instanceof Tree) { 390 return new Menu(I18N::translate('My pages'), '#', 'menu-mymenu', [], array_filter([ 391 $this->menuMyPage($tree), 392 $this->menuMyIndividualRecord($tree), 393 $this->menuMyPedigree($tree), 394 $this->menuMyAccount($tree), 395 $this->menuControlPanel($tree), 396 $this->menuChangeBlocks($tree), 397 ])); 398 } 399 400 return $this->menuMyAccount($tree); 401 } 402 403 return null; 404 } 405 406 /** 407 * A link to the user's individual record. 408 * 409 * @param Tree $tree 410 * 411 * @return Menu|null 412 */ 413 public function menuMyPedigree(Tree $tree): ?Menu 414 { 415 $gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid'); 416 417 $pedigree_chart = app(ModuleService::class)->findByComponent(ModuleChartInterface::class, $tree, Auth::user()) 418 ->filter(static function (ModuleInterface $module): bool { 419 return $module instanceof PedigreeChartModule; 420 }); 421 422 if ($gedcomid !== '' && $pedigree_chart instanceof PedigreeChartModule) { 423 return new Menu( 424 I18N::translate('My pedigree'), 425 route('pedigree', [ 426 'xref' => $gedcomid, 427 'tree' => $tree->name(), 428 ]), 429 'menu-mypedigree' 430 ); 431 } 432 433 return null; 434 } 435 436 /** 437 * Create a pending changes menu. 438 * 439 * @param Tree|null $tree 440 * 441 * @return Menu|null 442 */ 443 public function menuPendingChanges(?Tree $tree): ?Menu 444 { 445 if ($tree instanceof Tree && $tree->hasPendingEdit() && Auth::isModerator($tree)) { 446 $url = route(PendingChanges::class, [ 447 'tree' => $tree->name(), 448 'url' => (string) app(ServerRequestInterface::class)->getUri(), 449 ]); 450 451 return new Menu(I18N::translate('Pending changes'), $url, 'menu-pending'); 452 } 453 454 return null; 455 } 456 457 /** 458 * Themes menu. 459 * 460 * @return Menu|null 461 */ 462 public function menuThemes(): ?Menu 463 { 464 $themes = app(ModuleService::class)->findByInterface(ModuleThemeInterface::class, false, true); 465 466 $current_theme = app(ModuleThemeInterface::class); 467 468 if ($themes->count() > 1) { 469 $submenus = $themes->map(static function (ModuleThemeInterface $theme) use ($current_theme): Menu { 470 $active = $theme->name() === $current_theme->name(); 471 $class = 'menu-theme-' . $theme->name() . ($active ? ' active' : ''); 472 473 return new Menu($theme->title(), '#', $class, [ 474 'data-post-url' => route(SelectTheme::class, ['theme' => $theme->name()]), 475 ]); 476 }); 477 478 return new Menu(I18N::translate('Theme'), '#', 'menu-theme', [], $submenus->all()); 479 } 480 481 return null; 482 } 483 484 /** 485 * Misecellaneous dimensions, fonts, styles, etc. 486 * 487 * @param string $parameter_name 488 * 489 * @return string|int|float 490 */ 491 public function parameter($parameter_name) 492 { 493 return ''; 494 } 495 496 /** 497 * Generate a list of items for the main menu. 498 * 499 * @param Tree|null $tree 500 * 501 * @return Menu[] 502 */ 503 public function genealogyMenu(?Tree $tree): array 504 { 505 if ($tree === null) { 506 return []; 507 } 508 509 return app(ModuleService::class)->findByComponent(ModuleMenuInterface::class, $tree, Auth::user()) 510 ->map(static function (ModuleMenuInterface $menu) use ($tree): ?Menu { 511 return $menu->getMenu($tree); 512 }) 513 ->filter() 514 ->all(); 515 } 516 517 /** 518 * Create the genealogy menu. 519 * 520 * @param Menu[] $menus 521 * 522 * @return string 523 */ 524 public function genealogyMenuContent(array $menus): string 525 { 526 return implode('', array_map(static function (Menu $menu): string { 527 return $menu->bootstrap4(); 528 }, $menus)); 529 } 530 531 /** 532 * Generate a list of items for the user menu. 533 * 534 * @param Tree|null $tree 535 * 536 * @return Menu[] 537 */ 538 public function userMenu(?Tree $tree): array 539 { 540 return array_filter([ 541 $this->menuPendingChanges($tree), 542 $this->menuMyPages($tree), 543 $this->menuThemes(), 544 $this->menuLanguages(), 545 $this->menuLogin(), 546 $this->menuLogout(), 547 ]); 548 } 549 550 /** 551 * A list of CSS files to include for this page. 552 * 553 * @return string[] 554 */ 555 public function stylesheets(): array 556 { 557 return []; 558 } 559} 560