xref: /webtrees/app/Module/AncestorsChartModule.php (revision fdd33a18fa900157181a853e2b3bd83a6db5aedd)
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\Contracts\UserInterface;
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\Tree;
30use Illuminate\Support\Collection;
31use Symfony\Component\HttpFoundation\Request;
32use Symfony\Component\HttpFoundation\Response;
33
34/**
35 * Class AncestorsChartModule
36 */
37class AncestorsChartModule extends AbstractModule implements ModuleChartInterface
38{
39    use ModuleChartTrait;
40
41    // Chart styles
42    protected const CHART_STYLE_LIST        = 'list';
43    protected const CHART_STYLE_BOOKLET     = 'booklet';
44    protected const CHART_STYLE_INDIVIDUALS = 'individuals';
45    protected const CHART_STYLE_FAMILIES    = 'families';
46
47    // Defaults
48    protected const DEFAULT_COUSINS             = false;
49    protected const DEFAULT_STYLE               = self::CHART_STYLE_LIST;
50    protected const DEFAULT_GENERATIONS         = '4';
51
52    // Limits
53    protected const MINIMUM_GENERATIONS = 2;
54    protected const MAXIMUM_GENERATIONS = 10;
55
56    /**
57     * How should this module be identified in the control panel, etc.?
58     *
59     * @return string
60     */
61    public function title(): string
62    {
63        /* I18N: Name of a module/chart */
64        return I18N::translate('Ancestors');
65    }
66
67    /**
68     * A sentence describing what this module does.
69     *
70     * @return string
71     */
72    public function description(): string
73    {
74        /* I18N: Description of the “AncestorsChart” module */
75        return I18N::translate('A chart of an individual’s ancestors.');
76    }
77
78    /**
79     * CSS class for the URL.
80     *
81     * @return string
82     */
83    public function chartMenuClass(): string
84    {
85        return 'menu-chart-ancestry';
86    }
87
88    /**
89     * Return a menu item for this chart - for use in individual boxes.
90     *
91     * @param Individual $individual
92     *
93     * @return Menu|null
94     */
95    public function chartBoxMenu(Individual $individual): ?Menu
96    {
97        return $this->chartMenu($individual);
98    }
99
100    /**
101     * The title for a specific instance of this chart.
102     *
103     * @param Individual $individual
104     *
105     * @return string
106     */
107    public function chartTitle(Individual $individual): string
108    {
109        /* I18N: %s is an individual’s name */
110        return I18N::translate('Ancestors of %s', $individual->fullName());
111    }
112
113    /**
114     * A form to request the chart parameters.
115     *
116     * @param Request       $request
117     * @param Tree          $tree
118     * @param UserInterface $user
119     * @param ChartService  $chart_service
120     *
121     * @return Response
122     */
123    public function getChartAction(Request $request, Tree $tree, UserInterface $user, ChartService $chart_service): Response
124    {
125        $ajax       = (bool) $request->get('ajax');
126        $xref       = $request->get('xref', '');
127        $individual = Individual::getInstance($xref, $tree);
128
129        Auth::checkIndividualAccess($individual);
130        Auth::checkComponentAccess($this, 'chart', $tree, $user);
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', self::DEFAULT_GENERATIONS);
135
136        $generations = min($generations, self::MAXIMUM_GENERATIONS);
137        $generations = max($generations, self::MINIMUM_GENERATIONS);
138
139        if ($ajax) {
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'         => true,
163        ]);
164
165        return $this->viewResponse('modules/ancestors-chart/page', [
166            'ajax_url'            => $ajax_url,
167            'chart_style'         => $chart_style,
168            'chart_styles'        => $this->chartStyles(),
169            'default_generations' => self::DEFAULT_GENERATIONS,
170            'generations'         => $generations,
171            'individual'          => $individual,
172            'maximum_generations' => self::MAXIMUM_GENERATIONS,
173            'minimum_generations' => self::MINIMUM_GENERATIONS,
174            'module_name'         => $this->name(),
175            'show_cousins'        => $show_cousins,
176            'title'               => $this->chartTitle($individual),
177        ]);
178    }
179
180    /**
181     * Show a hierarchical list of ancestors
182     *
183     * @TODO replace ob_start() with views.
184     *
185     * @param Individual $individual
186     * @param int        $generations
187     *
188     * @return Response
189     */
190    protected function ancestorsList(Individual $individual, int $generations): Response
191    {
192        ob_start();
193
194        $this->printChildAscendancy($individual, 1, $generations - 1);
195
196        $html = ob_get_clean();
197
198        $html = '<ul class="wt-ancestors-chart-list list-unstyled">' . $html . '</ul>';
199
200        return new Response($html);
201    }
202
203    /**
204     * print a child ascendancy
205     *
206     * @param Individual $individual
207     * @param int        $sosa
208     * @param int        $generations
209     *
210     * @return void
211     */
212    protected function printChildAscendancy(Individual $individual, $sosa, $generations): void
213    {
214        echo '<li class="wt-chart-ancestors-list-item">';
215        echo '<table><tbody><tr><td>';
216        if ($sosa === 1) {
217            echo '<img src="', e(asset('css/images/spacer.png')), '" height="3" width="15"></td><td>';
218        } else {
219            echo '<img src="', e(asset('css/images/spacer.png')), '" height="3" width="2">';
220            echo '<img src="', e(asset('css/images/hline.png')), '" height="3" width="13"></td><td>';
221        }
222        echo FunctionsPrint::printPedigreePerson($individual);
223        echo '</td><td>';
224        if ($sosa > 1) {
225            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>';
226        }
227        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> ';
228        echo '</td><td class="details1">&nbsp;', FunctionsCharts::getSosaName($sosa), '</td>';
229        echo '</tr></tbody></table>';
230
231        // Parents
232        $family = $individual->primaryChildFamily();
233        if ($family && $generations > 0) {
234            // Marriage details
235            echo '<span class="details1">';
236            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>';
237            echo ' <span class="wt-chart-box wt-chart-box-m">', I18N::number($sosa * 2), '</span> ';
238            echo I18N::translate('and');
239            echo ' <span class="wt-chart-box wt-chart-box-f">', I18N::number($sosa * 2 + 1), '</span>';
240            if ($family->canShow()) {
241                foreach ($family->facts(Gedcom::MARRIAGE_EVENTS) as $fact) {
242                    echo ' <a href="', e($family->url()), '" class="details1">', $fact->summary(), '</a>';
243                }
244            }
245            echo '</span>';
246            echo '<ul class="wt-chart-ancestors-list list-unstyled" id="sosa_', $sosa, '">';
247            if ($family->husband()) {
248                $this->printChildAscendancy($family->husband(), $sosa * 2, $generations - 1);
249            }
250            if ($family->wife()) {
251                $this->printChildAscendancy($family->wife(), $sosa * 2 + 1, $generations - 1);
252            }
253            echo '</ul>';
254        }
255        echo '</li>';
256    }
257
258    /**
259     * Show a tabular list of individual ancestors.
260     *
261     * @param Tree       $tree
262     * @param Collection $ancestors
263     *
264     * @return Response
265     */
266    protected function ancestorsIndividuals(Tree $tree, Collection $ancestors): Response
267    {
268        $this->layout = 'layouts/ajax';
269
270        return $this->viewResponse('lists/individuals-table', [
271            'individuals' => $ancestors,
272            'sosa'        => true,
273            'tree'        => $tree,
274        ]);
275    }
276
277    /**
278     * Show a tabular list of individual ancestors.
279     *
280     * @param Tree       $tree
281     * @param Collection $ancestors
282     *
283     * @return Response
284     */
285    protected function ancestorsFamilies(Tree $tree, Collection $ancestors): Response
286    {
287        $this->layout = 'layouts/ajax';
288
289        $families = [];
290        foreach ($ancestors as $individual) {
291            foreach ($individual->childFamilies() as $family) {
292                $families[$family->xref()] = $family;
293            }
294        }
295
296        return $this->viewResponse('lists/families-table', [
297            'families' => $families,
298            'tree'     => $tree,
299        ]);
300    }
301
302    /**
303     * Show a booklet view of ancestors
304     *
305     * @TODO replace ob_start() with views.
306     *
307     * @param Collection $ancestors
308     * @param bool       $show_cousins
309     *
310     * @return Response
311     */
312    protected function ancestorsBooklet(Collection $ancestors, bool $show_cousins): Response
313    {
314        ob_start();
315
316        echo FunctionsPrint::printPedigreePerson($ancestors[1]);
317        foreach ($ancestors as $sosa => $individual) {
318            foreach ($individual->childFamilies() as $family) {
319                FunctionsCharts::printSosaFamily($family, $individual->xref(), $sosa, '', '', '', $show_cousins);
320            }
321        }
322
323        $html = ob_get_clean();
324
325        return new Response($html);
326    }
327
328    /**
329     * This chart can display its output in a number of styles
330     *
331     * @return array
332     */
333    protected function chartStyles(): array
334    {
335        return [
336            self::CHART_STYLE_LIST        => I18N::translate('List'),
337            self::CHART_STYLE_BOOKLET     => I18N::translate('Booklet'),
338            self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'),
339            self::CHART_STYLE_FAMILIES    => I18N::translate('Families'),
340        ];
341    }
342}
343