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