xref: /webtrees/app/Module/UserWelcomeModule.php (revision fcfa147e10aaa6c7ff580c29bd6e5b88666befc1)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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
158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17*fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
24a0ebf380SGreg Roachuse Fisharebest\Webtrees\Individual;
254ca7e03cSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
26e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
271e7a7a28SGreg Roachuse Illuminate\Support\Str;
288c2e8227SGreg Roach
298c2e8227SGreg Roach/**
308c2e8227SGreg Roach * Class UserWelcomeModule
318c2e8227SGreg Roach */
3237eb8894SGreg Roachclass UserWelcomeModule extends AbstractModule implements ModuleBlockInterface
33c1010edaSGreg Roach{
3449a243cbSGreg Roach    use ModuleBlockTrait;
3549a243cbSGreg Roach
36961ec755SGreg Roach    /**
374ca7e03cSGreg Roach     * @var ModuleService
384ca7e03cSGreg Roach     */
394ca7e03cSGreg Roach    private $module_service;
404ca7e03cSGreg Roach
414ca7e03cSGreg Roach    /**
424ca7e03cSGreg Roach     * UserWelcomeModule constructor.
434ca7e03cSGreg Roach     *
444ca7e03cSGreg Roach     * @param ModuleService $module_service
454ca7e03cSGreg Roach     */
465bdbe281SGreg Roach    public function __construct(ModuleService $module_service)
475bdbe281SGreg Roach    {
484ca7e03cSGreg Roach        $this->module_service = $module_service;
494ca7e03cSGreg Roach    }
504ca7e03cSGreg Roach
514ca7e03cSGreg Roach    /**
520cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
53961ec755SGreg Roach     *
54961ec755SGreg Roach     * @return string
55961ec755SGreg Roach     */
5649a243cbSGreg Roach    public function title(): string
57c1010edaSGreg Roach    {
58bbb76c12SGreg Roach        /* I18N: Name of a module */
59bbb76c12SGreg Roach        return I18N::translate('My page');
608c2e8227SGreg Roach    }
618c2e8227SGreg Roach
62961ec755SGreg Roach    /**
63961ec755SGreg Roach     * A sentence describing what this module does.
64961ec755SGreg Roach     *
65961ec755SGreg Roach     * @return string
66961ec755SGreg Roach     */
6749a243cbSGreg Roach    public function description(): string
68c1010edaSGreg Roach    {
69bbb76c12SGreg Roach        /* I18N: Description of the “My page” module */
70bbb76c12SGreg Roach        return I18N::translate('A greeting message and useful links for a user.');
718c2e8227SGreg Roach    }
728c2e8227SGreg Roach
7376692c8bSGreg Roach    /**
7476692c8bSGreg Roach     * Generate the HTML content of this block.
7576692c8bSGreg Roach     *
76e490cd80SGreg Roach     * @param Tree     $tree
7776692c8bSGreg Roach     * @param int      $block_id
783caaa4d2SGreg Roach     * @param string   $context
793caaa4d2SGreg Roach     * @param string[] $config
8076692c8bSGreg Roach     *
8176692c8bSGreg Roach     * @return string
8276692c8bSGreg Roach     */
833caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
84c1010edaSGreg Roach    {
85e490cd80SGreg Roach        $gedcomid   = $tree->getUserPreference(Auth::user(), 'gedcomid');
86e490cd80SGreg Roach        $individual = Individual::getInstance($gedcomid, $tree);
87a0ebf380SGreg Roach        $links      = [];
88a0ebf380SGreg Roach
8987cca37cSGreg Roach        $pedigree_chart = $this->module_service->findByComponent(ModuleChartInterface::class, $tree, Auth::user())
900797053bSGreg Roach            ->first(static function (ModuleInterface $module): bool {
9149a243cbSGreg Roach                return $module instanceof PedigreeChartModule;
920797053bSGreg Roach            });
9349a243cbSGreg Roach
9449a243cbSGreg Roach        if ($individual instanceof Individual) {
9549a243cbSGreg Roach            if ($pedigree_chart instanceof PedigreeChartModule) {
96a0ebf380SGreg Roach                $links[] = [
97f7dacbc4SGreg Roach                    'url'   => $pedigree_chart->chartUrl($individual),
98a0ebf380SGreg Roach                    'title' => I18N::translate('Default chart'),
99a0ebf380SGreg Roach                    'icon'  => 'icon-pedigree',
100a0ebf380SGreg Roach                ];
1013f8a84f7SDavid Drury            }
102a0ebf380SGreg Roach
103a0ebf380SGreg Roach            $links[] = [
104b1f1e4efSGreg Roach                'url'   => $individual->url(),
105a0ebf380SGreg Roach                'title' => I18N::translate('My individual record'),
106a0ebf380SGreg Roach                'icon'  => 'icon-indis',
107a0ebf380SGreg Roach            ];
1088c2e8227SGreg Roach        }
109a0ebf380SGreg Roach
110a0ebf380SGreg Roach        $links[] = [
1114653dc2eSGreg Roach            'url'   => route('my-account', []),
112a0ebf380SGreg Roach            'title' => I18N::translate('My account'),
113a0ebf380SGreg Roach            'icon'  => 'icon-mypage',
114a0ebf380SGreg Roach        ];
115147e99aaSGreg Roach        $content = view('modules/user_welcome/welcome', ['links' => $links]);
1168c2e8227SGreg Roach
117e5a6b4d4SGreg Roach        $real_name = '<span dir="auto">' . e(Auth::user()->realName()) . '</span>';
118334b74ddSGreg Roach
119bbb76c12SGreg Roach        /* I18N: A %s is the user’s name */
120334b74ddSGreg Roach        $title = I18N::translate('Welcome %s', $real_name);
121bbb76c12SGreg Roach
1223caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
123147e99aaSGreg Roach            return view('modules/block-template', [
1241e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
125d034ca3bSGreg Roach                'id'         => $block_id,
1261fa06fe3SGreg Roach                'config_url' => '',
127bbb76c12SGreg Roach                'title'      => $title,
128a0ebf380SGreg Roach                'content'    => $content,
129a0ebf380SGreg Roach            ]);
1308c2e8227SGreg Roach        }
131b2ce94c6SRico Sonntag
132b2ce94c6SRico Sonntag        return $content;
1338c2e8227SGreg Roach    }
1348c2e8227SGreg Roach
1353caaa4d2SGreg Roach    /**
1363caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1373caaa4d2SGreg Roach     *
1383caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1393caaa4d2SGreg Roach     *
1403caaa4d2SGreg Roach     * @return bool
1413caaa4d2SGreg Roach     */
142c1010edaSGreg Roach    public function loadAjax(): bool
143c1010edaSGreg Roach    {
1448c2e8227SGreg Roach        return false;
1458c2e8227SGreg Roach    }
1468c2e8227SGreg Roach
1473caaa4d2SGreg Roach    /**
1483caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1493caaa4d2SGreg Roach     *
1503caaa4d2SGreg Roach     * @return bool
1513caaa4d2SGreg Roach     */
152c1010edaSGreg Roach    public function isUserBlock(): bool
153c1010edaSGreg Roach    {
1548c2e8227SGreg Roach        return true;
1558c2e8227SGreg Roach    }
1568c2e8227SGreg Roach
1573caaa4d2SGreg Roach    /**
1583caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
1593caaa4d2SGreg Roach     *
1603caaa4d2SGreg Roach     * @return bool
1613caaa4d2SGreg Roach     */
16263276d8fSGreg Roach    public function isTreeBlock(): bool
163c1010edaSGreg Roach    {
1648c2e8227SGreg Roach        return false;
1658c2e8227SGreg Roach    }
1668c2e8227SGreg Roach}
167