xref: /webtrees/app/Module/DescendancyModule.php (revision 8c2e82270a639a3acf607b432e54721116dae723)
1*8c2e8227SGreg Roach<?php
2*8c2e8227SGreg Roachnamespace Fisharebest\Webtrees;
3*8c2e8227SGreg Roach
4*8c2e8227SGreg Roach/**
5*8c2e8227SGreg Roach * webtrees: online genealogy
6*8c2e8227SGreg Roach * Copyright (C) 2015 webtrees development team
7*8c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
8*8c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
9*8c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
10*8c2e8227SGreg Roach * (at your option) any later version.
11*8c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
12*8c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*8c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*8c2e8227SGreg Roach * GNU General Public License for more details.
15*8c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
16*8c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
17*8c2e8227SGreg Roach */
18*8c2e8227SGreg Roach
19*8c2e8227SGreg Roachuse Zend_Session;
20*8c2e8227SGreg Roach
21*8c2e8227SGreg Roach/**
22*8c2e8227SGreg Roach * Class DescendancyModule
23*8c2e8227SGreg Roach */
24*8c2e8227SGreg Roachclass DescendancyModule extends Module implements ModuleSidebarInterface {
25*8c2e8227SGreg Roach	/** {@inheritdoc} */
26*8c2e8227SGreg Roach	public function getTitle() {
27*8c2e8227SGreg Roach		return /* I18N: Name of a module/sidebar */
28*8c2e8227SGreg Roach			I18N::translate('Descendants');
29*8c2e8227SGreg Roach	}
30*8c2e8227SGreg Roach
31*8c2e8227SGreg Roach	/** {@inheritdoc} */
32*8c2e8227SGreg Roach	public function getDescription() {
33*8c2e8227SGreg Roach		return /* I18N: Description of the “Descendants” module */
34*8c2e8227SGreg Roach			I18N::translate('A sidebar showing the descendants of an individual.');
35*8c2e8227SGreg Roach	}
36*8c2e8227SGreg Roach
37*8c2e8227SGreg Roach	/** {@inheritdoc} */
38*8c2e8227SGreg Roach	public function modAction($modAction) {
39*8c2e8227SGreg Roach		Zend_Session::writeClose();
40*8c2e8227SGreg Roach		header('Content-Type: text/html; charset=UTF-8');
41*8c2e8227SGreg Roach
42*8c2e8227SGreg Roach		switch ($modAction) {
43*8c2e8227SGreg Roach		case 'search':
44*8c2e8227SGreg Roach			$search = Filter::get('search');
45*8c2e8227SGreg Roach			echo $this->search($search);
46*8c2e8227SGreg Roach			break;
47*8c2e8227SGreg Roach		case 'descendants':
48*8c2e8227SGreg Roach			$individual = Individual::getInstance(Filter::get('xref', WT_REGEX_XREF));
49*8c2e8227SGreg Roach			if ($individual) {
50*8c2e8227SGreg Roach				echo $this->loadSpouses($individual, 1);
51*8c2e8227SGreg Roach			}
52*8c2e8227SGreg Roach			break;
53*8c2e8227SGreg Roach		default:
54*8c2e8227SGreg Roach			http_response_code(404);
55*8c2e8227SGreg Roach			break;
56*8c2e8227SGreg Roach		}
57*8c2e8227SGreg Roach	}
58*8c2e8227SGreg Roach
59*8c2e8227SGreg Roach	/** {@inheritdoc} */
60*8c2e8227SGreg Roach	public function defaultSidebarOrder() {
61*8c2e8227SGreg Roach		return 30;
62*8c2e8227SGreg Roach	}
63*8c2e8227SGreg Roach
64*8c2e8227SGreg Roach	/** {@inheritdoc} */
65*8c2e8227SGreg Roach	public function hasSidebarContent() {
66*8c2e8227SGreg Roach		return !Auth::isSearchEngine();
67*8c2e8227SGreg Roach	}
68*8c2e8227SGreg Roach
69*8c2e8227SGreg Roach	/** {@inheritdoc} */
70*8c2e8227SGreg Roach	public function getSidebarAjaxContent() {
71*8c2e8227SGreg Roach		return '';
72*8c2e8227SGreg Roach	}
73*8c2e8227SGreg Roach
74*8c2e8227SGreg Roach	/** {@inheritdoc} */
75*8c2e8227SGreg Roach	public function getSidebarContent() {
76*8c2e8227SGreg Roach		global $controller;
77*8c2e8227SGreg Roach
78*8c2e8227SGreg Roach		$controller->addInlineJavascript('
79*8c2e8227SGreg Roach			function dsearchQ() {
80*8c2e8227SGreg Roach				var query = jQuery("#sb_desc_name").val();
81*8c2e8227SGreg Roach				if (query.length>1) {
82*8c2e8227SGreg Roach					jQuery("#sb_desc_content").load("module.php?mod=' . $this->getName() . '&mod_action=search&search="+query);
83*8c2e8227SGreg Roach				}
84*8c2e8227SGreg Roach			}
85*8c2e8227SGreg Roach
86*8c2e8227SGreg Roach			jQuery("#sb_desc_name").focus(function(){this.select();});
87*8c2e8227SGreg Roach			jQuery("#sb_desc_name").blur(function(){if (this.value=="") this.value="' . I18N::translate('Search') . '";});
88*8c2e8227SGreg Roach			var dtimerid = null;
89*8c2e8227SGreg Roach			jQuery("#sb_desc_name").keyup(function(e) {
90*8c2e8227SGreg Roach				if (dtimerid) window.clearTimeout(dtimerid);
91*8c2e8227SGreg Roach				dtimerid = window.setTimeout("dsearchQ()", 500);
92*8c2e8227SGreg Roach			});
93*8c2e8227SGreg Roach
94*8c2e8227SGreg Roach			jQuery("#sb_desc_content").on("click", ".sb_desc_indi", function() {
95*8c2e8227SGreg Roach				var self = jQuery(this),
96*8c2e8227SGreg Roach					state = self.children(".plusminus"),
97*8c2e8227SGreg Roach					target = self.siblings("div");
98*8c2e8227SGreg Roach				if(state.hasClass("icon-plus")) {
99*8c2e8227SGreg Roach					if (jQuery.trim(target.html())) {
100*8c2e8227SGreg Roach						target.show("fast"); // already got content so just show it
101*8c2e8227SGreg Roach					} else {
102*8c2e8227SGreg Roach						target
103*8c2e8227SGreg Roach							.hide()
104*8c2e8227SGreg Roach							.load(self.attr("href"), function(response, status, xhr) {
105*8c2e8227SGreg Roach								if(status == "success" && response !== "") {
106*8c2e8227SGreg Roach									target.show("fast");
107*8c2e8227SGreg Roach								}
108*8c2e8227SGreg Roach							})
109*8c2e8227SGreg Roach					}
110*8c2e8227SGreg Roach				} else {
111*8c2e8227SGreg Roach					target.hide("fast");
112*8c2e8227SGreg Roach				}
113*8c2e8227SGreg Roach				state.toggleClass("icon-minus icon-plus");
114*8c2e8227SGreg Roach				return false;
115*8c2e8227SGreg Roach			});
116*8c2e8227SGreg Roach		');
117*8c2e8227SGreg Roach
118*8c2e8227SGreg Roach		return
119*8c2e8227SGreg Roach			'<form method="post" action="module.php?mod=' . $this->getName() . '&amp;mod_action=search" onsubmit="return false;">' .
120*8c2e8227SGreg Roach			'<input type="search" name="sb_desc_name" id="sb_desc_name" placeholder="' . I18N::translate('Search') . '">' .
121*8c2e8227SGreg Roach			'</form>' .
122*8c2e8227SGreg Roach			'<div id="sb_desc_content">' .
123*8c2e8227SGreg Roach			'<ul>' . $this->getPersonLi($controller->record, 1) . '</ul>' .
124*8c2e8227SGreg Roach			'</div>';
125*8c2e8227SGreg Roach	}
126*8c2e8227SGreg Roach
127*8c2e8227SGreg Roach	/**
128*8c2e8227SGreg Roach	 * @param Individual $person
129*8c2e8227SGreg Roach	 * @param integer       $generations
130*8c2e8227SGreg Roach	 *
131*8c2e8227SGreg Roach	 * @return string
132*8c2e8227SGreg Roach	 */
133*8c2e8227SGreg Roach	public function getPersonLi(Individual $person, $generations = 0) {
134*8c2e8227SGreg Roach		$icon = $generations > 0 ? 'icon-minus' : 'icon-plus';
135*8c2e8227SGreg Roach		$lifespan = $person->canShow() ? '(' . $person->getLifeSpan() . ')' : '';
136*8c2e8227SGreg Roach		$spouses = $generations > 0 ? $this->loadSpouses($person, 0) : '';
137*8c2e8227SGreg Roach		return sprintf('<li class="sb_desc_indi_li">
138*8c2e8227SGreg Roach		                  <a class="sb_desc_indi" href="module.php?mod=%s&amp;mod_action=descendants&amp;xref=%s"><i class="plusminus %s"></i>%s %s %s</a>
139*8c2e8227SGreg Roach		                  <a class="icon-button_indi" href="%s"></a>
140*8c2e8227SGreg Roach		                  %s
141*8c2e8227SGreg Roach		                  <div>%s</div>
142*8c2e8227SGreg Roach		                </li>', $this->getName(), $person->getXref(), $icon, $person->getSexImage(), $person->getFullName(), $lifespan, $person->getHtmlUrl(), '', $spouses);
143*8c2e8227SGreg Roach	}
144*8c2e8227SGreg Roach
145*8c2e8227SGreg Roach	/**
146*8c2e8227SGreg Roach	 * @param Family     $family
147*8c2e8227SGreg Roach	 * @param Individual $person
148*8c2e8227SGreg Roach	 * @param integer       $generations
149*8c2e8227SGreg Roach	 *
150*8c2e8227SGreg Roach	 * @return string
151*8c2e8227SGreg Roach	 */
152*8c2e8227SGreg Roach	public function getFamilyLi(Family $family, Individual $person, $generations = 0) {
153*8c2e8227SGreg Roach		$marryear = $family->getMarriageYear();
154*8c2e8227SGreg Roach		$marr = $marryear ? '<i class="icon-rings"></i>' . $marryear : '';
155*8c2e8227SGreg Roach		$fam = '<a href="' . $family->getHtmlUrl() . '" class="icon-button_family"></a>';
156*8c2e8227SGreg Roach		$kids = $this->loadChildren($family, $generations);
157*8c2e8227SGreg Roach		return sprintf('<li class="sb_desc_indi_li">
158*8c2e8227SGreg Roach		                  <a class="sb_desc_indi" href="#"><i class="plusminus icon-minus"></i>%s %s %s</a>
159*8c2e8227SGreg Roach		                  <a class="icon-button_indi" href="%s"></a>
160*8c2e8227SGreg Roach		                  %s
161*8c2e8227SGreg Roach		                  <div>%s</div>
162*8c2e8227SGreg Roach		                </li>', $person->getSexImage(), $person->getFullName(), $marr, $person->getHtmlUrl(), $fam, $kids);
163*8c2e8227SGreg Roach	}
164*8c2e8227SGreg Roach
165*8c2e8227SGreg Roach	/**
166*8c2e8227SGreg Roach	 * @param string $query
167*8c2e8227SGreg Roach	 *
168*8c2e8227SGreg Roach	 * @return string
169*8c2e8227SGreg Roach	 */
170*8c2e8227SGreg Roach	public function search($query) {
171*8c2e8227SGreg Roach		if (strlen($query) < 2) {
172*8c2e8227SGreg Roach			return '';
173*8c2e8227SGreg Roach		}
174*8c2e8227SGreg Roach		$rows = Database::prepare(
175*8c2e8227SGreg Roach			"SELECT i_id AS xref" .
176*8c2e8227SGreg Roach			" FROM `##individuals`, `##name`" .
177*8c2e8227SGreg Roach			" WHERE (i_id LIKE ? OR n_sort LIKE ?)" .
178*8c2e8227SGreg Roach			" AND i_id=n_id AND i_file=n_file AND i_file=?" .
179*8c2e8227SGreg Roach			" ORDER BY n_sort"
180*8c2e8227SGreg Roach		)
181*8c2e8227SGreg Roach			->execute(array("%{$query}%", "%{$query}%", WT_GED_ID))
182*8c2e8227SGreg Roach			->fetchAll();
183*8c2e8227SGreg Roach
184*8c2e8227SGreg Roach		$out = '';
185*8c2e8227SGreg Roach		foreach ($rows as $row) {
186*8c2e8227SGreg Roach			$person = Individual::getInstance($row->xref);
187*8c2e8227SGreg Roach			if ($person->canShowName()) {
188*8c2e8227SGreg Roach				$out .= $this->getPersonLi($person);
189*8c2e8227SGreg Roach			}
190*8c2e8227SGreg Roach		}
191*8c2e8227SGreg Roach		if ($out) {
192*8c2e8227SGreg Roach			return '<ul>' . $out . '</ul>';
193*8c2e8227SGreg Roach		} else {
194*8c2e8227SGreg Roach			return '';
195*8c2e8227SGreg Roach		}
196*8c2e8227SGreg Roach	}
197*8c2e8227SGreg Roach
198*8c2e8227SGreg Roach	/**
199*8c2e8227SGreg Roach	 * @param Individual $person
200*8c2e8227SGreg Roach	 * @param integer       $generations
201*8c2e8227SGreg Roach	 *
202*8c2e8227SGreg Roach	 * @return string
203*8c2e8227SGreg Roach	 */
204*8c2e8227SGreg Roach	public function loadSpouses(Individual $person, $generations) {
205*8c2e8227SGreg Roach		$out = '';
206*8c2e8227SGreg Roach		if ($person && $person->canShow()) {
207*8c2e8227SGreg Roach			foreach ($person->getSpouseFamilies() as $family) {
208*8c2e8227SGreg Roach				$spouse = $family->getSpouse($person);
209*8c2e8227SGreg Roach				if ($spouse) {
210*8c2e8227SGreg Roach					$out .= $this->getFamilyLi($family, $spouse, $generations - 1);
211*8c2e8227SGreg Roach				}
212*8c2e8227SGreg Roach			}
213*8c2e8227SGreg Roach			if (!$out) {
214*8c2e8227SGreg Roach				$out = '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>';
215*8c2e8227SGreg Roach			}
216*8c2e8227SGreg Roach		}
217*8c2e8227SGreg Roach		if ($out) {
218*8c2e8227SGreg Roach			return '<ul>' . $out . '</ul>';
219*8c2e8227SGreg Roach		} else {
220*8c2e8227SGreg Roach			return '';
221*8c2e8227SGreg Roach		}
222*8c2e8227SGreg Roach	}
223*8c2e8227SGreg Roach
224*8c2e8227SGreg Roach	/**
225*8c2e8227SGreg Roach	 * @param Family $family
226*8c2e8227SGreg Roach	 * @param integer   $generations
227*8c2e8227SGreg Roach	 *
228*8c2e8227SGreg Roach	 * @return string
229*8c2e8227SGreg Roach	 */
230*8c2e8227SGreg Roach	public function loadChildren(Family $family, $generations) {
231*8c2e8227SGreg Roach		$out = '';
232*8c2e8227SGreg Roach		if ($family->canShow()) {
233*8c2e8227SGreg Roach			$children = $family->getChildren();
234*8c2e8227SGreg Roach			if ($children) {
235*8c2e8227SGreg Roach				foreach ($children as $child) {
236*8c2e8227SGreg Roach					$out .= $this->getPersonLi($child, $generations - 1);
237*8c2e8227SGreg Roach				}
238*8c2e8227SGreg Roach			} else {
239*8c2e8227SGreg Roach				$out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>';
240*8c2e8227SGreg Roach			}
241*8c2e8227SGreg Roach		}
242*8c2e8227SGreg Roach		if ($out) {
243*8c2e8227SGreg Roach			return '<ul>' . $out . '</ul>';
244*8c2e8227SGreg Roach		} else {
245*8c2e8227SGreg Roach			return '';
246*8c2e8227SGreg Roach		}
247*8c2e8227SGreg Roach	}
248*8c2e8227SGreg Roach}
249