xref: /webtrees/app/Module/DescendancyModule.php (revision ac71572d8462e396ed5a307f05b29381e49f9e6e)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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     * 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   = Validator::attributes($request)->tree();
93        $search = Validator::queryParams($request)->string('search');
94
95        $html = '';
96
97        if (strlen($search) >= 2) {
98            $html = $this->search_service
99                ->searchIndividualNames([$tree], [$search])
100                ->map(function (Individual $individual): string {
101                    return $this->getPersonLi($individual);
102                })
103                ->implode('');
104        }
105
106        if ($html !== '') {
107            $html = '<ul>' . $html . '</ul>';
108        }
109
110        return response($html);
111    }
112
113    /**
114     * @param ServerRequestInterface $request
115     *
116     * @return ResponseInterface
117     */
118    public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface
119    {
120        $tree = Validator::attributes($request)->tree();
121        $xref = Validator::queryParams($request)->isXref()->string('xref');
122
123        $individual = Registry::individualFactory()->make($xref, $tree);
124
125        if ($individual !== null && $individual->canShow()) {
126            $html = $this->loadSpouses($individual, 1);
127        } else {
128            $html = '';
129        }
130
131        return response($html);
132    }
133
134    /**
135     * @param Individual $individual
136     *
137     * @return bool
138     */
139    public function hasSidebarContent(Individual $individual): bool
140    {
141        return true;
142    }
143
144    /**
145     * Load this sidebar synchronously.
146     *
147     * @param Individual $individual
148     *
149     * @return string
150     */
151    public function getSidebarContent(Individual $individual): string
152    {
153        return view('modules/descendancy/sidebar', [
154            'individual_list' => $this->getPersonLi($individual, 1),
155            'tree'            => $individual->tree(),
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, int $generations = 0): string
168    {
169        $icon     = $generations > 0 ? 'icon-minus' : 'icon-plus';
170        $lifespan = $person->canShow() ? '(' . $person->lifespan() . ')' : '';
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="#" data-wt-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', ['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, int $generations = 0): string
199    {
200        $spouse = $family->spouse($person);
201        if ($spouse instanceof Individual) {
202            $spouse_name = '<small>' . view('icons/sex', ['sex' => $spouse->sex()]) . '</small>' . $spouse->fullName();
203            $spouse_link = '<a href="' . e($spouse->url()) . '" title="' . strip_tags($spouse->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="#" data-wt-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, int $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, int $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