18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 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 * @param SearchService $search_service 4557ab2231SGreg Roach */ 463976b470SGreg Roach public function __construct(SearchService $search_service) 473976b470SGreg Roach { 4857ab2231SGreg Roach $this->search_service = $search_service; 4957ab2231SGreg Roach } 5057ab2231SGreg Roach 51961ec755SGreg Roach /** 520cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 53961ec755SGreg Roach * 54961ec755SGreg Roach * @return string 55961ec755SGreg Roach */ 5649a243cbSGreg Roach public function title(): string 57c1010edaSGreg Roach { 58bbb76c12SGreg Roach /* I18N: Name of a module/sidebar */ 59bbb76c12SGreg Roach return I18N::translate('Descendants'); 608c2e8227SGreg Roach } 618c2e8227SGreg Roach 6249a243cbSGreg Roach public function description(): string 63c1010edaSGreg Roach { 64bbb76c12SGreg Roach /* I18N: Description of the “Descendants” module */ 65bbb76c12SGreg Roach return I18N::translate('A sidebar showing the descendants of an individual.'); 668c2e8227SGreg Roach } 678c2e8227SGreg Roach 6876692c8bSGreg Roach /** 6949a243cbSGreg Roach * The default position for this sidebar. It can be changed in the control panel. 7049a243cbSGreg Roach * 7149a243cbSGreg Roach * @return int 7249a243cbSGreg Roach */ 7349a243cbSGreg Roach public function defaultSidebarOrder(): int 7449a243cbSGreg Roach { 75353b36abSGreg Roach return 3; 7649a243cbSGreg Roach } 7749a243cbSGreg Roach 7849a243cbSGreg Roach /** 796ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8076692c8bSGreg Roach * 816ccdf4f0SGreg Roach * @return ResponseInterface 8276692c8bSGreg Roach */ 8357ab2231SGreg Roach public function getSearchAction(ServerRequestInterface $request): ResponseInterface 84c1010edaSGreg Roach { 85b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 86748dbe15SGreg Roach $search = Validator::queryParams($request)->string('search'); 878c2e8227SGreg Roach 881c0676b2SGreg Roach $html = ''; 891c0676b2SGreg Roach 901c0676b2SGreg Roach if (strlen($search) >= 2) { 9157ab2231SGreg Roach $html = $this->search_service 92a7a24840SGreg Roach ->searchIndividualNames([$tree], [$search]) 93*f25fc0f9SGreg Roach ->map(fn (Individual $individual): string => $this->getPersonLi($individual)) 9432cd2800SGreg Roach ->implode(''); 958c2e8227SGreg Roach } 968c2e8227SGreg Roach 971c0676b2SGreg Roach if ($html !== '') { 981c0676b2SGreg Roach $html = '<ul>' . $html . '</ul>'; 991c0676b2SGreg Roach } 1001c0676b2SGreg Roach 1016ccdf4f0SGreg Roach return response($html); 1021c0676b2SGreg Roach } 1031c0676b2SGreg Roach 1041c0676b2SGreg Roach /** 1056ccdf4f0SGreg Roach * @param ServerRequestInterface $request 1061c0676b2SGreg Roach * 1076ccdf4f0SGreg Roach * @return ResponseInterface 1081c0676b2SGreg Roach */ 10957ab2231SGreg Roach public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface 110c1010edaSGreg Roach { 111b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 112748dbe15SGreg Roach $xref = Validator::queryParams($request)->isXref()->string('xref'); 1131c0676b2SGreg Roach 1146b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 1151c0676b2SGreg Roach 1161c0676b2SGreg Roach if ($individual !== null && $individual->canShow()) { 1171c0676b2SGreg Roach $html = $this->loadSpouses($individual, 1); 1181c0676b2SGreg Roach } else { 1191c0676b2SGreg Roach $html = ''; 1201c0676b2SGreg Roach } 1211c0676b2SGreg Roach 1226ccdf4f0SGreg Roach return response($html); 1231c0676b2SGreg Roach } 1241c0676b2SGreg Roach 1250dcd9387SGreg Roach /** 1260dcd9387SGreg Roach * @param Individual $individual 1270dcd9387SGreg Roach * 1280dcd9387SGreg Roach * @return bool 1290dcd9387SGreg Roach */ 1308f53f488SRico Sonntag public function hasSidebarContent(Individual $individual): bool 131c1010edaSGreg Roach { 13238a9583bSGreg Roach return true; 1338c2e8227SGreg Roach } 1348c2e8227SGreg Roach 13576692c8bSGreg Roach /** 13676692c8bSGreg Roach * Load this sidebar synchronously. 13776692c8bSGreg Roach * 138225e381fSGreg Roach * @param Individual $individual 139225e381fSGreg Roach * 14076692c8bSGreg Roach * @return string 14176692c8bSGreg Roach */ 1428f53f488SRico Sonntag public function getSidebarContent(Individual $individual): string 143c1010edaSGreg Roach { 1441ef93f16SGreg Roach return view('modules/descendancy/sidebar', [ 145a817de92SGreg Roach 'individual_list' => $this->getPersonLi($individual, 1), 146ef5d23f1SGreg Roach 'tree' => $individual->tree(), 147a817de92SGreg Roach ]); 1488c2e8227SGreg Roach } 1498c2e8227SGreg Roach 1508c2e8227SGreg Roach /** 15176692c8bSGreg Roach * Format an individual in a list. 15276692c8bSGreg Roach * 1538c2e8227SGreg Roach * @param Individual $person 154cbc1590aSGreg Roach * @param int $generations 1558c2e8227SGreg Roach * 1568c2e8227SGreg Roach * @return string 1578c2e8227SGreg Roach */ 15873d58381SGreg Roach public function getPersonLi(Individual $person, int $generations = 0): string 159c1010edaSGreg Roach { 1608c2e8227SGreg Roach $icon = $generations > 0 ? 'icon-minus' : 'icon-plus'; 1615e6816beSGreg Roach $lifespan = $person->canShow() ? '(' . $person->lifespan() . ')' : ''; 1628c2e8227SGreg Roach $spouses = $generations > 0 ? $this->loadSpouses($person, 0) : ''; 1632622ba92SGreg Roach 1642622ba92SGreg Roach return 1652622ba92SGreg Roach '<li class="sb_desc_indi_li">' . 166d4786c66SGreg Roach '<a class="sb_desc_indi" href="#" data-wt-href="' . e(route('module', [ 16726684e68SGreg Roach 'module' => $this->name(), 168c1010edaSGreg Roach 'action' => 'Descendants', 1699022ab66SGreg Roach 'tree' => $person->tree()->name(), 170c0935879SGreg Roach 'xref' => $person->xref(), 171c1010edaSGreg Roach ])) . '">' . 1722622ba92SGreg Roach '<i class="plusminus ' . $icon . '"></i>' . 17308362db4SGreg Roach '<small>' . view('icons/sex', ['sex' => $person->sex()]) . '</small>' . $person->fullName() . $lifespan . 1742622ba92SGreg Roach '</a>' . 17539ca88baSGreg Roach '<a href="' . e($person->url()) . '" title="' . strip_tags($person->fullName()) . '">' . view('icons/individual') . '</a>' . 1762622ba92SGreg Roach '<div>' . $spouses . '</div>' . 1772622ba92SGreg Roach '</li>'; 1788c2e8227SGreg Roach } 1798c2e8227SGreg Roach 1808c2e8227SGreg Roach /** 18176692c8bSGreg Roach * Format a family in a list. 18276692c8bSGreg Roach * 1838c2e8227SGreg Roach * @param Family $family 1848c2e8227SGreg Roach * @param Individual $person 185cbc1590aSGreg Roach * @param int $generations 1868c2e8227SGreg Roach * 1878c2e8227SGreg Roach * @return string 1888c2e8227SGreg Roach */ 18973d58381SGreg Roach public function getFamilyLi(Family $family, Individual $person, int $generations = 0): string 190c1010edaSGreg Roach { 19139ca88baSGreg Roach $spouse = $family->spouse($person); 1924890720dSGreg Roach if ($spouse instanceof Individual) { 19308362db4SGreg Roach $spouse_name = '<small>' . view('icons/sex', ['sex' => $spouse->sex()]) . '</small>' . $spouse->fullName(); 1944890720dSGreg Roach $spouse_link = '<a href="' . e($spouse->url()) . '" title="' . strip_tags($spouse->fullName()) . '">' . view('icons/individual') . '</a>'; 1952622ba92SGreg Roach } else { 1962622ba92SGreg Roach $spouse_name = ''; 1972622ba92SGreg Roach $spouse_link = ''; 1982622ba92SGreg Roach } 1992622ba92SGreg Roach 20039ca88baSGreg Roach $family_link = '<a href="' . e($family->url()) . '" title="' . strip_tags($family->fullName()) . '">' . view('icons/family') . '</a>'; 20139b853a7SGreg Roach 2028c2e8227SGreg Roach $marryear = $family->getMarriageYear(); 2038c2e8227SGreg Roach $marr = $marryear ? '<i class="icon-rings"></i>' . $marryear : ''; 2042622ba92SGreg Roach 2052622ba92SGreg Roach return 2062622ba92SGreg Roach '<li class="sb_desc_indi_li">' . 207d4786c66SGreg Roach '<a class="sb_desc_indi" href="#" data-wt-href="#"><i class="plusminus icon-minus"></i>' . 20839b853a7SGreg Roach $spouse_name . 20939b853a7SGreg Roach $marr . 21039b853a7SGreg Roach '</a>' . 2112622ba92SGreg Roach $spouse_link . 21239b853a7SGreg Roach $family_link . 2132622ba92SGreg Roach '<div>' . $this->loadChildren($family, $generations) . '</div>' . 2142622ba92SGreg Roach '</li>'; 2158c2e8227SGreg Roach } 2168c2e8227SGreg Roach 2178c2e8227SGreg Roach /** 21876692c8bSGreg Roach * Display spouses. 21976692c8bSGreg Roach * 22049a243cbSGreg Roach * @param Individual $individual 221cbc1590aSGreg Roach * @param int $generations 2228c2e8227SGreg Roach * 2238c2e8227SGreg Roach * @return string 2248c2e8227SGreg Roach */ 22524f2a3afSGreg Roach public function loadSpouses(Individual $individual, int $generations): string 226c1010edaSGreg Roach { 2278c2e8227SGreg Roach $out = ''; 22849a243cbSGreg Roach if ($individual->canShow()) { 22939ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 23049a243cbSGreg Roach $out .= $this->getFamilyLi($family, $individual, $generations - 1); 2318c2e8227SGreg Roach } 2328c2e8227SGreg Roach } 233b6ec1ccfSGreg Roach if ($out !== '') { 2348c2e8227SGreg Roach return '<ul>' . $out . '</ul>'; 2358c2e8227SGreg Roach } 236b2ce94c6SRico Sonntag 237b2ce94c6SRico Sonntag return ''; 2388c2e8227SGreg Roach } 2398c2e8227SGreg Roach 2408c2e8227SGreg Roach /** 24176692c8bSGreg Roach * Display descendants. 24276692c8bSGreg Roach * 2438c2e8227SGreg Roach * @param Family $family 244cbc1590aSGreg Roach * @param int $generations 2458c2e8227SGreg Roach * 2468c2e8227SGreg Roach * @return string 2478c2e8227SGreg Roach */ 24824f2a3afSGreg Roach public function loadChildren(Family $family, int $generations): string 249c1010edaSGreg Roach { 2508c2e8227SGreg Roach $out = ''; 2518c2e8227SGreg Roach if ($family->canShow()) { 25239ca88baSGreg Roach $children = $family->children(); 253820b62dfSGreg Roach 254820b62dfSGreg Roach if ($children->isNotEmpty()) { 2558c2e8227SGreg Roach foreach ($children as $child) { 2568c2e8227SGreg Roach $out .= $this->getPersonLi($child, $generations - 1); 2578c2e8227SGreg Roach } 2588c2e8227SGreg Roach } else { 2598c2e8227SGreg Roach $out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>'; 2608c2e8227SGreg Roach } 2618c2e8227SGreg Roach } 262b6ec1ccfSGreg Roach if ($out !== '') { 2638c2e8227SGreg Roach return '<ul>' . $out . '</ul>'; 2648c2e8227SGreg Roach } 265b2ce94c6SRico Sonntag 266b2ce94c6SRico Sonntag return ''; 2678c2e8227SGreg Roach } 2688c2e8227SGreg Roach} 269