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