xref: /webtrees/app/Module/LoginBlockModule.php (revision 3976b4703df669696105ed6b024b96d433c8fbdb)
1e2a378d3SGreg Roach<?php
2*3976b470SGreg 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 */
17e7f56f2aSGreg Roachdeclare(strict_types=1);
18e7f56f2aSGreg Roach
1976692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2076692c8bSGreg Roach
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Site;
24e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
251e7a7a28SGreg Roachuse Illuminate\Support\Str;
26e2a378d3SGreg Roach
27e2a378d3SGreg Roach/**
28e2a378d3SGreg Roach * Class LoginBlockModule
29e2a378d3SGreg Roach */
3037eb8894SGreg Roachclass LoginBlockModule extends AbstractModule implements ModuleBlockInterface
31c1010edaSGreg Roach{
3249a243cbSGreg Roach    use ModuleBlockTrait;
3349a243cbSGreg Roach
34961ec755SGreg Roach    /**
350cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
36961ec755SGreg Roach     *
37961ec755SGreg Roach     * @return string
38961ec755SGreg Roach     */
3949a243cbSGreg Roach    public function title(): string
40c1010edaSGreg Roach    {
41bbb76c12SGreg Roach        /* I18N: Name of a module */
42bbb76c12SGreg Roach        return I18N::translate('Sign in');
43e2a378d3SGreg Roach    }
44e2a378d3SGreg Roach
45961ec755SGreg Roach    /**
46961ec755SGreg Roach     * A sentence describing what this module does.
47961ec755SGreg Roach     *
48961ec755SGreg Roach     * @return string
49961ec755SGreg Roach     */
5049a243cbSGreg Roach    public function description(): string
51c1010edaSGreg Roach    {
52bbb76c12SGreg Roach        /* I18N: Description of the “Sign in” module */
53bbb76c12SGreg Roach        return I18N::translate('An alternative way to sign in and sign out.');
54e2a378d3SGreg Roach    }
55e2a378d3SGreg Roach
5676692c8bSGreg Roach    /**
5776692c8bSGreg Roach     * Generate the HTML content of this block.
5876692c8bSGreg Roach     *
59e490cd80SGreg Roach     * @param Tree     $tree
6076692c8bSGreg Roach     * @param int      $block_id
613caaa4d2SGreg Roach     * @param string   $context
623caaa4d2SGreg Roach     * @param string[] $config
6376692c8bSGreg Roach     *
6476692c8bSGreg Roach     * @return string
6576692c8bSGreg Roach     */
663caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
67c1010edaSGreg Roach    {
68e2a378d3SGreg Roach        if (Auth::check()) {
69cdc90107SGreg Roach            $title   = I18N::translate('Sign out');
70147e99aaSGreg Roach            $content = view('modules/login_block/sign-out', [
71cf30a8edSGreg Roach                'user' => Auth::user(),
72cf30a8edSGreg Roach            ]);
73e2a378d3SGreg Roach        } else {
74cdc90107SGreg Roach            $title   = I18N::translate('Sign in');
75147e99aaSGreg Roach            $content = view('modules/login_block/sign-in', [
76c1010edaSGreg Roach                'allow_register' => (bool) Site::getPreference('USE_REGISTRATION_MODULE'),
77d8d70c25SGreg Roach                'url'            => route('tree-page', ['ged' => $tree->name()]),
78cf30a8edSGreg Roach            ]);
79e2a378d3SGreg Roach        }
80e2a378d3SGreg Roach
813caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
82147e99aaSGreg Roach            return view('modules/block-template', [
831e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
849c6524dcSGreg Roach                'id'         => $block_id,
859c6524dcSGreg Roach                'config_url' => '',
86cf30a8edSGreg Roach                'title'      => $title,
879c6524dcSGreg Roach                'content'    => $content,
889c6524dcSGreg Roach            ]);
89e2a378d3SGreg Roach        }
90b2ce94c6SRico Sonntag
91b2ce94c6SRico Sonntag        return $content;
92e2a378d3SGreg Roach    }
93e2a378d3SGreg Roach
943caaa4d2SGreg Roach    /**
953caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
963caaa4d2SGreg Roach     *
973caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
983caaa4d2SGreg Roach     *
993caaa4d2SGreg Roach     * @return bool
1003caaa4d2SGreg Roach     */
101c1010edaSGreg Roach    public function loadAjax(): bool
102c1010edaSGreg Roach    {
103e2a378d3SGreg Roach        return false;
104e2a378d3SGreg Roach    }
105e2a378d3SGreg Roach
1063caaa4d2SGreg Roach    /**
1073caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1083caaa4d2SGreg Roach     *
1093caaa4d2SGreg Roach     * @return bool
1103caaa4d2SGreg Roach     */
111c1010edaSGreg Roach    public function isUserBlock(): bool
112c1010edaSGreg Roach    {
113e2a378d3SGreg Roach        return true;
114e2a378d3SGreg Roach    }
115e2a378d3SGreg Roach
1163caaa4d2SGreg Roach    /**
1173caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
1183caaa4d2SGreg Roach     *
1193caaa4d2SGreg Roach     * @return bool
1203caaa4d2SGreg Roach     */
12163276d8fSGreg Roach    public function isTreeBlock(): bool
122c1010edaSGreg Roach    {
123e2a378d3SGreg Roach        return true;
124e2a378d3SGreg Roach    }
125e2a378d3SGreg Roach}
126