47 function ajax_autocompleter($selected, $htmlname, $url, $urloption =
'', $minLength = 2, $autoselect = 0, $ajaxoptions = array(), $moreparams =
'')
49 if (empty($minLength)) $minLength = 1;
51 $dataforrenderITem =
'ui-autocomplete';
52 $dataforitem =
'ui-autocomplete-item';
54 if (defined(
'JS_QUERY_AUTOCOMPLETE_RENDERITEM')) $dataforrenderITem = constant(
'JS_QUERY_AUTOCOMPLETE_RENDERITEM');
55 if (defined(
'JS_QUERY_AUTOCOMPLETE_ITEM')) $dataforitem = constant(
'JS_QUERY_AUTOCOMPLETE_ITEM');
59 $script =
'<input type="hidden" name="'.$htmlname.
'" id="'.$htmlname.
'" value="'.$selected.
'" '.($moreparams ? $moreparams :
'').
' />';
61 $script .=
'<!-- Javascript code for autocomplete of field '.$htmlname.
' -->'.
"\n";
62 $script .=
'<script>'.
"\n";
63 $script .=
'$(document).ready(function() {
64 var autoselect = '.$autoselect.
';
65 var options = '.json_encode($ajaxoptions).
'; /* Option of actions to do after keyup, or after select */
67 /* Remove selected id as soon as we type or delete a char (it means old selection is wrong). Use keyup/down instead of change to avoid loosing the product id. This is needed only for select of predefined product */
68 $("input#search_'.$htmlname.
'").keydown(function(e) {
69 if (e.keyCode != 9) /* If not "Tab" key */
71 if (e.keyCode == 13) { return false; } /* disable "ENTER" key useful for barcode readers */
72 console.log("Clear id previously selected for field '.$htmlname.
'");
73 $("#'.$htmlname.
'").val("");
77 // Check options for secondary actions when keyup
78 $("input#search_'.$htmlname.
'").keyup(function() {
79 if ($(this).val().length == 0)
81 $("#search_'.$htmlname.
'").val("");
82 $("#'.$htmlname.
'").val("").trigger("change");
83 if (options.option_disabled) {
84 $("#" + options.option_disabled).removeAttr("disabled");
86 if (options.disabled) {
87 $.each(options.disabled, function(key, value) {
88 $("#" + value).removeAttr("disabled");
92 $.each(options.update, function(key, value) {
93 $("#" + key).val("").trigger("change");
97 $.each(options.show, function(key, value) {
98 $("#" + value).hide().trigger("hide");
101 if (options.update_textarea) {
102 $.each(options.update_textarea, function(key, value) {
103 if (typeof CKEDITOR == "object" && typeof CKEDITOR.instances != "undefined" && CKEDITOR.instances[key] != "undefined") {
104 CKEDITOR.instances[key].setData("");
106 $("#" + key).html("");
113 $("input#search_'.$htmlname.
'").autocomplete({
114 source: function( request, response ) {
115 $.get("'.$url.($urloption ?
'?'.$urloption :
'').
'", { '.$htmlname.
': request.term }, function(data){
118 response($.map( data, function(item) {
119 if (autoselect == 1 && data.length == 1) {
120 $("#search_'.$htmlname.
'").val(item.value);
121 $("#'.$htmlname.
'").val(item.key).trigger("change");
123 var label = item.label.toString();
125 if (options.update) {
126 $.each(options.update, function(key, value) {
127 update[key] = item[value];
131 if (options.update_textarea) {
132 $.each(options.update_textarea, function(key, value) {
133 textarea[key] = item[value];
136 return { label: label, value: item.value, id: item.key, disabled: item.disabled,
137 update: update, textarea: textarea,
138 pbq: item.pbq, type: item.type, qty: item.qty, discount: item.discount, pricebasetype: item.pricebasetype, price_ht: item.price_ht, price_ttc: item.price_ttc }
141 else console.error("Error: Ajax url '.$url.($urloption ?
'?'.$urloption :
'').
' has returned an empty page. Should be an empty json array.");
145 minLength: '.$minLength.
',
146 select: function( event, ui ) { // Function ran once new value has been selected into javascript combo
147 console.log("We will trigger change on input '.$htmlname.
' because of the select definition of autocomplete code for input#search_'.$htmlname.
'");
148 console.log("Selected id = "+ui.item.id+" - If this value is null, it means you select a record with key that is null so selection is not effective");
150 console.log("Propagate before some properties retrieved into data-xxx properties");
151 $("#'.$htmlname.
'").attr("data-pbq", ui.item.pbq);
152 $("#'.$htmlname.
'").attr("data-pbqup", ui.item.price_ht);
153 $("#'.$htmlname.
'").attr("data-pbqbase", ui.item.pricebasetype);
154 $("#'.$htmlname.
'").attr("data-pbqqty", ui.item.qty);
155 $("#'.$htmlname.
'").attr("data-pbqpercent", ui.item.discount);
157 $("#'.$htmlname.
'").val(ui.item.id).trigger("change"); // Select new value
159 // Disable an element
160 if (options.option_disabled) {
161 console.log("Make action option_disabled on #"+options.option_disabled+" with disabled="+ui.item.disabled)
162 if (ui.item.disabled) {
163 $("#" + options.option_disabled).prop("disabled", true);
165 $.jnotify(options.error, "error", true); // Output with jnotify the error message
167 if (options.warning) {
168 $.jnotify(options.warning, "warning", false); // Output with jnotify the warning message
171 $("#" + options.option_disabled).removeAttr("disabled");
175 if (options.disabled) {
176 console.log("Make action disabled on each "+options.option_disabled)
177 $.each(options.disabled, function(key, value) {
178 $("#" + value).prop("disabled", true);
182 console.log("Make action show on each "+options.show)
183 $.each(options.show, function(key, value) {
184 $("#" + value).show().trigger("show");
189 if (ui.item.update) {
190 console.log("Make action update on each ui.item.update")
191 // loop on each "update" fields
192 $.each(ui.item.update, function(key, value) {
193 console.log("Set value "+value+" into #"+key);
194 $("#" + key).val(value).trigger("change");
197 if (ui.item.textarea) {
198 console.log("Make action textarea on each ui.item.textarea")
199 $.each(ui.item.textarea, function(key, value) {
200 if (typeof CKEDITOR == "object" && typeof CKEDITOR.instances != "undefined" && CKEDITOR.instances[key] != "undefined") {
201 CKEDITOR.instances[key].setData(value);
202 CKEDITOR.instances[key].focus();
204 $("#" + key).html(value);
205 $("#" + key).focus();
209 console.log("ajax_autocompleter new value selected, we trigger change also on original component so on field #search_'.$htmlname.
'");
211 $("#search_'.$htmlname.
'").trigger("change"); // We have changed value of the combo select, we must be sure to trigger all js hook binded on this event. This is required to trigger other javascript change method binded on original field by other code.
214 }).data("'.$dataforrenderITem.
'")._renderItem = function( ul, item ) {
216 .data( "'.$dataforitem.
'", item ) // jQuery UI > 1.10.0
217 .append( \'<a><span class="tag">\' + item.label + "</span></a>" )
222 $script .=
'</script>';
243 $script =
'<!-- Autocomplete -->'.
"\n";
244 $script .=
'<script>';
245 $script .=
'jQuery(document).ready(function() {
246 var fields = '.json_encode($fields).
';
247 var nboffields = fields.length;
248 var autoselect = '.$autoselect.
';
249 //alert(fields + " " + nboffields);
251 jQuery("input#'.$htmlname.
'").autocomplete({
253 minLength: '.$minLength.
',
254 source: function( request, response ) {
255 jQuery.getJSON( "'.$url.($option ?
'?'.$option :
'').
'", { '.$htmlname.
': request.term }, function(data){
256 response( jQuery.map( data, function( item ) {
257 if (autoselect == 1 && data.length == 1) {
258 jQuery("#'.$htmlname.
'").val(item.value);
259 // TODO move this to specific request
261 jQuery("#state_id").html(item.states);
263 for (i=0;i<nboffields;i++) {
264 if (item[fields[i]]) { // If defined
265 //alert(item[fields[i]]);
266 jQuery("#" + fields[i]).val(item[fields[i]]);
274 select: function( event, ui ) {
276 for (i=0;i<nboffields;i++) {
277 //alert(fields[i] + " = " + ui.item[fields[i]]);
278 if (fields[i]=="selectcountry_id")
280 if (ui.item[fields[i]] > 0) // Do not erase country if unknown
282 oldvalue=jQuery("#" + fields[i]).val();
283 newvalue=ui.item[fields[i]];
284 //alert(oldvalue+" "+newvalue);
285 jQuery("#" + fields[i]).val(ui.item[fields[i]]);
286 if (oldvalue != newvalue) // To force select2 to refresh visible content
288 needtotrigger="#" + fields[i];
291 // If we set new country and new state, we need to set a new list of state to allow change
292 if (ui.item.states && ui.item["state_id"] != jQuery("#state_id").value) {
293 jQuery("#state_id").html(ui.item.states);
297 else if (fields[i]=="state_id" || fields[i]=="state_id")
299 if (ui.item[fields[i]] > 0) // Do not erase state if unknown
301 oldvalue=jQuery("#" + fields[i]).val();
302 newvalue=ui.item[fields[i]];
303 //alert(oldvalue+" "+newvalue);
304 jQuery("#" + fields[i]).val(ui.item[fields[i]]); // This may fails if not correct country
305 if (oldvalue != newvalue) // To force select2 to refresh visible content
307 needtotrigger="#" + fields[i];
311 else if (ui.item[fields[i]]) { // If defined
312 oldvalue=jQuery("#" + fields[i]).val();
313 newvalue=ui.item[fields[i]];
314 //alert(oldvalue+" "+newvalue);
315 jQuery("#" + fields[i]).val(ui.item[fields[i]]);
316 if (oldvalue != newvalue) // To force select2 to refresh visible content
318 needtotrigger="#" + fields[i];
322 if (needtotrigger != "") // To force select2 to refresh visible content
324 // We introduce a delay so hand is back to js and all other js change can be done before the trigger that may execute a submit is done
325 // This is required for example when changing zip with autocomplete that change the country
326 jQuery(needtotrigger).delay(500).queue(function() {
327 jQuery(this).trigger("change");
334 $script .=
'</script>';
353 $msg =
'<div id="dialog-info" title="'.dol_escape_htmltag($newtitle).
'">';
355 $msg .=
'</div>'.
"\n";
358 jQuery("#dialog-info").dialog({
365 jQuery(this).dialog(\'close\');
391 function ajax_combobox($htmlname, $events = array(), $minLengthToAutocomplete = 0, $forcefocus = 0, $widthTypeOfAutocomplete =
'resolve')
396 if (!empty($conf->browser->layout) && $conf->browser->layout ==
'phone' && !empty($conf->global->MAIN_DISALLOW_SELECT2_WITH_SMARTPHONE))
return '';
398 if (!empty($conf->global->MAIN_DISABLE_AJAX_COMBOX))
return '';
399 if (empty($conf->use_javascript_ajax))
return '';
400 if (empty($conf->global->MAIN_USE_JQUERY_MULTISELECT) && !defined(
'REQUIRE_JQUERY_MULTISELECT'))
return '';
401 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
return '';
403 if (empty($minLengthToAutocomplete)) $minLengthToAutocomplete = 0;
405 $tmpplugin =
'select2';
406 $msg =
"\n".
'<!-- JS CODE TO ENABLE '.$tmpplugin.
' for id = '.$htmlname.
' -->
408 $(document).ready(function () {
409 $(\''.(preg_match(
'/^\./', $htmlname) ? $htmlname :
'#'.$htmlname).
'\').
'.$tmpplugin.'({
411 width: \''.$widthTypeOfAutocomplete.
'\',
412 minimumInputLength:
'.$minLengthToAutocomplete.',
414 containerCssClass: \
':all:\', /* Line to add class of origin SELECT propagated to the new <span class="select2-selection...> tag */
415 templateResult: function (data, container) { /* Format visible output into combo list */
416 /* Code to add class of origin OPTION propagated to the new select2 <li> tag */
417 if (data.element) { $(container).addClass($(data.element).attr("class")); }
418 //console.log(data.html);
419 if ($(data.element).attr("data-html") != undefined) return htmlEntityDecodeJs($(data.element).attr("data-html")); // If property html set, we decode html entities and use this
422 templateSelection: function (selection) { /* Format visible output of selected value */
423 return selection.text;
425 escapeMarkup: function(markup) {
428 dropdownCssClass: \'ui-dialog\'
430 if ($forcefocus) $msg .=
'.select2(\'focus\')';
433 if (is_array($events) && count($events))
436 jQuery("#'.$htmlname.
'").change(function () {
437 var obj = '.json_encode($events).
';
438 $.each(obj, function(key,values) {
439 if (values.method.length) {
440 runJsCodeForEvent'.$htmlname.
'(values);
445 function runJsCodeForEvent'.$htmlname.
'(obj) {
446 var id = $("#'.$htmlname.
'").val();
447 var method = obj.method;
449 var htmlname = obj.htmlname;
450 var showempty = obj.showempty;
451 console.log("Run runJsCodeForEvent-'.$htmlname.
' from ajax_combobox id="+id+" method="+method+" showempty="+showempty+" url="+url+" htmlname="+htmlname);
460 $.each(obj.params, function(key,action) {
462 var num = response.num;
464 $("#" + key).removeAttr(action);
466 $("#" + key).attr(action, action);
470 $("select#" + htmlname).html(response.value);
472 var selecthtml_str = response.value;
473 var selecthtml_dom=$.parseHTML(selecthtml_str);
474 $("#inputautocomplete"+htmlname).val(selecthtml_dom[0][0].innerHTML);
476 $("#inputautocomplete"+htmlname).val("");
478 $("select#" + htmlname).change(); /* Trigger event change */
485 $msg .=
"</script>\n";
503 function ajax_constantonoff($code, $input = array(), $entity = null, $revertonoff = 0, $strict = 0, $forcereload = 0, $marginleftonlyshort = 2, $forcenoajax = 0)
505 global $conf, $langs, $user;
507 $entity = ((isset($entity) && is_numeric($entity) && $entity >= 0) ? $entity : $conf->entity);
508 if (!isset($input)) $input = array();
510 if (empty($conf->use_javascript_ajax) || $forcenoajax)
512 if (empty($conf->global->$code))
print '<a href="'.
$_SERVER[
'PHP_SELF'].
'?action=set_'.$code.
'&token='.
newToken().
'&entity='.$entity.
'">'.
img_picto($langs->trans(
"Disabled"),
'off').
'</a>';
513 else print '<a href="'.$_SERVER[
'PHP_SELF'].
'?action=del_'.$code.
'&token='.
newToken().
'&entity='.$entity.
'">'.
img_picto($langs->trans(
"Enabled"),
'on').
'</a>';
515 $out =
"\n<!-- Ajax code to switch constant ".$code.
" -->".
'
517 $(document).ready(function() {
518 var input = '.json_encode($input).
';
519 var url = \''.DOL_URL_ROOT.
'/core/ajax/constantonoff.php\';
520 var code = \''.$code.
'\';
521 var entity = \
''.$entity.
'\';
522 var strict = \
''.$strict.
'\';
523 var userid = \
''.$user->id.
'\';
524 var yesButton = \
''.dol_escape_js($langs->transnoentities(
"Yes")).
'\';
525 var noButton = \
''.dol_escape_js($langs->transnoentities(
"No")).
'\';
526 var token = \
''.currentToken().
'\';
529 $(
"#set_" +
code).click(
function() {
530 if (input.alert && input.alert.set) {
531 if (input.alert.set.yesButton) yesButton = input.alert.set.yesButton;
532 if (input.alert.set.noButton) noButton = input.alert.set.noButton;
533 confirmConstantAction(
"set", url,
code, input, input.alert.set, entity, yesButton, noButton, strict, userid, token);
535 setConstant(url,
code, input, entity, 0,
'.$forcereload.', userid, token);
540 $(
"#del_" +
code).click(
function() {
541 if (input.alert && input.alert.del) {
542 if (input.alert.del.yesButton) yesButton = input.alert.del.yesButton;
543 if (input.alert.del.noButton) noButton = input.alert.del.noButton;
544 confirmConstantAction(
"del", url,
code, input, input.alert.del, entity, yesButton, noButton, strict, userid, token);
546 delConstant(url,
code, input, entity, 0,
'.$forcereload.', userid, token);
552 $out .= '<div
id=
"confirm_'.$code.'" title=
"" style=
"display: none;"></div>
';
553 $out .= '<span
id=
"set_'.$code.'" class=
"valignmiddle linkobject '.(!empty($conf->global->$code) ? 'hideobject' : '').'">
'.($revertonoff ?img_picto($langs->trans("Enabled"), 'switch_on
', '', false, 0, 0, '', '', $marginleftonlyshort) : img_picto($langs->trans("Disabled"), 'switch_off
', '', false, 0, 0, '', '', $marginleftonlyshort)).'</span>
';
554 $out .= '<span
id=
"del_'.$code.'" class=
"valignmiddle linkobject '.(!empty($conf->global->$code) ? '' : 'hideobject').'">
'.($revertonoff ?img_picto($langs->trans("Disabled"), 'switch_off
', '', false, 0, 0, '', '', $marginleftonlyshort) : img_picto($langs->trans("Enabled"), 'switch_on
', '', false, 0, 0, '', '', $marginleftonlyshort)).'</span>
';
573 function ajax_object_onoff($object, $code, $field, $text_on, $text_off, $input = array())
579 var input =
'.json_encode($input).';
582 $(
"#set_'.$code.'_'.$object->id.'").click(
function() {
583 $.get(
"'.DOL_URL_ROOT.'/core/ajax/objectonoff.php", {
585 field: \''.$field.
'\',
587 element: \''.$object->element.
'\',
588 id: \
''.$object->id.
'\',
589 token: \
''.newToken().
'\'
592 $(
"#set_'.$code.'_'.$object->id.'").hide();
593 $(
"#del_'.$code.'_'.$object->id.'").show();
595 if (input.disabled && input.disabled.length > 0) {
596 $.each(input.disabled,
function(key,value) {
597 $(
"#" + value).removeAttr(
"disabled");
598 if ($(
"#" + value).hasClass(
"butActionRefused") ==
true) {
599 $(
"#" + value).removeClass(
"butActionRefused");
600 $(
"#" + value).addClass(
"butAction");
604 }
else if (input.showhide && input.showhide.length > 0) {
605 $.each(input.showhide,
function(key,value) {
606 $(
"#" + value).show();
613 $(
"#del_'.$code.'_'.$object->id.'").click(
function() {
614 $.get(
"'.DOL_URL_ROOT.'/core/ajax/objectonoff.php", {
616 field: \''.$field.
'\',
618 element: \''.$object->element.
'\',
619 id: \
''.$object->id.
'\',
620 token: \
''.newToken().
'\'
623 $(
"#del_'.$code.'_'.$object->id.'").hide();
624 $(
"#set_'.$code.'_'.$object->id.'").show();
626 if (input.disabled && input.disabled.length > 0) {
627 $.each(input.disabled,
function(key,value) {
628 $(
"#" + value).prop(
"disabled",
true);
629 if ($(
"#" + value).hasClass(
"butAction") ==
true) {
630 $(
"#" + value).removeClass(
"butAction");
631 $(
"#" + value).addClass(
"butActionRefused");
635 }
else if (input.showhide && input.showhide.length > 0) {
636 $.each(input.showhide,
function(key,value) {
637 $(
"#" + value).hide();
644 $out .= '<span
id=
"set_'.$code.'_'.$object->id.'" class=
"linkobject '.($object->$code == 1 ? 'hideobject' : '').'">
'.img_picto($langs->trans($text_off), 'switch_off
').'</span>
';
645 $out .= '<span
id=
"del_'.$code.'_'.$object->id.'" class=
"linkobject '.($object->$code == 1 ? '' : 'hideobject').'">
'.img_picto($langs->trans($text_on), 'switch_on
').'</span>
';
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto= 'UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
ajax_autocompleter($selected, $htmlname, $url, $urloption= '', $minLength=2, $autoselect=0, $ajaxoptions=array(), $moreparams= '')
Generic function that return javascript to add to a page to transform a common input field into an au...
if(!empty($arrayfields['country.code_iso']['checked'])) print_liste_field_titre($arrayfields['country.code_iso']['label'] country if(!empty($arrayfields['typent.code']['checked'])) print_liste_field_titre($arrayfields['typent.code']['label'] typent code
ajax_multiautocompleter($htmlname, $fields, $url, $option= '', $minLength=2, $autoselect=0)
Generic function that return javascript to add to a page to transform a common input field into an au...
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete= 'resolve')
Convert a html select field into an ajax combobox.
select2arrayoflanguage
Set array used for select2 translations.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
ajax_constantonoff($code, $input=array(), $entity=null, $revertonoff=0, $strict=0, $forcereload=0, $marginleftonlyshort=2, $forcenoajax=0)
On/off button for constant.
ajax_dialog($title, $message, $w=350, $h=150)
Show an ajax dialog.
print $_SERVER["PHP_SELF"]
Edit parameters.
print
Draft customers invoices.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
dol_textishtml($msg, $option=0)
Return if a text is a html content.