xref: /webtrees/app/Module/InteractiveTreeModule.php (revision 71359d063ab70c24ff9a63d614303d749ea3653f)
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 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Exceptions\IndividualAccessDeniedException;
24use Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Individual;
27use Fisharebest\Webtrees\Menu;
28use Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
29use Fisharebest\Webtrees\Tree;
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 mixed[]    $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);
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    /**
217     * @param ServerRequestInterface $request
218     *
219     * @return ResponseInterface
220     */
221    public function postChartAction(ServerRequestInterface $request): ResponseInterface
222    {
223        $tree = $request->getAttribute('tree');
224        assert($tree instanceof Tree);
225
226        return redirect(route('module', [
227            'module' => $this->name(),
228            'action' => 'Chart',
229            'tree'   => $tree->name(),
230            'xref'   => $request->getParsedBody()['xref'] ?? '',
231        ]));
232    }
233
234    /**
235     * @param ServerRequestInterface $request
236     *
237     * @return ResponseInterface
238     */
239    public function getDetailsAction(ServerRequestInterface $request): ResponseInterface
240    {
241        $tree       = $request->getAttribute('tree');
242        $pid        = $request->getQueryParams()['pid'];
243        $individual = Individual::getInstance($pid, $tree);
244
245        if ($individual === null) {
246            throw new IndividualNotFoundException();
247        }
248
249        if (!$individual->canShow()) {
250            throw new IndividualAccessDeniedException();
251        }
252
253        $instance = $request->getQueryParams()['instance'];
254        $treeview = new TreeView($instance);
255
256        return response($treeview->getDetails($individual));
257    }
258
259    /**
260     * @param ServerRequestInterface $request
261     *
262     * @return ResponseInterface
263     */
264    public function getIndividualsAction(ServerRequestInterface $request): ResponseInterface
265    {
266        $tree     = $request->getAttribute('tree');
267        $q        = $request->getQueryParams()['q'];
268        $instance = $request->getQueryParams()['instance'];
269        $treeview = new TreeView($instance);
270
271        return response($treeview->getIndividuals($tree, $q));
272    }
273}
274