xref: /webtrees/app/Module/PedigreeChartModule.php (revision ee4364da76be9b2651fd11a0b3dae7961849d2f3)
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 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Module;
20
21use Fisharebest\Webtrees\Auth;
22use Fisharebest\Webtrees\Functions\FunctionsEdit;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Individual;
25use Fisharebest\Webtrees\Menu;
26use Fisharebest\Webtrees\Services\ChartService;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
29
30/**
31 * Class PedigreeChartModule
32 */
33class PedigreeChartModule extends AbstractModule implements ModuleChartInterface
34{
35    use ModuleChartTrait;
36
37    // Defaults
38    protected const DEFAULT_GENERATIONS = '4';
39
40    // Limits
41    protected const MAX_GENERATIONS     = 12;
42    protected const MIN_GENERATIONS     = 2;
43
44    // Chart orientation options.  These are used to generate icons, views, etc.
45    public const ORIENTATION_LEFT  = 'left';
46    public const ORIENTATION_RIGHT = 'right';
47    public const ORIENTATION_UP    = 'up';
48    public const ORIENTATION_DOWN  = 'down';
49
50    protected const MIRROR_ORIENTATION = [
51        self::ORIENTATION_UP    => self::ORIENTATION_DOWN,
52        self::ORIENTATION_DOWN  => self::ORIENTATION_UP,
53        self::ORIENTATION_LEFT  => self::ORIENTATION_RIGHT,
54        self::ORIENTATION_RIGHT => self::ORIENTATION_LEFT,
55    ];
56
57    protected const DEFAULT_ORIENTATION = self::ORIENTATION_RIGHT;
58
59    /** @var ChartService */
60    private $chart_service;
61
62    /**
63     * PedigreeChartModule constructor.
64     *
65     * @param ChartService $chart_service
66     */
67    public function __construct(ChartService $chart_service)
68    {
69        $this->chart_service = $chart_service;
70    }
71
72    /**
73     * How should this module be identified in the control panel, etc.?
74     *
75     * @return string
76     */
77    public function title(): string
78    {
79        /* I18N: Name of a module/chart */
80        return I18N::translate('Pedigree');
81    }
82
83    /**
84     * A sentence describing what this module does.
85     *
86     * @return string
87     */
88    public function description(): string
89    {
90        /* I18N: Description of the “PedigreeChart” module */
91        return I18N::translate('A chart of an individual’s ancestors, formatted as a tree.');
92    }
93
94    /**
95     * CSS class for the URL.
96     *
97     * @return string
98     */
99    public function chartMenuClass(): string
100    {
101        return 'menu-chart-pedigree';
102    }
103
104    /**
105     * Return a menu item for this chart - for use in individual boxes.
106     *
107     * @param Individual $individual
108     *
109     * @return Menu|null
110     */
111    public function chartBoxMenu(Individual $individual): ?Menu
112    {
113        return $this->chartMenu($individual);
114    }
115
116    /**
117     * The title for a specific instance of this chart.
118     *
119     * @param Individual $individual
120     *
121     * @return string
122     */
123    public function chartTitle(Individual $individual): string
124    {
125        /* I18N: %s is an individual’s name */
126        return I18N::translate('Pedigree tree of %s', $individual->fullName());
127    }
128
129    /**
130     * A form to request the chart parameters.
131     *
132     * @param ServerRequestInterface $request
133     *
134     * @return ResponseInterface
135     */
136    public function getChartAction(ServerRequestInterface $request): ResponseInterface
137    {
138        $tree       = $request->getAttribute('tree');
139        $user       = $request->getAttribute('user');
140        $ajax       = $request->getQueryParams()['ajax'] ?? '';
141        $xref       = $request->getQueryParams()['xref'] ?? '';
142        $individual = Individual::getInstance($xref, $tree);
143
144        Auth::checkIndividualAccess($individual);
145        Auth::checkComponentAccess($this, 'chart', $tree, $user);
146
147        $orientation = $request->getQueryParams()['orientation'] ?? static::DEFAULT_ORIENTATION;
148        $generations = (int) ($request->getQueryParams()['generations'] ?? static::DEFAULT_GENERATIONS);
149
150        $generations = min(static::MAX_GENERATIONS, $generations);
151        $generations = max(static::MIN_GENERATIONS, $generations);
152
153        $generation_options = $this->generationOptions();
154
155        if ($ajax === '1') {
156            return $this->chart($individual, $orientation, $generations, $this->chart_service);
157        }
158
159        $ajax_url = $this->chartUrl($individual, [
160            'ajax'        => true,
161            'generations' => $generations,
162            'orientation' => $orientation,
163        ]);
164
165        return $this->viewResponse('modules/pedigree-chart/page', [
166            'ajax_url'           => $ajax_url,
167            'generations'        => $generations,
168            'generation_options' => $generation_options,
169            'individual'         => $individual,
170            'module_name'        => $this->name(),
171            'orientation'        => $orientation,
172            'orientations'       => $this->orientations(),
173            'title'              => $this->chartTitle($individual),
174        ]);
175    }
176
177    /**
178     * @param Individual   $individual
179     * @param string       $orientation
180     * @param int          $generations
181     * @param ChartService $chart_service
182     *
183     * @return ResponseInterface
184     */
185    public function chart(Individual $individual, string $orientation, int $generations, ChartService $chart_service): ResponseInterface
186    {
187        $ancestors = $chart_service->sosaStradonitzAncestors($individual, $generations);
188
189        // Father’s ancestors link to the father’s pedigree
190        // Mother’s ancestors link to the mother’s pedigree..
191        $links = $ancestors->map(function (?Individual $individual, $sosa) use ($ancestors, $orientation, $generations): string {
192            if ($individual instanceof Individual && $sosa >= 2 ** $generations / 2 && $individual->childFamilies()->isNotEmpty()) {
193                // The last row/column, and there are more generations.
194                if ($sosa >= 2 ** $generations * 3 / 4) {
195                    return $this->nextLink($ancestors->get(3), $orientation, $generations);
196                }
197
198                return $this->nextLink($ancestors->get(2), $orientation, $generations);
199            }
200
201            // A spacer to fix the "Left" layout.
202            return '<span class="invisible px-2">' . view('icons/arrow-' . $orientation) . '</span>';
203        });
204
205        // Root individual links to their children.
206        $links->put(1, $this->previousLink($individual, $orientation, $generations));
207
208        $html = view('modules/pedigree-chart/chart', [
209            'ancestors'   => $ancestors,
210            'generations' => $generations,
211            'orientation' => $orientation,
212            'layout'      => 'right',
213            'links'       => $links,
214        ]);
215
216        return response($html);
217    }
218
219    /**
220     * Build a menu for the chart root individual
221     *
222     * @param Individual $individual
223     * @param string     $orientation
224     * @param int        $generations
225     *
226     * @return string
227     */
228    public function nextLink(Individual $individual, string $orientation, int $generations): string
229    {
230        $icon  = view('icons/arrow-' . $orientation);
231        $title = $this->chartTitle($individual);
232        $url   = $this->chartUrl($individual, [
233            'orientation' => $orientation,
234            'generations' => $generations,
235        ]);
236
237        return '<a class="px-2" href="' . e($url) . '" title="' . strip_tags($title) . '">' . $icon . '<span class="sr-only">' . $title . '</span></a>';
238    }
239
240    /**
241     * Build a menu for the chart root individual
242     *
243     * @param Individual $individual
244     * @param string     $orientation
245     * @param int        $generations
246     *
247     * @return string
248     */
249    public function previousLink(Individual $individual, string $orientation, int $generations): string
250    {
251        $icon = view('icons/arrow-' . self::MIRROR_ORIENTATION[$orientation]);
252
253        $siblings = [];
254        $spouses  = [];
255        $children = [];
256
257        foreach ($individual->childFamilies() as $family) {
258            foreach ($family->children() as $child) {
259                if ($child !== $individual) {
260                    $siblings[] = $this->individualLink($child, $orientation, $generations);
261                }
262            }
263        }
264
265        foreach ($individual->spouseFamilies() as $family) {
266            foreach ($family->spouses() as $spouse) {
267                if ($spouse !== $individual) {
268                    $spouses[] = $this->individualLink($spouse, $orientation, $generations);
269                }
270            }
271
272            foreach ($family->children() as $child) {
273                $children[] = $this->individualLink($child, $orientation, $generations);
274            }
275        }
276
277        return view('modules/pedigree-chart/previous', [
278            'icon'        => $icon,
279            'individual'  => $individual,
280            'generations' => $generations,
281            'orientation' => $orientation,
282            'chart'       => $this,
283            'siblings'    => $siblings,
284            'spouses'     => $spouses,
285            'children'    => $children,
286        ]);
287    }
288
289    /**
290     * @param Individual $individual
291     * @param string     $orientation
292     * @param int        $generations
293     *
294     * @return string
295     */
296    protected function individualLink(Individual $individual, string $orientation, int $generations): string
297    {
298        $text  = $individual->fullName();
299        $title = $this->chartTitle($individual);
300        $url   = $this->chartUrl($individual, [
301            'orientation' => $orientation,
302            'generations' => $generations,
303        ]);
304
305        return '<a class="dropdown-item" href="' . e($url) . '" title="' . strip_tags($title) . '">' . $text . '</a>';
306    }
307
308    /**
309     * @return string[]
310     */
311    protected function generationOptions(): array
312    {
313        return FunctionsEdit::numericOptions(range(static::MIN_GENERATIONS, static::MAX_GENERATIONS));
314    }
315
316    /**
317     * @return string[]
318     */
319    protected function orientations(): array
320    {
321        return [
322            self::ORIENTATION_LEFT  => I18N::translate('Left'),
323            self::ORIENTATION_RIGHT => I18N::translate('Right'),
324            self::ORIENTATION_UP    => I18N::translate('Up'),
325            self::ORIENTATION_DOWN  => I18N::translate('Down'),
326        ];
327    }
328}
329