xref: /webtrees/app/Module/DescendancyModule.php (revision 0a016d046f63b3b1ece7ec913216782c17f0cbad)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 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() {
77		return true;
78	}
79
80	/** {@inheritdoc} */
81	public function getSidebarAjaxContent() {
82		return '';
83	}
84
85	/**
86	 * Load this sidebar synchronously.
87	 *
88	 * @return string
89	 */
90	public function getSidebarContent() {
91		global $controller;
92
93		$controller->addInlineJavascript('
94			function dsearchQ() {
95				var query = $("#sb_desc_name").val();
96				if (query.length>1) {
97					$("#sb_desc_content").load("module.php?mod=' . $this->getName() . '&mod_action=search&search="+query);
98				}
99			}
100
101			$("#sb_desc_name").focus(function(){this.select();});
102			$("#sb_desc_name").blur(function(){if (this.value=="") this.value="' . I18N::translate('Search') . '";});
103			var dtimerid = null;
104			$("#sb_desc_name").keyup(function(e) {
105				if (dtimerid) window.clearTimeout(dtimerid);
106				dtimerid = window.setTimeout("dsearchQ()", 500);
107			});
108
109			$("#sb_desc_content").on("click", ".sb_desc_indi", function() {
110				var self = $(this),
111					state = self.children(".plusminus"),
112					target = self.siblings("div");
113				if(state.hasClass("icon-plus")) {
114					if (jQuery.trim(target.html())) {
115						target.show("fast"); // already got content so just show it
116					} else {
117						target
118							.hide()
119							.load(self.attr("href"), function(response, status, xhr) {
120								if(status == "success" && response !== "") {
121									target.show("fast");
122								}
123							})
124					}
125				} else {
126					target.hide("fast");
127				}
128				state.toggleClass("icon-minus icon-plus");
129				return false;
130			});
131		');
132
133		return
134			'<form method="post" action="module.php?mod=' . $this->getName() . '&amp;mod_action=search" onsubmit="return false;">' .
135			'<input type="search" name="sb_desc_name" id="sb_desc_name" placeholder="' . I18N::translate('Search') . '">' .
136			'</form>' .
137			'<div id="sb_desc_content">' .
138			'<ul>' . $this->getPersonLi($controller->record, 1) . '</ul>' .
139			'</div>';
140	}
141
142	/**
143	 * Format an individual in a list.
144	 *
145	 * @param Individual $person
146	 * @param int        $generations
147	 *
148	 * @return string
149	 */
150	public function getPersonLi(Individual $person, $generations = 0) {
151		$icon     = $generations > 0 ? 'icon-minus' : 'icon-plus';
152		$lifespan = $person->canShow() ? '(' . $person->getLifeSpan() . ')' : '';
153		$spouses  = $generations > 0 ? $this->loadSpouses($person, 0) : '';
154
155		return
156			'<li class="sb_desc_indi_li">' .
157			'<a class="sb_desc_indi" href="module.php?mod=' . $this->getName() . '&amp;mod_action=descendants&amp;xref=' . $person->getXref() . '">' .
158			'<i class="plusminus ' . $icon . '"></i>' .
159			$person->getSexImage() . $person->getFullName() . $lifespan .
160			'</a>' .
161			FontAwesome::linkIcon('individual', $person->getFullName(), ['href' => $person->getRawUrl()]) .
162			'<div>' . $spouses . '</div>' .
163			'</li>';
164	}
165
166	/**
167	 * Format a family in a list.
168	 *
169	 * @param Family     $family
170	 * @param Individual $person
171	 * @param int        $generations
172	 *
173	 * @return string
174	 */
175	public function getFamilyLi(Family $family, Individual $person, $generations = 0) {
176		$spouse = $family->getSpouse($person);
177		if ($spouse) {
178			$spouse_name = $spouse->getSexImage() . $spouse->getFullName();
179			$spouse_link = FontAwesome::linkIcon('individual', $spouse->getFullName(), ['href' => $person->getRawUrl()]);
180;
181		} else {
182			$spouse_name = '';
183			$spouse_link = '';
184		}
185
186		$marryear = $family->getMarriageYear();
187		$marr     = $marryear ? '<i class="icon-rings"></i>' . $marryear : '';
188
189		return
190			'<li class="sb_desc_indi_li">' .
191			'<a class="sb_desc_indi" href="#"><i class="plusminus icon-minus"></i>' . $spouse_name . $marr . '</a>' .
192			$spouse_link .
193			FontAwesome::linkIcon('family', $family->getFullName(), ['href' => $family->getRawUrl()]) .
194		 '<div>' . $this->loadChildren($family, $generations) . '</div>' .
195			'</li>';
196	}
197
198	/**
199	 * Respond to an autocomplete search request.
200	 *
201	 * @param string $query Search for this term
202	 * @param Tree   $tree  Search in this tree
203	 *
204	 * @return string
205	 */
206	public function search($query, Tree $tree) {
207		if (strlen($query) < 2) {
208			return '';
209		}
210
211		$rows = Database::prepare(
212			"SELECT i_id AS xref" .
213			" FROM `##individuals`" .
214			" JOIN `##name` ON i_id = n_id AND i_file = n_file" .
215			" WHERE n_sort LIKE CONCAT('%', :query, '%') AND i_file = :tree_id" .
216			" ORDER BY n_sort"
217		)->execute([
218			'query'   => $query,
219			'tree_id' => $tree->getTreeId(),
220		])->fetchAll();
221
222		$out = '';
223		foreach ($rows as $row) {
224			$person = Individual::getInstance($row->xref, $tree);
225			if ($person && $person->canShowName()) {
226				$out .= $this->getPersonLi($person);
227			}
228		}
229		if ($out) {
230			return '<ul>' . $out . '</ul>';
231		} else {
232			return '';
233		}
234	}
235
236	/**
237	 * Display spouses.
238	 *
239	 * @param Individual $person
240	 * @param int        $generations
241	 *
242	 * @return string
243	 */
244	public function loadSpouses(Individual $person, $generations) {
245		$out = '';
246		if ($person && $person->canShow()) {
247			foreach ($person->getSpouseFamilies() as $family) {
248				$out .= $this->getFamilyLi($family, $person, $generations - 1);
249			}
250		}
251		if ($out) {
252			return '<ul>' . $out . '</ul>';
253		} else {
254			return '';
255		}
256	}
257
258	/**
259	 * Display descendants.
260	 *
261	 * @param Family $family
262	 * @param int    $generations
263	 *
264	 * @return string
265	 */
266	public function loadChildren(Family $family, $generations) {
267		$out = '';
268		if ($family->canShow()) {
269			$children = $family->getChildren();
270			if ($children) {
271				foreach ($children as $child) {
272					$out .= $this->getPersonLi($child, $generations - 1);
273				}
274			} else {
275				$out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>';
276			}
277		}
278		if ($out) {
279			return '<ul>' . $out . '</ul>';
280		} else {
281			return '';
282		}
283	}
284}
285