xref: /webtrees/app/Module/AncestorsChartModule.php (revision 54452b04856df5056fa76d9764beb95110c35d02)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Auth;
21use Fisharebest\Webtrees\FontAwesome;
22use Fisharebest\Webtrees\Functions\FunctionsCharts;
23use Fisharebest\Webtrees\Functions\FunctionsPrint;
24use Fisharebest\Webtrees\Gedcom;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Individual;
27use Fisharebest\Webtrees\Menu;
28use Fisharebest\Webtrees\Services\ChartService;
29use Fisharebest\Webtrees\Theme;
30use Fisharebest\Webtrees\Tree;
31use Illuminate\Support\Collection;
32use Symfony\Component\HttpFoundation\Request;
33use Symfony\Component\HttpFoundation\Response;
34
35/**
36 * Class AncestorsChartModule
37 */
38class AncestorsChartModule extends AbstractModule implements ModuleInterface, ModuleChartInterface
39{
40    use ModuleChartTrait;
41
42    // Chart styles
43    protected const CHART_STYLE_LIST        = 'list';
44    protected const CHART_STYLE_BOOKLET     = 'booklet';
45    protected const CHART_STYLE_INDIVIDUALS = 'individuals';
46    protected const CHART_STYLE_FAMILIES    = 'families';
47
48    // Defaults
49    protected const DEFAULT_COUSINS             = false;
50    protected const DEFAULT_STYLE               = self::CHART_STYLE_LIST;
51    protected const DEFAULT_GENERATIONS         = '3';
52    protected const DEFAULT_MAXIMUM_GENERATIONS = '9';
53
54    /**
55     * How should this module be labelled on tabs, menus, etc.?
56     *
57     * @return string
58     */
59    public function title(): string
60    {
61        /* I18N: Name of a module/chart */
62        return I18N::translate('Ancestors');
63    }
64
65    /**
66     * A sentence describing what this module does.
67     *
68     * @return string
69     */
70    public function description(): string
71    {
72        /* I18N: Description of the “AncestorsChart” module */
73        return I18N::translate('A chart of an individual’s ancestors.');
74    }
75
76    /**
77     * CSS class for the URL.
78     *
79     * @return string
80     */
81    public function chartMenuClass(): string
82    {
83        return 'menu-chart-ancestry';
84    }
85
86    /**
87     * Return a menu item for this chart - for use in individual boxes.
88     *
89     * @param Individual $individual
90     *
91     * @return Menu|null
92     */
93    public function chartBoxMenu(Individual $individual): ?Menu
94    {
95        return $this->chartMenu($individual);
96    }
97
98    /**
99     * The title for a specific instance of this chart.
100     *
101     * @param Individual $individual
102     *
103     * @return string
104     */
105    public function chartTitle(Individual $individual): string
106    {
107        /* I18N: %s is an individual’s name */
108        return I18N::translate('Ancestors of %s', $individual->getFullName());
109    }
110
111    /**
112     * A form to request the chart parameters.
113     *
114     * @param Request      $request
115     * @param Tree         $tree
116     * @param ChartService $chart_service
117     *
118     * @return Response
119     */
120    public function getChartAction(Request $request, Tree $tree, ChartService $chart_service): Response
121    {
122        $ajax       = $request->get('ajax');
123        $xref       = $request->get('xref', '');
124        $individual = Individual::getInstance($xref, $tree);
125
126        Auth::checkIndividualAccess($individual);
127
128        $minimum_generations = 2;
129        $maximum_generations = (int) $tree->getPreference('MAX_PEDIGREE_GENERATIONS', self::DEFAULT_MAXIMUM_GENERATIONS);
130        $default_generations = (int) $tree->getPreference('DEFAULT_PEDIGREE_GENERATIONS', self::DEFAULT_GENERATIONS);
131
132        $show_cousins = (bool) $request->get('show_cousins', self::DEFAULT_COUSINS);
133        $chart_style  = $request->get('chart_style', self::DEFAULT_STYLE);
134        $generations  = (int) $request->get('generations', $default_generations);
135
136        $generations = min($generations, $maximum_generations);
137        $generations = max($generations, $minimum_generations);
138
139        if ($ajax === '1') {
140            $ancestors = $chart_service->sosaStradonitzAncestors($individual, $generations);
141
142            switch ($chart_style) {
143                default:
144                case self::CHART_STYLE_LIST:
145                    return $this->ancestorsList($individual, $generations);
146
147                case self::CHART_STYLE_BOOKLET:
148                    return $this->ancestorsBooklet($ancestors, $show_cousins);
149
150                case self::CHART_STYLE_INDIVIDUALS:
151                    return $this->ancestorsIndividuals($tree, $ancestors);
152
153                case self::CHART_STYLE_FAMILIES:
154                    return $this->ancestorsFamilies($tree, $ancestors);
155            }
156        }
157
158        $ajax_url = $this->chartUrl($individual, [
159            'generations'  => $generations,
160            'chart_style'  => $chart_style,
161            'show_cousins' => $show_cousins,
162            'ajax'         => '1',
163        ]);
164
165        return $this->viewResponse('modules/ancestors-chart/chart-page', [
166            'ajax_url'            => $ajax_url,
167            'chart_style'         => $chart_style,
168            'chart_styles'        => $this->chartStyles(),
169            'default_generations' => $default_generations,
170            'generations'         => $generations,
171            'individual'          => $individual,
172            'maximum_generations' => $maximum_generations,
173            'minimum_generations' => $minimum_generations,
174            'show_cousins'        => $show_cousins,
175            'title'               => $this->chartTitle($individual),
176        ]);
177    }
178
179    /**
180     * Show a hierarchical list of ancestors
181     *
182     * @TODO replace ob_start() with views.
183     *
184     * @param Individual $individual
185     * @param int        $generations
186     *
187     * @return Response
188     */
189    protected function ancestorsList(Individual $individual, int $generations): Response
190    {
191        ob_start();
192
193        $this->printChildAscendancy($individual, 1, $generations - 1);
194
195        $html = ob_get_clean();
196
197        $html = '<ul class="chart_common">' . $html . '</ul>';
198
199        return new Response($html);
200    }
201
202    /**
203     * print a child ascendancy
204     *
205     * @param Individual $individual
206     * @param int        $sosa
207     * @param int        $generations
208     *
209     * @return void
210     */
211    protected function printChildAscendancy(Individual $individual, $sosa, $generations)
212    {
213        echo '<li class="wt-ancestors-chart-list-item">';
214        echo '<table><tbody><tr><td>';
215        if ($sosa === 1) {
216            echo '<img src="', Theme::theme()->parameter('image-spacer'), '" height="3" width="15"></td><td>';
217        } else {
218            echo '<img src="', Theme::theme()->parameter('image-spacer'), '" height="3" width="2">';
219            echo '<img src="', Theme::theme()->parameter('image-hline'), '" height="3" width="13"></td><td>';
220        }
221        echo FunctionsPrint::printPedigreePerson($individual);
222        echo '</td><td>';
223        if ($sosa > 1) {
224            echo FontAwesome::linkIcon('arrow-down', $this->chartTitle($individual), [
225                'href' => $this->chartUrl($individual, [
226                    'generations' => $generations,
227                    'chart_style' => self::CHART_STYLE_LIST,
228                ])
229            ]);
230        }
231        echo '</td><td class="details1">&nbsp;<span class="person_box' . ($sosa === 1 ? 'NN' : ($sosa % 2 ? 'F' : '')) . '">', I18N::number($sosa), '</span> ';
232        echo '</td><td class="details1">&nbsp;', FunctionsCharts::getSosaName($sosa), '</td>';
233        echo '</tr></tbody></table>';
234
235        // Parents
236        $family = $individual->getPrimaryChildFamily();
237        if ($family && $generations > 0) {
238            // Marriage details
239            echo '<span class="details1">';
240            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>';
241            echo ' <span class="person_box">', I18N::number($sosa * 2), '</span> ', I18N::translate('and');
242            echo ' <span class="person_boxF">', I18N::number($sosa * 2 + 1), '</span>';
243            if ($family->canShow()) {
244                foreach ($family->facts(Gedcom::MARRIAGE_EVENTS) as $fact) {
245                    echo ' <a href="', e($family->url()), '" class="details1">', $fact->summary(), '</a>';
246                }
247            }
248            echo '</span>';
249            echo '<ul class="wt-ancestors-chart-list" id="sosa_', $sosa, '">';
250            if ($family->getHusband()) {
251                $this->printChildAscendancy($family->getHusband(), $sosa * 2, $generations - 1);
252            }
253            if ($family->getWife()) {
254                $this->printChildAscendancy($family->getWife(), $sosa * 2 + 1, $generations - 1);
255            }
256            echo '</ul>';
257        }
258        echo '</li>';
259    }
260
261    /**
262     * Show a tabular list of individual ancestors.
263     *
264     * @param Tree       $tree
265     * @param Collection $ancestors
266     *
267     * @return Response
268     */
269    protected function ancestorsIndividuals(Tree $tree, Collection $ancestors): Response
270    {
271        $this->layout = 'layouts/ajax';
272
273        return $this->viewResponse('lists/individuals-table', [
274            'individuals' => $ancestors,
275            'sosa'        => true,
276            'tree'        => $tree,
277        ]);
278    }
279
280    /**
281     * Show a tabular list of individual ancestors.
282     *
283     * @param Tree       $tree
284     * @param Collection $ancestors
285     *
286     * @return Response
287     */
288    protected function ancestorsFamilies(Tree $tree, Collection $ancestors): Response
289    {
290        $this->layout = 'layouts/ajax';
291
292        $families = [];
293        foreach ($ancestors as $individual) {
294            foreach ($individual->getChildFamilies() as $family) {
295                $families[$family->xref()] = $family;
296            }
297        }
298
299        return $this->viewResponse('lists/families-table', [
300            'families' => $families,
301            'tree'     => $tree,
302        ]);
303    }
304
305    /**
306     * Show a booklet view of ancestors
307     *
308     * @TODO replace ob_start() with views.
309     *
310     * @param Collection $ancestors
311     * @param bool       $show_cousins
312     *
313     * @return Response
314     */
315    protected function ancestorsBooklet(Collection $ancestors, bool $show_cousins): Response
316    {
317        ob_start();
318
319        echo FunctionsPrint::printPedigreePerson($ancestors[1]);
320        foreach ($ancestors as $sosa => $individual) {
321            foreach ($individual->getChildFamilies() as $family) {
322                FunctionsCharts::printSosaFamily($family, $individual->xref(), $sosa, '', '', '', $show_cousins);
323            }
324        }
325
326        $html = ob_get_clean();
327
328        return new Response($html);
329    }
330
331    /**
332     * This chart can display its output in a number of styles
333     *
334     * @return array
335     */
336    protected function chartStyles(): array
337    {
338        return [
339            self::CHART_STYLE_LIST        => I18N::translate('List'),
340            self::CHART_STYLE_BOOKLET     => I18N::translate('Booklet'),
341            self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'),
342            self::CHART_STYLE_FAMILIES    => I18N::translate('Families'),
343        ];
344    }
345}
346