xref: /webtrees/app/Module/DescendancyModule.php (revision a67cd39f5bed0a01c3af3490de0599bfc69d5f12)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Module;
20
21use Fisharebest\Webtrees\Family;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Individual;
24use Fisharebest\Webtrees\Services\SearchService;
25use Fisharebest\Webtrees\Tree;
26use InvalidArgumentException;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
29
30use function assert;
31use function view;
32
33/**
34 * Class DescendancyModule
35 */
36class DescendancyModule extends AbstractModule implements ModuleSidebarInterface
37{
38    use ModuleSidebarTrait;
39
40    /** @var SearchService */
41    private $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 = $request->getAttribute('tree');
93        assert($tree instanceof Tree, new InvalidArgumentException());
94
95        $search = $request->getQueryParams()['search'];
96
97        $html = '';
98
99        if (strlen($search) >= 2) {
100            $html = $this->search_service
101                ->searchIndividualNames([$tree], [$search])
102                ->map(function (Individual $individual): string {
103                    return $this->getPersonLi($individual);
104                })
105                ->implode('');
106        }
107
108        if ($html !== '') {
109            $html = '<ul>' . $html . '</ul>';
110        }
111
112        return response($html);
113    }
114
115    /**
116     * @param ServerRequestInterface $request
117     *
118     * @return ResponseInterface
119     */
120    public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface
121    {
122        $tree = $request->getAttribute('tree');
123        assert($tree instanceof Tree, new InvalidArgumentException());
124
125        $xref = $request->getQueryParams()['xref'];
126
127        $individual = Individual::getInstance($xref, $tree);
128
129        if ($individual !== null && $individual->canShow()) {
130            $html = $this->loadSpouses($individual, 1);
131        } else {
132            $html = '';
133        }
134
135        return response($html);
136    }
137
138    /** {@inheritdoc} */
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        ]);
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, $generations = 0): string
167    {
168        $icon     = $generations > 0 ? 'icon-minus' : 'icon-plus';
169        $lifespan = $person->canShow() ? '(' . $person->getLifeSpan() . ')' : '';
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="' . 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-' . $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, $generations = 0): string
198    {
199        $spouse = $family->spouse($person);
200        if ($spouse) {
201            $spouse_name = '<small>' . view('icons/sex-' . $spouse->sex()) . '</small>' . $spouse->fullName();
202            $spouse_link = '<a href="' . e($person->url()) . '" title="' . strip_tags($person->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="#"><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, $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, $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