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