xref: /webtrees/app/Module/CompactTreeChartModule.php (revision e759aebbec359cf7148227a725fea81907659ae8)
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\Contracts\UserInterface;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Individual;
24use Fisharebest\Webtrees\Menu;
25use Fisharebest\Webtrees\Services\ChartService;
26use Fisharebest\Webtrees\Tree;
27use Symfony\Component\HttpFoundation\Request;
28use Symfony\Component\HttpFoundation\Response;
29
30/**
31 * Class CompactTreeChartModule
32 */
33class CompactTreeChartModule extends AbstractModule implements ModuleChartInterface
34{
35    use ModuleChartTrait;
36
37    /**
38     * How should this module be labelled on tabs, menus, etc.?
39     *
40     * @return string
41     */
42    public function title(): string
43    {
44        /* I18N: Name of a module/chart */
45        return I18N::translate('Compact tree');
46    }
47
48    /**
49     * A sentence describing what this module does.
50     *
51     * @return string
52     */
53    public function description(): string
54    {
55        /* I18N: Description of the “CompactTreeChart” module */
56        return I18N::translate('A chart of an individual’s ancestors, as a compact tree.');
57    }
58
59    /**
60     * CSS class for the URL.
61     *
62     * @return string
63     */
64    public function chartMenuClass(): string
65    {
66        return 'menu-chart-compact';
67    }
68
69    /**
70     * Return a menu item for this chart - for use in individual boxes.
71     *
72     * @param Individual $individual
73     *
74     * @return Menu|null
75     */
76    public function chartBoxMenu(Individual $individual): ?Menu
77    {
78        return $this->chartMenu($individual);
79    }
80
81    /**
82     * The title for a specific instance of this chart.
83     *
84     * @param Individual $individual
85     *
86     * @return string
87     */
88    public function chartTitle(Individual $individual): string
89    {
90        /* I18N: %s is an individual’s name */
91        return I18N::translate('Compact tree of %s', $individual->getFullName());
92    }
93
94    /**
95     * A form to request the chart parameters.
96     *
97     * @param Request       $request
98     * @param Tree          $tree
99     * @param UserInterface $user
100     * @param ChartService  $chart_service
101     *
102     * @return Response
103     */
104    public function getChartAction(Request $request, Tree $tree, UserInterface $user, ChartService $chart_service): Response
105    {
106        $ajax       = (bool) $request->get('ajax');
107        $xref       = $request->get('xref', '');
108        $individual = Individual::getInstance($xref, $tree);
109
110        Auth::checkIndividualAccess($individual);
111        Auth::checkComponentAccess($this, 'chart', $tree, $user);
112
113        if ($ajax) {
114            return $this->chartCompact($individual, $chart_service);
115        }
116
117        $ajax_url = $this->chartUrl($individual, [
118            'ajax' => true,
119        ]);
120
121        return $this->viewResponse('modules/compact-chart/page', [
122            'ajax_url'    => $ajax_url,
123            'individual'  => $individual,
124            'module_name' => $this->name(),
125            'title'       => $this->chartTitle($individual),
126        ]);
127    }
128
129    /**
130     * @param Individual   $individual
131     * @param ChartService $chart_service
132     *
133     * @return Response
134     */
135    protected function chartCompact(Individual $individual, ChartService $chart_service): Response
136    {
137        $ancestors = $chart_service->sosaStradonitzAncestors($individual, 5);
138
139        $html = view('modules/compact-chart/chart', [
140            'ancestors' => $ancestors,
141            'module'    => $this,
142        ]);
143
144        return new Response($html);
145    }
146}
147