xref: /webtrees/app/Module/ModuleTabTrait.php (revision a9b613b19a7bebd6295716a08ffb33e94a76bd34)
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 the current access level for a module
62     *
63     * @template T of ModuleInterface
64     *
65     * @param Tree            $tree
66     * @param class-string<T> $interface
67     *
68     * @return int
69     */
70    abstract public function accessLevel(Tree $tree, string $interface): int;
71
72    /**
73     * Users change change the order of tabs using the control panel.
74     *
75     * @param int $tab_order
76     *
77     * @return void
78     */
79    public function setTabOrder(int $tab_order): void
80    {
81        $this->tab_order = $tab_order;
82    }
83
84    /**
85     * Users change change the order of tabs using the control panel.
86     *
87     * @return int
88     */
89    public function getTabOrder(): int
90    {
91        return $this->tab_order ?? $this->defaultTabOrder();
92    }
93
94    /**
95     * The default position for this tab.  It can be changed in the control panel.
96     *
97     * @return int
98     */
99    public function defaultTabOrder(): int
100    {
101        return 9999;
102    }
103
104    /**
105     * This module handles the following facts - so don't show them on the "Facts and events" tab.
106     *
107     * @return Collection<int,string>
108     */
109    public function supportedFacts(): Collection
110    {
111        return new Collection();
112    }
113
114    /**
115     * Generate the HTML content of this tab.
116     *
117     * @param Individual $individual
118     *
119     * @return string
120     */
121    public function getTabContent(Individual $individual): string
122    {
123        return '';
124    }
125
126    /**
127     * @param ServerRequestInterface $request
128     *
129     * @return ResponseInterface
130     */
131    public function getTabAction(ServerRequestInterface $request): ResponseInterface
132    {
133        $tree = Validator::attributes($request)->tree();
134        $user = Validator::attributes($request)->user();
135        $xref = Validator::queryParams($request)->isXref()->string('xref');
136
137        $record = Registry::individualFactory()->make($xref, $tree);
138        $record = Auth::checkIndividualAccess($record);
139
140        if ($this->accessLevel($tree, ModuleTabInterface::class) < Auth::accessLevel($tree, $user)) {
141            throw new HttpAccessDeniedException();
142        }
143
144        $layout = view('layouts/ajax', [
145            'content' => $this->getTabContent($record),
146        ]);
147
148        return response($layout);
149    }
150}
151