-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax_combo.php
324 lines (288 loc) · 11.2 KB
/
ajax_combo.php
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
<?php
// This script and data application were generated by AppGini 5.82
// Download AppGini for free from https://bigprof.com/appgini/download/
/*
ajax-callable script that returns code for either a combo drop-down or an auto-complete
drop-down, based on number of items.
REQUEST parameters:
===============
t: table name
f: lookup field name
id: selected id
p: page number (default = 1)
s: search term
o: 0 (default) for text-only or 1 for full options list
text: selected text
filterer_[filterer]: name of filterer field to be used to filter the drop-down contents
must be one of the filteres defined for the concerned field
*/
$start_ts = microtime(true);
// how many results to return per call, in case of json output
$results_per_page = 50;
$curr_dir = dirname(__FILE__);
include("$curr_dir/defaultLang.php");
include("$curr_dir/language.php");
include("$curr_dir/lib.php");
handle_maintenance();
// drop-downs config
$lookups = array(
'depots' => array(
'sur_le_site' => array(
'parent_table' => 'site_compostage',
'parent_pk_field' => 'id',
'parent_caption' => 'IF(CHAR_LENGTH(`site_compostage`.`nom_du_site`), CONCAT_WS(\'\', `site_compostage`.`nom_du_site`, \' \'), \'\')',
'parent_from' => '`site_compostage` ',
'filterers' => array(),
'custom_query' => '',
'inherit_permissions' => false,
'list_type' => 0,
'not_null' => false
),
'par_le_membre' => array(
'parent_table' => 'membres',
'parent_pk_field' => 'id',
'parent_caption' => 'IF(CHAR_LENGTH(`membres`.`prenom`) || CHAR_LENGTH(`membres`.`Nom`), CONCAT_WS(\'\', `membres`.`prenom`, `membres`.`Nom`), \'\')',
'parent_from' => '`membres` ',
'filterers' => array(),
'custom_query' => '',
'inherit_permissions' => false,
'list_type' => 0,
'not_null' => false
),
'dans_le_bac' => array(
'parent_table' => 'bacs_du_site',
'parent_pk_field' => 'id',
'parent_caption' => 'IF(CHAR_LENGTH(`bacs_du_site`.`type_de_bacs`) || CHAR_LENGTH(`bacs_du_site`.`appartient_site`), CONCAT_WS(\'\', `bacs_du_site`.`type_de_bacs`, \' du site \', IF( CHAR_LENGTH(`site_compostage1`.`nom_du_site`), CONCAT_WS(\'\', `site_compostage1`.`nom_du_site`), \'\')), \'\')',
'parent_from' => '`bacs_du_site` LEFT JOIN `site_compostage` as site_compostage1 ON `site_compostage1`.`id`=`bacs_du_site`.`appartient_site` ',
'filterers' => array('sur_le_site' => 'appartient_site'),
'custom_query' => '',
'inherit_permissions' => false,
'list_type' => 0,
'not_null' => false
),
),
'site_compostage' => array(
),
'bacs_du_site' => array(
'appartient_site' => array(
'parent_table' => 'site_compostage',
'parent_pk_field' => 'id',
'parent_caption' => '`site_compostage`.`nom_du_site`',
'parent_from' => '`site_compostage` ',
'filterers' => array(),
'custom_query' => '',
'inherit_permissions' => false,
'list_type' => 0,
'not_null' => false
),
),
'membres' => array(
),
'transferts' => array(
'sur_le_site' => array(
'parent_table' => 'site_compostage',
'parent_pk_field' => 'id',
'parent_caption' => '`site_compostage`.`nom_du_site`',
'parent_from' => '`site_compostage` ',
'filterers' => array(),
'custom_query' => '',
'inherit_permissions' => false,
'list_type' => 0,
'not_null' => false
),
'par_le_membre' => array(
'parent_table' => 'membres',
'parent_pk_field' => 'id',
'parent_caption' => 'IF(CHAR_LENGTH(`membres`.`Nom`) || CHAR_LENGTH(`membres`.`prenom`), CONCAT_WS(\'\', `membres`.`Nom`, `membres`.`prenom`), \'\')',
'parent_from' => '`membres` ',
'filterers' => array(),
'custom_query' => '',
'inherit_permissions' => false,
'list_type' => 0,
'not_null' => false
),
),
'intervention' => array(
'demande_par' => array(
'parent_table' => 'membres',
'parent_pk_field' => 'id',
'parent_caption' => 'IF(CHAR_LENGTH(`membres`.`Nom`) || CHAR_LENGTH(`membres`.`prenom`), CONCAT_WS(\'\', `membres`.`Nom`, `membres`.`prenom`), \'\')',
'parent_from' => '`membres` ',
'filterers' => array(),
'custom_query' => '',
'inherit_permissions' => false,
'list_type' => 0,
'not_null' => false
),
'realise_par' => array(
'parent_table' => 'membres',
'parent_pk_field' => 'id',
'parent_caption' => 'IF(CHAR_LENGTH(`membres`.`Nom`) || CHAR_LENGTH(`membres`.`prenom`), CONCAT_WS(\'\', `membres`.`Nom`, `membres`.`prenom`), \'\')',
'parent_from' => '`membres` ',
'filterers' => array(),
'custom_query' => '',
'inherit_permissions' => false,
'list_type' => 0,
'not_null' => false
),
'pour_site' => array(
'parent_table' => 'site_compostage',
'parent_pk_field' => 'id',
'parent_caption' => '`site_compostage`.`nom_du_site`',
'parent_from' => '`site_compostage` ',
'filterers' => array(),
'custom_query' => '',
'inherit_permissions' => false,
'list_type' => 0,
'not_null' => false
),
'pour_bac' => array(
'parent_table' => 'bacs_du_site',
'parent_pk_field' => 'id',
'parent_caption' => 'IF(CHAR_LENGTH(`bacs_du_site`.`type_de_bacs`) || CHAR_LENGTH(`bacs_du_site`.`appartient_site`), CONCAT_WS(\'\', `bacs_du_site`.`type_de_bacs`, \' du site \', IF( CHAR_LENGTH(`site_compostage1`.`nom_du_site`), CONCAT_WS(\'\', `site_compostage1`.`nom_du_site`), \'\')), \'\')',
'parent_from' => '`bacs_du_site` LEFT JOIN `site_compostage` as site_compostage1 ON `site_compostage1`.`id`=`bacs_du_site`.`appartient_site` ',
'filterers' => array('pour_site' => 'appartient_site'),
'custom_query' => '',
'inherit_permissions' => false,
'list_type' => 0,
'not_null' => false
),
),
);
// XSS prevention
$xss = new CI_Input();
$xss->charset = datalist_db_encoding;
// receive and verify user input
$table_name = $_REQUEST['t'];
$field_name = $_REQUEST['f'];
$search_id = makeSafe(from_utf8($_REQUEST['id']));
$selected_text = from_utf8($_REQUEST['text']);
$returnOptions = ($_REQUEST['o'] == 1 ? true : false);
$page = intval($_REQUEST['p']);
if($page < 1) $page = 1;
$skip = $results_per_page * ($page - 1);
$search_term = makeSafe(from_utf8($_REQUEST['s']));
if(!isset($lookups[$table_name][$field_name])) die('{ "error": "Invalid table or field." }');
// can user access the requested table?
$perm = getTablePermissions($table_name);
if(!$perm[0] && !$search_id) die('{ "error": "' . addslashes($Translation['tableAccessDenied']) . '" }');
$field = $lookups[$table_name][$field_name];
$wheres = array();
// search term provided?
if($search_term) {
$wheres[] = "{$field['parent_caption']} like '%{$search_term}%'";
}
// any filterers specified?
if(is_array($field['filterers'])) {
foreach($field['filterers'] as $filterer => $filterer_parent) {
$get = (isset($_REQUEST["filterer_{$filterer}"]) ? $_REQUEST["filterer_{$filterer}"] : false);
if($get) {
$wheres[] = "`{$field['parent_table']}`.`$filterer_parent`='" . makeSafe($get) . "'";
}
}
}
// inherit permissions?
if($field['inherit_permissions']) {
$inherit = permissions_sql($field['parent_table']);
if($inherit === false && !$search_id) die($Translation['tableAccessDenied']);
if($inherit['where']) $wheres[] = $inherit['where'];
if($inherit['from']) $field['parent_from'] .= ", {$inherit['from']}";
}
// single value?
if($field['list_type'] != 2 && $search_id) {
$wheres[] = "`{$field['parent_table']}`.`{$field['parent_pk_field']}`='{$search_id}'";
}
if(count($wheres)) {
$where = 'WHERE ' . implode(' AND ', $wheres);
}
// define the combo and return the code
$combo = new DataCombo;
if($field['custom_query']) {
$qm = array(); $custom_where = ''; $custom_order_by = '2';
$combo->Query = $field['custom_query'];
if(preg_match('/ order by (.*)$/i', $combo->Query, $qm)) {
$custom_order_by = $qm[1];
$combo->Query = preg_replace('/ order by .*$/i', '', $combo->Query);
}
if(preg_match('/ where (.*)$/i', $combo->Query, $qm)) {
$custom_where = $qm[1];
$combo->Query = preg_replace('/ where .*$/i', '', $combo->Query);
}
if($where && $custom_where) {
$combo->Query .= " {$where} AND ({$custom_where}) ORDER BY {$custom_order_by}";
}elseif($custom_where) {
$combo->Query .= " WHERE {$custom_where} ORDER BY {$custom_order_by}";
}else{
$combo->Query .= " {$where} ORDER BY {$custom_order_by}";
}
$query_match = array();
preg_match('/select (.*) from (.*)$/i', $combo->Query, $query_match);
if(isset($query_match[2])) {
$count_query = "SELECT count(1) FROM {$query_match[2]}";
}else{
$count_query = '';
}
}else{
$combo->Query = "SELECT " . ($field['inherit_permissions'] ? 'DISTINCT ' : '') . "`{$field['parent_table']}`.`{$field['parent_pk_field']}`, {$field['parent_caption']} FROM {$field['parent_from']} {$where} ORDER BY 2";
$count_query = "SELECT count(1) FROM {$field['parent_from']} {$where}";
}
$combo->table = $table_name;
$combo->parent_table = $field['parent_table'];
$combo->SelectName = $field_name;
$combo->ListType = $field['list_type'];
if($search_id) {
$combo->SelectedData = $search_id;
}elseif($selected_text) {
$combo->SelectedData = getValueGivenCaption($combo->Query, $selected_text);
}
if($field['list_type'] == 2) {
$combo->Render();
$combo->HTML = str_replace('<select ', '<select onchange="' . $field_name . '_changed();" ', $combo->HTML);
// return response
if($returnOptions) {
?><span id="<?php echo $field_name; ?>-combo-list"><?php echo $combo->HTML; ?></span><?php
}else{
?>
<span id="<?php echo $field_name; ?>-match-text"><?php echo $combo->MatchText; ?></span>
<input type="hidden" id="<?php echo $field_name; ?>" value="<?php echo html_attr($combo->SelectedData); ?>" />
<?php
}
}else{
/* return json */
header('Content-type: application/json');
if(!preg_match('/ limit .+/i', $combo->Query)) {
if(!$search_id) $combo->Query .= " LIMIT {$skip}, {$results_per_page}";
if($search_id) $combo->Query .= " LIMIT 1";
}
$prepared_data = array();
// specific caption provided and list_type is not radio?
if(!$search_id && $selected_text) {
$search_id = getValueGivenCaption($combo->Query, $selected_text);
if($search_id) $prepared_data[] = array('id' => to_utf8($search_id), 'text' => to_utf8($xss->xss_clean($selected_text)));
}else{
/*
* in case we have a search term, show matches starting with it first,
* followed by those containing it.
* build a UNION query for that purpose
*/
if($search_term) {
$query2 = $combo->Query;
$query1 = str_replace(" like '%{$search_term}%'", " like '{$search_term}%'", $query2);
$combo->Query = "({$query1}) UNION ({$query2})";
}
$eo = array('silentErrors' => true);
$res = sql($combo->Query, $eo);
while($row = db_fetch_row($res)) {
if(empty($prepared_data) && $page == 1 && !$search_id && !$field['not_null']) {
$prepared_data[] = array('id' => empty_lookup_value, 'text' => to_utf8("<{$Translation['none']}>"));
}
$prepared_data[] = array('id' => to_utf8($row[0]), 'text' => to_utf8($xss->xss_clean($row[1])));
}
}
if(empty($prepared_data)) { $prepared_data[] = array('id' => '', 'text' => to_utf8($Translation['No matches found!'])); }
echo json_encode(array(
'results' => $prepared_data,
'more' => (@db_num_rows($res) >= $results_per_page),
'elapsed' => round(microtime(true) - $start_ts, 3)
));
}