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