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