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