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\Tree; 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('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 . '", "' . e($individual->getTree()->getName()) . '");']; 55 } 56 57 /** 58 * Return a JSON structure to a JSON request 59 * 60 * @param Tree $tree 61 * @param string $list list of JSON requests 62 * 63 * @return string 64 */ 65 public function getPersons(Tree $tree, $list) { 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, $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, $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 $chart_url = route('module', ['module' => 'tree', 'action' => 'Treeview', 'xref' => $individual->getXref(), 'ged' => $individual->getTree()->getName()]); 126 127 $hmtl = $this->getThumbnail($individual); 128 $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="icon-button_indi tv_link tv_treelink"></a>'; 129 foreach ($individual->getFacts(WT_EVENTS_BIRT, true) as $fact) { 130 $hmtl .= $fact->summary(); 131 } 132 if ($family) { 133 foreach ($family->getFacts(WT_EVENTS_MARR, true) as $fact) { 134 $hmtl .= $fact->summary(); 135 } 136 } 137 foreach ($individual->getFacts(WT_EVENTS_DEAT, true) as $fact) { 138 $hmtl .= $fact->summary(); 139 } 140 141 return '<div class="tv' . $individual->getSex() . ' tv_person_expanded">' . $hmtl . '</div>'; 142 } 143 144 /** 145 * Draw the children for some families 146 * 147 * @param Family[] $familyList array of families to draw the children for 148 * @param int $gen number of generations to draw 149 * @param bool $ajax setted to true for an ajax call 150 * 151 * @return string 152 */ 153 private function drawChildren(array $familyList, $gen = 1, $ajax = false) { 154 $html = ''; 155 $children2draw = []; 156 $f2load = []; 157 158 foreach ($familyList as $f) { 159 if (empty($f)) { 160 continue; 161 } 162 $children = $f->getChildren(); 163 if ($children) { 164 $f2load[] = $f->getXref(); 165 foreach ($children as $child) { 166 // Eliminate duplicates - e.g. when adopted by a step-parent 167 $children2draw[$child->getXref()] = $child; 168 } 169 } 170 } 171 $tc = count($children2draw); 172 if ($tc) { 173 $f2load = implode(',', $f2load); 174 $nbc = 0; 175 foreach ($children2draw as $child) { 176 $nbc++; 177 if ($tc == 1) { 178 $co = 'c'; // unique 179 } elseif ($nbc == 1) { 180 $co = 't'; // first 181 } elseif ($nbc == $tc) { 182 $co = 'b'; //last 183 } else { 184 $co = 'h'; 185 } 186 $html .= $this->drawPerson($child, $gen - 1, -1, null, $co); 187 } 188 if (!$ajax) { 189 $html = '<td align="right"' . ($gen == 0 ? ' abbr="c' . $f2load . '"' : '') . '>' . $html . '</td>' . $this->drawHorizontalLine(); 190 } 191 } 192 193 return $html; 194 } 195 196 /** 197 * Draw a person in the tree 198 * 199 * @param Individual $person The Person object to draw the box for 200 * @param int $gen The number of generations up or down to print 201 * @param int $state Whether we are going up or down the tree, -1 for descendents +1 for ancestors 202 * @param Family $pfamily 203 * @param string $order first (1), last(2), unique(0), or empty. Required for drawing lines between boxes 204 * @param bool $isRoot 205 * 206 * @return string 207 * 208 * Notes : "spouse" means explicitely married partners. Thus, the word "partner" 209 * (for "life partner") here fits much better than "spouse" or "mate" 210 * to translate properly the modern french meaning of "conjoint" 211 */ 212 private function drawPerson(Individual $person, $gen, $state, Family $pfamily = null, $order = null, $isRoot = false) { 213 if ($gen < 0) { 214 return ''; 215 } 216 if (!empty($pfamily)) { 217 $partner = $pfamily->getSpouse($person); 218 } else { 219 $partner = $person->getCurrentSpouse(); 220 } 221 if ($isRoot) { 222 $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>'; 223 } else { 224 $html = ''; 225 } 226 /* height 1% : this hack enable the div auto-dimensioning in td for FF & Chrome */ 227 $html .= '<table class="tv_tree"' . ($isRoot ? ' id="tv_tree"' : '') . ' style="height: 1%"><tbody><tr>'; 228 229 if ($state <= 0) { 230 // draw children 231 $html .= $this->drawChildren($person->getSpouseFamilies(), $gen); 232 } else { 233 // draw the parent’s lines 234 $html .= $this->drawVerticalLine($order) . $this->drawHorizontalLine(); 235 } 236 237 /* 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 !!! */ 238 // 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. 239 $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);">'; 240 $html .= $this->drawPersonName($person); 241 $fop = []; // $fop is fathers of partners 242 if (!is_null($partner)) { 243 $dashed = ''; 244 foreach ($person->getSpouseFamilies() as $family) { 245 $spouse = $family->getSpouse($person); 246 if ($spouse) { 247 $spouse_parents = $spouse->getPrimaryChildFamily(); 248 if ($spouse_parents && $spouse_parents->getHusband()) { 249 $fop[] = [$spouse_parents->getHusband(), $spouse_parents]; 250 } elseif ($spouse_parents && $spouse_parents->getWife()) { 251 $fop[] = [$spouse_parents->getWife(), $spouse_parents]; 252 } 253 $html .= $this->drawPersonName($spouse, $dashed); 254 $dashed = 'dashed'; 255 } 256 } 257 } 258 $html .= '</div></td>'; 259 260 $primaryChildFamily = $person->getPrimaryChildFamily(); 261 if (!empty($primaryChildFamily)) { 262 $parent = $primaryChildFamily->getHusband(); 263 if (empty($parent)) { 264 $parent = $primaryChildFamily->getWife(); 265 } 266 } 267 if (!empty($parent) || count($fop) || ($state < 0)) { 268 $html .= $this->drawHorizontalLine(); 269 } 270 /* draw the parents */ 271 if ($state >= 0 && (!empty($parent) || count($fop))) { 272 $unique = (empty($parent) || count($fop) == 0); 273 $html .= '<td align="left"><table class="tv_tree"><tbody>'; 274 if (!empty($parent)) { 275 $u = $unique ? 'c' : 't'; 276 $html .= '<tr><td ' . ($gen == 0 ? ' abbr="p' . $primaryChildFamily->getXref() . '@' . $u . '"' : '') . '>'; 277 $html .= $this->drawPerson($parent, $gen - 1, 1, $primaryChildFamily, $u); 278 $html .= '</td></tr>'; 279 } 280 if (count($fop)) { 281 $n = 0; 282 $nb = count($fop); 283 foreach ($fop as $p) { 284 $n++; 285 $u = $unique ? 'c' : ($n == $nb || empty($p[1]) ? 'b' : 'h'); 286 $html .= '<tr><td ' . ($gen == 0 ? ' abbr="p' . $p[1]->getXref() . '@' . $u . '"' : '') . '>' . $this->drawPerson($p[0], $gen - 1, 1, $p[1], $u) . '</td></tr>'; 287 } 288 } 289 $html .= '</tbody></table></td>'; 290 } 291 if ($state < 0) { 292 $html .= $this->drawVerticalLine($order); 293 } 294 $html .= '</tr></tbody></table>'; 295 if ($isRoot) { 296 $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>'; 297 } 298 299 return $html; 300 } 301 302 /** 303 * Draw a person name preceded by sex icon, with parents as tooltip 304 * 305 * @param Individual $individual an individual 306 * @param string $dashed if = 'dashed' print dashed top border to separate multiple spuses 307 * 308 * @return string 309 */ 310 private function drawPersonName(Individual $individual, $dashed = '') { 311 $family = $individual->getPrimaryChildFamily(); 312 if ($family) { 313 $family_name = strip_tags($family->getFullName()); 314 } else { 315 $family_name = I18N::translateContext('unknown family', 'unknown'); 316 } 317 switch ($individual->getSex()) { 318 case 'M': 319 $title = ' title="' . /* I18N: e.g. “Son of [father name & mother name]” */ I18N::translate('Son of %s', $family_name) . '"'; 320 break; 321 case 'F': 322 $title = ' title="' . /* I18N: e.g. “Daughter of [father name & mother name]” */ I18N::translate('Daughter of %s', $family_name) . '"'; 323 break; 324 default: 325 $title = ' title="' . /* I18N: e.g. “Child of [father name & mother name]” */ I18N::translate('Child of %s', $family_name) . '"'; 326 break; 327 } 328 $sex = $individual->getSex(); 329 330 return '<div class="tv' . $sex . ' ' . $dashed . '"' . $title . '><a href="' . e($individual->url()) . '"></a>' . $individual->getFullName() . ' <span class="dates">' . $individual->getLifeSpan() . '</span></div>'; 331 } 332 333 /** 334 * Get the thumbnail image for the given person 335 * 336 * @param Individual $individual 337 * 338 * @return string 339 */ 340 private function getThumbnail(Individual $individual) { 341 if ($individual->getTree()->getPreference('SHOW_HIGHLIGHT_IMAGES')) { 342 return $individual->displayImage(40, 50, 'crop', []); 343 } else { 344 return ''; 345 } 346 } 347 348 /** 349 * Draw a vertical line 350 * 351 * @param string $order A parameter that set how to draw this line with auto-redimensionning capabilities 352 * 353 * @return string 354 * WARNING : some tricky hacks are required in CSS to ensure cross-browser compliance 355 * some browsers shows an image, which imply a size limit in height, 356 * and some other browsers (ex: firefox) shows a <div> tag, which have no size limit in height 357 * Therefore, Firefox is a good choice to print very big trees. 358 */ 359 private function drawVerticalLine($order) { 360 return '<td class="tv_vline tv_vline_' . $order . '"><div class="tv_vline tv_vline_' . $order . '"></div></td>'; 361 } 362 363 /** 364 * Draw an horizontal line 365 */ 366 private function drawHorizontalLine() { 367 return '<td class="tv_hline"><div class="tv_hline"></div></td>'; 368 } 369} 370