xref: /webtrees/app/Module/LoginBlockModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Site;
25use Fisharebest\Webtrees\Tree;
26use Illuminate\Support\Str;
27
28/**
29 * Class LoginBlockModule
30 */
31class LoginBlockModule extends AbstractModule implements ModuleBlockInterface
32{
33    use ModuleBlockTrait;
34
35    /**
36     * How should this module be identified in the control panel, etc.?
37     *
38     * @return string
39     */
40    public function title(): string
41    {
42        /* I18N: Name of a module */
43        return I18N::translate('Sign in');
44    }
45
46    public function description(): string
47    {
48        /* I18N: Description of the “Sign in” module */
49        return I18N::translate('An alternative way to sign in and sign out.');
50    }
51
52    /**
53     * Generate the HTML content of this block.
54     *
55     * @param Tree                 $tree
56     * @param int                  $block_id
57     * @param string               $context
58     * @param array<string,string> $config
59     *
60     * @return string
61     */
62    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
63    {
64        if (Auth::check()) {
65            $title   = I18N::translate('Sign out');
66            $content = view('modules/login_block/sign-out', [
67                'user' => Auth::user(),
68            ]);
69        } else {
70            $title   = I18N::translate('Sign in');
71            $content = view('modules/login_block/sign-in', [
72                'allow_register' => (bool) Site::getPreference('USE_REGISTRATION_MODULE'),
73                'tree'           => $tree,
74            ]);
75        }
76
77        if ($context !== self::CONTEXT_EMBED) {
78            return view('modules/block-template', [
79                'block'      => Str::kebab($this->name()),
80                'id'         => $block_id,
81                'config_url' => '',
82                'title'      => $title,
83                'content'    => $content,
84            ]);
85        }
86
87        return $content;
88    }
89
90    /**
91     * Should this block load asynchronously using AJAX?
92     *
93     * Simple blocks are faster in-line, more complex ones can be loaded later.
94     *
95     * @return bool
96     */
97    public function loadAjax(): bool
98    {
99        return false;
100    }
101
102    /**
103     * Can this block be shown on the user’s home page?
104     *
105     * @return bool
106     */
107    public function isUserBlock(): bool
108    {
109        return true;
110    }
111
112    /**
113     * Can this block be shown on the tree’s home page?
114     *
115     * @return bool
116     */
117    public function isTreeBlock(): bool
118    {
119        return true;
120    }
121}
122