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