xref: /webtrees/app/Module/InteractiveTreeModule.php (revision 57ab22314b2599feb432b1a1ed71643cfc2f0452)
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
205edf1a44SGreg Roachuse Fisharebest\Webtrees\Auth;
210bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualAccessDeniedException;
220bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
25168ff6f3Sric2016use Fisharebest\Webtrees\Menu;
26e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
276ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
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 */
3437eb8894SGreg Roachclass InteractiveTreeModule extends AbstractModule implements ModuleChartInterface, ModuleTabInterface
35c1010edaSGreg Roach{
3649a243cbSGreg Roach    use ModuleChartTrait;
3749a243cbSGreg Roach    use ModuleTabTrait;
3849a243cbSGreg Roach
39961ec755SGreg Roach    /**
400cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
41961ec755SGreg Roach     *
42961ec755SGreg Roach     * @return string
43961ec755SGreg Roach     */
4449a243cbSGreg Roach    public function title(): string
45c1010edaSGreg Roach    {
46bbb76c12SGreg Roach        /* I18N: Name of a module */
47bbb76c12SGreg Roach        return I18N::translate('Interactive tree');
488c2e8227SGreg Roach    }
498c2e8227SGreg Roach
50961ec755SGreg Roach    /**
51961ec755SGreg Roach     * A sentence describing what this module does.
52961ec755SGreg Roach     *
53961ec755SGreg Roach     * @return string
54961ec755SGreg Roach     */
5549a243cbSGreg Roach    public function description(): string
56c1010edaSGreg Roach    {
57bbb76c12SGreg Roach        /* I18N: Description of the “Interactive tree” module */
58bbb76c12SGreg Roach        return I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
598c2e8227SGreg Roach    }
608c2e8227SGreg Roach
6149a243cbSGreg Roach    /**
6249a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
6349a243cbSGreg Roach     *
6449a243cbSGreg Roach     * @return int
6549a243cbSGreg Roach     */
66cbf4b7faSGreg Roach    public function defaultTabOrder(): int
67cbf4b7faSGreg Roach    {
68fb7a0427SGreg Roach        return 7;
698c2e8227SGreg Roach    }
708c2e8227SGreg Roach
713caaa4d2SGreg Roach    /**
723caaa4d2SGreg Roach     * Generate the HTML content of this tab.
733caaa4d2SGreg Roach     *
743caaa4d2SGreg Roach     * @param Individual $individual
753caaa4d2SGreg Roach     *
763caaa4d2SGreg Roach     * @return string
773caaa4d2SGreg Roach     */
789b34404bSGreg Roach    public function getTabContent(Individual $individual): string
79c1010edaSGreg Roach    {
80225e381fSGreg Roach        $treeview = new TreeView('tvTab');
815edf1a44SGreg Roach
8265e02381SGreg Roach        [$html, $js] = $treeview->drawViewport($individual, 3);
838c2e8227SGreg Roach
845edf1a44SGreg Roach        return view('modules/interactive-tree/tab', [
85225e381fSGreg Roach            'html' => $html,
86225e381fSGreg Roach            'js'   => $js,
87225e381fSGreg Roach        ]);
888c2e8227SGreg Roach    }
898c2e8227SGreg Roach
903caaa4d2SGreg Roach    /**
913caaa4d2SGreg Roach     * Is this tab empty? If so, we don't always need to display it.
923caaa4d2SGreg Roach     *
933caaa4d2SGreg Roach     * @param Individual $individual
943caaa4d2SGreg Roach     *
953caaa4d2SGreg Roach     * @return bool
963caaa4d2SGreg Roach     */
979b34404bSGreg Roach    public function hasTabContent(Individual $individual): bool
98c1010edaSGreg Roach    {
9915d603e7SGreg Roach        return true;
1008c2e8227SGreg Roach    }
1018c2e8227SGreg Roach
1023caaa4d2SGreg Roach    /**
1033caaa4d2SGreg Roach     * A greyed out tab has no actual content, but may perhaps have
1043caaa4d2SGreg Roach     * options to create content.
1053caaa4d2SGreg Roach     *
1063caaa4d2SGreg Roach     * @param Individual $individual
1073caaa4d2SGreg Roach     *
1083caaa4d2SGreg Roach     * @return bool
1093caaa4d2SGreg Roach     */
1109b34404bSGreg Roach    public function isGrayedOut(Individual $individual): bool
111c1010edaSGreg Roach    {
1128c2e8227SGreg Roach        return false;
1138c2e8227SGreg Roach    }
1148c2e8227SGreg Roach
1153caaa4d2SGreg Roach    /**
1163caaa4d2SGreg Roach     * Can this tab load asynchronously?
1173caaa4d2SGreg Roach     *
1183caaa4d2SGreg Roach     * @return bool
1193caaa4d2SGreg Roach     */
1209b34404bSGreg Roach    public function canLoadAjax(): bool
121c1010edaSGreg Roach    {
1228c2e8227SGreg Roach        return true;
1238c2e8227SGreg Roach    }
1248c2e8227SGreg Roach
125168ff6f3Sric2016    /**
126377a2979SGreg Roach     * CSS class for the URL.
127377a2979SGreg Roach     *
128377a2979SGreg Roach     * @return string
129377a2979SGreg Roach     */
130377a2979SGreg Roach    public function chartMenuClass(): string
131377a2979SGreg Roach    {
132377a2979SGreg Roach        return 'menu-chart-tree';
133377a2979SGreg Roach    }
134377a2979SGreg Roach
135377a2979SGreg 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     */
142377a2979SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
143c1010edaSGreg Roach    {
144e6562982SGreg Roach        return $this->chartMenu($individual);
145e6562982SGreg Roach    }
146e6562982SGreg Roach
147e6562982SGreg Roach    /**
148e6562982SGreg Roach     * The title for a specific instance of this chart.
149e6562982SGreg Roach     *
150e6562982SGreg Roach     * @param Individual $individual
151e6562982SGreg Roach     *
152e6562982SGreg Roach     * @return string
153e6562982SGreg Roach     */
154e6562982SGreg Roach    public function chartTitle(Individual $individual): string
155e6562982SGreg Roach    {
156e6562982SGreg Roach        /* I18N: %s is an individual’s name */
15739ca88baSGreg Roach        return I18N::translate('Interactive tree of %s', $individual->fullName());
158e6562982SGreg Roach    }
159e6562982SGreg Roach
160e6562982SGreg Roach    /**
161e6562982SGreg Roach     * The URL for this chart.
162e6562982SGreg Roach     *
163e6562982SGreg Roach     * @param Individual $individual
164e6562982SGreg Roach     * @param string[]   $parameters
165e6562982SGreg Roach     *
166e6562982SGreg Roach     * @return string
167e6562982SGreg Roach     */
168e6562982SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
169e6562982SGreg Roach    {
170e6562982SGreg Roach        return route('module', [
17126684e68SGreg Roach                'module' => $this->name(),
1725edf1a44SGreg Roach                'action' => 'Chart',
173e6562982SGreg Roach                'xref'   => $individual->xref(),
174e6562982SGreg Roach                'ged'    => $individual->tree()->name(),
175e6562982SGreg Roach            ] + $parameters);
176e6562982SGreg Roach    }
177e6562982SGreg Roach
178e6562982SGreg Roach    /**
1796ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
18076692c8bSGreg Roach     *
1816ccdf4f0SGreg Roach     * @return ResponseInterface
18276692c8bSGreg Roach     */
183*57ab2231SGreg Roach    public function getChartAction(ServerRequestInterface $request): ResponseInterface
184c1010edaSGreg Roach    {
185*57ab2231SGreg Roach        $tree = $request->getAttribute('tree');
186*57ab2231SGreg Roach        $user = $request->getAttribute('user');
187e106ff43SGreg Roach        $xref = $request->getQueryParams()['xref'];
188e2ae4578SGreg Roach
189e2ae4578SGreg Roach        $individual = Individual::getInstance($xref, $tree);
190bfaf8159SGreg Roach
1915edf1a44SGreg Roach        Auth::checkIndividualAccess($individual);
1929867b2f0SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
193bfaf8159SGreg Roach
1948c2e8227SGreg Roach        $tv = new TreeView('tv');
1958c2e8227SGreg Roach
19665e02381SGreg Roach        [$html, $js] = $tv->drawViewport($individual, 4);
1978c2e8227SGreg Roach
198575707d1SGreg Roach        return $this->viewResponse('modules/interactive-tree/page', [
1995edf1a44SGreg Roach            'html'       => $html,
2008840c547SGreg Roach            'individual' => $individual,
201e2ae4578SGreg Roach            'js'         => $js,
2025edf1a44SGreg Roach            'title'      => $this->chartTitle($individual),
203e2ae4578SGreg Roach            'tree'       => $tree,
204f8a18b14SGreg Roach        ]);
2058c2e8227SGreg Roach    }
2068c2e8227SGreg Roach
207e2ae4578SGreg Roach    /**
2086ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
209e2ae4578SGreg Roach     *
2106ccdf4f0SGreg Roach     * @return ResponseInterface
211e2ae4578SGreg Roach     */
212*57ab2231SGreg Roach    public function getDetailsAction(ServerRequestInterface $request): ResponseInterface
213c1010edaSGreg Roach    {
214*57ab2231SGreg Roach        $tree       = $request->getAttribute('tree');
215e106ff43SGreg Roach        $pid        = $request->getQueryParams()['pid'];
216e2ae4578SGreg Roach        $individual = Individual::getInstance($pid, $tree);
217e2ae4578SGreg Roach
2180bc54ba3SGreg Roach        if ($individual === null) {
21959f2f229SGreg Roach            throw new IndividualNotFoundException();
2200bc54ba3SGreg Roach        }
2210bc54ba3SGreg Roach
2220bc54ba3SGreg Roach        if (!$individual->canShow()) {
22359f2f229SGreg Roach            throw new IndividualAccessDeniedException();
2240bc54ba3SGreg Roach        }
2250bc54ba3SGreg Roach
226e106ff43SGreg Roach        $instance = $request->getQueryParams()['instance'];
227e2ae4578SGreg Roach        $treeview = new TreeView($instance);
228e2ae4578SGreg Roach
2296ccdf4f0SGreg Roach        return response($treeview->getDetails($individual));
2308c2e8227SGreg Roach    }
2318c2e8227SGreg Roach
2328c2e8227SGreg Roach    /**
2336ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
2343cf92ae2SGreg Roach     *
2356ccdf4f0SGreg Roach     * @return ResponseInterface
2368c2e8227SGreg Roach     */
237*57ab2231SGreg Roach    public function getPersonsAction(ServerRequestInterface $request): ResponseInterface
238c1010edaSGreg Roach    {
239*57ab2231SGreg Roach        $tree     = $request->getAttribute('tree');
240e106ff43SGreg Roach        $q        = $request->getQueryParams()['q'];
241e106ff43SGreg Roach        $instance = $request->getQueryParams()['instance'];
242e2ae4578SGreg Roach        $treeview = new TreeView($instance);
2438c2e8227SGreg Roach
2446ccdf4f0SGreg Roach        return response($treeview->getPersons($tree, $q));
2458c2e8227SGreg Roach    }
2468c2e8227SGreg Roach}
247