xref: /webtrees/app/Module/InteractiveTreeModule.php (revision 49a243cb5fe7c21b24e262552d556b018bfe3f41)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualAccessDeniedException;
210bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
228d0ebef0SGreg Roachuse Fisharebest\Webtrees\Gedcom;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
25168ff6f3Sric2016use Fisharebest\Webtrees\Menu;
26e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
27e2ae4578SGreg Roachuse Fisharebest\Webtrees\Tree;
288d0ebef0SGreg Roachuse Fisharebest\Webtrees\Webtrees;
29e2ae4578SGreg Roachuse Symfony\Component\HttpFoundation\Request;
30e2ae4578SGreg Roachuse Symfony\Component\HttpFoundation\Response;
318c2e8227SGreg Roach
328c2e8227SGreg Roach/**
338c2e8227SGreg Roach * Class InteractiveTreeModule
348c2e8227SGreg Roach * 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
358c2e8227SGreg Roach */
36*49a243cbSGreg Roachclass InteractiveTreeModule extends AbstractModule implements ModuleInterface, ModuleChartInterface, ModuleTabInterface
37c1010edaSGreg Roach{
38*49a243cbSGreg Roach    use ModuleChartTrait;
39*49a243cbSGreg Roach    use ModuleTabTrait;
40*49a243cbSGreg Roach
418c2e8227SGreg Roach    /** {@inheritdoc} */
42*49a243cbSGreg Roach    public function title(): string
43c1010edaSGreg Roach    {
44bbb76c12SGreg Roach        /* I18N: Name of a module */
45bbb76c12SGreg Roach        return I18N::translate('Interactive tree');
468c2e8227SGreg Roach    }
478c2e8227SGreg Roach
488c2e8227SGreg Roach    /** {@inheritdoc} */
49*49a243cbSGreg Roach    public function description(): string
50c1010edaSGreg Roach    {
51bbb76c12SGreg Roach        /* I18N: Description of the “Interactive tree” module */
52bbb76c12SGreg Roach        return I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
538c2e8227SGreg Roach    }
548c2e8227SGreg Roach
55*49a243cbSGreg Roach    /**
56*49a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
57*49a243cbSGreg Roach     *
58*49a243cbSGreg Roach     * @return int
59*49a243cbSGreg Roach     */
60*49a243cbSGreg Roach    function defaultTabOrder(): int {
61*49a243cbSGreg Roach        return 90;
628c2e8227SGreg Roach    }
638c2e8227SGreg Roach
648c2e8227SGreg Roach    /** {@inheritdoc} */
659b34404bSGreg Roach    public function getTabContent(Individual $individual): string
66c1010edaSGreg Roach    {
67225e381fSGreg Roach        $treeview = new TreeView('tvTab');
6865e02381SGreg Roach        [$html, $js] = $treeview->drawViewport($individual, 3);
698c2e8227SGreg Roach
70a8cd57e1SGreg Roach        return view('modules/tree/tab', [
71225e381fSGreg Roach            'html'         => $html,
72225e381fSGreg Roach            'js'           => $js,
73d5691647SGreg Roach            'treeview_css' => $this->css(),
74d5691647SGreg Roach            'treeview_js'  => $this->js(),
75225e381fSGreg Roach        ]);
768c2e8227SGreg Roach    }
778c2e8227SGreg Roach
78d5691647SGreg Roach    /**
79d5691647SGreg Roach     * @return string
80d5691647SGreg Roach     */
81c1010edaSGreg Roach    public function css(): string
82c1010edaSGreg Roach    {
838d0ebef0SGreg Roach        return Webtrees::MODULES_PATH . $this->getName() . '/css/treeview.css';
84d5691647SGreg Roach    }
85d5691647SGreg Roach
86d5691647SGreg Roach    /**
87d5691647SGreg Roach     * @return string
88d5691647SGreg Roach     */
89c1010edaSGreg Roach    public function js(): string
90c1010edaSGreg Roach    {
918d0ebef0SGreg Roach        return Webtrees::MODULES_PATH . $this->getName() . '/js/treeview.js';
92d5691647SGreg Roach    }
93d5691647SGreg Roach
948c2e8227SGreg Roach    /** {@inheritdoc} */
959b34404bSGreg Roach    public function hasTabContent(Individual $individual): bool
96c1010edaSGreg Roach    {
9715d603e7SGreg Roach        return true;
988c2e8227SGreg Roach    }
998c2e8227SGreg Roach
1008c2e8227SGreg Roach    /** {@inheritdoc} */
1019b34404bSGreg Roach    public function isGrayedOut(Individual $individual): bool
102c1010edaSGreg Roach    {
1038c2e8227SGreg Roach        return false;
1048c2e8227SGreg Roach    }
1058c2e8227SGreg Roach
1068c2e8227SGreg Roach    /** {@inheritdoc} */
1079b34404bSGreg Roach    public function canLoadAjax():  bool
108c1010edaSGreg Roach    {
1098c2e8227SGreg Roach        return true;
1108c2e8227SGreg Roach    }
1118c2e8227SGreg Roach
112168ff6f3Sric2016    /**
113168ff6f3Sric2016     * Return a menu item for this chart.
114168ff6f3Sric2016     *
11560bc3e3fSGreg Roach     * @param Individual $individual
11660bc3e3fSGreg Roach     *
1174eb71cfaSGreg Roach     * @return Menu|null
118168ff6f3Sric2016     */
119*49a243cbSGreg Roach    public function getChartMenu(Individual $individual): ?Menu
120c1010edaSGreg Roach    {
121168ff6f3Sric2016        return new Menu(
122*49a243cbSGreg Roach            $this->title(),
123c1010edaSGreg Roach            route('module', [
124c1010edaSGreg Roach                'module' => $this->getName(),
125c1010edaSGreg Roach                'action' => 'Treeview',
126c0935879SGreg Roach                'xref'   => $individual->xref(),
127f4afa648SGreg Roach                'ged'    => $individual->tree()->name(),
128c1010edaSGreg Roach            ]),
129168ff6f3Sric2016            'menu-chart-tree',
13013abd6f3SGreg Roach            ['rel' => 'nofollow']
131168ff6f3Sric2016        );
132168ff6f3Sric2016    }
133168ff6f3Sric2016
1344eb71cfaSGreg Roach    /**
1354eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1364eb71cfaSGreg Roach     *
13760bc3e3fSGreg Roach     * @param Individual $individual
13860bc3e3fSGreg Roach     *
1394eb71cfaSGreg Roach     * @return Menu|null
1404eb71cfaSGreg Roach     */
141*49a243cbSGreg Roach    public function getBoxChartMenu(Individual $individual): ?Menu
142c1010edaSGreg Roach    {
143168ff6f3Sric2016        return $this->getChartMenu($individual);
144168ff6f3Sric2016    }
145168ff6f3Sric2016
14676692c8bSGreg Roach    /**
147e2ae4578SGreg Roach     * @param Request $request
148b6db7c1fSGreg Roach     * @param Tree    $tree
14976692c8bSGreg Roach     *
150e2ae4578SGreg Roach     * @return Response
15176692c8bSGreg Roach     */
152b6db7c1fSGreg Roach    public function getTreeviewAction(Request $request, Tree $tree): Response
153c1010edaSGreg Roach    {
1549e648e55SGreg Roach        $xref = $request->get('xref', '');
155e2ae4578SGreg Roach
156e2ae4578SGreg Roach        $individual = Individual::getInstance($xref, $tree);
157bfaf8159SGreg Roach
158bfaf8159SGreg Roach        if ($individual === null) {
15959f2f229SGreg Roach            throw new IndividualNotFoundException();
160bfaf8159SGreg Roach        }
161bfaf8159SGreg Roach
162bfaf8159SGreg Roach        if (!$individual->canShow()) {
16359f2f229SGreg Roach            throw new IndividualAccessDeniedException();
164bfaf8159SGreg Roach        }
165bfaf8159SGreg Roach
1668c2e8227SGreg Roach        $tv = new TreeView('tv');
1678c2e8227SGreg Roach
16865e02381SGreg Roach        [$html, $js] = $tv->drawViewport($individual, 4);
1698c2e8227SGreg Roach
170e2ae4578SGreg Roach        $title = I18N::translate('Interactive tree of %s', $individual->getFullName());
1718c2e8227SGreg Roach
172e2ae4578SGreg Roach        return $this->viewResponse('interactive-tree-page', [
173e2ae4578SGreg Roach            'title'      => $title,
1748840c547SGreg Roach            'individual' => $individual,
175e2ae4578SGreg Roach            'js'         => $js,
176f8a18b14SGreg Roach            'html'       => $html,
177e2ae4578SGreg Roach            'tree'       => $tree,
178f8a18b14SGreg Roach        ]);
1798c2e8227SGreg Roach    }
1808c2e8227SGreg Roach
181e2ae4578SGreg Roach    /**
182e2ae4578SGreg Roach     * @param Request $request
183b6db7c1fSGreg Roach     * @param Tree    $tree
184e2ae4578SGreg Roach     *
185e2ae4578SGreg Roach     * @return Response
186e2ae4578SGreg Roach     */
187b6db7c1fSGreg Roach    public function getDetailsAction(Request $request, Tree $tree): Response
188c1010edaSGreg Roach    {
1898d0ebef0SGreg Roach        $pid        = $request->get('pid', Gedcom::REGEX_XREF);
190e2ae4578SGreg Roach        $individual = Individual::getInstance($pid, $tree);
191e2ae4578SGreg Roach
1920bc54ba3SGreg Roach        if ($individual === null) {
19359f2f229SGreg Roach            throw new IndividualNotFoundException();
1940bc54ba3SGreg Roach        }
1950bc54ba3SGreg Roach
1960bc54ba3SGreg Roach        if (!$individual->canShow()) {
19759f2f229SGreg Roach            throw new IndividualAccessDeniedException();
1980bc54ba3SGreg Roach        }
1990bc54ba3SGreg Roach
2009e648e55SGreg Roach        $instance = $request->get('instance', '');
201e2ae4578SGreg Roach        $treeview = new TreeView($instance);
202e2ae4578SGreg Roach
203e2ae4578SGreg Roach        return new Response($treeview->getDetails($individual));
2048c2e8227SGreg Roach    }
2058c2e8227SGreg Roach
2068c2e8227SGreg Roach    /**
207e2ae4578SGreg Roach     * @param Request $request
208b6db7c1fSGreg Roach     * @param Tree    $tree
2093cf92ae2SGreg Roach     *
210e2ae4578SGreg Roach     * @return Response
2118c2e8227SGreg Roach     */
212b6db7c1fSGreg Roach    public function getPersonsAction(Request $request, Tree $tree): Response
213c1010edaSGreg Roach    {
2149e648e55SGreg Roach        $q        = $request->get('q', '');
2159e648e55SGreg Roach        $instance = $request->get('instance', '');
216e2ae4578SGreg Roach        $treeview = new TreeView($instance);
2178c2e8227SGreg Roach
218bc8b8f24SGreg Roach        return new Response($treeview->getPersons($tree, $q));
2198c2e8227SGreg Roach    }
2208c2e8227SGreg Roach}
221