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