xref: /webtrees/app/Module/DescendancyModule.php (revision 8121b9bec19818120092699199161a1357bb8f3f)
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\FontAwesome;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Individual;
24use Fisharebest\Webtrees\Services\SearchService;
25use Fisharebest\Webtrees\Tree;
26use Symfony\Component\HttpFoundation\Request;
27use Symfony\Component\HttpFoundation\Response;
28
29/**
30 * Class DescendancyModule
31 */
32class DescendancyModule extends AbstractModule implements ModuleSidebarInterface
33{
34    use ModuleSidebarTrait;
35
36    /**
37     * How should this module be labelled on tabs, menus, 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 Request       $request
70     * @param Tree          $tree
71     * @param SearchService $search_service
72     *
73     * @return Response
74     */
75    public function getSearchAction(Request $request, Tree $tree, SearchService $search_service): Response
76    {
77        $search = $request->get('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 new Response($html);
95    }
96
97    /**
98     * @param Request $request
99     * @param Tree    $tree
100     *
101     * @return Response
102     */
103    public function getDescendantsAction(Request $request, Tree $tree): Response
104    {
105        $xref = $request->get('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 new 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            $person->getSexImage() . $person->getFullName() . $lifespan .
162            '</a>' .
163            FontAwesome::linkIcon('individual', $person->getFullName(), ['href' => $person->url()]) .
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->getSpouse($person);
180        if ($spouse) {
181            $spouse_name = $spouse->getSexImage() . $spouse->getFullName();
182            $spouse_link = FontAwesome::linkIcon('individual', $spouse->getFullName(), ['href' => $person->url()]);
183        } else {
184            $spouse_name = '';
185            $spouse_link = '';
186        }
187
188        $marryear = $family->getMarriageYear();
189        $marr     = $marryear ? '<i class="icon-rings"></i>' . $marryear : '';
190
191        return
192            '<li class="sb_desc_indi_li">' .
193            '<a class="sb_desc_indi" href="#"><i class="plusminus icon-minus"></i>' . $spouse_name . $marr . '</a>' .
194            $spouse_link .
195            FontAwesome::linkIcon('family', $family->getFullName(), ['href' => $family->url()]) .
196            '<div>' . $this->loadChildren($family, $generations) . '</div>' .
197            '</li>';
198    }
199
200    /**
201     * Display spouses.
202     *
203     * @param Individual $individual
204     * @param int        $generations
205     *
206     * @return string
207     */
208    public function loadSpouses(Individual $individual, $generations)
209    {
210        $out = '';
211        if ($individual->canShow()) {
212            foreach ($individual->getSpouseFamilies() as $family) {
213                $out .= $this->getFamilyLi($family, $individual, $generations - 1);
214            }
215        }
216        if ($out) {
217            return '<ul>' . $out . '</ul>';
218        }
219
220        return '';
221    }
222
223    /**
224     * Display descendants.
225     *
226     * @param Family $family
227     * @param int    $generations
228     *
229     * @return string
230     */
231    public function loadChildren(Family $family, $generations)
232    {
233        $out = '';
234        if ($family->canShow()) {
235            $children = $family->getChildren();
236            if ($children) {
237                foreach ($children as $child) {
238                    $out .= $this->getPersonLi($child, $generations - 1);
239                }
240            } else {
241                $out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>';
242            }
243        }
244        if ($out) {
245            return '<ul>' . $out . '</ul>';
246        }
247
248        return '';
249    }
250}
251