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