1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\Family; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Individual; 25use Fisharebest\Webtrees\Registry; 26use Fisharebest\Webtrees\Services\SearchService; 27use Fisharebest\Webtrees\Validator; 28use Psr\Http\Message\ResponseInterface; 29use Psr\Http\Message\ServerRequestInterface; 30 31use function strlen; 32use function view; 33 34/** 35 * Class DescendancyModule 36 */ 37class DescendancyModule extends AbstractModule implements ModuleSidebarInterface 38{ 39 use ModuleSidebarTrait; 40 41 private SearchService $search_service; 42 43 /** 44 * @param SearchService $search_service 45 */ 46 public function __construct(SearchService $search_service) 47 { 48 $this->search_service = $search_service; 49 } 50 51 /** 52 * How should this module be identified in the control panel, etc.? 53 * 54 * @return string 55 */ 56 public function title(): string 57 { 58 /* I18N: Name of a module/sidebar */ 59 return I18N::translate('Descendants'); 60 } 61 62 /** 63 * A sentence describing what this module does. 64 * 65 * @return string 66 */ 67 public function description(): string 68 { 69 /* I18N: Description of the “Descendants” module */ 70 return I18N::translate('A sidebar showing the descendants of an individual.'); 71 } 72 73 /** 74 * The default position for this sidebar. It can be changed in the control panel. 75 * 76 * @return int 77 */ 78 public function defaultSidebarOrder(): int 79 { 80 return 3; 81 } 82 83 /** 84 * @param ServerRequestInterface $request 85 * 86 * @return ResponseInterface 87 */ 88 public function getSearchAction(ServerRequestInterface $request): ResponseInterface 89 { 90 $tree = Validator::attributes($request)->tree(); 91 $search = Validator::queryParams($request)->string('search'); 92 93 $html = ''; 94 95 if (strlen($search) >= 2) { 96 $html = $this->search_service 97 ->searchIndividualNames([$tree], [$search]) 98 ->map(fn(Individual $individual): string => $this->getPersonLi($individual)) 99 ->implode(''); 100 } 101 102 if ($html !== '') { 103 $html = '<ul>' . $html . '</ul>'; 104 } 105 106 return response($html); 107 } 108 109 /** 110 * @param ServerRequestInterface $request 111 * 112 * @return ResponseInterface 113 */ 114 public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface 115 { 116 $tree = Validator::attributes($request)->tree(); 117 $xref = Validator::queryParams($request)->isXref()->string('xref'); 118 119 $individual = Registry::individualFactory()->make($xref, $tree); 120 121 if ($individual !== null && $individual->canShow()) { 122 $html = $this->loadSpouses($individual, 1); 123 } else { 124 $html = ''; 125 } 126 127 return response($html); 128 } 129 130 /** 131 * @param Individual $individual 132 * 133 * @return bool 134 */ 135 public function hasSidebarContent(Individual $individual): bool 136 { 137 return true; 138 } 139 140 /** 141 * Load this sidebar synchronously. 142 * 143 * @param Individual $individual 144 * 145 * @return string 146 */ 147 public function getSidebarContent(Individual $individual): string 148 { 149 return view('modules/descendancy/sidebar', [ 150 'individual_list' => $this->getPersonLi($individual, 1), 151 'tree' => $individual->tree(), 152 ]); 153 } 154 155 /** 156 * Format an individual in a list. 157 * 158 * @param Individual $person 159 * @param int $generations 160 * 161 * @return string 162 */ 163 public function getPersonLi(Individual $person, int $generations = 0): string 164 { 165 $icon = $generations > 0 ? 'icon-minus' : 'icon-plus'; 166 $lifespan = $person->canShow() ? '(' . $person->lifespan() . ')' : ''; 167 $spouses = $generations > 0 ? $this->loadSpouses($person, 0) : ''; 168 169 return 170 '<li class="sb_desc_indi_li">' . 171 '<a class="sb_desc_indi" href="#" data-wt-href="' . e(route('module', [ 172 'module' => $this->name(), 173 'action' => 'Descendants', 174 'tree' => $person->tree()->name(), 175 'xref' => $person->xref(), 176 ])) . '">' . 177 '<i class="plusminus ' . $icon . '"></i>' . 178 '<small>' . view('icons/sex', ['sex' => $person->sex()]) . '</small>' . $person->fullName() . $lifespan . 179 '</a>' . 180 '<a href="' . e($person->url()) . '" title="' . strip_tags($person->fullName()) . '">' . view('icons/individual') . '</a>' . 181 '<div>' . $spouses . '</div>' . 182 '</li>'; 183 } 184 185 /** 186 * Format a family in a list. 187 * 188 * @param Family $family 189 * @param Individual $person 190 * @param int $generations 191 * 192 * @return string 193 */ 194 public function getFamilyLi(Family $family, Individual $person, int $generations = 0): string 195 { 196 $spouse = $family->spouse($person); 197 if ($spouse instanceof Individual) { 198 $spouse_name = '<small>' . view('icons/sex', ['sex' => $spouse->sex()]) . '</small>' . $spouse->fullName(); 199 $spouse_link = '<a href="' . e($spouse->url()) . '" title="' . strip_tags($spouse->fullName()) . '">' . view('icons/individual') . '</a>'; 200 } else { 201 $spouse_name = ''; 202 $spouse_link = ''; 203 } 204 205 $family_link = '<a href="' . e($family->url()) . '" title="' . strip_tags($family->fullName()) . '">' . view('icons/family') . '</a>'; 206 207 $marryear = $family->getMarriageYear(); 208 $marr = $marryear ? '<i class="icon-rings"></i>' . $marryear : ''; 209 210 return 211 '<li class="sb_desc_indi_li">' . 212 '<a class="sb_desc_indi" href="#" data-wt-href="#"><i class="plusminus icon-minus"></i>' . 213 $spouse_name . 214 $marr . 215 '</a>' . 216 $spouse_link . 217 $family_link . 218 '<div>' . $this->loadChildren($family, $generations) . '</div>' . 219 '</li>'; 220 } 221 222 /** 223 * Display spouses. 224 * 225 * @param Individual $individual 226 * @param int $generations 227 * 228 * @return string 229 */ 230 public function loadSpouses(Individual $individual, int $generations): string 231 { 232 $out = ''; 233 if ($individual->canShow()) { 234 foreach ($individual->spouseFamilies() as $family) { 235 $out .= $this->getFamilyLi($family, $individual, $generations - 1); 236 } 237 } 238 if ($out !== '') { 239 return '<ul>' . $out . '</ul>'; 240 } 241 242 return ''; 243 } 244 245 /** 246 * Display descendants. 247 * 248 * @param Family $family 249 * @param int $generations 250 * 251 * @return string 252 */ 253 public function loadChildren(Family $family, int $generations): string 254 { 255 $out = ''; 256 if ($family->canShow()) { 257 $children = $family->children(); 258 259 if ($children->isNotEmpty()) { 260 foreach ($children as $child) { 261 $out .= $this->getPersonLi($child, $generations - 1); 262 } 263 } else { 264 $out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>'; 265 } 266 } 267 if ($out !== '') { 268 return '<ul>' . $out . '</ul>'; 269 } 270 271 return ''; 272 } 273} 274