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