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