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