xref: /webtrees/app/Module/InteractiveTreeModule.php (revision cbf4b7fa194b4e336add2747abfccb05fbb0f4da)
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 */
3649a243cbSGreg Roachclass InteractiveTreeModule extends AbstractModule implements ModuleInterface, ModuleChartInterface, ModuleTabInterface
37c1010edaSGreg Roach{
3849a243cbSGreg Roach    use ModuleChartTrait;
3949a243cbSGreg Roach    use ModuleTabTrait;
4049a243cbSGreg Roach
418c2e8227SGreg Roach    /** {@inheritdoc} */
4249a243cbSGreg 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} */
4949a243cbSGreg 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
5549a243cbSGreg Roach    /**
5649a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
5749a243cbSGreg Roach     *
5849a243cbSGreg Roach     * @return int
5949a243cbSGreg Roach     */
60*cbf4b7faSGreg Roach    public function defaultTabOrder(): int
61*cbf4b7faSGreg Roach    {
6249a243cbSGreg Roach        return 90;
638c2e8227SGreg Roach    }
648c2e8227SGreg Roach
658c2e8227SGreg Roach    /** {@inheritdoc} */
669b34404bSGreg Roach    public function getTabContent(Individual $individual): string
67c1010edaSGreg Roach    {
68225e381fSGreg Roach        $treeview = new TreeView('tvTab');
6965e02381SGreg Roach        [$html, $js] = $treeview->drawViewport($individual, 3);
708c2e8227SGreg Roach
71a8cd57e1SGreg Roach        return view('modules/tree/tab', [
72225e381fSGreg Roach            'html'         => $html,
73225e381fSGreg Roach            'js'           => $js,
74d5691647SGreg Roach            'treeview_css' => $this->css(),
75d5691647SGreg Roach            'treeview_js'  => $this->js(),
76225e381fSGreg Roach        ]);
778c2e8227SGreg Roach    }
788c2e8227SGreg Roach
79d5691647SGreg Roach    /**
80d5691647SGreg Roach     * @return string
81d5691647SGreg Roach     */
82c1010edaSGreg Roach    public function css(): string
83c1010edaSGreg Roach    {
848d0ebef0SGreg Roach        return Webtrees::MODULES_PATH . $this->getName() . '/css/treeview.css';
85d5691647SGreg Roach    }
86d5691647SGreg Roach
87d5691647SGreg Roach    /**
88d5691647SGreg Roach     * @return string
89d5691647SGreg Roach     */
90c1010edaSGreg Roach    public function js(): string
91c1010edaSGreg Roach    {
928d0ebef0SGreg Roach        return Webtrees::MODULES_PATH . $this->getName() . '/js/treeview.js';
93d5691647SGreg Roach    }
94d5691647SGreg Roach
958c2e8227SGreg Roach    /** {@inheritdoc} */
969b34404bSGreg Roach    public function hasTabContent(Individual $individual): bool
97c1010edaSGreg Roach    {
9815d603e7SGreg Roach        return true;
998c2e8227SGreg Roach    }
1008c2e8227SGreg Roach
1018c2e8227SGreg Roach    /** {@inheritdoc} */
1029b34404bSGreg Roach    public function isGrayedOut(Individual $individual): bool
103c1010edaSGreg Roach    {
1048c2e8227SGreg Roach        return false;
1058c2e8227SGreg Roach    }
1068c2e8227SGreg Roach
1078c2e8227SGreg Roach    /** {@inheritdoc} */
1089b34404bSGreg Roach    public function canLoadAjax():  bool
109c1010edaSGreg Roach    {
1108c2e8227SGreg Roach        return true;
1118c2e8227SGreg Roach    }
1128c2e8227SGreg Roach
113168ff6f3Sric2016    /**
114168ff6f3Sric2016     * Return a menu item for this chart.
115168ff6f3Sric2016     *
11660bc3e3fSGreg Roach     * @param Individual $individual
11760bc3e3fSGreg Roach     *
1184eb71cfaSGreg Roach     * @return Menu|null
119168ff6f3Sric2016     */
12049a243cbSGreg Roach    public function getChartMenu(Individual $individual): ?Menu
121c1010edaSGreg Roach    {
122168ff6f3Sric2016        return new Menu(
12349a243cbSGreg Roach            $this->title(),
124c1010edaSGreg Roach            route('module', [
125c1010edaSGreg Roach                'module' => $this->getName(),
126c1010edaSGreg Roach                'action' => 'Treeview',
127c0935879SGreg Roach                'xref'   => $individual->xref(),
128f4afa648SGreg Roach                'ged'    => $individual->tree()->name(),
129c1010edaSGreg Roach            ]),
130168ff6f3Sric2016            'menu-chart-tree',
13113abd6f3SGreg Roach            ['rel' => 'nofollow']
132168ff6f3Sric2016        );
133168ff6f3Sric2016    }
134168ff6f3Sric2016
1354eb71cfaSGreg Roach    /**
1364eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1374eb71cfaSGreg Roach     *
13860bc3e3fSGreg Roach     * @param Individual $individual
13960bc3e3fSGreg Roach     *
1404eb71cfaSGreg Roach     * @return Menu|null
1414eb71cfaSGreg Roach     */
14249a243cbSGreg Roach    public function getBoxChartMenu(Individual $individual): ?Menu
143c1010edaSGreg Roach    {
144168ff6f3Sric2016        return $this->getChartMenu($individual);
145168ff6f3Sric2016    }
146168ff6f3Sric2016
14776692c8bSGreg Roach    /**
148e2ae4578SGreg Roach     * @param Request $request
149b6db7c1fSGreg Roach     * @param Tree    $tree
15076692c8bSGreg Roach     *
151e2ae4578SGreg Roach     * @return Response
15276692c8bSGreg Roach     */
153b6db7c1fSGreg Roach    public function getTreeviewAction(Request $request, Tree $tree): Response
154c1010edaSGreg Roach    {
1559e648e55SGreg Roach        $xref = $request->get('xref', '');
156e2ae4578SGreg Roach
157e2ae4578SGreg Roach        $individual = Individual::getInstance($xref, $tree);
158bfaf8159SGreg Roach
159bfaf8159SGreg Roach        if ($individual === null) {
16059f2f229SGreg Roach            throw new IndividualNotFoundException();
161bfaf8159SGreg Roach        }
162bfaf8159SGreg Roach
163bfaf8159SGreg Roach        if (!$individual->canShow()) {
16459f2f229SGreg Roach            throw new IndividualAccessDeniedException();
165bfaf8159SGreg Roach        }
166bfaf8159SGreg Roach
1678c2e8227SGreg Roach        $tv = new TreeView('tv');
1688c2e8227SGreg Roach
16965e02381SGreg Roach        [$html, $js] = $tv->drawViewport($individual, 4);
1708c2e8227SGreg Roach
171e2ae4578SGreg Roach        $title = I18N::translate('Interactive tree of %s', $individual->getFullName());
1728c2e8227SGreg Roach
173e2ae4578SGreg Roach        return $this->viewResponse('interactive-tree-page', [
174e2ae4578SGreg Roach            'title'      => $title,
1758840c547SGreg Roach            'individual' => $individual,
176e2ae4578SGreg Roach            'js'         => $js,
177f8a18b14SGreg Roach            'html'       => $html,
178e2ae4578SGreg Roach            'tree'       => $tree,
179f8a18b14SGreg Roach        ]);
1808c2e8227SGreg Roach    }
1818c2e8227SGreg Roach
182e2ae4578SGreg Roach    /**
183e2ae4578SGreg Roach     * @param Request $request
184b6db7c1fSGreg Roach     * @param Tree    $tree
185e2ae4578SGreg Roach     *
186e2ae4578SGreg Roach     * @return Response
187e2ae4578SGreg Roach     */
188b6db7c1fSGreg Roach    public function getDetailsAction(Request $request, Tree $tree): Response
189c1010edaSGreg Roach    {
1908d0ebef0SGreg Roach        $pid        = $request->get('pid', Gedcom::REGEX_XREF);
191e2ae4578SGreg Roach        $individual = Individual::getInstance($pid, $tree);
192e2ae4578SGreg Roach
1930bc54ba3SGreg Roach        if ($individual === null) {
19459f2f229SGreg Roach            throw new IndividualNotFoundException();
1950bc54ba3SGreg Roach        }
1960bc54ba3SGreg Roach
1970bc54ba3SGreg Roach        if (!$individual->canShow()) {
19859f2f229SGreg Roach            throw new IndividualAccessDeniedException();
1990bc54ba3SGreg Roach        }
2000bc54ba3SGreg Roach
2019e648e55SGreg Roach        $instance = $request->get('instance', '');
202e2ae4578SGreg Roach        $treeview = new TreeView($instance);
203e2ae4578SGreg Roach
204e2ae4578SGreg Roach        return new Response($treeview->getDetails($individual));
2058c2e8227SGreg Roach    }
2068c2e8227SGreg Roach
2078c2e8227SGreg Roach    /**
208e2ae4578SGreg Roach     * @param Request $request
209b6db7c1fSGreg Roach     * @param Tree    $tree
2103cf92ae2SGreg Roach     *
211e2ae4578SGreg Roach     * @return Response
2128c2e8227SGreg Roach     */
213b6db7c1fSGreg Roach    public function getPersonsAction(Request $request, Tree $tree): Response
214c1010edaSGreg Roach    {
2159e648e55SGreg Roach        $q        = $request->get('q', '');
2169e648e55SGreg Roach        $instance = $request->get('instance', '');
217e2ae4578SGreg Roach        $treeview = new TreeView($instance);
2188c2e8227SGreg Roach
219bc8b8f24SGreg Roach        return new Response($treeview->getPersons($tree, $q));
2208c2e8227SGreg Roach    }
2218c2e8227SGreg Roach}
222