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