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