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