xref: /webtrees/app/Module/AncestorsChartModule.php (revision d84d39881110b925b29db08735fe45f4c397aab8)
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;
21e539f5c6SGreg Roachuse Fisharebest\Webtrees\FontAwesome;
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\Theme;
30e539f5c6SGreg Roachuse Fisharebest\Webtrees\Tree;
31e539f5c6SGreg Roachuse Illuminate\Support\Collection;
32e539f5c6SGreg Roachuse Symfony\Component\HttpFoundation\Request;
33e539f5c6SGreg Roachuse Symfony\Component\HttpFoundation\Response;
34168ff6f3Sric2016
35168ff6f3Sric2016/**
36168ff6f3Sric2016 * Class AncestorsChartModule
37168ff6f3Sric2016 */
3849a243cbSGreg Roachclass AncestorsChartModule extends AbstractModule implements ModuleInterface, ModuleChartInterface
39c1010edaSGreg Roach{
4049a243cbSGreg Roach    use ModuleChartTrait;
4149a243cbSGreg Roach
42e539f5c6SGreg Roach    // Chart styles
43e539f5c6SGreg Roach    protected const CHART_STYLE_LIST        = 'list';
44e539f5c6SGreg Roach    protected const CHART_STYLE_BOOKLET     = 'booklet';
45e539f5c6SGreg Roach    protected const CHART_STYLE_INDIVIDUALS = 'individuals';
46e539f5c6SGreg Roach    protected const CHART_STYLE_FAMILIES    = 'families';
47e539f5c6SGreg Roach
48e539f5c6SGreg Roach    // Defaults
49e539f5c6SGreg Roach    protected const DEFAULT_COUSINS             = false;
50e539f5c6SGreg Roach    protected const DEFAULT_STYLE               = self::CHART_STYLE_LIST;
51e539f5c6SGreg Roach    protected const DEFAULT_GENERATIONS         = '3';
52e539f5c6SGreg Roach    protected const DEFAULT_MAXIMUM_GENERATIONS = '9';
53e539f5c6SGreg Roach
54168ff6f3Sric2016    /**
55168ff6f3Sric2016     * How should this module be labelled on tabs, menus, etc.?
56168ff6f3Sric2016     *
57168ff6f3Sric2016     * @return string
58168ff6f3Sric2016     */
5949a243cbSGreg Roach    public function title(): string
60c1010edaSGreg Roach    {
61bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
62bbb76c12SGreg Roach        return I18N::translate('Ancestors');
63168ff6f3Sric2016    }
64168ff6f3Sric2016
65168ff6f3Sric2016    /**
66168ff6f3Sric2016     * A sentence describing what this module does.
67168ff6f3Sric2016     *
68168ff6f3Sric2016     * @return string
69168ff6f3Sric2016     */
7049a243cbSGreg Roach    public function description(): string
71c1010edaSGreg Roach    {
72bbb76c12SGreg Roach        /* I18N: Description of the “AncestorsChart” module */
73bbb76c12SGreg Roach        return I18N::translate('A chart of an individual’s ancestors.');
74168ff6f3Sric2016    }
75168ff6f3Sric2016
76168ff6f3Sric2016    /**
77377a2979SGreg Roach     * CSS class for the URL.
78377a2979SGreg Roach     *
79377a2979SGreg Roach     * @return string
80377a2979SGreg Roach     */
81377a2979SGreg Roach    public function chartMenuClass(): string
82377a2979SGreg Roach    {
83377a2979SGreg Roach        return 'menu-chart-ancestry';
84377a2979SGreg Roach    }
85377a2979SGreg Roach
86377a2979SGreg Roach    /**
874eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
884eb71cfaSGreg Roach     *
8960bc3e3fSGreg Roach     * @param Individual $individual
9060bc3e3fSGreg Roach     *
914eb71cfaSGreg Roach     * @return Menu|null
924eb71cfaSGreg Roach     */
93377a2979SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
94c1010edaSGreg Roach    {
95e6562982SGreg Roach        return $this->chartMenu($individual);
96e6562982SGreg Roach    }
97e6562982SGreg Roach
98e6562982SGreg Roach    /**
99e6562982SGreg Roach     * The title for a specific instance of this chart.
100e6562982SGreg Roach     *
101e6562982SGreg Roach     * @param Individual $individual
102e6562982SGreg Roach     *
103e6562982SGreg Roach     * @return string
104e6562982SGreg Roach     */
105e6562982SGreg Roach    public function chartTitle(Individual $individual): string
106e6562982SGreg Roach    {
107e6562982SGreg Roach        /* I18N: %s is an individual’s name */
108e6562982SGreg Roach        return I18N::translate('Ancestors of %s', $individual->getFullName());
109e6562982SGreg Roach    }
110e6562982SGreg Roach
111e6562982SGreg Roach    /**
112e539f5c6SGreg Roach     * A form to request the chart parameters.
113e539f5c6SGreg Roach     *
114e539f5c6SGreg Roach     * @param Request      $request
115e539f5c6SGreg Roach     * @param Tree         $tree
116e539f5c6SGreg Roach     * @param ChartService $chart_service
117e539f5c6SGreg Roach     *
118e539f5c6SGreg Roach     * @return Response
119e539f5c6SGreg Roach     */
120e539f5c6SGreg Roach    public function getChartAction(Request $request, Tree $tree, ChartService $chart_service): Response
121e539f5c6SGreg Roach    {
122*d84d3988SGreg Roach        $ajax       = $request->get('ajax');
123e539f5c6SGreg Roach        $xref       = $request->get('xref', '');
124e539f5c6SGreg Roach        $individual = Individual::getInstance($xref, $tree);
125e539f5c6SGreg Roach
126e539f5c6SGreg Roach        Auth::checkIndividualAccess($individual);
127e539f5c6SGreg Roach
128e539f5c6SGreg Roach        $minimum_generations = 2;
129e539f5c6SGreg Roach        $maximum_generations = (int) $tree->getPreference('MAX_PEDIGREE_GENERATIONS', self::DEFAULT_MAXIMUM_GENERATIONS);
130e539f5c6SGreg Roach        $default_generations = (int) $tree->getPreference('DEFAULT_PEDIGREE_GENERATIONS', self::DEFAULT_GENERATIONS);
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);
134e539f5c6SGreg Roach        $generations  = (int) $request->get('generations', $default_generations);
135e539f5c6SGreg Roach
136e539f5c6SGreg Roach        $generations = min($generations, $maximum_generations);
137e539f5c6SGreg Roach        $generations = max($generations, $minimum_generations);
138e539f5c6SGreg Roach
139e539f5c6SGreg Roach        if ($ajax === '1') {
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
158*d84d3988SGreg Roach        return $this->viewResponse('modules/ancestors-chart/chart-page', [
159e539f5c6SGreg Roach            'chart_style'         => $chart_style,
160e539f5c6SGreg Roach            'chart_styles'        => $this->chartStyles(),
161e539f5c6SGreg Roach            'default_generations' => $default_generations,
162e539f5c6SGreg Roach            'generations'         => $generations,
163e539f5c6SGreg Roach            'individual'          => $individual,
164e539f5c6SGreg Roach            'maximum_generations' => $maximum_generations,
165e539f5c6SGreg Roach            'minimum_generations' => $minimum_generations,
166e539f5c6SGreg Roach            'module'              => $this,
167e539f5c6SGreg Roach            'show_cousins'        => $show_cousins,
168e539f5c6SGreg Roach            'title'               => $this->chartTitle($individual),
169e539f5c6SGreg Roach        ]);
170e539f5c6SGreg Roach    }
171e539f5c6SGreg Roach
172e539f5c6SGreg Roach    /**
173e539f5c6SGreg Roach     * Show a hierarchical list of ancestors
174e539f5c6SGreg Roach     *
175e539f5c6SGreg Roach     * @TODO replace ob_start() with views.
176e539f5c6SGreg Roach     *
177e539f5c6SGreg Roach     * @param Individual $individual
178e539f5c6SGreg Roach     * @param int        $generations
179e539f5c6SGreg Roach     *
180e539f5c6SGreg Roach     * @return Response
181e539f5c6SGreg Roach     */
182e539f5c6SGreg Roach    protected function ancestorsList(Individual $individual, int $generations): Response
183e539f5c6SGreg Roach    {
184e539f5c6SGreg Roach        ob_start();
185e539f5c6SGreg Roach
186e539f5c6SGreg Roach        $this->printChildAscendancy($individual, 1, $generations - 1);
187e539f5c6SGreg Roach
188e539f5c6SGreg Roach        $html = ob_get_clean();
189e539f5c6SGreg Roach
190e539f5c6SGreg Roach        $html = '<ul class="chart_common">' . $html . '</ul>';
191e539f5c6SGreg Roach
192e539f5c6SGreg Roach        return new Response($html);
193e539f5c6SGreg Roach    }
194e539f5c6SGreg Roach
195e539f5c6SGreg Roach    /**
196e539f5c6SGreg Roach     * print a child ascendancy
197e539f5c6SGreg Roach     *
198e539f5c6SGreg Roach     * @param Individual $individual
199e539f5c6SGreg Roach     * @param int        $sosa
200e539f5c6SGreg Roach     * @param int        $generations
201e539f5c6SGreg Roach     *
202e539f5c6SGreg Roach     * @return void
203e539f5c6SGreg Roach     */
204e539f5c6SGreg Roach    protected function printChildAscendancy(Individual $individual, $sosa, $generations)
205e539f5c6SGreg Roach    {
206e539f5c6SGreg Roach        echo '<li class="wt-ancestors-chart-list-item">';
207e539f5c6SGreg Roach        echo '<table><tbody><tr><td>';
208e539f5c6SGreg Roach        if ($sosa === 1) {
209e539f5c6SGreg Roach            echo '<img src="', Theme::theme()->parameter('image-spacer'), '" height="3" width="15"></td><td>';
210e539f5c6SGreg Roach        } else {
211e539f5c6SGreg Roach            echo '<img src="', Theme::theme()->parameter('image-spacer'), '" height="3" width="2">';
212e539f5c6SGreg Roach            echo '<img src="', Theme::theme()->parameter('image-hline'), '" height="3" width="13"></td><td>';
213e539f5c6SGreg Roach        }
214e539f5c6SGreg Roach        echo FunctionsPrint::printPedigreePerson($individual);
215e539f5c6SGreg Roach        echo '</td><td>';
216e539f5c6SGreg Roach        if ($sosa > 1) {
217e539f5c6SGreg Roach            echo FontAwesome::linkIcon('arrow-down', $this->chartTitle($individual), [
218e539f5c6SGreg Roach                'href' => $this->chartUrl($individual, [
219e539f5c6SGreg Roach                    'generations' => $generations,
220e539f5c6SGreg Roach                    'chart_style' => self::CHART_STYLE_LIST,
221e539f5c6SGreg Roach                ])
222e539f5c6SGreg Roach            ]);
223e539f5c6SGreg Roach        }
224e539f5c6SGreg Roach        echo '</td><td class="details1">&nbsp;<span class="person_box' . ($sosa === 1 ? 'NN' : ($sosa % 2 ? 'F' : '')) . '">', I18N::number($sosa), '</span> ';
225e539f5c6SGreg Roach        echo '</td><td class="details1">&nbsp;', FunctionsCharts::getSosaName($sosa), '</td>';
226e539f5c6SGreg Roach        echo '</tr></tbody></table>';
227e539f5c6SGreg Roach
228e539f5c6SGreg Roach        // Parents
229e539f5c6SGreg Roach        $family = $individual->getPrimaryChildFamily();
230e539f5c6SGreg Roach        if ($family && $generations > 0) {
231e539f5c6SGreg Roach            // Marriage details
232e539f5c6SGreg Roach            echo '<span class="details1">';
233e539f5c6SGreg Roach            echo '<img src="', Theme::theme()->parameter('image-spacer'), '" 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>';
234e539f5c6SGreg Roach            echo ' <span class="person_box">', I18N::number($sosa * 2), '</span> ', I18N::translate('and');
235e539f5c6SGreg Roach            echo ' <span class="person_boxF">', I18N::number($sosa * 2 + 1), '</span>';
236e539f5c6SGreg Roach            if ($family->canShow()) {
237e539f5c6SGreg Roach                foreach ($family->facts(Gedcom::MARRIAGE_EVENTS) as $fact) {
238e539f5c6SGreg Roach                    echo ' <a href="', e($family->url()), '" class="details1">', $fact->summary(), '</a>';
239e539f5c6SGreg Roach                }
240e539f5c6SGreg Roach            }
241e539f5c6SGreg Roach            echo '</span>';
242e539f5c6SGreg Roach            echo '<ul class="wt-ancestors-chart-list" id="sosa_', $sosa, '">';
243e539f5c6SGreg Roach            if ($family->getHusband()) {
244e539f5c6SGreg Roach                $this->printChildAscendancy($family->getHusband(), $sosa * 2, $generations - 1);
245e539f5c6SGreg Roach            }
246e539f5c6SGreg Roach            if ($family->getWife()) {
247e539f5c6SGreg Roach                $this->printChildAscendancy($family->getWife(), $sosa * 2 + 1, $generations - 1);
248e539f5c6SGreg Roach            }
249e539f5c6SGreg Roach            echo '</ul>';
250e539f5c6SGreg Roach        }
251e539f5c6SGreg Roach        echo '</li>';
252e539f5c6SGreg Roach    }
253e539f5c6SGreg Roach
254e539f5c6SGreg Roach    /**
255e539f5c6SGreg Roach     * Show a tabular list of individual ancestors.
256e539f5c6SGreg Roach     *
257e539f5c6SGreg Roach     * @param Tree       $tree
258e539f5c6SGreg Roach     * @param Collection $ancestors
259e539f5c6SGreg Roach     *
260e539f5c6SGreg Roach     * @return Response
261e539f5c6SGreg Roach     */
262e539f5c6SGreg Roach    protected function ancestorsIndividuals(Tree $tree, Collection $ancestors): Response
263e539f5c6SGreg Roach    {
264e539f5c6SGreg Roach        $this->layout = 'layouts/ajax';
265e539f5c6SGreg Roach
266e539f5c6SGreg Roach        return $this->viewResponse('lists/individuals-table', [
267e539f5c6SGreg Roach            'individuals' => $ancestors,
268e539f5c6SGreg Roach            'sosa'        => true,
269e539f5c6SGreg Roach            'tree'        => $tree,
270e539f5c6SGreg Roach        ]);
271e539f5c6SGreg Roach    }
272e539f5c6SGreg Roach
273e539f5c6SGreg Roach    /**
274e539f5c6SGreg Roach     * Show a tabular list of individual ancestors.
275e539f5c6SGreg Roach     *
276e539f5c6SGreg Roach     * @param Tree       $tree
277e539f5c6SGreg Roach     * @param Collection $ancestors
278e539f5c6SGreg Roach     *
279e539f5c6SGreg Roach     * @return Response
280e539f5c6SGreg Roach     */
281e539f5c6SGreg Roach    protected function ancestorsFamilies(Tree $tree, Collection $ancestors): Response
282e539f5c6SGreg Roach    {
283e539f5c6SGreg Roach        $this->layout = 'layouts/ajax';
284e539f5c6SGreg Roach
285e539f5c6SGreg Roach        $families = [];
286e539f5c6SGreg Roach        foreach ($ancestors as $individual) {
287e539f5c6SGreg Roach            foreach ($individual->getChildFamilies() as $family) {
288e539f5c6SGreg Roach                $families[$family->xref()] = $family;
289e539f5c6SGreg Roach            }
290e539f5c6SGreg Roach        }
291e539f5c6SGreg Roach
292e539f5c6SGreg Roach        return $this->viewResponse('lists/families-table', [
293e539f5c6SGreg Roach            'families' => $families,
294e539f5c6SGreg Roach            'tree'     => $tree,
295e539f5c6SGreg Roach        ]);
296e539f5c6SGreg Roach    }
297e539f5c6SGreg Roach
298e539f5c6SGreg Roach    /**
299e539f5c6SGreg Roach     * Show a booklet view of ancestors
300e539f5c6SGreg Roach     *
301e539f5c6SGreg Roach     * @TODO replace ob_start() with views.
302e539f5c6SGreg Roach     *
303e539f5c6SGreg Roach     * @param Collection $ancestors
304e539f5c6SGreg Roach     * @param bool       $show_cousins
305e539f5c6SGreg Roach     *
306e539f5c6SGreg Roach     * @return Response
307e539f5c6SGreg Roach     */
308e539f5c6SGreg Roach    protected function ancestorsBooklet(Collection $ancestors, bool $show_cousins): Response
309e539f5c6SGreg Roach    {
310e539f5c6SGreg Roach        ob_start();
311e539f5c6SGreg Roach
312e539f5c6SGreg Roach        echo FunctionsPrint::printPedigreePerson($ancestors[1]);
313e539f5c6SGreg Roach        foreach ($ancestors as $sosa => $individual) {
314e539f5c6SGreg Roach            foreach ($individual->getChildFamilies() as $family) {
315e539f5c6SGreg Roach                FunctionsCharts::printSosaFamily($family, $individual->xref(), $sosa, '', '', '', $show_cousins);
316e539f5c6SGreg Roach            }
317e539f5c6SGreg Roach        }
318e539f5c6SGreg Roach
319e539f5c6SGreg Roach        $html = ob_get_clean();
320e539f5c6SGreg Roach
321e539f5c6SGreg Roach        return new Response($html);
322e539f5c6SGreg Roach    }
323e539f5c6SGreg Roach
324e539f5c6SGreg Roach    /**
325e539f5c6SGreg Roach     * This chart can display its output in a number of styles
326e539f5c6SGreg Roach     *
327e539f5c6SGreg Roach     * @return array
328e539f5c6SGreg Roach     */
329e539f5c6SGreg Roach    protected function chartStyles(): array
330e539f5c6SGreg Roach    {
331e539f5c6SGreg Roach        return [
332e539f5c6SGreg Roach            self::CHART_STYLE_LIST        => I18N::translate('List'),
333e539f5c6SGreg Roach            self::CHART_STYLE_BOOKLET     => I18N::translate('Booklet'),
334e539f5c6SGreg Roach            self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'),
335e539f5c6SGreg Roach            self::CHART_STYLE_FAMILIES    => I18N::translate('Families'),
336e539f5c6SGreg Roach        ];
337e539f5c6SGreg Roach    }
338168ff6f3Sric2016}
339