xref: /webtrees/app/Module/FamilyBookChartModule.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
1168ff6f3Sric2016<?php
23976b470SGreg Roach
3168ff6f3Sric2016/**
4168ff6f3Sric2016 * webtrees: online genealogy
5*5bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
6168ff6f3Sric2016 * This program is free software: you can redistribute it and/or modify
7168ff6f3Sric2016 * it under the terms of the GNU General Public License as published by
8168ff6f3Sric2016 * the Free Software Foundation, either version 3 of the License, or
9168ff6f3Sric2016 * (at your option) any later version.
10168ff6f3Sric2016 * This program is distributed in the hope that it will be useful,
11168ff6f3Sric2016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12168ff6f3Sric2016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13168ff6f3Sric2016 * GNU General Public License for more details.
14168ff6f3Sric2016 * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16168ff6f3Sric2016 */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
20168ff6f3Sric2016namespace Fisharebest\Webtrees\Module;
21168ff6f3Sric2016
2271378461SGreg Roachuse Fig\Http\Message\RequestMethodInterface;
23389266c0SGreg Roachuse Fisharebest\Webtrees\Auth;
24168ff6f3Sric2016use Fisharebest\Webtrees\I18N;
25168ff6f3Sric2016use Fisharebest\Webtrees\Individual;
26e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu;
2709482a55SGreg Roachuse Fisharebest\Webtrees\Registry;
28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
296ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3171378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
323976b470SGreg Roach
3371378461SGreg Roachuse function route;
34168ff6f3Sric2016
35168ff6f3Sric2016/**
36168ff6f3Sric2016 * Class FamilyBookChartModule
37168ff6f3Sric2016 */
3871378461SGreg Roachclass FamilyBookChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
39c1010edaSGreg Roach{
4049a243cbSGreg Roach    use ModuleChartTrait;
4149a243cbSGreg Roach
4272f04adfSGreg Roach    protected const ROUTE_URL = '/tree/{tree}/family-book-{book_size}-{generations}-{spouses}/{xref}';
4371378461SGreg Roach
44389266c0SGreg Roach    // Defaults
4571378461SGreg Roach    public const    DEFAULT_GENERATIONS            = '2';
4671378461SGreg Roach    public const    DEFAULT_DESCENDANT_GENERATIONS = '5';
4771378461SGreg Roach    protected const DEFAULT_PARAMETERS             = [
4871378461SGreg Roach        'book_size'   => self::DEFAULT_GENERATIONS,
4971378461SGreg Roach        'generations' => self::DEFAULT_DESCENDANT_GENERATIONS,
5071378461SGreg Roach        'spouses'     => false,
5171378461SGreg Roach    ];
52389266c0SGreg Roach
53e759aebbSGreg Roach    // Limits
54b55cbc6bSGreg Roach    protected const MINIMUM_BOOK_SIZE = 2;
55b55cbc6bSGreg Roach    protected const MAXIMUM_BOOK_SIZE = 5;
56b55cbc6bSGreg Roach
5771378461SGreg Roach    protected const MINIMUM_GENERATIONS = 2;
5871378461SGreg Roach    protected const MAXIMUM_GENERATIONS = 10;
5971378461SGreg Roach
6071378461SGreg Roach    /**
6171378461SGreg Roach     * Initialization.
6271378461SGreg Roach     *
639e18e23bSGreg Roach     * @return void
6471378461SGreg Roach     */
659e18e23bSGreg Roach    public function boot(): void
6671378461SGreg Roach    {
67158900c2SGreg Roach        Registry::routeFactory()->routeMap()
6872f04adfSGreg Roach            ->get(static::class, static::ROUTE_URL, $this)
69158900c2SGreg Roach            ->allows(RequestMethodInterface::METHOD_POST);
7071378461SGreg Roach    }
71e759aebbSGreg Roach
72168ff6f3Sric2016    /**
730cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
74168ff6f3Sric2016     *
75168ff6f3Sric2016     * @return string
76168ff6f3Sric2016     */
7749a243cbSGreg Roach    public function title(): string
78c1010edaSGreg Roach    {
79bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
80bbb76c12SGreg Roach        return I18N::translate('Family book');
81168ff6f3Sric2016    }
82168ff6f3Sric2016
83168ff6f3Sric2016    /**
84168ff6f3Sric2016     * A sentence describing what this module does.
85168ff6f3Sric2016     *
86168ff6f3Sric2016     * @return string
87168ff6f3Sric2016     */
8849a243cbSGreg Roach    public function description(): string
89c1010edaSGreg Roach    {
90bbb76c12SGreg Roach        /* I18N: Description of the “FamilyBookChart” module */
91bbb76c12SGreg Roach        return I18N::translate('A chart of an individual’s ancestors and descendants, as a family book.');
92168ff6f3Sric2016    }
93168ff6f3Sric2016
94168ff6f3Sric2016    /**
95377a2979SGreg Roach     * CSS class for the URL.
96377a2979SGreg Roach     *
97377a2979SGreg Roach     * @return string
98377a2979SGreg Roach     */
99377a2979SGreg Roach    public function chartMenuClass(): string
100377a2979SGreg Roach    {
101377a2979SGreg Roach        return 'menu-chart-familybook';
102377a2979SGreg Roach    }
103377a2979SGreg Roach
104377a2979SGreg Roach    /**
1054eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1064eb71cfaSGreg Roach     *
10760bc3e3fSGreg Roach     * @param Individual $individual
10860bc3e3fSGreg Roach     *
1094eb71cfaSGreg Roach     * @return Menu|null
1104eb71cfaSGreg Roach     */
111377a2979SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
112c1010edaSGreg Roach    {
113e6562982SGreg Roach        return $this->chartMenu($individual);
114e6562982SGreg Roach    }
115e6562982SGreg Roach
116e6562982SGreg Roach    /**
117e6562982SGreg Roach     * The title for a specific instance of this chart.
118e6562982SGreg Roach     *
119e6562982SGreg Roach     * @param Individual $individual
120e6562982SGreg Roach     *
121e6562982SGreg Roach     * @return string
122e6562982SGreg Roach     */
123e6562982SGreg Roach    public function chartTitle(Individual $individual): string
124e6562982SGreg Roach    {
125e6562982SGreg Roach        /* I18N: %s is an individual’s name */
12639ca88baSGreg Roach        return I18N::translate('Family book of %s', $individual->fullName());
127e6562982SGreg Roach    }
128e6562982SGreg Roach
129e6562982SGreg Roach    /**
13071378461SGreg Roach     * The URL for a page showing chart options.
131389266c0SGreg Roach     *
13271378461SGreg Roach     * @param Individual                                $individual
13376d39c55SGreg Roach     * @param array<bool|int|string|array<string>|null> $parameters
13471378461SGreg Roach     *
13571378461SGreg Roach     * @return string
13671378461SGreg Roach     */
13771378461SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
13871378461SGreg Roach    {
13972f04adfSGreg Roach        return route(static::class, [
14071378461SGreg Roach                'xref' => $individual->xref(),
14171378461SGreg Roach                'tree' => $individual->tree()->name(),
14271378461SGreg Roach            ] + $parameters + self::DEFAULT_PARAMETERS);
14371378461SGreg Roach    }
14471378461SGreg Roach
14571378461SGreg Roach    /**
1466ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
147389266c0SGreg Roach     *
1486ccdf4f0SGreg Roach     * @return ResponseInterface
149389266c0SGreg Roach     */
15071378461SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
151389266c0SGreg Roach    {
152b55cbc6bSGreg Roach        $tree        = Validator::attributes($request)->tree();
153b55cbc6bSGreg Roach        $user        = Validator::attributes($request)->user();
154b55cbc6bSGreg Roach        $xref        = Validator::attributes($request)->isXref()->string('xref');
155b55cbc6bSGreg Roach        $book_size   = Validator::attributes($request)->isBetween(self::MINIMUM_BOOK_SIZE, self::MAXIMUM_BOOK_SIZE)->integer('book_size');
156b55cbc6bSGreg Roach        $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations');
157b55cbc6bSGreg Roach        $spouses     = Validator::attributes($request)->boolean('spouses');
158b55cbc6bSGreg Roach        $ajax        = Validator::queryParams($request)->boolean('ajax', false);
159389266c0SGreg Roach
16071378461SGreg Roach        // Convert POST requests into GET requests for pretty URLs.
16171378461SGreg Roach        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
16272f04adfSGreg Roach            return redirect(route(static::class, [
1634ea62551SGreg Roach                'tree'        => $tree->name(),
164158900c2SGreg Roach                'xref'        => Validator::parsedBody($request)->isXref()->string('xref'),
165158900c2SGreg Roach                'book_size'   => Validator::parsedBody($request)->isBetween(self::MINIMUM_BOOK_SIZE, self::MAXIMUM_BOOK_SIZE)->integer('book_size'),
166158900c2SGreg Roach                'generations' => Validator::parsedBody($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'),
167158900c2SGreg Roach                'spouses'     => Validator::parsedBody($request)->boolean('spouses'),
16871378461SGreg Roach            ]));
16971378461SGreg Roach        }
17071378461SGreg Roach
171ef483801SGreg Roach        Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
172389266c0SGreg Roach
173b55cbc6bSGreg Roach        $individual  = Registry::individualFactory()->make($xref, $tree);
174b55cbc6bSGreg Roach        $individual  = Auth::checkIndividualAccess($individual, false, true);
175389266c0SGreg Roach
176b55cbc6bSGreg Roach        if ($ajax) {
17771378461SGreg Roach            $this->layout = 'layouts/ajax';
17871378461SGreg Roach
17971378461SGreg Roach            return $this->viewResponse('modules/family-book-chart/chart', [
18071378461SGreg Roach                'individual'  => $individual,
18171378461SGreg Roach                'generations' => $generations,
18271378461SGreg Roach                'book_size'   => $book_size,
18371378461SGreg Roach                'spouses'     => $spouses,
18471378461SGreg Roach            ]);
185389266c0SGreg Roach        }
186389266c0SGreg Roach
187389266c0SGreg Roach        $ajax_url = $this->chartUrl($individual, [
1889b5537c3SGreg Roach            'ajax'        => true,
189389266c0SGreg Roach            'book_size'   => $book_size,
190389266c0SGreg Roach            'generations' => $generations,
19171378461SGreg Roach            'spouses'     => $spouses,
192389266c0SGreg Roach        ]);
193389266c0SGreg Roach
1949b5537c3SGreg Roach        return $this->viewResponse('modules/family-book-chart/page', [
195389266c0SGreg Roach            'ajax_url'            => $ajax_url,
196389266c0SGreg Roach            'book_size'           => $book_size,
197389266c0SGreg Roach            'generations'         => $generations,
198389266c0SGreg Roach            'individual'          => $individual,
199b55cbc6bSGreg Roach            'maximum_book_size'   => self::MAXIMUM_BOOK_SIZE,
200b55cbc6bSGreg Roach            'minimum_book_size'   => self::MINIMUM_BOOK_SIZE,
201e759aebbSGreg Roach            'maximum_generations' => self::MAXIMUM_GENERATIONS,
202e759aebbSGreg Roach            'minimum_generations' => self::MINIMUM_GENERATIONS,
20371378461SGreg Roach            'module'              => $this->name(),
20471378461SGreg Roach            'spouses'             => $spouses,
205389266c0SGreg Roach            'title'               => $this->chartTitle($individual),
206ef5d23f1SGreg Roach            'tree'                => $tree,
207389266c0SGreg Roach        ]);
208389266c0SGreg Roach    }
209168ff6f3Sric2016}
210