xref: /webtrees/app/Module/AncestorsChartModule.php (revision 0cf94a7ce3ba10064c41043d233a72eb3b194570)
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
20e539f5c6SGreg Roachuse Fisharebest\Webtrees\Auth;
21e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
22e539f5c6SGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsCharts;
23e539f5c6SGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint;
24e539f5c6SGreg Roachuse Fisharebest\Webtrees\Gedcom;
25168ff6f3Sric2016use Fisharebest\Webtrees\I18N;
26168ff6f3Sric2016use Fisharebest\Webtrees\Individual;
27e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu;
28e539f5c6SGreg Roachuse Fisharebest\Webtrees\Services\ChartService;
29e539f5c6SGreg Roachuse Fisharebest\Webtrees\Tree;
30e539f5c6SGreg Roachuse Illuminate\Support\Collection;
31e539f5c6SGreg Roachuse Symfony\Component\HttpFoundation\Request;
32e539f5c6SGreg Roachuse Symfony\Component\HttpFoundation\Response;
33168ff6f3Sric2016
34168ff6f3Sric2016/**
35168ff6f3Sric2016 * Class AncestorsChartModule
36168ff6f3Sric2016 */
3737eb8894SGreg Roachclass AncestorsChartModule extends AbstractModule implements ModuleChartInterface
38c1010edaSGreg Roach{
3949a243cbSGreg Roach    use ModuleChartTrait;
4049a243cbSGreg Roach
41e539f5c6SGreg Roach    // Chart styles
42e539f5c6SGreg Roach    protected const CHART_STYLE_LIST        = 'list';
43e539f5c6SGreg Roach    protected const CHART_STYLE_BOOKLET     = 'booklet';
44e539f5c6SGreg Roach    protected const CHART_STYLE_INDIVIDUALS = 'individuals';
45e539f5c6SGreg Roach    protected const CHART_STYLE_FAMILIES    = 'families';
46e539f5c6SGreg Roach
47e539f5c6SGreg Roach    // Defaults
48e539f5c6SGreg Roach    protected const DEFAULT_COUSINS             = false;
49e539f5c6SGreg Roach    protected const DEFAULT_STYLE               = self::CHART_STYLE_LIST;
50*0cf94a7cSGreg Roach    protected const DEFAULT_GENERATIONS         = '4';
51e759aebbSGreg Roach
52e759aebbSGreg Roach    // Limits
53e759aebbSGreg Roach    protected const MINIMUM_GENERATIONS = 2;
54e759aebbSGreg Roach    protected const MAXIMUM_GENERATIONS = 10;
55e539f5c6SGreg Roach
56168ff6f3Sric2016    /**
57168ff6f3Sric2016     * How should this module be labelled on tabs, menus, etc.?
58168ff6f3Sric2016     *
59168ff6f3Sric2016     * @return string
60168ff6f3Sric2016     */
6149a243cbSGreg Roach    public function title(): string
62c1010edaSGreg Roach    {
63bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
64bbb76c12SGreg Roach        return I18N::translate('Ancestors');
65168ff6f3Sric2016    }
66168ff6f3Sric2016
67168ff6f3Sric2016    /**
68168ff6f3Sric2016     * A sentence describing what this module does.
69168ff6f3Sric2016     *
70168ff6f3Sric2016     * @return string
71168ff6f3Sric2016     */
7249a243cbSGreg Roach    public function description(): string
73c1010edaSGreg Roach    {
74bbb76c12SGreg Roach        /* I18N: Description of the “AncestorsChart” module */
75bbb76c12SGreg Roach        return I18N::translate('A chart of an individual’s ancestors.');
76168ff6f3Sric2016    }
77168ff6f3Sric2016
78168ff6f3Sric2016    /**
79377a2979SGreg Roach     * CSS class for the URL.
80377a2979SGreg Roach     *
81377a2979SGreg Roach     * @return string
82377a2979SGreg Roach     */
83377a2979SGreg Roach    public function chartMenuClass(): string
84377a2979SGreg Roach    {
85377a2979SGreg Roach        return 'menu-chart-ancestry';
86377a2979SGreg Roach    }
87377a2979SGreg Roach
88377a2979SGreg Roach    /**
894eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
904eb71cfaSGreg Roach     *
9160bc3e3fSGreg Roach     * @param Individual $individual
9260bc3e3fSGreg Roach     *
934eb71cfaSGreg Roach     * @return Menu|null
944eb71cfaSGreg Roach     */
95377a2979SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
96c1010edaSGreg Roach    {
97e6562982SGreg Roach        return $this->chartMenu($individual);
98e6562982SGreg Roach    }
99e6562982SGreg Roach
100e6562982SGreg Roach    /**
101e6562982SGreg Roach     * The title for a specific instance of this chart.
102e6562982SGreg Roach     *
103e6562982SGreg Roach     * @param Individual $individual
104e6562982SGreg Roach     *
105e6562982SGreg Roach     * @return string
106e6562982SGreg Roach     */
107e6562982SGreg Roach    public function chartTitle(Individual $individual): string
108e6562982SGreg Roach    {
109e6562982SGreg Roach        /* I18N: %s is an individual’s name */
11039ca88baSGreg Roach        return I18N::translate('Ancestors of %s', $individual->fullName());
111e6562982SGreg Roach    }
112e6562982SGreg Roach
113e6562982SGreg Roach    /**
114e539f5c6SGreg Roach     * A form to request the chart parameters.
115e539f5c6SGreg Roach     *
116e539f5c6SGreg Roach     * @param Request       $request
117e539f5c6SGreg Roach     * @param Tree          $tree
118e5a6b4d4SGreg Roach     * @param UserInterface $user
119e539f5c6SGreg Roach     * @param ChartService  $chart_service
120e539f5c6SGreg Roach     *
121e539f5c6SGreg Roach     * @return Response
122e539f5c6SGreg Roach     */
123e5a6b4d4SGreg Roach    public function getChartAction(Request $request, Tree $tree, UserInterface $user, ChartService $chart_service): Response
124e539f5c6SGreg Roach    {
1259b5537c3SGreg Roach        $ajax       = (bool) $request->get('ajax');
126e539f5c6SGreg Roach        $xref       = $request->get('xref', '');
127e539f5c6SGreg Roach        $individual = Individual::getInstance($xref, $tree);
128e539f5c6SGreg Roach
129e539f5c6SGreg Roach        Auth::checkIndividualAccess($individual);
1309867b2f0SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
131e539f5c6SGreg Roach
132e539f5c6SGreg Roach        $show_cousins = (bool) $request->get('show_cousins', self::DEFAULT_COUSINS);
133e539f5c6SGreg Roach        $chart_style  = $request->get('chart_style', self::DEFAULT_STYLE);
134e759aebbSGreg Roach        $generations  = (int) $request->get('generations', self::DEFAULT_GENERATIONS);
135e539f5c6SGreg Roach
136*0cf94a7cSGreg Roach        $generations = min($generations, self::MAXIMUM_GENERATIONS);
137*0cf94a7cSGreg Roach        $generations = max($generations, self::MINIMUM_GENERATIONS);
138e539f5c6SGreg Roach
1399b5537c3SGreg Roach        if ($ajax) {
140e539f5c6SGreg Roach            $ancestors = $chart_service->sosaStradonitzAncestors($individual, $generations);
141e539f5c6SGreg Roach
142e539f5c6SGreg Roach            switch ($chart_style) {
143e539f5c6SGreg Roach                default:
144e539f5c6SGreg Roach                case self::CHART_STYLE_LIST:
145e539f5c6SGreg Roach                    return $this->ancestorsList($individual, $generations);
146e539f5c6SGreg Roach
147e539f5c6SGreg Roach                case self::CHART_STYLE_BOOKLET:
148e539f5c6SGreg Roach                    return $this->ancestorsBooklet($ancestors, $show_cousins);
149e539f5c6SGreg Roach
150e539f5c6SGreg Roach                case self::CHART_STYLE_INDIVIDUALS:
151e539f5c6SGreg Roach                    return $this->ancestorsIndividuals($tree, $ancestors);
152e539f5c6SGreg Roach
153e539f5c6SGreg Roach                case self::CHART_STYLE_FAMILIES:
154e539f5c6SGreg Roach                    return $this->ancestorsFamilies($tree, $ancestors);
155e539f5c6SGreg Roach            }
156e539f5c6SGreg Roach        }
157e539f5c6SGreg Roach
15854452b04SGreg Roach        $ajax_url = $this->chartUrl($individual, [
15954452b04SGreg Roach            'generations'  => $generations,
16054452b04SGreg Roach            'chart_style'  => $chart_style,
16154452b04SGreg Roach            'show_cousins' => $show_cousins,
1629b5537c3SGreg Roach            'ajax'         => true,
16354452b04SGreg Roach        ]);
16454452b04SGreg Roach
1659b5537c3SGreg Roach        return $this->viewResponse('modules/ancestors-chart/page', [
16654452b04SGreg Roach            'ajax_url'            => $ajax_url,
167e539f5c6SGreg Roach            'chart_style'         => $chart_style,
168e539f5c6SGreg Roach            'chart_styles'        => $this->chartStyles(),
169e759aebbSGreg Roach            'default_generations' => self::DEFAULT_GENERATIONS,
170e539f5c6SGreg Roach            'generations'         => $generations,
171e539f5c6SGreg Roach            'individual'          => $individual,
172e759aebbSGreg Roach            'maximum_generations' => self::MAXIMUM_GENERATIONS,
173e759aebbSGreg Roach            'minimum_generations' => self::MINIMUM_GENERATIONS,
17426684e68SGreg Roach            'module_name'         => $this->name(),
175e539f5c6SGreg Roach            'show_cousins'        => $show_cousins,
176e539f5c6SGreg Roach            'title'               => $this->chartTitle($individual),
177e539f5c6SGreg Roach        ]);
178e539f5c6SGreg Roach    }
179e539f5c6SGreg Roach
180e539f5c6SGreg Roach    /**
181e539f5c6SGreg Roach     * Show a hierarchical list of ancestors
182e539f5c6SGreg Roach     *
183e539f5c6SGreg Roach     * @TODO replace ob_start() with views.
184e539f5c6SGreg Roach     *
185e539f5c6SGreg Roach     * @param Individual $individual
186e539f5c6SGreg Roach     * @param int        $generations
187e539f5c6SGreg Roach     *
188e539f5c6SGreg Roach     * @return Response
189e539f5c6SGreg Roach     */
190e539f5c6SGreg Roach    protected function ancestorsList(Individual $individual, int $generations): Response
191e539f5c6SGreg Roach    {
192e539f5c6SGreg Roach        ob_start();
193e539f5c6SGreg Roach
194e539f5c6SGreg Roach        $this->printChildAscendancy($individual, 1, $generations - 1);
195e539f5c6SGreg Roach
196e539f5c6SGreg Roach        $html = ob_get_clean();
197e539f5c6SGreg Roach
198242a7862SGreg Roach        $html = '<ul class="wt-ancestors-chart-list list-unstyled">' . $html . '</ul>';
199e539f5c6SGreg Roach
200e539f5c6SGreg Roach        return new Response($html);
201e539f5c6SGreg Roach    }
202e539f5c6SGreg Roach
203e539f5c6SGreg Roach    /**
204e539f5c6SGreg Roach     * print a child ascendancy
205e539f5c6SGreg Roach     *
206e539f5c6SGreg Roach     * @param Individual $individual
207e539f5c6SGreg Roach     * @param int        $sosa
208e539f5c6SGreg Roach     * @param int        $generations
209e539f5c6SGreg Roach     *
210e539f5c6SGreg Roach     * @return void
211e539f5c6SGreg Roach     */
212e539f5c6SGreg Roach    protected function printChildAscendancy(Individual $individual, $sosa, $generations)
213e539f5c6SGreg Roach    {
214242a7862SGreg Roach        echo '<li class="wt-chart-ancestors-list-item">';
215e539f5c6SGreg Roach        echo '<table><tbody><tr><td>';
216e539f5c6SGreg Roach        if ($sosa === 1) {
217e837ff07SGreg Roach            echo '<img src="', e(asset('css/images/spacer.png')), '" height="3" width="15"></td><td>';
218e539f5c6SGreg Roach        } else {
219e837ff07SGreg Roach            echo '<img src="', e(asset('css/images/spacer.png')), '" height="3" width="2">';
220e837ff07SGreg Roach            echo '<img src="', e(asset('css/images/hline.png')), '" height="3" width="13"></td><td>';
221e539f5c6SGreg Roach        }
222e539f5c6SGreg Roach        echo FunctionsPrint::printPedigreePerson($individual);
223e539f5c6SGreg Roach        echo '</td><td>';
224e539f5c6SGreg Roach        if ($sosa > 1) {
22539b853a7SGreg Roach            echo '<a href="' . e($this->chartUrl($individual, ['generations' => $generations, 'chart_style' => self::CHART_STYLE_LIST])) . '" title="' . strip_tags($this->chartTitle($individual)) . '">' . view('icons/arrow-down') . '<span class="sr-only">' . $this->chartTitle($individual) . '</span></a>';
226e539f5c6SGreg Roach        }
22739ca88baSGreg Roach        echo '</td><td class="details1">&nbsp;<span class="wt-chart-box wt-chart-box-' . ($sosa === 1 ? strtolower($individual->sex()) : ($sosa % 2 ? 'f' : 'm')) . '">', I18N::number($sosa), '</span> ';
228e539f5c6SGreg Roach        echo '</td><td class="details1">&nbsp;', FunctionsCharts::getSosaName($sosa), '</td>';
229e539f5c6SGreg Roach        echo '</tr></tbody></table>';
230e539f5c6SGreg Roach
231e539f5c6SGreg Roach        // Parents
23239ca88baSGreg Roach        $family = $individual->primaryChildFamily();
233e539f5c6SGreg Roach        if ($family && $generations > 0) {
234e539f5c6SGreg Roach            // Marriage details
235e539f5c6SGreg Roach            echo '<span class="details1">';
236e837ff07SGreg Roach            echo '<img src="', e(asset('css/images/spacer.png')), '" height="2" width="15"><a href="#" onclick="return expand_layer(\'sosa_', $sosa, '\');" class="top"><i id="sosa_', $sosa, '_img" class="icon-minus" title="', I18N::translate('View this family'), '"></i></a>';
237242a7862SGreg Roach            echo ' <span class="wt-chart-box wt-chart-box-m">', I18N::number($sosa * 2), '</span> ';
238242a7862SGreg Roach            echo I18N::translate('and');
239242a7862SGreg Roach            echo ' <span class="wt-chart-box wt-chart-box-f">', I18N::number($sosa * 2 + 1), '</span>';
240e539f5c6SGreg Roach            if ($family->canShow()) {
241e539f5c6SGreg Roach                foreach ($family->facts(Gedcom::MARRIAGE_EVENTS) as $fact) {
242e539f5c6SGreg Roach                    echo ' <a href="', e($family->url()), '" class="details1">', $fact->summary(), '</a>';
243e539f5c6SGreg Roach                }
244e539f5c6SGreg Roach            }
245e539f5c6SGreg Roach            echo '</span>';
246242a7862SGreg Roach            echo '<ul class="wt-chart-ancestors-list list-unstyled" id="sosa_', $sosa, '">';
24739ca88baSGreg Roach            if ($family->husband()) {
24839ca88baSGreg Roach                $this->printChildAscendancy($family->husband(), $sosa * 2, $generations - 1);
249e539f5c6SGreg Roach            }
25039ca88baSGreg Roach            if ($family->wife()) {
25139ca88baSGreg Roach                $this->printChildAscendancy($family->wife(), $sosa * 2 + 1, $generations - 1);
252e539f5c6SGreg Roach            }
253e539f5c6SGreg Roach            echo '</ul>';
254e539f5c6SGreg Roach        }
255e539f5c6SGreg Roach        echo '</li>';
256e539f5c6SGreg Roach    }
257e539f5c6SGreg Roach
258e539f5c6SGreg Roach    /**
259e539f5c6SGreg Roach     * Show a tabular list of individual ancestors.
260e539f5c6SGreg Roach     *
261e539f5c6SGreg Roach     * @param Tree       $tree
262e539f5c6SGreg Roach     * @param Collection $ancestors
263e539f5c6SGreg Roach     *
264e539f5c6SGreg Roach     * @return Response
265e539f5c6SGreg Roach     */
266e539f5c6SGreg Roach    protected function ancestorsIndividuals(Tree $tree, Collection $ancestors): Response
267e539f5c6SGreg Roach    {
268e539f5c6SGreg Roach        $this->layout = 'layouts/ajax';
269e539f5c6SGreg Roach
270e539f5c6SGreg Roach        return $this->viewResponse('lists/individuals-table', [
271e539f5c6SGreg Roach            'individuals' => $ancestors,
272e539f5c6SGreg Roach            'sosa'        => true,
273e539f5c6SGreg Roach            'tree'        => $tree,
274e539f5c6SGreg Roach        ]);
275e539f5c6SGreg Roach    }
276e539f5c6SGreg Roach
277e539f5c6SGreg Roach    /**
278e539f5c6SGreg Roach     * Show a tabular list of individual ancestors.
279e539f5c6SGreg Roach     *
280e539f5c6SGreg Roach     * @param Tree       $tree
281e539f5c6SGreg Roach     * @param Collection $ancestors
282e539f5c6SGreg Roach     *
283e539f5c6SGreg Roach     * @return Response
284e539f5c6SGreg Roach     */
285e539f5c6SGreg Roach    protected function ancestorsFamilies(Tree $tree, Collection $ancestors): Response
286e539f5c6SGreg Roach    {
287e539f5c6SGreg Roach        $this->layout = 'layouts/ajax';
288e539f5c6SGreg Roach
289e539f5c6SGreg Roach        $families = [];
290e539f5c6SGreg Roach        foreach ($ancestors as $individual) {
29139ca88baSGreg Roach            foreach ($individual->childFamilies() as $family) {
292e539f5c6SGreg Roach                $families[$family->xref()] = $family;
293e539f5c6SGreg Roach            }
294e539f5c6SGreg Roach        }
295e539f5c6SGreg Roach
296e539f5c6SGreg Roach        return $this->viewResponse('lists/families-table', [
297e539f5c6SGreg Roach            'families' => $families,
298e539f5c6SGreg Roach            'tree'     => $tree,
299e539f5c6SGreg Roach        ]);
300e539f5c6SGreg Roach    }
301e539f5c6SGreg Roach
302e539f5c6SGreg Roach    /**
303e539f5c6SGreg Roach     * Show a booklet view of ancestors
304e539f5c6SGreg Roach     *
305e539f5c6SGreg Roach     * @TODO replace ob_start() with views.
306e539f5c6SGreg Roach     *
307e539f5c6SGreg Roach     * @param Collection $ancestors
308e539f5c6SGreg Roach     * @param bool       $show_cousins
309e539f5c6SGreg Roach     *
310e539f5c6SGreg Roach     * @return Response
311e539f5c6SGreg Roach     */
312e539f5c6SGreg Roach    protected function ancestorsBooklet(Collection $ancestors, bool $show_cousins): Response
313e539f5c6SGreg Roach    {
314e539f5c6SGreg Roach        ob_start();
315e539f5c6SGreg Roach
316e539f5c6SGreg Roach        echo FunctionsPrint::printPedigreePerson($ancestors[1]);
317e539f5c6SGreg Roach        foreach ($ancestors as $sosa => $individual) {
31839ca88baSGreg Roach            foreach ($individual->childFamilies() as $family) {
319e539f5c6SGreg Roach                FunctionsCharts::printSosaFamily($family, $individual->xref(), $sosa, '', '', '', $show_cousins);
320e539f5c6SGreg Roach            }
321e539f5c6SGreg Roach        }
322e539f5c6SGreg Roach
323e539f5c6SGreg Roach        $html = ob_get_clean();
324e539f5c6SGreg Roach
325e539f5c6SGreg Roach        return new Response($html);
326e539f5c6SGreg Roach    }
327e539f5c6SGreg Roach
328e539f5c6SGreg Roach    /**
329e539f5c6SGreg Roach     * This chart can display its output in a number of styles
330e539f5c6SGreg Roach     *
331e539f5c6SGreg Roach     * @return array
332e539f5c6SGreg Roach     */
333e539f5c6SGreg Roach    protected function chartStyles(): array
334e539f5c6SGreg Roach    {
335e539f5c6SGreg Roach        return [
336e539f5c6SGreg Roach            self::CHART_STYLE_LIST        => I18N::translate('List'),
337e539f5c6SGreg Roach            self::CHART_STYLE_BOOKLET     => I18N::translate('Booklet'),
338e539f5c6SGreg Roach            self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'),
339e539f5c6SGreg Roach            self::CHART_STYLE_FAMILIES    => I18N::translate('Families'),
340e539f5c6SGreg Roach        ];
341e539f5c6SGreg Roach    }
342168ff6f3Sric2016}
343