xref: /webtrees/app/Module/InteractiveTreeModule.php (revision add3fa4120ca696c713a0d0ac9b9c86f751fe49a)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Auth;
21use Fisharebest\Webtrees\Contracts\UserInterface;
22use Fisharebest\Webtrees\Exceptions\IndividualAccessDeniedException;
23use Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Individual;
26use Fisharebest\Webtrees\Menu;
27use Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
28use Fisharebest\Webtrees\Tree;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31
32/**
33 * Class InteractiveTreeModule
34 * 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
35 */
36class InteractiveTreeModule extends AbstractModule implements ModuleChartInterface, ModuleTabInterface
37{
38    use ModuleChartTrait;
39    use ModuleTabTrait;
40
41    /**
42     * How should this module be identified in the control panel, etc.?
43     *
44     * @return string
45     */
46    public function title(): string
47    {
48        /* I18N: Name of a module */
49        return I18N::translate('Interactive tree');
50    }
51
52    /**
53     * A sentence describing what this module does.
54     *
55     * @return string
56     */
57    public function description(): string
58    {
59        /* I18N: Description of the “Interactive tree” module */
60        return I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
61    }
62
63    /**
64     * The default position for this tab.  It can be changed in the control panel.
65     *
66     * @return int
67     */
68    public function defaultTabOrder(): int
69    {
70        return 7;
71    }
72
73    /** {@inheritdoc} */
74    public function getTabContent(Individual $individual): string
75    {
76        $treeview = new TreeView('tvTab');
77
78        [$html, $js] = $treeview->drawViewport($individual, 3);
79
80        return view('modules/interactive-tree/tab', [
81            'html' => $html,
82            'js'   => $js,
83        ]);
84    }
85
86    /** {@inheritdoc} */
87    public function hasTabContent(Individual $individual): bool
88    {
89        return true;
90    }
91
92    /** {@inheritdoc} */
93    public function isGrayedOut(Individual $individual): bool
94    {
95        return false;
96    }
97
98    /** {@inheritdoc} */
99    public function canLoadAjax(): bool
100    {
101        return true;
102    }
103
104    /**
105     * CSS class for the URL.
106     *
107     * @return string
108     */
109    public function chartMenuClass(): string
110    {
111        return 'menu-chart-tree';
112    }
113
114    /**
115     * Return a menu item for this chart - for use in individual boxes.
116     *
117     * @param Individual $individual
118     *
119     * @return Menu|null
120     */
121    public function chartBoxMenu(Individual $individual): ?Menu
122    {
123        return $this->chartMenu($individual);
124    }
125
126    /**
127     * The title for a specific instance of this chart.
128     *
129     * @param Individual $individual
130     *
131     * @return string
132     */
133    public function chartTitle(Individual $individual): string
134    {
135        /* I18N: %s is an individual’s name */
136        return I18N::translate('Interactive tree of %s', $individual->fullName());
137    }
138
139    /**
140     * The URL for this chart.
141     *
142     * @param Individual $individual
143     * @param string[]   $parameters
144     *
145     * @return string
146     */
147    public function chartUrl(Individual $individual, array $parameters = []): string
148    {
149        return route('module', [
150                'module' => $this->name(),
151                'action' => 'Chart',
152                'xref'   => $individual->xref(),
153                'ged'    => $individual->tree()->name(),
154            ] + $parameters);
155    }
156
157    /**
158     * @param ServerRequestInterface $request
159     * @param Tree                   $tree
160     * @param UserInterface          $user
161     *
162     * @return ResponseInterface
163     */
164    public function getChartAction(ServerRequestInterface $request, Tree $tree, UserInterface $user): ResponseInterface
165    {
166        $xref = $request->getQueryParams()['xref'];
167
168        $individual = Individual::getInstance($xref, $tree);
169
170        Auth::checkIndividualAccess($individual);
171        Auth::checkComponentAccess($this, 'chart', $tree, $user);
172
173        $tv = new TreeView('tv');
174
175        [$html, $js] = $tv->drawViewport($individual, 4);
176
177        return $this->viewResponse('modules/interactive-tree/page', [
178            'html'       => $html,
179            'individual' => $individual,
180            'js'         => $js,
181            'title'      => $this->chartTitle($individual),
182            'tree'       => $tree,
183        ]);
184    }
185
186    /**
187     * @param ServerRequestInterface $request
188     * @param Tree                   $tree
189     *
190     * @return ResponseInterface
191     */
192    public function getDetailsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
193    {
194        $pid        = $request->getQueryParams()['pid'];
195        $individual = Individual::getInstance($pid, $tree);
196
197        if ($individual === null) {
198            throw new IndividualNotFoundException();
199        }
200
201        if (!$individual->canShow()) {
202            throw new IndividualAccessDeniedException();
203        }
204
205        $instance = $request->getQueryParams()['instance'];
206        $treeview = new TreeView($instance);
207
208        return response($treeview->getDetails($individual));
209    }
210
211    /**
212     * @param ServerRequestInterface $request
213     * @param Tree                   $tree
214     *
215     * @return ResponseInterface
216     */
217    public function getPersonsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
218    {
219        $q        = $request->getQueryParams()['q'];
220        $instance = $request->getQueryParams()['instance'];
221        $treeview = new TreeView($instance);
222
223        return response($treeview->getPersons($tree, $q));
224    }
225}
226