xref: /webtrees/app/Module/InteractiveTreeModule.php (revision cd4fed472dcc87e9b04a20d3054a8ee970d2d149)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 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\Factory;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Individual;
28use Fisharebest\Webtrees\Menu;
29use Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
30use Fisharebest\Webtrees\Tree;
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 $individual->facts(['FAMC', 'FAMS'])->isNotEmpty();
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 mixed[]    $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);
193
194        $xref = $request->getQueryParams()['xref'];
195
196        $individual = Factory::individual()->make($xref, $tree);
197        $individual = Auth::checkIndividualAccess($individual, false, true);
198
199        $user = $request->getAttribute('user');
200
201        Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
202
203        $tv = new TreeView('tv');
204
205        [$html, $js] = $tv->drawViewport($individual, 4);
206
207        return $this->viewResponse('modules/interactive-tree/page', [
208            'html'       => $html,
209            'individual' => $individual,
210            'js'         => $js,
211            'module'     => $this->name(),
212            'title'      => $this->chartTitle($individual),
213            'tree'       => $tree,
214        ]);
215    }
216
217
218    /**
219     * @param ServerRequestInterface $request
220     *
221     * @return ResponseInterface
222     */
223    public function postChartAction(ServerRequestInterface $request): ResponseInterface
224    {
225        $tree = $request->getAttribute('tree');
226        assert($tree instanceof Tree);
227
228        $params = (array) $request->getParsedBody();
229
230        return redirect(route('module', [
231            'module' => $this->name(),
232            'action' => 'Chart',
233            'tree'   => $tree->name(),
234            'xref'   => $params['xref'] ?? '',
235        ]));
236    }
237
238    /**
239     * @param ServerRequestInterface $request
240     *
241     * @return ResponseInterface
242     */
243    public function getDetailsAction(ServerRequestInterface $request): ResponseInterface
244    {
245        $tree = $request->getAttribute('tree');
246        assert($tree instanceof Tree);
247
248        $pid        = $request->getQueryParams()['pid'];
249        $individual = Factory::individual()->make($pid, $tree);
250
251        if ($individual === null) {
252            throw new IndividualNotFoundException();
253        }
254
255        if (!$individual->canShow()) {
256            throw new IndividualAccessDeniedException();
257        }
258
259        $instance = $request->getQueryParams()['instance'];
260        $treeview = new TreeView($instance);
261
262        return response($treeview->getDetails($individual));
263    }
264
265    /**
266     * @param ServerRequestInterface $request
267     *
268     * @return ResponseInterface
269     */
270    public function getIndividualsAction(ServerRequestInterface $request): ResponseInterface
271    {
272        $tree = $request->getAttribute('tree');
273        assert($tree instanceof Tree);
274
275        $q        = $request->getQueryParams()['q'];
276        $instance = $request->getQueryParams()['instance'];
277        $treeview = new TreeView($instance);
278
279        return response($treeview->getIndividuals($tree, $q));
280    }
281}
282