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