xref: /webtrees/app/Module/CompactTreeChartModule.php (revision d993d560f991544b8dc49e013a8027c6fc967956)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Auth;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Individual;
23use Fisharebest\Webtrees\Menu;
24use Fisharebest\Webtrees\Services\ChartService;
25use Fisharebest\Webtrees\Tree;
26use Symfony\Component\HttpFoundation\Request;
27use Symfony\Component\HttpFoundation\Response;
28
29/**
30 * Class CompactTreeChartModule
31 */
32class CompactTreeChartModule extends AbstractModule implements ModuleInterface, ModuleChartInterface
33{
34    use ModuleChartTrait;
35
36    /**
37     * How should this module be labelled on tabs, menus, etc.?
38     *
39     * @return string
40     */
41    public function title(): string
42    {
43        /* I18N: Name of a module/chart */
44        return I18N::translate('Compact tree');
45    }
46
47    /**
48     * A sentence describing what this module does.
49     *
50     * @return string
51     */
52    public function description(): string
53    {
54        /* I18N: Description of the “CompactTreeChart” module */
55        return I18N::translate('A chart of an individual’s ancestors, as a compact tree.');
56    }
57
58    /**
59     * CSS class for the URL.
60     *
61     * @return string
62     */
63    public function chartMenuClass(): string
64    {
65        return 'menu-chart-compact';
66    }
67
68    /**
69     * Return a menu item for this chart - for use in individual boxes.
70     *
71     * @param Individual $individual
72     *
73     * @return Menu|null
74     */
75    public function chartBoxMenu(Individual $individual): ?Menu
76    {
77        return $this->chartMenu($individual);
78    }
79
80    /**
81     * The title for a specific instance of this chart.
82     *
83     * @param Individual $individual
84     *
85     * @return string
86     */
87    public function chartTitle(Individual $individual): string
88    {
89        /* I18N: %s is an individual’s name */
90        return I18N::translate('Compact tree of %s', $individual->getFullName());
91    }
92
93    /**
94     * A form to request the chart parameters.
95     *
96     * @param Request      $request
97     * @param Tree         $tree
98     * @param ChartService $chart_service
99     *
100     * @return Response
101     */
102    public function getChartAction(Request $request, Tree $tree, ChartService $chart_service): Response
103    {
104        $ajax       = (bool) $request->get('ajax');
105        $xref       = $request->get('xref', '');
106        $individual = Individual::getInstance($xref, $tree);
107
108        Auth::checkIndividualAccess($individual);
109
110        if ($ajax) {
111            return $this->chartCompact($individual, $chart_service);
112        }
113
114        $ajax_url = $this->chartUrl($individual, [
115            'ajax' => true,
116        ]);
117
118        return $this->viewResponse('modules/compact-chart/page', [
119            'ajax_url'    => $ajax_url,
120            'individual'  => $individual,
121            'module_name' => $this->name(),
122            'title'       => $this->chartTitle($individual),
123        ]);
124    }
125
126    /**
127     * @param Individual   $individual
128     * @param ChartService $chart_service
129     *
130     * @return Response
131     */
132    protected function chartCompact(Individual $individual, ChartService $chart_service): Response
133    {
134        $ancestors = $chart_service->sosaStradonitzAncestors($individual, 5);
135
136        $html = view('modules/compact-chart/chart', [
137            'ancestors' => $ancestors,
138        ]);
139
140        return new Response($html);
141    }
142}
143