xref: /webtrees/app/Module/ModuleTabTrait.php (revision d501c45d339d4a2d06248f9197d7875a4df14e48)
149a243cbSGreg Roach<?php
23976b470SGreg Roach
349a243cbSGreg Roach/**
449a243cbSGreg Roach * webtrees: online genealogy
549a243cbSGreg Roach * Copyright (C) 2019 webtrees development team
649a243cbSGreg Roach * This program is free software: you can redistribute it and/or modify
749a243cbSGreg Roach * it under the terms of the GNU General Public License as published by
849a243cbSGreg Roach * the Free Software Foundation, either version 3 of the License, or
949a243cbSGreg Roach * (at your option) any later version.
1049a243cbSGreg Roach * This program is distributed in the hope that it will be useful,
1149a243cbSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1249a243cbSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1349a243cbSGreg Roach * GNU General Public License for more details.
1449a243cbSGreg Roach * You should have received a copy of the GNU General Public License
1549a243cbSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
1649a243cbSGreg Roach */
17fcfa147eSGreg Roach
1849a243cbSGreg Roachdeclare(strict_types=1);
1949a243cbSGreg Roach
2049a243cbSGreg Roachnamespace Fisharebest\Webtrees\Module;
2149a243cbSGreg Roach
22852ede8cSGreg Roachuse Fisharebest\Webtrees\Auth;
23*d501c45dSGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpAccessDeniedException;
24852ede8cSGreg Roachuse Fisharebest\Webtrees\Individual;
25852ede8cSGreg Roachuse Fisharebest\Webtrees\Tree;
268eaf8709SGreg Roachuse Illuminate\Support\Collection;
27852ede8cSGreg Roachuse Psr\Http\Message\ResponseInterface;
28852ede8cSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
29852ede8cSGreg Roach
30852ede8cSGreg Roachuse function assert;
31852ede8cSGreg Roachuse function response;
32852ede8cSGreg Roachuse function view;
338eaf8709SGreg Roach
3449a243cbSGreg Roach/**
3549a243cbSGreg Roach * Trait ModuleTabTrait - default implementation of ModuleTabInterface
3649a243cbSGreg Roach */
3749a243cbSGreg Roachtrait ModuleTabTrait
3849a243cbSGreg Roach{
3949a243cbSGreg Roach    /** @var int The default position for this tab.  It can be changed in the control panel. */
40fb7a0427SGreg Roach    protected $tab_order;
4149a243cbSGreg Roach
4249a243cbSGreg Roach    /**
43e284d69fSGreg Roach     * @return string
44e284d69fSGreg Roach     */
45e284d69fSGreg Roach    abstract public function title(): string;
46e284d69fSGreg Roach
47e284d69fSGreg Roach    /**
48852ede8cSGreg Roach     * Get a the current access level for a module
49852ede8cSGreg Roach     *
50852ede8cSGreg Roach     * @param Tree   $tree
51852ede8cSGreg Roach     * @param string $interface
52852ede8cSGreg Roach     *
53852ede8cSGreg Roach     * @return int
54852ede8cSGreg Roach     */
55852ede8cSGreg Roach    abstract public function accessLevel(Tree $tree, string $interface): int;
56852ede8cSGreg Roach
57852ede8cSGreg Roach    /**
58852ede8cSGreg Roach     * Generate the HTML content of this tab.
59852ede8cSGreg Roach     *
60852ede8cSGreg Roach     * @param Individual $individual
61852ede8cSGreg Roach     *
62852ede8cSGreg Roach     * @return string
63852ede8cSGreg Roach     */
64852ede8cSGreg Roach    abstract public function getTabContent(Individual $individual): string;
65852ede8cSGreg Roach
66852ede8cSGreg Roach    /**
6739402588SGreg Roach     * The text that appears on the tab.
6839402588SGreg Roach     *
6939402588SGreg Roach     * @return string
7039402588SGreg Roach     */
7139402588SGreg Roach    public function tabTitle(): string
7239402588SGreg Roach    {
7339402588SGreg Roach        return $this->title();
7439402588SGreg Roach    }
7539402588SGreg Roach
7639402588SGreg Roach    /**
7749a243cbSGreg Roach     * Users change change the order of tabs using the control panel.
7849a243cbSGreg Roach     *
7949a243cbSGreg Roach     * @param int $tab_order
8049a243cbSGreg Roach     *
8149a243cbSGreg Roach     * @return void
8249a243cbSGreg Roach     */
8349a243cbSGreg Roach    public function setTabOrder(int $tab_order): void
8449a243cbSGreg Roach    {
8549a243cbSGreg Roach        $this->tab_order = $tab_order;
8649a243cbSGreg Roach    }
8749a243cbSGreg Roach
8849a243cbSGreg Roach    /**
8949a243cbSGreg Roach     * Users change change the order of tabs using the control panel.
9049a243cbSGreg Roach     *
9149a243cbSGreg Roach     * @return int
9249a243cbSGreg Roach     */
9349a243cbSGreg Roach    public function getTabOrder(): int
9449a243cbSGreg Roach    {
9524bd2cf5SGreg Roach        return $this->tab_order ?? $this->defaultTabOrder();
9649a243cbSGreg Roach    }
9749a243cbSGreg Roach
9849a243cbSGreg Roach    /**
9949a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
10049a243cbSGreg Roach     *
10149a243cbSGreg Roach     * @return int
10249a243cbSGreg Roach     */
103cbf4b7faSGreg Roach    public function defaultTabOrder(): int
104cbf4b7faSGreg Roach    {
10549a243cbSGreg Roach        return 9999;
10649a243cbSGreg Roach    }
1078eaf8709SGreg Roach
1088eaf8709SGreg Roach    /**
1098eaf8709SGreg Roach     * This module handles the following facts - so don't show them on the "Facts and events" tab.
1108eaf8709SGreg Roach     *
11154c7f8dfSGreg Roach     * @return Collection
1128eaf8709SGreg Roach     */
1138eaf8709SGreg Roach    public function supportedFacts(): Collection
1148eaf8709SGreg Roach    {
115075d1a05SGreg Roach        return new Collection();
1168eaf8709SGreg Roach    }
117852ede8cSGreg Roach
118852ede8cSGreg Roach    /**
119852ede8cSGreg Roach     * @param ServerRequestInterface $request
120852ede8cSGreg Roach     *
121852ede8cSGreg Roach     * @return ResponseInterface
122852ede8cSGreg Roach     */
123852ede8cSGreg Roach    public function getTabAction(ServerRequestInterface $request): ResponseInterface
124852ede8cSGreg Roach    {
125852ede8cSGreg Roach        $tree = $request->getAttribute('tree');
126852ede8cSGreg Roach        assert($tree instanceof Tree);
127852ede8cSGreg Roach
128852ede8cSGreg Roach        $xref = $request->getQueryParams()['xref'];
129852ede8cSGreg Roach
130852ede8cSGreg Roach        $record = Individual::getInstance($xref, $tree);
131852ede8cSGreg Roach        $record = Auth::checkIndividualAccess($record);
132852ede8cSGreg Roach
133852ede8cSGreg Roach        $user = $request->getAttribute('user');
134852ede8cSGreg Roach
135852ede8cSGreg Roach        if ($this->accessLevel($tree, 'tab') < Auth::accessLevel($tree, $user)) {
136*d501c45dSGreg Roach            throw new HttpAccessDeniedException();
137852ede8cSGreg Roach        }
138852ede8cSGreg Roach
139852ede8cSGreg Roach        $layout = view('layouts/ajax', [
140852ede8cSGreg Roach            'content' => $this->getTabContent($record),
141852ede8cSGreg Roach        ]);
142852ede8cSGreg Roach
143852ede8cSGreg Roach        return response($layout);
144852ede8cSGreg Roach    }
14549a243cbSGreg Roach}
146