1<?php 2namespace Fisharebest\Webtrees\Module\InteractiveTree; 3 4/** 5 * webtrees: online genealogy 6 * Copyright (C) 2015 webtrees development team 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18use Fisharebest\Webtrees\Family; 19use Fisharebest\Webtrees\Filter; 20use Fisharebest\Webtrees\I18N; 21use Fisharebest\Webtrees\Individual; 22 23/** 24 * Class TreeView 25 */ 26class TreeView { 27 private $name; 28 private $all_partners; 29 30 /** 31 * Treeview Constructor 32 * 33 * @param string $name the name of the TreeView object’s instance 34 */ 35 public function __construct($name = 'tree') { 36 $this->name = $name; 37 $this->all_partners = Filter::cookie('allPartners', 'true|false', 'true'); 38 } 39 40 /** 41 * Draw the viewport which creates the draggable/zoomable framework 42 * Size is set by the container, as the viewport can scale itself automatically 43 * 44 * @param Individual $root_person the id of the root person 45 * @param int $generations number of generations to draw 46 * 47 * @return string[] HTML and Javascript 48 */ 49 public function drawViewport(Individual $root_person, $generations) { 50 $html = ' 51 <a name="tv_content"></a> 52 <div id="' . $this->name . '_out" class="tv_out"> 53 <div id="tv_tools" class="noprint"> 54 <ul> 55 <li id="tvbCompact" class="tv_button"> 56 <img src="' . WT_STATIC_URL . WT_MODULES_DIR . 'tree/images/compact.png" alt="' . I18N::translate('Use compact layout') . '" title="' . I18N::translate('Use compact layout') . '"> 57 </li> 58 <li id="tvbAllPartners" class="tv_button' . ($this->all_partners === 'true' ? ' tvPressed' : '') . '"> 59 <a class="icon-sfamily" href="#" title="' . I18N::translate('Show all spouses and ancestors') . '"></a> 60 </li> 61 <li class="tv_button" id="' . $this->name . '_loading"> 62 <i class="icon-loading-small"></i> 63 </li> 64 </ul> 65 </div> 66 <h2 id="tree-title">' . I18N::translate('Interactive tree of %s', $root_person->getFullName()) . '</h2> 67 <div id="' . $this->name . '_in" class="tv_in" dir="ltr"> 68 ' . $this->drawPerson($root_person, $generations, 0, null, null, true) . ' 69 </div> 70 </div> 71 '; 72 73 return array($html, 'var ' . $this->name . 'Handler = new TreeViewHandler("' . $this->name . '");'); 74 } 75 76 /** 77 * Return a JSON structure to a JSON request 78 * 79 * @param string $list list of JSON requests 80 * 81 * @return string 82 */ 83 public function getPersons($list) { 84 global $WT_TREE; 85 86 $list = explode(';', $list); 87 $r = array(); 88 foreach ($list as $jsonRequest) { 89 $firstLetter = substr($jsonRequest, 0, 1); 90 $jsonRequest = substr($jsonRequest, 1); 91 switch ($firstLetter) { 92 case 'c': 93 $fidlist = explode(',', $jsonRequest); 94 $flist = array(); 95 foreach ($fidlist as $fid) { 96 $flist[] = Family::getInstance($fid, $WT_TREE); 97 } 98 $r[] = $this->drawChildren($flist, 1, true); 99 break; 100 case 'p': 101 $params = explode('@', $jsonRequest); 102 $fid = $params[0]; 103 $order = $params[1]; 104 $f = Family::getInstance($fid, $WT_TREE); 105 if ($f->getHusband()) { 106 $r[] = $this->drawPerson($f->getHusband(), 0, 1, $f, $order); 107 } elseif ($f->getWife()) { 108 $r[] = $this->drawPerson($f->getWife(), 0, 1, $f, $order); 109 } 110 break; 111 } 112 } 113 114 return json_encode($r); 115 } 116 117 /** 118 * Get the details for a person and their life partner(s) 119 * 120 * @param Individual $individual the individual to return the details for 121 * 122 * @return string 123 */ 124 public function getDetails(Individual $individual) { 125 $html = $this->getPersonDetails($individual, null); 126 foreach ($individual->getSpouseFamilies() as $family) { 127 $spouse = $family->getSpouse($individual); 128 if ($spouse) { 129 $html .= $this->getPersonDetails($spouse, $family); 130 } 131 } 132 133 return $html; 134 } 135 136 /** 137 * Return the details for a person 138 * 139 * @param Individual $individual 140 * @param Family $family 141 * 142 * @return string 143 */ 144 private function getPersonDetails(Individual $individual, Family $family = null) { 145 $hmtl = $this->getThumbnail($individual); 146 $hmtl .= '<a class="tv_link" href="' . $individual->getHtmlUrl() . '">' . $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>'; 147 foreach ($individual->getFacts(WT_EVENTS_BIRT, true) as $fact) { 148 $hmtl .= $fact->summary(); 149 } 150 if ($family) { 151 foreach ($family->getFacts(WT_EVENTS_MARR, true) as $fact) { 152 $hmtl .= $fact->summary(); 153 } 154 } 155 foreach ($individual->getFacts(WT_EVENTS_DEAT, true) as $fact) { 156 $hmtl .= $fact->summary(); 157 } 158 159 return '<div class="tv' . $individual->getSex() . ' tv_person_expanded">' . $hmtl . '</div>'; 160 } 161 162 /** 163 * Draw the children for some families 164 * 165 * @param Family[] $familyList array of families to draw the children for 166 * @param int $gen number of generations to draw 167 * @param bool $ajax setted to true for an ajax call 168 * 169 * @return string 170 */ 171 private function drawChildren(array $familyList, $gen = 1, $ajax = false) { 172 $html = ''; 173 $children2draw = array(); 174 $f2load = array(); 175 176 foreach ($familyList as $f) { 177 if (empty($f)) { 178 continue; 179 } 180 $children = $f->getChildren(); 181 if ($children) { 182 $f2load[] = $f->getXref(); 183 foreach ($children as $child) { 184 // Eliminate duplicates - e.g. when adopted by a step-parent 185 $children2draw[$child->getXref()] = $child; 186 } 187 } 188 } 189 $tc = count($children2draw); 190 if ($tc) { 191 $f2load = implode(',', $f2load); 192 $nbc = 0; 193 foreach ($children2draw as $child) { 194 $nbc++; 195 if ($tc == 1) { 196 $co = 'c'; // unique 197 } elseif ($nbc == 1) { 198 $co = 't'; // first 199 } elseif ($nbc == $tc) { 200 $co = 'b'; //last 201 } else { 202 $co = 'h'; 203 } 204 $html .= $this->drawPerson($child, $gen - 1, -1, null, $co); 205 } 206 if (!$ajax) { 207 $html = '<td align="right"' . ($gen == 0 ? ' abbr="c' . $f2load . '"' : '') . '>' . $html . '</td>' . $this->drawHorizontalLine(); 208 } 209 } 210 211 return $html; 212 } 213 214 /** 215 * Draw a person in the tree 216 * 217 * @param Individual $person The Person object to draw the box for 218 * @param int $gen The number of generations up or down to print 219 * @param int $state Whether we are going up or down the tree, -1 for descendents +1 for ancestors 220 * @param Family $pfamily 221 * @param string $order first (1), last(2), unique(0), or empty. Required for drawing lines between boxes 222 * @param bool $isRoot 223 * 224 * @return string 225 * 226 * Notes : "spouse" means explicitely married partners. Thus, the word "partner" 227 * (for "life partner") here fits much better than "spouse" or "mate" 228 * to translate properly the modern french meaning of "conjoint" 229 */ 230 private function drawPerson(Individual $person, $gen, $state, Family $pfamily = null, $order = null, $isRoot = false) { 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 = array(); // $fop is fathers of partners 260 if (!is_null($partner)) { 261 $dashed = ''; 262 foreach ($person->getSpouseFamilies() as $family) { 263 $spouse = $family->getSpouse($person); 264 if ($spouse) { 265 if ($spouse === $partner || $this->all_partners === 'true') { 266 $spouse_parents = $spouse->getPrimaryChildFamily(); 267 if ($spouse_parents && $spouse_parents->getHusband()) { 268 $fop[] = array($spouse_parents->getHusband(), $spouse_parents); 269 } elseif ($spouse_parents && $spouse_parents->getWife()) { 270 $fop[] = array($spouse_parents->getWife(), $spouse_parents); 271 } 272 $html .= $this->drawPersonName($spouse, $dashed); 273 if ($this->all_partners !== 'true') { 274 break; // we can stop here the foreach loop 275 } 276 $dashed = 'dashed'; 277 } 278 } 279 } 280 } 281 $html .= '</div></td>'; 282 283 $primaryChildFamily = $person->getPrimaryChildFamily(); 284 if (!empty($primaryChildFamily)) { 285 $parent = $primaryChildFamily->getHusband(); 286 if (empty($parent)) { 287 $parent = $primaryChildFamily->getWife(); 288 } 289 } 290 if (!empty($parent) || count($fop) || ($state < 0)) { 291 $html .= $this->drawHorizontalLine(); 292 } 293 /* draw the parents */ 294 if ($state >= 0 && (!empty($parent) || count($fop))) { 295 $unique = (empty($parent) || count($fop) == 0); 296 $html .= '<td align="left"><table class="tv_tree"><tbody>'; 297 if (!empty($parent)) { 298 $u = $unique ? 'c' : 't'; 299 $html .= '<tr><td ' . ($gen == 0 ? ' abbr="p' . $primaryChildFamily->getXref() . '@' . $u . '"' : '') . '>'; 300 $html .= $this->drawPerson($parent, $gen - 1, 1, $primaryChildFamily, $u); 301 $html .= '</td></tr>'; 302 } 303 if (count($fop)) { 304 $n = 0; 305 $nb = count($fop); 306 foreach ($fop as $p) { 307 $n++; 308 $u = $unique ? 'c' : ($n == $nb || empty($p[1]) ? 'b' : 'h'); 309 $html .= '<tr><td ' . ($gen == 0 ? ' abbr="p' . $p[1]->getXref() . '@' . $u . '"' : '') . '>' . $this->drawPerson($p[0], $gen - 1, 1, $p[1], $u) . '</td></tr>'; 310 } 311 } 312 $html .= '</tbody></table></td>'; 313 } 314 if ($state < 0) { 315 $html .= $this->drawVerticalLine($order); 316 } 317 $html .= '</tr></tbody></table>'; 318 if ($isRoot) { 319 $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>'; 320 } 321 322 return $html; 323 } 324 325 /** 326 * Draw a person name preceded by sex icon, with parents as tooltip 327 * 328 * @param Individual $individual an individual 329 * @param string $dashed if = 'dashed' print dashed top border to separate multiple spuses 330 * 331 * @return string 332 */ 333 private function drawPersonName(Individual $individual, $dashed = '') { 334 if ($this->all_partners === 'true') { 335 $family = $individual->getPrimaryChildFamily(); 336 if ($family) { 337 switch ($individual->getSex()) { 338 case 'M': 339 $title = ' title="' . strip_tags( 340 /* I18N: e.g. “Son of [father name & mother name]” */ 341 I18N::translate('Son of %s', $family->getFullName()) 342 ) . '"'; 343 break; 344 case 'F': 345 $title = ' title="' . strip_tags( 346 /* I18N: e.g. “Daughter of [father name & mother name]” */ 347 I18N::translate('Daughter of %s', $family->getFullName()) 348 ) . '"'; 349 break; 350 default: 351 $title = ' title="' . strip_tags( 352 /* I18N: e.g. “Child of [father name & mother name]” */ 353 I18N::translate('Child of %s', $family->getFullName()) 354 ) . '"'; 355 break; 356 } 357 } else { 358 $title = ''; 359 } 360 } else { 361 $title = ''; 362 } 363 $sex = $individual->getSex(); 364 365 return '<div class="tv' . $sex . ' ' . $dashed . '"' . $title . '><a href="' . $individual->getHtmlUrl() . '"></a>' . $individual->getFullName() . ' <span class="dates">' . $individual->getLifeSpan() . '</span></div>'; 366 } 367 368 /** 369 * Get the thumbnail image for the given person 370 * 371 * @param Individual $individual 372 * 373 * @return string 374 */ 375 private function getThumbnail(Individual $individual) { 376 if ($individual->getTree()->getPreference('SHOW_HIGHLIGHT_IMAGES')) { 377 return $individual->displayImage(); 378 } else { 379 return ''; 380 } 381 } 382 383 /** 384 * Draw a vertical line 385 * 386 * @param string $order A parameter that set how to draw this line with auto-redimensionning capabilities 387 * 388 * @return string 389 * WARNING : some tricky hacks are required in CSS to ensure cross-browser compliance 390 * some browsers shows an image, which imply a size limit in height, 391 * and some other browsers (ex: firefox) shows a <div> tag, which have no size limit in height 392 * Therefore, Firefox is a good choice to print very big trees. 393 */ 394 private function drawVerticalLine($order) { 395 return '<td class="tv_vline tv_vline_' . $order . '"><div class="tv_vline tv_vline_' . $order . '"></div></td>'; 396 } 397 398 /** 399 * Draw an horizontal line 400 */ 401 private function drawHorizontalLine() { 402 return '<td class="tv_hline"><div class="tv_hline"></div></td>'; 403 } 404} 405