xref: /webtrees/app/Module/InteractiveTreeModule.php (revision e60d40369c3d7739ac89b1d48a13da904510d04e)
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 Fisharebest\Webtrees\Webtrees;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32
33/**
34 * Class InteractiveTreeModule
35 * 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
36 */
37class InteractiveTreeModule extends AbstractModule implements ModuleChartInterface, ModuleTabInterface
38{
39    use ModuleChartTrait;
40    use ModuleTabTrait;
41
42    /**
43     * How should this module be identified in the control panel, etc.?
44     *
45     * @return string
46     */
47    public function title(): string
48    {
49        /* I18N: Name of a module */
50        return I18N::translate('Interactive tree');
51    }
52
53    /**
54     * A sentence describing what this module does.
55     *
56     * @return string
57     */
58    public function description(): string
59    {
60        /* I18N: Description of the “Interactive tree” module */
61        return I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
62    }
63
64    /**
65     * The default position for this tab.  It can be changed in the control panel.
66     *
67     * @return int
68     */
69    public function defaultTabOrder(): int
70    {
71        return 7;
72    }
73
74    /** {@inheritdoc} */
75    public function getTabContent(Individual $individual): string
76    {
77        $treeview = new TreeView('tvTab');
78
79        [$html, $js] = $treeview->drawViewport($individual, 3);
80
81        return view('modules/interactive-tree/tab', [
82            'html'         => $html,
83            'js'           => $js,
84            'treeview_css' => $this->css(),
85            'treeview_js'  => $this->js(),
86        ]);
87    }
88
89    /**
90     * @return string
91     */
92    public function css(): string
93    {
94        return Webtrees::MODULES_PATH . $this->name() . '/css/treeview.css';
95    }
96
97    /**
98     * @return string
99     */
100    public function js(): string
101    {
102        return Webtrees::MODULES_PATH . $this->name() . '/js/treeview.js';
103    }
104
105    /** {@inheritdoc} */
106    public function hasTabContent(Individual $individual): bool
107    {
108        return true;
109    }
110
111    /** {@inheritdoc} */
112    public function isGrayedOut(Individual $individual): bool
113    {
114        return false;
115    }
116
117    /** {@inheritdoc} */
118    public function canLoadAjax(): bool
119    {
120        return true;
121    }
122
123    /**
124     * CSS class for the URL.
125     *
126     * @return string
127     */
128    public function chartMenuClass(): string
129    {
130        return 'menu-chart-tree';
131    }
132
133    /**
134     * Return a menu item for this chart - for use in individual boxes.
135     *
136     * @param Individual $individual
137     *
138     * @return Menu|null
139     */
140    public function chartBoxMenu(Individual $individual): ?Menu
141    {
142        return $this->chartMenu($individual);
143    }
144
145    /**
146     * The title for a specific instance of this chart.
147     *
148     * @param Individual $individual
149     *
150     * @return string
151     */
152    public function chartTitle(Individual $individual): string
153    {
154        /* I18N: %s is an individual’s name */
155        return I18N::translate('Interactive tree of %s', $individual->fullName());
156    }
157
158    /**
159     * The URL for this chart.
160     *
161     * @param Individual $individual
162     * @param string[]   $parameters
163     *
164     * @return string
165     */
166    public function chartUrl(Individual $individual, array $parameters = []): string
167    {
168        return route('module', [
169                'module' => $this->name(),
170                'action' => 'Chart',
171                'xref'   => $individual->xref(),
172                'ged'    => $individual->tree()->name(),
173            ] + $parameters);
174    }
175
176    /**
177     * @param ServerRequestInterface $request
178     * @param Tree                   $tree
179     * @param UserInterface          $user
180     *
181     * @return ResponseInterface
182     */
183    public function getChartAction(ServerRequestInterface $request, Tree $tree, UserInterface $user): ResponseInterface
184    {
185        $xref = $request->getQueryParams()['xref'];
186
187        $individual = Individual::getInstance($xref, $tree);
188
189        Auth::checkIndividualAccess($individual);
190        Auth::checkComponentAccess($this, 'chart', $tree, $user);
191
192        $tv = new TreeView('tv');
193
194        [$html, $js] = $tv->drawViewport($individual, 4);
195
196        return $this->viewResponse('interactive-tree-page', [
197            'html'       => $html,
198            'individual' => $individual,
199            'js'         => $js,
200            'title'      => $this->chartTitle($individual),
201            'tree'       => $tree,
202        ]);
203    }
204
205    /**
206     * @param ServerRequestInterface $request
207     * @param Tree                   $tree
208     *
209     * @return ResponseInterface
210     */
211    public function getDetailsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
212    {
213        $pid        = $request->getQueryParams()['pid'];
214        $individual = Individual::getInstance($pid, $tree);
215
216        if ($individual === null) {
217            throw new IndividualNotFoundException();
218        }
219
220        if (!$individual->canShow()) {
221            throw new IndividualAccessDeniedException();
222        }
223
224        $instance = $request->getQueryParams()['instance'];
225        $treeview = new TreeView($instance);
226
227        return response($treeview->getDetails($individual));
228    }
229
230    /**
231     * @param ServerRequestInterface $request
232     * @param Tree                   $tree
233     *
234     * @return ResponseInterface
235     */
236    public function getPersonsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
237    {
238        $q        = $request->getQueryParams()['q'];
239        $instance = $request->getQueryParams()['instance'];
240        $treeview = new TreeView($instance);
241
242        return response($treeview->getPersons($tree, $q));
243    }
244}
245