xref: /webtrees/app/Module/InteractiveTreeModule.php (revision 8fcd0d32e56ee262912bbdb593202cfd1cbc1615)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
4*8fcd0d32SGreg 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 */
36c1010edaSGreg Roachclass InteractiveTreeModule extends AbstractModule implements ModuleTabInterface, ModuleChartInterface
37c1010edaSGreg Roach{
388c2e8227SGreg Roach    /** {@inheritdoc} */
398f53f488SRico Sonntag    public function getTitle(): string
40c1010edaSGreg Roach    {
41bbb76c12SGreg Roach        /* I18N: Name of a module */
42bbb76c12SGreg Roach        return I18N::translate('Interactive tree');
438c2e8227SGreg Roach    }
448c2e8227SGreg Roach
458c2e8227SGreg Roach    /** {@inheritdoc} */
468f53f488SRico Sonntag    public function getDescription(): string
47c1010edaSGreg Roach    {
48bbb76c12SGreg Roach        /* I18N: Description of the “Interactive tree” module */
49bbb76c12SGreg Roach        return I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
508c2e8227SGreg Roach    }
518c2e8227SGreg Roach
528c2e8227SGreg Roach    /** {@inheritdoc} */
539b34404bSGreg Roach    public function defaultTabOrder(): int
54c1010edaSGreg Roach    {
558c2e8227SGreg Roach        return 68;
568c2e8227SGreg Roach    }
578c2e8227SGreg Roach
588c2e8227SGreg Roach    /** {@inheritdoc} */
599b34404bSGreg Roach    public function getTabContent(Individual $individual): string
60c1010edaSGreg Roach    {
61225e381fSGreg Roach        $treeview = new TreeView('tvTab');
6265e02381SGreg Roach        [$html, $js] = $treeview->drawViewport($individual, 3);
638c2e8227SGreg Roach
64a8cd57e1SGreg Roach        return view('modules/tree/tab', [
65225e381fSGreg Roach            'html'         => $html,
66225e381fSGreg Roach            'js'           => $js,
67d5691647SGreg Roach            'treeview_css' => $this->css(),
68d5691647SGreg Roach            'treeview_js'  => $this->js(),
69225e381fSGreg Roach        ]);
708c2e8227SGreg Roach    }
718c2e8227SGreg Roach
72d5691647SGreg Roach    /**
73d5691647SGreg Roach     * @return string
74d5691647SGreg Roach     */
75c1010edaSGreg Roach    public function css(): string
76c1010edaSGreg Roach    {
778d0ebef0SGreg Roach        return Webtrees::MODULES_PATH . $this->getName() . '/css/treeview.css';
78d5691647SGreg Roach    }
79d5691647SGreg Roach
80d5691647SGreg Roach    /**
81d5691647SGreg Roach     * @return string
82d5691647SGreg Roach     */
83c1010edaSGreg Roach    public function js(): string
84c1010edaSGreg Roach    {
858d0ebef0SGreg Roach        return Webtrees::MODULES_PATH . $this->getName() . '/js/treeview.js';
86d5691647SGreg Roach    }
87d5691647SGreg Roach
888c2e8227SGreg Roach    /** {@inheritdoc} */
899b34404bSGreg Roach    public function hasTabContent(Individual $individual): bool
90c1010edaSGreg Roach    {
9115d603e7SGreg Roach        return true;
928c2e8227SGreg Roach    }
938c2e8227SGreg Roach
948c2e8227SGreg Roach    /** {@inheritdoc} */
959b34404bSGreg Roach    public function isGrayedOut(Individual $individual): bool
96c1010edaSGreg Roach    {
978c2e8227SGreg Roach        return false;
988c2e8227SGreg Roach    }
998c2e8227SGreg Roach
1008c2e8227SGreg Roach    /** {@inheritdoc} */
1019b34404bSGreg Roach    public function canLoadAjax():  bool
102c1010edaSGreg Roach    {
1038c2e8227SGreg Roach        return true;
1048c2e8227SGreg Roach    }
1058c2e8227SGreg Roach
106168ff6f3Sric2016    /**
107168ff6f3Sric2016     * Return a menu item for this chart.
108168ff6f3Sric2016     *
10960bc3e3fSGreg Roach     * @param Individual $individual
11060bc3e3fSGreg Roach     *
1114eb71cfaSGreg Roach     * @return Menu|null
112168ff6f3Sric2016     */
113c1010edaSGreg Roach    public function getChartMenu(Individual $individual)
114c1010edaSGreg Roach    {
115168ff6f3Sric2016        return new Menu(
116168ff6f3Sric2016            $this->getTitle(),
117c1010edaSGreg Roach            route('module', [
118c1010edaSGreg Roach                'module' => $this->getName(),
119c1010edaSGreg Roach                'action' => 'Treeview',
120c0935879SGreg Roach                'xref'   => $individual->xref(),
121f4afa648SGreg Roach                'ged'    => $individual->tree()->name(),
122c1010edaSGreg Roach            ]),
123168ff6f3Sric2016            'menu-chart-tree',
12413abd6f3SGreg Roach            ['rel' => 'nofollow']
125168ff6f3Sric2016        );
126168ff6f3Sric2016    }
127168ff6f3Sric2016
1284eb71cfaSGreg Roach    /**
1294eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1304eb71cfaSGreg Roach     *
13160bc3e3fSGreg Roach     * @param Individual $individual
13260bc3e3fSGreg Roach     *
1334eb71cfaSGreg Roach     * @return Menu|null
1344eb71cfaSGreg Roach     */
135c1010edaSGreg Roach    public function getBoxChartMenu(Individual $individual)
136c1010edaSGreg Roach    {
137168ff6f3Sric2016        return $this->getChartMenu($individual);
138168ff6f3Sric2016    }
139168ff6f3Sric2016
14076692c8bSGreg Roach    /**
141e2ae4578SGreg Roach     * @param Request $request
142b6db7c1fSGreg Roach     * @param Tree    $tree
14376692c8bSGreg Roach     *
144e2ae4578SGreg Roach     * @return Response
14576692c8bSGreg Roach     */
146b6db7c1fSGreg Roach    public function getTreeviewAction(Request $request, Tree $tree): Response
147c1010edaSGreg Roach    {
1489e648e55SGreg Roach        $xref = $request->get('xref', '');
149e2ae4578SGreg Roach
150e2ae4578SGreg Roach        $individual = Individual::getInstance($xref, $tree);
151bfaf8159SGreg Roach
152bfaf8159SGreg Roach        if ($individual === null) {
15359f2f229SGreg Roach            throw new IndividualNotFoundException();
154bfaf8159SGreg Roach        }
155bfaf8159SGreg Roach
156bfaf8159SGreg Roach        if (!$individual->canShow()) {
15759f2f229SGreg Roach            throw new IndividualAccessDeniedException();
158bfaf8159SGreg Roach        }
159bfaf8159SGreg Roach
1608c2e8227SGreg Roach        $tv = new TreeView('tv');
1618c2e8227SGreg Roach
16265e02381SGreg Roach        [$html, $js] = $tv->drawViewport($individual, 4);
1638c2e8227SGreg Roach
164e2ae4578SGreg Roach        $title = I18N::translate('Interactive tree of %s', $individual->getFullName());
1658c2e8227SGreg Roach
166e2ae4578SGreg Roach        return $this->viewResponse('interactive-tree-page', [
167e2ae4578SGreg Roach            'title'      => $title,
1688840c547SGreg Roach            'individual' => $individual,
169e2ae4578SGreg Roach            'js'         => $js,
170f8a18b14SGreg Roach            'html'       => $html,
171e2ae4578SGreg Roach            'tree'       => $tree,
172f8a18b14SGreg Roach        ]);
1738c2e8227SGreg Roach    }
1748c2e8227SGreg Roach
175e2ae4578SGreg Roach    /**
176e2ae4578SGreg Roach     * @param Request $request
177b6db7c1fSGreg Roach     * @param Tree    $tree
178e2ae4578SGreg Roach     *
179e2ae4578SGreg Roach     * @return Response
180e2ae4578SGreg Roach     */
181b6db7c1fSGreg Roach    public function getDetailsAction(Request $request, Tree $tree): Response
182c1010edaSGreg Roach    {
1838d0ebef0SGreg Roach        $pid        = $request->get('pid', Gedcom::REGEX_XREF);
184e2ae4578SGreg Roach        $individual = Individual::getInstance($pid, $tree);
185e2ae4578SGreg Roach
1860bc54ba3SGreg Roach        if ($individual === null) {
18759f2f229SGreg Roach            throw new IndividualNotFoundException();
1880bc54ba3SGreg Roach        }
1890bc54ba3SGreg Roach
1900bc54ba3SGreg Roach        if (!$individual->canShow()) {
19159f2f229SGreg Roach            throw new IndividualAccessDeniedException();
1920bc54ba3SGreg Roach        }
1930bc54ba3SGreg Roach
1949e648e55SGreg Roach        $instance = $request->get('instance', '');
195e2ae4578SGreg Roach        $treeview = new TreeView($instance);
196e2ae4578SGreg Roach
197e2ae4578SGreg Roach        return new Response($treeview->getDetails($individual));
1988c2e8227SGreg Roach    }
1998c2e8227SGreg Roach
2008c2e8227SGreg Roach    /**
201e2ae4578SGreg Roach     * @param Request $request
202b6db7c1fSGreg Roach     * @param Tree    $tree
2033cf92ae2SGreg Roach     *
204e2ae4578SGreg Roach     * @return Response
2058c2e8227SGreg Roach     */
206b6db7c1fSGreg Roach    public function getPersonsAction(Request $request, Tree $tree): Response
207c1010edaSGreg Roach    {
2089e648e55SGreg Roach        $q        = $request->get('q', '');
2099e648e55SGreg Roach        $instance = $request->get('instance', '');
210e2ae4578SGreg Roach        $treeview = new TreeView($instance);
2118c2e8227SGreg Roach
212bc8b8f24SGreg Roach        return new Response($treeview->getPersons($tree, $q));
2138c2e8227SGreg Roach    }
2148c2e8227SGreg Roach}
215