xref: /webtrees/app/Module/LoginBlockModule.php (revision ef5d23f1e3b84e7c7a8ab467240adde5c2b4291c)
1e2a378d3SGreg Roach<?php
23976b470SGreg Roach
3e2a378d3SGreg Roach/**
4e2a378d3SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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
15e2a378d3SGreg Roach * along with this program. If not, see <http://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
46961ec755SGreg Roach    /**
47961ec755SGreg Roach     * A sentence describing what this module does.
48961ec755SGreg Roach     *
49961ec755SGreg Roach     * @return string
50961ec755SGreg Roach     */
5149a243cbSGreg Roach    public function description(): string
52c1010edaSGreg Roach    {
53bbb76c12SGreg Roach        /* I18N: Description of the “Sign in” module */
54bbb76c12SGreg Roach        return I18N::translate('An alternative way to sign in and sign out.');
55e2a378d3SGreg Roach    }
56e2a378d3SGreg Roach
5776692c8bSGreg Roach    /**
5876692c8bSGreg Roach     * Generate the HTML content of this block.
5976692c8bSGreg Roach     *
60e490cd80SGreg Roach     * @param Tree     $tree
6176692c8bSGreg Roach     * @param int      $block_id
623caaa4d2SGreg Roach     * @param string   $context
633caaa4d2SGreg Roach     * @param string[] $config
6476692c8bSGreg Roach     *
6576692c8bSGreg Roach     * @return string
6676692c8bSGreg Roach     */
673caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
68c1010edaSGreg Roach    {
69e2a378d3SGreg Roach        if (Auth::check()) {
70cdc90107SGreg Roach            $title   = I18N::translate('Sign out');
71147e99aaSGreg Roach            $content = view('modules/login_block/sign-out', [
72cf30a8edSGreg Roach                'user' => Auth::user(),
73cf30a8edSGreg Roach            ]);
74e2a378d3SGreg Roach        } else {
75cdc90107SGreg Roach            $title   = I18N::translate('Sign in');
76147e99aaSGreg Roach            $content = view('modules/login_block/sign-in', [
77c1010edaSGreg Roach                'allow_register' => (bool) Site::getPreference('USE_REGISTRATION_MODULE'),
78*ef5d23f1SGreg Roach                'tree'           => $tree,
79cf30a8edSGreg Roach            ]);
80e2a378d3SGreg Roach        }
81e2a378d3SGreg Roach
823caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
83147e99aaSGreg Roach            return view('modules/block-template', [
841e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
859c6524dcSGreg Roach                'id'         => $block_id,
869c6524dcSGreg Roach                'config_url' => '',
87cf30a8edSGreg Roach                'title'      => $title,
889c6524dcSGreg Roach                'content'    => $content,
899c6524dcSGreg Roach            ]);
90e2a378d3SGreg Roach        }
91b2ce94c6SRico Sonntag
92b2ce94c6SRico Sonntag        return $content;
93e2a378d3SGreg Roach    }
94e2a378d3SGreg Roach
953caaa4d2SGreg Roach    /**
963caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
973caaa4d2SGreg Roach     *
983caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
993caaa4d2SGreg Roach     *
1003caaa4d2SGreg Roach     * @return bool
1013caaa4d2SGreg Roach     */
102c1010edaSGreg Roach    public function loadAjax(): bool
103c1010edaSGreg Roach    {
104e2a378d3SGreg Roach        return false;
105e2a378d3SGreg Roach    }
106e2a378d3SGreg Roach
1073caaa4d2SGreg Roach    /**
1083caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1093caaa4d2SGreg Roach     *
1103caaa4d2SGreg Roach     * @return bool
1113caaa4d2SGreg Roach     */
112c1010edaSGreg Roach    public function isUserBlock(): bool
113c1010edaSGreg Roach    {
114e2a378d3SGreg Roach        return true;
115e2a378d3SGreg Roach    }
116e2a378d3SGreg Roach
1173caaa4d2SGreg Roach    /**
1183caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
1193caaa4d2SGreg Roach     *
1203caaa4d2SGreg Roach     * @return bool
1213caaa4d2SGreg Roach     */
12263276d8fSGreg Roach    public function isTreeBlock(): bool
123c1010edaSGreg Roach    {
124e2a378d3SGreg Roach        return true;
125e2a378d3SGreg Roach    }
126e2a378d3SGreg Roach}
127