xref: /webtrees/app/Module/DescendancyModule.php (revision 6bdf767435631ad1dc27ec1ffd855d43dbdce907)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
4*6bdf7674SGreg Roach * Copyright (C) 2017 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
248c2e8227SGreg Roach
258c2e8227SGreg Roach/**
268c2e8227SGreg Roach * Class DescendancyModule
278c2e8227SGreg Roach */
28e2a378d3SGreg Roachclass DescendancyModule extends AbstractModule implements ModuleSidebarInterface {
298c2e8227SGreg Roach	/** {@inheritdoc} */
308c2e8227SGreg Roach	public function getTitle() {
318c2e8227SGreg Roach		return /* I18N: Name of a module/sidebar */
328c2e8227SGreg Roach			I18N::translate('Descendants');
338c2e8227SGreg Roach	}
348c2e8227SGreg Roach
358c2e8227SGreg Roach	/** {@inheritdoc} */
368c2e8227SGreg Roach	public function getDescription() {
378c2e8227SGreg Roach		return /* I18N: Description of the “Descendants” module */
388c2e8227SGreg Roach			I18N::translate('A sidebar showing the descendants of an individual.');
398c2e8227SGreg Roach	}
408c2e8227SGreg Roach
4176692c8bSGreg Roach	/**
4276692c8bSGreg Roach	 * This is a general purpose hook, allowing modules to respond to routes
4376692c8bSGreg Roach	 * of the form module.php?mod=FOO&mod_action=BAR
4476692c8bSGreg Roach	 *
4576692c8bSGreg Roach	 * @param string $mod_action
4676692c8bSGreg Roach	 */
4776692c8bSGreg Roach	public function modAction($mod_action) {
4824ec66ceSGreg Roach		global $WT_TREE;
4924ec66ceSGreg Roach
508c2e8227SGreg Roach		header('Content-Type: text/html; charset=UTF-8');
518c2e8227SGreg Roach
5276692c8bSGreg Roach		switch ($mod_action) {
538c2e8227SGreg Roach		case 'search':
548c2e8227SGreg Roach			$search = Filter::get('search');
552622ba92SGreg Roach			echo $this->search($search, $WT_TREE);
568c2e8227SGreg Roach			break;
578c2e8227SGreg Roach		case 'descendants':
5824ec66ceSGreg Roach			$individual = Individual::getInstance(Filter::get('xref', WT_REGEX_XREF), $WT_TREE);
598c2e8227SGreg Roach			if ($individual) {
608c2e8227SGreg Roach				echo $this->loadSpouses($individual, 1);
618c2e8227SGreg Roach			}
628c2e8227SGreg Roach			break;
638c2e8227SGreg Roach		default:
648c2e8227SGreg Roach			http_response_code(404);
658c2e8227SGreg Roach			break;
668c2e8227SGreg Roach		}
678c2e8227SGreg Roach	}
688c2e8227SGreg Roach
698c2e8227SGreg Roach	/** {@inheritdoc} */
708c2e8227SGreg Roach	public function defaultSidebarOrder() {
718c2e8227SGreg Roach		return 30;
728c2e8227SGreg Roach	}
738c2e8227SGreg Roach
748c2e8227SGreg Roach	/** {@inheritdoc} */
758c2e8227SGreg Roach	public function hasSidebarContent() {
7638a9583bSGreg Roach		return true;
778c2e8227SGreg Roach	}
788c2e8227SGreg Roach
798c2e8227SGreg Roach	/** {@inheritdoc} */
808c2e8227SGreg Roach	public function getSidebarAjaxContent() {
818c2e8227SGreg Roach		return '';
828c2e8227SGreg Roach	}
838c2e8227SGreg Roach
8476692c8bSGreg Roach	/**
8576692c8bSGreg Roach	 * Load this sidebar synchronously.
8676692c8bSGreg Roach	 *
8776692c8bSGreg Roach	 * @return string
8876692c8bSGreg Roach	 */
898c2e8227SGreg Roach	public function getSidebarContent() {
908c2e8227SGreg Roach		global $controller;
918c2e8227SGreg Roach
928c2e8227SGreg Roach		$controller->addInlineJavascript('
938c2e8227SGreg Roach			function dsearchQ() {
94564ae2d7SGreg Roach				var query = $("#sb_desc_name").val();
958c2e8227SGreg Roach				if (query.length>1) {
96564ae2d7SGreg Roach					$("#sb_desc_content").load("module.php?mod=' . $this->getName() . '&mod_action=search&search="+query);
978c2e8227SGreg Roach				}
988c2e8227SGreg Roach			}
998c2e8227SGreg Roach
100564ae2d7SGreg Roach			$("#sb_desc_name").focus(function(){this.select();});
101564ae2d7SGreg Roach			$("#sb_desc_name").blur(function(){if (this.value=="") this.value="' . I18N::translate('Search') . '";});
1028c2e8227SGreg Roach			var dtimerid = null;
103564ae2d7SGreg Roach			$("#sb_desc_name").keyup(function(e) {
1048c2e8227SGreg Roach				if (dtimerid) window.clearTimeout(dtimerid);
1058c2e8227SGreg Roach				dtimerid = window.setTimeout("dsearchQ()", 500);
1068c2e8227SGreg Roach			});
1078c2e8227SGreg Roach
108564ae2d7SGreg Roach			$("#sb_desc_content").on("click", ".sb_desc_indi", function() {
109564ae2d7SGreg Roach				var self = $(this),
1108c2e8227SGreg Roach					state = self.children(".plusminus"),
1118c2e8227SGreg Roach					target = self.siblings("div");
1128c2e8227SGreg Roach				if(state.hasClass("icon-plus")) {
1138c2e8227SGreg Roach					if (jQuery.trim(target.html())) {
1148c2e8227SGreg Roach						target.show("fast"); // already got content so just show it
1158c2e8227SGreg Roach					} else {
1168c2e8227SGreg Roach						target
1178c2e8227SGreg Roach							.hide()
1188c2e8227SGreg Roach							.load(self.attr("href"), function(response, status, xhr) {
1198c2e8227SGreg Roach								if(status == "success" && response !== "") {
1208c2e8227SGreg Roach									target.show("fast");
1218c2e8227SGreg Roach								}
1228c2e8227SGreg Roach							})
1238c2e8227SGreg Roach					}
1248c2e8227SGreg Roach				} else {
1258c2e8227SGreg Roach					target.hide("fast");
1268c2e8227SGreg Roach				}
1278c2e8227SGreg Roach				state.toggleClass("icon-minus icon-plus");
1288c2e8227SGreg Roach				return false;
1298c2e8227SGreg Roach			});
1308c2e8227SGreg Roach		');
1318c2e8227SGreg Roach
1328c2e8227SGreg Roach		return
1338c2e8227SGreg Roach			'<form method="post" action="module.php?mod=' . $this->getName() . '&amp;mod_action=search" onsubmit="return false;">' .
1348c2e8227SGreg Roach			'<input type="search" name="sb_desc_name" id="sb_desc_name" placeholder="' . I18N::translate('Search') . '">' .
1358c2e8227SGreg Roach			'</form>' .
1368c2e8227SGreg Roach			'<div id="sb_desc_content">' .
1378c2e8227SGreg Roach			'<ul>' . $this->getPersonLi($controller->record, 1) . '</ul>' .
1388c2e8227SGreg Roach			'</div>';
1398c2e8227SGreg Roach	}
1408c2e8227SGreg Roach
1418c2e8227SGreg Roach	/**
14276692c8bSGreg Roach	 * Format an individual in a list.
14376692c8bSGreg Roach	 *
1448c2e8227SGreg Roach	 * @param Individual $person
145cbc1590aSGreg Roach	 * @param int        $generations
1468c2e8227SGreg Roach	 *
1478c2e8227SGreg Roach	 * @return string
1488c2e8227SGreg Roach	 */
1498c2e8227SGreg Roach	public function getPersonLi(Individual $person, $generations = 0) {
1508c2e8227SGreg Roach		$icon     = $generations > 0 ? 'icon-minus' : 'icon-plus';
1518c2e8227SGreg Roach		$lifespan = $person->canShow() ? '(' . $person->getLifeSpan() . ')' : '';
1528c2e8227SGreg Roach		$spouses  = $generations > 0 ? $this->loadSpouses($person, 0) : '';
1532622ba92SGreg Roach
1542622ba92SGreg Roach		return
1552622ba92SGreg Roach			'<li class="sb_desc_indi_li">' .
1562622ba92SGreg Roach			'<a class="sb_desc_indi" href="module.php?mod=' . $this->getName() . '&amp;mod_action=descendants&amp;xref=' . $person->getXref() . '">' .
1572622ba92SGreg Roach			'<i class="plusminus ' . $icon . '"></i>' .
1582622ba92SGreg Roach			$person->getSexImage() . $person->getFullName() . $lifespan .
1592622ba92SGreg Roach			'</a>' .
1602622ba92SGreg Roach			'<a class="icon-button_indi" href="' . $person->getHtmlUrl() . '"></a>' .
1612622ba92SGreg Roach			'<div>' . $spouses . '</div>' .
1622622ba92SGreg Roach			'</li>';
1638c2e8227SGreg Roach	}
1648c2e8227SGreg Roach
1658c2e8227SGreg Roach	/**
16676692c8bSGreg Roach	 * Format a family in a list.
16776692c8bSGreg Roach	 *
1688c2e8227SGreg Roach	 * @param Family     $family
1698c2e8227SGreg Roach	 * @param Individual $person
170cbc1590aSGreg Roach	 * @param int        $generations
1718c2e8227SGreg Roach	 *
1728c2e8227SGreg Roach	 * @return string
1738c2e8227SGreg Roach	 */
1748c2e8227SGreg Roach	public function getFamilyLi(Family $family, Individual $person, $generations = 0) {
1752622ba92SGreg Roach		$spouse = $family->getSpouse($person);
1762622ba92SGreg Roach		if ($spouse) {
1772622ba92SGreg Roach			$spouse_name = $spouse->getSexImage() . $spouse->getFullName();
1782622ba92SGreg Roach			$spouse_link = '<a class="icon-button_indi" href="' . $spouse->getHtmlUrl() . '"></a>';
1792622ba92SGreg Roach		} else {
1802622ba92SGreg Roach			$spouse_name = '';
1812622ba92SGreg Roach			$spouse_link = '';
1822622ba92SGreg Roach		}
1832622ba92SGreg Roach
1848c2e8227SGreg Roach		$marryear = $family->getMarriageYear();
1858c2e8227SGreg Roach		$marr     = $marryear ? '<i class="icon-rings"></i>' . $marryear : '';
1862622ba92SGreg Roach
1872622ba92SGreg Roach		return
1882622ba92SGreg Roach			'<li class="sb_desc_indi_li">' .
1892622ba92SGreg Roach			'<a class="sb_desc_indi" href="#"><i class="plusminus icon-minus"></i>' . $spouse_name . $marr . '</a>' .
1902622ba92SGreg Roach			$spouse_link .
1912622ba92SGreg Roach			'<a href="' . $family->getHtmlUrl() . '" class="icon-button_family"></a>' .
1922622ba92SGreg Roach		 '<div>' . $this->loadChildren($family, $generations) . '</div>' .
1932622ba92SGreg Roach			'</li>';
1948c2e8227SGreg Roach	}
1958c2e8227SGreg Roach
1968c2e8227SGreg Roach	/**
19776692c8bSGreg Roach	 * Respond to an autocomplete search request.
19876692c8bSGreg Roach	 *
1992622ba92SGreg Roach	 * @param string $query Search for this term
2002622ba92SGreg Roach	 * @param Tree   $tree  Search in this tree
2018c2e8227SGreg Roach	 *
2028c2e8227SGreg Roach	 * @return string
2038c2e8227SGreg Roach	 */
2042622ba92SGreg Roach	public function search($query, Tree $tree) {
2058c2e8227SGreg Roach		if (strlen($query) < 2) {
2068c2e8227SGreg Roach			return '';
2078c2e8227SGreg Roach		}
2082622ba92SGreg Roach
2098c2e8227SGreg Roach		$rows = Database::prepare(
2108c2e8227SGreg Roach			"SELECT i_id AS xref" .
2112622ba92SGreg Roach			" FROM `##individuals`" .
2122622ba92SGreg Roach			" JOIN `##name` ON i_id = n_id AND i_file = n_file" .
2132622ba92SGreg Roach			" WHERE n_sort LIKE CONCAT('%', :query, '%') AND i_file = :tree_id" .
2148c2e8227SGreg Roach			" ORDER BY n_sort"
21513abd6f3SGreg Roach		)->execute([
2162622ba92SGreg Roach			'query'   => $query,
2172622ba92SGreg Roach			'tree_id' => $tree->getTreeId(),
21813abd6f3SGreg Roach		])->fetchAll();
2198c2e8227SGreg Roach
2208c2e8227SGreg Roach		$out = '';
2218c2e8227SGreg Roach		foreach ($rows as $row) {
2222622ba92SGreg Roach			$person = Individual::getInstance($row->xref, $tree);
223202f95afSGreg Roach			if ($person && $person->canShowName()) {
2248c2e8227SGreg Roach				$out .= $this->getPersonLi($person);
2258c2e8227SGreg Roach			}
2268c2e8227SGreg Roach		}
2278c2e8227SGreg Roach		if ($out) {
2288c2e8227SGreg Roach			return '<ul>' . $out . '</ul>';
2298c2e8227SGreg Roach		} else {
2308c2e8227SGreg Roach			return '';
2318c2e8227SGreg Roach		}
2328c2e8227SGreg Roach	}
2338c2e8227SGreg Roach
2348c2e8227SGreg Roach	/**
23576692c8bSGreg Roach	 * Display spouses.
23676692c8bSGreg Roach	 *
2378c2e8227SGreg Roach	 * @param Individual $person
238cbc1590aSGreg Roach	 * @param int        $generations
2398c2e8227SGreg Roach	 *
2408c2e8227SGreg Roach	 * @return string
2418c2e8227SGreg Roach	 */
2428c2e8227SGreg Roach	public function loadSpouses(Individual $person, $generations) {
2438c2e8227SGreg Roach		$out = '';
2448c2e8227SGreg Roach		if ($person && $person->canShow()) {
2458c2e8227SGreg Roach			foreach ($person->getSpouseFamilies() as $family) {
2462622ba92SGreg Roach				$out .= $this->getFamilyLi($family, $person, $generations - 1);
2478c2e8227SGreg Roach			}
2488c2e8227SGreg Roach		}
2498c2e8227SGreg Roach		if ($out) {
2508c2e8227SGreg Roach			return '<ul>' . $out . '</ul>';
2518c2e8227SGreg Roach		} else {
2528c2e8227SGreg Roach			return '';
2538c2e8227SGreg Roach		}
2548c2e8227SGreg Roach	}
2558c2e8227SGreg Roach
2568c2e8227SGreg Roach	/**
25776692c8bSGreg Roach	 * Display descendants.
25876692c8bSGreg Roach	 *
2598c2e8227SGreg Roach	 * @param Family $family
260cbc1590aSGreg Roach	 * @param int    $generations
2618c2e8227SGreg Roach	 *
2628c2e8227SGreg Roach	 * @return string
2638c2e8227SGreg Roach	 */
2648c2e8227SGreg Roach	public function loadChildren(Family $family, $generations) {
2658c2e8227SGreg Roach		$out = '';
2668c2e8227SGreg Roach		if ($family->canShow()) {
2678c2e8227SGreg Roach			$children = $family->getChildren();
2688c2e8227SGreg Roach			if ($children) {
2698c2e8227SGreg Roach				foreach ($children as $child) {
2708c2e8227SGreg Roach					$out .= $this->getPersonLi($child, $generations - 1);
2718c2e8227SGreg Roach				}
2728c2e8227SGreg Roach			} else {
2738c2e8227SGreg Roach				$out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>';
2748c2e8227SGreg Roach			}
2758c2e8227SGreg Roach		}
2768c2e8227SGreg Roach		if ($out) {
2778c2e8227SGreg Roach			return '<ul>' . $out . '</ul>';
2788c2e8227SGreg Roach		} else {
2798c2e8227SGreg Roach			return '';
2808c2e8227SGreg Roach		}
2818c2e8227SGreg Roach	}
2828c2e8227SGreg Roach}
283