xref: /webtrees/app/Module/InteractiveTreeModule.php (revision 8f53f488f13e53e44dc48778e8f51ec9f91352dd)
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;
27bfaf8159SGreg 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} */
36*8f53f488SRico Sonntag    public function getTitle(): string
37c1010edaSGreg Roach    {
38bbb76c12SGreg Roach        /* I18N: Name of a module */
39bbb76c12SGreg Roach        return I18N::translate('Interactive tree');
408c2e8227SGreg Roach    }
418c2e8227SGreg Roach
428c2e8227SGreg Roach    /** {@inheritdoc} */
43*8f53f488SRico Sonntag    public function getDescription(): string
44c1010edaSGreg Roach    {
45bbb76c12SGreg Roach        /* I18N: Description of the “Interactive tree” module */
46bbb76c12SGreg Roach        return 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
139b6db7c1fSGreg Roach     * @param Tree    $tree
14076692c8bSGreg Roach     *
141e2ae4578SGreg Roach     * @return Response
14276692c8bSGreg Roach     */
143b6db7c1fSGreg Roach    public function getTreeviewAction(Request $request, Tree $tree): Response
144c1010edaSGreg Roach    {
145e2ae4578SGreg Roach        $xref = $request->get('xref');
146e2ae4578SGreg Roach
147e2ae4578SGreg Roach        $individual = Individual::getInstance($xref, $tree);
148bfaf8159SGreg Roach
149bfaf8159SGreg Roach        if ($individual === null) {
15059f2f229SGreg Roach            throw new IndividualNotFoundException();
151bfaf8159SGreg Roach        }
152bfaf8159SGreg Roach
153bfaf8159SGreg Roach        if (!$individual->canShow()) {
15459f2f229SGreg Roach            throw new IndividualAccessDeniedException();
155bfaf8159SGreg Roach        }
156bfaf8159SGreg Roach
1578c2e8227SGreg Roach        $tv = new TreeView('tv');
1588c2e8227SGreg Roach
159e2ae4578SGreg Roach        list($html, $js) = $tv->drawViewport($individual, 4);
1608c2e8227SGreg Roach
161e2ae4578SGreg Roach        $title = I18N::translate('Interactive tree of %s', $individual->getFullName());
1628c2e8227SGreg Roach
163e2ae4578SGreg Roach        return $this->viewResponse('interactive-tree-page', [
164e2ae4578SGreg Roach            'title'      => $title,
1658840c547SGreg Roach            'individual' => $individual,
166e2ae4578SGreg Roach            'js'         => $js,
167f8a18b14SGreg Roach            'html'       => $html,
168e2ae4578SGreg Roach            'tree'       => $tree,
169f8a18b14SGreg Roach        ]);
1708c2e8227SGreg Roach    }
1718c2e8227SGreg Roach
172e2ae4578SGreg Roach    /**
173e2ae4578SGreg Roach     * @param Request $request
174b6db7c1fSGreg Roach     * @param Tree    $tree
175e2ae4578SGreg Roach     *
176e2ae4578SGreg Roach     * @return Response
177e2ae4578SGreg Roach     */
178b6db7c1fSGreg Roach    public function getDetailsAction(Request $request, Tree $tree): Response
179c1010edaSGreg Roach    {
180e2ae4578SGreg Roach        $pid        = $request->get('pid', WT_REGEX_XREF);
181e2ae4578SGreg Roach        $individual = Individual::getInstance($pid, $tree);
182e2ae4578SGreg Roach
1830bc54ba3SGreg Roach        if ($individual === null) {
18459f2f229SGreg Roach            throw new IndividualNotFoundException();
1850bc54ba3SGreg Roach        }
1860bc54ba3SGreg Roach
1870bc54ba3SGreg Roach        if (!$individual->canShow()) {
18859f2f229SGreg Roach            throw new IndividualAccessDeniedException();
1890bc54ba3SGreg Roach        }
1900bc54ba3SGreg Roach
191e2ae4578SGreg Roach        $instance = $request->get('instance');
192e2ae4578SGreg Roach        $treeview = new TreeView($instance);
193e2ae4578SGreg Roach
194e2ae4578SGreg Roach        return new Response($treeview->getDetails($individual));
1958c2e8227SGreg Roach    }
1968c2e8227SGreg Roach
1978c2e8227SGreg Roach    /**
198e2ae4578SGreg Roach     * @param Request $request
199b6db7c1fSGreg Roach     * @param Tree    $tree
2003cf92ae2SGreg Roach     *
201e2ae4578SGreg Roach     * @return Response
2028c2e8227SGreg Roach     */
203b6db7c1fSGreg Roach    public function getPersonsAction(Request $request, Tree $tree): Response
204c1010edaSGreg Roach    {
205e2ae4578SGreg Roach        $q        = $request->get('q');
206e2ae4578SGreg Roach        $instance = $request->get('instance');
207e2ae4578SGreg Roach        $treeview = new TreeView($instance);
2088c2e8227SGreg Roach
209bc8b8f24SGreg Roach        return new Response($treeview->getPersons($tree, $q));
2108c2e8227SGreg Roach    }
2118c2e8227SGreg Roach}
212