xref: /webtrees/app/Module/ModuleTabTrait.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
149a243cbSGreg Roach<?php
23976b470SGreg Roach
349a243cbSGreg Roach/**
449a243cbSGreg Roach * webtrees: online genealogy
5*89f7189bSGreg Roach * Copyright (C) 2021 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
15*89f7189bSGreg 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;
23d501c45dSGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpAccessDeniedException;
246b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
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    /**
4339402588SGreg Roach     * The text that appears on the tab.
4439402588SGreg Roach     *
4539402588SGreg Roach     * @return string
4639402588SGreg Roach     */
4739402588SGreg Roach    public function tabTitle(): string
4839402588SGreg Roach    {
4939402588SGreg Roach        return $this->title();
5039402588SGreg Roach    }
5139402588SGreg Roach
5239402588SGreg Roach    /**
5349a243cbSGreg Roach     * Users change change the order of tabs using the control panel.
5449a243cbSGreg Roach     *
5549a243cbSGreg Roach     * @param int $tab_order
5649a243cbSGreg Roach     *
5749a243cbSGreg Roach     * @return void
5849a243cbSGreg Roach     */
5949a243cbSGreg Roach    public function setTabOrder(int $tab_order): void
6049a243cbSGreg Roach    {
6149a243cbSGreg Roach        $this->tab_order = $tab_order;
6249a243cbSGreg Roach    }
6349a243cbSGreg Roach
6449a243cbSGreg Roach    /**
6549a243cbSGreg Roach     * Users change change the order of tabs using the control panel.
6649a243cbSGreg Roach     *
6749a243cbSGreg Roach     * @return int
6849a243cbSGreg Roach     */
6949a243cbSGreg Roach    public function getTabOrder(): int
7049a243cbSGreg Roach    {
7124bd2cf5SGreg Roach        return $this->tab_order ?? $this->defaultTabOrder();
7249a243cbSGreg Roach    }
7349a243cbSGreg Roach
7449a243cbSGreg Roach    /**
7549a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
7649a243cbSGreg Roach     *
7749a243cbSGreg Roach     * @return int
7849a243cbSGreg Roach     */
79cbf4b7faSGreg Roach    public function defaultTabOrder(): int
80cbf4b7faSGreg Roach    {
8149a243cbSGreg Roach        return 9999;
8249a243cbSGreg Roach    }
838eaf8709SGreg Roach
848eaf8709SGreg Roach    /**
858eaf8709SGreg Roach     * This module handles the following facts - so don't show them on the "Facts and events" tab.
868eaf8709SGreg Roach     *
87b5c8fd7eSGreg Roach     * @return Collection<string>
888eaf8709SGreg Roach     */
898eaf8709SGreg Roach    public function supportedFacts(): Collection
908eaf8709SGreg Roach    {
91075d1a05SGreg Roach        return new Collection();
928eaf8709SGreg Roach    }
93852ede8cSGreg Roach
94852ede8cSGreg Roach    /**
95852ede8cSGreg Roach     * @param ServerRequestInterface $request
96852ede8cSGreg Roach     *
97852ede8cSGreg Roach     * @return ResponseInterface
98852ede8cSGreg Roach     */
99852ede8cSGreg Roach    public function getTabAction(ServerRequestInterface $request): ResponseInterface
100852ede8cSGreg Roach    {
101852ede8cSGreg Roach        $tree = $request->getAttribute('tree');
102852ede8cSGreg Roach        assert($tree instanceof Tree);
103852ede8cSGreg Roach
104852ede8cSGreg Roach        $xref = $request->getQueryParams()['xref'];
105852ede8cSGreg Roach
1066b9cb339SGreg Roach        $record = Registry::individualFactory()->make($xref, $tree);
107852ede8cSGreg Roach        $record = Auth::checkIndividualAccess($record);
108852ede8cSGreg Roach
109852ede8cSGreg Roach        $user = $request->getAttribute('user');
110852ede8cSGreg Roach
111ef483801SGreg Roach        if ($this->accessLevel($tree, ModuleTabInterface::class) < Auth::accessLevel($tree, $user)) {
112d501c45dSGreg Roach            throw new HttpAccessDeniedException();
113852ede8cSGreg Roach        }
114852ede8cSGreg Roach
115852ede8cSGreg Roach        $layout = view('layouts/ajax', [
116852ede8cSGreg Roach            'content' => $this->getTabContent($record),
117852ede8cSGreg Roach        ]);
118852ede8cSGreg Roach
119852ede8cSGreg Roach        return response($layout);
120852ede8cSGreg Roach    }
12149a243cbSGreg Roach}
122