xref: /webtrees/app/Module/InteractiveTreeModule.php (revision 59597b37d69e8147c3f4a27643e9c8edaa2a0592)
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;
305229eadeSGreg Roachuse InvalidArgumentException;
316ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
326ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
33f3874e19SGreg Roach
345229eadeSGreg Roachuse function assert;
358c2e8227SGreg Roach
368c2e8227SGreg Roach/**
378c2e8227SGreg Roach * Class InteractiveTreeModule
388c2e8227SGreg 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
398c2e8227SGreg Roach */
4037eb8894SGreg Roachclass InteractiveTreeModule extends AbstractModule implements ModuleChartInterface, ModuleTabInterface
41c1010edaSGreg Roach{
4249a243cbSGreg Roach    use ModuleChartTrait;
4349a243cbSGreg Roach    use ModuleTabTrait;
4449a243cbSGreg Roach
45961ec755SGreg Roach    /**
460cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
47961ec755SGreg Roach     *
48961ec755SGreg Roach     * @return string
49961ec755SGreg Roach     */
5049a243cbSGreg Roach    public function title(): string
51c1010edaSGreg Roach    {
52bbb76c12SGreg Roach        /* I18N: Name of a module */
53bbb76c12SGreg Roach        return I18N::translate('Interactive tree');
548c2e8227SGreg Roach    }
558c2e8227SGreg Roach
56961ec755SGreg Roach    /**
57961ec755SGreg Roach     * A sentence describing what this module does.
58961ec755SGreg Roach     *
59961ec755SGreg Roach     * @return string
60961ec755SGreg Roach     */
6149a243cbSGreg Roach    public function description(): string
62c1010edaSGreg Roach    {
63bbb76c12SGreg Roach        /* I18N: Description of the “Interactive tree” module */
64bbb76c12SGreg Roach        return I18N::translate('An interactive tree, showing all the ancestors and descendants of an individual.');
658c2e8227SGreg Roach    }
668c2e8227SGreg Roach
6749a243cbSGreg Roach    /**
6849a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
6949a243cbSGreg Roach     *
7049a243cbSGreg Roach     * @return int
7149a243cbSGreg Roach     */
72cbf4b7faSGreg Roach    public function defaultTabOrder(): int
73cbf4b7faSGreg Roach    {
74fb7a0427SGreg Roach        return 7;
758c2e8227SGreg Roach    }
768c2e8227SGreg Roach
773caaa4d2SGreg Roach    /**
783caaa4d2SGreg Roach     * Generate the HTML content of this tab.
793caaa4d2SGreg Roach     *
803caaa4d2SGreg Roach     * @param Individual $individual
813caaa4d2SGreg Roach     *
823caaa4d2SGreg Roach     * @return string
833caaa4d2SGreg Roach     */
849b34404bSGreg Roach    public function getTabContent(Individual $individual): string
85c1010edaSGreg Roach    {
86225e381fSGreg Roach        $treeview = new TreeView('tvTab');
875edf1a44SGreg Roach
8865e02381SGreg Roach        [$html, $js] = $treeview->drawViewport($individual, 3);
898c2e8227SGreg Roach
905edf1a44SGreg Roach        return view('modules/interactive-tree/tab', [
91225e381fSGreg Roach            'html' => $html,
92225e381fSGreg Roach            'js'   => $js,
93225e381fSGreg Roach        ]);
948c2e8227SGreg Roach    }
958c2e8227SGreg Roach
963caaa4d2SGreg Roach    /**
973caaa4d2SGreg Roach     * Is this tab empty? If so, we don't always need to display it.
983caaa4d2SGreg Roach     *
993caaa4d2SGreg Roach     * @param Individual $individual
1003caaa4d2SGreg Roach     *
1013caaa4d2SGreg Roach     * @return bool
1023caaa4d2SGreg Roach     */
1039b34404bSGreg Roach    public function hasTabContent(Individual $individual): bool
104c1010edaSGreg Roach    {
10515d603e7SGreg Roach        return true;
1068c2e8227SGreg Roach    }
1078c2e8227SGreg Roach
1083caaa4d2SGreg Roach    /**
1093caaa4d2SGreg Roach     * A greyed out tab has no actual content, but may perhaps have
1103caaa4d2SGreg Roach     * options to create content.
1113caaa4d2SGreg Roach     *
1123caaa4d2SGreg Roach     * @param Individual $individual
1133caaa4d2SGreg Roach     *
1143caaa4d2SGreg Roach     * @return bool
1153caaa4d2SGreg Roach     */
1169b34404bSGreg Roach    public function isGrayedOut(Individual $individual): bool
117c1010edaSGreg Roach    {
1188c2e8227SGreg Roach        return false;
1198c2e8227SGreg Roach    }
1208c2e8227SGreg Roach
1213caaa4d2SGreg Roach    /**
1223caaa4d2SGreg Roach     * Can this tab load asynchronously?
1233caaa4d2SGreg Roach     *
1243caaa4d2SGreg Roach     * @return bool
1253caaa4d2SGreg Roach     */
1269b34404bSGreg Roach    public function canLoadAjax(): bool
127c1010edaSGreg Roach    {
1288c2e8227SGreg Roach        return true;
1298c2e8227SGreg Roach    }
1308c2e8227SGreg Roach
131168ff6f3Sric2016    /**
132377a2979SGreg Roach     * CSS class for the URL.
133377a2979SGreg Roach     *
134377a2979SGreg Roach     * @return string
135377a2979SGreg Roach     */
136377a2979SGreg Roach    public function chartMenuClass(): string
137377a2979SGreg Roach    {
138377a2979SGreg Roach        return 'menu-chart-tree';
139377a2979SGreg Roach    }
140377a2979SGreg Roach
141377a2979SGreg Roach    /**
1424eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1434eb71cfaSGreg Roach     *
14460bc3e3fSGreg Roach     * @param Individual $individual
14560bc3e3fSGreg Roach     *
1464eb71cfaSGreg Roach     * @return Menu|null
1474eb71cfaSGreg Roach     */
148377a2979SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
149c1010edaSGreg Roach    {
150e6562982SGreg Roach        return $this->chartMenu($individual);
151e6562982SGreg Roach    }
152e6562982SGreg Roach
153e6562982SGreg Roach    /**
154e6562982SGreg Roach     * The title for a specific instance of this chart.
155e6562982SGreg Roach     *
156e6562982SGreg Roach     * @param Individual $individual
157e6562982SGreg Roach     *
158e6562982SGreg Roach     * @return string
159e6562982SGreg Roach     */
160e6562982SGreg Roach    public function chartTitle(Individual $individual): string
161e6562982SGreg Roach    {
162e6562982SGreg Roach        /* I18N: %s is an individual’s name */
16339ca88baSGreg Roach        return I18N::translate('Interactive tree of %s', $individual->fullName());
164e6562982SGreg Roach    }
165e6562982SGreg Roach
166e6562982SGreg Roach    /**
167e6562982SGreg Roach     * The URL for this chart.
168e6562982SGreg Roach     *
169e6562982SGreg Roach     * @param Individual $individual
170*59597b37SGreg Roach     * @param mixed[]    $parameters
171e6562982SGreg Roach     *
172e6562982SGreg Roach     * @return string
173e6562982SGreg Roach     */
174e6562982SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
175e6562982SGreg Roach    {
176e6562982SGreg Roach        return route('module', [
17726684e68SGreg Roach                'module' => $this->name(),
1785edf1a44SGreg Roach                'action' => 'Chart',
179e6562982SGreg Roach                'xref'   => $individual->xref(),
1809022ab66SGreg Roach                'tree'    => $individual->tree()->name(),
181e6562982SGreg Roach            ] + $parameters);
182e6562982SGreg Roach    }
183e6562982SGreg Roach
184e6562982SGreg Roach    /**
1856ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
18676692c8bSGreg Roach     *
1876ccdf4f0SGreg Roach     * @return ResponseInterface
18876692c8bSGreg Roach     */
18957ab2231SGreg Roach    public function getChartAction(ServerRequestInterface $request): ResponseInterface
190c1010edaSGreg Roach    {
19157ab2231SGreg Roach        $tree = $request->getAttribute('tree');
1925229eadeSGreg Roach        assert($tree instanceof Tree, new InvalidArgumentException());
1935229eadeSGreg Roach
19457ab2231SGreg Roach        $user = $request->getAttribute('user');
195e106ff43SGreg Roach        $xref = $request->getQueryParams()['xref'];
196e2ae4578SGreg Roach
197e2ae4578SGreg Roach        $individual = Individual::getInstance($xref, $tree);
198bfaf8159SGreg Roach
1995edf1a44SGreg Roach        Auth::checkIndividualAccess($individual);
2009867b2f0SGreg Roach        Auth::checkComponentAccess($this, 'chart', $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
216e2ae4578SGreg Roach    /**
2176ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
218e2ae4578SGreg Roach     *
2196ccdf4f0SGreg Roach     * @return ResponseInterface
220e2ae4578SGreg Roach     */
22157ab2231SGreg Roach    public function getDetailsAction(ServerRequestInterface $request): ResponseInterface
222c1010edaSGreg Roach    {
22357ab2231SGreg Roach        $tree       = $request->getAttribute('tree');
224e106ff43SGreg Roach        $pid        = $request->getQueryParams()['pid'];
225e2ae4578SGreg Roach        $individual = Individual::getInstance($pid, $tree);
226e2ae4578SGreg Roach
2270bc54ba3SGreg Roach        if ($individual === null) {
22859f2f229SGreg Roach            throw new IndividualNotFoundException();
2290bc54ba3SGreg Roach        }
2300bc54ba3SGreg Roach
2310bc54ba3SGreg Roach        if (!$individual->canShow()) {
23259f2f229SGreg Roach            throw new IndividualAccessDeniedException();
2330bc54ba3SGreg Roach        }
2340bc54ba3SGreg Roach
235e106ff43SGreg Roach        $instance = $request->getQueryParams()['instance'];
236e2ae4578SGreg Roach        $treeview = new TreeView($instance);
237e2ae4578SGreg Roach
2386ccdf4f0SGreg Roach        return response($treeview->getDetails($individual));
2398c2e8227SGreg Roach    }
2408c2e8227SGreg Roach
2418c2e8227SGreg Roach    /**
2426ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
2433cf92ae2SGreg Roach     *
2446ccdf4f0SGreg Roach     * @return ResponseInterface
2458c2e8227SGreg Roach     */
24657ab2231SGreg Roach    public function getPersonsAction(ServerRequestInterface $request): ResponseInterface
247c1010edaSGreg Roach    {
24857ab2231SGreg Roach        $tree     = $request->getAttribute('tree');
249e106ff43SGreg Roach        $q        = $request->getQueryParams()['q'];
250e106ff43SGreg Roach        $instance = $request->getQueryParams()['instance'];
251e2ae4578SGreg Roach        $treeview = new TreeView($instance);
2528c2e8227SGreg Roach
2536ccdf4f0SGreg Roach        return response($treeview->getPersons($tree, $q));
2548c2e8227SGreg Roach    }
2558c2e8227SGreg Roach}
256