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