xref: /webtrees/app/Module/InteractiveTreeModule.php (revision 76d39c55735cfa9ad0972b0dd530e96b051f9ebe)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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
1589f7189bSGreg Roach * along with this program. If not, see <https://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;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
25168ff6f3Sric2016use Fisharebest\Webtrees\Menu;
26e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
2781b729d3SGreg Roachuse Fisharebest\Webtrees\Registry;
285229eadeSGreg Roachuse Fisharebest\Webtrees\Tree;
29ad40cb91SGreg Roachuse Fisharebest\Webtrees\Validator;
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    {
10476092b6cSGreg Roach        return $individual->facts(['FAMC', 'FAMS'])->isNotEmpty();
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
169*76d39c55SGreg Roach     * @param array<bool|int|string|array<string>|null> $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');
19175964c75SGreg Roach        assert($tree instanceof Tree);
1925229eadeSGreg Roach
193ad40cb91SGreg Roach        $xref = Validator::queryParams($request)->isXref()->requiredString('xref');
194e2ae4578SGreg Roach
1956b9cb339SGreg Roach        $individual = Registry::individualFactory()->make($xref, $tree);
1963c3fd0a5SGreg Roach        $individual = Auth::checkIndividualAccess($individual, false, true);
197bfaf8159SGreg Roach
198ddeb3354SGreg Roach        $user = $request->getAttribute('user');
199ddeb3354SGreg Roach
200ef483801SGreg Roach        Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
201bfaf8159SGreg Roach
2028c2e8227SGreg Roach        $tv = new TreeView('tv');
2038c2e8227SGreg Roach
20465e02381SGreg Roach        [$html, $js] = $tv->drawViewport($individual, 4);
2058c2e8227SGreg Roach
206575707d1SGreg Roach        return $this->viewResponse('modules/interactive-tree/page', [
2075edf1a44SGreg Roach            'html'       => $html,
2088840c547SGreg Roach            'individual' => $individual,
209e2ae4578SGreg Roach            'js'         => $js,
21071378461SGreg Roach            'module'     => $this->name(),
2115edf1a44SGreg Roach            'title'      => $this->chartTitle($individual),
212e2ae4578SGreg Roach            'tree'       => $tree,
213f8a18b14SGreg Roach        ]);
2148c2e8227SGreg Roach    }
2158c2e8227SGreg Roach
2160e519608SGreg Roach
2170e519608SGreg Roach    /**
2180e519608SGreg Roach     * @param ServerRequestInterface $request
2190e519608SGreg Roach     *
2200e519608SGreg Roach     * @return ResponseInterface
2210e519608SGreg Roach     */
2220e519608SGreg Roach    public function postChartAction(ServerRequestInterface $request): ResponseInterface
2230e519608SGreg Roach    {
2240e519608SGreg Roach        $tree = $request->getAttribute('tree');
22575964c75SGreg Roach        assert($tree instanceof Tree);
2260e519608SGreg Roach
227b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
228b46c87bdSGreg Roach
2290e519608SGreg Roach        return redirect(route('module', [
2300e519608SGreg Roach            'module' => $this->name(),
2310e519608SGreg Roach            'action' => 'Chart',
2320e519608SGreg Roach            'tree'   => $tree->name(),
233b46c87bdSGreg Roach            'xref'   => $params['xref'] ?? '',
2340e519608SGreg Roach        ]));
2350e519608SGreg Roach    }
2360e519608SGreg Roach
237e2ae4578SGreg Roach    /**
2386ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
239e2ae4578SGreg Roach     *
2406ccdf4f0SGreg Roach     * @return ResponseInterface
241e2ae4578SGreg Roach     */
24257ab2231SGreg Roach    public function getDetailsAction(ServerRequestInterface $request): ResponseInterface
243c1010edaSGreg Roach    {
24457ab2231SGreg Roach        $tree = $request->getAttribute('tree');
2454ea62551SGreg Roach        assert($tree instanceof Tree);
2464ea62551SGreg Roach
247e106ff43SGreg Roach        $pid        = $request->getQueryParams()['pid'];
2486b9cb339SGreg Roach        $individual = Registry::individualFactory()->make($pid, $tree);
249e2ae4578SGreg Roach
25081b729d3SGreg Roach        $individual = Auth::checkIndividualAccess($individual);
2510bc54ba3SGreg Roach
252e106ff43SGreg Roach        $instance = $request->getQueryParams()['instance'];
253e2ae4578SGreg Roach        $treeview = new TreeView($instance);
254e2ae4578SGreg Roach
2556ccdf4f0SGreg Roach        return response($treeview->getDetails($individual));
2568c2e8227SGreg Roach    }
2578c2e8227SGreg Roach
2588c2e8227SGreg Roach    /**
2596ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
2603cf92ae2SGreg Roach     *
2616ccdf4f0SGreg Roach     * @return ResponseInterface
2628c2e8227SGreg Roach     */
26306f42609SGreg Roach    public function getIndividualsAction(ServerRequestInterface $request): ResponseInterface
264c1010edaSGreg Roach    {
26557ab2231SGreg Roach        $tree = $request->getAttribute('tree');
2664ea62551SGreg Roach        assert($tree instanceof Tree);
2674ea62551SGreg Roach
268e106ff43SGreg Roach        $q        = $request->getQueryParams()['q'];
269e106ff43SGreg Roach        $instance = $request->getQueryParams()['instance'];
270e2ae4578SGreg Roach        $treeview = new TreeView($instance);
2718c2e8227SGreg Roach
27206f42609SGreg Roach        return response($treeview->getIndividuals($tree, $q));
2738c2e8227SGreg Roach    }
2748c2e8227SGreg Roach}
275