xref: /webtrees/app/Module/UserWelcomeModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
231fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
24a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\AccountEdit;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
26a0ebf380SGreg Roachuse Fisharebest\Webtrees\Individual;
2709482a55SGreg Roachuse Fisharebest\Webtrees\Registry;
284ca7e03cSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
29e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
301e7a7a28SGreg Roachuse Illuminate\Support\Str;
318c2e8227SGreg Roach
328c2e8227SGreg Roach/**
338c2e8227SGreg Roach * Class UserWelcomeModule
348c2e8227SGreg Roach */
3537eb8894SGreg Roachclass UserWelcomeModule extends AbstractModule implements ModuleBlockInterface
36c1010edaSGreg Roach{
3749a243cbSGreg Roach    use ModuleBlockTrait;
3849a243cbSGreg Roach
3943f2f523SGreg Roach    private ModuleService $module_service;
404ca7e03cSGreg Roach
414ca7e03cSGreg Roach    /**
424ca7e03cSGreg Roach     * @param ModuleService $module_service
434ca7e03cSGreg Roach     */
445bdbe281SGreg Roach    public function __construct(ModuleService $module_service)
455bdbe281SGreg Roach    {
464ca7e03cSGreg Roach        $this->module_service = $module_service;
474ca7e03cSGreg Roach    }
484ca7e03cSGreg Roach
494ca7e03cSGreg Roach    /**
500cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
51961ec755SGreg Roach     *
52961ec755SGreg Roach     * @return string
53961ec755SGreg Roach     */
5449a243cbSGreg Roach    public function title(): string
55c1010edaSGreg Roach    {
56bbb76c12SGreg Roach        /* I18N: Name of a module */
57bbb76c12SGreg Roach        return I18N::translate('My page');
588c2e8227SGreg Roach    }
598c2e8227SGreg Roach
6049a243cbSGreg Roach    public function description(): string
61c1010edaSGreg Roach    {
62bbb76c12SGreg Roach        /* I18N: Description of the “My page” module */
63bbb76c12SGreg Roach        return I18N::translate('A greeting message and useful links for a user.');
648c2e8227SGreg Roach    }
658c2e8227SGreg Roach
6676692c8bSGreg Roach    /**
6776692c8bSGreg Roach     * Generate the HTML content of this block.
6876692c8bSGreg Roach     *
69e490cd80SGreg Roach     * @param Tree                 $tree
7076692c8bSGreg Roach     * @param int                  $block_id
713caaa4d2SGreg Roach     * @param string               $context
7276d39c55SGreg Roach     * @param array<string,string> $config
7376692c8bSGreg Roach     *
7476692c8bSGreg Roach     * @return string
7576692c8bSGreg Roach     */
763caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
77c1010edaSGreg Roach    {
781fe542e9SGreg Roach        $gedcomid   = $tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF);
796b9cb339SGreg Roach        $individual = Registry::individualFactory()->make($gedcomid, $tree);
80a0ebf380SGreg Roach        $links      = [];
81a0ebf380SGreg Roach
8287cca37cSGreg Roach        $pedigree_chart = $this->module_service->findByComponent(ModuleChartInterface::class, $tree, Auth::user())
83*f25fc0f9SGreg Roach            ->first(static fn (ModuleInterface $module): bool => $module instanceof PedigreeChartModule);
8449a243cbSGreg Roach
8549a243cbSGreg Roach        if ($individual instanceof Individual) {
8649a243cbSGreg Roach            if ($pedigree_chart instanceof PedigreeChartModule) {
87a0ebf380SGreg Roach                $links[] = [
88f7dacbc4SGreg Roach                    'url'   => $pedigree_chart->chartUrl($individual),
89a0ebf380SGreg Roach                    'title' => I18N::translate('Default chart'),
90a0ebf380SGreg Roach                    'icon'  => 'icon-pedigree',
91a0ebf380SGreg Roach                ];
923f8a84f7SDavid Drury            }
93a0ebf380SGreg Roach
94a0ebf380SGreg Roach            $links[] = [
95b1f1e4efSGreg Roach                'url'   => $individual->url(),
96a0ebf380SGreg Roach                'title' => I18N::translate('My individual record'),
97a0ebf380SGreg Roach                'icon'  => 'icon-indis',
98a0ebf380SGreg Roach            ];
998c2e8227SGreg Roach        }
100a0ebf380SGreg Roach
101a0ebf380SGreg Roach        $links[] = [
102a49d0e3fSGreg Roach            'url'   => route(AccountEdit::class, ['tree' => $tree->name()]),
103a0ebf380SGreg Roach            'title' => I18N::translate('My account'),
104a0ebf380SGreg Roach            'icon'  => 'icon-mypage',
105a0ebf380SGreg Roach        ];
106147e99aaSGreg Roach        $content = view('modules/user_welcome/welcome', ['links' => $links]);
1078c2e8227SGreg Roach
108a743d8a2SGreg Roach        $real_name = "\u{2068}" . e(Auth::user()->realName()) . "\u{2069}";
109334b74ddSGreg Roach
110bbb76c12SGreg Roach        /* I18N: A %s is the user’s name */
111334b74ddSGreg Roach        $title = I18N::translate('Welcome %s', $real_name);
112bbb76c12SGreg Roach
1133caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
114147e99aaSGreg Roach            return view('modules/block-template', [
1151e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
116d034ca3bSGreg Roach                'id'         => $block_id,
1171fa06fe3SGreg Roach                'config_url' => '',
1184c718a92SGreg Roach                'title'      => $title,
119a0ebf380SGreg Roach                'content'    => $content,
120a0ebf380SGreg Roach            ]);
1218c2e8227SGreg Roach        }
122b2ce94c6SRico Sonntag
123b2ce94c6SRico Sonntag        return $content;
1248c2e8227SGreg Roach    }
1258c2e8227SGreg Roach
1263caaa4d2SGreg Roach    /**
1273caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1283caaa4d2SGreg Roach     *
1293caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1303caaa4d2SGreg Roach     *
1313caaa4d2SGreg Roach     * @return bool
1323caaa4d2SGreg Roach     */
133c1010edaSGreg Roach    public function loadAjax(): bool
134c1010edaSGreg Roach    {
1358c2e8227SGreg Roach        return false;
1368c2e8227SGreg Roach    }
1378c2e8227SGreg Roach
1383caaa4d2SGreg Roach    /**
1393caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1403caaa4d2SGreg Roach     *
1413caaa4d2SGreg Roach     * @return bool
1423caaa4d2SGreg Roach     */
143c1010edaSGreg Roach    public function isUserBlock(): bool
144c1010edaSGreg Roach    {
1458c2e8227SGreg Roach        return true;
1468c2e8227SGreg Roach    }
1478c2e8227SGreg Roach
1483caaa4d2SGreg Roach    /**
1493caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
1503caaa4d2SGreg Roach     *
1513caaa4d2SGreg Roach     * @return bool
1523caaa4d2SGreg Roach     */
15363276d8fSGreg Roach    public function isTreeBlock(): bool
154c1010edaSGreg Roach    {
1558c2e8227SGreg Roach        return false;
1568c2e8227SGreg Roach    }
1578c2e8227SGreg Roach}
158