xref: /webtrees/app/Module/ModuleTabTrait.php (revision b8fc901f205cd6af65496b916bf63547a3065a2f)
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 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Individual;
24use Fisharebest\Webtrees\Tree;
25use Illuminate\Support\Collection;
26use Psr\Http\Message\ResponseInterface;
27use Psr\Http\Message\ServerRequestInterface;
28use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
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     * @return string
44     */
45    abstract public function title(): string;
46
47    /**
48     * Get a the current access level for a module
49     *
50     * @param Tree   $tree
51     * @param string $interface
52     *
53     * @return int
54     */
55    abstract public function accessLevel(Tree $tree, string $interface): int;
56
57    /**
58     * Generate the HTML content of this tab.
59     *
60     * @param Individual $individual
61     *
62     * @return string
63     */
64    abstract public function getTabContent(Individual $individual): string;
65
66    /**
67     * The text that appears on the tab.
68     *
69     * @return string
70     */
71    public function tabTitle(): string
72    {
73        return $this->title();
74    }
75
76    /**
77     * Users change change the order of tabs using the control panel.
78     *
79     * @param int $tab_order
80     *
81     * @return void
82     */
83    public function setTabOrder(int $tab_order): void
84    {
85        $this->tab_order = $tab_order;
86    }
87
88    /**
89     * Users change change the order of tabs using the control panel.
90     *
91     * @return int
92     */
93    public function getTabOrder(): int
94    {
95        return $this->tab_order ?? $this->defaultTabOrder();
96    }
97
98    /**
99     * The default position for this tab.  It can be changed in the control panel.
100     *
101     * @return int
102     */
103    public function defaultTabOrder(): int
104    {
105        return 9999;
106    }
107
108    /**
109     * This module handles the following facts - so don't show them on the "Facts and events" tab.
110     *
111     * @return Collection
112     */
113    public function supportedFacts(): Collection
114    {
115        return new Collection([]);
116    }
117
118    /**
119     * @param ServerRequestInterface $request
120     *
121     * @return ResponseInterface
122     */
123    public function getTabAction(ServerRequestInterface $request): ResponseInterface
124    {
125        $tree = $request->getAttribute('tree');
126        assert($tree instanceof Tree);
127
128        $xref = $request->getQueryParams()['xref'];
129
130        $record = Individual::getInstance($xref, $tree);
131        $record = Auth::checkIndividualAccess($record);
132
133        $user = $request->getAttribute('user');
134
135        if ($this->accessLevel($tree, 'tab') < Auth::accessLevel($tree, $user)) {
136            throw new AccessDeniedHttpException('Access denied');
137        }
138
139        $layout = view('layouts/ajax', [
140            'content' => $this->getTabContent($record),
141        ]);
142
143        return response($layout);
144    }
145}
146