1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 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 <http://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\Services\SearchService; 26use Fisharebest\Webtrees\Tree; 27use Psr\Http\Message\ResponseInterface; 28use Psr\Http\Message\ServerRequestInterface; 29 30use function assert; 31use function view; 32 33/** 34 * Class DescendancyModule 35 */ 36class DescendancyModule extends AbstractModule implements ModuleSidebarInterface 37{ 38 use ModuleSidebarTrait; 39 40 /** @var SearchService */ 41 private $search_service; 42 43 /** 44 * DescendancyModule constructor. 45 * 46 * @param SearchService $search_service 47 */ 48 public function __construct(SearchService $search_service) 49 { 50 $this->search_service = $search_service; 51 } 52 53 /** 54 * How should this module be identified in the control panel, etc.? 55 * 56 * @return string 57 */ 58 public function title(): string 59 { 60 /* I18N: Name of a module/sidebar */ 61 return I18N::translate('Descendants'); 62 } 63 64 /** 65 * A sentence describing what this module does. 66 * 67 * @return string 68 */ 69 public function description(): string 70 { 71 /* I18N: Description of the “Descendants” module */ 72 return I18N::translate('A sidebar showing the descendants of an individual.'); 73 } 74 75 /** 76 * The default position for this sidebar. It can be changed in the control panel. 77 * 78 * @return int 79 */ 80 public function defaultSidebarOrder(): int 81 { 82 return 3; 83 } 84 85 /** 86 * @param ServerRequestInterface $request 87 * 88 * @return ResponseInterface 89 */ 90 public function getSearchAction(ServerRequestInterface $request): ResponseInterface 91 { 92 $tree = $request->getAttribute('tree'); 93 assert($tree instanceof Tree); 94 95 $search = $request->getQueryParams()['search']; 96 97 $html = ''; 98 99 if (strlen($search) >= 2) { 100 $html = $this->search_service 101 ->searchIndividualNames([$tree], [$search]) 102 ->map(function (Individual $individual): string { 103 return $this->getPersonLi($individual); 104 }) 105 ->implode(''); 106 } 107 108 if ($html !== '') { 109 $html = '<ul>' . $html . '</ul>'; 110 } 111 112 return response($html); 113 } 114 115 /** 116 * @param ServerRequestInterface $request 117 * 118 * @return ResponseInterface 119 */ 120 public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface 121 { 122 $tree = $request->getAttribute('tree'); 123 assert($tree instanceof Tree); 124 125 $xref = $request->getQueryParams()['xref'] ?? ''; 126 127 $individual = Individual::getInstance($xref, $tree); 128 129 if ($individual !== null && $individual->canShow()) { 130 $html = $this->loadSpouses($individual, 1); 131 } else { 132 $html = ''; 133 } 134 135 return response($html); 136 } 137 138 /** {@inheritdoc} */ 139 public function hasSidebarContent(Individual $individual): bool 140 { 141 return true; 142 } 143 144 /** 145 * Load this sidebar synchronously. 146 * 147 * @param Individual $individual 148 * 149 * @return string 150 */ 151 public function getSidebarContent(Individual $individual): string 152 { 153 return view('modules/descendancy/sidebar', [ 154 'individual_list' => $this->getPersonLi($individual, 1), 155 'tree' => $individual->tree(), 156 ]); 157 } 158 159 /** 160 * Format an individual in a list. 161 * 162 * @param Individual $person 163 * @param int $generations 164 * 165 * @return string 166 */ 167 public function getPersonLi(Individual $person, $generations = 0): string 168 { 169 $icon = $generations > 0 ? 'icon-minus' : 'icon-plus'; 170 $lifespan = $person->canShow() ? '(' . $person->lifespan() . ')' : ''; 171 $spouses = $generations > 0 ? $this->loadSpouses($person, 0) : ''; 172 173 return 174 '<li class="sb_desc_indi_li">' . 175 '<a class="sb_desc_indi" href="' . e(route('module', [ 176 'module' => $this->name(), 177 'action' => 'Descendants', 178 'tree' => $person->tree()->name(), 179 'xref' => $person->xref(), 180 ])) . '">' . 181 '<i class="plusminus ' . $icon . '"></i>' . 182 '<small>' . view('icons/sex', ['sex' => $person->sex()]) . '</small>' . $person->fullName() . $lifespan . 183 '</a>' . 184 '<a href="' . e($person->url()) . '" title="' . strip_tags($person->fullName()) . '">' . view('icons/individual') . '</a>' . 185 '<div>' . $spouses . '</div>' . 186 '</li>'; 187 } 188 189 /** 190 * Format a family in a list. 191 * 192 * @param Family $family 193 * @param Individual $person 194 * @param int $generations 195 * 196 * @return string 197 */ 198 public function getFamilyLi(Family $family, Individual $person, $generations = 0): string 199 { 200 $spouse = $family->spouse($person); 201 if ($spouse instanceof Individual) { 202 $spouse_name = '<small>' . view('icons/sex', ['sex' => $spouse->sex()]) . '</small>' . $spouse->fullName(); 203 $spouse_link = '<a href="' . e($spouse->url()) . '" title="' . strip_tags($spouse->fullName()) . '">' . view('icons/individual') . '</a>'; 204 } else { 205 $spouse_name = ''; 206 $spouse_link = ''; 207 } 208 209 $family_link = '<a href="' . e($family->url()) . '" title="' . strip_tags($family->fullName()) . '">' . view('icons/family') . '</a>'; 210 211 $marryear = $family->getMarriageYear(); 212 $marr = $marryear ? '<i class="icon-rings"></i>' . $marryear : ''; 213 214 return 215 '<li class="sb_desc_indi_li">' . 216 '<a class="sb_desc_indi" href="#"><i class="plusminus icon-minus"></i>' . 217 $spouse_name . 218 $marr . 219 '</a>' . 220 $spouse_link . 221 $family_link . 222 '<div>' . $this->loadChildren($family, $generations) . '</div>' . 223 '</li>'; 224 } 225 226 /** 227 * Display spouses. 228 * 229 * @param Individual $individual 230 * @param int $generations 231 * 232 * @return string 233 */ 234 public function loadSpouses(Individual $individual, $generations): string 235 { 236 $out = ''; 237 if ($individual->canShow()) { 238 foreach ($individual->spouseFamilies() as $family) { 239 $out .= $this->getFamilyLi($family, $individual, $generations - 1); 240 } 241 } 242 if ($out) { 243 return '<ul>' . $out . '</ul>'; 244 } 245 246 return ''; 247 } 248 249 /** 250 * Display descendants. 251 * 252 * @param Family $family 253 * @param int $generations 254 * 255 * @return string 256 */ 257 public function loadChildren(Family $family, $generations): string 258 { 259 $out = ''; 260 if ($family->canShow()) { 261 $children = $family->children(); 262 263 if ($children->isNotEmpty()) { 264 foreach ($children as $child) { 265 $out .= $this->getPersonLi($child, $generations - 1); 266 } 267 } else { 268 $out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>'; 269 } 270 } 271 if ($out) { 272 return '<ul>' . $out . '</ul>'; 273 } 274 275 return ''; 276 } 277} 278