xref: /webtrees/app/Module/InteractiveTreeModule.php (revision 75964c75fd01f8e12e2a313cc89fc2222413a03f)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
225edf1a44SGreg Roachuse Fisharebest\Webtrees\Auth;
230bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualAccessDeniedException;
240bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
27168ff6f3Sric2016use Fisharebest\Webtrees\Menu;
28e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
295229eadeSGreg Roachuse Fisharebest\Webtrees\Tree;
306ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
316ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
32f3874e19SGreg Roach
335229eadeSGreg Roachuse function assert;
348c2e8227SGreg Roach
358c2e8227SGreg Roach/**
368c2e8227SGreg Roach * Class InteractiveTreeModule
378c2e8227SGreg 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
388c2e8227SGreg Roach */
3937eb8894SGreg Roachclass InteractiveTreeModule extends AbstractModule implements ModuleChartInterface, ModuleTabInterface
40c1010edaSGreg Roach{
4149a243cbSGreg Roach    use ModuleChartTrait;
4249a243cbSGreg Roach    use ModuleTabTrait;
4349a243cbSGreg Roach
44961ec755SGreg Roach    /**
450cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
46961ec755SGreg Roach     *
47961ec755SGreg Roach     * @return string
48961ec755SGreg Roach     */
4949a243cbSGreg Roach    public function title(): string
50c1010edaSGreg Roach    {
51bbb76c12SGreg Roach        /* I18N: Name of a module */
52bbb76c12SGreg Roach        return I18N::translate('Interactive tree');
538c2e8227SGreg Roach    }
548c2e8227SGreg Roach
55961ec755SGreg Roach    /**
56961ec755SGreg Roach     * A sentence describing what this module does.
57961ec755SGreg Roach     *
58961ec755SGreg Roach     * @return string
59961ec755SGreg Roach     */
6049a243cbSGreg Roach    public function description(): string
61c1010edaSGreg Roach    {
62bbb76c12SGreg Roach        /* I18N: Description of the “Interactive tree” module */
63bbb76c12SGreg Roach        return I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
648c2e8227SGreg Roach    }
658c2e8227SGreg Roach
6649a243cbSGreg Roach    /**
6749a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
6849a243cbSGreg Roach     *
6949a243cbSGreg Roach     * @return int
7049a243cbSGreg Roach     */
71cbf4b7faSGreg Roach    public function defaultTabOrder(): int
72cbf4b7faSGreg Roach    {
73fb7a0427SGreg Roach        return 7;
748c2e8227SGreg Roach    }
758c2e8227SGreg Roach
763caaa4d2SGreg Roach    /**
773caaa4d2SGreg Roach     * Generate the HTML content of this tab.
783caaa4d2SGreg Roach     *
793caaa4d2SGreg Roach     * @param Individual $individual
803caaa4d2SGreg Roach     *
813caaa4d2SGreg Roach     * @return string
823caaa4d2SGreg Roach     */
839b34404bSGreg Roach    public function getTabContent(Individual $individual): string
84c1010edaSGreg Roach    {
85225e381fSGreg Roach        $treeview = new TreeView('tvTab');
865edf1a44SGreg Roach
8765e02381SGreg Roach        [$html, $js] = $treeview->drawViewport($individual, 3);
888c2e8227SGreg Roach
895edf1a44SGreg Roach        return view('modules/interactive-tree/tab', [
90225e381fSGreg Roach            'html' => $html,
91225e381fSGreg Roach            'js'   => $js,
92225e381fSGreg Roach        ]);
938c2e8227SGreg Roach    }
948c2e8227SGreg Roach
953caaa4d2SGreg Roach    /**
963caaa4d2SGreg Roach     * Is this tab empty? If so, we don't always need to display it.
973caaa4d2SGreg Roach     *
983caaa4d2SGreg Roach     * @param Individual $individual
993caaa4d2SGreg Roach     *
1003caaa4d2SGreg Roach     * @return bool
1013caaa4d2SGreg Roach     */
1029b34404bSGreg Roach    public function hasTabContent(Individual $individual): bool
103c1010edaSGreg Roach    {
10415d603e7SGreg Roach        return true;
1058c2e8227SGreg Roach    }
1068c2e8227SGreg Roach
1073caaa4d2SGreg Roach    /**
1083caaa4d2SGreg Roach     * A greyed out tab has no actual content, but may perhaps have
1093caaa4d2SGreg Roach     * options to create content.
1103caaa4d2SGreg Roach     *
1113caaa4d2SGreg Roach     * @param Individual $individual
1123caaa4d2SGreg Roach     *
1133caaa4d2SGreg Roach     * @return bool
1143caaa4d2SGreg Roach     */
1159b34404bSGreg Roach    public function isGrayedOut(Individual $individual): bool
116c1010edaSGreg Roach    {
1178c2e8227SGreg Roach        return false;
1188c2e8227SGreg Roach    }
1198c2e8227SGreg Roach
1203caaa4d2SGreg Roach    /**
1213caaa4d2SGreg Roach     * Can this tab load asynchronously?
1223caaa4d2SGreg Roach     *
1233caaa4d2SGreg Roach     * @return bool
1243caaa4d2SGreg Roach     */
1259b34404bSGreg Roach    public function canLoadAjax(): bool
126c1010edaSGreg Roach    {
1278c2e8227SGreg Roach        return true;
1288c2e8227SGreg Roach    }
1298c2e8227SGreg Roach
130168ff6f3Sric2016    /**
131377a2979SGreg Roach     * CSS class for the URL.
132377a2979SGreg Roach     *
133377a2979SGreg Roach     * @return string
134377a2979SGreg Roach     */
135377a2979SGreg Roach    public function chartMenuClass(): string
136377a2979SGreg Roach    {
137377a2979SGreg Roach        return 'menu-chart-tree';
138377a2979SGreg Roach    }
139377a2979SGreg Roach
140377a2979SGreg Roach    /**
1414eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1424eb71cfaSGreg Roach     *
14360bc3e3fSGreg Roach     * @param Individual $individual
14460bc3e3fSGreg Roach     *
1454eb71cfaSGreg Roach     * @return Menu|null
1464eb71cfaSGreg Roach     */
147377a2979SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
148c1010edaSGreg Roach    {
149e6562982SGreg Roach        return $this->chartMenu($individual);
150e6562982SGreg Roach    }
151e6562982SGreg Roach
152e6562982SGreg Roach    /**
153e6562982SGreg Roach     * The title for a specific instance of this chart.
154e6562982SGreg Roach     *
155e6562982SGreg Roach     * @param Individual $individual
156e6562982SGreg Roach     *
157e6562982SGreg Roach     * @return string
158e6562982SGreg Roach     */
159e6562982SGreg Roach    public function chartTitle(Individual $individual): string
160e6562982SGreg Roach    {
161e6562982SGreg Roach        /* I18N: %s is an individual’s name */
16239ca88baSGreg Roach        return I18N::translate('Interactive tree of %s', $individual->fullName());
163e6562982SGreg Roach    }
164e6562982SGreg Roach
165e6562982SGreg Roach    /**
166e6562982SGreg Roach     * The URL for this chart.
167e6562982SGreg Roach     *
168e6562982SGreg Roach     * @param Individual $individual
16959597b37SGreg Roach     * @param mixed[]    $parameters
170e6562982SGreg Roach     *
171e6562982SGreg Roach     * @return string
172e6562982SGreg Roach     */
173e6562982SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
174e6562982SGreg Roach    {
175e6562982SGreg Roach        return route('module', [
17626684e68SGreg Roach                'module' => $this->name(),
1775edf1a44SGreg Roach                'action' => 'Chart',
178e6562982SGreg Roach                'xref'   => $individual->xref(),
1799022ab66SGreg Roach                'tree'    => $individual->tree()->name(),
180e6562982SGreg Roach            ] + $parameters);
181e6562982SGreg Roach    }
182e6562982SGreg Roach
183e6562982SGreg Roach    /**
1846ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
18576692c8bSGreg Roach     *
1866ccdf4f0SGreg Roach     * @return ResponseInterface
18776692c8bSGreg Roach     */
18857ab2231SGreg Roach    public function getChartAction(ServerRequestInterface $request): ResponseInterface
189c1010edaSGreg Roach    {
19057ab2231SGreg Roach        $tree = $request->getAttribute('tree');
191*75964c75SGreg Roach        assert($tree instanceof Tree);
1925229eadeSGreg Roach
19357ab2231SGreg Roach        $user = $request->getAttribute('user');
194e106ff43SGreg Roach        $xref = $request->getQueryParams()['xref'];
195e2ae4578SGreg Roach
196e2ae4578SGreg Roach        $individual = Individual::getInstance($xref, $tree);
197bfaf8159SGreg Roach
1985edf1a44SGreg Roach        Auth::checkIndividualAccess($individual);
1999867b2f0SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
200bfaf8159SGreg Roach
2018c2e8227SGreg Roach        $tv = new TreeView('tv');
2028c2e8227SGreg Roach
20365e02381SGreg Roach        [$html, $js] = $tv->drawViewport($individual, 4);
2048c2e8227SGreg Roach
205575707d1SGreg Roach        return $this->viewResponse('modules/interactive-tree/page', [
2065edf1a44SGreg Roach            'html'       => $html,
2078840c547SGreg Roach            'individual' => $individual,
208e2ae4578SGreg Roach            'js'         => $js,
20971378461SGreg Roach            'module'     => $this->name(),
2105edf1a44SGreg Roach            'title'      => $this->chartTitle($individual),
211e2ae4578SGreg Roach            'tree'       => $tree,
212f8a18b14SGreg Roach        ]);
2138c2e8227SGreg Roach    }
2148c2e8227SGreg Roach
2150e519608SGreg Roach
2160e519608SGreg Roach    /**
2170e519608SGreg Roach     * @param ServerRequestInterface $request
2180e519608SGreg Roach     *
2190e519608SGreg Roach     * @return ResponseInterface
2200e519608SGreg Roach     */
2210e519608SGreg Roach    public function postChartAction(ServerRequestInterface $request): ResponseInterface
2220e519608SGreg Roach    {
2230e519608SGreg Roach        $tree = $request->getAttribute('tree');
224*75964c75SGreg Roach        assert($tree instanceof Tree);
2250e519608SGreg Roach
2260e519608SGreg Roach        return redirect(route('module', [
2270e519608SGreg Roach            'module' => $this->name(),
2280e519608SGreg Roach            'action' => 'Chart',
2290e519608SGreg Roach            'tree'   => $tree->name(),
2300e519608SGreg Roach            'xref'   => $request->getParsedBody()['xref'] ?? '',
2310e519608SGreg Roach        ]));
2320e519608SGreg Roach    }
2330e519608SGreg Roach
234e2ae4578SGreg Roach    /**
2356ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
236e2ae4578SGreg Roach     *
2376ccdf4f0SGreg Roach     * @return ResponseInterface
238e2ae4578SGreg Roach     */
23957ab2231SGreg Roach    public function getDetailsAction(ServerRequestInterface $request): ResponseInterface
240c1010edaSGreg Roach    {
24157ab2231SGreg Roach        $tree       = $request->getAttribute('tree');
242e106ff43SGreg Roach        $pid        = $request->getQueryParams()['pid'];
243e2ae4578SGreg Roach        $individual = Individual::getInstance($pid, $tree);
244e2ae4578SGreg Roach
2450bc54ba3SGreg Roach        if ($individual === null) {
24659f2f229SGreg Roach            throw new IndividualNotFoundException();
2470bc54ba3SGreg Roach        }
2480bc54ba3SGreg Roach
2490bc54ba3SGreg Roach        if (!$individual->canShow()) {
25059f2f229SGreg Roach            throw new IndividualAccessDeniedException();
2510bc54ba3SGreg Roach        }
2520bc54ba3SGreg Roach
253e106ff43SGreg Roach        $instance = $request->getQueryParams()['instance'];
254e2ae4578SGreg Roach        $treeview = new TreeView($instance);
255e2ae4578SGreg Roach
2566ccdf4f0SGreg Roach        return response($treeview->getDetails($individual));
2578c2e8227SGreg Roach    }
2588c2e8227SGreg Roach
2598c2e8227SGreg Roach    /**
2606ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
2613cf92ae2SGreg Roach     *
2626ccdf4f0SGreg Roach     * @return ResponseInterface
2638c2e8227SGreg Roach     */
26406f42609SGreg Roach    public function getIndividualsAction(ServerRequestInterface $request): ResponseInterface
265c1010edaSGreg Roach    {
26657ab2231SGreg Roach        $tree     = $request->getAttribute('tree');
267e106ff43SGreg Roach        $q        = $request->getQueryParams()['q'];
268e106ff43SGreg Roach        $instance = $request->getQueryParams()['instance'];
269e2ae4578SGreg Roach        $treeview = new TreeView($instance);
2708c2e8227SGreg Roach
27106f42609SGreg Roach        return response($treeview->getIndividuals($tree, $q));
2728c2e8227SGreg Roach    }
2738c2e8227SGreg Roach}
274