xref: /webtrees/app/Module/FamilyBookChartModule.php (revision cacefda1879a630cb123d4cd92f434ddbcc1f10e)
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 Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30use Psr\Http\Server\RequestHandlerInterface;
31
32use function app;
33use function assert;
34use function max;
35use function min;
36use function route;
37
38/**
39 * Class FamilyBookChartModule
40 */
41class FamilyBookChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
42{
43    use ModuleChartTrait;
44
45    private const ROUTE_NAME = 'family-book-chart';
46    private const ROUTE_URL  = '/tree/{tree}/family-book-{book_size}-{generations}-{spouses}/{xref}';
47
48    // Defaults
49    public const    DEFAULT_GENERATIONS            = '2';
50    public const    DEFAULT_DESCENDANT_GENERATIONS = '5';
51    public const    DEFAULT_MAXIMUM_GENERATIONS    = '9';
52    protected const DEFAULT_PARAMETERS             = [
53        'book_size'   => self::DEFAULT_GENERATIONS,
54        'generations' => self::DEFAULT_DESCENDANT_GENERATIONS,
55        'spouses'     => false,
56    ];
57
58    // Limits
59    protected const MINIMUM_GENERATIONS = 2;
60    protected const MAXIMUM_GENERATIONS = 10;
61
62    /**
63     * Initialization.
64     *
65     * @return void
66     */
67    public function boot(): void
68    {
69        $router_container = app(RouterContainer::class);
70        assert($router_container instanceof RouterContainer);
71
72        $router_container->getMap()
73            ->get(self::ROUTE_NAME, self::ROUTE_URL, self::class)
74            ->allows(RequestMethodInterface::METHOD_POST)
75            ->tokens([
76                'book_size'   => '\d+',
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('Family book');
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 “FamilyBookChart” module */
101        return I18N::translate('A chart of an individual’s ancestors and descendants, as a family book.');
102    }
103
104    /**
105     * CSS class for the URL.
106     *
107     * @return string
108     */
109    public function chartMenuClass(): string
110    {
111        return 'menu-chart-familybook';
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('Family book 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        $user        = $request->getAttribute('user');
164        $xref        = $request->getAttribute('xref');
165        $book_size   = (int) $request->getAttribute('book_size');
166        $generations = (int) $request->getAttribute('generations');
167        $spouses     = (bool) $request->getAttribute('spouses');
168        $ajax        = $request->getQueryParams()['ajax'] ?? '';
169        $individual  = Individual::getInstance($xref, $tree);
170
171        // Convert POST requests into GET requests for pretty URLs.
172        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
173            return redirect(route(self::ROUTE_NAME, [
174                'tree'        => $request->getAttribute('tree')->name(),
175                'xref'        => $request->getParsedBody()['xref'],
176                'book_size'   => $request->getParsedBody()['book_size'],
177                'generations' => $request->getParsedBody()['generations'],
178                'spouses'     => $request->getParsedBody()['spouses'] ?? false,
179            ]));
180        }
181
182        Auth::checkIndividualAccess($individual);
183        Auth::checkComponentAccess($this, 'chart', $tree, $user);
184
185        $generations = min($generations, self::MAXIMUM_GENERATIONS);
186        $generations = max($generations, self::MINIMUM_GENERATIONS);
187
188        // Generations of ancestors/descendants in each mini-tree.
189        $book_size = min($book_size, 5);
190        $book_size = max($book_size, 2);
191
192        if ($ajax === '1') {
193            $this->layout = 'layouts/ajax';
194
195            return $this->viewResponse('modules/family-book-chart/chart', [
196                'individual'  => $individual,
197                'generations' => $generations,
198                'book_size'   => $book_size,
199                'spouses'     => $spouses,
200            ]);
201        }
202
203        $ajax_url = $this->chartUrl($individual, [
204            'ajax'        => true,
205            'book_size'   => $book_size,
206            'generations' => $generations,
207            'spouses'     => $spouses,
208        ]);
209
210        return $this->viewResponse('modules/family-book-chart/page', [
211            'ajax_url'            => $ajax_url,
212            'book_size'           => $book_size,
213            'generations'         => $generations,
214            'individual'          => $individual,
215            'maximum_generations' => self::MAXIMUM_GENERATIONS,
216            'minimum_generations' => self::MINIMUM_GENERATIONS,
217            'module'              => $this->name(),
218            'spouses'             => $spouses,
219            'title'               => $this->chartTitle($individual),
220        ]);
221    }
222}
223