xref: /webtrees/app/Module/FamilyBookChartModule.php (revision 0b93976a9c83f1ad374620df2dc12a210d5be076)
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;
21e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
22168ff6f3Sric2016use Fisharebest\Webtrees\I18N;
23168ff6f3Sric2016use Fisharebest\Webtrees\Individual;
24e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu;
25389266c0SGreg Roachuse Fisharebest\Webtrees\Tree;
266ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
276ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
28f4ba05e3SGreg Roachuse function view;
29168ff6f3Sric2016
30168ff6f3Sric2016/**
31168ff6f3Sric2016 * Class FamilyBookChartModule
32168ff6f3Sric2016 */
3337eb8894SGreg Roachclass FamilyBookChartModule extends AbstractModule implements ModuleChartInterface
34c1010edaSGreg Roach{
3549a243cbSGreg Roach    use ModuleChartTrait;
3649a243cbSGreg Roach
37389266c0SGreg Roach    // Defaults
38389266c0SGreg Roach    private const DEFAULT_GENERATIONS            = '2';
39389266c0SGreg Roach    private const DEFAULT_DESCENDANT_GENERATIONS = '5';
40389266c0SGreg Roach    private const DEFAULT_MAXIMUM_GENERATIONS    = '9';
41389266c0SGreg Roach
42e759aebbSGreg Roach    // Limits
43e759aebbSGreg Roach    public const MINIMUM_GENERATIONS = 2;
44e759aebbSGreg Roach    public const MAXIMUM_GENERATIONS = 10;
45e759aebbSGreg Roach
46168ff6f3Sric2016    /**
470cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
48168ff6f3Sric2016     *
49168ff6f3Sric2016     * @return string
50168ff6f3Sric2016     */
5149a243cbSGreg Roach    public function title(): string
52c1010edaSGreg Roach    {
53bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
54bbb76c12SGreg Roach        return I18N::translate('Family book');
55168ff6f3Sric2016    }
56168ff6f3Sric2016
57168ff6f3Sric2016    /**
58168ff6f3Sric2016     * A sentence describing what this module does.
59168ff6f3Sric2016     *
60168ff6f3Sric2016     * @return string
61168ff6f3Sric2016     */
6249a243cbSGreg Roach    public function description(): string
63c1010edaSGreg Roach    {
64bbb76c12SGreg Roach        /* I18N: Description of the “FamilyBookChart” module */
65bbb76c12SGreg Roach        return I18N::translate('A chart of an individual’s ancestors and descendants, as a family book.');
66168ff6f3Sric2016    }
67168ff6f3Sric2016
68168ff6f3Sric2016    /**
69377a2979SGreg Roach     * CSS class for the URL.
70377a2979SGreg Roach     *
71377a2979SGreg Roach     * @return string
72377a2979SGreg Roach     */
73377a2979SGreg Roach    public function chartMenuClass(): string
74377a2979SGreg Roach    {
75377a2979SGreg Roach        return 'menu-chart-familybook';
76377a2979SGreg Roach    }
77377a2979SGreg Roach
78377a2979SGreg Roach    /**
794eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
804eb71cfaSGreg Roach     *
8160bc3e3fSGreg Roach     * @param Individual $individual
8260bc3e3fSGreg Roach     *
834eb71cfaSGreg Roach     * @return Menu|null
844eb71cfaSGreg Roach     */
85377a2979SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
86c1010edaSGreg Roach    {
87e6562982SGreg Roach        return $this->chartMenu($individual);
88e6562982SGreg Roach    }
89e6562982SGreg Roach
90e6562982SGreg Roach    /**
91e6562982SGreg Roach     * The title for a specific instance of this chart.
92e6562982SGreg Roach     *
93e6562982SGreg Roach     * @param Individual $individual
94e6562982SGreg Roach     *
95e6562982SGreg Roach     * @return string
96e6562982SGreg Roach     */
97e6562982SGreg Roach    public function chartTitle(Individual $individual): string
98e6562982SGreg Roach    {
99e6562982SGreg Roach        /* I18N: %s is an individual’s name */
10039ca88baSGreg Roach        return I18N::translate('Family book of %s', $individual->fullName());
101e6562982SGreg Roach    }
102e6562982SGreg Roach
103e6562982SGreg Roach    /**
104389266c0SGreg Roach     * A form to request the chart parameters.
105389266c0SGreg Roach     *
1066ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
107389266c0SGreg Roach     * @param Tree                   $tree
108e5a6b4d4SGreg Roach     * @param UserInterface          $user
109389266c0SGreg Roach     *
1106ccdf4f0SGreg Roach     * @return ResponseInterface
111389266c0SGreg Roach     */
1126ccdf4f0SGreg Roach    public function getChartAction(ServerRequestInterface $request, Tree $tree, UserInterface $user): ResponseInterface
113389266c0SGreg Roach    {
114*0b93976aSGreg Roach        $ajax       = $request->getQueryParams()['ajax'] ?? '';
115b7765f6bSGreg Roach        $xref       = $request->getQueryParams()['xref'] ?? '';
116389266c0SGreg Roach        $individual = Individual::getInstance($xref, $tree);
117389266c0SGreg Roach
118389266c0SGreg Roach        Auth::checkIndividualAccess($individual);
1199867b2f0SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
120389266c0SGreg Roach
121b7765f6bSGreg Roach        $show_spouse = (bool) ($request->getQueryParams()['show_spouse'] ?? false);
122b7765f6bSGreg Roach        $generations = (int) ($request->getQueryParams()['generations'] ?? self::DEFAULT_GENERATIONS);
123e759aebbSGreg Roach        $generations = min($generations, self::MAXIMUM_GENERATIONS);
124e759aebbSGreg Roach        $generations = max($generations, self::MINIMUM_GENERATIONS);
125389266c0SGreg Roach
126389266c0SGreg Roach        // Generations of ancestors/descendants in each mini-tree.
127b7765f6bSGreg Roach        $book_size = (int) ($request->getQueryParams()['book_size'] ?? 2);
128389266c0SGreg Roach        $book_size = min($book_size, 5);
129389266c0SGreg Roach        $book_size = max($book_size, 2);
130389266c0SGreg Roach
131*0b93976aSGreg Roach        if ($ajax === '1') {
132389266c0SGreg Roach            return $this->chart($individual, $generations, $book_size, $show_spouse);
133389266c0SGreg Roach        }
134389266c0SGreg Roach
135389266c0SGreg Roach        $ajax_url = $this->chartUrl($individual, [
1369b5537c3SGreg Roach            'ajax'        => true,
137389266c0SGreg Roach            'book_size'   => $book_size,
138389266c0SGreg Roach            'generations' => $generations,
139e5a6b4d4SGreg Roach            'show_spouse' => $show_spouse,
140389266c0SGreg Roach        ]);
141389266c0SGreg Roach
1429b5537c3SGreg Roach        return $this->viewResponse('modules/family-book-chart/page', [
143389266c0SGreg Roach            'ajax_url'            => $ajax_url,
144389266c0SGreg Roach            'book_size'           => $book_size,
145389266c0SGreg Roach            'generations'         => $generations,
146389266c0SGreg Roach            'individual'          => $individual,
147e759aebbSGreg Roach            'maximum_generations' => self::MAXIMUM_GENERATIONS,
148e759aebbSGreg Roach            'minimum_generations' => self::MINIMUM_GENERATIONS,
14926684e68SGreg Roach            'module_name'         => $this->name(),
150389266c0SGreg Roach            'show_spouse'         => $show_spouse,
151389266c0SGreg Roach            'title'               => $this->chartTitle($individual),
152389266c0SGreg Roach        ]);
153389266c0SGreg Roach    }
154389266c0SGreg Roach
155389266c0SGreg Roach    /**
156389266c0SGreg Roach     * @param Individual $individual
157389266c0SGreg Roach     * @param int        $generations
158389266c0SGreg Roach     * @param int        $book_size
159389266c0SGreg Roach     * @param bool       $show_spouse
160389266c0SGreg Roach     *
1616ccdf4f0SGreg Roach     * @return ResponseInterface
162389266c0SGreg Roach     */
1636ccdf4f0SGreg Roach    public function chart(Individual $individual, int $generations, int $book_size, bool $show_spouse): ResponseInterface
164389266c0SGreg Roach    {
165b7765f6bSGreg Roach        $html = view('modules/family-book-chart/chart', ['individual' => $individual, 'generations' => $generations, 'book_size' => $book_size, 'show_spouse' => $show_spouse]);
166389266c0SGreg Roach
1676ccdf4f0SGreg Roach        return response($html);
168389266c0SGreg Roach    }
169168ff6f3Sric2016}
170