xref: /webtrees/app/Module/InteractiveTreeModule.php (revision cd49476113f9d5da9160131d6707b5aebcaf2d20)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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\Exceptions\IndividualAccessDeniedException;
21use Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Individual;
24use Fisharebest\Webtrees\Menu;
25use Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
26use Fisharebest\Webtrees\Tree;
27use Symfony\Component\HttpFoundation\Request;
28use Symfony\Component\HttpFoundation\Response;
29
30/**
31 * Class InteractiveTreeModule
32 * 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
33 */
34class InteractiveTreeModule extends AbstractModule implements ModuleTabInterface, ModuleChartInterface
35{
36    /** {@inheritdoc} */
37    public function getTitle(): string
38    {
39        /* I18N: Name of a module */
40        return I18N::translate('Interactive tree');
41    }
42
43    /** {@inheritdoc} */
44    public function getDescription(): string
45    {
46        /* I18N: Description of the “Interactive tree” module */
47        return I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
48    }
49
50    /** {@inheritdoc} */
51    public function defaultTabOrder(): int
52    {
53        return 68;
54    }
55
56    /** {@inheritdoc} */
57    public function getTabContent(Individual $individual): string
58    {
59        $treeview = new TreeView('tvTab');
60        list($html, $js) = $treeview->drawViewport($individual, 3);
61
62        return view('modules/tree/tab', [
63            'html'         => $html,
64            'js'           => $js,
65            'treeview_css' => $this->css(),
66            'treeview_js'  => $this->js(),
67        ]);
68    }
69
70    /**
71     * @return string
72     */
73    public function css(): string
74    {
75        return WT_MODULES_DIR . $this->getName() . '/css/treeview.css';
76    }
77
78    /**
79     * @return string
80     */
81    public function js(): string
82    {
83        return WT_MODULES_DIR . $this->getName() . '/js/treeview.js';
84    }
85
86    /** {@inheritdoc} */
87    public function hasTabContent(Individual $individual): bool
88    {
89        return true;
90    }
91
92    /** {@inheritdoc} */
93    public function isGrayedOut(Individual $individual): bool
94    {
95        return false;
96    }
97
98    /** {@inheritdoc} */
99    public function canLoadAjax():  bool
100    {
101        return true;
102    }
103
104    /**
105     * Return a menu item for this chart.
106     *
107     * @param Individual $individual
108     *
109     * @return Menu|null
110     */
111    public function getChartMenu(Individual $individual)
112    {
113        return new Menu(
114            $this->getTitle(),
115            route('module', [
116                'module' => $this->getName(),
117                'action' => 'Treeview',
118                'xref'   => $individual->getXref(),
119                'ged'    => $individual->getTree()->getName(),
120            ]),
121            'menu-chart-tree',
122            ['rel' => 'nofollow']
123        );
124    }
125
126    /**
127     * Return a menu item for this chart - for use in individual boxes.
128     *
129     * @param Individual $individual
130     *
131     * @return Menu|null
132     */
133    public function getBoxChartMenu(Individual $individual)
134    {
135        return $this->getChartMenu($individual);
136    }
137
138    /**
139     * @param Request $request
140     * @param Tree    $tree
141     *
142     * @return Response
143     */
144    public function getTreeviewAction(Request $request, Tree $tree): Response
145    {
146        $xref = $request->get('xref', '');
147
148        $individual = Individual::getInstance($xref, $tree);
149
150        if ($individual === null) {
151            throw new IndividualNotFoundException();
152        }
153
154        if (!$individual->canShow()) {
155            throw new IndividualAccessDeniedException();
156        }
157
158        $tv = new TreeView('tv');
159
160        list($html, $js) = $tv->drawViewport($individual, 4);
161
162        $title = I18N::translate('Interactive tree of %s', $individual->getFullName());
163
164        return $this->viewResponse('interactive-tree-page', [
165            'title'      => $title,
166            'individual' => $individual,
167            'js'         => $js,
168            'html'       => $html,
169            'tree'       => $tree,
170        ]);
171    }
172
173    /**
174     * @param Request $request
175     * @param Tree    $tree
176     *
177     * @return Response
178     */
179    public function getDetailsAction(Request $request, Tree $tree): Response
180    {
181        $pid        = $request->get('pid', WT_REGEX_XREF);
182        $individual = Individual::getInstance($pid, $tree);
183
184        if ($individual === null) {
185            throw new IndividualNotFoundException();
186        }
187
188        if (!$individual->canShow()) {
189            throw new IndividualAccessDeniedException();
190        }
191
192        $instance = $request->get('instance', '');
193        $treeview = new TreeView($instance);
194
195        return new Response($treeview->getDetails($individual));
196    }
197
198    /**
199     * @param Request $request
200     * @param Tree    $tree
201     *
202     * @return Response
203     */
204    public function getPersonsAction(Request $request, Tree $tree): Response
205    {
206        $q        = $request->get('q', '');
207        $instance = $request->get('instance', '');
208        $treeview = new TreeView($instance);
209
210        return new Response($treeview->getPersons($tree, $q));
211    }
212}
213