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