xref: /webtrees/app/Module/WelcomeBlockModule.php (revision b50dba3a86d260384d07c30bedf82d8e78ab49f7)
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 fn(ModuleInterface $module): bool => $module instanceof PedigreeChartModule);
88
89        if ($pedigree_chart instanceof PedigreeChartModule) {
90            $links[] = [
91                $pedigree_chart->chartUrl($individual),
92                'url'   => $pedigree_chart->chartUrl($individual),
93                'title' => I18N::translate('Default chart'),
94                'icon'  => 'icon-pedigree',
95            ];
96        }
97
98        $links[] = [
99            'url'   => $individual->url(),
100            'title' => I18N::translate('Default individual'),
101            'icon'  => 'icon-indis',
102        ];
103
104        if (Site::getPreference('USE_REGISTRATION_MODULE') === '1' && !Auth::check()) {
105            $links[] = [
106                'url'   => route(RegisterPage::class, ['tree' => $tree->name()]),
107                'title' => I18N::translate('Request a new user account'),
108                'icon'  => 'icon-user_add',
109            ];
110        }
111
112        $content = view('modules/gedcom_block/welcome', ['links' => $links]);
113
114        if ($context !== self::CONTEXT_EMBED) {
115            return view('modules/block-template', [
116                'block'      => Str::kebab($this->name()),
117                'id'         => $block_id,
118                'config_url' => '',
119                'title'      => e($individual->tree()->title()),
120                'content'    => $content,
121            ]);
122        }
123
124        return $content;
125    }
126
127    /**
128     * Should this block load asynchronously using AJAX?
129     *
130     * Simple blocks are faster in-line, more complex ones can be loaded later.
131     *
132     * @return bool
133     */
134    public function loadAjax(): bool
135    {
136        return false;
137    }
138
139    /**
140     * Can this block be shown on the user’s home page?
141     *
142     * @return bool
143     */
144    public function isUserBlock(): bool
145    {
146        return false;
147    }
148
149    /**
150     * Can this block be shown on the tree’s home page?
151     *
152     * @return bool
153     */
154    public function isTreeBlock(): bool
155    {
156        return true;
157    }
158}
159