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