18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 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 158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 168c2e8227SGreg Roach */ 17e7f56f2aSGreg Roachdeclare(strict_types=1); 18e7f56f2aSGreg Roach 1976692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2076692c8bSGreg Roach 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual; 2432cd2800SGreg Roachuse Fisharebest\Webtrees\Services\SearchService; 256ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 266ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 273976b470SGreg Roach 28032918ffSGreg Roachuse function view; 298c2e8227SGreg Roach 308c2e8227SGreg Roach/** 318c2e8227SGreg Roach * Class DescendancyModule 328c2e8227SGreg Roach */ 3337eb8894SGreg Roachclass DescendancyModule extends AbstractModule implements ModuleSidebarInterface 34c1010edaSGreg Roach{ 3549a243cbSGreg Roach use ModuleSidebarTrait; 3649a243cbSGreg Roach 3757ab2231SGreg Roach /** @var SearchService */ 3857ab2231SGreg Roach private $search_service; 3957ab2231SGreg Roach 4057ab2231SGreg Roach /** 4157ab2231SGreg Roach * DescendancyModule constructor. 4257ab2231SGreg Roach * 4357ab2231SGreg Roach * @param SearchService $search_service 4457ab2231SGreg Roach */ 453976b470SGreg Roach public function __construct(SearchService $search_service) 463976b470SGreg Roach { 4757ab2231SGreg Roach $this->search_service = $search_service; 4857ab2231SGreg Roach } 4957ab2231SGreg Roach 50961ec755SGreg Roach /** 510cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 52961ec755SGreg Roach * 53961ec755SGreg Roach * @return string 54961ec755SGreg Roach */ 5549a243cbSGreg Roach public function title(): string 56c1010edaSGreg Roach { 57bbb76c12SGreg Roach /* I18N: Name of a module/sidebar */ 58bbb76c12SGreg Roach return I18N::translate('Descendants'); 598c2e8227SGreg Roach } 608c2e8227SGreg Roach 61961ec755SGreg Roach /** 62961ec755SGreg Roach * A sentence describing what this module does. 63961ec755SGreg Roach * 64961ec755SGreg Roach * @return string 65961ec755SGreg Roach */ 6649a243cbSGreg Roach public function description(): string 67c1010edaSGreg Roach { 68bbb76c12SGreg Roach /* I18N: Description of the “Descendants” module */ 69bbb76c12SGreg Roach return I18N::translate('A sidebar showing the descendants of an individual.'); 708c2e8227SGreg Roach } 718c2e8227SGreg Roach 7276692c8bSGreg Roach /** 7349a243cbSGreg Roach * The default position for this sidebar. It can be changed in the control panel. 7449a243cbSGreg Roach * 7549a243cbSGreg Roach * @return int 7649a243cbSGreg Roach */ 7749a243cbSGreg Roach public function defaultSidebarOrder(): int 7849a243cbSGreg Roach { 79353b36abSGreg Roach return 3; 8049a243cbSGreg Roach } 8149a243cbSGreg Roach 8249a243cbSGreg Roach /** 836ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8476692c8bSGreg Roach * 856ccdf4f0SGreg Roach * @return ResponseInterface 8676692c8bSGreg Roach */ 8757ab2231SGreg Roach public function getSearchAction(ServerRequestInterface $request): ResponseInterface 88c1010edaSGreg Roach { 8957ab2231SGreg Roach $tree = $request->getAttribute('tree'); 90ac5f8ed1SGreg Roach $search = $request->getQueryParams()['search']; 918c2e8227SGreg Roach 921c0676b2SGreg Roach $html = ''; 931c0676b2SGreg Roach 941c0676b2SGreg Roach if (strlen($search) >= 2) { 9557ab2231SGreg Roach $html = $this->search_service 96a7a24840SGreg Roach ->searchIndividualNames([$tree], [$search]) 9732cd2800SGreg Roach ->map(function (Individual $individual): string { 9832cd2800SGreg Roach return $this->getPersonLi($individual); 9932cd2800SGreg Roach }) 10032cd2800SGreg Roach ->implode(''); 1018c2e8227SGreg Roach } 1028c2e8227SGreg Roach 1031c0676b2SGreg Roach if ($html !== '') { 1041c0676b2SGreg Roach $html = '<ul>' . $html . '</ul>'; 1051c0676b2SGreg Roach } 1061c0676b2SGreg Roach 1076ccdf4f0SGreg Roach return response($html); 1081c0676b2SGreg Roach } 1091c0676b2SGreg Roach 1101c0676b2SGreg Roach /** 1116ccdf4f0SGreg Roach * @param ServerRequestInterface $request 1121c0676b2SGreg Roach * 1136ccdf4f0SGreg Roach * @return ResponseInterface 1141c0676b2SGreg Roach */ 11557ab2231SGreg Roach public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface 116c1010edaSGreg Roach { 11757ab2231SGreg Roach $tree = $request->getAttribute('tree'); 118ac5f8ed1SGreg Roach $xref = $request->getQueryParams()['xref']; 1191c0676b2SGreg Roach 1201c0676b2SGreg Roach $individual = Individual::getInstance($xref, $tree); 1211c0676b2SGreg Roach 1221c0676b2SGreg Roach if ($individual !== null && $individual->canShow()) { 1231c0676b2SGreg Roach $html = $this->loadSpouses($individual, 1); 1241c0676b2SGreg Roach } else { 1251c0676b2SGreg Roach $html = ''; 1261c0676b2SGreg Roach } 1271c0676b2SGreg Roach 1286ccdf4f0SGreg Roach return response($html); 1291c0676b2SGreg Roach } 1301c0676b2SGreg Roach 1318c2e8227SGreg Roach /** {@inheritdoc} */ 1328f53f488SRico Sonntag public function hasSidebarContent(Individual $individual): bool 133c1010edaSGreg Roach { 13438a9583bSGreg Roach return true; 1358c2e8227SGreg Roach } 1368c2e8227SGreg Roach 13776692c8bSGreg Roach /** 13876692c8bSGreg Roach * Load this sidebar synchronously. 13976692c8bSGreg Roach * 140225e381fSGreg Roach * @param Individual $individual 141225e381fSGreg Roach * 14276692c8bSGreg Roach * @return string 14376692c8bSGreg Roach */ 1448f53f488SRico Sonntag public function getSidebarContent(Individual $individual): string 145c1010edaSGreg Roach { 1461ef93f16SGreg Roach return view('modules/descendancy/sidebar', [ 147a817de92SGreg Roach 'individual_list' => $this->getPersonLi($individual, 1), 148a817de92SGreg Roach ]); 1498c2e8227SGreg Roach } 1508c2e8227SGreg Roach 1518c2e8227SGreg Roach /** 15276692c8bSGreg Roach * Format an individual in a list. 15376692c8bSGreg Roach * 1548c2e8227SGreg Roach * @param Individual $person 155cbc1590aSGreg Roach * @param int $generations 1568c2e8227SGreg Roach * 1578c2e8227SGreg Roach * @return string 1588c2e8227SGreg Roach */ 1598f53f488SRico Sonntag public function getPersonLi(Individual $person, $generations = 0): string 160c1010edaSGreg Roach { 1618c2e8227SGreg Roach $icon = $generations > 0 ? 'icon-minus' : 'icon-plus'; 1628c2e8227SGreg Roach $lifespan = $person->canShow() ? '(' . $person->getLifeSpan() . ')' : ''; 1638c2e8227SGreg Roach $spouses = $generations > 0 ? $this->loadSpouses($person, 0) : ''; 1642622ba92SGreg Roach 1652622ba92SGreg Roach return 1662622ba92SGreg Roach '<li class="sb_desc_indi_li">' . 167c1010edaSGreg Roach '<a class="sb_desc_indi" href="' . e(route('module', [ 16826684e68SGreg Roach 'module' => $this->name(), 169c1010edaSGreg Roach 'action' => 'Descendants', 170*9022ab66SGreg Roach 'tree' => $person->tree()->name(), 171c0935879SGreg Roach 'xref' => $person->xref(), 172c1010edaSGreg Roach ])) . '">' . 1732622ba92SGreg Roach '<i class="plusminus ' . $icon . '"></i>' . 174032918ffSGreg Roach '<small>' . view('icons/sex-' . $person->sex()) . '</small>' . $person->fullName() . $lifespan . 1752622ba92SGreg Roach '</a>' . 17639ca88baSGreg Roach '<a href="' . e($person->url()) . '" title="' . strip_tags($person->fullName()) . '">' . view('icons/individual') . '</a>' . 1772622ba92SGreg Roach '<div>' . $spouses . '</div>' . 1782622ba92SGreg Roach '</li>'; 1798c2e8227SGreg Roach } 1808c2e8227SGreg Roach 1818c2e8227SGreg Roach /** 18276692c8bSGreg Roach * Format a family in a list. 18376692c8bSGreg Roach * 1848c2e8227SGreg Roach * @param Family $family 1858c2e8227SGreg Roach * @param Individual $person 186cbc1590aSGreg Roach * @param int $generations 1878c2e8227SGreg Roach * 1888c2e8227SGreg Roach * @return string 1898c2e8227SGreg Roach */ 1908f53f488SRico Sonntag public function getFamilyLi(Family $family, Individual $person, $generations = 0): string 191c1010edaSGreg Roach { 19239ca88baSGreg Roach $spouse = $family->spouse($person); 1932622ba92SGreg Roach if ($spouse) { 194032918ffSGreg Roach $spouse_name = '<small>' . view('icons/sex-' . $spouse->sex()) . '</small>' . $spouse->fullName(); 19539ca88baSGreg Roach $spouse_link = '<a href="' . e($person->url()) . '" title="' . strip_tags($person->fullName()) . '">' . view('icons/individual') . '</a>'; 1962622ba92SGreg Roach } else { 1972622ba92SGreg Roach $spouse_name = ''; 1982622ba92SGreg Roach $spouse_link = ''; 1992622ba92SGreg Roach } 2002622ba92SGreg Roach 20139ca88baSGreg Roach $family_link = '<a href="' . e($family->url()) . '" title="' . strip_tags($family->fullName()) . '">' . view('icons/family') . '</a>'; 20239b853a7SGreg Roach 2038c2e8227SGreg Roach $marryear = $family->getMarriageYear(); 2048c2e8227SGreg Roach $marr = $marryear ? '<i class="icon-rings"></i>' . $marryear : ''; 2052622ba92SGreg Roach 2062622ba92SGreg Roach return 2072622ba92SGreg Roach '<li class="sb_desc_indi_li">' . 20839b853a7SGreg Roach '<a class="sb_desc_indi" href="#"><i class="plusminus icon-minus"></i>' . 20939b853a7SGreg Roach $spouse_name . 21039b853a7SGreg Roach $marr . 21139b853a7SGreg Roach '</a>' . 2122622ba92SGreg Roach $spouse_link . 21339b853a7SGreg Roach $family_link . 2142622ba92SGreg Roach '<div>' . $this->loadChildren($family, $generations) . '</div>' . 2152622ba92SGreg Roach '</li>'; 2168c2e8227SGreg Roach } 2178c2e8227SGreg Roach 2188c2e8227SGreg Roach /** 21976692c8bSGreg Roach * Display spouses. 22076692c8bSGreg Roach * 22149a243cbSGreg Roach * @param Individual $individual 222cbc1590aSGreg Roach * @param int $generations 2238c2e8227SGreg Roach * 2248c2e8227SGreg Roach * @return string 2258c2e8227SGreg Roach */ 226e364afe4SGreg Roach public function loadSpouses(Individual $individual, $generations): string 227c1010edaSGreg Roach { 2288c2e8227SGreg Roach $out = ''; 22949a243cbSGreg Roach if ($individual->canShow()) { 23039ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 23149a243cbSGreg Roach $out .= $this->getFamilyLi($family, $individual, $generations - 1); 2328c2e8227SGreg Roach } 2338c2e8227SGreg Roach } 2348c2e8227SGreg Roach if ($out) { 2358c2e8227SGreg Roach return '<ul>' . $out . '</ul>'; 2368c2e8227SGreg Roach } 237b2ce94c6SRico Sonntag 238b2ce94c6SRico Sonntag return ''; 2398c2e8227SGreg Roach } 2408c2e8227SGreg Roach 2418c2e8227SGreg Roach /** 24276692c8bSGreg Roach * Display descendants. 24376692c8bSGreg Roach * 2448c2e8227SGreg Roach * @param Family $family 245cbc1590aSGreg Roach * @param int $generations 2468c2e8227SGreg Roach * 2478c2e8227SGreg Roach * @return string 2488c2e8227SGreg Roach */ 249e364afe4SGreg Roach public function loadChildren(Family $family, $generations): string 250c1010edaSGreg Roach { 2518c2e8227SGreg Roach $out = ''; 2528c2e8227SGreg Roach if ($family->canShow()) { 25339ca88baSGreg Roach $children = $family->children(); 254820b62dfSGreg Roach 255820b62dfSGreg Roach if ($children->isNotEmpty()) { 2568c2e8227SGreg Roach foreach ($children as $child) { 2578c2e8227SGreg Roach $out .= $this->getPersonLi($child, $generations - 1); 2588c2e8227SGreg Roach } 2598c2e8227SGreg Roach } else { 2608c2e8227SGreg Roach $out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>'; 2618c2e8227SGreg Roach } 2628c2e8227SGreg Roach } 2638c2e8227SGreg Roach if ($out) { 2648c2e8227SGreg Roach return '<ul>' . $out . '</ul>'; 2658c2e8227SGreg Roach } 266b2ce94c6SRico Sonntag 267b2ce94c6SRico Sonntag return ''; 2688c2e8227SGreg Roach } 2698c2e8227SGreg Roach} 270