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