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