xref: /webtrees/app/Module/FamilyBookChartModule.php (revision b8c8582ab2a169215e9010c84703b6f7f9c8027d)
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\I18N;
26use Fisharebest\Webtrees\Individual;
27use Fisharebest\Webtrees\Menu;
28use Fisharebest\Webtrees\Tree;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function app;
34use function assert;
35use function is_string;
36use function max;
37use function min;
38use function route;
39
40/**
41 * Class FamilyBookChartModule
42 */
43class FamilyBookChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
44{
45    use ModuleChartTrait;
46
47    protected const ROUTE_URL  = '/tree/{tree}/family-book-{book_size}-{generations}-{spouses}/{xref}';
48
49    // Defaults
50    public const    DEFAULT_GENERATIONS            = '2';
51    public const    DEFAULT_DESCENDANT_GENERATIONS = '5';
52    public const    DEFAULT_MAXIMUM_GENERATIONS    = '9';
53    protected const DEFAULT_PARAMETERS             = [
54        'book_size'   => self::DEFAULT_GENERATIONS,
55        'generations' => self::DEFAULT_DESCENDANT_GENERATIONS,
56        'spouses'     => false,
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(static::class, static::ROUTE_URL, $this)
75            ->allows(RequestMethodInterface::METHOD_POST)
76            ->tokens([
77                'book_size'   => '\d+',
78                'generations' => '\d+',
79                'spouses'     => '1?',
80            ]);
81    }
82
83    /**
84     * How should this module be identified in the control panel, etc.?
85     *
86     * @return string
87     */
88    public function title(): string
89    {
90        /* I18N: Name of a module/chart */
91        return I18N::translate('Family book');
92    }
93
94    /**
95     * A sentence describing what this module does.
96     *
97     * @return string
98     */
99    public function description(): string
100    {
101        /* I18N: Description of the “FamilyBookChart” module */
102        return I18N::translate('A chart of an individual’s ancestors and descendants, as a family book.');
103    }
104
105    /**
106     * CSS class for the URL.
107     *
108     * @return string
109     */
110    public function chartMenuClass(): string
111    {
112        return 'menu-chart-familybook';
113    }
114
115    /**
116     * Return a menu item for this chart - for use in individual boxes.
117     *
118     * @param Individual $individual
119     *
120     * @return Menu|null
121     */
122    public function chartBoxMenu(Individual $individual): ?Menu
123    {
124        return $this->chartMenu($individual);
125    }
126
127    /**
128     * The title for a specific instance of this chart.
129     *
130     * @param Individual $individual
131     *
132     * @return string
133     */
134    public function chartTitle(Individual $individual): string
135    {
136        /* I18N: %s is an individual’s name */
137        return I18N::translate('Family book of %s', $individual->fullName());
138    }
139
140    /**
141     * The URL for a page showing chart options.
142     *
143     * @param Individual $individual
144     * @param mixed[]    $parameters
145     *
146     * @return string
147     */
148    public function chartUrl(Individual $individual, array $parameters = []): string
149    {
150        return route(static::class, [
151                'xref' => $individual->xref(),
152                'tree' => $individual->tree()->name(),
153            ] + $parameters + self::DEFAULT_PARAMETERS);
154    }
155
156    /**
157     * @param ServerRequestInterface $request
158     *
159     * @return ResponseInterface
160     */
161    public function handle(ServerRequestInterface $request): ResponseInterface
162    {
163        $tree = $request->getAttribute('tree');
164        assert($tree instanceof Tree);
165
166        $xref = $request->getAttribute('xref');
167        assert(is_string($xref));
168
169        $individual = Individual::getInstance($xref, $tree);
170        $individual = Auth::checkIndividualAccess($individual, false, true);
171
172        $user        = $request->getAttribute('user');
173        $book_size   = (int) $request->getAttribute('book_size');
174        $generations = (int) $request->getAttribute('generations');
175        $spouses     = (bool) $request->getAttribute('spouses');
176        $ajax        = $request->getQueryParams()['ajax'] ?? '';
177
178        // Convert POST requests into GET requests for pretty URLs.
179        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
180            $params = (array) $request->getParsedBody();
181
182            return redirect(route(static::class, [
183                'tree'        => $tree->name(),
184                'xref'        => $params['xref'],
185                'book_size'   => $params['book_size'],
186                'generations' => $params['generations'],
187                'spouses'     => $params['spouses'] ?? false,
188            ]));
189        }
190
191        Auth::checkComponentAccess($this, 'chart', $tree, $user);
192
193        $generations = min($generations, self::MAXIMUM_GENERATIONS);
194        $generations = max($generations, self::MINIMUM_GENERATIONS);
195
196        // Generations of ancestors/descendants in each mini-tree.
197        $book_size = min($book_size, 5);
198        $book_size = max($book_size, 2);
199
200        if ($ajax === '1') {
201            $this->layout = 'layouts/ajax';
202
203            return $this->viewResponse('modules/family-book-chart/chart', [
204                'individual'  => $individual,
205                'generations' => $generations,
206                'book_size'   => $book_size,
207                'spouses'     => $spouses,
208            ]);
209        }
210
211        $ajax_url = $this->chartUrl($individual, [
212            'ajax'        => true,
213            'book_size'   => $book_size,
214            'generations' => $generations,
215            'spouses'     => $spouses,
216        ]);
217
218        return $this->viewResponse('modules/family-book-chart/page', [
219            'ajax_url'            => $ajax_url,
220            'book_size'           => $book_size,
221            'generations'         => $generations,
222            'individual'          => $individual,
223            'maximum_generations' => self::MAXIMUM_GENERATIONS,
224            'minimum_generations' => self::MINIMUM_GENERATIONS,
225            'module'              => $this->name(),
226            'spouses'             => $spouses,
227            'title'               => $this->chartTitle($individual),
228            'tree'                => $tree,
229        ]);
230    }
231}
232