/
ajax.lib.php
326 lines (301 loc) · 10.6 KB
/
ajax.lib.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
324
325
326
<?php
/* Copyright (C) 2007-2010 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2007-2012 Regis Houssin <regis@dolibarr.fr>
*
* 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 2 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/>.
* or see http://www.gnu.org/
*/
/**
* \file htdocs/core/lib/ajax.lib.php
* \brief Page called by Ajax request for produts
*/
/**
* Get value of an HTML field, do Ajax process and show result
*
* @param string $selected Preselecte value
* @param string $htmlname HTML name of input field
* @param string $url Url for request: /chemin/fichier.php
* @param string $option More parameters on URL request
* @param int $minLength Minimum number of chars to trigger that Ajax search
* @param int $autoselect Automatic selection if just one value
* @return string Script
*/
function ajax_autocompleter($selected,$htmlname,$url,$option='',$minLength=2,$autoselect=0)
{
if (empty($minLength)) $minLength=1;
$script = '<input type="hidden" name="'.$htmlname.'" id="'.$htmlname.'" value="'.$selected.'" />';
$script.= '<script type="text/javascript">';
$script.= 'jQuery(document).ready(function() {
var autoselect = '.$autoselect.';
jQuery("input#search_'.$htmlname.'").blur(function() {
//console.log(this.value.length);
if (this.value.length == 0)
{
jQuery("#search_'.$htmlname.'").val("");
jQuery("#'.$htmlname.'").val("");
}
});
jQuery("input#search_'.$htmlname.'").autocomplete({
source: function( request, response ) {
jQuery.get("'.$url.($option?'?'.$option:'').'", { '.$htmlname.': request.term }, function(data){
response( jQuery.map( data, function( item ) {
if (autoselect == 1 && data.length == 1) {
jQuery("#search_'.$htmlname.'").val(item.value);
jQuery("#'.$htmlname.'").val(item.key);
}
var label = item.label.toString();
return { label: label, value: item.value, id: item.key}
}));
}, "json");
},
dataType: "json",
minLength: '.$minLength.',
select: function( event, ui ) {
jQuery("#'.$htmlname.'").val(ui.item.id);
}
}).data( "autocomplete" )._renderItem = function( ul, item ) {
return jQuery( "<li></li>" )
.data( "item.autocomplete", item )
.append( \'<a href="#"><span class="tag">\' + item.label + "</span></a>" )
.appendTo(ul);
};
});';
$script.= '</script>';
return $script;
}
/**
* Get value of field, do Ajax process and return result
*
* @param string $htmlname Name of field
* @param string $fields other fields to autocomplete
* @param string $url Chemin du fichier de reponse : /chemin/fichier.php
* @param string $option More parameters on URL request
* @param int $minLength Minimum number of chars to trigger that Ajax search
* @param int $autoselect Automatic selection if just one value
* @return string Script
*/
function ajax_multiautocompleter($htmlname,$fields,$url,$option='',$minLength=2,$autoselect=0)
{
$script = '<!-- Autocomplete -->'."\n";
$script.= '<script type="text/javascript">';
$script.= 'jQuery(document).ready(function() {
var fields = '.json_encode($fields).';
var length = fields.length;
var autoselect = '.$autoselect.';
//alert(fields + " " + length);
jQuery("input#'.$htmlname.'").autocomplete({
dataType: "json",
minLength: '.$minLength.',
source: function( request, response ) {
jQuery.getJSON( "'.$url.($option?'?'.$option:'').'", { '.$htmlname.': request.term }, function(data){
response( jQuery.map( data, function( item ) {
if (autoselect == 1 && data.length == 1) {
jQuery("#'.$htmlname.'").val(item.value);
// TODO move this to specific request
if (item.states) {
jQuery("#departement_id").html(item.states);
}
for (i=0;i<length;i++) {
if (item[fields[i]]) { // If defined
//alert(item[fields[i]]);
jQuery("#" + fields[i]).val(item[fields[i]]);
}
}
}
return item
}));
});
},
select: function( event, ui ) {
for (i=0;i<length;i++) {
//alert(fields[i] + " = " + ui.item[fields[i]]);
if (fields[i]=="selectcountry_id")
{
if (ui.item[fields[i]] > 0) // Do not erase country if unknown
{
jQuery("#" + fields[i]).val(ui.item[fields[i]]);
// If we set new country and new state, we need to set a new list of state to allow change
if (ui.item.states && ui.item["departement_id"] != jQuery("#departement_id").value) {
jQuery("#departement_id").html(ui.item.states);
}
}
}
else if (fields[i]=="state_id" || fields[i]=="departement_id")
{
if (ui.item[fields[i]] > 0) // Do not erase state if unknown
{
jQuery("#" + fields[i]).val(ui.item[fields[i]]); // This may fails if not correct country
}
}
else if (ui.item[fields[i]]) { // If defined
//alert(fields[i]);
//alert(ui.item[fields[i]]);
jQuery("#" + fields[i]).val(ui.item[fields[i]]);
}
}
}
});
});';
$script.= '</script>';
return $script;
}
/**
* Show an ajax dialog
*
* @param string $title Title of dialog box
* @param string $message Message of dialog box
* @param int $w Width of dialog box
* @param int $h height of dialog box
* @return void
*/
function ajax_dialog($title,$message,$w=350,$h=150)
{
global $langs;
$msg.= '<div id="dialog-info" title="'.dol_escape_htmltag($title).'">';
$msg.= $message;
$msg.= '</div>'."\n";
$msg.= '<script type="text/javascript">
jQuery(function() {
jQuery("#dialog-info").dialog({
resizable: false,
height:'.$h.',
width:'.$w.',
modal: true,
buttons: {
Ok: function() {
jQuery(this ).dialog(\'close\');
}
}
});
});
</script>';
$msg.= "\n";
return $msg;
}
/**
* Convert a select html field into an ajax combobox
*
* @param string $htmlname Name of html field
* @param array $event Event options
* @return string Return html string to convert a select field into a combo
*/
function ajax_combobox($htmlname, $event=array())
{
$msg.= '<script type="text/javascript">
$(function() {
$("#'.$htmlname.'").combobox({
selected : function(event,ui) {
var obj = '.json_encode($event).';
$.each(obj, function(key,values) {
if (values.method.length) {
getMethod(values);
}
});
}
});
function getMethod(obj) {
var id = $("#'.$htmlname.'").val();
var method = obj.method;
var url = obj.url;
var htmlname = obj.htmlname;
$.get(url,
{
action: method,
id: id,
htmlname: htmlname
},
function(response) {
$("select#" + htmlname).html(response);
});
}
});';
$msg.= "</script>\n";
return $msg;
}
/**
* On/off button for constant
*
* @param string $code Name of constant
* @param array $input Input element
* @param int $entity Entity to set
* @return void
*/
function ajax_constantonoff($code,$input=array(),$entity=false)
{
global $conf, $langs;
$entity = ((isset($entity) && is_numeric($entity) && $entity >= 0) ? $entity : $conf->entity);
$out= '<script type="text/javascript">
$(function() {
var input = '.json_encode($input).';
// Set constant
$("#set_'.$code.'").click(function() {
$.get( "'.DOL_URL_ROOT.'/core/ajax/constantonoff.php", {
action: \'set\',
name: \''.$code.'\',
entity: \''.$entity.'\'
},
function() {
$("#set_'.$code.'").hide();
$("#del_'.$code.'").show();
// Enable another element
if (input.disabled && input.disabled.length > 0) {
$.each(input.disabled, function(key,value) {
$("#" + value).removeAttr("disabled");
if ($("#" + value).hasClass("butActionRefused") == true) {
$("#" + value).removeClass("butActionRefused");
$("#" + value).addClass("butAction");
}
});
// Show another element
} else if (input.showhide && input.showhide.length > 0) {
$.each(input.showhide, function(key,value) {
$("#" + value).show();
});
}
});
});
// Del constant
$("#del_'.$code.'").click(function() {
$.get( "'.DOL_URL_ROOT.'/core/ajax/constantonoff.php", {
action: \'del\',
name: \''.$code.'\',
entity: \''.$entity.'\'
},
function() {
$("#del_'.$code.'").hide();
$("#set_'.$code.'").show();
// Disable another element
if (input.disabled && input.disabled.length > 0) {
$.each(input.disabled, function(key,value) {
$("#" + value).attr("disabled", true);
if ($("#" + value).hasClass("butAction") == true) {
$("#" + value).removeClass("butAction");
$("#" + value).addClass("butActionRefused");
}
});
// Hide another element
} else if (input.showhide && input.showhide.length > 0) {
$.each(input.showhide, function(key,value) {
$("#" + value).hide();
});
}
});
});
});
</script>';
$out.= '<span id="set_'.$code.'" class="linkobject '.($conf->global->$code?'hideobject':'').'">'.img_picto($langs->trans("Disabled"),'switch_off').'</span>';
$out.= '<span id="del_'.$code.'" class="linkobject '.($conf->global->$code?'':'hideobject').'">'.img_picto($langs->trans("Enabled"),'switch_on').'</span>';
return $out;
}
?>