xref: /webtrees/app/Module/InteractiveTreeModule.php (revision b3679361640bf9d3bb5424fb15815c51cf0c1513)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Individual;
25use Fisharebest\Webtrees\Menu;
26use Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
27use Fisharebest\Webtrees\Registry;
28use Fisharebest\Webtrees\Tree;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31
32use function assert;
33
34/**
35 * Class InteractiveTreeModule
36 * 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
37 */
38class InteractiveTreeModule extends AbstractModule implements ModuleChartInterface, ModuleTabInterface
39{
40    use ModuleChartTrait;
41    use ModuleTabTrait;
42
43    /**
44     * How should this module be identified in the control panel, etc.?
45     *
46     * @return string
47     */
48    public function title(): string
49    {
50        /* I18N: Name of a module */
51        return I18N::translate('Interactive tree');
52    }
53
54    /**
55     * A sentence describing what this module does.
56     *
57     * @return string
58     */
59    public function description(): string
60    {
61        /* I18N: Description of the “Interactive tree” module */
62        return I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
63    }
64
65    /**
66     * The default position for this tab.  It can be changed in the control panel.
67     *
68     * @return int
69     */
70    public function defaultTabOrder(): int
71    {
72        return 7;
73    }
74
75    /**
76     * Generate the HTML content of this tab.
77     *
78     * @param Individual $individual
79     *
80     * @return string
81     */
82    public function getTabContent(Individual $individual): string
83    {
84        $treeview = new TreeView('tvTab');
85
86        [$html, $js] = $treeview->drawViewport($individual, 3);
87
88        return view('modules/interactive-tree/tab', [
89            'html' => $html,
90            'js'   => $js,
91        ]);
92    }
93
94    /**
95     * Is this tab empty? If so, we don't always need to display it.
96     *
97     * @param Individual $individual
98     *
99     * @return bool
100     */
101    public function hasTabContent(Individual $individual): bool
102    {
103        return $individual->facts(['FAMC', 'FAMS'])->isNotEmpty();
104    }
105
106    /**
107     * A greyed out tab has no actual content, but may perhaps have
108     * options to create content.
109     *
110     * @param Individual $individual
111     *
112     * @return bool
113     */
114    public function isGrayedOut(Individual $individual): bool
115    {
116        return false;
117    }
118
119    /**
120     * Can this tab load asynchronously?
121     *
122     * @return bool
123     */
124    public function canLoadAjax(): bool
125    {
126        return true;
127    }
128
129    /**
130     * CSS class for the URL.
131     *
132     * @return string
133     */
134    public function chartMenuClass(): string
135    {
136        return 'menu-chart-tree';
137    }
138
139    /**
140     * Return a menu item for this chart - for use in individual boxes.
141     *
142     * @param Individual $individual
143     *
144     * @return Menu|null
145     */
146    public function chartBoxMenu(Individual $individual): ?Menu
147    {
148        return $this->chartMenu($individual);
149    }
150
151    /**
152     * The title for a specific instance of this chart.
153     *
154     * @param Individual $individual
155     *
156     * @return string
157     */
158    public function chartTitle(Individual $individual): string
159    {
160        /* I18N: %s is an individual’s name */
161        return I18N::translate('Interactive tree of %s', $individual->fullName());
162    }
163
164    /**
165     * The URL for this chart.
166     *
167     * @param Individual                        $individual
168     * @param array<bool|int|string|array|null> $parameters
169     *
170     * @return string
171     */
172    public function chartUrl(Individual $individual, array $parameters = []): string
173    {
174        return route('module', [
175                'module' => $this->name(),
176                'action' => 'Chart',
177                'xref'   => $individual->xref(),
178                'tree'    => $individual->tree()->name(),
179            ] + $parameters);
180    }
181
182    /**
183     * @param ServerRequestInterface $request
184     *
185     * @return ResponseInterface
186     */
187    public function getChartAction(ServerRequestInterface $request): ResponseInterface
188    {
189        $tree = $request->getAttribute('tree');
190        assert($tree instanceof Tree);
191
192        $xref = $request->getQueryParams()['xref'];
193
194        $individual = Registry::individualFactory()->make($xref, $tree);
195        $individual = Auth::checkIndividualAccess($individual, false, true);
196
197        $user = $request->getAttribute('user');
198
199        Auth::checkComponentAccess($this, ModuleChartInterface::class, $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        $params = (array) $request->getParsedBody();
227
228        return redirect(route('module', [
229            'module' => $this->name(),
230            'action' => 'Chart',
231            'tree'   => $tree->name(),
232            'xref'   => $params['xref'] ?? '',
233        ]));
234    }
235
236    /**
237     * @param ServerRequestInterface $request
238     *
239     * @return ResponseInterface
240     */
241    public function getDetailsAction(ServerRequestInterface $request): ResponseInterface
242    {
243        $tree = $request->getAttribute('tree');
244        assert($tree instanceof Tree);
245
246        $pid        = $request->getQueryParams()['pid'];
247        $individual = Registry::individualFactory()->make($pid, $tree);
248
249        $individual = Auth::checkIndividualAccess($individual);
250
251        $instance = $request->getQueryParams()['instance'];
252        $treeview = new TreeView($instance);
253
254        return response($treeview->getDetails($individual));
255    }
256
257    /**
258     * @param ServerRequestInterface $request
259     *
260     * @return ResponseInterface
261     */
262    public function getIndividualsAction(ServerRequestInterface $request): ResponseInterface
263    {
264        $tree = $request->getAttribute('tree');
265        assert($tree instanceof Tree);
266
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