1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 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\InteractiveTree; 19 20use Fisharebest\Webtrees\Family; 21use Fisharebest\Webtrees\I18N; 22use Fisharebest\Webtrees\Individual; 23use Fisharebest\Webtrees\Tree; 24 25/** 26 * Class TreeView 27 */ 28class TreeView 29{ 30 /** @var string HTML element name */ 31 private $name; 32 33 /** 34 * Treeview Constructor 35 * 36 * @param string $name the name of the TreeView object’s instance 37 */ 38 public function __construct($name = 'tree') 39 { 40 $this->name = $name; 41 } 42 43 /** 44 * Draw the viewport which creates the draggable/zoomable framework 45 * Size is set by the container, as the viewport can scale itself automatically 46 * 47 * @param Individual $individual Draw the chart for this individual 48 * @param int $generations number of generations to draw 49 * 50 * @return string[] HTML and Javascript 51 */ 52 public function drawViewport(Individual $individual, $generations): array 53 { 54 $html = view('interactive-tree-chart', [ 55 'name' => $this->name, 56 'individual' => $this->drawPerson($individual, $generations, 0, null, null, true), 57 ]); 58 59 return [ 60 $html, 61 'var ' . $this->name . 'Handler = new TreeViewHandler("' . $this->name . '", "' . e($individual->getTree()->getName()) . '");', 62 ]; 63 } 64 65 /** 66 * Return a JSON structure to a JSON request 67 * 68 * @param Tree $tree 69 * @param string $list list of JSON requests 70 * 71 * @return string 72 */ 73 public function getPersons(Tree $tree, $list): string 74 { 75 $list = explode(';', $list); 76 $r = []; 77 foreach ($list as $jsonRequest) { 78 $firstLetter = substr($jsonRequest, 0, 1); 79 $jsonRequest = substr($jsonRequest, 1); 80 switch ($firstLetter) { 81 case 'c': 82 $fidlist = explode(',', $jsonRequest); 83 $flist = []; 84 foreach ($fidlist as $fid) { 85 $flist[] = Family::getInstance($fid, $tree); 86 } 87 $r[] = $this->drawChildren($flist, 1, true); 88 break; 89 case 'p': 90 $params = explode('@', $jsonRequest); 91 $fid = $params[0]; 92 $order = $params[1]; 93 $f = Family::getInstance($fid, $tree); 94 if ($f->getHusband()) { 95 $r[] = $this->drawPerson($f->getHusband(), 0, 1, $f, $order); 96 } elseif ($f->getWife()) { 97 $r[] = $this->drawPerson($f->getWife(), 0, 1, $f, $order); 98 } 99 break; 100 } 101 } 102 103 return json_encode($r); 104 } 105 106 /** 107 * Get the details for a person and their life partner(s) 108 * 109 * @param Individual $individual the individual to return the details for 110 * 111 * @return string 112 */ 113 public function getDetails(Individual $individual): string 114 { 115 $html = $this->getPersonDetails($individual, null); 116 foreach ($individual->getSpouseFamilies() as $family) { 117 $spouse = $family->getSpouse($individual); 118 if ($spouse) { 119 $html .= $this->getPersonDetails($spouse, $family); 120 } 121 } 122 123 return $html; 124 } 125 126 /** 127 * Return the details for a person 128 * 129 * @param Individual $individual 130 * @param Family $family 131 * 132 * @return string 133 */ 134 private function getPersonDetails(Individual $individual, Family $family = null): string 135 { 136 $chart_url = route('module', [ 137 'module' => 'tree', 138 'action' => 'Treeview', 139 'xref' => $individual->getXref(), 140 'ged' => $individual->getTree()->getName(), 141 ]); 142 143 $hmtl = $this->getThumbnail($individual); 144 $hmtl .= '<a class="tv_link" href="' . e($individual->url()) . '">' . $individual->getFullName() . '</a> <a href="' . e($chart_url) . '" title="' . I18N::translate('Interactive tree of %s', strip_tags($individual->getFullName())) . '" class="wt-icon-individual tv_link tv_treelink"></a>'; 145 foreach ($individual->getFacts(WT_EVENTS_BIRT, true) as $fact) { 146 $hmtl .= $fact->summary(); 147 } 148 if ($family) { 149 foreach ($family->getFacts(WT_EVENTS_MARR, true) as $fact) { 150 $hmtl .= $fact->summary(); 151 } 152 } 153 foreach ($individual->getFacts(WT_EVENTS_DEAT, true) as $fact) { 154 $hmtl .= $fact->summary(); 155 } 156 157 return '<div class="tv' . $individual->getSex() . ' tv_person_expanded">' . $hmtl . '</div>'; 158 } 159 160 /** 161 * Draw the children for some families 162 * 163 * @param Family[] $familyList array of families to draw the children for 164 * @param int $gen number of generations to draw 165 * @param bool $ajax setted to true for an ajax call 166 * 167 * @return string 168 */ 169 private function drawChildren(array $familyList, $gen = 1, $ajax = false): string 170 { 171 $html = ''; 172 $children2draw = []; 173 $f2load = []; 174 175 foreach ($familyList as $f) { 176 if (empty($f)) { 177 continue; 178 } 179 $children = $f->getChildren(); 180 if ($children) { 181 $f2load[] = $f->getXref(); 182 foreach ($children as $child) { 183 // Eliminate duplicates - e.g. when adopted by a step-parent 184 $children2draw[$child->getXref()] = $child; 185 } 186 } 187 } 188 $tc = count($children2draw); 189 if ($tc) { 190 $f2load = implode(',', $f2load); 191 $nbc = 0; 192 foreach ($children2draw as $child) { 193 $nbc++; 194 if ($tc == 1) { 195 $co = 'c'; // unique 196 } elseif ($nbc == 1) { 197 $co = 't'; // first 198 } elseif ($nbc == $tc) { 199 $co = 'b'; //last 200 } else { 201 $co = 'h'; 202 } 203 $html .= $this->drawPerson($child, $gen - 1, -1, null, $co); 204 } 205 if (!$ajax) { 206 $html = '<td align="right"' . ($gen == 0 ? ' abbr="c' . $f2load . '"' : '') . '>' . $html . '</td>' . $this->drawHorizontalLine(); 207 } 208 } 209 210 return $html; 211 } 212 213 /** 214 * Draw a person in the tree 215 * 216 * @param Individual $person The Person object to draw the box for 217 * @param int $gen The number of generations up or down to print 218 * @param int $state Whether we are going up or down the tree, -1 for descendents +1 for ancestors 219 * @param Family $pfamily 220 * @param string $order first (1), last(2), unique(0), or empty. Required for drawing lines between boxes 221 * @param bool $isRoot 222 * 223 * @return string 224 * 225 * Notes : "spouse" means explicitely married partners. Thus, the word "partner" 226 * (for "life partner") here fits much better than "spouse" or "mate" 227 * to translate properly the modern french meaning of "conjoint" 228 */ 229 private function drawPerson(Individual $person, $gen, $state, Family $pfamily = null, $order = null, $isRoot = false): string 230 { 231 if ($gen < 0) { 232 return ''; 233 } 234 if (!empty($pfamily)) { 235 $partner = $pfamily->getSpouse($person); 236 } else { 237 $partner = $person->getCurrentSpouse(); 238 } 239 if ($isRoot) { 240 $html = '<table id="tvTreeBorder" class="tv_tree"><tbody><tr><td id="tv_tree_topleft"></td><td id="tv_tree_top"></td><td id="tv_tree_topright"></td></tr><tr><td id="tv_tree_left"></td><td>'; 241 } else { 242 $html = ''; 243 } 244 /* height 1% : this hack enable the div auto-dimensioning in td for FF & Chrome */ 245 $html .= '<table class="tv_tree"' . ($isRoot ? ' id="tv_tree"' : '') . ' style="height: 1%"><tbody><tr>'; 246 247 if ($state <= 0) { 248 // draw children 249 $html .= $this->drawChildren($person->getSpouseFamilies(), $gen); 250 } else { 251 // draw the parent’s lines 252 $html .= $this->drawVerticalLine($order) . $this->drawHorizontalLine(); 253 } 254 255 /* draw the person. Do NOT add person or family id as an id, since a same person could appear more than once in the tree !!! */ 256 // Fixing the width for td to the box initial width when the person is the root person fix a rare bug that happen when a person without child and without known parents is the root person : an unwanted white rectangle appear at the right of the person’s boxes, otherwise. 257 $html .= '<td' . ($isRoot ? ' style="width:1px"' : '') . '><div class="tv_box' . ($isRoot ? ' rootPerson' : '') . '" dir="' . I18N::direction() . '" style="text-align: ' . (I18N::direction() === 'rtl' ? 'right' : 'left') . '; direction: ' . I18N::direction() . '" abbr="' . $person->getXref() . '" onclick="' . $this->name . 'Handler.expandBox(this, event);">'; 258 $html .= $this->drawPersonName($person); 259 $fop = []; // $fop is fathers of partners 260 if ($partner !== null) { 261 $dashed = ''; 262 foreach ($person->getSpouseFamilies() as $family) { 263 $spouse = $family->getSpouse($person); 264 if ($spouse) { 265 $spouse_parents = $spouse->getPrimaryChildFamily(); 266 if ($spouse_parents && $spouse_parents->getHusband()) { 267 $fop[] = [ 268 $spouse_parents->getHusband(), 269 $spouse_parents, 270 ]; 271 } elseif ($spouse_parents && $spouse_parents->getWife()) { 272 $fop[] = [ 273 $spouse_parents->getWife(), 274 $spouse_parents, 275 ]; 276 } 277 $html .= $this->drawPersonName($spouse, $dashed); 278 $dashed = 'dashed'; 279 } 280 } 281 } 282 $html .= '</div></td>'; 283 284 $primaryChildFamily = $person->getPrimaryChildFamily(); 285 if (!empty($primaryChildFamily)) { 286 $parent = $primaryChildFamily->getHusband(); 287 if (empty($parent)) { 288 $parent = $primaryChildFamily->getWife(); 289 } 290 } 291 if (!empty($parent) || count($fop) || ($state < 0)) { 292 $html .= $this->drawHorizontalLine(); 293 } 294 /* draw the parents */ 295 if ($state >= 0 && (!empty($parent) || count($fop))) { 296 $unique = (empty($parent) || count($fop) == 0); 297 $html .= '<td align="left"><table class="tv_tree"><tbody>'; 298 if (!empty($parent)) { 299 $u = $unique ? 'c' : 't'; 300 $html .= '<tr><td ' . ($gen == 0 ? ' abbr="p' . $primaryChildFamily->getXref() . '@' . $u . '"' : '') . '>'; 301 $html .= $this->drawPerson($parent, $gen - 1, 1, $primaryChildFamily, $u); 302 $html .= '</td></tr>'; 303 } 304 if (count($fop)) { 305 $n = 0; 306 $nb = count($fop); 307 foreach ($fop as $p) { 308 $n++; 309 $u = $unique ? 'c' : ($n == $nb || empty($p[1]) ? 'b' : 'h'); 310 $html .= '<tr><td ' . ($gen == 0 ? ' abbr="p' . $p[1]->getXref() . '@' . $u . '"' : '') . '>' . $this->drawPerson($p[0], $gen - 1, 1, $p[1], $u) . '</td></tr>'; 311 } 312 } 313 $html .= '</tbody></table></td>'; 314 } 315 if ($state < 0) { 316 $html .= $this->drawVerticalLine($order); 317 } 318 $html .= '</tr></tbody></table>'; 319 if ($isRoot) { 320 $html .= '</td><td id="tv_tree_right"></td></tr><tr><td id="tv_tree_bottomleft"></td><td id="tv_tree_bottom"></td><td id="tv_tree_bottomright"></td></tr></tbody></table>'; 321 } 322 323 return $html; 324 } 325 326 /** 327 * Draw a person name preceded by sex icon, with parents as tooltip 328 * 329 * @param Individual $individual an individual 330 * @param string $dashed if = 'dashed' print dashed top border to separate multiple spuses 331 * 332 * @return string 333 */ 334 private function drawPersonName(Individual $individual, $dashed = ''): string 335 { 336 $family = $individual->getPrimaryChildFamily(); 337 if ($family) { 338 $family_name = strip_tags($family->getFullName()); 339 } else { 340 $family_name = I18N::translateContext('unknown family', 'unknown'); 341 } 342 switch ($individual->getSex()) { 343 case 'M': 344 /* I18N: e.g. “Son of [father name & mother name]” */ 345 $title = ' title="' . I18N::translate('Son of %s', $family_name) . '"'; 346 break; 347 case 'F': 348 /* I18N: e.g. “Daughter of [father name & mother name]” */ 349 $title = ' title="' . I18N::translate('Daughter of %s', $family_name) . '"'; 350 break; 351 default: 352 /* I18N: e.g. “Child of [father name & mother name]” */ 353 $title = ' title="' . I18N::translate('Child of %s', $family_name) . '"'; 354 break; 355 } 356 $sex = $individual->getSex(); 357 358 return '<div class="tv' . $sex . ' ' . $dashed . '"' . $title . '><a href="' . e($individual->url()) . '"></a>' . $individual->getFullName() . ' <span class="dates">' . $individual->getLifeSpan() . '</span></div>'; 359 } 360 361 /** 362 * Get the thumbnail image for the given person 363 * 364 * @param Individual $individual 365 * 366 * @return string 367 */ 368 private function getThumbnail(Individual $individual) 369 { 370 if ($individual->getTree()->getPreference('SHOW_HIGHLIGHT_IMAGES')) { 371 return $individual->displayImage(40, 50, 'crop', []); 372 } 373 374 return ''; 375 } 376 377 /** 378 * Draw a vertical line 379 * 380 * @param string $order A parameter that set how to draw this line with auto-redimensionning capabilities 381 * 382 * @return string 383 * WARNING : some tricky hacks are required in CSS to ensure cross-browser compliance 384 * some browsers shows an image, which imply a size limit in height, 385 * and some other browsers (ex: firefox) shows a <div> tag, which have no size limit in height 386 * Therefore, Firefox is a good choice to print very big trees. 387 */ 388 private function drawVerticalLine($order): string 389 { 390 return '<td class="tv_vline tv_vline_' . $order . '"><div class="tv_vline tv_vline_' . $order . '"></div></td>'; 391 } 392 393 /** 394 * Draw an horizontal line 395 */ 396 private function drawHorizontalLine(): string 397 { 398 return '<td class="tv_hline"><div class="tv_hline"></div></td>'; 399 } 400} 401