1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 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 <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\Contracts\UserInterface; 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\HomePage; 29use Fisharebest\Webtrees\Http\RequestHandlers\LoginPage; 30use Fisharebest\Webtrees\Http\RequestHandlers\Logout; 31use Fisharebest\Webtrees\Http\RequestHandlers\ManageTrees; 32use Fisharebest\Webtrees\Http\RequestHandlers\PendingChanges; 33use Fisharebest\Webtrees\Http\RequestHandlers\SelectLanguage; 34use Fisharebest\Webtrees\Http\RequestHandlers\SelectTheme; 35use Fisharebest\Webtrees\Http\RequestHandlers\TreePage; 36use Fisharebest\Webtrees\Http\RequestHandlers\TreePageEdit; 37use Fisharebest\Webtrees\Http\RequestHandlers\UserPage; 38use Fisharebest\Webtrees\Http\RequestHandlers\UserPageEdit; 39use Fisharebest\Webtrees\I18N; 40use Fisharebest\Webtrees\Individual; 41use Fisharebest\Webtrees\Menu; 42use Fisharebest\Webtrees\Registry; 43use Fisharebest\Webtrees\Services\ModuleService; 44use Fisharebest\Webtrees\Tree; 45use Fisharebest\Webtrees\Validator; 46use Psr\Http\Message\ServerRequestInterface; 47 48use function count; 49use function in_array; 50use function route; 51use function view; 52 53/** 54 * Trait ModuleThemeTrait - default implementation of ModuleThemeInterface 55 */ 56trait ModuleThemeTrait 57{ 58 /** 59 * How should this module be identified in the control panel, etc.? 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 array<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 array<Menu> 150 */ 151 public function individualBoxMenuCharts(Individual $individual): array 152 { 153 $menus = []; 154 155 $module_service = Registry::container()->get(ModuleService::class); 156 157 foreach ($module_service->findByComponent(ModuleChartInterface::class, $individual->tree(), Auth::user()) as $chart) { 158 $menu = $chart->chartBoxMenu($individual); 159 if ($menu) { 160 $menus[] = $menu; 161 } 162 } 163 164 usort($menus, static function (Menu $x, Menu $y): int { 165 return I18N::comparator()($x->getLabel(), $y->getLabel()); 166 }); 167 168 return $menus; 169 } 170 171 /** 172 * Family links, to show in chart boxes. 173 * 174 * @param Individual $individual 175 * 176 * @return array<Menu> 177 */ 178 public function individualBoxMenuFamilyLinks(Individual $individual): array 179 { 180 $menus = []; 181 182 foreach ($individual->spouseFamilies() as $family) { 183 $menus[] = new Menu('<strong>' . I18N::translate('Family with spouse') . '</strong>', $family->url()); 184 $spouse = $family->spouse($individual); 185 if ($spouse && $spouse->canShowName()) { 186 $menus[] = new Menu($spouse->fullName(), $spouse->url()); 187 } 188 foreach ($family->children() as $child) { 189 if ($child->canShowName()) { 190 $menus[] = new Menu($child->fullName(), $child->url()); 191 } 192 } 193 } 194 195 return $menus; 196 } 197 198 /** 199 * Generate a menu item to change the blocks on the current tree/user page. 200 * 201 * @param Tree $tree 202 * 203 * @return Menu|null 204 */ 205 public function menuChangeBlocks(Tree $tree): ?Menu 206 { 207 $request = Registry::container()->get(ServerRequestInterface::class); 208 $route = Validator::attributes($request)->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(ManageTrees::class, ['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-wt-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 = Registry::container()->get(ServerRequestInterface::class); 277 278 // Return to this page after login... 279 $redirect = Validator::queryParams($request)->string('url', (string) $request->getUri()); 280 $tree = Validator::attributes($request)->treeOptional(); 281 $route = Validator::attributes($request)->route(); 282 283 // ...but switch from the tree-page to the user-page 284 if ($route->name === TreePage::class) { 285 $redirect = route(UserPage::class, ['tree' => $tree?->name()]); 286 } 287 288 // Stay on the same tree page 289 $url = route(LoginPage::class, ['tree' => $tree?->name(), 'url' => $redirect]); 290 291 return new Menu(I18N::translate('Sign in'), $url, 'menu-login', ['rel' => 'nofollow']); 292 } 293 294 /** 295 * A logout menu option (or null if we are already logged out). 296 * 297 * @return Menu|null 298 */ 299 public function menuLogout(): ?Menu 300 { 301 if (Auth::check()) { 302 $parameters = [ 303 'data-wt-post-url' => route(Logout::class), 304 'data-wt-reload-url' => route(HomePage::class) 305 ]; 306 307 return new Menu(I18N::translate('Sign out'), '#', 'menu-logout', $parameters); 308 } 309 310 return null; 311 } 312 313 /** 314 * A link to allow users to edit their account settings. 315 * 316 * @param Tree|null $tree 317 * 318 * @return Menu 319 */ 320 public function menuMyAccount(?Tree $tree): Menu 321 { 322 $url = route(AccountEdit::class, ['tree' => $tree?->name()]); 323 324 return new Menu(I18N::translate('My account'), $url, 'menu-myaccount'); 325 } 326 327 /** 328 * A link to the user's individual record (individual.php). 329 * 330 * @param Tree $tree 331 * 332 * @return Menu|null 333 */ 334 public function menuMyIndividualRecord(Tree $tree): ?Menu 335 { 336 $record = Registry::individualFactory()->make($tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF), $tree); 337 338 if ($record) { 339 return new Menu(I18N::translate('My individual record'), $record->url(), 'menu-myrecord'); 340 } 341 342 return null; 343 } 344 345 /** 346 * A link to the user's personal home page. 347 * 348 * @param Tree $tree 349 * 350 * @return Menu 351 */ 352 public function menuMyPage(Tree $tree): Menu 353 { 354 return new Menu(I18N::translate('My page'), route(UserPage::class, ['tree' => $tree->name()]), 'menu-mypage'); 355 } 356 357 /** 358 * A menu for the user's personal pages. 359 * 360 * @param Tree|null $tree 361 * 362 * @return Menu|null 363 */ 364 public function menuMyPages(?Tree $tree): ?Menu 365 { 366 if (Auth::check()) { 367 if ($tree instanceof Tree) { 368 return new Menu(I18N::translate('My pages'), '#', 'menu-mymenu', [], array_filter([ 369 $this->menuMyPage($tree), 370 $this->menuMyIndividualRecord($tree), 371 $this->menuMyPedigree($tree), 372 $this->menuMyAccount($tree), 373 $this->menuControlPanel($tree), 374 $this->menuChangeBlocks($tree), 375 ])); 376 } 377 378 return $this->menuMyAccount($tree); 379 } 380 381 return null; 382 } 383 384 /** 385 * A link to the user's individual record. 386 * 387 * @param Tree $tree 388 * 389 * @return Menu|null 390 */ 391 public function menuMyPedigree(Tree $tree): ?Menu 392 { 393 $my_xref = $tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF); 394 395 $module_service = Registry::container()->get(ModuleService::class); 396 $pedigree_chart = $module_service 397 ->findByComponent(ModuleChartInterface::class, $tree, Auth::user()) 398 ->first(static fn (ModuleInterface $module): bool => $module instanceof PedigreeChartModule); 399 400 if ($my_xref !== '' && $pedigree_chart instanceof PedigreeChartModule) { 401 $individual = Registry::individualFactory()->make($my_xref, $tree); 402 403 if ($individual instanceof Individual) { 404 return new Menu( 405 I18N::translate('My pedigree'), 406 $pedigree_chart->chartUrl($individual), 407 'menu-mypedigree' 408 ); 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 $request = Registry::container()->get(ServerRequestInterface::class); 426 427 $url = route(PendingChanges::class, [ 428 'tree' => $tree->name(), 429 'url' => (string) $request->getUri(), 430 ]); 431 432 return new Menu(I18N::translate('Pending changes'), $url, 'menu-pending'); 433 } 434 435 return null; 436 } 437 438 /** 439 * Themes menu. 440 * 441 * @return Menu|null 442 */ 443 public function menuThemes(): ?Menu 444 { 445 $module_service = Registry::container()->get(ModuleService::class); 446 $themes = $module_service->findByInterface(ModuleThemeInterface::class, false, true); 447 $current_theme = Registry::container()->get(ModuleThemeInterface::class); 448 449 if ($themes->count() > 1) { 450 $submenus = $themes->map(static function (ModuleThemeInterface $theme) use ($current_theme): Menu { 451 $active = $theme->name() === $current_theme->name(); 452 $class = 'menu-theme-' . $theme->name() . ($active ? ' active' : ''); 453 454 return new Menu($theme->title(), '#', $class, [ 455 'data-wt-post-url' => route(SelectTheme::class, ['theme' => $theme->name()]), 456 ]); 457 }); 458 459 return new Menu(I18N::translate('Theme'), '#', 'menu-theme', [], $submenus->all()); 460 } 461 462 return null; 463 } 464 465 /** 466 * Generate a list of items for the main menu. 467 * 468 * @param Tree|null $tree 469 * 470 * @return array<Menu> 471 */ 472 public function genealogyMenu(?Tree $tree): array 473 { 474 if ($tree === null) { 475 return []; 476 } 477 478 $module_service = Registry::container()->get(ModuleService::class); 479 480 return $module_service 481 ->findByComponent(ModuleMenuInterface::class, $tree, Auth::user()) 482 ->map(static fn (ModuleMenuInterface $menu): ?Menu => $menu->getMenu($tree)) 483 ->filter() 484 ->all(); 485 } 486 487 /** 488 * Create the genealogy menu. 489 * 490 * @param array<Menu> $menus 491 * 492 * @return string 493 */ 494 public function genealogyMenuContent(array $menus): string 495 { 496 return implode('', array_map(static function (Menu $menu): string { 497 return view('components/menu-item', ['menu' => $menu]); 498 }, $menus)); 499 } 500 501 /** 502 * Generate a list of items for the user menu. 503 * 504 * @param Tree|null $tree 505 * 506 * @return array<Menu> 507 */ 508 public function userMenu(?Tree $tree): array 509 { 510 return array_filter([ 511 $this->menuPendingChanges($tree), 512 $this->menuMyPages($tree), 513 $this->menuThemes(), 514 $this->menuLanguages(), 515 $this->menuLogin(), 516 $this->menuLogout(), 517 ]); 518 } 519 520 /** 521 * A list of CSS files to include for this page. 522 * 523 * @return array<string> 524 */ 525 public function stylesheets(): array 526 { 527 return []; 528 } 529} 530