xref: /webtrees/app/Module/DescendancyModule.php (revision 748dbe155a6d19d66918ad136947fa23ee8f8469)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
55bfc6897SGreg 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
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
25f0c88a96SGreg Roachuse Fisharebest\Webtrees\Registry;
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
3110e06497SGreg Roachuse function strlen;
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
4143f2f523SGreg Roach    private SearchService $search_service;
4257ab2231SGreg Roach
4357ab2231SGreg Roach    /**
4457ab2231SGreg Roach     * DescendancyModule constructor.
4557ab2231SGreg Roach     *
4657ab2231SGreg Roach     * @param SearchService $search_service
4757ab2231SGreg Roach     */
483976b470SGreg Roach    public function __construct(SearchService $search_service)
493976b470SGreg Roach    {
5057ab2231SGreg Roach        $this->search_service = $search_service;
5157ab2231SGreg Roach    }
5257ab2231SGreg Roach
53961ec755SGreg Roach    /**
540cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
55961ec755SGreg Roach     *
56961ec755SGreg Roach     * @return string
57961ec755SGreg Roach     */
5849a243cbSGreg Roach    public function title(): string
59c1010edaSGreg Roach    {
60bbb76c12SGreg Roach        /* I18N: Name of a module/sidebar */
61bbb76c12SGreg Roach        return I18N::translate('Descendants');
628c2e8227SGreg Roach    }
638c2e8227SGreg Roach
64961ec755SGreg Roach    /**
65961ec755SGreg Roach     * A sentence describing what this module does.
66961ec755SGreg Roach     *
67961ec755SGreg Roach     * @return string
68961ec755SGreg Roach     */
6949a243cbSGreg Roach    public function description(): string
70c1010edaSGreg Roach    {
71bbb76c12SGreg Roach        /* I18N: Description of the “Descendants” module */
72bbb76c12SGreg Roach        return I18N::translate('A sidebar showing the descendants of an individual.');
738c2e8227SGreg Roach    }
748c2e8227SGreg Roach
7576692c8bSGreg Roach    /**
7649a243cbSGreg Roach     * The default position for this sidebar.  It can be changed in the control panel.
7749a243cbSGreg Roach     *
7849a243cbSGreg Roach     * @return int
7949a243cbSGreg Roach     */
8049a243cbSGreg Roach    public function defaultSidebarOrder(): int
8149a243cbSGreg Roach    {
82353b36abSGreg Roach        return 3;
8349a243cbSGreg Roach    }
8449a243cbSGreg Roach
8549a243cbSGreg Roach    /**
866ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
8776692c8bSGreg Roach     *
886ccdf4f0SGreg Roach     * @return ResponseInterface
8976692c8bSGreg Roach     */
9057ab2231SGreg Roach    public function getSearchAction(ServerRequestInterface $request): ResponseInterface
91c1010edaSGreg Roach    {
92b55cbc6bSGreg Roach        $tree   = Validator::attributes($request)->tree();
93*748dbe15SGreg Roach        $search = Validator::queryParams($request)->string('search');
948c2e8227SGreg Roach
951c0676b2SGreg Roach        $html = '';
961c0676b2SGreg Roach
971c0676b2SGreg Roach        if (strlen($search) >= 2) {
9857ab2231SGreg Roach            $html = $this->search_service
99a7a24840SGreg Roach                ->searchIndividualNames([$tree], [$search])
10032cd2800SGreg Roach                ->map(function (Individual $individual): string {
10132cd2800SGreg Roach                    return $this->getPersonLi($individual);
10232cd2800SGreg Roach                })
10332cd2800SGreg Roach                ->implode('');
1048c2e8227SGreg Roach        }
1058c2e8227SGreg Roach
1061c0676b2SGreg Roach        if ($html !== '') {
1071c0676b2SGreg Roach            $html = '<ul>' . $html . '</ul>';
1081c0676b2SGreg Roach        }
1091c0676b2SGreg Roach
1106ccdf4f0SGreg Roach        return response($html);
1111c0676b2SGreg Roach    }
1121c0676b2SGreg Roach
1131c0676b2SGreg Roach    /**
1146ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
1151c0676b2SGreg Roach     *
1166ccdf4f0SGreg Roach     * @return ResponseInterface
1171c0676b2SGreg Roach     */
11857ab2231SGreg Roach    public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface
119c1010edaSGreg Roach    {
120b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
121*748dbe15SGreg Roach        $xref = Validator::queryParams($request)->isXref()->string('xref');
1221c0676b2SGreg Roach
1236b9cb339SGreg Roach        $individual = Registry::individualFactory()->make($xref, $tree);
1241c0676b2SGreg Roach
1251c0676b2SGreg Roach        if ($individual !== null && $individual->canShow()) {
1261c0676b2SGreg Roach            $html = $this->loadSpouses($individual, 1);
1271c0676b2SGreg Roach        } else {
1281c0676b2SGreg Roach            $html = '';
1291c0676b2SGreg Roach        }
1301c0676b2SGreg Roach
1316ccdf4f0SGreg Roach        return response($html);
1321c0676b2SGreg Roach    }
1331c0676b2SGreg Roach
1340dcd9387SGreg Roach    /**
1350dcd9387SGreg Roach     * @param Individual $individual
1360dcd9387SGreg Roach     *
1370dcd9387SGreg Roach     * @return bool
1380dcd9387SGreg Roach     */
1398f53f488SRico Sonntag    public function hasSidebarContent(Individual $individual): bool
140c1010edaSGreg Roach    {
14138a9583bSGreg Roach        return true;
1428c2e8227SGreg Roach    }
1438c2e8227SGreg Roach
14476692c8bSGreg Roach    /**
14576692c8bSGreg Roach     * Load this sidebar synchronously.
14676692c8bSGreg Roach     *
147225e381fSGreg Roach     * @param Individual $individual
148225e381fSGreg Roach     *
14976692c8bSGreg Roach     * @return string
15076692c8bSGreg Roach     */
1518f53f488SRico Sonntag    public function getSidebarContent(Individual $individual): string
152c1010edaSGreg Roach    {
1531ef93f16SGreg Roach        return view('modules/descendancy/sidebar', [
154a817de92SGreg Roach            'individual_list' => $this->getPersonLi($individual, 1),
155ef5d23f1SGreg Roach            'tree'            => $individual->tree(),
156a817de92SGreg Roach        ]);
1578c2e8227SGreg Roach    }
1588c2e8227SGreg Roach
1598c2e8227SGreg Roach    /**
16076692c8bSGreg Roach     * Format an individual in a list.
16176692c8bSGreg Roach     *
1628c2e8227SGreg Roach     * @param Individual $person
163cbc1590aSGreg Roach     * @param int        $generations
1648c2e8227SGreg Roach     *
1658c2e8227SGreg Roach     * @return string
1668c2e8227SGreg Roach     */
16773d58381SGreg Roach    public function getPersonLi(Individual $person, int $generations = 0): string
168c1010edaSGreg Roach    {
1698c2e8227SGreg Roach        $icon     = $generations > 0 ? 'icon-minus' : 'icon-plus';
1705e6816beSGreg Roach        $lifespan = $person->canShow() ? '(' . $person->lifespan() . ')' : '';
1718c2e8227SGreg Roach        $spouses  = $generations > 0 ? $this->loadSpouses($person, 0) : '';
1722622ba92SGreg Roach
1732622ba92SGreg Roach        return
1742622ba92SGreg Roach            '<li class="sb_desc_indi_li">' .
175d4786c66SGreg Roach            '<a class="sb_desc_indi" href="#" data-wt-href="' . e(route('module', [
17626684e68SGreg Roach                'module' => $this->name(),
177c1010edaSGreg Roach                'action' => 'Descendants',
1789022ab66SGreg Roach                'tree'    => $person->tree()->name(),
179c0935879SGreg Roach                'xref'   => $person->xref(),
180c1010edaSGreg Roach            ])) . '">' .
1812622ba92SGreg Roach            '<i class="plusminus ' . $icon . '"></i>' .
18208362db4SGreg Roach            '<small>' . view('icons/sex', ['sex' => $person->sex()]) . '</small>' . $person->fullName() . $lifespan .
1832622ba92SGreg Roach            '</a>' .
18439ca88baSGreg Roach            '<a href="' . e($person->url()) . '" title="' . strip_tags($person->fullName()) . '">' . view('icons/individual') . '</a>' .
1852622ba92SGreg Roach            '<div>' . $spouses . '</div>' .
1862622ba92SGreg Roach            '</li>';
1878c2e8227SGreg Roach    }
1888c2e8227SGreg Roach
1898c2e8227SGreg Roach    /**
19076692c8bSGreg Roach     * Format a family in a list.
19176692c8bSGreg Roach     *
1928c2e8227SGreg Roach     * @param Family     $family
1938c2e8227SGreg Roach     * @param Individual $person
194cbc1590aSGreg Roach     * @param int        $generations
1958c2e8227SGreg Roach     *
1968c2e8227SGreg Roach     * @return string
1978c2e8227SGreg Roach     */
19873d58381SGreg Roach    public function getFamilyLi(Family $family, Individual $person, int $generations = 0): string
199c1010edaSGreg Roach    {
20039ca88baSGreg Roach        $spouse = $family->spouse($person);
2014890720dSGreg Roach        if ($spouse instanceof Individual) {
20208362db4SGreg Roach            $spouse_name = '<small>' . view('icons/sex', ['sex' => $spouse->sex()]) . '</small>' . $spouse->fullName();
2034890720dSGreg Roach            $spouse_link = '<a href="' . e($spouse->url()) . '" title="' . strip_tags($spouse->fullName()) . '">' . view('icons/individual') . '</a>';
2042622ba92SGreg Roach        } else {
2052622ba92SGreg Roach            $spouse_name = '';
2062622ba92SGreg Roach            $spouse_link = '';
2072622ba92SGreg Roach        }
2082622ba92SGreg Roach
20939ca88baSGreg Roach        $family_link = '<a href="' . e($family->url()) . '" title="' . strip_tags($family->fullName()) . '">' . view('icons/family') . '</a>';
21039b853a7SGreg Roach
2118c2e8227SGreg Roach        $marryear = $family->getMarriageYear();
2128c2e8227SGreg Roach        $marr     = $marryear ? '<i class="icon-rings"></i>' . $marryear : '';
2132622ba92SGreg Roach
2142622ba92SGreg Roach        return
2152622ba92SGreg Roach            '<li class="sb_desc_indi_li">' .
216d4786c66SGreg Roach            '<a class="sb_desc_indi" href="#" data-wt-href="#"><i class="plusminus icon-minus"></i>' .
21739b853a7SGreg Roach            $spouse_name .
21839b853a7SGreg Roach            $marr .
21939b853a7SGreg Roach            '</a>' .
2202622ba92SGreg Roach            $spouse_link .
22139b853a7SGreg Roach            $family_link .
2222622ba92SGreg Roach            '<div>' . $this->loadChildren($family, $generations) . '</div>' .
2232622ba92SGreg Roach            '</li>';
2248c2e8227SGreg Roach    }
2258c2e8227SGreg Roach
2268c2e8227SGreg Roach    /**
22776692c8bSGreg Roach     * Display spouses.
22876692c8bSGreg Roach     *
22949a243cbSGreg Roach     * @param Individual $individual
230cbc1590aSGreg Roach     * @param int        $generations
2318c2e8227SGreg Roach     *
2328c2e8227SGreg Roach     * @return string
2338c2e8227SGreg Roach     */
23424f2a3afSGreg Roach    public function loadSpouses(Individual $individual, int $generations): string
235c1010edaSGreg Roach    {
2368c2e8227SGreg Roach        $out = '';
23749a243cbSGreg Roach        if ($individual->canShow()) {
23839ca88baSGreg Roach            foreach ($individual->spouseFamilies() as $family) {
23949a243cbSGreg Roach                $out .= $this->getFamilyLi($family, $individual, $generations - 1);
2408c2e8227SGreg Roach            }
2418c2e8227SGreg Roach        }
2428c2e8227SGreg Roach        if ($out) {
2438c2e8227SGreg Roach            return '<ul>' . $out . '</ul>';
2448c2e8227SGreg Roach        }
245b2ce94c6SRico Sonntag
246b2ce94c6SRico Sonntag        return '';
2478c2e8227SGreg Roach    }
2488c2e8227SGreg Roach
2498c2e8227SGreg Roach    /**
25076692c8bSGreg Roach     * Display descendants.
25176692c8bSGreg Roach     *
2528c2e8227SGreg Roach     * @param Family $family
253cbc1590aSGreg Roach     * @param int    $generations
2548c2e8227SGreg Roach     *
2558c2e8227SGreg Roach     * @return string
2568c2e8227SGreg Roach     */
25724f2a3afSGreg Roach    public function loadChildren(Family $family, int $generations): string
258c1010edaSGreg Roach    {
2598c2e8227SGreg Roach        $out = '';
2608c2e8227SGreg Roach        if ($family->canShow()) {
26139ca88baSGreg Roach            $children = $family->children();
262820b62dfSGreg Roach
263820b62dfSGreg Roach            if ($children->isNotEmpty()) {
2648c2e8227SGreg Roach                foreach ($children as $child) {
2658c2e8227SGreg Roach                    $out .= $this->getPersonLi($child, $generations - 1);
2668c2e8227SGreg Roach                }
2678c2e8227SGreg Roach            } else {
2688c2e8227SGreg Roach                $out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>';
2698c2e8227SGreg Roach            }
2708c2e8227SGreg Roach        }
2718c2e8227SGreg Roach        if ($out) {
2728c2e8227SGreg Roach            return '<ul>' . $out . '</ul>';
2738c2e8227SGreg Roach        }
274b2ce94c6SRico Sonntag
275b2ce94c6SRico Sonntag        return '';
2768c2e8227SGreg Roach    }
2778c2e8227SGreg Roach}
278