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