1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 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\Http\RequestHandlers\RegisterPage; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Services\ModuleService; 26use Fisharebest\Webtrees\Site; 27use Fisharebest\Webtrees\Tree; 28use Illuminate\Support\Str; 29 30/** 31 * Class WelcomeBlockModule 32 */ 33class WelcomeBlockModule extends AbstractModule implements ModuleBlockInterface 34{ 35 use ModuleBlockTrait; 36 37 private ModuleService $module_service; 38 39 /** 40 * WelcomeBlockModule constructor. 41 * 42 * @param ModuleService $module_service 43 */ 44 public function __construct(ModuleService $module_service) 45 { 46 $this->module_service = $module_service; 47 } 48 49 /** 50 * How should this module be identified in the control panel, etc.? 51 * 52 * @return string 53 */ 54 public function title(): string 55 { 56 /* I18N: Name of a module */ 57 return I18N::translate('Home page'); 58 } 59 60 /** 61 * A sentence describing what this module does. 62 * 63 * @return string 64 */ 65 public function description(): string 66 { 67 /* I18N: Description of the “Home page” module */ 68 return I18N::translate('A greeting message for site visitors.'); 69 } 70 71 /** 72 * Generate the HTML content of this block. 73 * 74 * @param Tree $tree 75 * @param int $block_id 76 * @param string $context 77 * @param array<string> $config 78 * 79 * @return string 80 */ 81 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 82 { 83 $individual = $tree->significantIndividual(Auth::user()); 84 85 $links = []; 86 87 $pedigree_chart = $this->module_service 88 ->findByComponent(ModuleChartInterface::class, $tree, Auth::user()) 89 ->first(static function (ModuleInterface $module): bool { 90 return $module instanceof PedigreeChartModule; 91 }); 92 93 if ($pedigree_chart instanceof PedigreeChartModule) { 94 $links[] = [ 95 $pedigree_chart->chartUrl($individual), 96 'url' => $pedigree_chart->chartUrl($individual), 97 'title' => I18N::translate('Default chart'), 98 'icon' => 'icon-pedigree', 99 ]; 100 } 101 102 $links[] = [ 103 'url' => $individual->url(), 104 'title' => I18N::translate('Default individual'), 105 'icon' => 'icon-indis', 106 ]; 107 108 if (Site::getPreference('USE_REGISTRATION_MODULE') === '1' && !Auth::check()) { 109 $links[] = [ 110 'url' => route(RegisterPage::class, ['tree' => $tree->name()]), 111 'title' => I18N::translate('Request a new user account'), 112 'icon' => 'icon-user_add', 113 ]; 114 } 115 116 $content = view('modules/gedcom_block/welcome', ['links' => $links]); 117 118 if ($context !== self::CONTEXT_EMBED) { 119 return view('modules/block-template', [ 120 'block' => Str::kebab($this->name()), 121 'id' => $block_id, 122 'config_url' => '', 123 'title' => e($individual->tree()->title()), 124 'content' => $content, 125 ]); 126 } 127 128 return $content; 129 } 130 131 /** 132 * Should this block load asynchronously using AJAX? 133 * 134 * Simple blocks are faster in-line, more complex ones can be loaded later. 135 * 136 * @return bool 137 */ 138 public function loadAjax(): bool 139 { 140 return false; 141 } 142 143 /** 144 * Can this block be shown on the user’s home page? 145 * 146 * @return bool 147 */ 148 public function isUserBlock(): bool 149 { 150 return false; 151 } 152 153 /** 154 * Can this block be shown on the tree’s home page? 155 * 156 * @return bool 157 */ 158 public function isTreeBlock(): bool 159 { 160 return true; 161 } 162} 163