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