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