xref: /webtrees/app/Module/ModuleTabTrait.php (revision 24f2a3af38709f9bf0a739b30264240d20ba34e8)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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\Exceptions\HttpAccessDeniedException;
24use Fisharebest\Webtrees\Registry;
25use Fisharebest\Webtrees\Tree;
26use Illuminate\Support\Collection;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
29
30use function assert;
31use function response;
32use function view;
33
34/**
35 * Trait ModuleTabTrait - default implementation of ModuleTabInterface
36 */
37trait ModuleTabTrait
38{
39    /** @var int The default position for this tab.  It can be changed in the control panel. */
40    protected $tab_order;
41
42    /**
43     * The text that appears on the tab.
44     *
45     * @return string
46     */
47    public function tabTitle(): string
48    {
49        return $this->title();
50    }
51
52    /**
53     * Users change change the order of tabs using the control panel.
54     *
55     * @param int $tab_order
56     *
57     * @return void
58     */
59    public function setTabOrder(int $tab_order): void
60    {
61        $this->tab_order = $tab_order;
62    }
63
64    /**
65     * Users change change the order of tabs using the control panel.
66     *
67     * @return int
68     */
69    public function getTabOrder(): int
70    {
71        return $this->tab_order ?? $this->defaultTabOrder();
72    }
73
74    /**
75     * The default position for this tab.  It can be changed in the control panel.
76     *
77     * @return int
78     */
79    public function defaultTabOrder(): int
80    {
81        return 9999;
82    }
83
84    /**
85     * This module handles the following facts - so don't show them on the "Facts and events" tab.
86     *
87     * @return Collection<string>
88     */
89    public function supportedFacts(): Collection
90    {
91        return new Collection();
92    }
93
94    /**
95     * @param ServerRequestInterface $request
96     *
97     * @return ResponseInterface
98     */
99    public function getTabAction(ServerRequestInterface $request): ResponseInterface
100    {
101        $tree = $request->getAttribute('tree');
102        assert($tree instanceof Tree);
103
104        $xref = $request->getQueryParams()['xref'];
105
106        $record = Registry::individualFactory()->make($xref, $tree);
107        $record = Auth::checkIndividualAccess($record);
108
109        $user = $request->getAttribute('user');
110
111        if ($this->accessLevel($tree, ModuleTabInterface::class) < Auth::accessLevel($tree, $user)) {
112            throw new HttpAccessDeniedException();
113        }
114
115        $layout = view('layouts/ajax', [
116            'content' => $this->getTabContent($record),
117        ]);
118
119        return response($layout);
120    }
121}
122