summaryrefslogtreecommitdiff
path: root/app/Select2.php
blob: 30a32a2c1a126fe717590b28685eedaffcdab8cc (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
<?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;

use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

/**
 * Generate markup and AJAX responses for SELECT2 queries.
 *
 * @link https://select2.github.io/
 */
class Select2 extends Html {
	// Send this many results with each request.
	const RESULTS_PER_PAGE = 20;

	// Don't send queries with fewer than this many characters
	const MINIMUM_INPUT_LENGTH = '1';

	// Don't send queries until this many milliseconds.
	const DELAY = '350';

	// API endpoints
	const URL_FAM  = 'action.php?action=select2-family';
	const URL_INDI = 'action.php?action=select2-individual';
	const URL_NOTE = 'action.php?action=select2-note';
	const URL_OBJE = 'action.php?action=select2-media';
	const URL_PLAC = 'action.php?action=select2-place';
	const URL_REPO = 'action.php?action=select2-repository';
	const URL_SOUR = 'action.php?action=select2-source';
	const URL_SUBM = 'action.php?action=select2-submitter';
	const URL_FLAG = 'action.php?action=select2-flag';

	/**
	 * Select2 configuration that is common to all searches.
	 *
	 * @return string[]
	 */
	private static function commonConfig() {
		return [
			'autocomplete'                    => 'off',
			'class'                           => 'form-control select2',
			'data-ajax--delay'                => self::DELAY,
			'data-ajax--minimum-input-length' => self::MINIMUM_INPUT_LENGTH,
			'data-ajax--type'                 => 'POST',
			'data-allow-clear'                => 'true',
			'data-placeholder'                => '',
		];
	}

	/**
	 * Select2 configuration for a family lookup.
	 *
	 * @return string[]
	 */
	public static function familyConfig() {
		return self::commonConfig() + ['data-ajax--url' => self::URL_FAM];
	}

	/**
	 * Format a family name for display in a Select2 control.
	 *
	 * @param Family $family
	 *
	 * @return string
	 */
	public static function familyValue(Family $family) {
		return $family->getFullName();
	}

	/**
	 * Look up a family.
	 *
	 * @param Tree   $tree  Search this tree.
	 * @param int    $page  Skip this number of pages.  Starts with zero.
	 * @param string $query Search terms.
	 *
	 * @return mixed[]
	 */
	public static function familySearch(Tree $tree, $page, $query) {
		$offset  = $page * self::RESULTS_PER_PAGE;
		$more    = false;
		$results = [];

		$cursor = Database::prepare("SELECT DISTINCT 'FAM' AS type, f_id AS xref, f_gedcom AS gedcom, husb_name.n_sort, wife_name.n_sort" .
			" FROM `##families`" .
			" JOIN `##name` AS husb_name ON f_husb = husb_name.n_id AND f_file = husb_name.n_file" .
			" JOIN `##name` AS wife_name ON f_wife = wife_name.n_id AND f_file = wife_name.n_file" .
			" WHERE CONCAT(husb_name.n_full, ' ', wife_name.n_full) LIKE CONCAT('%', REPLACE(:query, ' ', '%'), '%') AND f_file = :tree_id" .
			" AND husb_name.n_type <> '_MARNM' AND wife_name.n_type <> '_MARNM'" .
			" ORDER BY husb_name.n_sort, wife_name.n_sort COLLATE :collation")->execute([
			'query'     => $query,
			'tree_id'   => $tree->getTreeId(),
			'collation' => I18N::collation(),
		]);

		while (is_object($row = $cursor->fetch())) {
			$family = Family::getInstance($row->xref, $tree, $row->gedcom);
			// Filter for privacy
			if ($family !== null && $family->canShowName()) {
				if ($offset > 0) {
					// Skip results
					$offset--;
				} elseif (count($results) === self::RESULTS_PER_PAGE) {
					// Stop when we have found a page of results
					$more = true;
					break;
				} else {
					// Add to the results
					$results[] = [
						'id'   => $row->xref,
						'text' => self::familyValue($family),
					];
				}
			}
		}
		$cursor->closeCursor();

		return [
			'results'    => $results,
			'pagination' => [
				'more' => $more,
			],
		];
	}

	/**
	 * Select2 configuration for a flag icon lookup.
	 *
	 * @return string[]
	 */
	public static function flagConfig() {
		return self::commonConfig() + ['data-ajax--url' => self::URL_FLAG];
	}

	/**
	 * Format a flag icon for display in a Select2 control.
	 *
	 * @param string $flag
	 *
	 * @return string
	 */
	public static function flagValue($flag) {
		return '<img src="' . WT_MODULES_DIR . 'googlemap/places/flags/' . $flag . '"> ' . $flag;
	}

	/**
	 * Look up a flag icon.
	 *
	 * @param int    $page  Skip this number of pages.  Starts with zero.
	 * @param string $query Search terms.
	 *
	 * @return mixed[]
	 */
	public static function flagSearch($page, $query) {
		$offset    = $page * self::RESULTS_PER_PAGE;
		$more      = false;
		$results   = [];
		$directory = WT_ROOT . WT_MODULES_DIR . 'googlemap/places/flags/';
		$di        = new RecursiveDirectoryIterator($directory);
		$it        = new RecursiveIteratorIterator($di);

		$flag_files = [];
		foreach ($it as $file) {
			$file_path = substr($file->getPathname() , strlen($directory));
			if ($file->getExtension() === 'png' && stripos($file_path, $query) !== false) {
				if ($offset > 0) {
					// Skip results
					$offset--;
				} elseif (count($flag_files) >= self::RESULTS_PER_PAGE) {
					$more = true;
					break;
				} else {
					$flag_files[] = $file_path;
				}
			}
		}

		foreach ($flag_files as $flag_file) {
			$results[] = [
				'id'   => $flag_file,
				'text' => self::flagValue($flag_file),
			];
		}

		return [
			'results'    => $results,
			'pagination' => [
				'more' => $more,
			],
		];
	}

	/**
	 * Select2 configuration for an individual lookup.
	 *
	 * @return string[]
	 */
	public static function individualConfig() {
		return self::commonConfig() + ['data-ajax--url' => self::URL_INDI];
	}

	/**
	 * Format an individual name for display in a Select2 control.
	 *
	 * @param Individual $individual
	 *
	 * @return string
	 */
	public static function individualValue(Individual $individual) {
		return $individual->getFullName() . ', ' . $individual->getLifeSpan();
	}

	/**
	 * Look up an individual.
	 *
	 * @param Tree   $tree  Search this tree.
	 * @param int    $page  Skip this number of pages.  Starts with zero.
	 * @param string $query Search terms.
	 *
	 * @return mixed[]
	 */
	public static function individualSearch(Tree $tree, $page, $query) {
		$offset  = $page * self::RESULTS_PER_PAGE;
		$more    = false;
		$results = [];
		$cursor  = Database::prepare("SELECT i_id AS xref, i_gedcom AS gedcom, n_full" . " FROM `##individuals`" . " JOIN `##name` ON i_id = n_id AND i_file = n_file" . " WHERE n_full LIKE CONCAT('%', REPLACE(:query, ' ', '%'), '%') AND i_file = :tree_id" . " ORDER BY n_full COLLATE :collation")->execute([
			'query'     => $query,
			'tree_id'   => $tree->getTreeId(),
			'collation' => I18N::collation(),
		]);

		while (is_object($row = $cursor->fetch())) {
			$individual = Individual::getInstance($row->xref, $tree, $row->gedcom);
			// Filter for privacy
			if ($individual !== null && $individual->canShowName()) {
				if ($offset > 0) {
					// Skip results
					$offset--;
				} elseif (count($results) === self::RESULTS_PER_PAGE) {
					// Stop when we have found a page of results
					$more = true;
					break;
				} else {
					// Add to the results
					$results[] = [
						'id'   => $row->xref,
						'text' => str_replace(['@N.N.', '@P.N.'], [I18N::translateContext('Unknown surname', '…'), I18N::translateContext('Unknown given name', '…')], $row->n_full) . ', ' . $individual->getLifeSpan(),
					];
				}
			}
		}
		$cursor->closeCursor();

		return [
			'results'    => $results,
			'pagination' => [
				'more' => $more,
			],
		];
	}

	/**
	 * Select2 configuration for a media object lookup.
	 *
	 * @return string[]
	 */
	public static function mediaObjectConfig() {
		return self::commonConfig() + ['data-ajax--url' => self::URL_OBJE];
	}

	/**
	 * Format a media object name for display in a Select2 control.
	 *
	 * @param Media $media
	 *
	 * @return string
	 */
	public static function mediaObjectValue(Media $media) {
		return $media->getFullName() . ', ' . basename($media->getFilename());
	}

	/**
	 * Look up a media object.
	 *
	 * @param Tree   $tree  Search this tree.
	 * @param int    $page  Skip this number of pages.  Starts with zero.
	 * @param string $query Search terms.
	 *
	 * @return mixed[]
	 */
	public static function mediaObjectSearch(Tree $tree, $page, $query) {
		$offset  = $page * self::RESULTS_PER_PAGE;
		$more    = false;
		$results = [];
		$cursor  = Database::prepare("SELECT m_id AS xref, m_gedcom AS gedcom, n_full" . " FROM `##media`" . " JOIN `##name` ON m_id = n_id AND m_file = n_file" . " WHERE n_full LIKE CONCAT('%', REPLACE(:query, ' ', '%'), '%') AND m_file = :tree_id" . " ORDER BY n_full COLLATE :collation")->execute([
			'query'     => $query,
			'tree_id'   => $tree->getTreeId(),
			'collation' => I18N::collation(),
		]);

		while (is_object($row = $cursor->fetch())) {
			$media = Media::getInstance($row->xref, $tree, $row->gedcom);
			// Filter for privacy
			if ($media !== null && $media->canShow()) {
				if ($offset > 0) {
					// Skip results
					$offset--;
				} elseif (count($results) === self::RESULTS_PER_PAGE) {
					// Stop when we have found a page of results
					$more = true;
					break;
				} else {
					// Add to the results
					$results[] = [
						'id'   => $row->xref,
						'text' => self::mediaObjectValue($media),
					];
				}
			}
		}
		$cursor->closeCursor();

		return [
			'results'    => $results,
			'pagination' => [
				'more' => $more,
			],
		];
	}

	/**
	 * Select2 configuration for a note.
	 *
	 * @return string[]
	 */
	public static function noteConfig() {
		return self::commonConfig() + ['data-ajax--url' => self::URL_NOTE];
	}

	/**
	 * Format a note name for display in a Select2 control.
	 *
	 * @param Note $note
	 *
	 * @return string
	 */
	public static function noteValue(Note $note) {
		return $note->getFullName();
	}

	/**
	 * Look up a note.
	 *
	 * @param Tree   $tree  Search this tree.
	 * @param int    $page  Skip this number of pages.  Starts with zero.
	 * @param string $query Search terms.
	 *
	 * @return mixed[]
	 */
	public static function noteSearch(Tree $tree, $page, $query) {
		$offset  = $page * self::RESULTS_PER_PAGE;
		$more    = false;
		$results = [];
		$cursor  = Database::prepare("SELECT o_id AS xref, o_gedcom AS gedcom, n_full" . " FROM `##other`" . " JOIN `##name` ON o_id = n_id AND o_file = n_file" . " WHERE n_full LIKE CONCAT('%', REPLACE(:query, ' ', '%'), '%') AND o_file = :tree_id AND o_type='NOTE'" . " ORDER BY n_full COLLATE :collation")->execute([
			'query'     => $query,
			'tree_id'   => $tree->getTreeId(),
			'collation' => I18N::collation(),
		]);

		while (is_object($row = $cursor->fetch())) {
			$note = Note::getInstance($row->xref, $tree, $row->gedcom);
			// Filter for privacy
			if ($note !== null && $note->canShowName()) {
				if ($offset > 0) {
					// Skip results
					$offset--;
				} elseif (count($results) === self::RESULTS_PER_PAGE) {
					// Stop when we have found a page of results
					$more = true;
					break;
				} else {
					// Add to the results
					$results[] = [
						'id'   => $row->xref,
						'text' => self::noteValue($note),
					];
				}
			}
		}
		$cursor->closeCursor();

		return [
			'results'    => $results,
			'pagination' => [
				'more' => $more,
			],
		];
	}

	/**
	 * Select2 configuration for a note.
	 *
	 * @return string[]
	 */
	public static function placeConfig() {
		return self::commonConfig() + ['data-ajax--url' => self::URL_PLAC];
	}

	/**
	 * Format a note name for display in a Select2 control.
	 *
	 * @param Note $note
	 *
	 * @return string
	 */
	public static function placeValue(Note $note) {
		return $note->getFullName();
	}

	/**
	 * Look up a note.
	 *
	 * @param Tree   $tree   Search this tree.
	 * @param int    $page   Skip this number of pages.  Starts with zero.
	 * @param string $query  Search terms.
	 * @param bool   $create if true, include the query in the results so it can be created.
	 *
	 * @return mixed[]
	 */
	public static function placeSearch(Tree $tree, $page, $query, $create) {
		$offset  = $page * self::RESULTS_PER_PAGE;
		$results = [];
		$found   = false;

		// Do not filter by privacy. Place names on their own do not identify individuals.
		foreach (Place::findPlaces($query, $tree) as $place) {
			$place_name = $place->getGedcomName();
			if ($place_name === $query) {
				$found = true;
			}
			$results[]  = [
				'id'   => $place_name,
				'text' => $place_name,
			];
		}

		// No place found? Use an external gazetteer
		if (empty($results) && $tree->getPreference('GEONAMES_ACCOUNT')) {
			$url =
				"http://api.geonames.org/searchJSON" .
				"?name_startsWith=" . urlencode($query) .
				"&lang=" . WT_LOCALE .
				"&fcode=CMTY&fcode=ADM4&fcode=PPL&fcode=PPLA&fcode=PPLC" .
				"&style=full" .
				"&username=" . $tree->getPreference('GEONAMES_ACCOUNT');
			// try to use curl when file_get_contents not allowed
			if (ini_get('allow_url_fopen')) {
				$json   = file_get_contents($url);
				$places = json_decode($json, true);
			} elseif (function_exists('curl_init')) {
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_URL, $url);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				$json   = curl_exec($ch);
				$places = json_decode($json, true);
				curl_close($ch);
			} else {
				$places = [];
			}
			if (isset($places['geonames']) && is_array($places['geonames'])) {
				foreach ($places['geonames'] as $k => $place) {
					$place_name = $place['name'] . ', ' . $place['adminName2'] . ', ' . $place['adminName1'] . ', ' . $place['countryName'];
					if ($place_name === $query) {
						$found = true;
					}
					$results[]  = [
						'id'   => $place_name,
						'text' => $place_name,
					];
				}
			}
		}

		// Include the query term in the results.  This allows the user to select a
		// place that doesn't already exist in the database.
		if (!$found && $create) {
			array_unshift($results, [
				'id'   => $query,
				'text' => $query,
			]);
		}

		$more    = count($results) > $offset + self::RESULTS_PER_PAGE;
		$results = array_slice($results, $offset, self::RESULTS_PER_PAGE);

		return [
			'results'    => $results,
			'pagination' => [
				'more' => $more,
			],
		];
	}

	/**
	 * Select2 configuration for a repository lookup.
	 *
	 * @return string[]
	 */
	public static function repositoryConfig() {
		return self::commonConfig() + ['data-ajax--url' => self::URL_REPO];
	}

	/**
	 * Format a repository name for display in a Select2 control.
	 *
	 * @param Repository $repository
	 *
	 * @return string
	 */
	public static function repositoryValue(Repository $repository) {
		return $repository->getFullName();
	}

	/**
	 * Look up a repository.
	 *
	 * @param Tree   $tree  Search this tree.
	 * @param int    $page  Skip this number of pages.  Starts with zero.
	 * @param string $query Search terms.
	 *
	 * @return mixed[]
	 */
	public static function repositorySearch(Tree $tree, $page, $query) {
		$offset  = $page * self::RESULTS_PER_PAGE;
		$more    = false;
		$results = [];
		$cursor  = Database::prepare("SELECT o_id AS xref, o_gedcom AS gedcom, n_full" . " FROM `##other`" . " JOIN `##name` ON o_id = n_id AND o_file = n_file" . " WHERE o_type = 'REPO' AND n_full LIKE CONCAT('%', REPLACE(:query, ' ', '%'), '%') AND o_file = :tree_id" . " ORDER BY n_full COLLATE :collation")->execute([
			'query'     => $query,
			'tree_id'   => $tree->getTreeId(),
			'collation' => I18N::collation(),
		]);

		while (is_object($row = $cursor->fetch())) {
			$repository = Repository::getInstance($row->xref, $tree, $row->gedcom);
			// Filter for privacy
			if ($repository !== null && $repository->canShow()) {
				if ($offset > 0) {
					// Skip results
					$offset--;
				} elseif (count($results) === self::RESULTS_PER_PAGE) {
					// Stop when we have found a page of results
					$more = true;
					break;
				} else {
					// Add to the results
					$results[] = [
						'id'   => $row->xref,
						'text' => self::repositoryValue($repository),
					];
				}
			}
		}
		$cursor->closeCursor();

		return [
			'results'    => $results,
			'pagination' => [
				'more' => $more,
			],
		];
	}

	/**
	 * Select2 configuration for a source lookup.
	 *
	 * @return string[]
	 */
	public static function sourceConfig() {
		return self::commonConfig() + ['data-ajax--url' => self::URL_SOUR];
	}

	/**
	 * Format a source name for display in a Select2 control.
	 *
	 * @param Source $source
	 *
	 * @return string
	 */
	public static function sourceValue(Source $source) {
		return $source->getFullName();
	}

	/**
	 * Look up a source.
	 *
	 * @param Tree   $tree  Search this tree.
	 * @param int    $page  Skip this number of pages.  Starts with zero.
	 * @param string $query Search terms.
	 *
	 * @return mixed[]
	 */
	public static function sourceSearch(Tree $tree, $page, $query) {
		$offset  = $page * self::RESULTS_PER_PAGE;
		$more    = false;
		$results = [];
		$cursor  = Database::prepare("SELECT s_id AS xref, s_gedcom AS gedcom, n_full" . " FROM `##sources`" . " JOIN `##name` ON s_id = n_id AND s_file = n_file" . " WHERE n_full LIKE CONCAT('%', REPLACE(:query, ' ', '%'), '%') AND s_file = :tree_id" . " ORDER BY n_full COLLATE :collation")->execute([
			'query'     => $query,
			'tree_id'   => $tree->getTreeId(),
			'collation' => I18N::collation(),
		]);

		while (is_object($row = $cursor->fetch())) {
			$source = Source::getInstance($row->xref, $tree, $row->gedcom);
			// Filter for privacy
			if ($source !== null && $source->canShow()) {
				if ($offset > 0) {
					// Skip results
					$offset--;
				} elseif (count($results) === self::RESULTS_PER_PAGE) {
					// Stop when we have found a page of results
					$more = true;
					break;
				} else {
					// Add to the results
					$results[] = [
						'id'   => $row->xref,
						'text' => self::sourceValue($source),
					];
				}
			}
		}
		$cursor->closeCursor();

		return [
			'results'    => $results,
			'pagination' => [
				'more' => $more,
			],
		];
	}

	/**
	 * Select2 configuration for a submitter lookup.
	 *
	 * @return string[]
	 */
	public static function submitterConfig() {
		return self::commonConfig() + ['data-ajax--url' => self::URL_SUBM];
	}

	/**
	 * Format a family name for display in a Select2 control.
	 *
	 * @param GedcomRecord $submitter
	 *
	 * @return string
	 */
	public static function submitterValue(GedcomRecord $submitter) {
		return $submitter->getFullName();
	}

	/**
	 * Look up a submitter.
	 *
	 * @param Tree   $tree  Search this tree.
	 * @param int    $page  Skip this number of pages.  Starts with zero.
	 * @param string $query Search terms.
	 *
	 * @return mixed[]
	 */
	public static function submitterSearch(Tree $tree, $page, $query) {
		$offset  = $page * self::RESULTS_PER_PAGE;
		$more    = false;
		$results = [];
		$cursor  = Database::prepare("SELECT i_id AS xref, i_gedcom AS gedcom, n_full" . " FROM `##individuals`" . " JOIN `##name` ON i_id = n_id AND i_file = n_file" . " WHERE n_full LIKE CONCAT('%', REPLACE(:query, ' ', '%'), '%') AND i_file = :tree_id" . " ORDER BY n_full COLLATE :collation")->execute([
			'query'     => $query,
			'tree_id'   => $tree->getTreeId(),
			'collation' => I18N::collation(),
		]);

		while (is_object($row = $cursor->fetch())) {
			$submitter = GedcomRecord::getInstance($row->xref, $tree, $row->gedcom);
			// Filter for privacy
			if ($submitter !== null && $submitter->canShow()) {
				if ($offset > 0) {
					// Skip results
					$offset--;
				} elseif (count($results) === self::RESULTS_PER_PAGE) {
					// Stop when we have found a page of results
					$more = true;
					break;
				} else {
					// Add to the results
					$results[] = [
						'id'   => $row->xref,
						'text' => self::submitterValue($submitter),
					];
				}
			}
		}
		$cursor->closeCursor();

		return [
			'results'    => $results,
			'pagination' => [
				'more' => $more,
			],
		];
	}
}