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