xref: /webtrees/app/Module/DescendancyModule.php (revision fcfa147e10aaa6c7ff580c29bd6e5b88666befc1)
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 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Family;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Individual;
25use Fisharebest\Webtrees\Services\SearchService;
26use Fisharebest\Webtrees\Tree;
27use InvalidArgumentException;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30
31use function assert;
32use function view;
33
34/**
35 * Class DescendancyModule
36 */
37class DescendancyModule extends AbstractModule implements ModuleSidebarInterface
38{
39    use ModuleSidebarTrait;
40
41    /** @var SearchService */
42    private $search_service;
43
44    /**
45     * DescendancyModule constructor.
46     *
47     * @param SearchService $search_service
48     */
49    public function __construct(SearchService $search_service)
50    {
51        $this->search_service = $search_service;
52    }
53
54    /**
55     * How should this module be identified in the control panel, etc.?
56     *
57     * @return string
58     */
59    public function title(): string
60    {
61        /* I18N: Name of a module/sidebar */
62        return I18N::translate('Descendants');
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 “Descendants” module */
73        return I18N::translate('A sidebar showing the descendants of an individual.');
74    }
75
76    /**
77     * The default position for this sidebar.  It can be changed in the control panel.
78     *
79     * @return int
80     */
81    public function defaultSidebarOrder(): int
82    {
83        return 3;
84    }
85
86    /**
87     * @param ServerRequestInterface $request
88     *
89     * @return ResponseInterface
90     */
91    public function getSearchAction(ServerRequestInterface $request): ResponseInterface
92    {
93        $tree = $request->getAttribute('tree');
94        assert($tree instanceof Tree, new InvalidArgumentException());
95
96        $search = $request->getQueryParams()['search'];
97
98        $html = '';
99
100        if (strlen($search) >= 2) {
101            $html = $this->search_service
102                ->searchIndividualNames([$tree], [$search])
103                ->map(function (Individual $individual): string {
104                    return $this->getPersonLi($individual);
105                })
106                ->implode('');
107        }
108
109        if ($html !== '') {
110            $html = '<ul>' . $html . '</ul>';
111        }
112
113        return response($html);
114    }
115
116    /**
117     * @param ServerRequestInterface $request
118     *
119     * @return ResponseInterface
120     */
121    public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface
122    {
123        $tree = $request->getAttribute('tree');
124        assert($tree instanceof Tree, new InvalidArgumentException());
125
126        $xref = $request->getQueryParams()['xref'];
127
128        $individual = Individual::getInstance($xref, $tree);
129
130        if ($individual !== null && $individual->canShow()) {
131            $html = $this->loadSpouses($individual, 1);
132        } else {
133            $html = '';
134        }
135
136        return response($html);
137    }
138
139    /** {@inheritdoc} */
140    public function hasSidebarContent(Individual $individual): bool
141    {
142        return true;
143    }
144
145    /**
146     * Load this sidebar synchronously.
147     *
148     * @param Individual $individual
149     *
150     * @return string
151     */
152    public function getSidebarContent(Individual $individual): string
153    {
154        return view('modules/descendancy/sidebar', [
155            'individual_list' => $this->getPersonLi($individual, 1),
156        ]);
157    }
158
159    /**
160     * Format an individual in a list.
161     *
162     * @param Individual $person
163     * @param int        $generations
164     *
165     * @return string
166     */
167    public function getPersonLi(Individual $person, $generations = 0): string
168    {
169        $icon     = $generations > 0 ? 'icon-minus' : 'icon-plus';
170        $lifespan = $person->canShow() ? '(' . $person->getLifeSpan() . ')' : '';
171        $spouses  = $generations > 0 ? $this->loadSpouses($person, 0) : '';
172
173        return
174            '<li class="sb_desc_indi_li">' .
175            '<a class="sb_desc_indi" href="' . e(route('module', [
176                'module' => $this->name(),
177                'action' => 'Descendants',
178                'tree'    => $person->tree()->name(),
179                'xref'   => $person->xref(),
180            ])) . '">' .
181            '<i class="plusminus ' . $icon . '"></i>' .
182            '<small>' . view('icons/sex-' . $person->sex()) . '</small>' . $person->fullName() . $lifespan .
183            '</a>' .
184            '<a href="' . e($person->url()) . '" title="' . strip_tags($person->fullName()) . '">' . view('icons/individual') . '</a>' .
185            '<div>' . $spouses . '</div>' .
186            '</li>';
187    }
188
189    /**
190     * Format a family in a list.
191     *
192     * @param Family     $family
193     * @param Individual $person
194     * @param int        $generations
195     *
196     * @return string
197     */
198    public function getFamilyLi(Family $family, Individual $person, $generations = 0): string
199    {
200        $spouse = $family->spouse($person);
201        if ($spouse) {
202            $spouse_name = '<small>' . view('icons/sex-' . $spouse->sex()) . '</small>' . $spouse->fullName();
203            $spouse_link = '<a href="' . e($person->url()) . '" title="' . strip_tags($person->fullName()) . '">' . view('icons/individual') . '</a>';
204        } else {
205            $spouse_name = '';
206            $spouse_link = '';
207        }
208
209        $family_link = '<a href="' . e($family->url()) . '" title="' . strip_tags($family->fullName()) . '">' . view('icons/family') . '</a>';
210
211        $marryear = $family->getMarriageYear();
212        $marr     = $marryear ? '<i class="icon-rings"></i>' . $marryear : '';
213
214        return
215            '<li class="sb_desc_indi_li">' .
216            '<a class="sb_desc_indi" href="#"><i class="plusminus icon-minus"></i>' .
217            $spouse_name .
218            $marr .
219            '</a>' .
220            $spouse_link .
221            $family_link .
222            '<div>' . $this->loadChildren($family, $generations) . '</div>' .
223            '</li>';
224    }
225
226    /**
227     * Display spouses.
228     *
229     * @param Individual $individual
230     * @param int        $generations
231     *
232     * @return string
233     */
234    public function loadSpouses(Individual $individual, $generations): string
235    {
236        $out = '';
237        if ($individual->canShow()) {
238            foreach ($individual->spouseFamilies() as $family) {
239                $out .= $this->getFamilyLi($family, $individual, $generations - 1);
240            }
241        }
242        if ($out) {
243            return '<ul>' . $out . '</ul>';
244        }
245
246        return '';
247    }
248
249    /**
250     * Display descendants.
251     *
252     * @param Family $family
253     * @param int    $generations
254     *
255     * @return string
256     */
257    public function loadChildren(Family $family, $generations): string
258    {
259        $out = '';
260        if ($family->canShow()) {
261            $children = $family->children();
262
263            if ($children->isNotEmpty()) {
264                foreach ($children as $child) {
265                    $out .= $this->getPersonLi($child, $generations - 1);
266                }
267            } else {
268                $out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>';
269            }
270        }
271        if ($out) {
272            return '<ul>' . $out . '</ul>';
273        }
274
275        return '';
276    }
277}
278