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 */ 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; 2532cd2800SGreg Roachuse Fisharebest\Webtrees\Services\SearchService; 265229eadeSGreg Roachuse Fisharebest\Webtrees\Tree; 276ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 293976b470SGreg Roach 305229eadeSGreg Roachuse function assert; 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 4057ab2231SGreg Roach /** @var SearchService */ 4157ab2231SGreg Roach private $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 { 9257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 9375964c75SGreg Roach assert($tree instanceof Tree); 945229eadeSGreg Roach 95ac5f8ed1SGreg Roach $search = $request->getQueryParams()['search']; 968c2e8227SGreg Roach 971c0676b2SGreg Roach $html = ''; 981c0676b2SGreg Roach 991c0676b2SGreg Roach if (strlen($search) >= 2) { 10057ab2231SGreg Roach $html = $this->search_service 101a7a24840SGreg Roach ->searchIndividualNames([$tree], [$search]) 10232cd2800SGreg Roach ->map(function (Individual $individual): string { 10332cd2800SGreg Roach return $this->getPersonLi($individual); 10432cd2800SGreg Roach }) 10532cd2800SGreg Roach ->implode(''); 1068c2e8227SGreg Roach } 1078c2e8227SGreg Roach 1081c0676b2SGreg Roach if ($html !== '') { 1091c0676b2SGreg Roach $html = '<ul>' . $html . '</ul>'; 1101c0676b2SGreg Roach } 1111c0676b2SGreg Roach 1126ccdf4f0SGreg Roach return response($html); 1131c0676b2SGreg Roach } 1141c0676b2SGreg Roach 1151c0676b2SGreg Roach /** 1166ccdf4f0SGreg Roach * @param ServerRequestInterface $request 1171c0676b2SGreg Roach * 1186ccdf4f0SGreg Roach * @return ResponseInterface 1191c0676b2SGreg Roach */ 12057ab2231SGreg Roach public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface 121c1010edaSGreg Roach { 12257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 12375964c75SGreg Roach assert($tree instanceof Tree); 1245229eadeSGreg Roach 125ac5f8ed1SGreg Roach $xref = $request->getQueryParams()['xref']; 1261c0676b2SGreg Roach 1271c0676b2SGreg Roach $individual = Individual::getInstance($xref, $tree); 1281c0676b2SGreg Roach 1291c0676b2SGreg Roach if ($individual !== null && $individual->canShow()) { 1301c0676b2SGreg Roach $html = $this->loadSpouses($individual, 1); 1311c0676b2SGreg Roach } else { 1321c0676b2SGreg Roach $html = ''; 1331c0676b2SGreg Roach } 1341c0676b2SGreg Roach 1356ccdf4f0SGreg Roach return response($html); 1361c0676b2SGreg Roach } 1371c0676b2SGreg Roach 1388c2e8227SGreg Roach /** {@inheritdoc} */ 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), 155*ef5d23f1SGreg 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 */ 1678f53f488SRico Sonntag public function getPersonLi(Individual $person, $generations = 0): string 168c1010edaSGreg Roach { 1698c2e8227SGreg Roach $icon = $generations > 0 ? 'icon-minus' : 'icon-plus'; 1708c2e8227SGreg Roach $lifespan = $person->canShow() ? '(' . $person->getLifeSpan() . ')' : ''; 1718c2e8227SGreg Roach $spouses = $generations > 0 ? $this->loadSpouses($person, 0) : ''; 1722622ba92SGreg Roach 1732622ba92SGreg Roach return 1742622ba92SGreg Roach '<li class="sb_desc_indi_li">' . 175c1010edaSGreg Roach '<a class="sb_desc_indi" 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>' . 182032918ffSGreg Roach '<small>' . view('icons/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 */ 1988f53f488SRico Sonntag public function getFamilyLi(Family $family, Individual $person, $generations = 0): string 199c1010edaSGreg Roach { 20039ca88baSGreg Roach $spouse = $family->spouse($person); 2012622ba92SGreg Roach if ($spouse) { 202032918ffSGreg Roach $spouse_name = '<small>' . view('icons/sex-' . $spouse->sex()) . '</small>' . $spouse->fullName(); 20339ca88baSGreg Roach $spouse_link = '<a href="' . e($person->url()) . '" title="' . strip_tags($person->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">' . 21639b853a7SGreg Roach '<a class="sb_desc_indi" 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 */ 234e364afe4SGreg Roach public function loadSpouses(Individual $individual, $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 */ 257e364afe4SGreg Roach public function loadChildren(Family $family, $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