xref: /webtrees/app/Module/InteractiveTreeModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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;
28ad40cb91SGreg Roachuse Fisharebest\Webtrees\Validator;
296ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
31f3874e19SGreg 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 */
3637eb8894SGreg Roachclass InteractiveTreeModule extends AbstractModule implements ModuleChartInterface, ModuleTabInterface
37c1010edaSGreg Roach{
3849a243cbSGreg Roach    use ModuleChartTrait;
3949a243cbSGreg Roach    use ModuleTabTrait;
4049a243cbSGreg Roach
41961ec755SGreg Roach    /**
420cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
43961ec755SGreg Roach     *
44961ec755SGreg Roach     * @return string
45961ec755SGreg Roach     */
4649a243cbSGreg Roach    public function title(): string
47c1010edaSGreg Roach    {
48bbb76c12SGreg Roach        /* I18N: Name of a module */
49bbb76c12SGreg Roach        return I18N::translate('Interactive tree');
508c2e8227SGreg Roach    }
518c2e8227SGreg Roach
5249a243cbSGreg Roach    public function description(): string
53c1010edaSGreg Roach    {
54bbb76c12SGreg Roach        /* I18N: Description of the “Interactive tree” module */
55bbb76c12SGreg Roach        return I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
568c2e8227SGreg Roach    }
578c2e8227SGreg Roach
5849a243cbSGreg Roach    /**
5949a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
6049a243cbSGreg Roach     *
6149a243cbSGreg Roach     * @return int
6249a243cbSGreg Roach     */
63cbf4b7faSGreg Roach    public function defaultTabOrder(): int
64cbf4b7faSGreg Roach    {
65fb7a0427SGreg Roach        return 7;
668c2e8227SGreg Roach    }
678c2e8227SGreg Roach
683caaa4d2SGreg Roach    /**
693caaa4d2SGreg Roach     * Generate the HTML content of this tab.
703caaa4d2SGreg Roach     *
713caaa4d2SGreg Roach     * @param Individual $individual
723caaa4d2SGreg Roach     *
733caaa4d2SGreg Roach     * @return string
743caaa4d2SGreg Roach     */
759b34404bSGreg Roach    public function getTabContent(Individual $individual): string
76c1010edaSGreg Roach    {
77225e381fSGreg Roach        $treeview = new TreeView('tvTab');
785edf1a44SGreg Roach
7965e02381SGreg Roach        [$html, $js] = $treeview->drawViewport($individual, 3);
808c2e8227SGreg Roach
815edf1a44SGreg Roach        return view('modules/interactive-tree/tab', [
82225e381fSGreg Roach            'html' => $html,
83225e381fSGreg Roach            'js'   => $js,
84225e381fSGreg Roach        ]);
858c2e8227SGreg Roach    }
868c2e8227SGreg Roach
873caaa4d2SGreg Roach    /**
883caaa4d2SGreg Roach     * Is this tab empty? If so, we don't always need to display it.
893caaa4d2SGreg Roach     *
903caaa4d2SGreg Roach     * @param Individual $individual
913caaa4d2SGreg Roach     *
923caaa4d2SGreg Roach     * @return bool
933caaa4d2SGreg Roach     */
949b34404bSGreg Roach    public function hasTabContent(Individual $individual): bool
95c1010edaSGreg Roach    {
9676092b6cSGreg Roach        return $individual->facts(['FAMC', 'FAMS'])->isNotEmpty();
978c2e8227SGreg Roach    }
988c2e8227SGreg Roach
993caaa4d2SGreg Roach    /**
1003caaa4d2SGreg Roach     * A greyed out tab has no actual content, but may perhaps have
1013caaa4d2SGreg Roach     * options to create content.
1023caaa4d2SGreg Roach     *
1033caaa4d2SGreg Roach     * @param Individual $individual
1043caaa4d2SGreg Roach     *
1053caaa4d2SGreg Roach     * @return bool
1063caaa4d2SGreg Roach     */
1079b34404bSGreg Roach    public function isGrayedOut(Individual $individual): bool
108c1010edaSGreg Roach    {
1098c2e8227SGreg Roach        return false;
1108c2e8227SGreg Roach    }
1118c2e8227SGreg Roach
1123caaa4d2SGreg Roach    /**
1133caaa4d2SGreg Roach     * Can this tab load asynchronously?
1143caaa4d2SGreg Roach     *
1153caaa4d2SGreg Roach     * @return bool
1163caaa4d2SGreg Roach     */
1179b34404bSGreg Roach    public function canLoadAjax(): bool
118c1010edaSGreg Roach    {
1198c2e8227SGreg Roach        return true;
1208c2e8227SGreg Roach    }
1218c2e8227SGreg Roach
122168ff6f3Sric2016    /**
123377a2979SGreg Roach     * CSS class for the URL.
124377a2979SGreg Roach     *
125377a2979SGreg Roach     * @return string
126377a2979SGreg Roach     */
127377a2979SGreg Roach    public function chartMenuClass(): string
128377a2979SGreg Roach    {
129377a2979SGreg Roach        return 'menu-chart-tree';
130377a2979SGreg Roach    }
131377a2979SGreg Roach
132377a2979SGreg Roach    /**
1334eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1344eb71cfaSGreg Roach     *
13560bc3e3fSGreg Roach     * @param Individual $individual
13660bc3e3fSGreg Roach     *
1374eb71cfaSGreg Roach     * @return Menu|null
1384eb71cfaSGreg Roach     */
139*1ff45046SGreg Roach    public function chartBoxMenu(Individual $individual): Menu|null
140c1010edaSGreg Roach    {
141e6562982SGreg Roach        return $this->chartMenu($individual);
142e6562982SGreg Roach    }
143e6562982SGreg Roach
144e6562982SGreg Roach    /**
145e6562982SGreg Roach     * The title for a specific instance of this chart.
146e6562982SGreg Roach     *
147e6562982SGreg Roach     * @param Individual $individual
148e6562982SGreg Roach     *
149e6562982SGreg Roach     * @return string
150e6562982SGreg Roach     */
151e6562982SGreg Roach    public function chartTitle(Individual $individual): string
152e6562982SGreg Roach    {
153e6562982SGreg Roach        /* I18N: %s is an individual’s name */
15439ca88baSGreg Roach        return I18N::translate('Interactive tree of %s', $individual->fullName());
155e6562982SGreg Roach    }
156e6562982SGreg Roach
157e6562982SGreg Roach    /**
158e6562982SGreg Roach     * The URL for this chart.
159e6562982SGreg Roach     *
160e6562982SGreg Roach     * @param Individual                                $individual
16176d39c55SGreg Roach     * @param array<bool|int|string|array<string>|null> $parameters
162e6562982SGreg Roach     *
163e6562982SGreg Roach     * @return string
164e6562982SGreg Roach     */
165e6562982SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
166e6562982SGreg Roach    {
167e6562982SGreg Roach        return route('module', [
16826684e68SGreg Roach                'module' => $this->name(),
1695edf1a44SGreg Roach                'action' => 'Chart',
170e6562982SGreg Roach                'xref'   => $individual->xref(),
1719022ab66SGreg Roach                'tree'    => $individual->tree()->name(),
172e6562982SGreg Roach            ] + $parameters);
173e6562982SGreg Roach    }
174e6562982SGreg Roach
175e6562982SGreg Roach    /**
1766ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
17776692c8bSGreg Roach     *
1786ccdf4f0SGreg Roach     * @return ResponseInterface
17976692c8bSGreg Roach     */
18057ab2231SGreg Roach    public function getChartAction(ServerRequestInterface $request): ResponseInterface
181c1010edaSGreg Roach    {
182b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
183b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
184b55cbc6bSGreg Roach        $xref = Validator::queryParams($request)->isXref()->string('xref');
1855229eadeSGreg Roach
186b55cbc6bSGreg Roach        Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
187e2ae4578SGreg Roach
1886b9cb339SGreg Roach        $individual = Registry::individualFactory()->make($xref, $tree);
1893c3fd0a5SGreg Roach        $individual = Auth::checkIndividualAccess($individual, false, true);
190bfaf8159SGreg Roach
1918c2e8227SGreg Roach        $tv = new TreeView('tv');
1928c2e8227SGreg Roach
19365e02381SGreg Roach        [$html, $js] = $tv->drawViewport($individual, 4);
1948c2e8227SGreg Roach
195575707d1SGreg Roach        return $this->viewResponse('modules/interactive-tree/page', [
1965edf1a44SGreg Roach            'html'       => $html,
1978840c547SGreg Roach            'individual' => $individual,
198e2ae4578SGreg Roach            'js'         => $js,
19971378461SGreg Roach            'module'     => $this->name(),
2005edf1a44SGreg Roach            'title'      => $this->chartTitle($individual),
201e2ae4578SGreg Roach            'tree'       => $tree,
202f8a18b14SGreg Roach        ]);
2038c2e8227SGreg Roach    }
2048c2e8227SGreg Roach
2050e519608SGreg Roach    /**
2060e519608SGreg Roach     * @param ServerRequestInterface $request
2070e519608SGreg Roach     *
2080e519608SGreg Roach     * @return ResponseInterface
2090e519608SGreg Roach     */
2100e519608SGreg Roach    public function postChartAction(ServerRequestInterface $request): ResponseInterface
2110e519608SGreg Roach    {
2120e519608SGreg Roach        return redirect(route('module', [
2130e519608SGreg Roach            'module' => $this->name(),
2140e519608SGreg Roach            'action' => 'Chart',
215748dbe15SGreg Roach            'tree'   => Validator::attributes($request)->tree()->name(),
216748dbe15SGreg Roach            'xref'   => Validator::parsedBody($request)->isXref()->string('xref'),
2170e519608SGreg Roach        ]));
2180e519608SGreg Roach    }
2190e519608SGreg Roach
220e2ae4578SGreg Roach    /**
2216ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
222e2ae4578SGreg Roach     *
2236ccdf4f0SGreg Roach     * @return ResponseInterface
224e2ae4578SGreg Roach     */
22557ab2231SGreg Roach    public function getDetailsAction(ServerRequestInterface $request): ResponseInterface
226c1010edaSGreg Roach    {
227b55cbc6bSGreg Roach        $tree       = Validator::attributes($request)->tree();
228748dbe15SGreg Roach        $pid        = Validator::queryParams($request)->string('pid');
2296b9cb339SGreg Roach        $individual = Registry::individualFactory()->make($pid, $tree);
23081b729d3SGreg Roach        $individual = Auth::checkIndividualAccess($individual);
231748dbe15SGreg Roach        $instance   = Validator::queryParams($request)->string('instance');
232e2ae4578SGreg Roach        $treeview   = new TreeView($instance);
233e2ae4578SGreg Roach
2346ccdf4f0SGreg Roach        return response($treeview->getDetails($individual));
2358c2e8227SGreg Roach    }
2368c2e8227SGreg Roach
2378c2e8227SGreg Roach    /**
2386ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
2393cf92ae2SGreg Roach     *
2406ccdf4f0SGreg Roach     * @return ResponseInterface
2418c2e8227SGreg Roach     */
24206f42609SGreg Roach    public function getIndividualsAction(ServerRequestInterface $request): ResponseInterface
243c1010edaSGreg Roach    {
244b55cbc6bSGreg Roach        $tree     = Validator::attributes($request)->tree();
245748dbe15SGreg Roach        $q        = Validator::queryParams($request)->string('q');
246748dbe15SGreg Roach        $instance = Validator::queryParams($request)->string('instance');
247e2ae4578SGreg Roach        $treeview = new TreeView($instance);
2488c2e8227SGreg Roach
24906f42609SGreg Roach        return response($treeview->getIndividuals($tree, $q));
2508c2e8227SGreg Roach    }
2518c2e8227SGreg Roach}
252