xref: /webtrees/app/Module/InteractiveTreeModule.php (revision bfaf8159bfb7825b9a3af52cb2a71378e3117bec)
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;
27*bfaf8159SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
288c2e8227SGreg Roach
298c2e8227SGreg Roach/**
308c2e8227SGreg Roach * Class InteractiveTreeModule
318c2e8227SGreg 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
328c2e8227SGreg Roach */
33c1010edaSGreg Roachclass InteractiveTreeModule extends AbstractModule implements ModuleTabInterface, ModuleChartInterface
34c1010edaSGreg Roach{
358c2e8227SGreg Roach    /** {@inheritdoc} */
36c1010edaSGreg Roach    public function getTitle()
37c1010edaSGreg Roach    {
38e2ae4578SGreg Roach        return /* I18N: Name of a module */
39e2ae4578SGreg Roach            I18N::translate('Interactive tree');
408c2e8227SGreg Roach    }
418c2e8227SGreg Roach
428c2e8227SGreg Roach    /** {@inheritdoc} */
43c1010edaSGreg Roach    public function getDescription()
44c1010edaSGreg Roach    {
45e2ae4578SGreg Roach        return /* I18N: Description of the “Interactive tree” module */
46e2ae4578SGreg Roach            I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
478c2e8227SGreg Roach    }
488c2e8227SGreg Roach
498c2e8227SGreg Roach    /** {@inheritdoc} */
50c1010edaSGreg Roach    public function defaultTabOrder()
51c1010edaSGreg Roach    {
528c2e8227SGreg Roach        return 68;
538c2e8227SGreg Roach    }
548c2e8227SGreg Roach
558c2e8227SGreg Roach    /** {@inheritdoc} */
56c1010edaSGreg Roach    public function getTabContent(Individual $individual)
57c1010edaSGreg Roach    {
58225e381fSGreg Roach        $treeview = new TreeView('tvTab');
59225e381fSGreg Roach        list($html, $js) = $treeview->drawViewport($individual, 3);
608c2e8227SGreg Roach
61a8cd57e1SGreg Roach        return view('modules/tree/tab', [
62225e381fSGreg Roach            'html'         => $html,
63225e381fSGreg Roach            'js'           => $js,
64d5691647SGreg Roach            'treeview_css' => $this->css(),
65d5691647SGreg Roach            'treeview_js'  => $this->js(),
66225e381fSGreg Roach        ]);
678c2e8227SGreg Roach    }
688c2e8227SGreg Roach
69d5691647SGreg Roach    /**
70d5691647SGreg Roach     * @return string
71d5691647SGreg Roach     */
72c1010edaSGreg Roach    public function css(): string
73c1010edaSGreg Roach    {
74d5691647SGreg Roach        return WT_MODULES_DIR . $this->getName() . '/css/treeview.css';
75d5691647SGreg Roach    }
76d5691647SGreg Roach
77d5691647SGreg Roach    /**
78d5691647SGreg Roach     * @return string
79d5691647SGreg Roach     */
80c1010edaSGreg Roach    public function js(): string
81c1010edaSGreg Roach    {
82d5691647SGreg Roach        return WT_MODULES_DIR . $this->getName() . '/js/treeview.js';
83d5691647SGreg Roach    }
84d5691647SGreg Roach
858c2e8227SGreg Roach    /** {@inheritdoc} */
86c1010edaSGreg Roach    public function hasTabContent(Individual $individual)
87c1010edaSGreg Roach    {
8815d603e7SGreg Roach        return true;
898c2e8227SGreg Roach    }
908c2e8227SGreg Roach
918c2e8227SGreg Roach    /** {@inheritdoc} */
92c1010edaSGreg Roach    public function isGrayedOut(Individual $individual)
93c1010edaSGreg Roach    {
948c2e8227SGreg Roach        return false;
958c2e8227SGreg Roach    }
968c2e8227SGreg Roach
978c2e8227SGreg Roach    /** {@inheritdoc} */
98c1010edaSGreg Roach    public function canLoadAjax()
99c1010edaSGreg Roach    {
1008c2e8227SGreg Roach        return true;
1018c2e8227SGreg Roach    }
1028c2e8227SGreg Roach
103168ff6f3Sric2016    /**
104168ff6f3Sric2016     * Return a menu item for this chart.
105168ff6f3Sric2016     *
10660bc3e3fSGreg Roach     * @param Individual $individual
10760bc3e3fSGreg Roach     *
1084eb71cfaSGreg Roach     * @return Menu|null
109168ff6f3Sric2016     */
110c1010edaSGreg Roach    public function getChartMenu(Individual $individual)
111c1010edaSGreg Roach    {
112168ff6f3Sric2016        return new Menu(
113168ff6f3Sric2016            $this->getTitle(),
114c1010edaSGreg Roach            route('module', [
115c1010edaSGreg Roach                'module' => $this->getName(),
116c1010edaSGreg Roach                'action' => 'Treeview',
117c1010edaSGreg Roach                'xref'   => $individual->getXref(),
118c1010edaSGreg Roach                'ged'    => $individual->getTree()->getName(),
119c1010edaSGreg Roach            ]),
120168ff6f3Sric2016            'menu-chart-tree',
12113abd6f3SGreg Roach            ['rel' => 'nofollow']
122168ff6f3Sric2016        );
123168ff6f3Sric2016    }
124168ff6f3Sric2016
1254eb71cfaSGreg Roach    /**
1264eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1274eb71cfaSGreg Roach     *
12860bc3e3fSGreg Roach     * @param Individual $individual
12960bc3e3fSGreg Roach     *
1304eb71cfaSGreg Roach     * @return Menu|null
1314eb71cfaSGreg Roach     */
132c1010edaSGreg Roach    public function getBoxChartMenu(Individual $individual)
133c1010edaSGreg Roach    {
134168ff6f3Sric2016        return $this->getChartMenu($individual);
135168ff6f3Sric2016    }
136168ff6f3Sric2016
13776692c8bSGreg Roach    /**
138e2ae4578SGreg Roach     * @param Request $request
13976692c8bSGreg Roach     *
140e2ae4578SGreg Roach     * @return Response
14176692c8bSGreg Roach     */
142c1010edaSGreg Roach    public function getTreeviewAction(Request $request): Response
143c1010edaSGreg Roach    {
144e2ae4578SGreg Roach        /** @var Tree $tree */
145e2ae4578SGreg Roach        $tree = $request->attributes->get('tree');
14624ec66ceSGreg Roach
147e2ae4578SGreg Roach        $xref = $request->get('xref');
148e2ae4578SGreg Roach
149e2ae4578SGreg Roach        $individual = Individual::getInstance($xref, $tree);
150*bfaf8159SGreg Roach
151*bfaf8159SGreg Roach        if ($individual === null) {
152*bfaf8159SGreg Roach            throw new IndividualNotFoundException;
153*bfaf8159SGreg Roach        }
154*bfaf8159SGreg Roach
155*bfaf8159SGreg Roach        if (!$individual->canShow()) {
156*bfaf8159SGreg Roach            throw new IndividualAccessDeniedException;
157*bfaf8159SGreg Roach        }
158*bfaf8159SGreg Roach
1598c2e8227SGreg Roach        $tv = new TreeView('tv');
1608c2e8227SGreg Roach
161e2ae4578SGreg Roach        list($html, $js) = $tv->drawViewport($individual, 4);
1628c2e8227SGreg Roach
163e2ae4578SGreg Roach        $title = I18N::translate('Interactive tree of %s', $individual->getFullName());
1648c2e8227SGreg Roach
165e2ae4578SGreg Roach        return $this->viewResponse('interactive-tree-page', [
166e2ae4578SGreg Roach            'title'      => $title,
1678840c547SGreg Roach            'individual' => $individual,
168e2ae4578SGreg Roach            'js'         => $js,
169f8a18b14SGreg Roach            'html'       => $html,
170e2ae4578SGreg Roach            'tree'       => $tree,
171f8a18b14SGreg Roach        ]);
1728c2e8227SGreg Roach    }
1738c2e8227SGreg Roach
174e2ae4578SGreg Roach    /**
175e2ae4578SGreg Roach     * @param Request $request
176e2ae4578SGreg Roach     *
177e2ae4578SGreg Roach     * @return Response
178e2ae4578SGreg Roach     */
179c1010edaSGreg Roach    public function getDetailsAction(Request $request): Response
180c1010edaSGreg Roach    {
181e2ae4578SGreg Roach        /** @var Tree $tree */
182e2ae4578SGreg Roach        $tree = $request->attributes->get('tree');
1838c2e8227SGreg Roach
184e2ae4578SGreg Roach        $pid        = $request->get('pid', WT_REGEX_XREF);
185e2ae4578SGreg Roach        $individual = Individual::getInstance($pid, $tree);
186e2ae4578SGreg Roach
1870bc54ba3SGreg Roach        if ($individual === null) {
1880bc54ba3SGreg Roach            throw new IndividualNotFoundException;
1890bc54ba3SGreg Roach        }
1900bc54ba3SGreg Roach
1910bc54ba3SGreg Roach        if (!$individual->canShow()) {
1920bc54ba3SGreg Roach            throw new IndividualAccessDeniedException;
1930bc54ba3SGreg Roach        }
1940bc54ba3SGreg Roach
195e2ae4578SGreg Roach        $instance = $request->get('instance');
196e2ae4578SGreg Roach        $treeview = new TreeView($instance);
197e2ae4578SGreg Roach
198e2ae4578SGreg Roach        return new Response($treeview->getDetails($individual));
1998c2e8227SGreg Roach    }
2008c2e8227SGreg Roach
2018c2e8227SGreg Roach    /**
202e2ae4578SGreg Roach     * @param Request $request
2033cf92ae2SGreg Roach     *
204e2ae4578SGreg Roach     * @return Response
2058c2e8227SGreg Roach     */
206c1010edaSGreg Roach    public function getPersonsAction(Request $request): Response
207c1010edaSGreg Roach    {
208bc8b8f24SGreg Roach        /** @var Tree $tree */
209bc8b8f24SGreg Roach        $tree = $request->attributes->get('tree');
210bc8b8f24SGreg Roach
211e2ae4578SGreg Roach        $q        = $request->get('q');
212e2ae4578SGreg Roach        $instance = $request->get('instance');
213e2ae4578SGreg Roach        $treeview = new TreeView($instance);
2148c2e8227SGreg Roach
215bc8b8f24SGreg Roach        return new Response($treeview->getPersons($tree, $q));
2168c2e8227SGreg Roach    }
2178c2e8227SGreg Roach}
218