xref: /webtrees/app/Module/InteractiveTreeModule.php (revision c1010eda29c0909ed4d5d463f32d32bfefdd4dfe)
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()
36    {
37        return /* I18N: Name of a module */
38            I18N::translate('Interactive tree');
39    }
40
41    /** {@inheritdoc} */
42    public function getDescription()
43    {
44        return /* I18N: Description of the “Interactive tree” module */
45            I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
46    }
47
48    /** {@inheritdoc} */
49    public function defaultTabOrder()
50    {
51        return 68;
52    }
53
54    /** {@inheritdoc} */
55    public function getTabContent(Individual $individual)
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)
86    {
87        return true;
88    }
89
90    /** {@inheritdoc} */
91    public function isGrayedOut(Individual $individual)
92    {
93        return false;
94    }
95
96    /** {@inheritdoc} */
97    public function canLoadAjax()
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     *
139     * @return Response
140     */
141    public function getTreeviewAction(Request $request): Response
142    {
143        /** @var Tree $tree */
144        $tree = $request->attributes->get('tree');
145
146        $xref = $request->get('xref');
147
148        $individual = Individual::getInstance($xref, $tree);
149        $tv         = new TreeView('tv');
150
151        list($html, $js) = $tv->drawViewport($individual, 4);
152
153        $title = I18N::translate('Interactive tree of %s', $individual->getFullName());
154
155        return $this->viewResponse('interactive-tree-page', [
156            'title'      => $title,
157            'individual' => $individual,
158            'js'         => $js,
159            'html'       => $html,
160            'tree'       => $tree,
161        ]);
162    }
163
164    /**
165     * @param Request $request
166     *
167     * @return Response
168     */
169    public function getDetailsAction(Request $request): Response
170    {
171        /** @var Tree $tree */
172        $tree = $request->attributes->get('tree');
173
174        $pid        = $request->get('pid', WT_REGEX_XREF);
175        $individual = Individual::getInstance($pid, $tree);
176
177        if ($individual === null) {
178            throw new IndividualNotFoundException;
179        }
180
181        if (!$individual->canShow()) {
182            throw new IndividualAccessDeniedException;
183        }
184
185        $instance = $request->get('instance');
186        $treeview = new TreeView($instance);
187
188        return new Response($treeview->getDetails($individual));
189    }
190
191    /**
192     * @param Request $request
193     *
194     * @return Response
195     */
196    public function getPersonsAction(Request $request): Response
197    {
198        /** @var Tree $tree */
199        $tree = $request->attributes->get('tree');
200
201        $q        = $request->get('q');
202        $instance = $request->get('instance');
203        $treeview = new TreeView($instance);
204
205        return new Response($treeview->getPersons($tree, $q));
206    }
207}
208