xref: /webtrees/app/Module/InteractiveTreeModule.php (revision 051c822d024c344f8050d1178cd077a864982c84)
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    /**
74     * Generate the HTML content of this tab.
75     *
76     * @param Individual $individual
77     *
78     * @return string
79     */
80    public function getTabContent(Individual $individual): string
81    {
82        $treeview = new TreeView('tvTab');
83
84        [$html, $js] = $treeview->drawViewport($individual, 3);
85
86        return view('modules/interactive-tree/tab', [
87            'html' => $html,
88            'js'   => $js,
89        ]);
90    }
91
92    /**
93     * Is this tab empty? If so, we don't always need to display it.
94     *
95     * @param Individual $individual
96     *
97     * @return bool
98     */
99    public function hasTabContent(Individual $individual): bool
100    {
101        return true;
102    }
103
104    /**
105     * A greyed out tab has no actual content, but may perhaps have
106     * options to create content.
107     *
108     * @param Individual $individual
109     *
110     * @return bool
111     */
112    public function isGrayedOut(Individual $individual): bool
113    {
114        return false;
115    }
116
117    /**
118     * Can this tab load asynchronously?
119     *
120     * @return bool
121     */
122    public function canLoadAjax(): bool
123    {
124        return true;
125    }
126
127    /**
128     * CSS class for the URL.
129     *
130     * @return string
131     */
132    public function chartMenuClass(): string
133    {
134        return 'menu-chart-tree';
135    }
136
137    /**
138     * Return a menu item for this chart - for use in individual boxes.
139     *
140     * @param Individual $individual
141     *
142     * @return Menu|null
143     */
144    public function chartBoxMenu(Individual $individual): ?Menu
145    {
146        return $this->chartMenu($individual);
147    }
148
149    /**
150     * The title for a specific instance of this chart.
151     *
152     * @param Individual $individual
153     *
154     * @return string
155     */
156    public function chartTitle(Individual $individual): string
157    {
158        /* I18N: %s is an individual’s name */
159        return I18N::translate('Interactive tree of %s', $individual->fullName());
160    }
161
162    /**
163     * The URL for this chart.
164     *
165     * @param Individual $individual
166     * @param string[]   $parameters
167     *
168     * @return string
169     */
170    public function chartUrl(Individual $individual, array $parameters = []): string
171    {
172        return route('module', [
173                'module' => $this->name(),
174                'action' => 'Chart',
175                'xref'   => $individual->xref(),
176                'ged'    => $individual->tree()->name(),
177            ] + $parameters);
178    }
179
180    /**
181     * @param ServerRequestInterface $request
182     * @param Tree                   $tree
183     * @param UserInterface          $user
184     *
185     * @return ResponseInterface
186     */
187    public function getChartAction(ServerRequestInterface $request, Tree $tree, UserInterface $user): ResponseInterface
188    {
189        $xref = $request->getQueryParams()['xref'];
190
191        $individual = Individual::getInstance($xref, $tree);
192
193        Auth::checkIndividualAccess($individual);
194        Auth::checkComponentAccess($this, 'chart', $tree, $user);
195
196        $tv = new TreeView('tv');
197
198        [$html, $js] = $tv->drawViewport($individual, 4);
199
200        return $this->viewResponse('modules/interactive-tree/page', [
201            'html'       => $html,
202            'individual' => $individual,
203            'js'         => $js,
204            'title'      => $this->chartTitle($individual),
205            'tree'       => $tree,
206        ]);
207    }
208
209    /**
210     * @param ServerRequestInterface $request
211     * @param Tree                   $tree
212     *
213     * @return ResponseInterface
214     */
215    public function getDetailsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
216    {
217        $pid        = $request->getQueryParams()['pid'];
218        $individual = Individual::getInstance($pid, $tree);
219
220        if ($individual === null) {
221            throw new IndividualNotFoundException();
222        }
223
224        if (!$individual->canShow()) {
225            throw new IndividualAccessDeniedException();
226        }
227
228        $instance = $request->getQueryParams()['instance'];
229        $treeview = new TreeView($instance);
230
231        return response($treeview->getDetails($individual));
232    }
233
234    /**
235     * @param ServerRequestInterface $request
236     * @param Tree                   $tree
237     *
238     * @return ResponseInterface
239     */
240    public function getPersonsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
241    {
242        $q        = $request->getQueryParams()['q'];
243        $instance = $request->getQueryParams()['instance'];
244        $treeview = new TreeView($instance);
245
246        return response($treeview->getPersons($tree, $q));
247    }
248}
249