xref: /webtrees/app/Module/DescendancyModule.php (revision 2ebcf907ed34213f816592af04e6c160335d6311)
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    /** @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);
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);
125
126        $xref = $request->getQueryParams()['xref'] ?? '';
127
128        $individual = Registry::individualFactory()->make($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    /**
140     * @param Individual $individual
141     *
142     * @return bool
143     */
144    public function hasSidebarContent(Individual $individual): bool
145    {
146        return true;
147    }
148
149    /**
150     * Load this sidebar synchronously.
151     *
152     * @param Individual $individual
153     *
154     * @return string
155     */
156    public function getSidebarContent(Individual $individual): string
157    {
158        return view('modules/descendancy/sidebar', [
159            'individual_list' => $this->getPersonLi($individual, 1),
160            'tree'            => $individual->tree(),
161        ]);
162    }
163
164    /**
165     * Format an individual in a list.
166     *
167     * @param Individual $person
168     * @param int        $generations
169     *
170     * @return string
171     */
172    public function getPersonLi(Individual $person, int $generations = 0): string
173    {
174        $icon     = $generations > 0 ? 'icon-minus' : 'icon-plus';
175        $lifespan = $person->canShow() ? '(' . $person->lifespan() . ')' : '';
176        $spouses  = $generations > 0 ? $this->loadSpouses($person, 0) : '';
177
178        return
179            '<li class="sb_desc_indi_li">' .
180            '<a class="sb_desc_indi" href="#" data-wt-href="' . e(route('module', [
181                'module' => $this->name(),
182                'action' => 'Descendants',
183                'tree'    => $person->tree()->name(),
184                'xref'   => $person->xref(),
185            ])) . '">' .
186            '<i class="plusminus ' . $icon . '"></i>' .
187            '<small>' . view('icons/sex', ['sex' => $person->sex()]) . '</small>' . $person->fullName() . $lifespan .
188            '</a>' .
189            '<a href="' . e($person->url()) . '" title="' . strip_tags($person->fullName()) . '">' . view('icons/individual') . '</a>' .
190            '<div>' . $spouses . '</div>' .
191            '</li>';
192    }
193
194    /**
195     * Format a family in a list.
196     *
197     * @param Family     $family
198     * @param Individual $person
199     * @param int        $generations
200     *
201     * @return string
202     */
203    public function getFamilyLi(Family $family, Individual $person, int $generations = 0): string
204    {
205        $spouse = $family->spouse($person);
206        if ($spouse instanceof Individual) {
207            $spouse_name = '<small>' . view('icons/sex', ['sex' => $spouse->sex()]) . '</small>' . $spouse->fullName();
208            $spouse_link = '<a href="' . e($spouse->url()) . '" title="' . strip_tags($spouse->fullName()) . '">' . view('icons/individual') . '</a>';
209        } else {
210            $spouse_name = '';
211            $spouse_link = '';
212        }
213
214        $family_link = '<a href="' . e($family->url()) . '" title="' . strip_tags($family->fullName()) . '">' . view('icons/family') . '</a>';
215
216        $marryear = $family->getMarriageYear();
217        $marr     = $marryear ? '<i class="icon-rings"></i>' . $marryear : '';
218
219        return
220            '<li class="sb_desc_indi_li">' .
221            '<a class="sb_desc_indi" href="#" data-wt-href="#"><i class="plusminus icon-minus"></i>' .
222            $spouse_name .
223            $marr .
224            '</a>' .
225            $spouse_link .
226            $family_link .
227            '<div>' . $this->loadChildren($family, $generations) . '</div>' .
228            '</li>';
229    }
230
231    /**
232     * Display spouses.
233     *
234     * @param Individual $individual
235     * @param int        $generations
236     *
237     * @return string
238     */
239    public function loadSpouses(Individual $individual, int $generations): string
240    {
241        $out = '';
242        if ($individual->canShow()) {
243            foreach ($individual->spouseFamilies() as $family) {
244                $out .= $this->getFamilyLi($family, $individual, $generations - 1);
245            }
246        }
247        if ($out) {
248            return '<ul>' . $out . '</ul>';
249        }
250
251        return '';
252    }
253
254    /**
255     * Display descendants.
256     *
257     * @param Family $family
258     * @param int    $generations
259     *
260     * @return string
261     */
262    public function loadChildren(Family $family, int $generations): string
263    {
264        $out = '';
265        if ($family->canShow()) {
266            $children = $family->children();
267
268            if ($children->isNotEmpty()) {
269                foreach ($children as $child) {
270                    $out .= $this->getPersonLi($child, $generations - 1);
271                }
272            } else {
273                $out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>';
274            }
275        }
276        if ($out) {
277            return '<ul>' . $out . '</ul>';
278        }
279
280        return '';
281    }
282}
283