xref: /webtrees/app/Module/LoginBlockModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
1e2a378d3SGreg Roach<?php
23976b470SGreg Roach
3e2a378d3SGreg Roach/**
4e2a378d3SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6e2a378d3SGreg Roach * This program is free software: you can redistribute it and/or modify
7e2a378d3SGreg Roach * it under the terms of the GNU General Public License as published by
8e2a378d3SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9e2a378d3SGreg Roach * (at your option) any later version.
10e2a378d3SGreg Roach * This program is distributed in the hope that it will be useful,
11e2a378d3SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12e2a378d3SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13e2a378d3SGreg Roach * GNU General Public License for more details.
14e2a378d3SGreg 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/>.
16e2a378d3SGreg Roach */
17fcfa147eSGreg 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;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Site;
25e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
261e7a7a28SGreg Roachuse Illuminate\Support\Str;
27e2a378d3SGreg Roach
28e2a378d3SGreg Roach/**
29e2a378d3SGreg Roach * Class LoginBlockModule
30e2a378d3SGreg Roach */
3137eb8894SGreg Roachclass LoginBlockModule extends AbstractModule implements ModuleBlockInterface
32c1010edaSGreg Roach{
3349a243cbSGreg Roach    use ModuleBlockTrait;
3449a243cbSGreg Roach
35961ec755SGreg Roach    /**
360cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
37961ec755SGreg Roach     *
38961ec755SGreg Roach     * @return string
39961ec755SGreg Roach     */
4049a243cbSGreg Roach    public function title(): string
41c1010edaSGreg Roach    {
42bbb76c12SGreg Roach        /* I18N: Name of a module */
43bbb76c12SGreg Roach        return I18N::translate('Sign in');
44e2a378d3SGreg Roach    }
45e2a378d3SGreg Roach
4649a243cbSGreg Roach    public function description(): string
47c1010edaSGreg Roach    {
48bbb76c12SGreg Roach        /* I18N: Description of the “Sign in” module */
49bbb76c12SGreg Roach        return I18N::translate('An alternative way to sign in and sign out.');
50e2a378d3SGreg Roach    }
51e2a378d3SGreg Roach
5276692c8bSGreg Roach    /**
5376692c8bSGreg Roach     * Generate the HTML content of this block.
5476692c8bSGreg Roach     *
55e490cd80SGreg Roach     * @param Tree                 $tree
5676692c8bSGreg Roach     * @param int                  $block_id
573caaa4d2SGreg Roach     * @param string               $context
5876d39c55SGreg Roach     * @param array<string,string> $config
5976692c8bSGreg Roach     *
6076692c8bSGreg Roach     * @return string
6176692c8bSGreg Roach     */
623caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
63c1010edaSGreg Roach    {
64e2a378d3SGreg Roach        if (Auth::check()) {
65cdc90107SGreg Roach            $title   = I18N::translate('Sign out');
66147e99aaSGreg Roach            $content = view('modules/login_block/sign-out', [
67cf30a8edSGreg Roach                'user' => Auth::user(),
68cf30a8edSGreg Roach            ]);
69e2a378d3SGreg Roach        } else {
70cdc90107SGreg Roach            $title   = I18N::translate('Sign in');
71147e99aaSGreg Roach            $content = view('modules/login_block/sign-in', [
72c1010edaSGreg Roach                'allow_register' => (bool) Site::getPreference('USE_REGISTRATION_MODULE'),
73ef5d23f1SGreg Roach                'tree'           => $tree,
74cf30a8edSGreg Roach            ]);
75e2a378d3SGreg Roach        }
76e2a378d3SGreg Roach
773caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
78147e99aaSGreg Roach            return view('modules/block-template', [
791e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
809c6524dcSGreg Roach                'id'         => $block_id,
819c6524dcSGreg Roach                'config_url' => '',
82cf30a8edSGreg Roach                'title'      => $title,
839c6524dcSGreg Roach                'content'    => $content,
849c6524dcSGreg Roach            ]);
85e2a378d3SGreg Roach        }
86b2ce94c6SRico Sonntag
87b2ce94c6SRico Sonntag        return $content;
88e2a378d3SGreg Roach    }
89e2a378d3SGreg Roach
903caaa4d2SGreg Roach    /**
913caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
923caaa4d2SGreg Roach     *
933caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
943caaa4d2SGreg Roach     *
953caaa4d2SGreg Roach     * @return bool
963caaa4d2SGreg Roach     */
97c1010edaSGreg Roach    public function loadAjax(): bool
98c1010edaSGreg Roach    {
99e2a378d3SGreg Roach        return false;
100e2a378d3SGreg Roach    }
101e2a378d3SGreg Roach
1023caaa4d2SGreg Roach    /**
1033caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1043caaa4d2SGreg Roach     *
1053caaa4d2SGreg Roach     * @return bool
1063caaa4d2SGreg Roach     */
107c1010edaSGreg Roach    public function isUserBlock(): bool
108c1010edaSGreg Roach    {
109e2a378d3SGreg Roach        return true;
110e2a378d3SGreg Roach    }
111e2a378d3SGreg Roach
1123caaa4d2SGreg Roach    /**
1133caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
1143caaa4d2SGreg Roach     *
1153caaa4d2SGreg Roach     * @return bool
1163caaa4d2SGreg Roach     */
11763276d8fSGreg Roach    public function isTreeBlock(): bool
118c1010edaSGreg Roach    {
119e2a378d3SGreg Roach        return true;
120e2a378d3SGreg Roach    }
121e2a378d3SGreg Roach}
122