xref: /webtrees/app/Module/UserWelcomeModule.php (revision d0c1a61ef810518d72356545b8b85abf2582ef6a)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Auth;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Individual;
23use Fisharebest\Webtrees\Services\ModuleService;
24use Fisharebest\Webtrees\Tree;
25use Symfony\Component\HttpFoundation\Request;
26
27/**
28 * Class UserWelcomeModule
29 */
30class UserWelcomeModule extends AbstractModule implements ModuleBlockInterface
31{
32    use ModuleBlockTrait;
33
34    /**
35     * @var ModuleService
36     */
37    private $module_service;
38
39    /**
40     * UserWelcomeModule constructor.
41     *
42     * @param ModuleService $module_service
43     */
44    public function __construct(ModuleService $module_service)
45    {
46        $this->module_service = $module_service;
47    }
48
49    /**
50     * How should this module be identified in the control panel, etc.?
51     *
52     * @return string
53     */
54    public function title(): string
55    {
56        /* I18N: Name of a module */
57        return I18N::translate('My page');
58    }
59
60    /**
61     * A sentence describing what this module does.
62     *
63     * @return string
64     */
65    public function description(): string
66    {
67        /* I18N: Description of the “My page” module */
68        return I18N::translate('A greeting message and useful links for a user.');
69    }
70
71    /**
72     * Generate the HTML content of this block.
73     *
74     * @param Tree     $tree
75     * @param int      $block_id
76     * @param string   $ctype
77     * @param string[] $cfg
78     *
79     * @return string
80     */
81    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
82    {
83        $gedcomid   = $tree->getUserPreference(Auth::user(), 'gedcomid');
84        $individual = Individual::getInstance($gedcomid, $tree);
85        $links      = [];
86
87        $pedigree_chart = $this->module_service->findByComponent(ModuleChartInterface::class, $tree, Auth::user())
88            ->filter(function (ModuleInterface $module): bool {
89                return $module instanceof PedigreeChartModule;
90            });
91
92        if ($individual instanceof Individual) {
93            if ($pedigree_chart instanceof PedigreeChartModule) {
94                $links[] = [
95                    'url'   => route('pedigree', [
96                        'xref' => $individual->xref(),
97                        'ged'  => $individual->tree()->name(),
98                    ]),
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('my-account', []),
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 ($ctype !== '') {
124            return view('modules/block-template', [
125                'block'      => str_replace('_', '-', $this->name()),
126                'id'         => $block_id,
127                'config_url' => '',
128                'title'      => $title,
129                'content'    => $content,
130            ]);
131        }
132
133        return $content;
134    }
135
136    /** {@inheritdoc} */
137    public function loadAjax(): bool
138    {
139        return false;
140    }
141
142    /** {@inheritdoc} */
143    public function isUserBlock(): bool
144    {
145        return true;
146    }
147
148    /** {@inheritdoc} */
149    public function isTreeBlock(): bool
150    {
151        return false;
152    }
153
154    /**
155     * Update the configuration for a block.
156     *
157     * @param Request $request
158     * @param int     $block_id
159     *
160     * @return void
161     */
162    public function saveBlockConfiguration(Request $request, int $block_id): void
163    {
164    }
165
166    /**
167     * An HTML form to edit block settings
168     *
169     * @param Tree $tree
170     * @param int  $block_id
171     *
172     * @return void
173     */
174    public function editBlockConfiguration(Tree $tree, int $block_id): void
175    {
176    }
177}
178