xref: /webtrees/app/Module/FamilyBookChartModule.php (revision 9e18e23b968678b192e5541acd3252e4978d69c3)
1168ff6f3Sric2016<?php
23976b470SGreg Roach
3168ff6f3Sric2016/**
4168ff6f3Sric2016 * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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
15168ff6f3Sric2016 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16168ff6f3Sric2016 */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
20168ff6f3Sric2016namespace Fisharebest\Webtrees\Module;
21168ff6f3Sric2016
2271378461SGreg Roachuse Aura\Router\RouterContainer;
2371378461SGreg Roachuse Fig\Http\Message\RequestMethodInterface;
24389266c0SGreg Roachuse Fisharebest\Webtrees\Auth;
25168ff6f3Sric2016use Fisharebest\Webtrees\I18N;
26168ff6f3Sric2016use Fisharebest\Webtrees\Individual;
27e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu;
286ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
296ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3071378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
313976b470SGreg Roach
32*9e18e23bSGreg Roachuse function app;
33*9e18e23bSGreg Roachuse function assert;
3471378461SGreg Roachuse function max;
3571378461SGreg Roachuse function min;
3671378461SGreg Roachuse function route;
37168ff6f3Sric2016
38168ff6f3Sric2016/**
39168ff6f3Sric2016 * Class FamilyBookChartModule
40168ff6f3Sric2016 */
4171378461SGreg Roachclass FamilyBookChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
42c1010edaSGreg Roach{
4349a243cbSGreg Roach    use ModuleChartTrait;
4449a243cbSGreg Roach
4571378461SGreg Roach    private const ROUTE_NAME = 'family-book-chart';
4671378461SGreg Roach    private const ROUTE_URL  = '/tree/{tree}/family-book-{book_size}-{generations}-{spouses}/{xref}';
4771378461SGreg Roach
48389266c0SGreg Roach    // Defaults
4971378461SGreg Roach    public const    DEFAULT_GENERATIONS            = '2';
5071378461SGreg Roach    public const    DEFAULT_DESCENDANT_GENERATIONS = '5';
5171378461SGreg Roach    public const    DEFAULT_MAXIMUM_GENERATIONS    = '9';
5271378461SGreg Roach    protected const DEFAULT_PARAMETERS             = [
5371378461SGreg Roach        'book_size'   => self::DEFAULT_GENERATIONS,
5471378461SGreg Roach        'generations' => self::DEFAULT_DESCENDANT_GENERATIONS,
5571378461SGreg Roach        'spouses'     => false,
5671378461SGreg Roach    ];
57389266c0SGreg Roach
58e759aebbSGreg Roach    // Limits
5971378461SGreg Roach    protected const MINIMUM_GENERATIONS = 2;
6071378461SGreg Roach    protected const MAXIMUM_GENERATIONS = 10;
6171378461SGreg Roach
6271378461SGreg Roach    /**
6371378461SGreg Roach     * Initialization.
6471378461SGreg Roach     *
65*9e18e23bSGreg Roach     * @return void
6671378461SGreg Roach     */
67*9e18e23bSGreg Roach    public function boot(): void
6871378461SGreg Roach    {
69*9e18e23bSGreg Roach        $router_container = app(RouterContainer::class);
70*9e18e23bSGreg Roach        assert($router_container instanceof RouterContainer);
71*9e18e23bSGreg Roach
7271378461SGreg Roach        $router_container->getMap()
7371378461SGreg Roach            ->get(self::ROUTE_NAME, self::ROUTE_URL, self::class)
7471378461SGreg Roach            ->allows(RequestMethodInterface::METHOD_POST)
7571378461SGreg Roach            ->tokens([
7671378461SGreg Roach                'book_size'   => '\d+',
7771378461SGreg Roach                'generations' => '\d+',
7871378461SGreg Roach                'spouses'     => '1?',
7971378461SGreg Roach            ]);
8071378461SGreg Roach    }
81e759aebbSGreg Roach
82168ff6f3Sric2016    /**
830cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
84168ff6f3Sric2016     *
85168ff6f3Sric2016     * @return string
86168ff6f3Sric2016     */
8749a243cbSGreg Roach    public function title(): string
88c1010edaSGreg Roach    {
89bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
90bbb76c12SGreg Roach        return I18N::translate('Family book');
91168ff6f3Sric2016    }
92168ff6f3Sric2016
93168ff6f3Sric2016    /**
94168ff6f3Sric2016     * A sentence describing what this module does.
95168ff6f3Sric2016     *
96168ff6f3Sric2016     * @return string
97168ff6f3Sric2016     */
9849a243cbSGreg Roach    public function description(): string
99c1010edaSGreg Roach    {
100bbb76c12SGreg Roach        /* I18N: Description of the “FamilyBookChart” module */
101bbb76c12SGreg Roach        return I18N::translate('A chart of an individual’s ancestors and descendants, as a family book.');
102168ff6f3Sric2016    }
103168ff6f3Sric2016
104168ff6f3Sric2016    /**
105377a2979SGreg Roach     * CSS class for the URL.
106377a2979SGreg Roach     *
107377a2979SGreg Roach     * @return string
108377a2979SGreg Roach     */
109377a2979SGreg Roach    public function chartMenuClass(): string
110377a2979SGreg Roach    {
111377a2979SGreg Roach        return 'menu-chart-familybook';
112377a2979SGreg Roach    }
113377a2979SGreg Roach
114377a2979SGreg Roach    /**
1154eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1164eb71cfaSGreg Roach     *
11760bc3e3fSGreg Roach     * @param Individual $individual
11860bc3e3fSGreg Roach     *
1194eb71cfaSGreg Roach     * @return Menu|null
1204eb71cfaSGreg Roach     */
121377a2979SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
122c1010edaSGreg Roach    {
123e6562982SGreg Roach        return $this->chartMenu($individual);
124e6562982SGreg Roach    }
125e6562982SGreg Roach
126e6562982SGreg Roach    /**
127e6562982SGreg Roach     * The title for a specific instance of this chart.
128e6562982SGreg Roach     *
129e6562982SGreg Roach     * @param Individual $individual
130e6562982SGreg Roach     *
131e6562982SGreg Roach     * @return string
132e6562982SGreg Roach     */
133e6562982SGreg Roach    public function chartTitle(Individual $individual): string
134e6562982SGreg Roach    {
135e6562982SGreg Roach        /* I18N: %s is an individual’s name */
13639ca88baSGreg Roach        return I18N::translate('Family book of %s', $individual->fullName());
137e6562982SGreg Roach    }
138e6562982SGreg Roach
139e6562982SGreg Roach    /**
14071378461SGreg Roach     * The URL for a page showing chart options.
141389266c0SGreg Roach     *
14271378461SGreg Roach     * @param Individual $individual
14359597b37SGreg Roach     * @param mixed[]    $parameters
14471378461SGreg Roach     *
14571378461SGreg Roach     * @return string
14671378461SGreg Roach     */
14771378461SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
14871378461SGreg Roach    {
14971378461SGreg Roach        return route(self::ROUTE_NAME, [
15071378461SGreg Roach                'xref' => $individual->xref(),
15171378461SGreg Roach                'tree' => $individual->tree()->name(),
15271378461SGreg Roach            ] + $parameters + self::DEFAULT_PARAMETERS);
15371378461SGreg Roach    }
15471378461SGreg Roach
15571378461SGreg Roach    /**
1566ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
157389266c0SGreg Roach     *
1586ccdf4f0SGreg Roach     * @return ResponseInterface
159389266c0SGreg Roach     */
16071378461SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
161389266c0SGreg Roach    {
16257ab2231SGreg Roach        $tree        = $request->getAttribute('tree');
16357ab2231SGreg Roach        $user        = $request->getAttribute('user');
16471378461SGreg Roach        $xref        = $request->getAttribute('xref');
16571378461SGreg Roach        $book_size   = (int) $request->getAttribute('book_size');
16671378461SGreg Roach        $generations = (int) $request->getAttribute('generations');
16771378461SGreg Roach        $spouses     = (bool) $request->getAttribute('spouses');
1680b93976aSGreg Roach        $ajax        = $request->getQueryParams()['ajax'] ?? '';
169389266c0SGreg Roach        $individual  = Individual::getInstance($xref, $tree);
170389266c0SGreg Roach
17171378461SGreg Roach        // Convert POST requests into GET requests for pretty URLs.
17271378461SGreg Roach        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
17371378461SGreg Roach            return redirect(route(self::ROUTE_NAME, [
17471378461SGreg Roach                'tree'        => $request->getAttribute('tree')->name(),
17571378461SGreg Roach                'xref'        => $request->getParsedBody()['xref'],
17671378461SGreg Roach                'book_size'   => $request->getParsedBody()['book_size'],
17771378461SGreg Roach                'generations' => $request->getParsedBody()['generations'],
17871378461SGreg Roach                'spouses'     => $request->getParsedBody()['spouses'] ?? false,
17971378461SGreg Roach            ]));
18071378461SGreg Roach        }
18171378461SGreg Roach
182389266c0SGreg Roach        Auth::checkIndividualAccess($individual);
1839867b2f0SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
184389266c0SGreg Roach
185e759aebbSGreg Roach        $generations = min($generations, self::MAXIMUM_GENERATIONS);
186e759aebbSGreg Roach        $generations = max($generations, self::MINIMUM_GENERATIONS);
187389266c0SGreg Roach
188389266c0SGreg Roach        // Generations of ancestors/descendants in each mini-tree.
189389266c0SGreg Roach        $book_size = min($book_size, 5);
190389266c0SGreg Roach        $book_size = max($book_size, 2);
191389266c0SGreg Roach
1920b93976aSGreg Roach        if ($ajax === '1') {
19371378461SGreg Roach            $this->layout = 'layouts/ajax';
19471378461SGreg Roach
19571378461SGreg Roach            return $this->viewResponse('modules/family-book-chart/chart', [
19671378461SGreg Roach                'individual'  => $individual,
19771378461SGreg Roach                'generations' => $generations,
19871378461SGreg Roach                'book_size'   => $book_size,
19971378461SGreg Roach                'spouses'     => $spouses,
20071378461SGreg Roach            ]);
201389266c0SGreg Roach        }
202389266c0SGreg Roach
203389266c0SGreg Roach        $ajax_url = $this->chartUrl($individual, [
2049b5537c3SGreg Roach            'ajax'        => true,
205389266c0SGreg Roach            'book_size'   => $book_size,
206389266c0SGreg Roach            'generations' => $generations,
20771378461SGreg Roach            'spouses'     => $spouses,
208389266c0SGreg Roach        ]);
209389266c0SGreg Roach
2109b5537c3SGreg Roach        return $this->viewResponse('modules/family-book-chart/page', [
211389266c0SGreg Roach            'ajax_url'            => $ajax_url,
212389266c0SGreg Roach            'book_size'           => $book_size,
213389266c0SGreg Roach            'generations'         => $generations,
214389266c0SGreg Roach            'individual'          => $individual,
215e759aebbSGreg Roach            'maximum_generations' => self::MAXIMUM_GENERATIONS,
216e759aebbSGreg Roach            'minimum_generations' => self::MINIMUM_GENERATIONS,
21771378461SGreg Roach            'module'              => $this->name(),
21871378461SGreg Roach            'spouses'             => $spouses,
219389266c0SGreg Roach            'title'               => $this->chartTitle($individual),
220389266c0SGreg Roach        ]);
221389266c0SGreg Roach    }
222168ff6f3Sric2016}
223