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
|
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2019 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/>.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
/**
* Generate markup and AJAX responses for SELECT2 queries.
*
* Note that the single space in the title attributes is necessary to prevent
* select2 from copying the (raw HTML) of the value into the title.
*
* @link https://stackoverflow.com/questions/35500508/how-to-disable-the-title-in-select2
*
* @link https://select2.github.io/
*/
class Select2 extends Html
{
// Send this many results with each request.
public const RESULTS_PER_PAGE = 20;
// Don't send queries with fewer than this many characters
private const MINIMUM_INPUT_LENGTH = '1';
// Don't send queries until this many milliseconds.
private const DELAY = '350';
/**
* Select2 configuration that is common to all searches.
*
* @return string[]
*/
private static function commonConfig(): array
{
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.
*
* @param Tree $tree
*
* @return string[]
*/
public static function familyConfig(Tree $tree): array
{
$url = route('select2-family', ['ged' => $tree->name()]);
return self::commonConfig() + ['data-ajax--url' => $url];
}
/**
* Select2 configuration for a flag icon lookup.
*
* @return string[]
*/
public static function flagConfig(): array
{
return self::commonConfig() + ['data-ajax--url' => route('select2-flag')];
}
/**
* Format a flag icon for display in a Select2 control.
*
* @param string $flag
*
* @return string
*/
public static function flagValue($flag): string
{
return '<img src="' . Webtrees::MODULES_PATH . '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(int $page, string $query): array
{
$offset = $page * self::RESULTS_PER_PAGE;
$more = false;
$results = [];
$directory = WT_ROOT . Webtrees::MODULES_PATH . '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),
'title' => ' ',
];
}
return [
'results' => $results,
'pagination' => [
'more' => $more,
],
];
}
/**
* Select2 configuration for an individual lookup.
*
* @param Tree $tree
*
* @return string[]
*/
public static function individualConfig(Tree $tree): array
{
$url = route('select2-individual', ['ged' => $tree->name()]);
return self::commonConfig() + ['data-ajax--url' => $url];
}
/**
* Select2 configuration for a media object lookup.
*
* @param Tree $tree
*
* @return string[]
*/
public static function mediaObjectConfig(Tree $tree): array
{
$url = route('select2-media', ['ged' => $tree->name()]);
return self::commonConfig() + ['data-ajax--url' => $url];
}
/**
* Select2 configuration for a note.
*
* @param Tree $tree
*
* @return string[]
*/
public static function noteConfig(Tree $tree): array
{
$url = route('select2-note', ['ged' => $tree->name()]);
return self::commonConfig() + ['data-ajax--url' => $url];
}
/**
* Select2 configuration for a repository lookup.
*
* @param Tree $tree
*
* @return string[]
*/
public static function repositoryConfig(Tree $tree): array
{
$url = route('select2-repository', ['ged' => $tree->name()]);
return self::commonConfig() + ['data-ajax--url' => $url];
}
/**
* Select2 configuration for a source lookup.
*
* @param Tree $tree
*
* @return string[]
*/
public static function sourceConfig(Tree $tree): array
{
$url = route('select2-source', ['ged' => $tree->name()]);
return self::commonConfig() + ['data-ajax--url' => $url];
}
/**
* Select2 configuration for a submitter lookup.
*
* @param Tree $tree
*
* @return string[]
*/
public static function submitterConfig(Tree $tree): array
{
$url = route('select2-submitter', ['ged' => $tree->name()]);
return self::commonConfig() + ['data-ajax--url' => $url];
}
}
|