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