xref: /webtrees/app/Module/InteractiveTreeModule.php (revision 5f52b64188c4f443eebeb985bb53134753a7560f)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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\Validator;
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    public function description(): string
53    {
54        /* I18N: Description of the “Interactive tree” module */
55        return I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
56    }
57
58    /**
59     * The default position for this tab.  It can be changed in the control panel.
60     *
61     * @return int
62     */
63    public function defaultTabOrder(): int
64    {
65        return 7;
66    }
67
68    /**
69     * Generate the HTML content of this tab.
70     *
71     * @param Individual $individual
72     *
73     * @return string
74     */
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        ]);
85    }
86
87    /**
88     * Is this tab empty? If so, we don't always need to display it.
89     *
90     * @param Individual $individual
91     *
92     * @return bool
93     */
94    public function hasTabContent(Individual $individual): bool
95    {
96        return $individual->facts(['FAMC', 'FAMS'])->isNotEmpty();
97    }
98
99    /**
100     * A greyed out tab has no actual content, but may perhaps have
101     * options to create content.
102     *
103     * @param Individual $individual
104     *
105     * @return bool
106     */
107    public function isGrayedOut(Individual $individual): bool
108    {
109        return false;
110    }
111
112    /**
113     * Can this tab load asynchronously?
114     *
115     * @return bool
116     */
117    public function canLoadAjax(): bool
118    {
119        return true;
120    }
121
122    /**
123     * CSS class for the URL.
124     *
125     * @return string
126     */
127    public function chartMenuClass(): string
128    {
129        return 'menu-chart-tree';
130    }
131
132    /**
133     * Return a menu item for this chart - for use in individual boxes.
134     *
135     * @param Individual $individual
136     *
137     * @return Menu|null
138     */
139    public function chartBoxMenu(Individual $individual): Menu|null
140    {
141        return $this->chartMenu($individual);
142    }
143
144    /**
145     * The title for a specific instance of this chart.
146     *
147     * @param Individual $individual
148     *
149     * @return string
150     */
151    public function chartTitle(Individual $individual): string
152    {
153        /* I18N: %s is an individual’s name */
154        return I18N::translate('Interactive tree of %s', $individual->fullName());
155    }
156
157    /**
158     * The URL for this chart.
159     *
160     * @param Individual                                $individual
161     * @param array<bool|int|string|array<string>|null> $parameters
162     *
163     * @return string
164     */
165    public function chartUrl(Individual $individual, array $parameters = []): string
166    {
167        return route('module', [
168                'module' => $this->name(),
169                'action' => 'Chart',
170                'xref'   => $individual->xref(),
171                'tree'    => $individual->tree()->name(),
172            ] + $parameters);
173    }
174
175    /**
176     * @param ServerRequestInterface $request
177     *
178     * @return ResponseInterface
179     */
180    public function getChartAction(ServerRequestInterface $request): ResponseInterface
181    {
182        $tree = Validator::attributes($request)->tree();
183        $user = Validator::attributes($request)->user();
184        $xref = Validator::queryParams($request)->isXref()->string('xref');
185
186        Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
187
188        $individual = Registry::individualFactory()->make($xref, $tree);
189        $individual = Auth::checkIndividualAccess($individual, false, true);
190
191        $tv = new TreeView('tv');
192
193        [$html, $js] = $tv->drawViewport($individual, 4);
194
195        return $this->viewResponse('modules/interactive-tree/page', [
196            'html'       => $html,
197            'individual' => $individual,
198            'js'         => $js,
199            'module'     => $this->name(),
200            'title'      => $this->chartTitle($individual),
201            'tree'       => $tree,
202        ]);
203    }
204
205    /**
206     * @param ServerRequestInterface $request
207     *
208     * @return ResponseInterface
209     */
210    public function postChartAction(ServerRequestInterface $request): ResponseInterface
211    {
212        return redirect(route('module', [
213            'module' => $this->name(),
214            'action' => 'Chart',
215            'tree'   => Validator::attributes($request)->tree()->name(),
216            'xref'   => Validator::parsedBody($request)->isXref()->string('xref'),
217        ]));
218    }
219
220    /**
221     * @param ServerRequestInterface $request
222     *
223     * @return ResponseInterface
224     */
225    public function getDetailsAction(ServerRequestInterface $request): ResponseInterface
226    {
227        $tree       = Validator::attributes($request)->tree();
228        $pid        = Validator::queryParams($request)->string('pid');
229        $individual = Registry::individualFactory()->make($pid, $tree);
230        $individual = Auth::checkIndividualAccess($individual);
231        $instance   = Validator::queryParams($request)->string('instance');
232        $treeview   = new TreeView($instance);
233
234        return response($treeview->getDetails($individual));
235    }
236
237    /**
238     * @param ServerRequestInterface $request
239     *
240     * @return ResponseInterface
241     */
242    public function getIndividualsAction(ServerRequestInterface $request): ResponseInterface
243    {
244        $tree     = Validator::attributes($request)->tree();
245        $q        = Validator::queryParams($request)->string('q');
246        $instance = Validator::queryParams($request)->string('instance');
247        $treeview = new TreeView($instance);
248
249        return response($treeview->getIndividuals($tree, $q));
250    }
251}
252