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