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; 17 18use Fisharebest\Webtrees\Database; 19use Fisharebest\Webtrees\Family; 20use Fisharebest\Webtrees\Filter; 21use Fisharebest\Webtrees\FontAwesome; 22use Fisharebest\Webtrees\I18N; 23use Fisharebest\Webtrees\Individual; 24use Fisharebest\Webtrees\Tree; 25 26/** 27 * Class DescendancyModule 28 */ 29class DescendancyModule extends AbstractModule implements ModuleSidebarInterface { 30 /** {@inheritdoc} */ 31 public function getTitle() { 32 return /* I18N: Name of a module/sidebar */ 33 I18N::translate('Descendants'); 34 } 35 36 /** {@inheritdoc} */ 37 public function getDescription() { 38 return /* I18N: Description of the “Descendants” module */ 39 I18N::translate('A sidebar showing the descendants of an individual.'); 40 } 41 42 /** 43 * This is a general purpose hook, allowing modules to respond to routes 44 * of the form module.php?mod=FOO&mod_action=BAR 45 * 46 * @param string $mod_action 47 */ 48 public function modAction($mod_action) { 49 global $WT_TREE; 50 51 header('Content-Type: text/html; charset=UTF-8'); 52 53 switch ($mod_action) { 54 case 'search': 55 $search = Filter::get('search'); 56 echo $this->search($search, $WT_TREE); 57 break; 58 case 'descendants': 59 $individual = Individual::getInstance(Filter::get('xref', WT_REGEX_XREF), $WT_TREE); 60 if ($individual) { 61 echo $this->loadSpouses($individual, 1); 62 } 63 break; 64 default: 65 http_response_code(404); 66 break; 67 } 68 } 69 70 /** {@inheritdoc} */ 71 public function defaultSidebarOrder() { 72 return 30; 73 } 74 75 /** {@inheritdoc} */ 76 public function hasSidebarContent(Individual $individual) { 77 return true; 78 } 79 80 /** {@inheritdoc} */ 81 public function getSidebarAjaxContent() { 82 return ''; 83 } 84 85 /** 86 * Load this sidebar synchronously. 87 * 88 * @param Individual $individual 89 * 90 * @return string 91 */ 92 public function getSidebarContent(Individual $individual) { 93 return view('modules/descendancy', [ 94 'individual_list' => $this->getPersonLi($individual, 1), 95 ]); 96 97 echo 98 '<form method="post" action="module.php?mod=' . $this->getName() . '&mod_action=search" onsubmit="return false;">' . 99 '<input type="search" name="sb_desc_name" id="sb_desc_name" placeholder="' . I18N::translate('Search') . '">' . 100 '</form>' . 101 '<div id="sb_desc_content">' . 102 '<ul>' . $this->getPersonLi($individual, 1) . '</ul>' . 103 '</div> 104 <script> 105 function dsearchQ() { 106 var query = $("#sb_desc_name").val(); 107 if (query.length>1) { 108 $("#sb_desc_content").load("module.php?mod=' . $this->getName() . '&mod_action=search&search="+query); 109 } 110 } 111 112 $("#sb_desc_name").focus(function(){this.select();}); 113 $("#sb_desc_name").blur(function(){if (this.value === "") this.value="' . I18N::translate('Search') . '";}); 114 var dtimerid = null; 115 $("#sb_desc_name").keyup(function(e) { 116 if (dtimerid) window.clearTimeout(dtimerid); 117 dtimerid = window.setTimeout("dsearchQ()", 500); 118 }); 119 120 $("#sb_desc_content").on("click", ".sb_desc_indi", function() { 121 var self = $(this), 122 state = self.children(".plusminus"), 123 target = self.siblings("div"); 124 if(state.hasClass("icon-plus")) { 125 if (jQuery.trim(target.html())) { 126 target.show("fast"); // already got content so just show it 127 } else { 128 target 129 .hide() 130 .load(self.attr("href"), function(response, status, xhr) { 131 if(status === "success" && response !== "") { 132 target.show("fast"); 133 } 134 }) 135 } 136 } else { 137 target.hide("fast"); 138 } 139 state.toggleClass("icon-minus icon-plus"); 140 return false; 141 }); 142 </script>'; 143 } 144 145 /** 146 * Format an individual in a list. 147 * 148 * @param Individual $person 149 * @param int $generations 150 * 151 * @return string 152 */ 153 public function getPersonLi(Individual $person, $generations = 0) { 154 $icon = $generations > 0 ? 'icon-minus' : 'icon-plus'; 155 $lifespan = $person->canShow() ? '(' . $person->getLifeSpan() . ')' : ''; 156 $spouses = $generations > 0 ? $this->loadSpouses($person, 0) : ''; 157 158 return 159 '<li class="sb_desc_indi_li">' . 160 '<a class="sb_desc_indi" href="module.php?mod=' . $this->getName() . '&mod_action=descendants&xref=' . $person->getXref() . '">' . 161 '<i class="plusminus ' . $icon . '"></i>' . 162 $person->getSexImage() . $person->getFullName() . $lifespan . 163 '</a>' . 164 FontAwesome::linkIcon('individual', $person->getFullName(), ['href' => $person->url()]) . 165 '<div>' . $spouses . '</div>' . 166 '</li>'; 167 } 168 169 /** 170 * Format a family in a list. 171 * 172 * @param Family $family 173 * @param Individual $person 174 * @param int $generations 175 * 176 * @return string 177 */ 178 public function getFamilyLi(Family $family, Individual $person, $generations = 0) { 179 $spouse = $family->getSpouse($person); 180 if ($spouse) { 181 $spouse_name = $spouse->getSexImage() . $spouse->getFullName(); 182 $spouse_link = FontAwesome::linkIcon('individual', $spouse->getFullName(), ['href' => $person->url()]); 183 } else { 184 $spouse_name = ''; 185 $spouse_link = ''; 186 } 187 188 $marryear = $family->getMarriageYear(); 189 $marr = $marryear ? '<i class="icon-rings"></i>' . $marryear : ''; 190 191 return 192 '<li class="sb_desc_indi_li">' . 193 '<a class="sb_desc_indi" href="#"><i class="plusminus icon-minus"></i>' . $spouse_name . $marr . '</a>' . 194 $spouse_link . 195 FontAwesome::linkIcon('family', $family->getFullName(), ['href' => $family->url()]) . 196 '<div>' . $this->loadChildren($family, $generations) . '</div>' . 197 '</li>'; 198 } 199 200 /** 201 * Respond to an autocomplete search request. 202 * 203 * @param string $query Search for this term 204 * @param Tree $tree Search in this tree 205 * 206 * @return string 207 */ 208 public function search($query, Tree $tree) { 209 if (strlen($query) < 2) { 210 return ''; 211 } 212 213 $rows = Database::prepare( 214 "SELECT i_id AS xref" . 215 " FROM `##individuals`" . 216 " JOIN `##name` ON i_id = n_id AND i_file = n_file" . 217 " WHERE n_sort LIKE CONCAT('%', :query, '%') AND i_file = :tree_id" . 218 " ORDER BY n_sort" 219 )->execute([ 220 'query' => $query, 221 'tree_id' => $tree->getTreeId(), 222 ])->fetchAll(); 223 224 $out = ''; 225 foreach ($rows as $row) { 226 $person = Individual::getInstance($row->xref, $tree); 227 if ($person && $person->canShowName()) { 228 $out .= $this->getPersonLi($person); 229 } 230 } 231 if ($out) { 232 return '<ul>' . $out . '</ul>'; 233 } else { 234 return ''; 235 } 236 } 237 238 /** 239 * Display spouses. 240 * 241 * @param Individual $person 242 * @param int $generations 243 * 244 * @return string 245 */ 246 public function loadSpouses(Individual $person, $generations) { 247 $out = ''; 248 if ($person && $person->canShow()) { 249 foreach ($person->getSpouseFamilies() as $family) { 250 $out .= $this->getFamilyLi($family, $person, $generations - 1); 251 } 252 } 253 if ($out) { 254 return '<ul>' . $out . '</ul>'; 255 } else { 256 return ''; 257 } 258 } 259 260 /** 261 * Display descendants. 262 * 263 * @param Family $family 264 * @param int $generations 265 * 266 * @return string 267 */ 268 public function loadChildren(Family $family, $generations) { 269 $out = ''; 270 if ($family->canShow()) { 271 $children = $family->getChildren(); 272 if ($children) { 273 foreach ($children as $child) { 274 $out .= $this->getPersonLi($child, $generations - 1); 275 } 276 } else { 277 $out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>'; 278 } 279 } 280 if ($out) { 281 return '<ul>' . $out . '</ul>'; 282 } else { 283 return ''; 284 } 285 } 286} 287