xref: /webtrees/app/Module/UserWelcomeModule.php (revision 9cad76450e19b542a70c3dfaef6fe5a028eaee70)
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\Module;
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     * How should this module be labelled on tabs, menus, etc.?
36     *
37     * @return string
38     */
39    public function title(): string
40    {
41        /* I18N: Name of a module */
42        return I18N::translate('My page');
43    }
44
45    /**
46     * A sentence describing what this module does.
47     *
48     * @return string
49     */
50    public function description(): string
51    {
52        /* I18N: Description of the “My page” module */
53        return I18N::translate('A greeting message and useful links for a user.');
54    }
55
56    /**
57     * Generate the HTML content of this block.
58     *
59     * @param Tree     $tree
60     * @param int      $block_id
61     * @param string   $ctype
62     * @param string[] $cfg
63     *
64     * @return string
65     */
66    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
67    {
68        $gedcomid   = $tree->getUserPreference(Auth::user(), 'gedcomid');
69        $individual = Individual::getInstance($gedcomid, $tree);
70        $links      = [];
71
72        $pedigree_chart = Module::findByComponent('chart', $tree, Auth::user())
73            ->filter(function (ModuleInterface $module): bool {
74                return $module instanceof PedigreeChartModule;
75            });
76
77        if ($individual instanceof Individual) {
78            if ($pedigree_chart instanceof PedigreeChartModule) {
79                $links[] = [
80                    'url'   => route('pedigree', [
81                        'xref' => $individual->xref(),
82                        'ged'  => $individual->tree()->name(),
83                    ]),
84                    'title' => I18N::translate('Default chart'),
85                    'icon'  => 'icon-pedigree',
86                ];
87            }
88
89            $links[] = [
90                'url'   => $individual->url(),
91                'title' => I18N::translate('My individual record'),
92                'icon'  => 'icon-indis',
93            ];
94        }
95
96        $links[] = [
97            'url'   => route('my-account', []),
98            'title' => I18N::translate('My account'),
99            'icon'  => 'icon-mypage',
100        ];
101        $content = view('modules/user_welcome/welcome', ['links' => $links]);
102
103        $real_name = '<span dir="auto">' . e(Auth::user()->getRealName()) . '</span>';
104
105        /* I18N: A %s is the user’s name */
106        $title = I18N::translate('Welcome %s', $real_name);
107
108        if ($ctype !== '') {
109            return view('modules/block-template', [
110                'block'      => str_replace('_', '-', $this->name()),
111                'id'         => $block_id,
112                'config_url' => '',
113                'title'      => $title,
114                'content'    => $content,
115            ]);
116        }
117
118        return $content;
119    }
120
121    /** {@inheritdoc} */
122    public function loadAjax(): bool
123    {
124        return false;
125    }
126
127    /** {@inheritdoc} */
128    public function isUserBlock(): bool
129    {
130        return true;
131    }
132
133    /** {@inheritdoc} */
134    public function isTreeBlock(): bool
135    {
136        return false;
137    }
138
139    /**
140     * Update the configuration for a block.
141     *
142     * @param Request $request
143     * @param int     $block_id
144     *
145     * @return void
146     */
147    public function saveBlockConfiguration(Request $request, int $block_id)
148    {
149    }
150
151    /**
152     * An HTML form to edit block settings
153     *
154     * @param Tree $tree
155     * @param int  $block_id
156     *
157     * @return void
158     */
159    public function editBlockConfiguration(Tree $tree, int $block_id)
160    {
161    }
162}
163