xref: /webtrees/app/Module/InteractiveTreeModule.php (revision c09358794665832df009408551d8543ccb6e3783)
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 */
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;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
24168ff6f3Sric2016use Fisharebest\Webtrees\Menu;
25e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
26e2ae4578SGreg Roachuse Fisharebest\Webtrees\Tree;
27e2ae4578SGreg Roachuse Symfony\Component\HttpFoundation\Request;
28e2ae4578SGreg Roachuse Symfony\Component\HttpFoundation\Response;
298c2e8227SGreg Roach
308c2e8227SGreg Roach/**
318c2e8227SGreg Roach * Class InteractiveTreeModule
328c2e8227SGreg 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
338c2e8227SGreg Roach */
34c1010edaSGreg Roachclass InteractiveTreeModule extends AbstractModule implements ModuleTabInterface, ModuleChartInterface
35c1010edaSGreg Roach{
368c2e8227SGreg Roach    /** {@inheritdoc} */
378f53f488SRico Sonntag    public function getTitle(): string
38c1010edaSGreg Roach    {
39bbb76c12SGreg Roach        /* I18N: Name of a module */
40bbb76c12SGreg Roach        return I18N::translate('Interactive tree');
418c2e8227SGreg Roach    }
428c2e8227SGreg Roach
438c2e8227SGreg Roach    /** {@inheritdoc} */
448f53f488SRico Sonntag    public function getDescription(): string
45c1010edaSGreg Roach    {
46bbb76c12SGreg Roach        /* I18N: Description of the “Interactive tree” module */
47bbb76c12SGreg Roach        return I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
488c2e8227SGreg Roach    }
498c2e8227SGreg Roach
508c2e8227SGreg Roach    /** {@inheritdoc} */
519b34404bSGreg Roach    public function defaultTabOrder(): int
52c1010edaSGreg Roach    {
538c2e8227SGreg Roach        return 68;
548c2e8227SGreg Roach    }
558c2e8227SGreg Roach
568c2e8227SGreg Roach    /** {@inheritdoc} */
579b34404bSGreg Roach    public function getTabContent(Individual $individual): string
58c1010edaSGreg Roach    {
59225e381fSGreg Roach        $treeview = new TreeView('tvTab');
60225e381fSGreg Roach        list($html, $js) = $treeview->drawViewport($individual, 3);
618c2e8227SGreg Roach
62a8cd57e1SGreg Roach        return view('modules/tree/tab', [
63225e381fSGreg Roach            'html'         => $html,
64225e381fSGreg Roach            'js'           => $js,
65d5691647SGreg Roach            'treeview_css' => $this->css(),
66d5691647SGreg Roach            'treeview_js'  => $this->js(),
67225e381fSGreg Roach        ]);
688c2e8227SGreg Roach    }
698c2e8227SGreg Roach
70d5691647SGreg Roach    /**
71d5691647SGreg Roach     * @return string
72d5691647SGreg Roach     */
73c1010edaSGreg Roach    public function css(): string
74c1010edaSGreg Roach    {
75d5691647SGreg Roach        return WT_MODULES_DIR . $this->getName() . '/css/treeview.css';
76d5691647SGreg Roach    }
77d5691647SGreg Roach
78d5691647SGreg Roach    /**
79d5691647SGreg Roach     * @return string
80d5691647SGreg Roach     */
81c1010edaSGreg Roach    public function js(): string
82c1010edaSGreg Roach    {
83d5691647SGreg Roach        return WT_MODULES_DIR . $this->getName() . '/js/treeview.js';
84d5691647SGreg Roach    }
85d5691647SGreg Roach
868c2e8227SGreg Roach    /** {@inheritdoc} */
879b34404bSGreg Roach    public function hasTabContent(Individual $individual): bool
88c1010edaSGreg Roach    {
8915d603e7SGreg Roach        return true;
908c2e8227SGreg Roach    }
918c2e8227SGreg Roach
928c2e8227SGreg Roach    /** {@inheritdoc} */
939b34404bSGreg Roach    public function isGrayedOut(Individual $individual): bool
94c1010edaSGreg Roach    {
958c2e8227SGreg Roach        return false;
968c2e8227SGreg Roach    }
978c2e8227SGreg Roach
988c2e8227SGreg Roach    /** {@inheritdoc} */
999b34404bSGreg Roach    public function canLoadAjax():  bool
100c1010edaSGreg Roach    {
1018c2e8227SGreg Roach        return true;
1028c2e8227SGreg Roach    }
1038c2e8227SGreg Roach
104168ff6f3Sric2016    /**
105168ff6f3Sric2016     * Return a menu item for this chart.
106168ff6f3Sric2016     *
10760bc3e3fSGreg Roach     * @param Individual $individual
10860bc3e3fSGreg Roach     *
1094eb71cfaSGreg Roach     * @return Menu|null
110168ff6f3Sric2016     */
111c1010edaSGreg Roach    public function getChartMenu(Individual $individual)
112c1010edaSGreg Roach    {
113168ff6f3Sric2016        return new Menu(
114168ff6f3Sric2016            $this->getTitle(),
115c1010edaSGreg Roach            route('module', [
116c1010edaSGreg Roach                'module' => $this->getName(),
117c1010edaSGreg Roach                'action' => 'Treeview',
118*c0935879SGreg Roach                'xref'   => $individual->xref(),
119aa6f03bbSGreg Roach                'ged'    => $individual->getTree()->name(),
120c1010edaSGreg Roach            ]),
121168ff6f3Sric2016            'menu-chart-tree',
12213abd6f3SGreg Roach            ['rel' => 'nofollow']
123168ff6f3Sric2016        );
124168ff6f3Sric2016    }
125168ff6f3Sric2016
1264eb71cfaSGreg Roach    /**
1274eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1284eb71cfaSGreg Roach     *
12960bc3e3fSGreg Roach     * @param Individual $individual
13060bc3e3fSGreg Roach     *
1314eb71cfaSGreg Roach     * @return Menu|null
1324eb71cfaSGreg Roach     */
133c1010edaSGreg Roach    public function getBoxChartMenu(Individual $individual)
134c1010edaSGreg Roach    {
135168ff6f3Sric2016        return $this->getChartMenu($individual);
136168ff6f3Sric2016    }
137168ff6f3Sric2016
13876692c8bSGreg Roach    /**
139e2ae4578SGreg Roach     * @param Request $request
140b6db7c1fSGreg Roach     * @param Tree    $tree
14176692c8bSGreg Roach     *
142e2ae4578SGreg Roach     * @return Response
14376692c8bSGreg Roach     */
144b6db7c1fSGreg Roach    public function getTreeviewAction(Request $request, Tree $tree): Response
145c1010edaSGreg Roach    {
1469e648e55SGreg Roach        $xref = $request->get('xref', '');
147e2ae4578SGreg Roach
148e2ae4578SGreg Roach        $individual = Individual::getInstance($xref, $tree);
149bfaf8159SGreg Roach
150bfaf8159SGreg Roach        if ($individual === null) {
15159f2f229SGreg Roach            throw new IndividualNotFoundException();
152bfaf8159SGreg Roach        }
153bfaf8159SGreg Roach
154bfaf8159SGreg Roach        if (!$individual->canShow()) {
15559f2f229SGreg Roach            throw new IndividualAccessDeniedException();
156bfaf8159SGreg Roach        }
157bfaf8159SGreg Roach
1588c2e8227SGreg Roach        $tv = new TreeView('tv');
1598c2e8227SGreg Roach
160e2ae4578SGreg Roach        list($html, $js) = $tv->drawViewport($individual, 4);
1618c2e8227SGreg Roach
162e2ae4578SGreg Roach        $title = I18N::translate('Interactive tree of %s', $individual->getFullName());
1638c2e8227SGreg Roach
164e2ae4578SGreg Roach        return $this->viewResponse('interactive-tree-page', [
165e2ae4578SGreg Roach            'title'      => $title,
1668840c547SGreg Roach            'individual' => $individual,
167e2ae4578SGreg Roach            'js'         => $js,
168f8a18b14SGreg Roach            'html'       => $html,
169e2ae4578SGreg Roach            'tree'       => $tree,
170f8a18b14SGreg Roach        ]);
1718c2e8227SGreg Roach    }
1728c2e8227SGreg Roach
173e2ae4578SGreg Roach    /**
174e2ae4578SGreg Roach     * @param Request $request
175b6db7c1fSGreg Roach     * @param Tree    $tree
176e2ae4578SGreg Roach     *
177e2ae4578SGreg Roach     * @return Response
178e2ae4578SGreg Roach     */
179b6db7c1fSGreg Roach    public function getDetailsAction(Request $request, Tree $tree): Response
180c1010edaSGreg Roach    {
181e2ae4578SGreg Roach        $pid        = $request->get('pid', WT_REGEX_XREF);
182e2ae4578SGreg Roach        $individual = Individual::getInstance($pid, $tree);
183e2ae4578SGreg Roach
1840bc54ba3SGreg Roach        if ($individual === null) {
18559f2f229SGreg Roach            throw new IndividualNotFoundException();
1860bc54ba3SGreg Roach        }
1870bc54ba3SGreg Roach
1880bc54ba3SGreg Roach        if (!$individual->canShow()) {
18959f2f229SGreg Roach            throw new IndividualAccessDeniedException();
1900bc54ba3SGreg Roach        }
1910bc54ba3SGreg Roach
1929e648e55SGreg Roach        $instance = $request->get('instance', '');
193e2ae4578SGreg Roach        $treeview = new TreeView($instance);
194e2ae4578SGreg Roach
195e2ae4578SGreg Roach        return new Response($treeview->getDetails($individual));
1968c2e8227SGreg Roach    }
1978c2e8227SGreg Roach
1988c2e8227SGreg Roach    /**
199e2ae4578SGreg Roach     * @param Request $request
200b6db7c1fSGreg Roach     * @param Tree    $tree
2013cf92ae2SGreg Roach     *
202e2ae4578SGreg Roach     * @return Response
2038c2e8227SGreg Roach     */
204b6db7c1fSGreg Roach    public function getPersonsAction(Request $request, Tree $tree): Response
205c1010edaSGreg Roach    {
2069e648e55SGreg Roach        $q        = $request->get('q', '');
2079e648e55SGreg Roach        $instance = $request->get('instance', '');
208e2ae4578SGreg Roach        $treeview = new TreeView($instance);
2098c2e8227SGreg Roach
210bc8b8f24SGreg Roach        return new Response($treeview->getPersons($tree, $q));
2118c2e8227SGreg Roach    }
2128c2e8227SGreg Roach}
213