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