xref: /webtrees/app/Module/FamilyBookChartModule.php (revision 66c7498f9d559689b159190509749c1997dc5403)
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 max;
36use function min;
37use function route;
38
39/**
40 * Class FamilyBookChartModule
41 */
42class FamilyBookChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
43{
44    use ModuleChartTrait;
45
46    private const ROUTE_NAME = 'family-book-chart';
47    private 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(self::ROUTE_NAME, self::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(self::ROUTE_NAME, [
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        $user        = $request->getAttribute('user');
167        $xref        = $request->getAttribute('xref');
168        $book_size   = (int) $request->getAttribute('book_size');
169        $generations = (int) $request->getAttribute('generations');
170        $spouses     = (bool) $request->getAttribute('spouses');
171        $ajax        = $request->getQueryParams()['ajax'] ?? '';
172        $individual  = Individual::getInstance($xref, $tree);
173
174        // Convert POST requests into GET requests for pretty URLs.
175        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
176            return redirect(route(self::ROUTE_NAME, [
177                'tree'        => $tree->name(),
178                'xref'        => $request->getParsedBody()['xref'],
179                'book_size'   => $request->getParsedBody()['book_size'],
180                'generations' => $request->getParsedBody()['generations'],
181                'spouses'     => $request->getParsedBody()['spouses'] ?? false,
182            ]));
183        }
184
185        Auth::checkIndividualAccess($individual);
186        Auth::checkComponentAccess($this, 'chart', $tree, $user);
187
188        $generations = min($generations, self::MAXIMUM_GENERATIONS);
189        $generations = max($generations, self::MINIMUM_GENERATIONS);
190
191        // Generations of ancestors/descendants in each mini-tree.
192        $book_size = min($book_size, 5);
193        $book_size = max($book_size, 2);
194
195        if ($ajax === '1') {
196            $this->layout = 'layouts/ajax';
197
198            return $this->viewResponse('modules/family-book-chart/chart', [
199                'individual'  => $individual,
200                'generations' => $generations,
201                'book_size'   => $book_size,
202                'spouses'     => $spouses,
203            ]);
204        }
205
206        $ajax_url = $this->chartUrl($individual, [
207            'ajax'        => true,
208            'book_size'   => $book_size,
209            'generations' => $generations,
210            'spouses'     => $spouses,
211        ]);
212
213        return $this->viewResponse('modules/family-book-chart/page', [
214            'ajax_url'            => $ajax_url,
215            'book_size'           => $book_size,
216            'generations'         => $generations,
217            'individual'          => $individual,
218            'maximum_generations' => self::MAXIMUM_GENERATIONS,
219            'minimum_generations' => self::MINIMUM_GENERATIONS,
220            'module'              => $this->name(),
221            'spouses'             => $spouses,
222            'title'               => $this->chartTitle($individual),
223            'tree'                => $tree,
224        ]);
225    }
226}
227