summaryrefslogtreecommitdiff
path: root/app/Module/FamiliesSidebarModule.php
blob: 119bc098555899c1d7b3b8415e21811fa8773c75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
/**
 * webtrees: online genealogy
 * Copyright (C) 2017 webtrees development team
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
namespace Fisharebest\Webtrees\Module;

use Fisharebest\Webtrees\Database;
use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\Filter;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Query\QueryName;
use Fisharebest\Webtrees\Theme;
use Fisharebest\Webtrees\Tree;

/**
 * Class FamiliesSidebarModule
 */
class FamiliesSidebarModule extends AbstractModule implements ModuleSidebarInterface {
	/** {@inheritdoc} */
	public function getTitle() {
		return /* I18N: Name of a module/sidebar */ I18N::translate('Family list');
	}

	/** {@inheritdoc} */
	public function getDescription() {
		return /* I18N: Description of the “Families” module */ I18N::translate('A sidebar showing an alphabetic list of all the families in the family tree.');
	}

	/**
	 * This is a general purpose hook, allowing modules to respond to routes
	 * of the form module.php?mod=FOO&mod_action=BAR
	 *
	 * @param string $mod_action
	 */
	public function modAction($mod_action) {
		switch ($mod_action) {
		case 'ajax':
			header('Content-Type: text/html; charset=UTF-8');
			echo $this->getSidebarAjaxContent();
			break;
		default:
			http_response_code(404);
			break;
		}
	}

	/** {@inheritdoc} */
	public function defaultSidebarOrder() {
		return 50;
	}

	/** {@inheritdoc} */
	public function hasSidebarContent() {
		return true;
	}

	/** {@inheritdoc} */
	public function getSidebarAjaxContent() {
		global $WT_TREE;

		$alpha   = Filter::get('alpha'); // All surnames beginning with this letter where "@"=unknown and ","=none
		$surname = Filter::get('surname'); // All indis with this surname.
		$search  = Filter::get('search');

		if ($search) {
			return $this->search($WT_TREE, $search);
		} elseif ($alpha == '@' || $alpha == ',' || $surname) {
			return $this->getSurnameFams($WT_TREE, $alpha, $surname);
		} elseif ($alpha) {
			return $this->getAlphaSurnames($WT_TREE, $alpha);
		} else {
			return '';
		}
	}

	/**
	 * Load this sidebar synchronously.
	 *
	 * @return string
	 */
	public function getSidebarContent() {
		global $controller, $WT_TREE;

		// Fetch a list of the initial letters of all surnames in the database
		$initials = QueryName::surnameAlpha($WT_TREE, true, false, false);

		$controller->addInlineJavascript('
			var famloadedNames = new Array();

			function fsearchQ() {
				var query = $("#sb_fam_name").val();
				if (query.length>1) {
					$("#sb_fam_content").load("module.php?mod=' . $this->getName() . '&mod_action=ajax&search="+query);
				}
			}

			var famtimerid = null;
			$("#sb_fam_name").keyup(function(e) {
				if (famtimerid) window.clearTimeout(famtimerid);
				famtimerid = window.setTimeout("fsearchQ()", 500);
			});
			$("#sb_content_families").on("click", ".sb_fam_letter", function() {
				$("#sb_fam_content").load(this.href);
				return false;
			});
			$("#sb_content_families").on("click", ".sb_fam_surname", function() {
				var element = $(this);
				var surname = element.data("surname");
				var alpha   = element.data("alpha");

				if (!famloadedNames[surname]) {
					jQuery.ajax({
					  url: "module.php?mod=' . $this->getName() . '&mod_action=ajax&alpha=" + encodeURIComponent(alpha) + "&surname=" + encodeURIComponent(surname),
					  cache: false,
					  success: function(html) {
					    $("div.name_tree_div", element.closest("li"))
					    .html(html)
					    .show("fast")
					    .css("list-style-image", "url(' . Theme::theme()->parameter('image-minus') . ')");
					    famloadedNames[surname]=2;
					  }
					});
				} else if (famloadedNames[surname]==1) {
					famloadedNames[surname]=2;
					$("div.name_tree_div", $(this).closest("li"))
					.show()
					.css("list-style-image", "url(' . Theme::theme()->parameter('image-minus') . ')");
				} else {
					famloadedNames[surname]=1;
					$("div.name_tree_div", $(this).closest("li"))
					.hide("fast")
					.css("list-style-image", "url(' . Theme::theme()->parameter('image-plus') . ')");
				}
				return false;
			});
		');

		$out = '<form method="post" action="module.php?mod=' . $this->getName() . '&amp;mod_action=ajax" onsubmit="return false;"><input type="search" name="sb_fam_name" id="sb_fam_name" placeholder="' . I18N::translate('Search') . '"><p>';
		foreach ($initials as $letter => $count) {
			switch ($letter) {
			case '@':
				$html = I18N::translateContext('Unknown surname', '…');
				break;
			case ',':
				$html = I18N::translate('None');
				break;
			case ' ':
				$html = '&nbsp;';
				break;
			default:
				$html = $letter;
				break;
			}
			$html = '<a href="module.php?mod=' . $this->getName() . '&amp;mod_action=ajax&amp;alpha=' . urlencode($letter) . '" class="sb_fam_letter">' . $html . '</a>';
			$out .= $html . ' ';
		}

		$out .= '</p>';
		$out .= '<div id="sb_fam_content">';
		$out .= '</div></form>';

		return $out;
	}

	/**
	 * Get a list of surname initials.
	 *
	 * @param Tree   $tree
	 * @param string $alpha
	 *
	 * @return string
	 */
	private function getAlphaSurnames(Tree $tree, $alpha) {
		$surnames = QueryName::surnames($tree, '', $alpha, true, true);
		$out      = '<ul>';
		foreach (array_keys($surnames) as $surname) {
			$out .= '<li class="sb_fam_surname_li"><a href="#" data-surname="' . Filter::escapeHtml($surname) . '" data-alpha="' . Filter::escapeHtml($alpha) . '" class="sb_fam_surname">' . Filter::escapeHtml($surname) . '</a>';
			$out .= '<div class="name_tree_div"></div>';
			$out .= '</li>';
		}
		$out .= '</ul>';

		return $out;
	}

	/**
	 * Get a list of surnames.
	 *
	 * @param Tree   $tree
	 * @param string $alpha
	 * @param string $surname
	 *
	 * @return string
	 */
	public function getSurnameFams(Tree $tree, $alpha, $surname) {
		$families = QueryName::families($tree, $surname, $alpha, '', true);
		$out      = '<ul>';
		foreach ($families as $family) {
			if ($family->canShowName()) {
				$out .= '<li><a href="' . $family->getHtmlUrl() . '">' . $family->getFullName() . ' ';
				if ($family->canShow()) {
					$marriage_year = $family->getMarriageYear();
					if ($marriage_year) {
						$out .= ' (' . $marriage_year . ')';
					}
				}
				$out .= '</a></li>';
			}
		}
		$out .= '</ul>';

		return $out;
	}

	/**
	 * Autocomplete search for families.
	 *
	 * @param Tree   $tree  Search this tree
	 * @param string $query Search for this text
	 *
	 * @return string
	 */
	private function search(Tree $tree, $query) {
		if (strlen($query) < 2) {
			return '';
		}
		$rows = Database::prepare(
			"SELECT i_id AS xref" .
			" FROM `##individuals`, `##name`" .
			" WHERE (i_id LIKE CONCAT('%', :query_1, '%') OR n_sort LIKE CONCAT('%', :query_2, '%'))" .
			" AND i_id = n_id AND i_file = n_file AND i_file = :tree_id" .
			" ORDER BY n_sort COLLATE :collation" .
			" LIMIT 50"
		)->execute([
			'query_1'   => $query,
			'query_2'   => $query,
			'tree_id'   => $tree->getTreeId(),
			'collation' => I18N::collation(),
		])->fetchAll();

		$ids = [];
		foreach ($rows as $row) {
			$ids[] = $row->xref;
		}

		$vars = [];
		if (empty($ids)) {
			//-- no match : search for FAM id
			$where  = "f_id LIKE CONCAT('%', ?, '%')";
			$vars[] = $query;
		} else {
			//-- search for spouses
			$qs    = implode(',', array_fill(0, count($ids), '?'));
			$where = "(f_husb IN ($qs) OR f_wife IN ($qs))";
			$vars  = array_merge($vars, $ids, $ids);
		}

		$vars[] = $tree->getTreeId();
		$rows   = Database::prepare("SELECT f_id AS xref, f_file AS gedcom_id, f_gedcom AS gedcom FROM `##families` WHERE {$where} AND f_file=?")
		->execute($vars)
		->fetchAll();

		$out = '<ul>';
		foreach ($rows as $row) {
			$family = Family::getInstance($row->xref, $tree, $row->gedcom);
			if ($family->canShowName()) {
				$out .= '<li><a href="' . $family->getHtmlUrl() . '">' . $family->getFullName() . ' ';
				if ($family->canShow()) {
					$marriage_year = $family->getMarriageYear();
					if ($marriage_year) {
						$out .= ' (' . $marriage_year . ')';
					}
				}
				$out .= '</a></li>';
			}
		}
		$out .= '</ul>';

		return $out;
	}
}