xref: /webtrees/app/Module/UserWelcomeModule.php (revision e873f434551745f888937263ff89e80db3b0f785)
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\Contracts\UserInterface;
24use Fisharebest\Webtrees\Http\RequestHandlers\AccountEdit;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Individual;
27use Fisharebest\Webtrees\Registry;
28use Fisharebest\Webtrees\Services\ModuleService;
29use Fisharebest\Webtrees\Tree;
30use Illuminate\Support\Str;
31
32/**
33 * Class UserWelcomeModule
34 */
35class UserWelcomeModule extends AbstractModule implements ModuleBlockInterface
36{
37    use ModuleBlockTrait;
38
39    private ModuleService $module_service;
40
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('My page');
58    }
59
60    public function description(): string
61    {
62        /* I18N: Description of the “My page” module */
63        return I18N::translate('A greeting message and useful links for a user.');
64    }
65
66    /**
67     * Generate the HTML content of this block.
68     *
69     * @param Tree                 $tree
70     * @param int                  $block_id
71     * @param string               $context
72     * @param array<string,string> $config
73     *
74     * @return string
75     */
76    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
77    {
78        $gedcomid   = $tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF);
79        $individual = Registry::individualFactory()->make($gedcomid, $tree);
80        $links      = [];
81
82        $pedigree_chart = $this->module_service->findByComponent(ModuleChartInterface::class, $tree, Auth::user())
83            ->first(static fn (ModuleInterface $module): bool => $module instanceof PedigreeChartModule);
84
85        if ($individual instanceof Individual) {
86            if ($pedigree_chart instanceof PedigreeChartModule) {
87                $links[] = [
88                    'url'   => $pedigree_chart->chartUrl($individual),
89                    'title' => I18N::translate('Default chart'),
90                    'icon'  => 'icon-pedigree',
91                ];
92            }
93
94            $links[] = [
95                'url'   => $individual->url(),
96                'title' => I18N::translate('My individual record'),
97                'icon'  => 'icon-indis',
98            ];
99        }
100
101        $links[] = [
102            'url'   => route(AccountEdit::class, ['tree' => $tree->name()]),
103            'title' => I18N::translate('My account'),
104            'icon'  => 'icon-mypage',
105        ];
106        $content = view('modules/user_welcome/welcome', ['links' => $links]);
107
108        $real_name = "\u{2068}" . e(Auth::user()->realName()) . "\u{2069}";
109
110        /* I18N: A %s is the user’s name */
111        $title = I18N::translate('Welcome %s', $real_name);
112
113        if ($context !== self::CONTEXT_EMBED) {
114            return view('modules/block-template', [
115                'block'      => Str::kebab($this->name()),
116                'id'         => $block_id,
117                'config_url' => '',
118                'title'      => $title,
119                'content'    => $content,
120            ]);
121        }
122
123        return $content;
124    }
125
126    /**
127     * Should this block load asynchronously using AJAX?
128     *
129     * Simple blocks are faster in-line, more complex ones can be loaded later.
130     *
131     * @return bool
132     */
133    public function loadAjax(): bool
134    {
135        return false;
136    }
137
138    /**
139     * Can this block be shown on the user’s home page?
140     *
141     * @return bool
142     */
143    public function isUserBlock(): bool
144    {
145        return true;
146    }
147
148    /**
149     * Can this block be shown on the tree’s home page?
150     *
151     * @return bool
152     */
153    public function isTreeBlock(): bool
154    {
155        return false;
156    }
157}
158