xref: /webtrees/app/Module/DescendancyModule.php (revision 1f3fb95c905e623518962b025974a0e6c19d068b)
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		} else {
181			$spouse_name = '';
182			$spouse_link = '';
183		}
184
185		$marryear = $family->getMarriageYear();
186		$marr     = $marryear ? '<i class="icon-rings"></i>' . $marryear : '';
187
188		return
189			'<li class="sb_desc_indi_li">' .
190			'<a class="sb_desc_indi" href="#"><i class="plusminus icon-minus"></i>' . $spouse_name . $marr . '</a>' .
191			$spouse_link .
192			FontAwesome::linkIcon('family', $family->getFullName(), ['href' => $family->getRawUrl()]) .
193		 '<div>' . $this->loadChildren($family, $generations) . '</div>' .
194			'</li>';
195	}
196
197	/**
198	 * Respond to an autocomplete search request.
199	 *
200	 * @param string $query Search for this term
201	 * @param Tree   $tree  Search in this tree
202	 *
203	 * @return string
204	 */
205	public function search($query, Tree $tree) {
206		if (strlen($query) < 2) {
207			return '';
208		}
209
210		$rows = Database::prepare(
211			"SELECT i_id AS xref" .
212			" FROM `##individuals`" .
213			" JOIN `##name` ON i_id = n_id AND i_file = n_file" .
214			" WHERE n_sort LIKE CONCAT('%', :query, '%') AND i_file = :tree_id" .
215			" ORDER BY n_sort"
216		)->execute([
217			'query'   => $query,
218			'tree_id' => $tree->getTreeId(),
219		])->fetchAll();
220
221		$out = '';
222		foreach ($rows as $row) {
223			$person = Individual::getInstance($row->xref, $tree);
224			if ($person && $person->canShowName()) {
225				$out .= $this->getPersonLi($person);
226			}
227		}
228		if ($out) {
229			return '<ul>' . $out . '</ul>';
230		} else {
231			return '';
232		}
233	}
234
235	/**
236	 * Display spouses.
237	 *
238	 * @param Individual $person
239	 * @param int        $generations
240	 *
241	 * @return string
242	 */
243	public function loadSpouses(Individual $person, $generations) {
244		$out = '';
245		if ($person && $person->canShow()) {
246			foreach ($person->getSpouseFamilies() as $family) {
247				$out .= $this->getFamilyLi($family, $person, $generations - 1);
248			}
249		}
250		if ($out) {
251			return '<ul>' . $out . '</ul>';
252		} else {
253			return '';
254		}
255	}
256
257	/**
258	 * Display descendants.
259	 *
260	 * @param Family $family
261	 * @param int    $generations
262	 *
263	 * @return string
264	 */
265	public function loadChildren(Family $family, $generations) {
266		$out = '';
267		if ($family->canShow()) {
268			$children = $family->getChildren();
269			if ($children) {
270				foreach ($children as $child) {
271					$out .= $this->getPersonLi($child, $generations - 1);
272				}
273			} else {
274				$out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>';
275			}
276		}
277		if ($out) {
278			return '<ul>' . $out . '</ul>';
279		} else {
280			return '';
281		}
282	}
283}
284