xref: /webtrees/app/Module/FamilyBookChartModule.php (revision 7ef421a4290b6b468944b845497d6139b0eddd3f)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Aura\Router\RouterContainer;
23use Fig\Http\Message\RequestMethodInterface;
24use Fisharebest\Webtrees\Auth;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Individual;
27use Fisharebest\Webtrees\Menu;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30use Psr\Http\Server\RequestHandlerInterface;
31
32use function max;
33use function min;
34use function route;
35
36/**
37 * Class FamilyBookChartModule
38 */
39class FamilyBookChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
40{
41    use ModuleChartTrait;
42
43    private const ROUTE_NAME = 'family-book-chart';
44    private const ROUTE_URL  = '/tree/{tree}/family-book-{book_size}-{generations}-{spouses}/{xref}';
45
46    // Defaults
47    public const    DEFAULT_GENERATIONS            = '2';
48    public const    DEFAULT_DESCENDANT_GENERATIONS = '5';
49    public const    DEFAULT_MAXIMUM_GENERATIONS    = '9';
50    protected const DEFAULT_PARAMETERS             = [
51        'book_size'   => self::DEFAULT_GENERATIONS,
52        'generations' => self::DEFAULT_DESCENDANT_GENERATIONS,
53        'spouses'     => false,
54    ];
55
56    // Limits
57    protected const MINIMUM_GENERATIONS = 2;
58    protected const MAXIMUM_GENERATIONS = 10;
59
60    /**
61     * Initialization.
62     *
63     * @param RouterContainer $router_container
64     */
65    public function boot(RouterContainer $router_container): void
66    {
67        $router_container->getMap()
68            ->get(self::ROUTE_NAME, self::ROUTE_URL, self::class)
69            ->allows(RequestMethodInterface::METHOD_POST)
70            ->tokens([
71                'book_size'   => '\d+',
72                'generations' => '\d+',
73                'spouses'     => '1?',
74            ]);
75    }
76
77    /**
78     * How should this module be identified in the control panel, etc.?
79     *
80     * @return string
81     */
82    public function title(): string
83    {
84        /* I18N: Name of a module/chart */
85        return I18N::translate('Family book');
86    }
87
88    /**
89     * A sentence describing what this module does.
90     *
91     * @return string
92     */
93    public function description(): string
94    {
95        /* I18N: Description of the “FamilyBookChart” module */
96        return I18N::translate('A chart of an individual’s ancestors and descendants, as a family book.');
97    }
98
99    /**
100     * CSS class for the URL.
101     *
102     * @return string
103     */
104    public function chartMenuClass(): string
105    {
106        return 'menu-chart-familybook';
107    }
108
109    /**
110     * Return a menu item for this chart - for use in individual boxes.
111     *
112     * @param Individual $individual
113     *
114     * @return Menu|null
115     */
116    public function chartBoxMenu(Individual $individual): ?Menu
117    {
118        return $this->chartMenu($individual);
119    }
120
121    /**
122     * The title for a specific instance of this chart.
123     *
124     * @param Individual $individual
125     *
126     * @return string
127     */
128    public function chartTitle(Individual $individual): string
129    {
130        /* I18N: %s is an individual’s name */
131        return I18N::translate('Family book of %s', $individual->fullName());
132    }
133
134    /**
135     * The URL for a page showing chart options.
136     *
137     * @param Individual $individual
138     * @param mixed[]    $parameters
139     *
140     * @return string
141     */
142    public function chartUrl(Individual $individual, array $parameters = []): string
143    {
144        return route(self::ROUTE_NAME, [
145                'xref' => $individual->xref(),
146                'tree' => $individual->tree()->name(),
147            ] + $parameters + self::DEFAULT_PARAMETERS);
148    }
149
150    /**
151     * @param ServerRequestInterface $request
152     *
153     * @return ResponseInterface
154     */
155    public function handle(ServerRequestInterface $request): ResponseInterface
156    {
157        $tree        = $request->getAttribute('tree');
158        $user        = $request->getAttribute('user');
159        $xref        = $request->getAttribute('xref');
160        $book_size   = (int) $request->getAttribute('book_size');
161        $generations = (int) $request->getAttribute('generations');
162        $spouses     = (bool) $request->getAttribute('spouses');
163        $ajax        = $request->getQueryParams()['ajax'] ?? '';
164        $individual  = Individual::getInstance($xref, $tree);
165
166        // Convert POST requests into GET requests for pretty URLs.
167        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
168            return redirect(route(self::ROUTE_NAME, [
169                'tree'        => $request->getAttribute('tree')->name(),
170                'xref'        => $request->getParsedBody()['xref'],
171                'book_size'   => $request->getParsedBody()['book_size'],
172                'generations' => $request->getParsedBody()['generations'],
173                'spouses'     => $request->getParsedBody()['spouses'] ?? false,
174            ]));
175        }
176
177        Auth::checkIndividualAccess($individual);
178        Auth::checkComponentAccess($this, 'chart', $tree, $user);
179
180        $generations = min($generations, self::MAXIMUM_GENERATIONS);
181        $generations = max($generations, self::MINIMUM_GENERATIONS);
182
183        // Generations of ancestors/descendants in each mini-tree.
184        $book_size = min($book_size, 5);
185        $book_size = max($book_size, 2);
186
187        if ($ajax === '1') {
188            $this->layout = 'layouts/ajax';
189
190            return $this->viewResponse('modules/family-book-chart/chart', [
191                'individual'  => $individual,
192                'generations' => $generations,
193                'book_size'   => $book_size,
194                'spouses'     => $spouses,
195            ]);
196        }
197
198        $ajax_url = $this->chartUrl($individual, [
199            'ajax'        => true,
200            'book_size'   => $book_size,
201            'generations' => $generations,
202            'spouses'     => $spouses,
203        ]);
204
205        return $this->viewResponse('modules/family-book-chart/page', [
206            'ajax_url'            => $ajax_url,
207            'book_size'           => $book_size,
208            'generations'         => $generations,
209            'individual'          => $individual,
210            'maximum_generations' => self::MAXIMUM_GENERATIONS,
211            'minimum_generations' => self::MINIMUM_GENERATIONS,
212            'module'              => $this->name(),
213            'spouses'             => $spouses,
214            'title'               => $this->chartTitle($individual),
215        ]);
216    }
217}
218