xref: /webtrees/app/Module/DescendancyModule.php (revision 73d58381388d199b1beb6f2a613b98f25617f706)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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;
275229eadeSGreg Roachuse Fisharebest\Webtrees\Tree;
286ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
296ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
303976b470SGreg Roach
315229eadeSGreg Roachuse function assert;
32032918ffSGreg Roachuse function view;
338c2e8227SGreg Roach
348c2e8227SGreg Roach/**
358c2e8227SGreg Roach * Class DescendancyModule
368c2e8227SGreg Roach */
3737eb8894SGreg Roachclass DescendancyModule extends AbstractModule implements ModuleSidebarInterface
38c1010edaSGreg Roach{
3949a243cbSGreg Roach    use ModuleSidebarTrait;
4049a243cbSGreg Roach
4157ab2231SGreg Roach    /** @var SearchService */
4257ab2231SGreg Roach    private $search_service;
4357ab2231SGreg Roach
4457ab2231SGreg Roach    /**
4557ab2231SGreg Roach     * DescendancyModule constructor.
4657ab2231SGreg Roach     *
4757ab2231SGreg Roach     * @param SearchService $search_service
4857ab2231SGreg Roach     */
493976b470SGreg Roach    public function __construct(SearchService $search_service)
503976b470SGreg Roach    {
5157ab2231SGreg Roach        $this->search_service = $search_service;
5257ab2231SGreg Roach    }
5357ab2231SGreg Roach
54961ec755SGreg Roach    /**
550cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
56961ec755SGreg Roach     *
57961ec755SGreg Roach     * @return string
58961ec755SGreg Roach     */
5949a243cbSGreg Roach    public function title(): string
60c1010edaSGreg Roach    {
61bbb76c12SGreg Roach        /* I18N: Name of a module/sidebar */
62bbb76c12SGreg Roach        return I18N::translate('Descendants');
638c2e8227SGreg Roach    }
648c2e8227SGreg Roach
65961ec755SGreg Roach    /**
66961ec755SGreg Roach     * A sentence describing what this module does.
67961ec755SGreg Roach     *
68961ec755SGreg Roach     * @return string
69961ec755SGreg Roach     */
7049a243cbSGreg Roach    public function description(): string
71c1010edaSGreg Roach    {
72bbb76c12SGreg Roach        /* I18N: Description of the “Descendants” module */
73bbb76c12SGreg Roach        return I18N::translate('A sidebar showing the descendants of an individual.');
748c2e8227SGreg Roach    }
758c2e8227SGreg Roach
7676692c8bSGreg Roach    /**
7749a243cbSGreg Roach     * The default position for this sidebar.  It can be changed in the control panel.
7849a243cbSGreg Roach     *
7949a243cbSGreg Roach     * @return int
8049a243cbSGreg Roach     */
8149a243cbSGreg Roach    public function defaultSidebarOrder(): int
8249a243cbSGreg Roach    {
83353b36abSGreg Roach        return 3;
8449a243cbSGreg Roach    }
8549a243cbSGreg Roach
8649a243cbSGreg Roach    /**
876ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
8876692c8bSGreg Roach     *
896ccdf4f0SGreg Roach     * @return ResponseInterface
9076692c8bSGreg Roach     */
9157ab2231SGreg Roach    public function getSearchAction(ServerRequestInterface $request): ResponseInterface
92c1010edaSGreg Roach    {
9357ab2231SGreg Roach        $tree = $request->getAttribute('tree');
9475964c75SGreg Roach        assert($tree instanceof Tree);
955229eadeSGreg Roach
96ac5f8ed1SGreg Roach        $search = $request->getQueryParams()['search'];
978c2e8227SGreg Roach
981c0676b2SGreg Roach        $html = '';
991c0676b2SGreg Roach
1001c0676b2SGreg Roach        if (strlen($search) >= 2) {
10157ab2231SGreg Roach            $html = $this->search_service
102a7a24840SGreg Roach                ->searchIndividualNames([$tree], [$search])
10332cd2800SGreg Roach                ->map(function (Individual $individual): string {
10432cd2800SGreg Roach                    return $this->getPersonLi($individual);
10532cd2800SGreg Roach                })
10632cd2800SGreg Roach                ->implode('');
1078c2e8227SGreg Roach        }
1088c2e8227SGreg Roach
1091c0676b2SGreg Roach        if ($html !== '') {
1101c0676b2SGreg Roach            $html = '<ul>' . $html . '</ul>';
1111c0676b2SGreg Roach        }
1121c0676b2SGreg Roach
1136ccdf4f0SGreg Roach        return response($html);
1141c0676b2SGreg Roach    }
1151c0676b2SGreg Roach
1161c0676b2SGreg Roach    /**
1176ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
1181c0676b2SGreg Roach     *
1196ccdf4f0SGreg Roach     * @return ResponseInterface
1201c0676b2SGreg Roach     */
12157ab2231SGreg Roach    public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface
122c1010edaSGreg Roach    {
12357ab2231SGreg Roach        $tree = $request->getAttribute('tree');
12475964c75SGreg Roach        assert($tree instanceof Tree);
1255229eadeSGreg Roach
12618c571c3SGreg Roach        $xref = $request->getQueryParams()['xref'] ?? '';
1271c0676b2SGreg Roach
1286b9cb339SGreg Roach        $individual = Registry::individualFactory()->make($xref, $tree);
1291c0676b2SGreg Roach
1301c0676b2SGreg Roach        if ($individual !== null && $individual->canShow()) {
1311c0676b2SGreg Roach            $html = $this->loadSpouses($individual, 1);
1321c0676b2SGreg Roach        } else {
1331c0676b2SGreg Roach            $html = '';
1341c0676b2SGreg Roach        }
1351c0676b2SGreg Roach
1366ccdf4f0SGreg Roach        return response($html);
1371c0676b2SGreg Roach    }
1381c0676b2SGreg Roach
1390dcd9387SGreg Roach    /**
1400dcd9387SGreg Roach     * @param Individual $individual
1410dcd9387SGreg Roach     *
1420dcd9387SGreg Roach     * @return bool
1430dcd9387SGreg Roach     */
1448f53f488SRico Sonntag    public function hasSidebarContent(Individual $individual): bool
145c1010edaSGreg Roach    {
14638a9583bSGreg Roach        return true;
1478c2e8227SGreg Roach    }
1488c2e8227SGreg Roach
14976692c8bSGreg Roach    /**
15076692c8bSGreg Roach     * Load this sidebar synchronously.
15176692c8bSGreg Roach     *
152225e381fSGreg Roach     * @param Individual $individual
153225e381fSGreg Roach     *
15476692c8bSGreg Roach     * @return string
15576692c8bSGreg Roach     */
1568f53f488SRico Sonntag    public function getSidebarContent(Individual $individual): string
157c1010edaSGreg Roach    {
1581ef93f16SGreg Roach        return view('modules/descendancy/sidebar', [
159a817de92SGreg Roach            'individual_list' => $this->getPersonLi($individual, 1),
160ef5d23f1SGreg Roach            'tree'            => $individual->tree(),
161a817de92SGreg Roach        ]);
1628c2e8227SGreg Roach    }
1638c2e8227SGreg Roach
1648c2e8227SGreg Roach    /**
16576692c8bSGreg Roach     * Format an individual in a list.
16676692c8bSGreg Roach     *
1678c2e8227SGreg Roach     * @param Individual $person
168cbc1590aSGreg Roach     * @param int        $generations
1698c2e8227SGreg Roach     *
1708c2e8227SGreg Roach     * @return string
1718c2e8227SGreg Roach     */
172*73d58381SGreg Roach    public function getPersonLi(Individual $person, int $generations = 0): string
173c1010edaSGreg Roach    {
1748c2e8227SGreg Roach        $icon     = $generations > 0 ? 'icon-minus' : 'icon-plus';
1755e6816beSGreg Roach        $lifespan = $person->canShow() ? '(' . $person->lifespan() . ')' : '';
1768c2e8227SGreg Roach        $spouses  = $generations > 0 ? $this->loadSpouses($person, 0) : '';
1772622ba92SGreg Roach
1782622ba92SGreg Roach        return
1792622ba92SGreg Roach            '<li class="sb_desc_indi_li">' .
180d4786c66SGreg Roach            '<a class="sb_desc_indi" href="#" data-wt-href="' . e(route('module', [
18126684e68SGreg Roach                'module' => $this->name(),
182c1010edaSGreg Roach                'action' => 'Descendants',
1839022ab66SGreg Roach                'tree'    => $person->tree()->name(),
184c0935879SGreg Roach                'xref'   => $person->xref(),
185c1010edaSGreg Roach            ])) . '">' .
1862622ba92SGreg Roach            '<i class="plusminus ' . $icon . '"></i>' .
18708362db4SGreg Roach            '<small>' . view('icons/sex', ['sex' => $person->sex()]) . '</small>' . $person->fullName() . $lifespan .
1882622ba92SGreg Roach            '</a>' .
18939ca88baSGreg Roach            '<a href="' . e($person->url()) . '" title="' . strip_tags($person->fullName()) . '">' . view('icons/individual') . '</a>' .
1902622ba92SGreg Roach            '<div>' . $spouses . '</div>' .
1912622ba92SGreg Roach            '</li>';
1928c2e8227SGreg Roach    }
1938c2e8227SGreg Roach
1948c2e8227SGreg Roach    /**
19576692c8bSGreg Roach     * Format a family in a list.
19676692c8bSGreg Roach     *
1978c2e8227SGreg Roach     * @param Family     $family
1988c2e8227SGreg Roach     * @param Individual $person
199cbc1590aSGreg Roach     * @param int        $generations
2008c2e8227SGreg Roach     *
2018c2e8227SGreg Roach     * @return string
2028c2e8227SGreg Roach     */
203*73d58381SGreg Roach    public function getFamilyLi(Family $family, Individual $person, int $generations = 0): string
204c1010edaSGreg Roach    {
20539ca88baSGreg Roach        $spouse = $family->spouse($person);
2064890720dSGreg Roach        if ($spouse instanceof Individual) {
20708362db4SGreg Roach            $spouse_name = '<small>' . view('icons/sex', ['sex' => $spouse->sex()]) . '</small>' . $spouse->fullName();
2084890720dSGreg Roach            $spouse_link = '<a href="' . e($spouse->url()) . '" title="' . strip_tags($spouse->fullName()) . '">' . view('icons/individual') . '</a>';
2092622ba92SGreg Roach        } else {
2102622ba92SGreg Roach            $spouse_name = '';
2112622ba92SGreg Roach            $spouse_link = '';
2122622ba92SGreg Roach        }
2132622ba92SGreg Roach
21439ca88baSGreg Roach        $family_link = '<a href="' . e($family->url()) . '" title="' . strip_tags($family->fullName()) . '">' . view('icons/family') . '</a>';
21539b853a7SGreg Roach
2168c2e8227SGreg Roach        $marryear = $family->getMarriageYear();
2178c2e8227SGreg Roach        $marr     = $marryear ? '<i class="icon-rings"></i>' . $marryear : '';
2182622ba92SGreg Roach
2192622ba92SGreg Roach        return
2202622ba92SGreg Roach            '<li class="sb_desc_indi_li">' .
221d4786c66SGreg Roach            '<a class="sb_desc_indi" href="#" data-wt-href="#"><i class="plusminus icon-minus"></i>' .
22239b853a7SGreg Roach            $spouse_name .
22339b853a7SGreg Roach            $marr .
22439b853a7SGreg Roach            '</a>' .
2252622ba92SGreg Roach            $spouse_link .
22639b853a7SGreg Roach            $family_link .
2272622ba92SGreg Roach            '<div>' . $this->loadChildren($family, $generations) . '</div>' .
2282622ba92SGreg Roach            '</li>';
2298c2e8227SGreg Roach    }
2308c2e8227SGreg Roach
2318c2e8227SGreg Roach    /**
23276692c8bSGreg Roach     * Display spouses.
23376692c8bSGreg Roach     *
23449a243cbSGreg Roach     * @param Individual $individual
235cbc1590aSGreg Roach     * @param int        $generations
2368c2e8227SGreg Roach     *
2378c2e8227SGreg Roach     * @return string
2388c2e8227SGreg Roach     */
23924f2a3afSGreg Roach    public function loadSpouses(Individual $individual, int $generations): string
240c1010edaSGreg Roach    {
2418c2e8227SGreg Roach        $out = '';
24249a243cbSGreg Roach        if ($individual->canShow()) {
24339ca88baSGreg Roach            foreach ($individual->spouseFamilies() as $family) {
24449a243cbSGreg Roach                $out .= $this->getFamilyLi($family, $individual, $generations - 1);
2458c2e8227SGreg Roach            }
2468c2e8227SGreg Roach        }
2478c2e8227SGreg Roach        if ($out) {
2488c2e8227SGreg Roach            return '<ul>' . $out . '</ul>';
2498c2e8227SGreg Roach        }
250b2ce94c6SRico Sonntag
251b2ce94c6SRico Sonntag        return '';
2528c2e8227SGreg Roach    }
2538c2e8227SGreg Roach
2548c2e8227SGreg Roach    /**
25576692c8bSGreg Roach     * Display descendants.
25676692c8bSGreg Roach     *
2578c2e8227SGreg Roach     * @param Family $family
258cbc1590aSGreg Roach     * @param int    $generations
2598c2e8227SGreg Roach     *
2608c2e8227SGreg Roach     * @return string
2618c2e8227SGreg Roach     */
26224f2a3afSGreg Roach    public function loadChildren(Family $family, int $generations): string
263c1010edaSGreg Roach    {
2648c2e8227SGreg Roach        $out = '';
2658c2e8227SGreg Roach        if ($family->canShow()) {
26639ca88baSGreg Roach            $children = $family->children();
267820b62dfSGreg Roach
268820b62dfSGreg Roach            if ($children->isNotEmpty()) {
2698c2e8227SGreg Roach                foreach ($children as $child) {
2708c2e8227SGreg Roach                    $out .= $this->getPersonLi($child, $generations - 1);
2718c2e8227SGreg Roach                }
2728c2e8227SGreg Roach            } else {
2738c2e8227SGreg Roach                $out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>';
2748c2e8227SGreg Roach            }
2758c2e8227SGreg Roach        }
2768c2e8227SGreg Roach        if ($out) {
2778c2e8227SGreg Roach            return '<ul>' . $out . '</ul>';
2788c2e8227SGreg Roach        }
279b2ce94c6SRico Sonntag
280b2ce94c6SRico Sonntag        return '';
2818c2e8227SGreg Roach    }
2828c2e8227SGreg Roach}
283