xref: /webtrees/app/Module/WelcomeBlockModule.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;
2356f9a9c1SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\RegisterPage;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
254ca7e03cSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Site;
27e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
281e7a7a28SGreg Roachuse Illuminate\Support\Str;
298c2e8227SGreg Roach
308c2e8227SGreg Roach/**
318c2e8227SGreg Roach * Class WelcomeBlockModule
328c2e8227SGreg Roach */
3337eb8894SGreg Roachclass WelcomeBlockModule extends AbstractModule implements ModuleBlockInterface
34c1010edaSGreg Roach{
3549a243cbSGreg Roach    use ModuleBlockTrait;
3649a243cbSGreg Roach
3743f2f523SGreg Roach    private ModuleService $module_service;
384ca7e03cSGreg Roach
394ca7e03cSGreg Roach    /**
404ca7e03cSGreg Roach     * @param ModuleService $module_service
414ca7e03cSGreg Roach     */
424ca7e03cSGreg Roach    public function __construct(ModuleService $module_service)
434ca7e03cSGreg Roach    {
444ca7e03cSGreg Roach        $this->module_service = $module_service;
454ca7e03cSGreg Roach    }
464ca7e03cSGreg Roach
474ca7e03cSGreg Roach    /**
480cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
49961ec755SGreg Roach     *
50961ec755SGreg Roach     * @return string
51961ec755SGreg Roach     */
5249a243cbSGreg Roach    public function title(): string
53c1010edaSGreg Roach    {
54bbb76c12SGreg Roach        /* I18N: Name of a module */
55bbb76c12SGreg Roach        return I18N::translate('Home page');
568c2e8227SGreg Roach    }
578c2e8227SGreg Roach
5849a243cbSGreg Roach    public function description(): string
59c1010edaSGreg Roach    {
60bbb76c12SGreg Roach        /* I18N: Description of the “Home page” module */
61bbb76c12SGreg Roach        return I18N::translate('A greeting message for site visitors.');
628c2e8227SGreg Roach    }
638c2e8227SGreg Roach
6476692c8bSGreg Roach    /**
6576692c8bSGreg Roach     * Generate the HTML content of this block.
6676692c8bSGreg Roach     *
67e490cd80SGreg Roach     * @param Tree                 $tree
6876692c8bSGreg Roach     * @param int                  $block_id
693caaa4d2SGreg Roach     * @param string               $context
7076d39c55SGreg Roach     * @param array<string,string> $config
7176692c8bSGreg Roach     *
7276692c8bSGreg Roach     * @return string
7376692c8bSGreg Roach     */
743caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
75c1010edaSGreg Roach    {
76af343084SGreg Roach        $individual = $tree->significantIndividual(Auth::user());
7775d70144SGreg Roach
7875d70144SGreg Roach        $links = [];
7975d70144SGreg Roach
800797053bSGreg Roach        $pedigree_chart = $this->module_service
810797053bSGreg Roach            ->findByComponent(ModuleChartInterface::class, $tree, Auth::user())
82*f25fc0f9SGreg Roach            ->first(static fn (ModuleInterface $module): bool => $module instanceof PedigreeChartModule);
8349a243cbSGreg Roach
8449a243cbSGreg Roach        if ($pedigree_chart instanceof PedigreeChartModule) {
8575d70144SGreg Roach            $links[] = [
86f7dacbc4SGreg Roach                $pedigree_chart->chartUrl($individual),
87f7dacbc4SGreg Roach                'url'   => $pedigree_chart->chartUrl($individual),
8875d70144SGreg Roach                'title' => I18N::translate('Default chart'),
8975d70144SGreg Roach                'icon'  => 'icon-pedigree',
9075d70144SGreg Roach            ];
914eb71cfaSGreg Roach        }
9275d70144SGreg Roach
9375d70144SGreg Roach        $links[] = [
94b1f1e4efSGreg Roach            'url'   => $individual->url(),
9575d70144SGreg Roach            'title' => I18N::translate('Default individual'),
9675d70144SGreg Roach            'icon'  => 'icon-indis',
9775d70144SGreg Roach        ];
9875d70144SGreg Roach
9915d603e7SGreg Roach        if (Site::getPreference('USE_REGISTRATION_MODULE') === '1' && !Auth::check()) {
10075d70144SGreg Roach            $links[] = [
1019fa6ab69SGreg Roach                'url'   => route(RegisterPage::class, ['tree' => $tree->name()]),
10275d70144SGreg Roach                'title' => I18N::translate('Request a new user account'),
10375d70144SGreg Roach                'icon'  => 'icon-user_add',
10475d70144SGreg Roach            ];
1058c2e8227SGreg Roach        }
10675d70144SGreg Roach
107147e99aaSGreg Roach        $content = view('modules/gedcom_block/welcome', ['links' => $links]);
1088c2e8227SGreg Roach
1093caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
110147e99aaSGreg Roach            return view('modules/block-template', [
1111e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
112d034ca3bSGreg Roach                'id'         => $block_id,
1131fa06fe3SGreg Roach                'config_url' => '',
114a743d8a2SGreg Roach                'title'      => e($individual->tree()->title()),
11575d70144SGreg Roach                'content'    => $content,
11675d70144SGreg Roach            ]);
1178c2e8227SGreg Roach        }
118b2ce94c6SRico Sonntag
119b2ce94c6SRico Sonntag        return $content;
1208c2e8227SGreg Roach    }
1218c2e8227SGreg Roach
1223caaa4d2SGreg Roach    /**
1233caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1243caaa4d2SGreg Roach     *
1253caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1263caaa4d2SGreg Roach     *
1273caaa4d2SGreg Roach     * @return bool
1283caaa4d2SGreg Roach     */
129c1010edaSGreg Roach    public function loadAjax(): bool
130c1010edaSGreg Roach    {
1318c2e8227SGreg Roach        return false;
1328c2e8227SGreg Roach    }
1338c2e8227SGreg Roach
1343caaa4d2SGreg Roach    /**
1353caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1363caaa4d2SGreg Roach     *
1373caaa4d2SGreg Roach     * @return bool
1383caaa4d2SGreg Roach     */
139c1010edaSGreg Roach    public function isUserBlock(): bool
140c1010edaSGreg Roach    {
1418c2e8227SGreg Roach        return false;
1428c2e8227SGreg Roach    }
1438c2e8227SGreg Roach
1443caaa4d2SGreg Roach    /**
1453caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
1463caaa4d2SGreg Roach     *
1473caaa4d2SGreg Roach     * @return bool
1483caaa4d2SGreg Roach     */
14963276d8fSGreg Roach    public function isTreeBlock(): bool
150c1010edaSGreg Roach    {
1518c2e8227SGreg Roach        return true;
1528c2e8227SGreg Roach    }
1538c2e8227SGreg Roach}
154