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