xref: /webtrees/app/Module/DescendancyModule.php (revision 9f2390a04226d0058d1862402c80d50fe6e79aa1)
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		return view('modules/descendancy', [
94			'individual_list' => $this->getPersonLi($individual, 1),
95		]);
96	}
97
98	/**
99	 * Format an individual in a list.
100	 *
101	 * @param Individual $person
102	 * @param int        $generations
103	 *
104	 * @return string
105	 */
106	public function getPersonLi(Individual $person, $generations = 0) {
107		$icon     = $generations > 0 ? 'icon-minus' : 'icon-plus';
108		$lifespan = $person->canShow() ? '(' . $person->getLifeSpan() . ')' : '';
109		$spouses  = $generations > 0 ? $this->loadSpouses($person, 0) : '';
110
111		return
112			'<li class="sb_desc_indi_li">' .
113			'<a class="sb_desc_indi" href="module.php?mod=' . $this->getName() . '&amp;mod_action=descendants&amp;xref=' . $person->getXref() . '">' .
114			'<i class="plusminus ' . $icon . '"></i>' .
115			$person->getSexImage() . $person->getFullName() . $lifespan .
116			'</a>' .
117			FontAwesome::linkIcon('individual', $person->getFullName(), ['href' => $person->url()]) .
118			'<div>' . $spouses . '</div>' .
119			'</li>';
120	}
121
122	/**
123	 * Format a family in a list.
124	 *
125	 * @param Family     $family
126	 * @param Individual $person
127	 * @param int        $generations
128	 *
129	 * @return string
130	 */
131	public function getFamilyLi(Family $family, Individual $person, $generations = 0) {
132		$spouse = $family->getSpouse($person);
133		if ($spouse) {
134			$spouse_name = $spouse->getSexImage() . $spouse->getFullName();
135			$spouse_link = FontAwesome::linkIcon('individual', $spouse->getFullName(), ['href' => $person->url()]);
136		} else {
137			$spouse_name = '';
138			$spouse_link = '';
139		}
140
141		$marryear = $family->getMarriageYear();
142		$marr     = $marryear ? '<i class="icon-rings"></i>' . $marryear : '';
143
144		return
145			'<li class="sb_desc_indi_li">' .
146			'<a class="sb_desc_indi" href="#"><i class="plusminus icon-minus"></i>' . $spouse_name . $marr . '</a>' .
147			$spouse_link .
148			FontAwesome::linkIcon('family', $family->getFullName(), ['href' => $family->url()]) .
149		 '<div>' . $this->loadChildren($family, $generations) . '</div>' .
150			'</li>';
151	}
152
153	/**
154	 * Respond to an autocomplete search request.
155	 *
156	 * @param string $query Search for this term
157	 * @param Tree   $tree  Search in this tree
158	 *
159	 * @return string
160	 */
161	public function search($query, Tree $tree) {
162		if (strlen($query) < 2) {
163			return '';
164		}
165
166		$rows = Database::prepare(
167			"SELECT i_id AS xref" .
168			" FROM `##individuals`" .
169			" JOIN `##name` ON i_id = n_id AND i_file = n_file" .
170			" WHERE n_sort LIKE CONCAT('%', :query, '%') AND i_file = :tree_id" .
171			" ORDER BY n_sort"
172		)->execute([
173			'query'   => $query,
174			'tree_id' => $tree->getTreeId(),
175		])->fetchAll();
176
177		$out = '';
178		foreach ($rows as $row) {
179			$person = Individual::getInstance($row->xref, $tree);
180			if ($person && $person->canShowName()) {
181				$out .= $this->getPersonLi($person);
182			}
183		}
184		if ($out) {
185			return '<ul>' . $out . '</ul>';
186		} else {
187			return '';
188		}
189	}
190
191	/**
192	 * Display spouses.
193	 *
194	 * @param Individual $person
195	 * @param int        $generations
196	 *
197	 * @return string
198	 */
199	public function loadSpouses(Individual $person, $generations) {
200		$out = '';
201		if ($person && $person->canShow()) {
202			foreach ($person->getSpouseFamilies() as $family) {
203				$out .= $this->getFamilyLi($family, $person, $generations - 1);
204			}
205		}
206		if ($out) {
207			return '<ul>' . $out . '</ul>';
208		} else {
209			return '';
210		}
211	}
212
213	/**
214	 * Display descendants.
215	 *
216	 * @param Family $family
217	 * @param int    $generations
218	 *
219	 * @return string
220	 */
221	public function loadChildren(Family $family, $generations) {
222		$out = '';
223		if ($family->canShow()) {
224			$children = $family->getChildren();
225			if ($children) {
226				foreach ($children as $child) {
227					$out .= $this->getPersonLi($child, $generations - 1);
228				}
229			} else {
230				$out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>';
231			}
232		}
233		if ($out) {
234			return '<ul>' . $out . '</ul>';
235		} else {
236			return '';
237		}
238	}
239}
240