xref: /webtrees/app/Module/FamilyBookChartModule.php (revision b7765f6bb42c7bc4d40e918dd7f82d433fe894e4)
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;
22389266c0SGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint;
23168ff6f3Sric2016use Fisharebest\Webtrees\I18N;
24168ff6f3Sric2016use Fisharebest\Webtrees\Individual;
25e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu;
26389266c0SGreg Roachuse Fisharebest\Webtrees\Tree;
276ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
29389266c0SGreg Roachuse stdClass;
30f4ba05e3SGreg Roachuse function view;
31168ff6f3Sric2016
32168ff6f3Sric2016/**
33168ff6f3Sric2016 * Class FamilyBookChartModule
34168ff6f3Sric2016 */
3537eb8894SGreg Roachclass FamilyBookChartModule extends AbstractModule implements ModuleChartInterface
36c1010edaSGreg Roach{
3749a243cbSGreg Roach    use ModuleChartTrait;
3849a243cbSGreg Roach
39389266c0SGreg Roach    // Defaults
40389266c0SGreg Roach    private const DEFAULT_GENERATIONS            = '2';
41389266c0SGreg Roach    private const DEFAULT_DESCENDANT_GENERATIONS = '5';
42389266c0SGreg Roach    private const DEFAULT_MAXIMUM_GENERATIONS    = '9';
43389266c0SGreg Roach
44e759aebbSGreg Roach    // Limits
45e759aebbSGreg Roach    public const MINIMUM_GENERATIONS = 2;
46e759aebbSGreg Roach    public const MAXIMUM_GENERATIONS = 10;
47e759aebbSGreg Roach
48389266c0SGreg Roach    /** @var stdClass */
49389266c0SGreg Roach    private $box;
50389266c0SGreg Roach
51389266c0SGreg Roach    /** @var bool */
52389266c0SGreg Roach    private $show_spouse;
53389266c0SGreg Roach
54389266c0SGreg Roach    /** @var int */
55389266c0SGreg Roach    private $descent;
56389266c0SGreg Roach
57389266c0SGreg Roach    /** @var int */
58389266c0SGreg Roach    private $generations;
59389266c0SGreg Roach
60168ff6f3Sric2016    /**
610cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
62168ff6f3Sric2016     *
63168ff6f3Sric2016     * @return string
64168ff6f3Sric2016     */
6549a243cbSGreg Roach    public function title(): string
66c1010edaSGreg Roach    {
67bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
68bbb76c12SGreg Roach        return I18N::translate('Family book');
69168ff6f3Sric2016    }
70168ff6f3Sric2016
71168ff6f3Sric2016    /**
72168ff6f3Sric2016     * A sentence describing what this module does.
73168ff6f3Sric2016     *
74168ff6f3Sric2016     * @return string
75168ff6f3Sric2016     */
7649a243cbSGreg Roach    public function description(): string
77c1010edaSGreg Roach    {
78bbb76c12SGreg Roach        /* I18N: Description of the “FamilyBookChart” module */
79bbb76c12SGreg Roach        return I18N::translate('A chart of an individual’s ancestors and descendants, as a family book.');
80168ff6f3Sric2016    }
81168ff6f3Sric2016
82168ff6f3Sric2016    /**
83377a2979SGreg Roach     * CSS class for the URL.
84377a2979SGreg Roach     *
85377a2979SGreg Roach     * @return string
86377a2979SGreg Roach     */
87377a2979SGreg Roach    public function chartMenuClass(): string
88377a2979SGreg Roach    {
89377a2979SGreg Roach        return 'menu-chart-familybook';
90377a2979SGreg Roach    }
91377a2979SGreg Roach
92377a2979SGreg Roach    /**
934eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
944eb71cfaSGreg Roach     *
9560bc3e3fSGreg Roach     * @param Individual $individual
9660bc3e3fSGreg Roach     *
974eb71cfaSGreg Roach     * @return Menu|null
984eb71cfaSGreg Roach     */
99377a2979SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
100c1010edaSGreg Roach    {
101e6562982SGreg Roach        return $this->chartMenu($individual);
102e6562982SGreg Roach    }
103e6562982SGreg Roach
104e6562982SGreg Roach    /**
105e6562982SGreg Roach     * The title for a specific instance of this chart.
106e6562982SGreg Roach     *
107e6562982SGreg Roach     * @param Individual $individual
108e6562982SGreg Roach     *
109e6562982SGreg Roach     * @return string
110e6562982SGreg Roach     */
111e6562982SGreg Roach    public function chartTitle(Individual $individual): string
112e6562982SGreg Roach    {
113e6562982SGreg Roach        /* I18N: %s is an individual’s name */
11439ca88baSGreg Roach        return I18N::translate('Family book of %s', $individual->fullName());
115e6562982SGreg Roach    }
116e6562982SGreg Roach
117e6562982SGreg Roach    /**
118389266c0SGreg Roach     * A form to request the chart parameters.
119389266c0SGreg Roach     *
1206ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
121389266c0SGreg Roach     * @param Tree                   $tree
122e5a6b4d4SGreg Roach     * @param UserInterface          $user
123389266c0SGreg Roach     *
1246ccdf4f0SGreg Roach     * @return ResponseInterface
125389266c0SGreg Roach     */
1266ccdf4f0SGreg Roach    public function getChartAction(ServerRequestInterface $request, Tree $tree, UserInterface $user): ResponseInterface
127389266c0SGreg Roach    {
128*b7765f6bSGreg Roach        $ajax       = (bool) ($request->getQueryParams()['ajax'] ?? false);
129*b7765f6bSGreg Roach        $xref       = $request->getQueryParams()['xref'] ?? '';
130389266c0SGreg Roach        $individual = Individual::getInstance($xref, $tree);
131389266c0SGreg Roach
132389266c0SGreg Roach        Auth::checkIndividualAccess($individual);
1339867b2f0SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
134389266c0SGreg Roach
135*b7765f6bSGreg Roach        $show_spouse = (bool) ($request->getQueryParams()['show_spouse'] ?? false);
136*b7765f6bSGreg Roach        $generations = (int) ($request->getQueryParams()['generations'] ?? self::DEFAULT_GENERATIONS);
137e759aebbSGreg Roach        $generations = min($generations, self::MAXIMUM_GENERATIONS);
138e759aebbSGreg Roach        $generations = max($generations, self::MINIMUM_GENERATIONS);
139389266c0SGreg Roach
140389266c0SGreg Roach        // Generations of ancestors/descendants in each mini-tree.
141*b7765f6bSGreg Roach        $book_size = (int) ($request->getQueryParams()['book_size'] ?? 2);
142389266c0SGreg Roach        $book_size = min($book_size, 5);
143389266c0SGreg Roach        $book_size = max($book_size, 2);
144389266c0SGreg Roach
1459b5537c3SGreg Roach        if ($ajax) {
146389266c0SGreg Roach            return $this->chart($individual, $generations, $book_size, $show_spouse);
147389266c0SGreg Roach        }
148389266c0SGreg Roach
149389266c0SGreg Roach        $ajax_url = $this->chartUrl($individual, [
1509b5537c3SGreg Roach            'ajax'        => true,
151389266c0SGreg Roach            'book_size'   => $book_size,
152389266c0SGreg Roach            'generations' => $generations,
153e5a6b4d4SGreg Roach            'show_spouse' => $show_spouse,
154389266c0SGreg Roach        ]);
155389266c0SGreg Roach
1569b5537c3SGreg Roach        return $this->viewResponse('modules/family-book-chart/page', [
157389266c0SGreg Roach            'ajax_url'            => $ajax_url,
158389266c0SGreg Roach            'book_size'           => $book_size,
159389266c0SGreg Roach            'generations'         => $generations,
160389266c0SGreg Roach            'individual'          => $individual,
161e759aebbSGreg Roach            'maximum_generations' => self::MAXIMUM_GENERATIONS,
162e759aebbSGreg Roach            'minimum_generations' => self::MINIMUM_GENERATIONS,
16326684e68SGreg Roach            'module_name'         => $this->name(),
164389266c0SGreg Roach            'show_spouse'         => $show_spouse,
165389266c0SGreg Roach            'title'               => $this->chartTitle($individual),
166389266c0SGreg Roach        ]);
167389266c0SGreg Roach    }
168389266c0SGreg Roach
169389266c0SGreg Roach    /**
170389266c0SGreg Roach     * @param Individual $individual
171389266c0SGreg Roach     * @param int        $generations
172389266c0SGreg Roach     * @param int        $book_size
173389266c0SGreg Roach     * @param bool       $show_spouse
174389266c0SGreg Roach     *
1756ccdf4f0SGreg Roach     * @return ResponseInterface
176389266c0SGreg Roach     */
1776ccdf4f0SGreg Roach    public function chart(Individual $individual, int $generations, int $book_size, bool $show_spouse): ResponseInterface
178389266c0SGreg Roach    {
179389266c0SGreg Roach        $this->box = (object) [
180cab242e7SGreg Roach            'width'  => app(ModuleThemeInterface::class)->parameter('chart-box-x'),
181cab242e7SGreg Roach            'height' => app(ModuleThemeInterface::class)->parameter('chart-box-y'),
182389266c0SGreg Roach        ];
183389266c0SGreg Roach
184389266c0SGreg Roach        $this->show_spouse = $show_spouse;
185389266c0SGreg Roach        $this->descent     = $generations;
186389266c0SGreg Roach        $this->generations = $book_size;
187389266c0SGreg Roach
188*b7765f6bSGreg Roach        $html = view('modules/family-book-chart/chart', ['individual' => $individual, 'generations' => $generations, 'book_size' => $book_size, 'show_spouse' => $show_spouse]);
189389266c0SGreg Roach
1906ccdf4f0SGreg Roach        return response($html);
191389266c0SGreg Roach    }
192168ff6f3Sric2016}
193