xref: /webtrees/app/Module/UserWelcomeModule.php (revision 3340ecd27b8901a894bff51b7c40bfa2896a552b)
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    private ModuleService $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('My 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 “My page” module */
70        return I18N::translate('A greeting message and useful links for a user.');
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 array<string> $config
80     *
81     * @return string
82     */
83    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
84    {
85        $gedcomid   = $tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF);
86        $individual = Registry::individualFactory()->make($gedcomid, $tree);
87        $links      = [];
88
89        $pedigree_chart = $this->module_service->findByComponent(ModuleChartInterface::class, $tree, Auth::user())
90            ->first(static function (ModuleInterface $module): bool {
91                return $module instanceof PedigreeChartModule;
92            });
93
94        if ($individual instanceof Individual) {
95            if ($pedigree_chart instanceof PedigreeChartModule) {
96                $links[] = [
97                    'url'   => $pedigree_chart->chartUrl($individual),
98                    'title' => I18N::translate('Default chart'),
99                    'icon'  => 'icon-pedigree',
100                ];
101            }
102
103            $links[] = [
104                'url'   => $individual->url(),
105                'title' => I18N::translate('My individual record'),
106                'icon'  => 'icon-indis',
107            ];
108        }
109
110        $links[] = [
111            'url'   => route(AccountEdit::class, ['tree' => $tree->name()]),
112            'title' => I18N::translate('My account'),
113            'icon'  => 'icon-mypage',
114        ];
115        $content = view('modules/user_welcome/welcome', ['links' => $links]);
116
117        $real_name = "\u{2068}" . e(Auth::user()->realName()) . "\u{2069}";
118
119        /* I18N: A %s is the user’s name */
120        $title = I18N::translate('Welcome %s', $real_name);
121
122        if ($context !== self::CONTEXT_EMBED) {
123            return view('modules/block-template', [
124                'block'      => Str::kebab($this->name()),
125                'id'         => $block_id,
126                'config_url' => '',
127                'title'      => e($title),
128                'content'    => $content,
129            ]);
130        }
131
132        return $content;
133    }
134
135    /**
136     * Should this block load asynchronously using AJAX?
137     *
138     * Simple blocks are faster in-line, more complex ones can be loaded later.
139     *
140     * @return bool
141     */
142    public function loadAjax(): bool
143    {
144        return false;
145    }
146
147    /**
148     * Can this block be shown on the user’s home page?
149     *
150     * @return bool
151     */
152    public function isUserBlock(): bool
153    {
154        return true;
155    }
156
157    /**
158     * Can this block be shown on the tree’s home page?
159     *
160     * @return bool
161     */
162    public function isTreeBlock(): bool
163    {
164        return false;
165    }
166}
167