xref: /webtrees/app/Module/LoginBlockModule.php (revision d72b284a0846ca045e548a1c77ad11813bcbab92)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Module;
20
21use Fisharebest\Webtrees\Auth;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Site;
24use Fisharebest\Webtrees\Tree;
25use Illuminate\Support\Str;
26
27/**
28 * Class LoginBlockModule
29 */
30class LoginBlockModule extends AbstractModule implements ModuleBlockInterface
31{
32    use ModuleBlockTrait;
33
34    /**
35     * How should this module be identified in the control panel, etc.?
36     *
37     * @return string
38     */
39    public function title(): string
40    {
41        /* I18N: Name of a module */
42        return I18N::translate('Sign in');
43    }
44
45    /**
46     * A sentence describing what this module does.
47     *
48     * @return string
49     */
50    public function description(): string
51    {
52        /* I18N: Description of the “Sign in” module */
53        return I18N::translate('An alternative way to sign in and sign out.');
54    }
55
56    /**
57     * Generate the HTML content of this block.
58     *
59     * @param Tree     $tree
60     * @param int      $block_id
61     * @param string   $context
62     * @param string[] $config
63     *
64     * @return string
65     */
66    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
67    {
68        if (Auth::check()) {
69            $title   = I18N::translate('Sign out');
70            $content = view('modules/login_block/sign-out', [
71                'user' => Auth::user(),
72            ]);
73        } else {
74            $title   = I18N::translate('Sign in');
75            $content = view('modules/login_block/sign-in', [
76                'allow_register' => (bool) Site::getPreference('USE_REGISTRATION_MODULE'),
77                'url'            => route('tree-page', ['tree' => $tree->name()]),
78            ]);
79        }
80
81        if ($context !== self::CONTEXT_EMBED) {
82            return view('modules/block-template', [
83                'block'      => Str::kebab($this->name()),
84                'id'         => $block_id,
85                'config_url' => '',
86                'title'      => $title,
87                'content'    => $content,
88            ]);
89        }
90
91        return $content;
92    }
93
94    /**
95     * Should this block load asynchronously using AJAX?
96     *
97     * Simple blocks are faster in-line, more complex ones can be loaded later.
98     *
99     * @return bool
100     */
101    public function loadAjax(): bool
102    {
103        return false;
104    }
105
106    /**
107     * Can this block be shown on the user’s home page?
108     *
109     * @return bool
110     */
111    public function isUserBlock(): bool
112    {
113        return true;
114    }
115
116    /**
117     * Can this block be shown on the tree’s home page?
118     *
119     * @return bool
120     */
121    public function isTreeBlock(): bool
122    {
123        return true;
124    }
125}
126