xref: /webtrees/app/Module/HourglassChartModule.php (revision b8fc901f205cd6af65496b916bf63547a3065a2f)
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 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Aura\Router\RouterContainer;
23use Fig\Http\Message\RequestMethodInterface;
24use Fisharebest\Webtrees\Auth;
25use Fisharebest\Webtrees\Family;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Individual;
28use Fisharebest\Webtrees\Menu;
29use Fisharebest\Webtrees\Tree;
30use Illuminate\Support\Collection;
31use Psr\Http\Message\ResponseInterface;
32use Psr\Http\Message\ServerRequestInterface;
33use Psr\Http\Server\RequestHandlerInterface;
34
35use function app;
36use function assert;
37use function is_string;
38use function response;
39use function view;
40
41/**
42 * Class HourglassChartModule
43 */
44class HourglassChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
45{
46    use ModuleChartTrait;
47
48    private const ROUTE_NAME = 'hourglass-chart';
49    private const ROUTE_URL  = '/tree/{tree}/hourglass-{generations}-{spouses}/{xref}';
50
51    // Defaults
52    private const   DEFAULT_GENERATIONS = '3';
53    private const   DEFAULT_SPOUSES     = false;
54    protected const DEFAULT_PARAMETERS  = [
55        'generations' => self::DEFAULT_GENERATIONS,
56        'spouses'     => self::DEFAULT_SPOUSES,
57    ];
58
59    // Limits
60    protected const MINIMUM_GENERATIONS = 2;
61    protected const MAXIMUM_GENERATIONS = 10;
62
63    /**
64     * Initialization.
65     *
66     * @return void
67     */
68    public function boot(): void
69    {
70        $router_container = app(RouterContainer::class);
71        assert($router_container instanceof RouterContainer);
72
73        $router_container->getMap()
74            ->get(self::ROUTE_NAME, self::ROUTE_URL, $this)
75            ->allows(RequestMethodInterface::METHOD_POST)
76            ->tokens([
77                'generations' => '\d+',
78                'spouses'     => '1?',
79            ]);
80    }
81
82    /**
83     * How should this module be identified in the control panel, etc.?
84     *
85     * @return string
86     */
87    public function title(): string
88    {
89        /* I18N: Name of a module/chart */
90        return I18N::translate('Hourglass chart');
91    }
92
93    /**
94     * A sentence describing what this module does.
95     *
96     * @return string
97     */
98    public function description(): string
99    {
100        /* I18N: Description of the “HourglassChart” module */
101        return I18N::translate('An hourglass chart of an individual’s ancestors and descendants.');
102    }
103
104    /**
105     * CSS class for the URL.
106     *
107     * @return string
108     */
109    public function chartMenuClass(): string
110    {
111        return 'menu-chart-hourglass';
112    }
113
114    /**
115     * Return a menu item for this chart - for use in individual boxes.
116     *
117     * @param Individual $individual
118     *
119     * @return Menu|null
120     */
121    public function chartBoxMenu(Individual $individual): ?Menu
122    {
123        return $this->chartMenu($individual);
124    }
125
126    /**
127     * The title for a specific instance of this chart.
128     *
129     * @param Individual $individual
130     *
131     * @return string
132     */
133    public function chartTitle(Individual $individual): string
134    {
135        /* I18N: %s is an individual’s name */
136        return I18N::translate('Hourglass chart of %s', $individual->fullName());
137    }
138
139    /**
140     * The URL for a page showing chart options.
141     *
142     * @param Individual $individual
143     * @param mixed[]    $parameters
144     *
145     * @return string
146     */
147    public function chartUrl(Individual $individual, array $parameters = []): string
148    {
149        return route(self::ROUTE_NAME, [
150                'xref' => $individual->xref(),
151                'tree' => $individual->tree()->name(),
152            ] + $parameters + self::DEFAULT_PARAMETERS);
153    }
154
155    /**
156     * @param ServerRequestInterface $request
157     *
158     * @return ResponseInterface
159     */
160    public function handle(ServerRequestInterface $request): ResponseInterface
161    {
162        $tree = $request->getAttribute('tree');
163        assert($tree instanceof Tree);
164
165        $xref = $request->getAttribute('xref');
166        assert(is_string($xref));
167
168        $individual = Individual::getInstance($xref, $tree);
169        $individual = Auth::checkIndividualAccess($individual);
170
171        $user        = $request->getAttribute('user');
172        $generations = (int) $request->getAttribute('generations');
173        $spouses     = (bool) $request->getAttribute('spouses');
174        $ajax        = $request->getQueryParams()['ajax'] ?? '';
175
176        // Convert POST requests into GET requests for pretty URLs.
177        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
178            return redirect(route(self::ROUTE_NAME, [
179                'tree'        => $tree->name(),
180                'xref'        => $request->getParsedBody()['xref'],
181                'generations' => $request->getParsedBody()['generations'],
182                'spouses'     => $request->getParsedBody()['spouses'] ?? false,
183            ]));
184        }
185
186        Auth::checkComponentAccess($this, 'chart', $tree, $user);
187
188        $generations = min($generations, self::MAXIMUM_GENERATIONS);
189        $generations = max($generations, self::MINIMUM_GENERATIONS);
190
191        if ($ajax === '1') {
192            $this->layout = 'layouts/ajax';
193
194            return $this->viewResponse('modules/hourglass-chart/chart', [
195                'generations' => $generations,
196                'individual'  => $individual,
197                'spouses'     => $spouses,
198            ]);
199        }
200
201        $ajax_url = $this->chartUrl($individual, [
202            'ajax'        => true,
203            'generations' => $generations,
204            'spouses'     => $spouses,
205        ]);
206
207        return $this->viewResponse('modules/hourglass-chart/page', [
208            'ajax_url'            => $ajax_url,
209            'generations'         => $generations,
210            'individual'          => $individual,
211            'maximum_generations' => self::MAXIMUM_GENERATIONS,
212            'minimum_generations' => self::MINIMUM_GENERATIONS,
213            'module'              => $this->name(),
214            'spouses'             => $spouses,
215            'title'               => $this->chartTitle($individual),
216            'tree'                => $tree,
217        ]);
218    }
219
220    /**
221     * Generate an extension to the chart
222     *
223     * @param ServerRequestInterface $request
224     *
225     * @return ResponseInterface
226     */
227    public function getAncestorsAction(ServerRequestInterface $request): ResponseInterface
228    {
229        $tree = $request->getAttribute('tree');
230        assert($tree instanceof Tree);
231
232        $xref = $request->getQueryParams()['xref'] ?? '';
233
234        $family = Family::getInstance($xref, $tree);
235        $family = Auth::checkFamilyAccess($family);
236
237        return response(view('modules/hourglass-chart/parents', [
238            'family'      => $family,
239            'generations' => 1,
240        ]));
241    }
242
243    /**
244     * Generate an extension to the chart
245     *
246     * @param ServerRequestInterface $request
247     *
248     * @return ResponseInterface
249     */
250    public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface
251    {
252        $tree = $request->getAttribute('tree');
253        assert($tree instanceof Tree);
254
255        $xref = $request->getQueryParams()['xref'] ?? '';
256
257        $spouses    = (bool) ($request->getQueryParams()['spouses'] ?? false);
258        $individual = Individual::getInstance($xref, $tree);
259        $individual = Auth::checkIndividualAccess($individual);
260
261        $children = $individual->spouseFamilies()->map(static function (Family $family): Collection {
262            return $family->children();
263        })->flatten();
264
265        return response(view('modules/hourglass-chart/children', [
266            'children'    => $children,
267            'generations' => 1,
268            'spouses'     => $spouses,
269        ]));
270    }
271}
272