48function ajax_autocompleter($selected, $htmlname, $url, $urloption =
'', $minLength = 2, $autoselect = 0, $ajaxoptions = array(), $moreparams =
'')
50 if (empty($minLength)) {
54 $dataforrenderITem =
'ui-autocomplete';
55 $dataforitem =
'ui-autocomplete-item';
57 if (defined(
'JS_QUERY_AUTOCOMPLETE_RENDERITEM')) {
58 $dataforrenderITem = constant(
'JS_QUERY_AUTOCOMPLETE_RENDERITEM');
60 if (defined(
'JS_QUERY_AUTOCOMPLETE_ITEM')) {
61 $dataforitem = constant(
'JS_QUERY_AUTOCOMPLETE_ITEM');
64 $htmlnamejquery = str_replace(
'.',
'\\\\.', $htmlname);
68 $script =
'<input type="hidden" name="'.$htmlname.
'" id="'.$htmlname.
'" value="'.$selected.
'" '.($moreparams ? $moreparams :
'').
' />';
70 $script .=
'<!-- Javascript code for autocomplete of field '.$htmlname.
' -->'.
"\n";
71 $script .=
'<script>'.
"\n";
72 $script .=
'$(document).ready(function() {
73 var autoselect = '.((int) $autoselect).
';
74 var options = '.json_encode($ajaxoptions).
'; /* Option of actions to do after keyup, or after select */
76 /* 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 */
77 $("input#search_'.$htmlnamejquery.
'").keydown(function(e) {
78 if (e.keyCode != 9) /* If not "Tab" key */
80 if (e.keyCode == 13) { return false; } /* disable "ENTER" key useful for barcode readers */
81 console.log("Clear id previously selected for field '.$htmlname.
'");
82 $("#'.$htmlnamejquery.
'").val("");
86 // Check options for secondary actions when keyup
87 $("input#search_'.$htmlnamejquery.
'").keyup(function() {
88 if ($(this).val().length == 0)
90 $("#search_'.$htmlnamejquery.
'").val("");
91 $("#'.$htmlnamejquery.
'").val("").trigger("change");
92 if (options.option_disabled) {
93 $("#" + options.option_disabled).removeAttr("disabled");
95 if (options.disabled) {
96 $.each(options.disabled, function(key, value) {
97 $("#" + value).removeAttr("disabled");
100 if (options.update) {
101 $.each(options.update, function(key, value) {
102 $("#" + key).val("").trigger("change");
106 $.each(options.show, function(key, value) {
107 $("#" + value).hide().trigger("hide");
110 if (options.update_textarea) {
111 $.each(options.update_textarea, function(key, value) {
112 if (typeof CKEDITOR == "object" && typeof CKEDITOR.instances != "undefined" && CKEDITOR.instances[key] != "undefined") {
113 CKEDITOR.instances[key].setData("");
115 $("#" + key).html("");
122 // Activate the autocomplete to execute the GET
123 $("input#search_'.$htmlnamejquery.
'").autocomplete({
124 source: function( request, response ) {
125 $.get("'.$url.($urloption ?
'?'.$urloption :
'').
'", { "'.str_replace(
'.',
'_', $htmlname).
'": request.term }, function(data){
128 response($.map( data, function(item) {
129 if (autoselect == 1 && data.length == 1) {
130 $("#search_'.$htmlnamejquery.
'").val(item.value);
131 $("#'.$htmlnamejquery.
'").val(item.key).trigger("change");
134 if (item.label != null) {
135 label = item.label.toString();
138 if (options.update) {
139 $.each(options.update, function(key, value) {
140 update[key] = item[value];
144 if (options.update_textarea) {
145 $.each(options.update_textarea, function(key, value) {
146 textarea[key] = item[value];
150 console.log("Return value from GET to the rest of code");
151 return { label: label,
154 disabled: item.disabled,
160 discount: item.discount,
161 pricebasetype: item.pricebasetype,
162 price_ht: item.price_ht,
163 price_ttc: item.price_ttc,
164 price_unit_ht: item.price_unit_ht,
165 price_unit_ht_locale: item.price_unit_ht_locale,
166 multicurrency_code: item.multicurrency_code,
167 multicurrency_unitprice: item.multicurrency_unitprice,
168 description : item.description,
169 ref_customer: item.ref_customer,
171 default_vat_code: item.default_vat_code,
172 supplier_ref: item.supplier_ref
176 console.error("Error: Ajax url '.$url.($urloption ?
'?'.$urloption :
'').
' has returned an empty page. Should be an empty json array.");
181 minLength: '.((
int) $minLength).
',
182 select: function( event, ui ) { // Function ran once a new value has been selected into the javascript combo
183 console.log("We will trigger change on input '.$htmlname.
' because of the select definition of autocomplete code for input#search_'.$htmlname.
'");
184 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");
186 console.log("Before, we propagate some properties, retrieved by the ajax of the get, into the data-xxx properties of the component #'.$htmlnamejquery.
'");
187 //console.log(ui.item);
189 // For supplier price and customer when price by quantity is off
190 $("#'.$htmlnamejquery.
'").attr("data-up", ui.item.price_ht);
191 $("#'.$htmlnamejquery.
'").attr("data-up-locale", ui.item.price_unit_ht_locale);
192 $("#'.$htmlnamejquery.
'").attr("data-base", ui.item.pricebasetype);
193 $("#'.$htmlnamejquery.
'").attr("data-qty", ui.item.qty);
194 $("#'.$htmlnamejquery.
'").attr("data-discount", ui.item.discount);
195 $("#'.$htmlnamejquery.
'").attr("data-description", ui.item.description);
196 $("#'.$htmlnamejquery.
'").attr("data-ref-customer", ui.item.ref_customer);
197 $("#'.$htmlnamejquery.
'").attr("data-tvatx", ui.item.tva_tx);
198 $("#'.$htmlnamejquery.
'").attr("data-default-vat-code", ui.item.default_vat_code);
199 $("#'.$htmlnamejquery.
'").attr("data-supplier-ref", ui.item.supplier_ref); // supplier_ref of price
201 // For multi-currency values
202 $("#'.$htmlnamejquery.
'").attr("data-multicurrency-code", ui.item.multicurrency_code);
203 $("#'.$htmlnamejquery.
'").attr("data-multicurrency-unitprice", ui.item.multicurrency_unitprice);
207 // For customer price when PRODUIT_CUSTOMER_PRICES_BY_QTY is on
208 console.log("PRODUIT_CUSTOMER_PRICES_BY_QTY is on, so we propagate also prices by quantity into data-pbqxxx properties");
209 $("#'.$htmlnamejquery.
'").attr("data-pbq", ui.item.pbq);
210 $("#'.$htmlnamejquery.
'").attr("data-pbqup", ui.item.price_ht);
211 $("#'.$htmlnamejquery.
'").attr("data-pbqbase", ui.item.pricebasetype);
212 $("#'.$htmlnamejquery.
'").attr("data-pbqqty", ui.item.qty);
213 $("#'.$htmlnamejquery.
'").attr("data-pbqpercent", ui.item.discount);
217 // A new value has been selected, we trigger the handlers on #htmlnamejquery
218 console.log("Now, we trigger changes on #'.$htmlnamejquery.
'");
219 $("#'.$htmlnamejquery.
'").val(ui.item.id).trigger("change"); // Select new value
221 // Complementary actions
223 // Disable an element
224 if (options.option_disabled) {
225 console.log("Make action option_disabled on #"+options.option_disabled+" with disabled="+ui.item.disabled)
226 if (ui.item.disabled) {
227 $("#" + options.option_disabled).prop("disabled", true);
229 $.jnotify(options.error, "error", true); // Output with jnotify the error message
231 if (options.warning) {
232 $.jnotify(options.warning, "warning", false); // Output with jnotify the warning message
235 $("#" + options.option_disabled).removeAttr("disabled");
239 if (options.disabled) {
240 console.log("Make action \'disabled\' on each "+options.option_disabled)
241 $.each(options.disabled, function(key, value) {
242 $("#" + value).prop("disabled", true);
246 console.log("Make action \'show\' on each "+options.show)
247 $.each(options.show, function(key, value) {
248 $("#" + value).show().trigger("show");
253 if (ui.item.update) {
254 console.log("Make action \'update\' on each ui.item.update (if there is)")
255 // loop on each "update" fields
256 $.each(ui.item.update, function(key, value) {
257 console.log("Set value "+value+" into #"+key);
258 $("#" + key).val(value).trigger("change");
261 if (ui.item.textarea) {
262 console.log("Make action \'textarea\' on each ui.item.textarea (if there is)")
263 $.each(ui.item.textarea, function(key, value) {
264 if (typeof CKEDITOR == "object" && typeof CKEDITOR.instances != "undefined" && CKEDITOR.instances[key] != "undefined") {
265 CKEDITOR.instances[key].setData(value);
266 CKEDITOR.instances[key].focus();
268 $("#" + key).html(value);
269 $("#" + key).focus();
273 console.log("ajax_autocompleter new value selected, we trigger change also on original component so on field #search_'.$htmlname.
'");
275 $("#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.
278 }).data("'.$dataforrenderITem.
'")._renderItem = function( ul, item ) {
280 .data( "'.$dataforitem.
'", item ) // jQuery UI > 1.10.0
281 .append( \'<a><span class="tag">\' + item.label + "</span></a>" )
286 $script .=
'</script>';
456function ajax_combobox($htmlname, $events = array(), $minLengthToAutocomplete = 0, $forcefocus = 0, $widthTypeOfAutocomplete =
'resolve', $idforemptyvalue =
'-1', $morecss =
'')
461 if (!empty($conf->browser->layout) && $conf->browser->layout ==
'phone' &&
getDolGlobalString(
'MAIN_DISALLOW_SELECT2_WITH_SMARTPHONE')) {
468 if (empty($conf->use_javascript_ajax)) {
471 if (!
getDolGlobalString(
'MAIN_USE_JQUERY_MULTISELECT') && !defined(
'REQUIRE_JQUERY_MULTISELECT')) {
478 if (empty($minLengthToAutocomplete)) {
479 $minLengthToAutocomplete = 0;
482 $moreselect2theme = ($morecss ?
dol_escape_js(
' '.$morecss) :
'');
483 $moreselect2theme = preg_replace(
'/widthcentpercentminus[^\s]*/',
'', $moreselect2theme);
485 $tmpplugin =
'select2';
486 $msg =
"\n".
'<!-- JS CODE TO ENABLE '.$tmpplugin.
' for id = '.$htmlname.
' -->
488 $(document).ready(function () {
489 $(\''.(preg_match(
'/^\./', $htmlname) ? $htmlname :
'#'.$htmlname).
'\').
'.$tmpplugin.'({
491 if (preg_match(
'/onrightofpage/', $morecss)) {
492 $msg .=
' dropdownAutoWidth: true, dropdownParent: $(\'#'.$htmlname.
'\').parent(),
'."\n";
494 $msg .= ' width: \
''.dol_escape_js($widthTypeOfAutocomplete).
'\',
495 minimumInputLength:
'.((int) $minLengthToAutocomplete).',
496 language: select2arrayoflanguage,
497 matcher:
function (params, data) {
498 if ($.trim(params.term) ===
"") {
501 keywords = (params.term).split(
" ");
502 for (var i = 0; i < keywords.length; i++) {
503 if (((data.text).toUpperCase()).indexOf((keywords[i]).toUpperCase()) == -1) {
509 theme: \
'default'.$moreselect2theme.
'\',
510 containerCssClass: \
':all:\', /* Line to add class of origin SELECT propagated to the new <span class="select2-selection...> tag */
511 selectionCssClass: \':all:\', /* Line to add class of origin SELECT propagated to the new <span class="select2-selection...> tag */
512 dropdownCssClass: \'ui-dialog\',
513 templateResult: function (data, container) { /* Format visible output into combo list */
514 /* Code to add class of origin OPTION propagated to the new select2 <li> tag */
515 if (data.element) { $(container).addClass($(data.element).attr("class")); }
516 //console.log("data html is "+$(data.element).attr("data-html"));
517 if (data.id == '.((int) $idforemptyvalue).
' && $(data.element).attr("data-html") == undefined) {
520 if ($(data.element).attr("data-html") != undefined) {
521 /* If property html set, we decode html entities and use this. */
522 /* Note that HTML content must have been sanitized from js with dol_escape_htmltag(xxx, 0, 0, \'\', 0, 1) when building the select option. */
523 if (typeof htmlEntityDecodeJs === "function") {
524 return htmlEntityDecodeJs($(data.element).attr("data-html"));
529 templateSelection: function (selection) { /* Format visible output of selected value */
530 if (selection.id == '.((int) $idforemptyvalue).
') return \'<span class="placeholder">\'+selection.text+\'</span>\';
531 return selection.text;
533 escapeMarkup: function(markup) {
538 $msg .=
'.select2(\'focus\')';
543 $msg .=
"</script>\n";
642function ajax_constantonoff($code, $input = array(), $entity =
null, $revertonoff = 0, $strict = 0, $forcereload = 0, $marginleftonlyshort = 2, $forcenoajax = 0, $setzeroinsteadofdel = 0, $suffix =
'', $mode =
'', $morecss =
'inline-block')
644 global $conf, $langs, $user;
646 $entity = ((isset($entity) && is_numeric($entity) && $entity >= 0) ? $entity : $conf->entity);
647 if (!isset($input)) {
651 if (empty($conf->use_javascript_ajax) || $forcenoajax) {
652 if (empty($conf->global->$code)) {
653 $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>';
655 $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>';
658 $out =
"\n<!-- Ajax code to switch constant ".$code.
" -->".
'
660 $(document).ready(function() {
661 var input = '.json_encode($input).
';
662 var url = \''.DOL_URL_ROOT.
'/core/ajax/constantonoff.php\';
664 var entity = \
''.dol_escape_js($entity).
'\';
665 var strict = \
''.dol_escape_js($strict).
'\';
666 var userid = \
''.dol_escape_js($user->id).
'\';
667 var yesButton = \
''.dol_escape_js($langs->transnoentities(
"Yes")).
'\';
668 var noButton = \
''.dol_escape_js($langs->transnoentities(
"No")).
'\';
669 var token = \
''.currentToken().
'\';
672 $(
"#set_" + code).click(
function() {
673 if (input.alert && input.alert.set) {
674 if (input.alert.set.yesButton) yesButton = input.alert.set.yesButton;
675 if (input.alert.set.noButton) noButton = input.alert.set.noButton;
676 confirmConstantAction(
"set", url, code, input, input.alert.set, entity, yesButton, noButton, strict, userid, token);
678 setConstant(url, code, input, entity, 0,
'.((int) $forcereload).', userid, token);
683 $(
"#del_" + code).click(
function() {
684 if (input.alert && input.alert.del) {
685 if (input.alert.del.yesButton) yesButton = input.alert.del.yesButton;
686 if (input.alert.del.noButton) noButton = input.alert.del.noButton;
687 confirmConstantAction(
"del", url, code, input, input.alert.del, entity, yesButton, noButton, strict, userid, token);
689 if (empty($setzeroinsteadofdel)) {
690 $out .= ' delConstant(url, code, input, entity, 0,
'.((int) $forcereload).', userid, token);
';
692 $out .= ' setConstant(url, code, input, entity, 0,
'.((int) $forcereload).', userid, token, 0);
';
699 $out .= '<div
id=
"confirm_'.$code.'" title=
"" style=
"display: none;"></div>
';
700 $out .= '<span
id=
"set_'.$code.'" class=
"valignmiddle inline-block 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>
';
701 $out .= '<span
id=
"del_'.$code.'" class=
"valignmiddle inline-block linkobject '.(!empty($conf->global->$code) ? '' : 'hideobject').'">
'.($revertonoff ? img_picto($langs->trans("Disabled"), 'switch_off
'.$suffix, '', false, 0, 0, '', '', $marginleftonlyshort) : img_picto($langs->trans("Enabled"), 'switch_on
'.$suffix, '', false, 0, 0, '', '', $marginleftonlyshort)).'</span>
';
725function ajax_object_onoff($object, $code, $field, $text_on, $text_off, $input = array(), $morecss = '
', $htmlname = '', $forcenojs = 0, $moreparam = '')
727 global $conf, $langs;
729 if (empty($htmlname)) {
732 //var_dump($object->module); var_dump($object->element);
736 if (!empty($conf->use_javascript_ajax) && empty($forcenojs)) {
739 var input =
'.json_encode($input).';
742 $(
"#set_'.$htmlname.'_'.$object->id.'").click(
function() {
743 console.log(
"Click managed by ajax_object_onoff");
744 $.get(
"'.DOL_URL_ROOT.'/core/ajax/objectonoff.php", {
746 field: \''.dol_escape_js($field).
'\',
749 id: \
''.((int)
$object->id).
'\',
750 token: \
''.currentToken().
'\'
753 $(
"#set_'.$htmlname.'_'.$object->id.'").hide();
754 $(
"#del_'.$htmlname.'_'.$object->id.'").show();
756 if (input.disabled && input.disabled.length > 0) {
757 $.each(input.disabled,
function(key,value) {
758 $(
"#" + value).removeAttr(
"disabled");
759 if ($(
"#" + value).hasClass(
"butActionRefused") == true) {
760 $(
"#" + value).removeClass(
"butActionRefused");
761 $(
"#" + value).addClass(
"butAction");
765 }
else if (input.showhide && input.showhide.length > 0) {
766 $.each(input.showhide,
function(key,value) {
767 $(
"#" + value).show();
774 $(
"#del_'.$htmlname.'_'.$object->id.'").click(
function() {
775 console.log(
"Click managed by ajax_object_onoff");
776 $.get(
"'.DOL_URL_ROOT.'/core/ajax/objectonoff.php", {
778 field: \''.dol_escape_js($field).
'\',
781 id: \''.((int)
$object->id).
'\',
785 $("#del_'.$htmlname.
'_'.
$object->id.
'").hide();
786 $("#set_'.$htmlname.
'_'.
$object->id.
'").show();
787 // Disable another element
788 if (input.disabled && input.disabled.length > 0) {
789 $.each(input.disabled, function(key,value) {
790 $("#" + value).prop("disabled", true);
791 if ($("#" + value).hasClass("butAction") == true) {
792 $("#" + value).removeClass("butAction");
793 $("#" + value).addClass("butActionRefused");
796 // Hide another element
797 } else if (input.showhide && input.showhide.length > 0) {
798 $.each(input.showhide, function(key,value) {
799 $("#" + value).hide();
808 $switchon =
'switch_on';
809 $switchoff =
'switch_off';
812 $tmparray = explode(
':', $text_on);
813 if (!empty($tmparray[1])) {
814 $text_on = $tmparray[0];
815 $switchon = $tmparray[1];
816 if (!empty($tmparray[2])) {
817 $cssswitchon = $tmparray[2];
820 $tmparray = explode(
':', $text_off);
821 if (!empty($tmparray[1])) {
822 $text_off = $tmparray[0];
823 $switchoff = $tmparray[1];
824 if (!empty($tmparray[2])) {
825 $cssswitchoff = $tmparray[2];
829 if (empty($conf->use_javascript_ajax) || $forcenojs) {
830 $out .=
'<a id="set_'.$htmlname.
'_'.
$object->id.
'" class="linkobject '.(
$object->$code == 1 ?
'hideobject' :
'').($morecss ?
' '.$morecss :
'').
'" href="'.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 :
'')).
'">'.
img_picto($langs->trans($text_off), $switchoff,
'', false, 0, 0,
'', $cssswitchoff).
'</a>';
831 $out .=
'<a id="del_'.$htmlname.
'_'.
$object->id.
'" class="linkobject '.(
$object->$code == 1 ?
'' :
'hideobject').($morecss ?
' '.$morecss :
'').
'" href="'.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 :
'')).
'">'.
img_picto($langs->trans($text_on), $switchon,
'', false, 0, 0,
'', $cssswitchon).
'</a>';
833 $out .=
'<span id="set_'.$htmlname.
'_'.
$object->id.
'" class="linkobject '.(
$object->$code == 1 ?
'hideobject' :
'').($morecss ?
' '.$morecss :
'').
'">'.
img_picto($langs->trans($text_off), $switchoff,
'', false, 0, 0,
'', $cssswitchoff).
'</span>';
834 $out .=
'<span id="del_'.$htmlname.
'_'.
$object->id.
'" class="linkobject '.(
$object->$code == 1 ?
'' :
'hideobject').($morecss ?
' '.$morecss :
'').
'">'.
img_picto($langs->trans($text_on), $switchon,
'', false, 0, 0,
'', $cssswitchon).
'</span>';
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 ...
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
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 ...
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
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.