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