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