xref: /webtrees/app/Module/DescendancyModule.php (revision e2a378d30d9bd3fff591da7a11c7cb5ead502323)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roachnamespace Fisharebest\Webtrees;
38c2e8227SGreg Roach
48c2e8227SGreg Roach/**
58c2e8227SGreg Roach * webtrees: online genealogy
68c2e8227SGreg Roach * Copyright (C) 2015 webtrees development team
78c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
88c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
98c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
108c2e8227SGreg Roach * (at your option) any later version.
118c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
128c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
138c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
148c2e8227SGreg Roach * GNU General Public License for more details.
158c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
168c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
178c2e8227SGreg Roach */
188c2e8227SGreg Roach
198c2e8227SGreg Roachuse Zend_Session;
208c2e8227SGreg Roach
218c2e8227SGreg Roach/**
228c2e8227SGreg Roach * Class DescendancyModule
238c2e8227SGreg Roach */
24*e2a378d3SGreg Roachclass DescendancyModule extends AbstractModule implements ModuleSidebarInterface {
258c2e8227SGreg Roach	/** {@inheritdoc} */
268c2e8227SGreg Roach	public function getTitle() {
278c2e8227SGreg Roach		return /* I18N: Name of a module/sidebar */
288c2e8227SGreg Roach			I18N::translate('Descendants');
298c2e8227SGreg Roach	}
308c2e8227SGreg Roach
318c2e8227SGreg Roach	/** {@inheritdoc} */
328c2e8227SGreg Roach	public function getDescription() {
338c2e8227SGreg Roach		return /* I18N: Description of the “Descendants” module */
348c2e8227SGreg Roach			I18N::translate('A sidebar showing the descendants of an individual.');
358c2e8227SGreg Roach	}
368c2e8227SGreg Roach
378c2e8227SGreg Roach	/** {@inheritdoc} */
388c2e8227SGreg Roach	public function modAction($modAction) {
3924ec66ceSGreg Roach		global $WT_TREE;
4024ec66ceSGreg Roach
418c2e8227SGreg Roach		Zend_Session::writeClose();
428c2e8227SGreg Roach		header('Content-Type: text/html; charset=UTF-8');
438c2e8227SGreg Roach
448c2e8227SGreg Roach		switch ($modAction) {
458c2e8227SGreg Roach		case 'search':
468c2e8227SGreg Roach			$search = Filter::get('search');
478c2e8227SGreg Roach			echo $this->search($search);
488c2e8227SGreg Roach			break;
498c2e8227SGreg Roach		case 'descendants':
5024ec66ceSGreg Roach			$individual = Individual::getInstance(Filter::get('xref', WT_REGEX_XREF), $WT_TREE);
518c2e8227SGreg Roach			if ($individual) {
528c2e8227SGreg Roach				echo $this->loadSpouses($individual, 1);
538c2e8227SGreg Roach			}
548c2e8227SGreg Roach			break;
558c2e8227SGreg Roach		default:
568c2e8227SGreg Roach			http_response_code(404);
578c2e8227SGreg Roach			break;
588c2e8227SGreg Roach		}
598c2e8227SGreg Roach	}
608c2e8227SGreg Roach
618c2e8227SGreg Roach	/** {@inheritdoc} */
628c2e8227SGreg Roach	public function defaultSidebarOrder() {
638c2e8227SGreg Roach		return 30;
648c2e8227SGreg Roach	}
658c2e8227SGreg Roach
668c2e8227SGreg Roach	/** {@inheritdoc} */
678c2e8227SGreg Roach	public function hasSidebarContent() {
688c2e8227SGreg Roach		return !Auth::isSearchEngine();
698c2e8227SGreg Roach	}
708c2e8227SGreg Roach
718c2e8227SGreg Roach	/** {@inheritdoc} */
728c2e8227SGreg Roach	public function getSidebarAjaxContent() {
738c2e8227SGreg Roach		return '';
748c2e8227SGreg Roach	}
758c2e8227SGreg Roach
768c2e8227SGreg Roach	/** {@inheritdoc} */
778c2e8227SGreg Roach	public function getSidebarContent() {
788c2e8227SGreg Roach		global $controller;
798c2e8227SGreg Roach
808c2e8227SGreg Roach		$controller->addInlineJavascript('
818c2e8227SGreg Roach			function dsearchQ() {
828c2e8227SGreg Roach				var query = jQuery("#sb_desc_name").val();
838c2e8227SGreg Roach				if (query.length>1) {
848c2e8227SGreg Roach					jQuery("#sb_desc_content").load("module.php?mod=' . $this->getName() . '&mod_action=search&search="+query);
858c2e8227SGreg Roach				}
868c2e8227SGreg Roach			}
878c2e8227SGreg Roach
888c2e8227SGreg Roach			jQuery("#sb_desc_name").focus(function(){this.select();});
898c2e8227SGreg Roach			jQuery("#sb_desc_name").blur(function(){if (this.value=="") this.value="' . I18N::translate('Search') . '";});
908c2e8227SGreg Roach			var dtimerid = null;
918c2e8227SGreg Roach			jQuery("#sb_desc_name").keyup(function(e) {
928c2e8227SGreg Roach				if (dtimerid) window.clearTimeout(dtimerid);
938c2e8227SGreg Roach				dtimerid = window.setTimeout("dsearchQ()", 500);
948c2e8227SGreg Roach			});
958c2e8227SGreg Roach
968c2e8227SGreg Roach			jQuery("#sb_desc_content").on("click", ".sb_desc_indi", function() {
978c2e8227SGreg Roach				var self = jQuery(this),
988c2e8227SGreg Roach					state = self.children(".plusminus"),
998c2e8227SGreg Roach					target = self.siblings("div");
1008c2e8227SGreg Roach				if(state.hasClass("icon-plus")) {
1018c2e8227SGreg Roach					if (jQuery.trim(target.html())) {
1028c2e8227SGreg Roach						target.show("fast"); // already got content so just show it
1038c2e8227SGreg Roach					} else {
1048c2e8227SGreg Roach						target
1058c2e8227SGreg Roach							.hide()
1068c2e8227SGreg Roach							.load(self.attr("href"), function(response, status, xhr) {
1078c2e8227SGreg Roach								if(status == "success" && response !== "") {
1088c2e8227SGreg Roach									target.show("fast");
1098c2e8227SGreg Roach								}
1108c2e8227SGreg Roach							})
1118c2e8227SGreg Roach					}
1128c2e8227SGreg Roach				} else {
1138c2e8227SGreg Roach					target.hide("fast");
1148c2e8227SGreg Roach				}
1158c2e8227SGreg Roach				state.toggleClass("icon-minus icon-plus");
1168c2e8227SGreg Roach				return false;
1178c2e8227SGreg Roach			});
1188c2e8227SGreg Roach		');
1198c2e8227SGreg Roach
1208c2e8227SGreg Roach		return
1218c2e8227SGreg Roach			'<form method="post" action="module.php?mod=' . $this->getName() . '&amp;mod_action=search" onsubmit="return false;">' .
1228c2e8227SGreg Roach			'<input type="search" name="sb_desc_name" id="sb_desc_name" placeholder="' . I18N::translate('Search') . '">' .
1238c2e8227SGreg Roach			'</form>' .
1248c2e8227SGreg Roach			'<div id="sb_desc_content">' .
1258c2e8227SGreg Roach			'<ul>' . $this->getPersonLi($controller->record, 1) . '</ul>' .
1268c2e8227SGreg Roach			'</div>';
1278c2e8227SGreg Roach	}
1288c2e8227SGreg Roach
1298c2e8227SGreg Roach	/**
1308c2e8227SGreg Roach	 * @param Individual $person
1318c2e8227SGreg Roach	 * @param integer       $generations
1328c2e8227SGreg Roach	 *
1338c2e8227SGreg Roach	 * @return string
1348c2e8227SGreg Roach	 */
1358c2e8227SGreg Roach	public function getPersonLi(Individual $person, $generations = 0) {
1368c2e8227SGreg Roach		$icon = $generations > 0 ? 'icon-minus' : 'icon-plus';
1378c2e8227SGreg Roach		$lifespan = $person->canShow() ? '(' . $person->getLifeSpan() . ')' : '';
1388c2e8227SGreg Roach		$spouses = $generations > 0 ? $this->loadSpouses($person, 0) : '';
1398c2e8227SGreg Roach		return sprintf('<li class="sb_desc_indi_li">
1408c2e8227SGreg Roach		                  <a class="sb_desc_indi" href="module.php?mod=%s&amp;mod_action=descendants&amp;xref=%s"><i class="plusminus %s"></i>%s %s %s</a>
1418c2e8227SGreg Roach		                  <a class="icon-button_indi" href="%s"></a>
1428c2e8227SGreg Roach		                  %s
1438c2e8227SGreg Roach		                  <div>%s</div>
1448c2e8227SGreg Roach		                </li>', $this->getName(), $person->getXref(), $icon, $person->getSexImage(), $person->getFullName(), $lifespan, $person->getHtmlUrl(), '', $spouses);
1458c2e8227SGreg Roach	}
1468c2e8227SGreg Roach
1478c2e8227SGreg Roach	/**
1488c2e8227SGreg Roach	 * @param Family     $family
1498c2e8227SGreg Roach	 * @param Individual $person
1508c2e8227SGreg Roach	 * @param integer       $generations
1518c2e8227SGreg Roach	 *
1528c2e8227SGreg Roach	 * @return string
1538c2e8227SGreg Roach	 */
1548c2e8227SGreg Roach	public function getFamilyLi(Family $family, Individual $person, $generations = 0) {
1558c2e8227SGreg Roach		$marryear = $family->getMarriageYear();
1568c2e8227SGreg Roach		$marr = $marryear ? '<i class="icon-rings"></i>' . $marryear : '';
1578c2e8227SGreg Roach		$fam = '<a href="' . $family->getHtmlUrl() . '" class="icon-button_family"></a>';
1588c2e8227SGreg Roach		$kids = $this->loadChildren($family, $generations);
1598c2e8227SGreg Roach		return sprintf('<li class="sb_desc_indi_li">
1608c2e8227SGreg Roach		                  <a class="sb_desc_indi" href="#"><i class="plusminus icon-minus"></i>%s %s %s</a>
1618c2e8227SGreg Roach		                  <a class="icon-button_indi" href="%s"></a>
1628c2e8227SGreg Roach		                  %s
1638c2e8227SGreg Roach		                  <div>%s</div>
1648c2e8227SGreg Roach		                </li>', $person->getSexImage(), $person->getFullName(), $marr, $person->getHtmlUrl(), $fam, $kids);
1658c2e8227SGreg Roach	}
1668c2e8227SGreg Roach
1678c2e8227SGreg Roach	/**
1688c2e8227SGreg Roach	 * @param string $query
1698c2e8227SGreg Roach	 *
1708c2e8227SGreg Roach	 * @return string
1718c2e8227SGreg Roach	 */
1728c2e8227SGreg Roach	public function search($query) {
17324ec66ceSGreg Roach		global $WT_TREE;
17424ec66ceSGreg Roach
1758c2e8227SGreg Roach		if (strlen($query) < 2) {
1768c2e8227SGreg Roach			return '';
1778c2e8227SGreg Roach		}
1788c2e8227SGreg Roach		$rows = Database::prepare(
1798c2e8227SGreg Roach			"SELECT i_id AS xref" .
1808c2e8227SGreg Roach			" FROM `##individuals`, `##name`" .
1818c2e8227SGreg Roach			" WHERE (i_id LIKE ? OR n_sort LIKE ?)" .
1828c2e8227SGreg Roach			" AND i_id=n_id AND i_file=n_file AND i_file=?" .
1838c2e8227SGreg Roach			" ORDER BY n_sort"
1848c2e8227SGreg Roach		)
18524ec66ceSGreg Roach			->execute(array("%{$query}%", "%{$query}%", $WT_TREE->getTreeId()))
1868c2e8227SGreg Roach			->fetchAll();
1878c2e8227SGreg Roach
1888c2e8227SGreg Roach		$out = '';
1898c2e8227SGreg Roach		foreach ($rows as $row) {
19024ec66ceSGreg Roach			$person = Individual::getInstance($row->xref, $WT_TREE);
1918c2e8227SGreg Roach			if ($person->canShowName()) {
1928c2e8227SGreg Roach				$out .= $this->getPersonLi($person);
1938c2e8227SGreg Roach			}
1948c2e8227SGreg Roach		}
1958c2e8227SGreg Roach		if ($out) {
1968c2e8227SGreg Roach			return '<ul>' . $out . '</ul>';
1978c2e8227SGreg Roach		} else {
1988c2e8227SGreg Roach			return '';
1998c2e8227SGreg Roach		}
2008c2e8227SGreg Roach	}
2018c2e8227SGreg Roach
2028c2e8227SGreg Roach	/**
2038c2e8227SGreg Roach	 * @param Individual $person
2048c2e8227SGreg Roach	 * @param integer       $generations
2058c2e8227SGreg Roach	 *
2068c2e8227SGreg Roach	 * @return string
2078c2e8227SGreg Roach	 */
2088c2e8227SGreg Roach	public function loadSpouses(Individual $person, $generations) {
2098c2e8227SGreg Roach		$out = '';
2108c2e8227SGreg Roach		if ($person && $person->canShow()) {
2118c2e8227SGreg Roach			foreach ($person->getSpouseFamilies() as $family) {
2128c2e8227SGreg Roach				$spouse = $family->getSpouse($person);
2138c2e8227SGreg Roach				if ($spouse) {
2148c2e8227SGreg Roach					$out .= $this->getFamilyLi($family, $spouse, $generations - 1);
2158c2e8227SGreg Roach				}
2168c2e8227SGreg Roach			}
2178c2e8227SGreg Roach			if (!$out) {
2188c2e8227SGreg Roach				$out = '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>';
2198c2e8227SGreg Roach			}
2208c2e8227SGreg Roach		}
2218c2e8227SGreg Roach		if ($out) {
2228c2e8227SGreg Roach			return '<ul>' . $out . '</ul>';
2238c2e8227SGreg Roach		} else {
2248c2e8227SGreg Roach			return '';
2258c2e8227SGreg Roach		}
2268c2e8227SGreg Roach	}
2278c2e8227SGreg Roach
2288c2e8227SGreg Roach	/**
2298c2e8227SGreg Roach	 * @param Family $family
2308c2e8227SGreg Roach	 * @param integer   $generations
2318c2e8227SGreg Roach	 *
2328c2e8227SGreg Roach	 * @return string
2338c2e8227SGreg Roach	 */
2348c2e8227SGreg Roach	public function loadChildren(Family $family, $generations) {
2358c2e8227SGreg Roach		$out = '';
2368c2e8227SGreg Roach		if ($family->canShow()) {
2378c2e8227SGreg Roach			$children = $family->getChildren();
2388c2e8227SGreg Roach			if ($children) {
2398c2e8227SGreg Roach				foreach ($children as $child) {
2408c2e8227SGreg Roach					$out .= $this->getPersonLi($child, $generations - 1);
2418c2e8227SGreg Roach				}
2428c2e8227SGreg Roach			} else {
2438c2e8227SGreg Roach				$out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>';
2448c2e8227SGreg Roach			}
2458c2e8227SGreg Roach		}
2468c2e8227SGreg Roach		if ($out) {
2478c2e8227SGreg Roach			return '<ul>' . $out . '</ul>';
2488c2e8227SGreg Roach		} else {
2498c2e8227SGreg Roach			return '';
2508c2e8227SGreg Roach		}
2518c2e8227SGreg Roach	}
2528c2e8227SGreg Roach}
253