dolibarr 24.0.3
ajax.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2007-2010 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2007-2015 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
5 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
6 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2026 Open-Dsi <support@open-dsi.fr>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 * or see https://www.gnu.org/
22 */
23
50function ajax_autocompleter($selected, $htmlname, $url, $urloption = '', $minLength = 2, $autoselect = 0, $ajaxoptions = array(), $moreparams = '')
51{
52 if (empty($minLength)) {
53 $minLength = 1;
54 }
55
56 $dataforrenderITem = 'ui-autocomplete';
57 $dataforitem = 'ui-autocomplete-item';
58 // Allow two constant to use other values for backward compatibility
59 if (defined('JS_QUERY_AUTOCOMPLETE_RENDERITEM')) {
60 $dataforrenderITem = constant('JS_QUERY_AUTOCOMPLETE_RENDERITEM');
61 }
62 if (defined('JS_QUERY_AUTOCOMPLETE_ITEM')) {
63 $dataforitem = constant('JS_QUERY_AUTOCOMPLETE_ITEM');
64 }
65
66 $htmlnamejquery = str_replace('.', '\\\\.', $htmlname);
67
68 // Input search_htmlname is original field
69 // Input htmlname is a second input field used when using ajax autocomplete.
70 $script = '<input type="hidden" name="'.$htmlname.'" id="'.$htmlname.'" value="'.$selected.'" '.($moreparams ? $moreparams : '').' />';
71
72 $script .= '<!-- Javascript code for autocomplete of field '.$htmlname.' -->'."\n";
73 $script .= '<script>'."\n";
74 $script .= '$(document).ready(function() {
75 var autoselect = '.((int) $autoselect).';
76 var options = '.json_encode($ajaxoptions).'; /* Option of actions to do after keyup, or after select */
77
78 /* 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 losing the product id. This is needed only for select of predefined product */
79 $("input#search_'.$htmlnamejquery.'").keydown(function(e) {
80 if (e.keyCode != 9) /* If not "Tab" key */
81 {
82 if (e.keyCode == 13) { return false; } /* disable "ENTER" key useful for barcode readers */
83 console.log("Clear id previously selected for field '.$htmlname.'");
84 $("#'.$htmlnamejquery.'").val("");
85 }
86 });
87
88 // Check options for secondary actions when keyup
89 $("input#search_'.$htmlnamejquery.'").keyup(function() {
90 if ($(this).val().length == 0)
91 {
92 $("#search_'.$htmlnamejquery.'").val("");
93 $("#'.$htmlnamejquery.'").val("").trigger("change");
94 if (options.option_disabled) {
95 $("#" + options.option_disabled).removeAttr("disabled");
96 }
97 if (options.disabled) {
98 $.each(options.disabled, function(key, value) {
99 $("#" + value).removeAttr("disabled");
100 });
101 }
102 if (options.update) {
103 $.each(options.update, function(key, value) {
104 $("#" + key).val("").trigger("change");
105 });
106 }
107 if (options.show) {
108 $.each(options.show, function(key, value) {
109 $("#" + value).hide().trigger("hide");
110 });
111 }
112 if (options.update_textarea) {
113 $.each(options.update_textarea, function(key, value) {
114 if (typeof CKEDITOR == "object" && typeof CKEDITOR.instances != "undefined" && CKEDITOR.instances[key] != "undefined") {
115 CKEDITOR.instances[key].setData("");
116 } else {
117 $("#" + key).html("");
118 }
119 });
120 }
121 }
122 });
123
124 // Activate the autocomplete to execute the GET
125 $("input#search_'.$htmlnamejquery.'").autocomplete({
126 source: function( request, response ) {
127 $.get("'.$url.($urloption ? '?'.$urloption : '').'", { "'.str_replace('.', '_', $htmlname).'": request.term }, function(data){
128 if (data != null)
129 {
130 response($.map( data, function(item) {
131 console.log("Received answer from ajax GET, we populate array to return to the jquery autocomplete");
132 if (autoselect == 1 && data.length == 1) {
133 $("#search_'.$htmlnamejquery.'").val(item.value);
134 ';
135 if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES')) {
136 $script .= '
137 // When product has only one price by qty and 1 result, must set data attributes before triggering change
138 $("#'.$htmlname.'").attr("data-pbq", item.pbq);
139 $("#'.$htmlname.'").attr("data-pbqup", item.price_ht);
140 $("#'.$htmlname.'").attr("data-pbqbase", item.pricebasetype);
141 $("#'.$htmlname.'").attr("data-pbqqty", item.qty);
142 $("#'.$htmlname.'").attr("data-pbqpercent", item.discount);
143 ';
144 }
145 $script .= '
146 $("#'.$htmlnamejquery.'").val(item.key).trigger("change");
147 }
148 var label = "";
149 if (item.labelhtml != null) {
150 label = item.labelhtml.toString();
151 } else if (item.label != null) {
152 label = item.label.toString();
153 }
154 var update = {};
155 if (options.update) {
156 $.each(options.update, function(key, value) {
157 update[key] = item[value];
158 });
159 }
160 var textarea = {};
161 if (options.update_textarea) {
162 $.each(options.update_textarea, function(key, value) {
163 textarea[key] = item[value];
164 });
165 }
166
167 return { label: label,
168 value: item.value,
169 id: item.key,
170 disabled: item.disabled,
171 update: update,
172 textarea: textarea,
173 pbq: item.pbq,
174 type: item.type,
175 qty: item.qty,
176 discount: item.discount,
177 pricebasetype: item.pricebasetype,
178 price_ht: item.price_ht,
179 price_ttc: item.price_ttc,
180 price_unit_ht: item.price_unit_ht,
181 price_unit_ht_locale: item.price_unit_ht_locale,
182 multicurrency_code: item.multicurrency_code,
183 multicurrency_unitprice: item.multicurrency_unitprice,
184 description : item.description,
185 ref_customer: item.ref_customer,
186 tva_tx: item.tva_tx,
187 default_vat_code: item.default_vat_code,
188 supplier_ref: item.supplier_ref
189 }
190 }));
191 } else {
192 console.error("Error: Ajax url '.$url.($urloption ? '?'.$urloption : '').' has returned an empty page. Should be an empty json array.");
193 }
194 }, "json");
195 },
196 dataType: "json",
197 minLength: '.((int) $minLength).',
198 select: function( event, ui ) { // Function ran once a new value has been selected into the javascript combo
199 console.log("We will trigger change on input '.$htmlname.' because of the select definition of autocomplete code for input#search_'.$htmlname.'");
200 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");
201
202 console.log("Before, we propagate some properties, retrieved by the ajax of the get, into the data-xxx properties of the component #'.$htmlnamejquery.'");
203 //console.log(ui.item);
204
205 // For supplier price and customer when price by quantity is off
206 $("#'.$htmlnamejquery.'").attr("data-up", ui.item.price_ht);
207 $("#'.$htmlnamejquery.'").attr("data-up-locale", ui.item.price_unit_ht_locale);
208 $("#'.$htmlnamejquery.'").attr("data-base", ui.item.pricebasetype);
209 $("#'.$htmlnamejquery.'").attr("data-qty", ui.item.qty);
210 $("#'.$htmlnamejquery.'").attr("data-discount", ui.item.discount);
211 $("#'.$htmlnamejquery.'").attr("data-description", ui.item.description);
212 $("#'.$htmlnamejquery.'").attr("data-ref-customer", ui.item.ref_customer);
213 $("#'.$htmlnamejquery.'").attr("data-tvatx", ui.item.tva_tx);
214 $("#'.$htmlnamejquery.'").attr("data-default-vat-code", ui.item.default_vat_code);
215 $("#'.$htmlnamejquery.'").attr("data-supplier-ref", ui.item.supplier_ref); // supplier_ref of price
216
217 // For multi-currency values
218 $("#'.$htmlnamejquery.'").attr("data-multicurrency-code", ui.item.multicurrency_code);
219 $("#'.$htmlnamejquery.'").attr("data-multicurrency-unitprice", ui.item.multicurrency_unitprice);
220 ';
221 if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES')) {
222 $script .= '
223 // For customer price when PRODUIT_CUSTOMER_PRICES_BY_QTY is on
224 console.log("PRODUIT_CUSTOMER_PRICES_BY_QTY is on, so we propagate also prices by quantity into data-pbqxxx properties");
225 $("#'.$htmlnamejquery.'").attr("data-pbq", ui.item.pbq);
226 $("#'.$htmlnamejquery.'").attr("data-pbqup", ui.item.price_ht);
227 $("#'.$htmlnamejquery.'").attr("data-pbqbase", ui.item.pricebasetype);
228 $("#'.$htmlnamejquery.'").attr("data-pbqqty", ui.item.qty);
229 $("#'.$htmlnamejquery.'").attr("data-pbqpercent", ui.item.discount);
230 ';
231 }
232 $script .= '
233 // A new value has been selected, we trigger the handlers on #htmlnamejquery
234 console.log("Now, we trigger changes on #'.$htmlnamejquery.'");
235 $("#'.$htmlnamejquery.'").val(ui.item.id).trigger("change"); // Select new value
236
237 // Complementary actions
238
239 // Disable an element
240 if (options.option_disabled) {
241 console.log("Make action option_disabled on #"+options.option_disabled+" with disabled="+ui.item.disabled)
242 if (ui.item.disabled) {
243 $("#" + options.option_disabled).prop("disabled", true);
244 if (options.error) {
245 $.jnotify(options.error, "error", true); // Output with jnotify the error message
246 }
247 if (options.warning) {
248 $.jnotify(options.warning, "warning", false); // Output with jnotify the warning message
249 }
250 } else {
251 $("#" + options.option_disabled).removeAttr("disabled");
252 }
253 }
254
255 if (options.disabled) {
256 console.log("Make action \'disabled\' on each "+options.option_disabled)
257 $.each(options.disabled, function(key, value) {
258 $("#" + value).prop("disabled", true);
259 });
260 }
261 if (options.show) {
262 console.log("Make action \'show\' on each "+options.show)
263 $.each(options.show, function(key, value) {
264 $("#" + value).show().trigger("show");
265 });
266 }
267
268 // Update an input
269 if (ui.item.update) {
270 console.log("Make action \'update\' on each ui.item.update (if there is)")
271 // loop on each "update" fields
272 $.each(ui.item.update, function(key, value) {
273 console.log("Set value "+value+" into #"+key);
274 $("#" + key).val(value).trigger("change");
275 });
276 }
277 if (ui.item.textarea) {
278 console.log("Make action \'textarea\' on each ui.item.textarea (if there is)")
279 $.each(ui.item.textarea, function(key, value) {
280 if (typeof CKEDITOR == "object" && typeof CKEDITOR.instances != "undefined" && CKEDITOR.instances[key] != "undefined") {
281 CKEDITOR.instances[key].setData(value);
282 CKEDITOR.instances[key].focus();
283 } else {
284 $("#" + key).html(value);
285 $("#" + key).focus();
286 }
287 });
288 }
289 console.log("ajax_autocompleter new value selected, we trigger change also on original component so on field #search_'.$htmlname.'");
290
291 $("#search_'.$htmlnamejquery.'").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.
292 }
293 ,delay: 500
294 });
295 const widgetData = $("input#search_'.$htmlnamejquery.'").data("'.$dataforrenderITem.'");
296 if (widgetData) {
297 widgetData._renderItem = function( ul, item ) {
298 return $("<li>")
299 .data( "'.$dataforitem.'", item ) // jQuery UI > 1.10.0
300 .append( \'<a><span class="tag">\' + item.label + "</span></a>" )
301 .appendTo(ul);
302 };
303 }
304
305 });';
306 $script .= '</script>';
307
308 return $script;
309}
310
325function ajax_multiautocompleter($htmlname, $fields, $url, $option = '', $minLength = 2, $autoselect = 0)
326{
327 $script = '<!-- Autocomplete -->'."\n";
328 $script .= '<script>';
329 $script .= 'jQuery(document).ready(function() {
330 var fields = '.json_encode($fields).';
331 var nboffields = fields.length;
332 var autoselect = '.$autoselect.';
333 //alert(fields + " " + nboffields);
334
335 // Activate the autocomplete to execute the GET
336 jQuery("input#'.$htmlname.'").autocomplete({
337 dataType: "json",
338 minLength: '.$minLength.',
339 source: function( request, response ) {
340 jQuery.getJSON( "'.$url.($option ? '?'.$option : '').'", { '.$htmlname.': request.term }, function(data){
341 response( jQuery.map( data, function( item ) {
342 if (autoselect == 1 && data.length == 1) {
343 jQuery("#'.$htmlname.'").val(item.value);
344 // TODO move this to specific request
345 if (item.states) {
346 jQuery("#state_id").html(item.states);
347 }
348 for (i=0;i<nboffields;i++) {
349 if (item[fields[i]]) { // If defined
350 //alert(item[fields[i]]);
351 jQuery("#" + fields[i]).val(item[fields[i]]);
352 }
353 }
354 }
355 return item
356 }));
357 });
358 },
359 select: function( event, ui ) {
360 needtotrigger = "";
361 for (i=0;i<nboffields;i++) {
362 //alert(fields[i] + " = " + ui.item[fields[i]]);
363 if (fields[i]=="selectcountry_id")
364 {
365 if (ui.item[fields[i]] > 0) // Do not erase country if unknown
366 {
367 oldvalue=jQuery("#" + fields[i]).val();
368 newvalue=ui.item[fields[i]];
369 //alert(oldvalue+" "+newvalue);
370 jQuery("#" + fields[i]).val(ui.item[fields[i]]);
371 if (oldvalue != newvalue) // To force select2 to refresh visible content
372 {
373 needtotrigger="#" + fields[i];
374 }
375
376 // If we set new country and new state, we need to set a new list of state to allow change
377 if (ui.item.states && ui.item["state_id"] != jQuery("#state_id").value) {
378 jQuery("#state_id").html(ui.item.states);
379 }
380 }
381 }
382 else if (fields[i]=="state_id" || fields[i]=="state_id")
383 {
384 if (ui.item[fields[i]] > 0) // Do not erase state if unknown
385 {
386 oldvalue=jQuery("#" + fields[i]).val();
387 newvalue=ui.item[fields[i]];
388 //alert(oldvalue+" "+newvalue);
389 jQuery("#" + fields[i]).val(ui.item[fields[i]]); // This may fails if not correct country
390 if (oldvalue != newvalue) // To force select2 to refresh visible content
391 {
392 needtotrigger="#" + fields[i];
393 }
394 }
395 }
396 else if (ui.item[fields[i]]) { // If defined
397 oldvalue=jQuery("#" + fields[i]).val();
398 newvalue=ui.item[fields[i]];
399 //alert(oldvalue+" "+newvalue);
400 jQuery("#" + fields[i]).val(ui.item[fields[i]]);
401 if (oldvalue != newvalue) // To force select2 to refresh visible content
402 {
403 needtotrigger="#" + fields[i];
404 }
405 }
406
407 if (needtotrigger != "") // To force select2 to refresh visible content
408 {
409 // 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
410 // This is required for example when changing zip with autocomplete that change the country
411 jQuery(needtotrigger).delay(500).queue(function() {
412 jQuery(this).trigger("change");
413 });
414 }
415 }
416 }
417 });
418 });';
419 $script .= '</script>';
420
421 return $script;
422}
423
433function ajax_dialog($title, $message, $w = 350, $h = 150)
434{
435 $newtitle = dol_textishtml($title) ? dol_string_nohtmltag($title, 1) : $title;
436 $msg = '<div id="dialog-info" title="'.dol_escape_htmltag($newtitle).'">';
437 $msg .= $message;
438 $msg .= '</div>'."\n";
439 $msg .= '<script>
440 jQuery(function() {
441 jQuery("#dialog-info").dialog({
442 resizable: false,
443 height:'.$h.',
444 width:'.$w.',
445 modal: true,
446 buttons: {
447 Ok: function() {
448 jQuery(this).dialog(\'close\');
449 }
450 }
451 });
452 });
453 </script>';
454
455 $msg .= "\n";
456
457 return $msg;
458}
459
460
476function ajax_combobox($htmlname, $events = array(), $minLengthToAutocomplete = 0, $forcefocus = 0, $widthTypeOfAutocomplete = 'resolve', $idforemptyvalue = '-1', $morecss = '')
477{
478 global $conf;
479
480 // select2 can be disabled for smartphones
481 if (!empty($conf->browser->layout) && $conf->browser->layout == 'phone' && getDolGlobalString('MAIN_DISALLOW_SELECT2_WITH_SMARTPHONE')) {
482 return '';
483 }
484
485 if (getDolGlobalString('MAIN_DISABLE_AJAX_COMBOX')) {
486 return '';
487 }
488 if (empty($conf->use_javascript_ajax)) {
489 return '';
490 }
491 if (!getDolGlobalString('MAIN_USE_JQUERY_MULTISELECT') && !defined('REQUIRE_JQUERY_MULTISELECT')) {
492 return '';
493 }
494 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
495 return '';
496 }
497
498 if (empty($minLengthToAutocomplete)) {
499 $minLengthToAutocomplete = 0;
500 }
501
502 $moreselect2theme = ($morecss ? dol_escape_js(' '.$morecss) : '');
503 $moreselect2theme = preg_replace('/widthcentpercentminus[^\s]*/', '', $moreselect2theme);
504
505 $tmpplugin = 'select2';
506 $componentname = (preg_match('/^\./', $htmlname) ? $htmlname : '#'.$htmlname); // To support when htmlname is not an ID but a class
507 $msg = "\n";
508 $msg .= '<!-- JS CODE TO ENABLE '.$tmpplugin.' for id = '.$htmlname.' -->'."\n";
509 $msg .= "<script>\n";
510 $msg .= '$(document).ready(function () {
511 $(\''.dol_escape_js($componentname).'\').'.$tmpplugin.'({';
512 // when $morecss contains 'onrightofpage', the select2 component must also be inside a parent with class="parentonrightofpage"
513 if (preg_match('/onrightofpage/', $morecss)) { // In this cas, htmlname must be an ID not a class.
514 $msg .= ' dropdownAutoWidth: true, ';
515 $msg .= ' dropdownParent: $(\'#'.$htmlname.'\').parent(), ';
516 }
517 $msg .= '
518 dir: \'ltr\',
519 width: \''.dol_escape_js($widthTypeOfAutocomplete).'\', /* off or resolve */
520 minimumInputLength: '.((int) $minLengthToAutocomplete).',
521 language: (typeof select2arrayoflanguage === \'undefined\') ? \'en\' : select2arrayoflanguage,
522 matcher: function (params, data) {
523 if ($.trim(params.term) === "") { return data; }';
524 if (getDolGlobalString('MAIN_ENABLE_ACCENT_INSENSITIVE_SEARCH')) { // lowercase + remove accents
525 $msg .= '
526 var term = params.term.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase()
527 var searchText = data.element ? $(data.element).attr("data-search") : undefined;
528 var text = (searchText !== undefined ? searchText : (data.text || "")).normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase() || "";';
529 } else { // lowercase only (accent kept)
530 $msg .= '
531 var term = params.term.toLowerCase();
532 var searchText = data.element ? $(data.element).attr("data-search") : undefined;
533 var text = (searchText !== undefined ? searchText : (data.text || "")).toLowerCase();';
534 }
535 $msg .= '
536 var keywords = term.split(" ");
537 for (var i = 0; i < keywords.length; i++) {
538 if (text.indexOf(keywords[i]) === -1) {
539 return null;
540 }
541 }
542 return data;
543 },
544 theme: \'default'.dol_escape_js($moreselect2theme).'\', /* to add css on generated html components */
545 containerCssClass: \':all:\', /* Line to add class of origin SELECT propagated to the new <span class="select2-selection...> tag */
546 selectionCssClass: \':all:\', /* Line to add class of origin SELECT propagated to the new <span class="select2-selection...> tag */
547 dropdownCssClass: \'ui-dialog\',
548 templateResult: function (data, container) { /* Format visible output into combo list */
549 /* Code to add class of origin OPTION propagated to the new select2 <li> tag */
550 if (data.element) { $(container).addClass($(data.element).attr("class")); }
551 /* console.log("data html is "+$(data.element).attr("data-html")); */
552 if (data.id == \''.(dol_escape_js($idforemptyvalue)).'\' && $(data.element).attr("data-html") == undefined) {
553 return \'&nbsp;\';
554 }
555 if ($(data.element).attr("data-html") != undefined) {
556 /* If property html set, we decode html entities and use this. */
557 /* Note that HTML content must have been sanitized against js injection with dol_escape_htmltag(xxx, 0, 0, \'\', 0, 1) when building the select option. */
558 if (typeof htmlEntityDecodeJs === "function") {
559 return htmlEntityDecodeJs($(data.element).attr("data-html"));
560 }
561 }
562 return data.text;
563 },
564 templateSelection: function (selection) { /* Format visible output of selected value */
565 if (selection.id == \''.(dol_escape_js($idforemptyvalue)).'\') return \'<span class="placeholder">\'+selection.text+\'</span>\';
566 if (selection.element && $(selection.element).attr("data-select-html") != undefined) {
567 if (typeof htmlEntityDecodeJs === "function") {
568 return htmlEntityDecodeJs($(selection.element).attr("data-select-html"));
569 }
570 }
571 return selection.text;
572 },
573 escapeMarkup: function(markup) {
574 return markup;
575 }
576 })';
577 if ($forcefocus) {
578 $msg .= '.select2(\'focus\')';
579 }
580 $msg .= ';'."\n";
581
582 $msg .= '});'."\n";
583 $msg .= "</script>\n";
584
585 $msg .= ajax_event($htmlname, $events);
586
587 return $msg;
588}
589
590
599function ajax_event($htmlname, $events)
600{
601 $out = '';
602
603 if (is_array($events) && count($events)) { // If an array of js events to do were provided.
604 $out = '<!-- JS code to manage event for id = ' . $htmlname . ' -->
605 <script>
606 $(document).ready(function () {
607 jQuery("#'.$htmlname.'").change(function () {
608 var obj = '.json_encode($events) . ';
609 $.each(obj, function(key,values) {
610 if (values.method.length) {
611 runJsCodeForEvent'.$htmlname.'(values);
612 }
613 });
614 });
615 function runJsCodeForEvent'.$htmlname.'(obj) {
616 var id = $("#'.$htmlname.'").val();
617 var method = obj.method;
618 var url = obj.url;
619 var htmlname = obj.htmlname;
620 var showempty = obj.showempty;
621 console.log("Run runJsCodeForEvent-'.$htmlname.' from ajax_combobox id="+id+" method="+method+" showempty="+showempty+" url="+url+" htmlname="+htmlname);
622 // The token is required because the request carries an "action" parameter: with
623 // MAIN_SECURITY_CSRF_WITH_TOKEN >= 3 (the default), such a GET is refused without it.
624 // currentToken() is used and not newToken() because the called pages set NOTOKENRENEWAL.
625 $.getJSON(url,
626 {
627 action: method,
628 id: id,
629 htmlname: htmlname,
630 showempty: showempty,
631 token: \''.currentToken().'\'
632 },
633 function(response) {
634 $.each(obj.params, function(key,action) {
635 if (key.length) {
636 var num = response.num;
637 if (num > 0) {
638 $("#" + key).removeAttr(action);
639 } else {
640 $("#" + key).attr(action, action);
641 }
642 }
643 });
644
645 console.log("Replace HTML content of select#"+htmlname);
646 $("select#" + htmlname).html(response.value);
647 if (response.num) {
648 var selecthtml_str = response.value; /* response.value is the HTML string with list of options */
649 var selecthtml_dom=$.parseHTML(selecthtml_str);
650 if (typeof(selecthtml_dom[0][0]) !== \'undefined\') {
651 $("#inputautocomplete"+htmlname).val(selecthtml_dom[0][0].innerHTML);
652 }
653 } else {
654 $("#inputautocomplete"+htmlname).val("");
655 }
656 $("select#" + htmlname).change(); /* Trigger event change */
657 }
658 );
659 }
660 });
661 </script>';
662 }
663
664 return $out;
665}
666
667
689function ajax_constantonoff($code, $input = array(), $entity = null, $revertonoff = 0, $strict = 0, $forcereload = 0, $marginleftonlyshort = 2, $forcenoajax = 0, $setzeroinsteadofdel = 0, $suffix = '', $mode = '', $morecss = 'inline-block', $userconst = 0, $showwarning = '', $disabled = 0)
690{
691 global $conf, $langs, $user, $db;
692
693 $entity = ((isset($entity) && is_numeric($entity) && $entity >= 0) ? $entity : $conf->entity);
694 if (!isset($input)) {
695 $input = array();
696 }
697
698 $out = '';
699
700 if (empty($conf->use_javascript_ajax) || $forcenoajax) {
701 if (!getDolGlobalString($code)) {
702 $out = '<a '.($morecss ? 'class="'.$morecss.'" ' : '').'href="'.$_SERVER['PHP_SELF'].'?action=set_'.$code.'&token='.newToken().'&entity='.$entity.($mode ? '&mode='.$mode : '').($forcereload ? '&dol_resetcache=1' : '').'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
703 } else {
704 $out = '<a '.($morecss ? 'class="'.$morecss.'" ' : '').' href="'.$_SERVER['PHP_SELF'].'?action=del_'.$code.'&token='.newToken().'&entity='.$entity.($mode ? '&mode='.$mode : '').($forcereload ? '&dol_resetcache=1' : '').'">'.img_picto($langs->trans("Enabled"), 'on').'</a>';
705 }
706 } else {
707 $userconstid = 0;
708 if (is_object($userconst)) {
709 $userconstid = $userconst->id;
710 } elseif (is_numeric($userconst) && $userconst > 0) {
711 $userconstid = $userconst;
712 $userconst = new User($db);
713 $userconst->fetch($userconstid);
714 }
715
716 if ($disabled) {
717 $morecss .= ' disabled opacitymedium';
718 } else {
719 $out = "\n<!-- Ajax code to switch constant ".$code." -->".'
720 <script>
721 $(document).ready(function() {
722 var input = '.json_encode($input).';
723 var url = \''.DOL_URL_ROOT.'/core/ajax/constantonoff.php\';
724 var code = \''.dol_escape_js($code).'\';
725 var entity = \''.dol_escape_js((string) $entity).'\';
726 var strict = \''.dol_escape_js((string) $strict).'\';
727 var userid = \''.dol_escape_js((string) $user->id).'\';
728 var userconst = '.((int) $userconstid).';
729 var yesButton = \''.dol_escape_js($langs->transnoentities("Yes")).'\';
730 var noButton = \''.dol_escape_js($langs->transnoentities("No")).'\';
731 var token = \''.currentToken().'\';
732 var warning = \''.dol_escape_js($showwarning).'\';
733
734 // Set constant
735 $("#set_" + code).click(function() {
736 if (warning) {
737 alert(warning);
738 }
739
740 if (input.alert && input.alert.set) {
741 if (input.alert.set.yesButton) yesButton = input.alert.set.yesButton;
742 if (input.alert.set.noButton) noButton = input.alert.set.noButton;
743 confirmConstantAction("set", url, code, input, input.alert.set, entity, yesButton, noButton, strict, userid, token);
744 } else {
745 setConstant(url, code, input, entity, 0, '.((int) $forcereload).', userid, token, 1, userconst);
746 }
747 });
748
749 // Del constant
750 $("#del_" + code).click(function() {
751 if (input.alert && input.alert.del) {
752 if (input.alert.del.yesButton) yesButton = input.alert.del.yesButton;
753 if (input.alert.del.noButton) noButton = input.alert.del.noButton;
754 confirmConstantAction("del", url, code, input, input.alert.del, entity, yesButton, noButton, strict, userid, token);
755 } else {';
756 if (empty($setzeroinsteadofdel)) {
757 $out .= ' delConstant(url, code, input, entity, 0, '.((int) $forcereload).', userid, token, userconst);';
758 } else {
759 $out .= ' setConstant(url, code, input, entity, 0, '.((int) $forcereload).', userid, token, 0, userconst);';
760 }
761 $out .= ' }
762 });
763 });
764 </script>'."\n";
765 }
766
767 if (!empty($userconst) && $userconst instanceof User) {
768 $value = getDolUserString($code, '', $userconst);
769 } else {
770 $value = getDolGlobalString($code);
771 }
772
773 if (is_array($suffix)) {
774 $suffixon = $suffix['ifon'];
775 $suffixoff = $suffix['ifoff'];
776 } else { // old mode deprecated
777 $suffixon = (string) $suffix;
778 $suffixoff = '';
779 }
780
781 $out .= '<div id="confirm_'.$code.'" title="" style="display: none;"></div>';
782 $out .= '<span id="set_'.$code.'" class="valignmiddle inline-block linkobject '.($value ? 'hideobject' : '').($morecss ? ' '.$morecss : '').'">'.($revertonoff ? img_picto($langs->trans("Enabled"), 'switch_on'.$suffixoff, '', 0, 0, 0, '', '', $marginleftonlyshort) : img_picto($langs->trans("Disabled"), 'switch_off'.$suffixoff, '', 0, 0, 0, '', '', $marginleftonlyshort)).'</span>';
783 $out .= '<span id="del_'.$code.'" class="valignmiddle inline-block linkobject '.($value ? '' : 'hideobject').($morecss ? ' '.$morecss : '').'">'.($revertonoff ? img_picto($langs->trans("Disabled"), 'switch_off'.$suffixon, '', 0, 0, 0, '', '', $marginleftonlyshort) : img_picto($langs->trans("Enabled"), 'switch_on'.$suffixon, '', 0, 0, 0, '', '', $marginleftonlyshort)).'</span>';
784 $out .= "\n";
785 }
786
787 return $out;
788}
789
808function ajax_object_onoff($object, $code, $field, $text_on, $text_off, $input = array(), $morecss = '', $htmlname = '', $forcenojs = 0, $moreparam = '', $readonly = 0)
809{
810 global $conf, $langs;
811
812 if (empty($htmlname)) {
813 $htmlname = $code;
814 }
815
816 $out = '';
817
818 if (!empty($conf->use_javascript_ajax) && empty($forcenojs) && empty($readonly)) {
819 $out .= '<script>
820 $(function() {
821 var input = '.json_encode($input).';
822
823 // Set constant
824 $("#set_'.$htmlname.'_'.$object->id.'").click(function() {
825 console.log("Click managed by ajax_object_onoff");
826 $.get( "'.DOL_URL_ROOT.'/core/ajax/objectonoff.php", {
827 action: \'set\',
828 field: \''.dol_escape_js($field).'\',
829 value: \'1\',
830 element: \''.dol_escape_js((empty($object->module) || $object->module == $object->element) ? $object->element : $object->element.'@'.$object->module).'\',
831 id: \''.((int) $object->id).'\',
832 token: \''.currentToken().'\'
833 }).done(
834 function(response) {
835 try {
836 var data = JSON.parse(response);
837 console.log(data);
838 } catch (e) {
839 console.log(response);
840 }
841
842 $("#set_'.$htmlname.'_'.$object->id.'").hide();
843 $("#del_'.$htmlname.'_'.$object->id.'").show();
844 // Enable another element
845 if (input.disabled && input.disabled.length > 0) {
846 $.each(input.disabled, function(key,value) {
847 $("#" + value).removeAttr("disabled");
848 if ($("#" + value).hasClass("butActionRefused") == true) {
849 $("#" + value).removeClass("butActionRefused");
850 $("#" + value).addClass("butAction");
851 }
852 });
853 // Show another element
854 } else if (input.showhide && input.showhide.length > 0) {
855 $.each(input.showhide, function(key,value) {
856 $("#" + value).show();
857 });
858 }
859 }).fail(
860 function(response) {
861 //alert(response.responseText);
862 console.warn(response.responseText);
863 Dolibarr.tools.setEventMessage(response.responseText, "errors");
864 }
865 );
866 });
867
868 // Del constant
869 $("#del_'.$htmlname.'_'.$object->id.'").click(function() {
870 console.log("Click managed by ajax_object_onoff");
871 $.get( "'.DOL_URL_ROOT.'/core/ajax/objectonoff.php", {
872 action: \'set\',
873 field: \''.dol_escape_js($field).'\',
874 value: \'0\',
875 element: \''.dol_escape_js((empty($object->module) || $object->module == $object->element) ? $object->element : $object->element.'@'.$object->module).'\',
876 id: \''.((int) $object->id).'\',
877 token: \''.currentToken().'\'
878 }).done(
879 function(response) {
880 try {
881 var data = JSON.parse(response);
882 console.log(data);
883 } catch (e) {
884 console.log(response);
885 }
886
887 $("#del_'.$htmlname.'_'.$object->id.'").hide();
888 $("#set_'.$htmlname.'_'.$object->id.'").show();
889 // Disable another element
890 if (input.disabled && input.disabled.length > 0) {
891 $.each(input.disabled, function(key,value) {
892 $("#" + value).prop("disabled", true);
893 if ($("#" + value).hasClass("butAction") == true) {
894 $("#" + value).removeClass("butAction");
895 $("#" + value).addClass("butActionRefused");
896 }
897 });
898 // Hide another element
899 } else if (input.showhide && input.showhide.length > 0) {
900 $.each(input.showhide, function(key,value) {
901 $("#" + value).hide();
902 });
903 }
904 }).fail(
905 function(response) {
906 //alert(response.responseText);
907 console.warn(response.responseText);
908 Dolibarr.tools.setEventMessage(response.responseText, "errors");
909 }
910 );
911 });
912 });
913 </script>';
914 }
915
916 $switchon = 'switch_on';
917 $switchoff = 'switch_off';
918 $cssswitchon = '';
919 $cssswitchoff = '';
920 $tmparray = explode(':', $text_on);
921 if (!empty($tmparray[1])) {
922 $text_on = $tmparray[0];
923 $switchon = $tmparray[1];
924 if (!empty($tmparray[2])) {
925 $cssswitchon = $tmparray[2];
926 }
927 }
928 $tmparray = explode(':', $text_off);
929 if (!empty($tmparray[1])) {
930 $text_off = $tmparray[0];
931 $switchoff = $tmparray[1];
932 if (!empty($tmparray[2])) {
933 $cssswitchoff = $tmparray[2];
934 }
935 }
936
937 if (empty($conf->use_javascript_ajax) || $forcenojs) {
938 $url = DOL_URL_ROOT.'/core/ajax/objectonoff.php?action=set&token='.newToken().'&id='.((int) $object->id).'&element='.urlencode($object->element).'&field='.urlencode($field).'&value=1&backtopage='.urlencode($_SERVER["PHP_SELF"].'?id='.$object->id.($moreparam ? '&'.$moreparam : ''));
939 if ($readonly) {
940 $url = '#';
941 }
942 $out .= '<a id="set_'.$htmlname.'_'.$object->id.'" class="linkobject '.($object->$code == 1 ? 'hideobject' : '').($morecss ? ' '.$morecss : '').'" href="'.$url.'">'.img_picto($langs->trans($text_off), $switchoff, '', 0, 0, 0, '', $cssswitchoff).'</a>';
943 $url = DOL_URL_ROOT.'/core/ajax/objectonoff.php?action=set&token='.newToken().'&id='.((int) $object->id).'&element='.urlencode($object->element).'&field='.urlencode($field).'&value=0&backtopage='.urlencode($_SERVER["PHP_SELF"].'?id='.$object->id.($moreparam ? '&'.$moreparam : ''));
944 if ($readonly) {
945 $url = '#';
946 }
947 $out .= '<a id="del_'.$htmlname.'_'.$object->id.'" class="linkobject '.($object->$code == 1 ? '' : 'hideobject').($morecss ? ' '.$morecss : '').'" href="'.$url.'">'.img_picto($langs->trans($text_on), $switchon, '', 0, 0, 0, '', $cssswitchon).'</a>';
948 } else {
949 $out .= '<span id="set_'.$htmlname.'_'.$object->id.'" class="linkobject '.($object->$code == 1 ? 'hideobject' : '').($morecss ? ' '.$morecss : '').'">'.img_picto($langs->trans($text_off), $switchoff, '', 0, 0, 0, '', $cssswitchoff).'</span>';
950 $out .= '<span id="del_'.$htmlname.'_'.$object->id.'" class="linkobject '.($object->$code == 1 ? '' : 'hideobject').($morecss ? ' '.$morecss : '').'">'.img_picto($langs->trans($text_on), $switchon, '', 0, 0, 0, '', $cssswitchon).'</span>';
951 }
952
953 return $out;
954}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
ajax_autocompleter($selected, $htmlname, $url, $urloption='', $minLength=2, $autoselect=0, $ajaxoptions=array(), $moreparams='')
Generic function that return javascript to add to transform a common input text or select field into ...
Definition ajax.lib.php:50
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Definition ajax.lib.php:476
ajax_dialog($title, $message, $w=350, $h=150)
Show an ajax dialog.
Definition ajax.lib.php:433
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 text field into ...
Definition ajax.lib.php:325
ajax_event($htmlname, $events)
Add event management script.
Definition ajax.lib.php:599
Class to manage Dolibarr users.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
currentToken()
Return the value of token currently saved into session with name 'token'.
dol_escape_js($stringtoescape, $mode=0, $noescapebackslashn=0)
Returns text escaped for inclusion into JavaScript code.
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.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
if(empty( $takeposterminal)) fail($message)
Abort invoice creation with a given error message.
Definition invoice.php:124