xref: /webtrees/app/Module/ModuleTabTrait.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
149a243cbSGreg Roach<?php
23976b470SGreg Roach
349a243cbSGreg Roach/**
449a243cbSGreg Roach * webtrees: online genealogy
5*5bfc6897SGreg Roach * Copyright (C) 2022 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
1589f7189bSGreg Roach * along with this program. If not, see <https://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;
2381b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException;
24bfed30e4SGreg Roachuse Fisharebest\Webtrees\Individual;
256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
26bfed30e4SGreg Roachuse Fisharebest\Webtrees\Tree;
27b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
288eaf8709SGreg Roachuse Illuminate\Support\Collection;
29852ede8cSGreg Roachuse Psr\Http\Message\ResponseInterface;
30852ede8cSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
31852ede8cSGreg Roach
32852ede8cSGreg Roachuse function response;
33852ede8cSGreg Roachuse function view;
348eaf8709SGreg Roach
3549a243cbSGreg Roach/**
3649a243cbSGreg Roach * Trait ModuleTabTrait - default implementation of ModuleTabInterface
3749a243cbSGreg Roach */
3849a243cbSGreg Roachtrait ModuleTabTrait
3949a243cbSGreg Roach{
4033c746f1SGreg Roach    // The default position for this tab.  It can be changed in the control panel.
4133c746f1SGreg Roach    protected int $tab_order;
4249a243cbSGreg Roach
4349a243cbSGreg Roach    /**
44bfed30e4SGreg Roach     * How should this module be identified in the control panel, etc.?
45bfed30e4SGreg Roach     *
46bfed30e4SGreg Roach     * @return string
47bfed30e4SGreg Roach     */
48bfed30e4SGreg Roach    abstract public function title(): string;
49bfed30e4SGreg Roach
50bfed30e4SGreg Roach    /**
5139402588SGreg Roach     * The text that appears on the tab.
5239402588SGreg Roach     *
5339402588SGreg Roach     * @return string
5439402588SGreg Roach     */
5539402588SGreg Roach    public function tabTitle(): string
5639402588SGreg Roach    {
5739402588SGreg Roach        return $this->title();
5839402588SGreg Roach    }
5939402588SGreg Roach
6039402588SGreg Roach    /**
61bfed30e4SGreg Roach     * Get a the current access level for a module
62bfed30e4SGreg Roach     *
63bfed30e4SGreg Roach     * @param Tree   $tree
64bfed30e4SGreg Roach     * @param string $interface
65bfed30e4SGreg Roach     *
66bfed30e4SGreg Roach     * @return int
67bfed30e4SGreg Roach     */
68bfed30e4SGreg Roach    abstract public function accessLevel(Tree $tree, string $interface): int;
69bfed30e4SGreg Roach
70bfed30e4SGreg Roach    /**
7149a243cbSGreg Roach     * Users change change the order of tabs using the control panel.
7249a243cbSGreg Roach     *
7349a243cbSGreg Roach     * @param int $tab_order
7449a243cbSGreg Roach     *
7549a243cbSGreg Roach     * @return void
7649a243cbSGreg Roach     */
7749a243cbSGreg Roach    public function setTabOrder(int $tab_order): void
7849a243cbSGreg Roach    {
7949a243cbSGreg Roach        $this->tab_order = $tab_order;
8049a243cbSGreg Roach    }
8149a243cbSGreg Roach
8249a243cbSGreg Roach    /**
8349a243cbSGreg Roach     * Users change change the order of tabs using the control panel.
8449a243cbSGreg Roach     *
8549a243cbSGreg Roach     * @return int
8649a243cbSGreg Roach     */
8749a243cbSGreg Roach    public function getTabOrder(): int
8849a243cbSGreg Roach    {
8924bd2cf5SGreg Roach        return $this->tab_order ?? $this->defaultTabOrder();
9049a243cbSGreg Roach    }
9149a243cbSGreg Roach
9249a243cbSGreg Roach    /**
9349a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
9449a243cbSGreg Roach     *
9549a243cbSGreg Roach     * @return int
9649a243cbSGreg Roach     */
97cbf4b7faSGreg Roach    public function defaultTabOrder(): int
98cbf4b7faSGreg Roach    {
9949a243cbSGreg Roach        return 9999;
10049a243cbSGreg Roach    }
1018eaf8709SGreg Roach
1028eaf8709SGreg Roach    /**
1038eaf8709SGreg Roach     * This module handles the following facts - so don't show them on the "Facts and events" tab.
1048eaf8709SGreg Roach     *
10536779af1SGreg Roach     * @return Collection<int,string>
1068eaf8709SGreg Roach     */
1078eaf8709SGreg Roach    public function supportedFacts(): Collection
1088eaf8709SGreg Roach    {
109075d1a05SGreg Roach        return new Collection();
1108eaf8709SGreg Roach    }
111852ede8cSGreg Roach
112852ede8cSGreg Roach    /**
113bfed30e4SGreg Roach     * Generate the HTML content of this tab.
114bfed30e4SGreg Roach     *
115bfed30e4SGreg Roach     * @param Individual $individual
116bfed30e4SGreg Roach     *
117bfed30e4SGreg Roach     * @return string
118bfed30e4SGreg Roach     */
119bfed30e4SGreg Roach    public function getTabContent(Individual $individual): string
120bfed30e4SGreg Roach    {
121bfed30e4SGreg Roach        return '';
122bfed30e4SGreg Roach    }
123bfed30e4SGreg Roach
124bfed30e4SGreg Roach    /**
125852ede8cSGreg Roach     * @param ServerRequestInterface $request
126852ede8cSGreg Roach     *
127852ede8cSGreg Roach     * @return ResponseInterface
128852ede8cSGreg Roach     */
129852ede8cSGreg Roach    public function getTabAction(ServerRequestInterface $request): ResponseInterface
130852ede8cSGreg Roach    {
131b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
132b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
133852ede8cSGreg Roach
134852ede8cSGreg Roach        $xref = $request->getQueryParams()['xref'];
135852ede8cSGreg Roach
1366b9cb339SGreg Roach        $record = Registry::individualFactory()->make($xref, $tree);
137852ede8cSGreg Roach        $record = Auth::checkIndividualAccess($record);
138852ede8cSGreg Roach
139ef483801SGreg Roach        if ($this->accessLevel($tree, ModuleTabInterface::class) < Auth::accessLevel($tree, $user)) {
140d501c45dSGreg Roach            throw new HttpAccessDeniedException();
141852ede8cSGreg Roach        }
142852ede8cSGreg Roach
143852ede8cSGreg Roach        $layout = view('layouts/ajax', [
144852ede8cSGreg Roach            'content' => $this->getTabContent($record),
145852ede8cSGreg Roach        ]);
146852ede8cSGreg Roach
147852ede8cSGreg Roach        return response($layout);
148852ede8cSGreg Roach    }
14949a243cbSGreg Roach}
150