xref: /webtrees/app/Module/DescendancyModule.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
5*5bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
226b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
2632cd2800SGreg Roachuse Fisharebest\Webtrees\Services\SearchService;
27b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
286ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
296ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
303976b470SGreg Roach
31032918ffSGreg Roachuse function view;
328c2e8227SGreg Roach
338c2e8227SGreg Roach/**
348c2e8227SGreg Roach * Class DescendancyModule
358c2e8227SGreg Roach */
3637eb8894SGreg Roachclass DescendancyModule extends AbstractModule implements ModuleSidebarInterface
37c1010edaSGreg Roach{
3849a243cbSGreg Roach    use ModuleSidebarTrait;
3949a243cbSGreg Roach
4043f2f523SGreg Roach    private SearchService $search_service;
4157ab2231SGreg Roach
4257ab2231SGreg Roach    /**
4357ab2231SGreg Roach     * DescendancyModule constructor.
4457ab2231SGreg Roach     *
4557ab2231SGreg Roach     * @param SearchService $search_service
4657ab2231SGreg Roach     */
473976b470SGreg Roach    public function __construct(SearchService $search_service)
483976b470SGreg Roach    {
4957ab2231SGreg Roach        $this->search_service = $search_service;
5057ab2231SGreg Roach    }
5157ab2231SGreg Roach
52961ec755SGreg Roach    /**
530cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
54961ec755SGreg Roach     *
55961ec755SGreg Roach     * @return string
56961ec755SGreg Roach     */
5749a243cbSGreg Roach    public function title(): string
58c1010edaSGreg Roach    {
59bbb76c12SGreg Roach        /* I18N: Name of a module/sidebar */
60bbb76c12SGreg Roach        return I18N::translate('Descendants');
618c2e8227SGreg Roach    }
628c2e8227SGreg Roach
63961ec755SGreg Roach    /**
64961ec755SGreg Roach     * A sentence describing what this module does.
65961ec755SGreg Roach     *
66961ec755SGreg Roach     * @return string
67961ec755SGreg Roach     */
6849a243cbSGreg Roach    public function description(): string
69c1010edaSGreg Roach    {
70bbb76c12SGreg Roach        /* I18N: Description of the “Descendants” module */
71bbb76c12SGreg Roach        return I18N::translate('A sidebar showing the descendants of an individual.');
728c2e8227SGreg Roach    }
738c2e8227SGreg Roach
7476692c8bSGreg Roach    /**
7549a243cbSGreg Roach     * The default position for this sidebar.  It can be changed in the control panel.
7649a243cbSGreg Roach     *
7749a243cbSGreg Roach     * @return int
7849a243cbSGreg Roach     */
7949a243cbSGreg Roach    public function defaultSidebarOrder(): int
8049a243cbSGreg Roach    {
81353b36abSGreg Roach        return 3;
8249a243cbSGreg Roach    }
8349a243cbSGreg Roach
8449a243cbSGreg Roach    /**
856ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
8676692c8bSGreg Roach     *
876ccdf4f0SGreg Roach     * @return ResponseInterface
8876692c8bSGreg Roach     */
8957ab2231SGreg Roach    public function getSearchAction(ServerRequestInterface $request): ResponseInterface
90c1010edaSGreg Roach    {
91b55cbc6bSGreg Roach        $tree   = Validator::attributes($request)->tree();
92ac5f8ed1SGreg Roach        $search = $request->getQueryParams()['search'];
938c2e8227SGreg Roach
941c0676b2SGreg Roach        $html = '';
951c0676b2SGreg Roach
961c0676b2SGreg Roach        if (strlen($search) >= 2) {
9757ab2231SGreg Roach            $html = $this->search_service
98a7a24840SGreg Roach                ->searchIndividualNames([$tree], [$search])
9932cd2800SGreg Roach                ->map(function (Individual $individual): string {
10032cd2800SGreg Roach                    return $this->getPersonLi($individual);
10132cd2800SGreg Roach                })
10232cd2800SGreg Roach                ->implode('');
1038c2e8227SGreg Roach        }
1048c2e8227SGreg Roach
1051c0676b2SGreg Roach        if ($html !== '') {
1061c0676b2SGreg Roach            $html = '<ul>' . $html . '</ul>';
1071c0676b2SGreg Roach        }
1081c0676b2SGreg Roach
1096ccdf4f0SGreg Roach        return response($html);
1101c0676b2SGreg Roach    }
1111c0676b2SGreg Roach
1121c0676b2SGreg Roach    /**
1136ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
1141c0676b2SGreg Roach     *
1156ccdf4f0SGreg Roach     * @return ResponseInterface
1161c0676b2SGreg Roach     */
11757ab2231SGreg Roach    public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface
118c1010edaSGreg Roach    {
119b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
12018c571c3SGreg Roach        $xref = $request->getQueryParams()['xref'] ?? '';
1211c0676b2SGreg Roach
1226b9cb339SGreg Roach        $individual = Registry::individualFactory()->make($xref, $tree);
1231c0676b2SGreg Roach
1241c0676b2SGreg Roach        if ($individual !== null && $individual->canShow()) {
1251c0676b2SGreg Roach            $html = $this->loadSpouses($individual, 1);
1261c0676b2SGreg Roach        } else {
1271c0676b2SGreg Roach            $html = '';
1281c0676b2SGreg Roach        }
1291c0676b2SGreg Roach
1306ccdf4f0SGreg Roach        return response($html);
1311c0676b2SGreg Roach    }
1321c0676b2SGreg Roach
1330dcd9387SGreg Roach    /**
1340dcd9387SGreg Roach     * @param Individual $individual
1350dcd9387SGreg Roach     *
1360dcd9387SGreg Roach     * @return bool
1370dcd9387SGreg Roach     */
1388f53f488SRico Sonntag    public function hasSidebarContent(Individual $individual): bool
139c1010edaSGreg Roach    {
14038a9583bSGreg Roach        return true;
1418c2e8227SGreg Roach    }
1428c2e8227SGreg Roach
14376692c8bSGreg Roach    /**
14476692c8bSGreg Roach     * Load this sidebar synchronously.
14576692c8bSGreg Roach     *
146225e381fSGreg Roach     * @param Individual $individual
147225e381fSGreg Roach     *
14876692c8bSGreg Roach     * @return string
14976692c8bSGreg Roach     */
1508f53f488SRico Sonntag    public function getSidebarContent(Individual $individual): string
151c1010edaSGreg Roach    {
1521ef93f16SGreg Roach        return view('modules/descendancy/sidebar', [
153a817de92SGreg Roach            'individual_list' => $this->getPersonLi($individual, 1),
154ef5d23f1SGreg Roach            'tree'            => $individual->tree(),
155a817de92SGreg Roach        ]);
1568c2e8227SGreg Roach    }
1578c2e8227SGreg Roach
1588c2e8227SGreg Roach    /**
15976692c8bSGreg Roach     * Format an individual in a list.
16076692c8bSGreg Roach     *
1618c2e8227SGreg Roach     * @param Individual $person
162cbc1590aSGreg Roach     * @param int        $generations
1638c2e8227SGreg Roach     *
1648c2e8227SGreg Roach     * @return string
1658c2e8227SGreg Roach     */
16673d58381SGreg Roach    public function getPersonLi(Individual $person, int $generations = 0): string
167c1010edaSGreg Roach    {
1688c2e8227SGreg Roach        $icon     = $generations > 0 ? 'icon-minus' : 'icon-plus';
1695e6816beSGreg Roach        $lifespan = $person->canShow() ? '(' . $person->lifespan() . ')' : '';
1708c2e8227SGreg Roach        $spouses  = $generations > 0 ? $this->loadSpouses($person, 0) : '';
1712622ba92SGreg Roach
1722622ba92SGreg Roach        return
1732622ba92SGreg Roach            '<li class="sb_desc_indi_li">' .
174d4786c66SGreg Roach            '<a class="sb_desc_indi" href="#" data-wt-href="' . e(route('module', [
17526684e68SGreg Roach                'module' => $this->name(),
176c1010edaSGreg Roach                'action' => 'Descendants',
1779022ab66SGreg Roach                'tree'    => $person->tree()->name(),
178c0935879SGreg Roach                'xref'   => $person->xref(),
179c1010edaSGreg Roach            ])) . '">' .
1802622ba92SGreg Roach            '<i class="plusminus ' . $icon . '"></i>' .
18108362db4SGreg Roach            '<small>' . view('icons/sex', ['sex' => $person->sex()]) . '</small>' . $person->fullName() . $lifespan .
1822622ba92SGreg Roach            '</a>' .
18339ca88baSGreg Roach            '<a href="' . e($person->url()) . '" title="' . strip_tags($person->fullName()) . '">' . view('icons/individual') . '</a>' .
1842622ba92SGreg Roach            '<div>' . $spouses . '</div>' .
1852622ba92SGreg Roach            '</li>';
1868c2e8227SGreg Roach    }
1878c2e8227SGreg Roach
1888c2e8227SGreg Roach    /**
18976692c8bSGreg Roach     * Format a family in a list.
19076692c8bSGreg Roach     *
1918c2e8227SGreg Roach     * @param Family     $family
1928c2e8227SGreg Roach     * @param Individual $person
193cbc1590aSGreg Roach     * @param int        $generations
1948c2e8227SGreg Roach     *
1958c2e8227SGreg Roach     * @return string
1968c2e8227SGreg Roach     */
19773d58381SGreg Roach    public function getFamilyLi(Family $family, Individual $person, int $generations = 0): string
198c1010edaSGreg Roach    {
19939ca88baSGreg Roach        $spouse = $family->spouse($person);
2004890720dSGreg Roach        if ($spouse instanceof Individual) {
20108362db4SGreg Roach            $spouse_name = '<small>' . view('icons/sex', ['sex' => $spouse->sex()]) . '</small>' . $spouse->fullName();
2024890720dSGreg Roach            $spouse_link = '<a href="' . e($spouse->url()) . '" title="' . strip_tags($spouse->fullName()) . '">' . view('icons/individual') . '</a>';
2032622ba92SGreg Roach        } else {
2042622ba92SGreg Roach            $spouse_name = '';
2052622ba92SGreg Roach            $spouse_link = '';
2062622ba92SGreg Roach        }
2072622ba92SGreg Roach
20839ca88baSGreg Roach        $family_link = '<a href="' . e($family->url()) . '" title="' . strip_tags($family->fullName()) . '">' . view('icons/family') . '</a>';
20939b853a7SGreg Roach
2108c2e8227SGreg Roach        $marryear = $family->getMarriageYear();
2118c2e8227SGreg Roach        $marr     = $marryear ? '<i class="icon-rings"></i>' . $marryear : '';
2122622ba92SGreg Roach
2132622ba92SGreg Roach        return
2142622ba92SGreg Roach            '<li class="sb_desc_indi_li">' .
215d4786c66SGreg Roach            '<a class="sb_desc_indi" href="#" data-wt-href="#"><i class="plusminus icon-minus"></i>' .
21639b853a7SGreg Roach            $spouse_name .
21739b853a7SGreg Roach            $marr .
21839b853a7SGreg Roach            '</a>' .
2192622ba92SGreg Roach            $spouse_link .
22039b853a7SGreg Roach            $family_link .
2212622ba92SGreg Roach            '<div>' . $this->loadChildren($family, $generations) . '</div>' .
2222622ba92SGreg Roach            '</li>';
2238c2e8227SGreg Roach    }
2248c2e8227SGreg Roach
2258c2e8227SGreg Roach    /**
22676692c8bSGreg Roach     * Display spouses.
22776692c8bSGreg Roach     *
22849a243cbSGreg Roach     * @param Individual $individual
229cbc1590aSGreg Roach     * @param int        $generations
2308c2e8227SGreg Roach     *
2318c2e8227SGreg Roach     * @return string
2328c2e8227SGreg Roach     */
23324f2a3afSGreg Roach    public function loadSpouses(Individual $individual, int $generations): string
234c1010edaSGreg Roach    {
2358c2e8227SGreg Roach        $out = '';
23649a243cbSGreg Roach        if ($individual->canShow()) {
23739ca88baSGreg Roach            foreach ($individual->spouseFamilies() as $family) {
23849a243cbSGreg Roach                $out .= $this->getFamilyLi($family, $individual, $generations - 1);
2398c2e8227SGreg Roach            }
2408c2e8227SGreg Roach        }
2418c2e8227SGreg Roach        if ($out) {
2428c2e8227SGreg Roach            return '<ul>' . $out . '</ul>';
2438c2e8227SGreg Roach        }
244b2ce94c6SRico Sonntag
245b2ce94c6SRico Sonntag        return '';
2468c2e8227SGreg Roach    }
2478c2e8227SGreg Roach
2488c2e8227SGreg Roach    /**
24976692c8bSGreg Roach     * Display descendants.
25076692c8bSGreg Roach     *
2518c2e8227SGreg Roach     * @param Family $family
252cbc1590aSGreg Roach     * @param int    $generations
2538c2e8227SGreg Roach     *
2548c2e8227SGreg Roach     * @return string
2558c2e8227SGreg Roach     */
25624f2a3afSGreg Roach    public function loadChildren(Family $family, int $generations): string
257c1010edaSGreg Roach    {
2588c2e8227SGreg Roach        $out = '';
2598c2e8227SGreg Roach        if ($family->canShow()) {
26039ca88baSGreg Roach            $children = $family->children();
261820b62dfSGreg Roach
262820b62dfSGreg Roach            if ($children->isNotEmpty()) {
2638c2e8227SGreg Roach                foreach ($children as $child) {
2648c2e8227SGreg Roach                    $out .= $this->getPersonLi($child, $generations - 1);
2658c2e8227SGreg Roach                }
2668c2e8227SGreg Roach            } else {
2678c2e8227SGreg Roach                $out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>';
2688c2e8227SGreg Roach            }
2698c2e8227SGreg Roach        }
2708c2e8227SGreg Roach        if ($out) {
2718c2e8227SGreg Roach            return '<ul>' . $out . '</ul>';
2728c2e8227SGreg Roach        }
273b2ce94c6SRico Sonntag
274b2ce94c6SRico Sonntag        return '';
2758c2e8227SGreg Roach    }
2768c2e8227SGreg Roach}
277