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