xref: /webtrees/app/Module/InteractiveTreeModule.php (revision 71378461661e7642e52abe7d41c9cfffb3e5369b)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17e7f56f2aSGreg Roachdeclare(strict_types=1);
18e7f56f2aSGreg Roach
1976692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2076692c8bSGreg Roach
215edf1a44SGreg Roachuse Fisharebest\Webtrees\Auth;
220bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualAccessDeniedException;
230bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
24*71378461SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\ModuleAction;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
27168ff6f3Sric2016use Fisharebest\Webtrees\Menu;
28e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
296ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
318c2e8227SGreg Roach
328c2e8227SGreg Roach/**
338c2e8227SGreg Roach * Class InteractiveTreeModule
348c2e8227SGreg Roach * Tip : you could change the number of generations loaded before ajax calls both in individual page and in treeview page to optimize speed and server load
358c2e8227SGreg Roach */
3637eb8894SGreg Roachclass InteractiveTreeModule extends AbstractModule implements ModuleChartInterface, ModuleTabInterface
37c1010edaSGreg Roach{
3849a243cbSGreg Roach    use ModuleChartTrait;
3949a243cbSGreg Roach    use ModuleTabTrait;
4049a243cbSGreg Roach
41961ec755SGreg Roach    /**
420cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
43961ec755SGreg Roach     *
44961ec755SGreg Roach     * @return string
45961ec755SGreg Roach     */
4649a243cbSGreg Roach    public function title(): string
47c1010edaSGreg Roach    {
48bbb76c12SGreg Roach        /* I18N: Name of a module */
49bbb76c12SGreg Roach        return I18N::translate('Interactive tree');
508c2e8227SGreg Roach    }
518c2e8227SGreg Roach
52961ec755SGreg Roach    /**
53961ec755SGreg Roach     * A sentence describing what this module does.
54961ec755SGreg Roach     *
55961ec755SGreg Roach     * @return string
56961ec755SGreg Roach     */
5749a243cbSGreg Roach    public function description(): string
58c1010edaSGreg Roach    {
59bbb76c12SGreg Roach        /* I18N: Description of the “Interactive tree” module */
60bbb76c12SGreg Roach        return I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
618c2e8227SGreg Roach    }
628c2e8227SGreg Roach
6349a243cbSGreg Roach    /**
6449a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
6549a243cbSGreg Roach     *
6649a243cbSGreg Roach     * @return int
6749a243cbSGreg Roach     */
68cbf4b7faSGreg Roach    public function defaultTabOrder(): int
69cbf4b7faSGreg Roach    {
70fb7a0427SGreg Roach        return 7;
718c2e8227SGreg Roach    }
728c2e8227SGreg Roach
733caaa4d2SGreg Roach    /**
743caaa4d2SGreg Roach     * Generate the HTML content of this tab.
753caaa4d2SGreg Roach     *
763caaa4d2SGreg Roach     * @param Individual $individual
773caaa4d2SGreg Roach     *
783caaa4d2SGreg Roach     * @return string
793caaa4d2SGreg Roach     */
809b34404bSGreg Roach    public function getTabContent(Individual $individual): string
81c1010edaSGreg Roach    {
82225e381fSGreg Roach        $treeview = new TreeView('tvTab');
835edf1a44SGreg Roach
8465e02381SGreg Roach        [$html, $js] = $treeview->drawViewport($individual, 3);
858c2e8227SGreg Roach
865edf1a44SGreg Roach        return view('modules/interactive-tree/tab', [
87225e381fSGreg Roach            'html' => $html,
88225e381fSGreg Roach            'js'   => $js,
89225e381fSGreg Roach        ]);
908c2e8227SGreg Roach    }
918c2e8227SGreg Roach
923caaa4d2SGreg Roach    /**
933caaa4d2SGreg Roach     * Is this tab empty? If so, we don't always need to display it.
943caaa4d2SGreg Roach     *
953caaa4d2SGreg Roach     * @param Individual $individual
963caaa4d2SGreg Roach     *
973caaa4d2SGreg Roach     * @return bool
983caaa4d2SGreg Roach     */
999b34404bSGreg Roach    public function hasTabContent(Individual $individual): bool
100c1010edaSGreg Roach    {
10115d603e7SGreg Roach        return true;
1028c2e8227SGreg Roach    }
1038c2e8227SGreg Roach
1043caaa4d2SGreg Roach    /**
1053caaa4d2SGreg Roach     * A greyed out tab has no actual content, but may perhaps have
1063caaa4d2SGreg Roach     * options to create content.
1073caaa4d2SGreg Roach     *
1083caaa4d2SGreg Roach     * @param Individual $individual
1093caaa4d2SGreg Roach     *
1103caaa4d2SGreg Roach     * @return bool
1113caaa4d2SGreg Roach     */
1129b34404bSGreg Roach    public function isGrayedOut(Individual $individual): bool
113c1010edaSGreg Roach    {
1148c2e8227SGreg Roach        return false;
1158c2e8227SGreg Roach    }
1168c2e8227SGreg Roach
1173caaa4d2SGreg Roach    /**
1183caaa4d2SGreg Roach     * Can this tab load asynchronously?
1193caaa4d2SGreg Roach     *
1203caaa4d2SGreg Roach     * @return bool
1213caaa4d2SGreg Roach     */
1229b34404bSGreg Roach    public function canLoadAjax(): bool
123c1010edaSGreg Roach    {
1248c2e8227SGreg Roach        return true;
1258c2e8227SGreg Roach    }
1268c2e8227SGreg Roach
127168ff6f3Sric2016    /**
128377a2979SGreg Roach     * CSS class for the URL.
129377a2979SGreg Roach     *
130377a2979SGreg Roach     * @return string
131377a2979SGreg Roach     */
132377a2979SGreg Roach    public function chartMenuClass(): string
133377a2979SGreg Roach    {
134377a2979SGreg Roach        return 'menu-chart-tree';
135377a2979SGreg Roach    }
136377a2979SGreg Roach
137377a2979SGreg Roach    /**
1384eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1394eb71cfaSGreg Roach     *
14060bc3e3fSGreg Roach     * @param Individual $individual
14160bc3e3fSGreg Roach     *
1424eb71cfaSGreg Roach     * @return Menu|null
1434eb71cfaSGreg Roach     */
144377a2979SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
145c1010edaSGreg Roach    {
146e6562982SGreg Roach        return $this->chartMenu($individual);
147e6562982SGreg Roach    }
148e6562982SGreg Roach
149e6562982SGreg Roach    /**
150e6562982SGreg Roach     * The title for a specific instance of this chart.
151e6562982SGreg Roach     *
152e6562982SGreg Roach     * @param Individual $individual
153e6562982SGreg Roach     *
154e6562982SGreg Roach     * @return string
155e6562982SGreg Roach     */
156e6562982SGreg Roach    public function chartTitle(Individual $individual): string
157e6562982SGreg Roach    {
158e6562982SGreg Roach        /* I18N: %s is an individual’s name */
15939ca88baSGreg Roach        return I18N::translate('Interactive tree of %s', $individual->fullName());
160e6562982SGreg Roach    }
161e6562982SGreg Roach
162e6562982SGreg Roach    /**
163e6562982SGreg Roach     * The URL for this chart.
164e6562982SGreg Roach     *
165e6562982SGreg Roach     * @param Individual $individual
166e6562982SGreg Roach     * @param string[]   $parameters
167e6562982SGreg Roach     *
168e6562982SGreg Roach     * @return string
169e6562982SGreg Roach     */
170e6562982SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
171e6562982SGreg Roach    {
172e6562982SGreg Roach        return route('module', [
17326684e68SGreg Roach                'module' => $this->name(),
1745edf1a44SGreg Roach                'action' => 'Chart',
175e6562982SGreg Roach                'xref'   => $individual->xref(),
176e6562982SGreg Roach                'ged'    => $individual->tree()->name(),
177e6562982SGreg Roach            ] + $parameters);
178e6562982SGreg Roach    }
179e6562982SGreg Roach
180e6562982SGreg Roach    /**
1816ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
18276692c8bSGreg Roach     *
1836ccdf4f0SGreg Roach     * @return ResponseInterface
18476692c8bSGreg Roach     */
18557ab2231SGreg Roach    public function getChartAction(ServerRequestInterface $request): ResponseInterface
186c1010edaSGreg Roach    {
18757ab2231SGreg Roach        $tree = $request->getAttribute('tree');
18857ab2231SGreg Roach        $user = $request->getAttribute('user');
189e106ff43SGreg Roach        $xref = $request->getQueryParams()['xref'];
190e2ae4578SGreg Roach
191e2ae4578SGreg Roach        $individual = Individual::getInstance($xref, $tree);
192bfaf8159SGreg Roach
1935edf1a44SGreg Roach        Auth::checkIndividualAccess($individual);
1949867b2f0SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
195bfaf8159SGreg Roach
1968c2e8227SGreg Roach        $tv = new TreeView('tv');
1978c2e8227SGreg Roach
19865e02381SGreg Roach        [$html, $js] = $tv->drawViewport($individual, 4);
1998c2e8227SGreg Roach
200575707d1SGreg Roach        return $this->viewResponse('modules/interactive-tree/page', [
2015edf1a44SGreg Roach            'html'       => $html,
2028840c547SGreg Roach            'individual' => $individual,
203e2ae4578SGreg Roach            'js'         => $js,
204*71378461SGreg Roach            'module'     => $this->name(),
2055edf1a44SGreg Roach            'title'      => $this->chartTitle($individual),
206e2ae4578SGreg Roach            'tree'       => $tree,
207f8a18b14SGreg Roach        ]);
2088c2e8227SGreg Roach    }
2098c2e8227SGreg Roach
210e2ae4578SGreg Roach    /**
2116ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
212e2ae4578SGreg Roach     *
2136ccdf4f0SGreg Roach     * @return ResponseInterface
214e2ae4578SGreg Roach     */
21557ab2231SGreg Roach    public function getDetailsAction(ServerRequestInterface $request): ResponseInterface
216c1010edaSGreg Roach    {
21757ab2231SGreg Roach        $tree       = $request->getAttribute('tree');
218e106ff43SGreg Roach        $pid        = $request->getQueryParams()['pid'];
219e2ae4578SGreg Roach        $individual = Individual::getInstance($pid, $tree);
220e2ae4578SGreg Roach
2210bc54ba3SGreg Roach        if ($individual === null) {
22259f2f229SGreg Roach            throw new IndividualNotFoundException();
2230bc54ba3SGreg Roach        }
2240bc54ba3SGreg Roach
2250bc54ba3SGreg Roach        if (!$individual->canShow()) {
22659f2f229SGreg Roach            throw new IndividualAccessDeniedException();
2270bc54ba3SGreg Roach        }
2280bc54ba3SGreg Roach
229e106ff43SGreg Roach        $instance = $request->getQueryParams()['instance'];
230e2ae4578SGreg Roach        $treeview = new TreeView($instance);
231e2ae4578SGreg Roach
2326ccdf4f0SGreg Roach        return response($treeview->getDetails($individual));
2338c2e8227SGreg Roach    }
2348c2e8227SGreg Roach
2358c2e8227SGreg Roach    /**
2366ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
2373cf92ae2SGreg Roach     *
2386ccdf4f0SGreg Roach     * @return ResponseInterface
2398c2e8227SGreg Roach     */
24057ab2231SGreg Roach    public function getPersonsAction(ServerRequestInterface $request): ResponseInterface
241c1010edaSGreg Roach    {
24257ab2231SGreg Roach        $tree     = $request->getAttribute('tree');
243e106ff43SGreg Roach        $q        = $request->getQueryParams()['q'];
244e106ff43SGreg Roach        $instance = $request->getQueryParams()['instance'];
245e2ae4578SGreg Roach        $treeview = new TreeView($instance);
2468c2e8227SGreg Roach
2476ccdf4f0SGreg Roach        return response($treeview->getPersons($tree, $q));
2488c2e8227SGreg Roach    }
2498c2e8227SGreg Roach}
250