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