xref: /webtrees/app/Module/FamilyBookChartModule.php (revision 57ab22314b2599feb432b1a1ed71643cfc2f0452)
1168ff6f3Sric2016<?php
2168ff6f3Sric2016/**
3168ff6f3Sric2016 * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
5168ff6f3Sric2016 * This program is free software: you can redistribute it and/or modify
6168ff6f3Sric2016 * it under the terms of the GNU General Public License as published by
7168ff6f3Sric2016 * the Free Software Foundation, either version 3 of the License, or
8168ff6f3Sric2016 * (at your option) any later version.
9168ff6f3Sric2016 * This program is distributed in the hope that it will be useful,
10168ff6f3Sric2016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11168ff6f3Sric2016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12168ff6f3Sric2016 * GNU General Public License for more details.
13168ff6f3Sric2016 * You should have received a copy of the GNU General Public License
14168ff6f3Sric2016 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15168ff6f3Sric2016 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
18168ff6f3Sric2016namespace Fisharebest\Webtrees\Module;
19168ff6f3Sric2016
20389266c0SGreg Roachuse Fisharebest\Webtrees\Auth;
21168ff6f3Sric2016use Fisharebest\Webtrees\I18N;
22168ff6f3Sric2016use Fisharebest\Webtrees\Individual;
23e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu;
246ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
256ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
26f4ba05e3SGreg Roachuse function view;
27168ff6f3Sric2016
28168ff6f3Sric2016/**
29168ff6f3Sric2016 * Class FamilyBookChartModule
30168ff6f3Sric2016 */
3137eb8894SGreg Roachclass FamilyBookChartModule extends AbstractModule implements ModuleChartInterface
32c1010edaSGreg Roach{
3349a243cbSGreg Roach    use ModuleChartTrait;
3449a243cbSGreg Roach
35389266c0SGreg Roach    // Defaults
36389266c0SGreg Roach    private const DEFAULT_GENERATIONS            = '2';
37389266c0SGreg Roach    private const DEFAULT_DESCENDANT_GENERATIONS = '5';
38389266c0SGreg Roach    private const DEFAULT_MAXIMUM_GENERATIONS    = '9';
39389266c0SGreg Roach
40e759aebbSGreg Roach    // Limits
41e759aebbSGreg Roach    public const MINIMUM_GENERATIONS = 2;
42e759aebbSGreg Roach    public const MAXIMUM_GENERATIONS = 10;
43e759aebbSGreg Roach
44168ff6f3Sric2016    /**
450cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
46168ff6f3Sric2016     *
47168ff6f3Sric2016     * @return string
48168ff6f3Sric2016     */
4949a243cbSGreg Roach    public function title(): string
50c1010edaSGreg Roach    {
51bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
52bbb76c12SGreg Roach        return I18N::translate('Family book');
53168ff6f3Sric2016    }
54168ff6f3Sric2016
55168ff6f3Sric2016    /**
56168ff6f3Sric2016     * A sentence describing what this module does.
57168ff6f3Sric2016     *
58168ff6f3Sric2016     * @return string
59168ff6f3Sric2016     */
6049a243cbSGreg Roach    public function description(): string
61c1010edaSGreg Roach    {
62bbb76c12SGreg Roach        /* I18N: Description of the “FamilyBookChart” module */
63bbb76c12SGreg Roach        return I18N::translate('A chart of an individual’s ancestors and descendants, as a family book.');
64168ff6f3Sric2016    }
65168ff6f3Sric2016
66168ff6f3Sric2016    /**
67377a2979SGreg Roach     * CSS class for the URL.
68377a2979SGreg Roach     *
69377a2979SGreg Roach     * @return string
70377a2979SGreg Roach     */
71377a2979SGreg Roach    public function chartMenuClass(): string
72377a2979SGreg Roach    {
73377a2979SGreg Roach        return 'menu-chart-familybook';
74377a2979SGreg Roach    }
75377a2979SGreg Roach
76377a2979SGreg Roach    /**
774eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
784eb71cfaSGreg Roach     *
7960bc3e3fSGreg Roach     * @param Individual $individual
8060bc3e3fSGreg Roach     *
814eb71cfaSGreg Roach     * @return Menu|null
824eb71cfaSGreg Roach     */
83377a2979SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
84c1010edaSGreg Roach    {
85e6562982SGreg Roach        return $this->chartMenu($individual);
86e6562982SGreg Roach    }
87e6562982SGreg Roach
88e6562982SGreg Roach    /**
89e6562982SGreg Roach     * The title for a specific instance of this chart.
90e6562982SGreg Roach     *
91e6562982SGreg Roach     * @param Individual $individual
92e6562982SGreg Roach     *
93e6562982SGreg Roach     * @return string
94e6562982SGreg Roach     */
95e6562982SGreg Roach    public function chartTitle(Individual $individual): string
96e6562982SGreg Roach    {
97e6562982SGreg Roach        /* I18N: %s is an individual’s name */
9839ca88baSGreg Roach        return I18N::translate('Family book of %s', $individual->fullName());
99e6562982SGreg Roach    }
100e6562982SGreg Roach
101e6562982SGreg Roach    /**
102389266c0SGreg Roach     * A form to request the chart parameters.
103389266c0SGreg Roach     *
1046ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
105389266c0SGreg Roach     *
1066ccdf4f0SGreg Roach     * @return ResponseInterface
107389266c0SGreg Roach     */
108*57ab2231SGreg Roach    public function getChartAction(ServerRequestInterface $request): ResponseInterface
109389266c0SGreg Roach    {
110*57ab2231SGreg Roach        $tree       = $request->getAttribute('tree');
111*57ab2231SGreg Roach        $user       = $request->getAttribute('user');
1120b93976aSGreg Roach        $ajax       = $request->getQueryParams()['ajax'] ?? '';
113b7765f6bSGreg Roach        $xref       = $request->getQueryParams()['xref'] ?? '';
114389266c0SGreg Roach        $individual = Individual::getInstance($xref, $tree);
115389266c0SGreg Roach
116389266c0SGreg Roach        Auth::checkIndividualAccess($individual);
1179867b2f0SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
118389266c0SGreg Roach
119b7765f6bSGreg Roach        $show_spouse = (bool) ($request->getQueryParams()['show_spouse'] ?? false);
120b7765f6bSGreg Roach        $generations = (int) ($request->getQueryParams()['generations'] ?? self::DEFAULT_GENERATIONS);
121e759aebbSGreg Roach        $generations = min($generations, self::MAXIMUM_GENERATIONS);
122e759aebbSGreg Roach        $generations = max($generations, self::MINIMUM_GENERATIONS);
123389266c0SGreg Roach
124389266c0SGreg Roach        // Generations of ancestors/descendants in each mini-tree.
125b7765f6bSGreg Roach        $book_size = (int) ($request->getQueryParams()['book_size'] ?? 2);
126389266c0SGreg Roach        $book_size = min($book_size, 5);
127389266c0SGreg Roach        $book_size = max($book_size, 2);
128389266c0SGreg Roach
1290b93976aSGreg Roach        if ($ajax === '1') {
130389266c0SGreg Roach            return $this->chart($individual, $generations, $book_size, $show_spouse);
131389266c0SGreg Roach        }
132389266c0SGreg Roach
133389266c0SGreg Roach        $ajax_url = $this->chartUrl($individual, [
1349b5537c3SGreg Roach            'ajax'        => true,
135389266c0SGreg Roach            'book_size'   => $book_size,
136389266c0SGreg Roach            'generations' => $generations,
137e5a6b4d4SGreg Roach            'show_spouse' => $show_spouse,
138389266c0SGreg Roach        ]);
139389266c0SGreg Roach
1409b5537c3SGreg Roach        return $this->viewResponse('modules/family-book-chart/page', [
141389266c0SGreg Roach            'ajax_url'            => $ajax_url,
142389266c0SGreg Roach            'book_size'           => $book_size,
143389266c0SGreg Roach            'generations'         => $generations,
144389266c0SGreg Roach            'individual'          => $individual,
145e759aebbSGreg Roach            'maximum_generations' => self::MAXIMUM_GENERATIONS,
146e759aebbSGreg Roach            'minimum_generations' => self::MINIMUM_GENERATIONS,
14726684e68SGreg Roach            'module_name'         => $this->name(),
148389266c0SGreg Roach            'show_spouse'         => $show_spouse,
149389266c0SGreg Roach            'title'               => $this->chartTitle($individual),
150389266c0SGreg Roach        ]);
151389266c0SGreg Roach    }
152389266c0SGreg Roach
153389266c0SGreg Roach    /**
154389266c0SGreg Roach     * @param Individual $individual
155389266c0SGreg Roach     * @param int        $generations
156389266c0SGreg Roach     * @param int        $book_size
157389266c0SGreg Roach     * @param bool       $show_spouse
158389266c0SGreg Roach     *
1596ccdf4f0SGreg Roach     * @return ResponseInterface
160389266c0SGreg Roach     */
1616ccdf4f0SGreg Roach    public function chart(Individual $individual, int $generations, int $book_size, bool $show_spouse): ResponseInterface
162389266c0SGreg Roach    {
163b7765f6bSGreg Roach        $html = view('modules/family-book-chart/chart', ['individual' => $individual, 'generations' => $generations, 'book_size' => $book_size, 'show_spouse' => $show_spouse]);
164389266c0SGreg Roach
1656ccdf4f0SGreg Roach        return response($html);
166389266c0SGreg Roach    }
167168ff6f3Sric2016}
168