xref: /webtrees/app/Module/InteractiveTreeModule.php (revision c1010eda29c0909ed4d5d463f32d32bfefdd4dfe)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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 */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualAccessDeniedException;
190bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
22168ff6f3Sric2016use Fisharebest\Webtrees\Menu;
23e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
24e2ae4578SGreg Roachuse Fisharebest\Webtrees\Tree;
25e2ae4578SGreg Roachuse Symfony\Component\HttpFoundation\Request;
26e2ae4578SGreg Roachuse Symfony\Component\HttpFoundation\Response;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class InteractiveTreeModule
308c2e8227SGreg 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
318c2e8227SGreg Roach */
32*c1010edaSGreg Roachclass InteractiveTreeModule extends AbstractModule implements ModuleTabInterface, ModuleChartInterface
33*c1010edaSGreg Roach{
348c2e8227SGreg Roach    /** {@inheritdoc} */
35*c1010edaSGreg Roach    public function getTitle()
36*c1010edaSGreg Roach    {
37e2ae4578SGreg Roach        return /* I18N: Name of a module */
38e2ae4578SGreg Roach            I18N::translate('Interactive tree');
398c2e8227SGreg Roach    }
408c2e8227SGreg Roach
418c2e8227SGreg Roach    /** {@inheritdoc} */
42*c1010edaSGreg Roach    public function getDescription()
43*c1010edaSGreg Roach    {
44e2ae4578SGreg Roach        return /* I18N: Description of the “Interactive tree” module */
45e2ae4578SGreg Roach            I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
468c2e8227SGreg Roach    }
478c2e8227SGreg Roach
488c2e8227SGreg Roach    /** {@inheritdoc} */
49*c1010edaSGreg Roach    public function defaultTabOrder()
50*c1010edaSGreg Roach    {
518c2e8227SGreg Roach        return 68;
528c2e8227SGreg Roach    }
538c2e8227SGreg Roach
548c2e8227SGreg Roach    /** {@inheritdoc} */
55*c1010edaSGreg Roach    public function getTabContent(Individual $individual)
56*c1010edaSGreg Roach    {
57225e381fSGreg Roach        $treeview = new TreeView('tvTab');
58225e381fSGreg Roach        list($html, $js) = $treeview->drawViewport($individual, 3);
598c2e8227SGreg Roach
60a8cd57e1SGreg Roach        return view('modules/tree/tab', [
61225e381fSGreg Roach            'html'         => $html,
62225e381fSGreg Roach            'js'           => $js,
63d5691647SGreg Roach            'treeview_css' => $this->css(),
64d5691647SGreg Roach            'treeview_js'  => $this->js(),
65225e381fSGreg Roach        ]);
668c2e8227SGreg Roach    }
678c2e8227SGreg Roach
68d5691647SGreg Roach    /**
69d5691647SGreg Roach     * @return string
70d5691647SGreg Roach     */
71*c1010edaSGreg Roach    public function css(): string
72*c1010edaSGreg Roach    {
73d5691647SGreg Roach        return WT_MODULES_DIR . $this->getName() . '/css/treeview.css';
74d5691647SGreg Roach    }
75d5691647SGreg Roach
76d5691647SGreg Roach    /**
77d5691647SGreg Roach     * @return string
78d5691647SGreg Roach     */
79*c1010edaSGreg Roach    public function js(): string
80*c1010edaSGreg Roach    {
81d5691647SGreg Roach        return WT_MODULES_DIR . $this->getName() . '/js/treeview.js';
82d5691647SGreg Roach    }
83d5691647SGreg Roach
848c2e8227SGreg Roach    /** {@inheritdoc} */
85*c1010edaSGreg Roach    public function hasTabContent(Individual $individual)
86*c1010edaSGreg Roach    {
8715d603e7SGreg Roach        return true;
888c2e8227SGreg Roach    }
898c2e8227SGreg Roach
908c2e8227SGreg Roach    /** {@inheritdoc} */
91*c1010edaSGreg Roach    public function isGrayedOut(Individual $individual)
92*c1010edaSGreg Roach    {
938c2e8227SGreg Roach        return false;
948c2e8227SGreg Roach    }
958c2e8227SGreg Roach
968c2e8227SGreg Roach    /** {@inheritdoc} */
97*c1010edaSGreg Roach    public function canLoadAjax()
98*c1010edaSGreg Roach    {
998c2e8227SGreg Roach        return true;
1008c2e8227SGreg Roach    }
1018c2e8227SGreg Roach
102168ff6f3Sric2016    /**
103168ff6f3Sric2016     * Return a menu item for this chart.
104168ff6f3Sric2016     *
10560bc3e3fSGreg Roach     * @param Individual $individual
10660bc3e3fSGreg Roach     *
1074eb71cfaSGreg Roach     * @return Menu|null
108168ff6f3Sric2016     */
109*c1010edaSGreg Roach    public function getChartMenu(Individual $individual)
110*c1010edaSGreg Roach    {
111168ff6f3Sric2016        return new Menu(
112168ff6f3Sric2016            $this->getTitle(),
113*c1010edaSGreg Roach            route('module', [
114*c1010edaSGreg Roach                'module' => $this->getName(),
115*c1010edaSGreg Roach                'action' => 'Treeview',
116*c1010edaSGreg Roach                'xref'   => $individual->getXref(),
117*c1010edaSGreg Roach                'ged'    => $individual->getTree()->getName(),
118*c1010edaSGreg Roach            ]),
119168ff6f3Sric2016            'menu-chart-tree',
12013abd6f3SGreg Roach            ['rel' => 'nofollow']
121168ff6f3Sric2016        );
122168ff6f3Sric2016    }
123168ff6f3Sric2016
1244eb71cfaSGreg Roach    /**
1254eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1264eb71cfaSGreg Roach     *
12760bc3e3fSGreg Roach     * @param Individual $individual
12860bc3e3fSGreg Roach     *
1294eb71cfaSGreg Roach     * @return Menu|null
1304eb71cfaSGreg Roach     */
131*c1010edaSGreg Roach    public function getBoxChartMenu(Individual $individual)
132*c1010edaSGreg Roach    {
133168ff6f3Sric2016        return $this->getChartMenu($individual);
134168ff6f3Sric2016    }
135168ff6f3Sric2016
13676692c8bSGreg Roach    /**
137e2ae4578SGreg Roach     * @param Request $request
13876692c8bSGreg Roach     *
139e2ae4578SGreg Roach     * @return Response
14076692c8bSGreg Roach     */
141*c1010edaSGreg Roach    public function getTreeviewAction(Request $request): Response
142*c1010edaSGreg Roach    {
143e2ae4578SGreg Roach        /** @var Tree $tree */
144e2ae4578SGreg Roach        $tree = $request->attributes->get('tree');
14524ec66ceSGreg Roach
146e2ae4578SGreg Roach        $xref = $request->get('xref');
147e2ae4578SGreg Roach
148e2ae4578SGreg Roach        $individual = Individual::getInstance($xref, $tree);
1498c2e8227SGreg Roach        $tv         = new TreeView('tv');
1508c2e8227SGreg Roach
151e2ae4578SGreg Roach        list($html, $js) = $tv->drawViewport($individual, 4);
1528c2e8227SGreg Roach
153e2ae4578SGreg Roach        $title = I18N::translate('Interactive tree of %s', $individual->getFullName());
1548c2e8227SGreg Roach
155e2ae4578SGreg Roach        return $this->viewResponse('interactive-tree-page', [
156e2ae4578SGreg Roach            'title'      => $title,
1578840c547SGreg Roach            'individual' => $individual,
158e2ae4578SGreg Roach            'js'         => $js,
159f8a18b14SGreg Roach            'html'       => $html,
160e2ae4578SGreg Roach            'tree'       => $tree,
161f8a18b14SGreg Roach        ]);
1628c2e8227SGreg Roach    }
1638c2e8227SGreg Roach
164e2ae4578SGreg Roach    /**
165e2ae4578SGreg Roach     * @param Request $request
166e2ae4578SGreg Roach     *
167e2ae4578SGreg Roach     * @return Response
168e2ae4578SGreg Roach     */
169*c1010edaSGreg Roach    public function getDetailsAction(Request $request): Response
170*c1010edaSGreg Roach    {
171e2ae4578SGreg Roach        /** @var Tree $tree */
172e2ae4578SGreg Roach        $tree = $request->attributes->get('tree');
1738c2e8227SGreg Roach
174e2ae4578SGreg Roach        $pid        = $request->get('pid', WT_REGEX_XREF);
175e2ae4578SGreg Roach        $individual = Individual::getInstance($pid, $tree);
176e2ae4578SGreg Roach
1770bc54ba3SGreg Roach        if ($individual === null) {
1780bc54ba3SGreg Roach            throw new IndividualNotFoundException;
1790bc54ba3SGreg Roach        }
1800bc54ba3SGreg Roach
1810bc54ba3SGreg Roach        if (!$individual->canShow()) {
1820bc54ba3SGreg Roach            throw new IndividualAccessDeniedException;
1830bc54ba3SGreg Roach        }
1840bc54ba3SGreg Roach
185e2ae4578SGreg Roach        $instance = $request->get('instance');
186e2ae4578SGreg Roach        $treeview = new TreeView($instance);
187e2ae4578SGreg Roach
188e2ae4578SGreg Roach        return new Response($treeview->getDetails($individual));
1898c2e8227SGreg Roach    }
1908c2e8227SGreg Roach
1918c2e8227SGreg Roach    /**
192e2ae4578SGreg Roach     * @param Request $request
1933cf92ae2SGreg Roach     *
194e2ae4578SGreg Roach     * @return Response
1958c2e8227SGreg Roach     */
196*c1010edaSGreg Roach    public function getPersonsAction(Request $request): Response
197*c1010edaSGreg Roach    {
198bc8b8f24SGreg Roach        /** @var Tree $tree */
199bc8b8f24SGreg Roach        $tree = $request->attributes->get('tree');
200bc8b8f24SGreg Roach
201e2ae4578SGreg Roach        $q        = $request->get('q');
202e2ae4578SGreg Roach        $instance = $request->get('instance');
203e2ae4578SGreg Roach        $treeview = new TreeView($instance);
2048c2e8227SGreg Roach
205bc8b8f24SGreg Roach        return new Response($treeview->getPersons($tree, $q));
2068c2e8227SGreg Roach    }
2078c2e8227SGreg Roach}
208