xref: /webtrees/app/Module/DescendancyModule.php (revision 0b4092ed8e31e3588eb143d0ade6c4a411367da8)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Family;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Individual;
23use Fisharebest\Webtrees\Services\SearchService;
24use Fisharebest\Webtrees\Tree;
25use Psr\Http\Message\ResponseInterface;
26use Psr\Http\Message\ServerRequestInterface;
27use function view;
28
29/**
30 * Class DescendancyModule
31 */
32class DescendancyModule extends AbstractModule implements ModuleSidebarInterface
33{
34    use ModuleSidebarTrait;
35
36    /**
37     * How should this module be identified in the control panel, etc.?
38     *
39     * @return string
40     */
41    public function title(): string
42    {
43        /* I18N: Name of a module/sidebar */
44        return I18N::translate('Descendants');
45    }
46
47    /**
48     * A sentence describing what this module does.
49     *
50     * @return string
51     */
52    public function description(): string
53    {
54        /* I18N: Description of the “Descendants” module */
55        return I18N::translate('A sidebar showing the descendants of an individual.');
56    }
57
58    /**
59     * The default position for this sidebar.  It can be changed in the control panel.
60     *
61     * @return int
62     */
63    public function defaultSidebarOrder(): int
64    {
65        return 3;
66    }
67
68    /**
69     * @param ServerRequestInterface $request
70     * @param Tree                   $tree
71     * @param SearchService          $search_service
72     *
73     * @return ResponseInterface
74     */
75    public function getSearchAction(ServerRequestInterface $request, Tree $tree, SearchService $search_service): ResponseInterface
76    {
77        $search = $request->getQueryParams()['search'];
78
79        $html = '';
80
81        if (strlen($search) >= 2) {
82            $html = $search_service
83                ->searchIndividualNames([$tree], [$search])
84                ->map(function (Individual $individual): string {
85                    return $this->getPersonLi($individual);
86                })
87                ->implode('');
88        }
89
90        if ($html !== '') {
91            $html = '<ul>' . $html . '</ul>';
92        }
93
94        return response($html);
95    }
96
97    /**
98     * @param ServerRequestInterface $request
99     * @param Tree                   $tree
100     *
101     * @return ResponseInterface
102     */
103    public function getDescendantsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
104    {
105        $xref = $request->getQueryParams()['xref'];
106
107        $individual = Individual::getInstance($xref, $tree);
108
109        if ($individual !== null && $individual->canShow()) {
110            $html = $this->loadSpouses($individual, 1);
111        } else {
112            $html = '';
113        }
114
115        return response($html);
116    }
117
118    /** {@inheritdoc} */
119    public function hasSidebarContent(Individual $individual): bool
120    {
121        return true;
122    }
123
124    /**
125     * Load this sidebar synchronously.
126     *
127     * @param Individual $individual
128     *
129     * @return string
130     */
131    public function getSidebarContent(Individual $individual): string
132    {
133        return view('modules/descendancy/sidebar', [
134            'individual_list' => $this->getPersonLi($individual, 1),
135        ]);
136    }
137
138    /**
139     * Format an individual in a list.
140     *
141     * @param Individual $person
142     * @param int        $generations
143     *
144     * @return string
145     */
146    public function getPersonLi(Individual $person, $generations = 0): string
147    {
148        $icon     = $generations > 0 ? 'icon-minus' : 'icon-plus';
149        $lifespan = $person->canShow() ? '(' . $person->getLifeSpan() . ')' : '';
150        $spouses  = $generations > 0 ? $this->loadSpouses($person, 0) : '';
151
152        return
153            '<li class="sb_desc_indi_li">' .
154            '<a class="sb_desc_indi" href="' . e(route('module', [
155                'module' => $this->name(),
156                'action' => 'Descendants',
157                'ged'    => $person->tree()->name(),
158                'xref'   => $person->xref(),
159            ])) . '">' .
160            '<i class="plusminus ' . $icon . '"></i>' .
161            '<small>' . view('icons/sex-' . $person->sex()) . '</small>' . $person->fullName() . $lifespan .
162            '</a>' .
163            '<a href="' . e($person->url()) . '" title="' . strip_tags($person->fullName()) . '">' . view('icons/individual') . '</a>' .
164            '<div>' . $spouses . '</div>' .
165            '</li>';
166    }
167
168    /**
169     * Format a family in a list.
170     *
171     * @param Family     $family
172     * @param Individual $person
173     * @param int        $generations
174     *
175     * @return string
176     */
177    public function getFamilyLi(Family $family, Individual $person, $generations = 0): string
178    {
179        $spouse = $family->spouse($person);
180        if ($spouse) {
181            $spouse_name = '<small>' . view('icons/sex-' . $spouse->sex()) . '</small>' . $spouse->fullName();
182            $spouse_link = '<a href="' . e($person->url()) . '" title="' . strip_tags($person->fullName()) . '">' . view('icons/individual') . '</a>';
183        } else {
184            $spouse_name = '';
185            $spouse_link = '';
186        }
187
188        $family_link = '<a href="' . e($family->url()) . '" title="' . strip_tags($family->fullName()) . '">' . view('icons/family') . '</a>';
189
190        $marryear = $family->getMarriageYear();
191        $marr     = $marryear ? '<i class="icon-rings"></i>' . $marryear : '';
192
193        return
194            '<li class="sb_desc_indi_li">' .
195            '<a class="sb_desc_indi" href="#"><i class="plusminus icon-minus"></i>' .
196            $spouse_name .
197            $marr .
198            '</a>' .
199            $spouse_link .
200            $family_link .
201            '<div>' . $this->loadChildren($family, $generations) . '</div>' .
202            '</li>';
203    }
204
205    /**
206     * Display spouses.
207     *
208     * @param Individual $individual
209     * @param int        $generations
210     *
211     * @return string
212     */
213    public function loadSpouses(Individual $individual, $generations): string
214    {
215        $out = '';
216        if ($individual->canShow()) {
217            foreach ($individual->spouseFamilies() as $family) {
218                $out .= $this->getFamilyLi($family, $individual, $generations - 1);
219            }
220        }
221        if ($out) {
222            return '<ul>' . $out . '</ul>';
223        }
224
225        return '';
226    }
227
228    /**
229     * Display descendants.
230     *
231     * @param Family $family
232     * @param int    $generations
233     *
234     * @return string
235     */
236    public function loadChildren(Family $family, $generations): string
237    {
238        $out = '';
239        if ($family->canShow()) {
240            $children = $family->children();
241
242            if ($children->isNotEmpty()) {
243                foreach ($children as $child) {
244                    $out .= $this->getPersonLi($child, $generations - 1);
245                }
246            } else {
247                $out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>';
248            }
249        }
250        if ($out) {
251            return '<ul>' . $out . '</ul>';
252        }
253
254        return '';
255    }
256}
257