2007-12-29 18:06:53 +01:00
< ? php
2010-10-03 23:43:03 +02:00
/* Copyright ( C ) 2007 - 2010 Laurent Destailleur < eldy @ users . sourceforge . net >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2007 - 2015 Regis Houssin < regis . houssin @ inodbox . com >
2012-07-18 15:17:04 +02:00
* Copyright ( C ) 2012 Christophe Battarel < christophe . battarel @ altairis . fr >
2025-01-30 22:20:24 +01:00
* Copyright ( C ) 2024 - 2025 MDW < mdeweerd @ users . noreply . github . com >
2024-09-05 16:05:37 +02:00
* Copyright ( C ) 2024 Frédéric France < frederic . france @ free . fr >
2007-12-29 18:06:53 +01:00
*
* 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
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2007-12-29 18:06:53 +01:00
* ( 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
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
* or see https :// www . gnu . org /
2009-03-28 20:11:38 +01:00
*/
/**
2011-10-24 09:49:50 +02:00
* \file htdocs / core / lib / ajax . lib . php
2020-05-14 16:52:21 +02:00
* \brief Page called to enhance interface with Javascript and Ajax features .
2007-12-29 18:06:53 +01:00
*/
/**
2024-05-08 14:23:39 +02:00
* Generic function that return javascript to add to transform a common input text or select field into an autocomplete field by calling an Ajax page ( ex : / societe / ajax / ajaxcompanies . php ) .
2020-08-07 14:56:30 +02:00
* The HTML field must be an input text with id = search_ $htmlname .
2024-05-08 14:23:39 +02:00
* This use the jQuery " autocomplete " function . If we want to use the select2 , we must instead use input select into functions that call this method .
2011-11-09 22:10:58 +01:00
*
2020-08-07 14:56:30 +02:00
* @ param string $selected Preselected value
* @ param string $htmlname HTML name of input field
* @ param string $url Ajax Url to call for request : / path / page . php . Must return a json array ( 'key' => id , 'value' => String shown into input field once selected , 'label' => String shown into combo list )
* @ param string $urloption More parameters on URL request
* @ param int $minLength Minimum number of chars to trigger that Ajax search
2021-09-07 14:10:26 +02:00
* @ param int $autoselect Automatic selection if just one value ( trigger ( " change " ) on field is done if search return only 1 result )
2024-03-30 12:57:07 +01:00
* @ param array < string , string | string [] > $ajaxoptions Multiple options array
* - Ex : array ( 'update' => array ( 'field1' , 'field2' ... )) will reset field1 and field2 once select done
* - Ex : array ( 'disabled' => )
* - Ex : array ( 'show' => )
* - Ex : array ( 'update_textarea' => )
* - Ex : array ( 'option_disabled' => id to disable and warning to show if we select a disabled value ( this is possible when using autocomplete ajax )
2020-08-04 14:13:52 +02:00
* @ param string $moreparams More params provided to ajax call
2020-08-07 14:56:30 +02:00
* @ return string Script
2009-03-28 20:11:38 +01:00
*/
2020-08-04 14:13:52 +02:00
function ajax_autocompleter ( $selected , $htmlname , $url , $urloption = '' , $minLength = 2 , $autoselect = 0 , $ajaxoptions = array (), $moreparams = '' )
2007-12-29 18:06:53 +01:00
{
2021-02-23 22:03:23 +01:00
if ( empty ( $minLength )) {
$minLength = 1 ;
}
2011-05-31 22:36:06 +02:00
2020-09-07 10:18:17 +02:00
$dataforrenderITem = 'ui-autocomplete' ;
$dataforitem = 'ui-autocomplete-item' ;
// Allow two constant to use other values for backward compatibility
2021-02-23 22:03:23 +01:00
if ( defined ( 'JS_QUERY_AUTOCOMPLETE_RENDERITEM' )) {
$dataforrenderITem = constant ( 'JS_QUERY_AUTOCOMPLETE_RENDERITEM' );
}
if ( defined ( 'JS_QUERY_AUTOCOMPLETE_ITEM' )) {
$dataforitem = constant ( 'JS_QUERY_AUTOCOMPLETE_ITEM' );
}
2017-07-28 18:09:38 +02:00
2022-05-17 00:50:59 +02:00
$htmlnamejquery = str_replace ( '.' , '\\\\.' , $htmlname );
2020-09-07 10:18:17 +02:00
// Input search_htmlname is original field
// Input htmlname is a second input field used when using ajax autocomplete.
2020-08-04 14:13:52 +02:00
$script = '<input type="hidden" name="' . $htmlname . '" id="' . $htmlname . '" value="' . $selected . '" ' . ( $moreparams ? $moreparams : '' ) . ' />' ;
2009-08-28 00:20:01 +02:00
2020-02-21 17:53:37 +01:00
$script .= '<!-- Javascript code for autocomplete of field ' . $htmlname . ' -->' . " \n " ;
$script .= '<script>' . " \n " ;
$script .= ' $ ( document ) . ready ( function () {
2021-09-07 14:10:26 +02:00
var autoselect = '.((int) $autoselect).' ;
2020-07-06 15:34:26 +02:00
var options = '.json_encode($ajaxoptions).' ; /* Option of actions to do after keyup, or after select */
2012-08-22 17:42:40 +02:00
2024-01-13 19:48:20 +01:00
/* 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 */
2022-05-17 00:50:59 +02:00
$ ( " input#search_'. $htmlnamejquery .' " ) . keydown ( function ( e ) {
2017-10-06 10:46:45 +02:00
if ( e . keyCode != 9 ) /* If not "Tab" key */
{
2020-12-08 20:53:42 +01:00
if ( e . keyCode == 13 ) { return false ; } /* disable "ENTER" key useful for barcode readers */
2017-10-06 10:46:45 +02:00
console . log ( " Clear id previously selected for field '. $htmlname .' " );
2022-05-17 00:50:59 +02:00
$ ( " #'. $htmlnamejquery .' " ) . val ( " " );
2017-10-06 10:46:45 +02:00
}
2012-11-26 13:33:18 +01:00
});
2016-10-12 15:33:33 +02:00
2016-02-14 23:53:42 +01:00
// Check options for secondary actions when keyup
2022-05-17 00:50:59 +02:00
$ ( " input#search_'. $htmlnamejquery .' " ) . keyup ( function () {
2012-08-22 17:42:40 +02:00
if ( $ ( this ) . val () . length == 0 )
{
2022-05-17 00:50:59 +02:00
$ ( " #search_'. $htmlnamejquery .' " ) . val ( " " );
$ ( " #'. $htmlnamejquery .' " ) . val ( " " ) . trigger ( " change " );
2012-08-22 17:42:40 +02:00
if ( options . option_disabled ) {
$ ( " # " + options . option_disabled ) . removeAttr ( " disabled " );
}
if ( options . disabled ) {
$ . each ( options . disabled , function ( key , value ) {
$ ( " # " + value ) . removeAttr ( " disabled " );
});
}
if ( options . update ) {
$ . each ( options . update , function ( key , value ) {
$ ( " # " + key ) . val ( " " ) . trigger ( " change " );
});
}
if ( options . show ) {
$ . each ( options . show , function ( key , value ) {
$ ( " # " + value ) . hide () . trigger ( " hide " );
});
}
2012-08-29 17:40:13 +02:00
if ( options . update_textarea ) {
$ . each ( options . update_textarea , function ( key , value ) {
if ( typeof CKEDITOR == " object " && typeof CKEDITOR . instances != " undefined " && CKEDITOR . instances [ key ] != " undefined " ) {
CKEDITOR . instances [ key ] . setData ( " " );
} else {
$ ( " # " + key ) . html ( " " );
}
});
}
2012-08-22 17:42:40 +02:00
}
2010-10-11 23:32:53 +02:00
});
2020-02-19 03:07:12 +01:00
2024-05-08 14:23:39 +02:00
// Activate the autocomplete to execute the GET
2022-05-17 00:50:59 +02:00
$ ( " input#search_'. $htmlnamejquery .' " ) . autocomplete ({
2010-10-08 17:28:18 +02:00
source : function ( request , response ) {
2022-05-17 00:50:59 +02:00
$ . get ( " '. $url .( $urloption ? '?'. $urloption : '').' " , { " '.str_replace('.', '_', $htmlname ).' " : request . term }, function ( data ){
2014-08-31 15:09:10 +02:00
if ( data != null )
{
response ( $ . map ( data , function ( item ) {
2024-11-12 01:26:08 +01:00
console . log ( " Received answer from ajax GET, we populate array to return to the jquery autocomplete " );
2014-08-31 15:09:10 +02:00
if ( autoselect == 1 && data . length == 1 ) {
2022-05-17 00:50:59 +02:00
$ ( " #search_'. $htmlnamejquery .' " ) . val ( item . value );
$ ( " #'. $htmlnamejquery .' " ) . val ( item . key ) . trigger ( " change " );
2014-08-31 15:09:10 +02:00
}
2022-11-25 23:47:17 +01:00
var label = " " ;
2024-11-12 01:26:08 +01:00
if ( item . labelhtml != null ) {
label = item . labelhtml . toString ();
} else if ( item . label != null ) {
2022-11-25 23:47:17 +01:00
label = item . label . toString ();
}
2014-08-31 15:09:10 +02:00
var update = {};
if ( options . update ) {
$ . each ( options . update , function ( key , value ) {
update [ key ] = item [ value ];
});
}
var textarea = {};
if ( options . update_textarea ) {
$ . each ( options . update_textarea , function ( key , value ) {
textarea [ key ] = item [ value ];
});
}
2022-10-13 14:31:08 +02:00
return { label : label ,
value : item . value ,
id : item . key ,
disabled : item . disabled ,
2022-09-25 12:36:57 +02:00
update : update ,
textarea : textarea ,
2020-10-26 16:31:07 +01:00
pbq : item . pbq ,
2022-09-25 12:36:57 +02:00
type : item . type ,
qty : item . qty ,
discount : item . discount ,
2021-02-15 13:12:27 +01:00
pricebasetype : item . pricebasetype ,
price_ht : item . price_ht ,
2020-10-26 16:31:07 +01:00
price_ttc : item . price_ttc ,
2022-10-13 14:31:08 +02:00
price_unit_ht : item . price_unit_ht ,
price_unit_ht_locale : item . price_unit_ht_locale ,
2024-05-03 00:30:01 +02:00
multicurrency_code : item . multicurrency_code ,
multicurrency_unitprice : item . multicurrency_unitprice ,
2021-02-15 13:12:27 +01:00
description : item . description ,
2022-09-25 12:36:57 +02:00
ref_customer : item . ref_customer ,
2022-12-04 15:16:45 +01:00
tva_tx : item . tva_tx ,
2024-05-03 00:30:01 +02:00
default_vat_code : item . default_vat_code ,
supplier_ref : item . supplier_ref
2022-12-04 15:16:45 +01:00
}
2014-08-31 15:09:10 +02:00
}));
2022-05-17 00:50:59 +02:00
} else {
console . error ( " Error: Ajax url '. $url .( $urloption ? '?'. $urloption : '').' has returned an empty page. Should be an empty json array. " );
2014-08-31 15:09:10 +02:00
}
2010-10-09 18:44:49 +02:00
}, " json " );
},
2010-10-11 23:32:53 +02:00
dataType : " json " ,
2022-05-17 00:50:59 +02:00
minLength : '.((int) $minLength).' ,
2024-05-08 14:23:39 +02:00
select : function ( event , ui ) { // Function ran once a new value has been selected into the javascript combo
2021-02-02 13:04:41 +01:00
console . log ( " We will trigger change on input '. $htmlname .' because of the select definition of autocomplete code for input#search_'. $htmlname .' " );
2016-04-28 20:04:48 +02:00
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 " );
2020-07-06 15:34:26 +02:00
2024-05-03 00:30:01 +02:00
console . log ( " Before, we propagate some properties, retrieved by the ajax of the get, into the data-xxx properties of the component #'. $htmlnamejquery .' " );
2022-12-04 15:16:45 +01:00
//console.log(ui.item);
2020-07-06 15:34:26 +02:00
2021-02-15 13:12:27 +01:00
// For supplier price and customer when price by quantity is off
2022-05-17 00:50:59 +02:00
$ ( " #'. $htmlnamejquery .' " ) . attr ( " data-up " , ui . item . price_ht );
2022-10-13 14:31:08 +02:00
$ ( " #'. $htmlnamejquery .' " ) . attr ( " data-up-locale " , ui . item . price_unit_ht_locale );
2022-05-17 00:50:59 +02:00
$ ( " #'. $htmlnamejquery .' " ) . attr ( " data-base " , ui . item . pricebasetype );
$ ( " #'. $htmlnamejquery .' " ) . attr ( " data-qty " , ui . item . qty );
$ ( " #'. $htmlnamejquery .' " ) . attr ( " data-discount " , ui . item . discount );
$ ( " #'. $htmlnamejquery .' " ) . attr ( " data-description " , ui . item . description );
$ ( " #'. $htmlnamejquery .' " ) . attr ( " data-ref-customer " , ui . item . ref_customer );
2022-09-25 12:36:57 +02:00
$ ( " #'. $htmlnamejquery .' " ) . attr ( " data-tvatx " , ui . item . tva_tx );
2022-12-04 15:16:45 +01:00
$ ( " #'. $htmlnamejquery .' " ) . attr ( " data-default-vat-code " , ui . item . default_vat_code );
2024-05-03 00:30:01 +02:00
$ ( " #'. $htmlnamejquery .' " ) . attr ( " data-supplier-ref " , ui . item . supplier_ref ); // supplier_ref of price
2024-02-10 01:09:24 +01:00
// For multi-currency values
$ ( " #'. $htmlnamejquery .' " ) . attr ( " data-multicurrency-code " , ui . item . multicurrency_code );
$ ( " #'. $htmlnamejquery .' " ) . attr ( " data-multicurrency-unitprice " , ui . item . multicurrency_unitprice );
' ;
2023-11-27 11:39:32 +01:00
if ( getDolGlobalString ( 'PRODUIT_CUSTOMER_PRICES_BY_QTY' ) || getDolGlobalString ( 'PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES' )) {
2020-10-26 16:31:07 +01:00
$script .= '
2021-02-15 13:12:27 +01:00
// For customer price when PRODUIT_CUSTOMER_PRICES_BY_QTY is on
2024-05-03 00:30:01 +02:00
console . log ( " PRODUIT_CUSTOMER_PRICES_BY_QTY is on, so we propagate also prices by quantity into data-pbqxxx properties " );
2022-05-17 00:50:59 +02:00
$ ( " #'. $htmlnamejquery .' " ) . attr ( " data-pbq " , ui . item . pbq );
$ ( " #'. $htmlnamejquery .' " ) . attr ( " data-pbqup " , ui . item . price_ht );
$ ( " #'. $htmlnamejquery .' " ) . attr ( " data-pbqbase " , ui . item . pricebasetype );
$ ( " #'. $htmlnamejquery .' " ) . attr ( " data-pbqqty " , ui . item . qty );
$ ( " #'. $htmlnamejquery .' " ) . attr ( " data-pbqpercent " , ui . item . discount );
2020-10-26 16:31:07 +01:00
' ;
}
$script .= '
2022-10-13 14:31:08 +02:00
// A new value has been selected, we trigger the handlers on #htmlnamejquery
2024-05-03 00:30:01 +02:00
console . log ( " Now, we trigger changes on #'. $htmlnamejquery .' " );
2022-05-17 00:50:59 +02:00
$ ( " #'. $htmlnamejquery .' " ) . val ( ui . item . id ) . trigger ( " change " ); // Select new value
2021-02-02 13:04:41 +01:00
2022-10-13 18:32:40 +02:00
// Complementary actions
2012-08-17 18:17:15 +02:00
// Disable an element
2012-08-22 17:42:40 +02:00
if ( options . option_disabled ) {
2017-08-23 15:02:38 +02:00
console . log ( " Make action option_disabled on # " + options . option_disabled + " with disabled= " + ui . item . disabled )
2012-08-17 16:58:57 +02:00
if ( ui . item . disabled ) {
2015-05-12 14:47:33 +02:00
$ ( " # " + options . option_disabled ) . prop ( " disabled " , true );
2012-08-17 16:58:57 +02:00
if ( options . error ) {
2014-05-05 17:59:43 +02:00
$ . jnotify ( options . error , " error " , true ); // Output with jnotify the error message
2012-08-17 16:58:57 +02:00
}
2014-05-05 17:59:43 +02:00
if ( options . warning ) {
$ . jnotify ( options . warning , " warning " , false ); // Output with jnotify the warning message
}
2017-08-23 15:02:38 +02:00
} else {
2012-08-22 17:42:40 +02:00
$ ( " # " + options . option_disabled ) . removeAttr ( " disabled " );
2012-08-17 16:58:57 +02:00
}
}
2021-02-02 13:04:41 +01:00
2012-08-22 17:42:40 +02:00
if ( options . disabled ) {
2024-05-08 14:23:39 +02:00
console . log ( " Make action \ 'disabled \ ' on each " + options . option_disabled )
2012-08-22 17:42:40 +02:00
$ . each ( options . disabled , function ( key , value ) {
2015-05-12 14:47:33 +02:00
$ ( " # " + value ) . prop ( " disabled " , true );
2012-08-22 17:42:40 +02:00
});
}
if ( options . show ) {
2024-05-08 14:23:39 +02:00
console . log ( " Make action \ 'show \ ' on each " + options . show )
2012-08-22 17:42:40 +02:00
$ . each ( options . show , function ( key , value ) {
$ ( " # " + value ) . show () . trigger ( " show " );
});
}
2021-02-02 13:04:41 +01:00
2012-08-22 17:42:40 +02:00
// Update an input
2012-08-17 18:17:15 +02:00
if ( ui . item . update ) {
2024-05-08 14:23:39 +02:00
console . log ( " Make action \ 'update \ ' on each ui.item.update (if there is) " )
2013-11-28 15:54:12 +01:00
// loop on each "update" fields
2012-08-17 18:17:15 +02:00
$ . each ( ui . item . update , function ( key , value ) {
2021-02-10 02:17:01 +01:00
console . log ( " Set value " + value + " into # " + key );
2012-08-22 17:42:40 +02:00
$ ( " # " + key ) . val ( value ) . trigger ( " change " );
});
}
if ( ui . item . textarea ) {
2024-05-08 14:23:39 +02:00
console . log ( " Make action \ 'textarea \ ' on each ui.item.textarea (if there is) " )
2012-08-22 17:42:40 +02:00
$ . each ( ui . item . textarea , function ( key , value ) {
2012-08-28 12:36:35 +02:00
if ( typeof CKEDITOR == " object " && typeof CKEDITOR . instances != " undefined " && CKEDITOR . instances [ key ] != " undefined " ) {
2012-08-22 17:42:40 +02:00
CKEDITOR . instances [ key ] . setData ( value );
CKEDITOR . instances [ key ] . focus ();
} else {
$ ( " # " + key ) . html ( value );
$ ( " # " + key ) . focus ();
}
2012-08-17 18:17:15 +02:00
});
}
2021-02-02 13:04:41 +01:00
console . log ( " ajax_autocompleter new value selected, we trigger change also on original component so on field #search_'. $htmlname .' " );
2017-06-21 11:37:34 +02:00
2022-05-17 00:50:59 +02:00
$ ( " #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.
2010-10-08 10:20:57 +02:00
}
2014-10-30 18:45:47 +01:00
, delay : 500
2017-07-28 18:09:38 +02:00
}) . data ( " '. $dataforrenderITem .' " ) . _renderItem = function ( ul , item ) {
2015-11-30 20:28:36 +01:00
return $ ( " <li> " )
2017-07-28 18:09:38 +02:00
. data ( " '. $dataforitem .' " , item ) // jQuery UI > 1.10.0
2012-11-20 12:09:52 +01:00
. append ( \ ' < a >< span class = " tag " > \ ' + item . label + " </span></a> " )
2011-09-20 11:40:27 +02:00
. appendTo ( ul );
2010-10-11 10:43:33 +02:00
};
2014-02-04 20:49:07 +01:00
2010-10-08 10:20:57 +02:00
}); ' ;
2020-02-21 17:53:37 +01:00
$script .= '</script>' ;
2010-03-04 09:28:30 +01:00
return $script ;
}
2010-10-12 19:14:53 +02:00
/**
2024-05-08 14:23:39 +02:00
* Generic function that return javascript to add to a page to transform a common input text field into an autocomplete field by calling an Ajax page ( ex : core / ajax / ziptown . php ) .
2014-08-30 20:05:16 +02:00
* The Ajax page can also returns several values ( json format ) to fill several input fields .
* The HTML field must be an input text with id = $htmlname .
* This use the jQuery " autocomplete " function .
2011-12-28 21:30:33 +01:00
*
2014-08-30 20:05:16 +02:00
* @ param string $htmlname HTML name of input field
2024-08-21 13:23:35 +02:00
* @ param string [] $fields Array of key of fields to autocomplete
2014-08-30 20:05:16 +02:00
* @ param string $url URL for ajax request : / chemin / fichier . php
2012-02-04 14:39:47 +01:00
* @ 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
2010-10-12 19:14:53 +02:00
*/
2019-01-27 15:20:16 +01:00
function ajax_multiautocompleter ( $htmlname , $fields , $url , $option = '' , $minLength = 2 , $autoselect = 0 )
2010-10-12 19:14:53 +02:00
{
2011-11-05 12:45:13 +01:00
$script = '<!-- Autocomplete -->' . " \n " ;
2020-02-21 17:53:37 +01:00
$script .= '<script>' ;
$script .= ' jQuery ( document ) . ready ( function () {
2011-11-05 12:45:13 +01:00
var fields = '.json_encode($fields).' ;
2014-08-30 20:05:16 +02:00
var nboffields = fields . length ;
2011-02-07 15:15:56 +01:00
var autoselect = '.$autoselect.' ;
2014-08-30 20:05:16 +02:00
//alert(fields + " " + nboffields);
2010-10-22 10:11:51 +02:00
2024-05-08 14:23:39 +02:00
// Activate the autocomplete to execute the GET
2024-08-21 13:23:35 +02:00
jQuery ( " input#'. $htmlname .' " ) . autocomplete ({
dataType : " json " ,
minLength : '.$minLength.' ,
source : function ( request , response ) {
jQuery . getJSON ( " '. $url .( $option ? '?'. $option : '').' " , { '.$htmlname.' : request . term }, function ( data ){
2010-10-13 11:32:53 +02:00
response ( jQuery . map ( data , function ( item ) {
2011-02-07 15:15:56 +01:00
if ( autoselect == 1 && data . length == 1 ) {
2010-10-13 11:32:53 +02:00
jQuery ( " #'. $htmlname .' " ) . val ( item . value );
2010-10-13 19:03:28 +02:00
// TODO move this to specific request
2010-10-13 18:49:43 +02:00
if ( item . states ) {
2013-09-20 14:34:14 +02:00
jQuery ( " #state_id " ) . html ( item . states );
2010-10-13 18:47:49 +02:00
}
2014-08-30 20:05:16 +02:00
for ( i = 0 ; i < nboffields ; i ++ ) {
2010-10-31 21:09:50 +01:00
if ( item [ fields [ i ]]) { // If defined
2024-08-21 13:23:35 +02:00
//alert(item[fields[i]]);
2015-06-20 17:23:28 +02:00
jQuery ( " # " + fields [ i ]) . val ( item [ fields [ i ]]);
2010-10-13 11:32:53 +02:00
}
}
}
return item
}));
});
2024-08-21 13:23:35 +02:00
},
select : function ( event , ui ) {
needtotrigger = " " ;
for ( i = 0 ; i < nboffields ; i ++ ) {
//alert(fields[i] + " = " + ui.item[fields[i]]);
2011-12-29 18:34:56 +01:00
if ( fields [ i ] == " selectcountry_id " )
2010-10-31 21:09:50 +01:00
{
2024-08-21 13:23:35 +02:00
if ( ui . item [ fields [ i ]] > 0 ) // Do not erase country if unknown
{
oldvalue = jQuery ( " # " + fields [ i ]) . val ();
newvalue = ui . item [ fields [ i ]];
//alert(oldvalue+" "+newvalue);
jQuery ( " # " + fields [ i ]) . val ( ui . item [ fields [ i ]]);
if ( oldvalue != newvalue ) // To force select2 to refresh visible content
{
needtotrigger = " # " + fields [ i ];
2015-06-20 17:23:28 +02:00
}
2024-08-21 13:23:35 +02:00
// 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 [ " state_id " ] != jQuery ( " #state_id " ) . value ) {
jQuery ( " #state_id " ) . html ( ui . item . states );
}
}
2010-10-31 21:09:50 +01:00
}
2024-08-21 13:23:35 +02:00
else if ( fields [ i ] == " state_id " || fields [ i ] == " state_id " )
{
if ( ui . item [ fields [ i ]] > 0 ) // Do not erase state if unknown
{
oldvalue = jQuery ( " # " + fields [ i ]) . val ();
newvalue = ui . item [ fields [ i ]];
//alert(oldvalue+" "+newvalue);
jQuery ( " # " + fields [ i ]) . val ( ui . item [ fields [ i ]]); // This may fails if not correct country
if ( oldvalue != newvalue ) // To force select2 to refresh visible content
{
needtotrigger = " # " + fields [ i ];
2015-06-20 17:23:28 +02:00
}
2024-08-21 13:23:35 +02:00
}
}
2010-10-31 21:09:50 +01:00
else if ( ui . item [ fields [ i ]]) { // If defined
2024-08-21 13:23:35 +02:00
oldvalue = jQuery ( " # " + fields [ i ]) . val ();
newvalue = ui . item [ fields [ i ]];
//alert(oldvalue+" "+newvalue);
jQuery ( " # " + fields [ i ]) . val ( ui . item [ fields [ i ]]);
if ( oldvalue != newvalue ) // To force select2 to refresh visible content
{
needtotrigger = " # " + fields [ i ];
2015-06-20 17:23:28 +02:00
}
2010-10-13 09:38:48 +02:00
}
2016-10-12 15:33:33 +02:00
2016-04-23 16:49:43 +02:00
if ( needtotrigger != " " ) // To force select2 to refresh visible content
{
// 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
// This is required for example when changing zip with autocomplete that change the country
jQuery ( needtotrigger ) . delay ( 500 ) . queue ( function () {
2024-08-21 13:23:35 +02:00
jQuery ( this ) . trigger ( " change " );
2016-04-23 16:49:43 +02:00
});
}
2015-06-20 17:23:28 +02:00
}
2024-08-21 13:23:35 +02:00
}
2010-10-12 19:14:53 +02:00
});
}); ' ;
2020-02-21 17:53:37 +01:00
$script .= '</script>' ;
2010-10-12 19:14:53 +02:00
return $script ;
}
2010-10-03 23:43:03 +02:00
/**
2010-10-04 11:56:24 +02:00
* Show an ajax dialog
2012-02-04 14:39:47 +01:00
*
* @ 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
2019-11-26 00:28:26 +01:00
* @ return string
2010-10-03 23:43:03 +02:00
*/
2019-01-27 15:20:16 +01:00
function ajax_dialog ( $title , $message , $w = 350 , $h = 150 )
2010-10-03 23:43:03 +02:00
{
2023-12-04 12:05:28 +01:00
$newtitle = dol_textishtml ( $title ) ? dol_string_nohtmltag ( $title , 1 ) : $title ;
2020-02-21 17:53:37 +01:00
$msg = '<div id="dialog-info" title="' . dol_escape_htmltag ( $newtitle ) . '">' ;
$msg .= $message ;
$msg .= '</div>' . " \n " ;
2020-09-07 10:18:17 +02:00
$msg .= ' < script >
2010-10-03 23:43:03 +02:00
jQuery ( function () {
jQuery ( " #dialog-info " ) . dialog ({
resizable : false ,
height : '.$h.' ,
width : '.$w.' ,
modal : true ,
buttons : {
Ok : function () {
2012-08-13 20:53:48 +02:00
jQuery ( this ) . dialog ( \ ' close\ ' );
2010-10-03 23:43:03 +02:00
}
}
});
});
</ script > ' ;
2010-10-11 23:32:53 +02:00
2020-09-07 10:18:17 +02:00
$msg .= " \n " ;
2010-10-11 23:32:53 +02:00
2020-09-07 10:18:17 +02:00
return $msg ;
2010-10-03 23:43:03 +02:00
}
2017-03-23 11:16:30 +01:00
2011-04-25 16:24:43 +02:00
/**
2014-02-04 20:49:07 +01:00
* Convert a html select field into an ajax combobox .
* Use ajax_combobox () only for small combo list ! If not , use instead ajax_autocompleter () .
* TODO : It is used when COMPANY_USE_SEARCH_TO_SELECT and CONTACT_USE_SEARCH_TO_SELECT are set by html . formcompany . class . php . Should use ajax_autocompleter instead like done by html . form . class . php for select_produits .
2011-07-28 20:57:23 +02:00
*
2025-01-30 22:20:24 +01:00
* @ param string $htmlname Name of html select field ( 'myid' or '.myclass' )
2024-10-20 10:13:40 +02:00
* @ param array < array { method : string , url : string , htmlname : string , params ? : array < string , string > } > $events More events option . Example : array ( array ( 'method' => 'getContacts' , 'url' => dol_buildpath ( '/core/ajax/contacts.php' , 1 ), 'htmlname' => 'contactid' , 'params' => array ( 'add-customer-contact' => 'disabled' )))
2025-01-30 22:20:24 +01:00
* @ param int < 0 , max > $minLengthToAutocomplete Minimum length of input string to start autocomplete
* @ param int < 0 , 1 > $forcefocus Force focus on field
* @ param 'resolve' | 'off' $widthTypeOfAutocomplete 'resolve' or 'off'
* @ param string $idforemptyvalue Defaults to '-1'
* @ param string $morecss More css
* @ return string Return html string to convert a select field into a combo , or '' if feature has been disabled for some reason .
2019-03-11 01:01:15 +01:00
* @ see selectArrayAjax () of html . form . class
2011-04-25 16:24:43 +02:00
*/
2022-11-08 03:01:32 +01:00
function ajax_combobox ( $htmlname , $events = array (), $minLengthToAutocomplete = 0 , $forcefocus = 0 , $widthTypeOfAutocomplete = 'resolve' , $idforemptyvalue = '-1' , $morecss = '' )
2011-04-25 16:24:43 +02:00
{
2013-04-03 00:36:31 +02:00
global $conf ;
2017-06-21 11:37:34 +02:00
2019-03-15 01:28:54 +01:00
// select2 can be disabled for smartphones
2023-11-27 11:39:32 +01:00
if ( ! empty ( $conf -> browser -> layout ) && $conf -> browser -> layout == 'phone' && getDolGlobalString ( 'MAIN_DISALLOW_SELECT2_WITH_SMARTPHONE' )) {
2021-02-23 22:03:23 +01:00
return '' ;
}
2017-11-11 14:20:08 +01:00
2023-11-27 11:39:32 +01:00
if ( getDolGlobalString ( 'MAIN_DISABLE_AJAX_COMBOX' )) {
2021-02-23 22:03:23 +01:00
return '' ;
}
if ( empty ( $conf -> use_javascript_ajax )) {
return '' ;
}
2023-11-27 11:39:32 +01:00
if ( ! getDolGlobalString ( 'MAIN_USE_JQUERY_MULTISELECT' ) && ! defined ( 'REQUIRE_JQUERY_MULTISELECT' )) {
2021-02-23 22:03:23 +01:00
return '' ;
}
2023-11-27 11:39:32 +01:00
if ( getDolGlobalString ( 'MAIN_OPTIMIZEFORTEXTBROWSER' )) {
2021-02-23 22:03:23 +01:00
return '' ;
}
2015-01-30 19:57:38 +01:00
2021-02-23 22:03:23 +01:00
if ( empty ( $minLengthToAutocomplete )) {
$minLengthToAutocomplete = 0 ;
}
2015-01-30 19:57:38 +01:00
2022-11-08 03:01:32 +01:00
$moreselect2theme = ( $morecss ? dol_escape_js ( ' ' . $morecss ) : '' );
$moreselect2theme = preg_replace ( '/widthcentpercentminus[^\s]*/' , '' , $moreselect2theme );
2020-09-07 10:18:17 +02:00
$tmpplugin = 'select2' ;
$msg = " \n " . '<!-- JS CODE TO ENABLE ' . $tmpplugin . ' for id = ' . $htmlname . ' -->
2022-09-07 18:38:00 +02:00
< script >
$ ( document ) . ready ( function () {
2025-01-16 15:24:09 +01:00
$ ( \ '' . ( dol_escape_js ( preg_match ( '/^\./' , $htmlname ) ? $htmlname : '#' . $htmlname )) . '\').' . $tmpplugin . ' ({
2023-01-05 03:12:52 +01:00
dir : \ 'ltr\',' ;
if ( preg_match ( '/onrightofpage/' , $morecss )) { // when $morecss contains 'onrightofpage', the select2 component must also be inside a parent with class="parentonrightofpage"
$msg .= ' dropdownAutoWidth: true, dropdownParent: $(\'#' . $htmlname . '\').parent(), ' . " \n " ;
}
$msg .= ' width: \'' . dol_escape_js ( $widthTypeOfAutocomplete ) . ' \ ' , /* off or resolve */
2022-11-08 03:01:32 +01:00
minimumInputLength : '.((int) $minLengthToAutocomplete).' ,
2025-01-16 15:33:00 +01:00
language : ( typeof select2arrayoflanguage === \ ' undefined\ ' ) ? \ ' en\ ' : select2arrayoflanguage ,
2022-09-07 18:38:00 +02:00
matcher : function ( params , data ) {
if ( $ . trim ( params . term ) === " " ) {
return data ;
}
keywords = ( params . term ) . split ( " " );
for ( var i = 0 ; i < keywords . length ; i ++ ) {
if ((( data . text ) . toUpperCase ()) . indexOf (( keywords [ i ]) . toUpperCase ()) == - 1 ) {
return null ;
}
}
return data ;
},
2025-01-16 15:24:09 +01:00
theme : \ 'default' . dol_escape_js ( $moreselect2theme ) . ' \ ' , /* to add css on generated html components */
2022-09-07 18:38:00 +02:00
containerCssClass : \ ' : all : \ ' , /* Line to add class of origin SELECT propagated to the new <span class="select2-selection...> tag */
2021-01-28 16:31:32 +01:00
selectionCssClass : \ ' : all : \ ' , /* Line to add class of origin SELECT propagated to the new <span class="select2-selection...> tag */
2023-01-05 03:12:52 +01:00
dropdownCssClass : \ ' ui - dialog\ ' ,
2017-10-21 19:05:24 +02:00
templateResult : function ( data , container ) { /* Format visible output into combo list */
2017-10-26 14:06:23 +02:00
/* Code to add class of origin OPTION propagated to the new select2 <li> tag */
2017-11-11 14:20:08 +01:00
if ( data . element ) { $ ( container ) . addClass ( $ ( data . element ) . attr ( " class " )); }
2023-07-22 16:17:06 +02:00
//console.log("data html is "+$(data.element).attr("data-html"));
2022-09-07 18:38:00 +02:00
if ( data . id == '.((int) $idforemptyvalue).' && $ ( data . element ) . attr ( " data-html " ) == undefined ) {
2021-04-25 15:09:56 +02:00
return \ ' & nbsp ; \ ' ;
}
2023-07-22 16:17:06 +02:00
if ( $ ( data . element ) . attr ( " data-html " ) != undefined ) {
/* If property html set, we decode html entities and use this. */
/* Note that HTML content must have been sanitized from js with dol_escape_htmltag(xxx, 0, 0, \'\', 0, 1) when building the select option. */
2024-02-26 20:11:55 +01:00
if ( typeof htmlEntityDecodeJs === " function " ) {
return htmlEntityDecodeJs ( $ ( data . element ) . attr ( " data-html " ));
}
2023-07-22 16:17:06 +02:00
}
2021-01-28 16:31:32 +01:00
return data . text ;
2017-10-21 19:05:24 +02:00
},
templateSelection : function ( selection ) { /* Format visible output of selected value */
2021-05-25 22:51:37 +02:00
if ( selection . id == '.((int) $idforemptyvalue).' ) return \ ' < span class = " placeholder " > \ ' + selection . text + \ ' </ span > \ ' ;
2017-10-21 19:05:24 +02:00
return selection . text ;
},
2017-10-26 20:06:23 +02:00
escapeMarkup : function ( markup ) {
return markup ;
2023-01-05 03:12:52 +01:00
}
2015-05-31 12:25:33 +02:00
}) ' ;
2021-02-23 22:03:23 +01:00
if ( $forcefocus ) {
$msg .= '.select2(\'focus\')' ;
}
2020-02-21 17:53:37 +01:00
$msg .= ';' . " \n " ;
2014-12-05 13:26:47 +01:00
2023-03-12 20:43:13 +01:00
$msg .= '});' . " \n " ;
$msg .= " </script> \n " ;
$msg .= ajax_event ( $htmlname , $events );
return $msg ;
}
/**
* Add event management script .
*
* @ param string $htmlname Name of html select field ( 'myid' or '.myclass' )
2024-11-04 12:32:13 +01:00
* @ param array < array { method : string , url : string , htmlname : string , params : array < string , string > } > $events Add some Ajax events option on change of $htmlname component to call ajax to autofill a HTML element ( select #htmlname and #inputautocompletehtmlname)
* Example : array ( array ( 'method' => 'getContacts' , 'url' => dol_buildpath ( '/core/ajax/contacts.php' , 1 ), 'htmlname' => 'contactid' , 'params' => array ( 'add-customer-contact' => 'disabled' )))
2023-03-12 20:43:13 +01:00
* @ return string Return JS string to manage event
*/
function ajax_event ( $htmlname , $events )
{
$out = '' ;
if ( is_array ( $events ) && count ( $events )) { // If an array of js events to do were provided.
$out = '<!-- JS code to manage event for id = ' . $htmlname . ' -->
< script >
$ ( document ) . ready ( function () {
2017-02-02 20:47:45 +01:00
jQuery ( " #'. $htmlname .' " ) . change ( function () {
2023-03-12 20:43:13 +01:00
var obj = '.json_encode($events) . ' ;
2014-12-20 13:06:08 +01:00
$ . each ( obj , function ( key , values ) {
if ( values . method . length ) {
2017-02-02 20:47:45 +01:00
runJsCodeForEvent '.$htmlname.' ( values );
2014-12-20 13:06:08 +01:00
}
2012-03-15 16:52:18 +01:00
});
2014-12-20 13:06:08 +01:00
});
2017-02-02 20:47:45 +01:00
function runJsCodeForEvent '.$htmlname.' ( obj ) {
var id = $ ( " #'. $htmlname .' " ) . val ();
2014-12-20 13:06:08 +01:00
var method = obj . method ;
var url = obj . url ;
var htmlname = obj . htmlname ;
var showempty = obj . showempty ;
2023-03-12 20:43:13 +01:00
console . log ( " Run runJsCodeForEvent-'. $htmlname .' from ajax_combobox id= " + id + " method= " + method + " showempty= " + showempty + " url= " + url + " htmlname= " + htmlname );
2017-11-12 13:40:05 +01:00
$ . getJSON ( url ,
2014-12-20 13:06:08 +01:00
{
action : method ,
id : id ,
htmlname : htmlname ,
showempty : showempty
},
function ( response ) {
$ . each ( obj . params , function ( key , action ) {
if ( key . length ) {
var num = response . num ;
if ( num > 0 ) {
$ ( " # " + key ) . removeAttr ( action );
} else {
$ ( " # " + key ) . attr ( action , action );
}
2012-03-18 14:06:55 +01:00
}
2014-12-20 13:06:08 +01:00
});
2024-05-08 14:23:39 +02:00
console . log ( " Replace HTML content of select# " + htmlname );
2014-12-20 13:06:08 +01:00
$ ( " select# " + htmlname ) . html ( response . value );
if ( response . num ) {
2024-05-08 14:23:39 +02:00
var selecthtml_str = response . value ; /* response.value is the HTML string with list of options */
2014-12-20 13:06:08 +01:00
var selecthtml_dom = $ . parseHTML ( selecthtml_str );
2021-02-14 21:32:19 +01:00
if ( typeof ( selecthtml_dom [ 0 ][ 0 ]) !== \ ' undefined\ ' ) {
2023-03-12 20:43:13 +01:00
$ ( " #inputautocomplete " + htmlname ) . val ( selecthtml_dom [ 0 ][ 0 ] . innerHTML );
2021-02-14 21:32:19 +01:00
}
2014-12-20 13:06:08 +01:00
} else {
$ ( " #inputautocomplete " + htmlname ) . val ( " " );
2012-03-18 14:06:55 +01:00
}
2014-12-20 13:06:08 +01:00
$ ( " select# " + htmlname ) . change (); /* Trigger event change */
2014-06-17 11:18:30 +02:00
}
2014-12-20 13:06:08 +01:00
);
2023-03-12 20:43:13 +01:00
}
});
</ script > ' ;
2014-12-20 13:06:08 +01:00
}
2015-01-30 19:57:38 +01:00
2023-03-12 20:43:13 +01:00
return $out ;
2011-04-25 16:24:43 +02:00
}
2023-03-12 20:43:13 +01:00
2011-05-15 22:21:34 +02:00
/**
* On / off button for constant
2011-07-28 20:57:23 +02:00
*
2024-03-07 16:02:14 +01:00
* @ param string $code Name of constant
2024-11-04 12:32:13 +01:00
* @ param array < string , string [] > $input Array of complementary actions to do if success ( " disabled " | " enabled'|'set'|'del') => CSS element to switch, 'alert' => message to show, ... Example: array('disabled'=>array(0=>'cssid'))
* @ param ? int $entity Entity . Current entity is used if null .
* @ param int < 0 , 1 > $revertonoff 1 = Revert on / off
* @ param int < 0 , 1 > $strict 0 = Default , 1 = Only the complementary actions " disabled and " enabled " (found into $input ) are processed. Use only " disabled " with delConstant and " enabled " with setConstant.
2024-03-07 16:02:14 +01:00
* @ param int $forcereload Force to reload page if we click / change value ( this is supported only when there is no 'alert' option in input )
2024-11-04 12:32:13 +01:00
* @ param int < 0 , 2 > $marginleftonlyshort 1 = Add a short left margin on picto , 2 = Add a larger left margin on picto , 0 = No left margin .
* @ param int < 0 , 1 > $forcenoajax 1 = Force to use a ahref link instead of ajax code .
* @ param int < 0 , 1 > $setzeroinsteadofdel 1 = Set constant to '0' instead of deleting it when $input is empty .
2024-05-31 23:57:44 +02:00
* @ param string $suffix Suffix to use on the name of the switch picto when option is on . Example : '' , '_red'
2024-03-07 16:02:14 +01:00
* @ param string $mode Add parameter & mode = to the href link ( Used for href link )
* @ param string $morecss More CSS
2024-11-24 13:00:08 +01:00
* @ param User | int $userconst If set , use the ajax On / Off for user or user ID $userconst
2024-11-18 12:52:49 +01:00
* @ param string $showwarning String to show a warning when enabled the option
2024-03-07 16:02:14 +01:00
* @ return string
2024-06-20 13:30:44 +02:00
* @ see ajax_object_onoff () to update the status of an object
2011-05-15 22:21:34 +02:00
*/
2024-11-18 12:52:49 +01:00
function 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 = '' )
2011-05-15 22:21:34 +02:00
{
2024-11-24 13:00:08 +01:00
global $conf , $langs , $user , $db ;
2012-01-11 21:12:15 +01:00
2012-02-13 09:44:24 +01:00
$entity = (( isset ( $entity ) && is_numeric ( $entity ) && $entity >= 0 ) ? $entity : $conf -> entity );
2021-02-23 22:03:23 +01:00
if ( ! isset ( $input )) {
$input = array ();
}
2011-05-25 15:27:07 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $conf -> use_javascript_ajax ) || $forcenoajax ) {
2024-10-30 00:08:43 +01:00
if ( ! getDolGlobalString ( $code )) {
2023-10-10 22:35:35 +02:00
$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>' ;
2021-02-23 22:03:23 +01:00
} else {
2023-10-10 22:35:35 +02:00
$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>' ;
2021-02-23 22:03:23 +01:00
}
2020-05-21 15:05:19 +02:00
} else {
2024-11-25 18:57:57 +01:00
$userconstid = 0 ;
2024-11-24 13:00:08 +01:00
if ( is_object ( $userconst )) {
$userconstid = $userconst -> id ;
2024-11-25 18:57:57 +01:00
} elseif ( is_numeric ( $userconst ) && $userconst > 0 ) {
2024-11-24 13:00:08 +01:00
$userconstid = $userconst ;
$userconst = new User ( $db );
$userconst -> fetch ( $userconstid );
}
2020-02-21 17:53:37 +01:00
$out = " \n <!-- Ajax code to switch constant " . $code . " --> " . '
2019-01-20 23:36:39 +01:00
< script >
2014-06-29 23:35:00 +02:00
$ ( document ) . ready ( function () {
var input = '.json_encode($input).' ;
var url = \ '' . DOL_URL_ROOT . ' / core / ajax / constantonoff . php\ ' ;
2022-02-06 22:11:44 +01:00
var code = \ '' . dol_escape_js ( $code ) . ' \ ' ;
var entity = \ '' . dol_escape_js ( $entity ) . ' \ ' ;
2024-09-05 16:05:37 +02:00
var strict = \ '' . dol_escape_js (( string ) $strict ) . ' \ ' ;
var userid = \ '' . dol_escape_js (( string ) $user -> id ) . ' \ ' ;
2024-11-24 13:00:08 +01:00
var userconst = '.((int) $userconstid).' ;
2020-09-19 01:53:22 +02:00
var yesButton = \ '' . dol_escape_js ( $langs -> transnoentities ( " Yes " )) . ' \ ' ;
var noButton = \ '' . dol_escape_js ( $langs -> transnoentities ( " No " )) . ' \ ' ;
2020-09-19 12:50:47 +02:00
var token = \ '' . currentToken () . ' \ ' ;
2024-11-18 12:52:49 +01:00
var warning = \ '' . dol_escape_js ( $showwarning ) . ' \ ' ;
2011-07-28 20:57:23 +02:00
2014-06-29 23:35:00 +02:00
// Set constant
$ ( " #set_ " + code ) . click ( function () {
2024-11-18 12:52:49 +01:00
if ( warning ) {
alert ( warning );
}
2024-11-21 12:47:48 +01:00
2014-06-29 23:35:00 +02:00
if ( input . alert && input . alert . set ) {
if ( input . alert . set . yesButton ) yesButton = input . alert . set . yesButton ;
if ( input . alert . set . noButton ) noButton = input . alert . set . noButton ;
2020-09-19 01:53:22 +02:00
confirmConstantAction ( " set " , url , code , input , input . alert . set , entity , yesButton , noButton , strict , userid , token );
2014-06-29 23:35:00 +02:00
} else {
2024-10-30 02:38:59 +01:00
setConstant ( url , code , input , entity , 0 , '.((int) $forcereload).' , userid , token , 1 , userconst );
2014-06-29 23:35:00 +02:00
}
});
2011-07-28 20:57:23 +02:00
2014-06-29 23:35:00 +02:00
// Del constant
$ ( " #del_ " + code ) . click ( function () {
if ( input . alert && input . alert . del ) {
if ( input . alert . del . yesButton ) yesButton = input . alert . del . yesButton ;
if ( input . alert . del . noButton ) noButton = input . alert . del . noButton ;
2020-09-19 01:53:22 +02:00
confirmConstantAction ( " del " , url , code , input , input . alert . del , entity , yesButton , noButton , strict , userid , token );
2021-08-05 14:42:28 +02:00
} else { ' ;
if ( empty ( $setzeroinsteadofdel )) {
2024-10-30 02:38:59 +01:00
$out .= ' delConstant(url, code, input, entity, 0, ' . (( int ) $forcereload ) . ', userid, token, userconst);' ;
2021-08-05 14:42:28 +02:00
} else {
2024-10-30 02:38:59 +01:00
$out .= ' setConstant(url, code, input, entity, 0, ' . (( int ) $forcereload ) . ', userid, token, 0, userconst);' ;
2021-08-05 14:42:28 +02:00
}
$out .= ' }
2014-06-29 23:35:00 +02:00
});
2011-05-15 22:21:34 +02:00
});
2014-06-29 23:35:00 +02:00
</ script > ' . " \n " ;
2011-05-25 15:27:07 +02:00
2024-11-24 13:00:08 +01:00
if ( ! empty ( $userconst ) && $userconst instanceof User ) {
$value = getDolUserString ( $code , '' , $userconst );
2024-10-30 02:38:59 +01:00
} else {
$value = getDolGlobalString ( $code );
}
2020-02-21 17:53:37 +01:00
$out .= '<div id="confirm_' . $code . '" title="" style="display: none;"></div>' ;
2024-10-30 02:38:59 +01:00
$out .= '<span id="set_' . $code . '" class="valignmiddle inline-block linkobject ' . ( $value ? 'hideobject' : '' ) . ( $morecss ? ' ' . $morecss : '' ) . '">' . ( $revertonoff ? img_picto ( $langs -> trans ( " Enabled " ), 'switch_on' , '' , 0 , 0 , 0 , '' , '' , $marginleftonlyshort ) : img_picto ( $langs -> trans ( " Disabled " ), 'switch_off' , '' , 0 , 0 , 0 , '' , '' , $marginleftonlyshort )) . '</span>' ;
$out .= '<span id="del_' . $code . '" class="valignmiddle inline-block linkobject ' . ( $value ? '' : 'hideobject' ) . ( $morecss ? ' ' . $morecss : '' ) . '">' . ( $revertonoff ? img_picto ( $langs -> trans ( " Disabled " ), 'switch_off' . $suffix , '' , 0 , 0 , 0 , '' , '' , $marginleftonlyshort ) : img_picto ( $langs -> trans ( " Enabled " ), 'switch_on' . $suffix , '' , 0 , 0 , 0 , '' , '' , $marginleftonlyshort )) . '</span>' ;
2020-02-21 17:53:37 +01:00
$out .= " \n " ;
2014-06-29 23:35:00 +02:00
}
2011-05-25 15:27:07 +02:00
2011-05-15 22:21:34 +02:00
return $out ;
}
2014-10-12 14:20:49 +02:00
/**
2023-04-09 13:00:34 +02:00
* On / off button to change a property status of an object
* This uses the ajax service objectonoff . php ( May be called when MAIN_DIRECT_STATUS_UPDATE is set for some pages )
2014-10-12 14:20:49 +02:00
*
2024-11-25 18:57:57 +01:00
* @ param CommonObject $object Object to set
2022-10-09 20:52:50 +02:00
* @ param string $code Name of property in object : 'status' or 'status_buy' for product by example
2019-08-02 17:12:03 +02:00
* @ param string $field Name of database field : 'tosell' or 'tobuy' for product by example
2023-04-09 13:00:34 +02:00
* @ param string $text_on Text if on ( 'Text' or 'Text:Picto on:Css picto on' )
* @ param string $text_off Text if off ( 'Text' or 'Text:Picto off:Css picto off' )
2024-11-04 12:32:13 +01:00
* @ param array < string , string [] > $input Array of type -> list of CSS element to switch . Example : array ( 'disabled' => array ( 0 => 'cssid' ))
2021-09-07 17:26:31 +02:00
* @ param string $morecss More CSS
2023-04-09 13:00:34 +02:00
* @ param string $htmlname Name of HTML component . Keep '' or use a different value if you need to use this component several time on the same page for the same field .
* @ param int $forcenojs Force the component to work as link post ( without javascript ) instead of ajax call
2024-08-26 20:51:42 +02:00
* @ param string $moreparam When $forcenojs = 1 then we can add more parameters to the backtopage URL . String must url encoded . Example : 'abc=def&fgh=ijk'
2018-10-19 22:52:18 +02:00
* @ return string html for button on / off
2024-06-20 13:30:44 +02:00
* @ see ajax_constantonoff () to update that value of a constant
2014-10-12 14:20:49 +02:00
*/
2024-08-26 20:51:42 +02:00
function ajax_object_onoff ( $object , $code , $field , $text_on , $text_off , $input = array (), $morecss = '' , $htmlname = '' , $forcenojs = 0 , $moreparam = '' )
2014-10-12 14:20:49 +02:00
{
2023-04-09 13:00:34 +02:00
global $conf , $langs ;
2014-10-12 14:20:49 +02:00
2022-10-09 20:52:50 +02:00
if ( empty ( $htmlname )) {
$htmlname = $code ;
}
2023-04-09 13:00:34 +02:00
$out = '' ;
2024-06-20 13:30:44 +02:00
if ( ! empty ( $conf -> use_javascript_ajax ) && empty ( $forcenojs )) {
2023-04-09 13:00:34 +02:00
$out .= ' < script >
2014-10-12 14:20:49 +02:00
$ ( function () {
var input = '.json_encode($input).' ;
// Set constant
2022-10-09 20:52:50 +02:00
$ ( " #set_'. $htmlname .'_'. $object->id .' " ) . click ( function () {
2022-10-08 16:40:21 +02:00
console . log ( " Click managed by ajax_object_onoff " );
2014-10-18 17:11:02 +02:00
$ . get ( " '.DOL_URL_ROOT.'/core/ajax/objectonoff.php " , {
2014-10-18 22:46:13 +02:00
action : \ ' set\ ' ,
2022-10-09 20:52:50 +02:00
field : \ '' . dol_escape_js ( $field ) . ' \ ' ,
2014-10-12 15:55:18 +02:00
value : \ ' 1 \ ' ,
2023-03-26 20:51:51 +02:00
element : \ '' . dol_escape_js (( empty ( $object -> module ) || $object -> module == $object -> element ) ? $object -> element : $object -> element . '@' . $object -> module ) . ' \ ' ,
2023-03-21 18:55:56 +01:00
id : \ '' . (( int ) $object -> id ) . ' \ ' ,
2022-10-08 16:40:21 +02:00
token : \ '' . currentToken () . ' \ '
2014-10-12 14:20:49 +02:00
},
function () {
2022-10-09 20:52:50 +02:00
$ ( " #set_'. $htmlname .'_'. $object->id .' " ) . hide ();
$ ( " #del_'. $htmlname .'_'. $object->id .' " ) . show ();
2014-10-12 14:20:49 +02:00
// 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
2022-10-09 20:52:50 +02:00
$ ( " #del_'. $htmlname .'_'. $object->id .' " ) . click ( function () {
2022-10-08 16:40:21 +02:00
console . log ( " Click managed by ajax_object_onoff " );
2014-10-18 17:11:02 +02:00
$ . get ( " '.DOL_URL_ROOT.'/core/ajax/objectonoff.php " , {
2014-10-18 22:46:13 +02:00
action : \ ' set\ ' ,
2022-10-09 20:52:50 +02:00
field : \ '' . dol_escape_js ( $field ) . ' \ ' ,
2014-10-12 15:55:18 +02:00
value : \ ' 0 \ ' ,
2023-03-26 20:51:51 +02:00
element : \ '' . dol_escape_js (( empty ( $object -> module ) || $object -> module == $object -> element ) ? $object -> element : $object -> element . '@' . $object -> module ) . ' \ ' ,
2023-03-21 18:55:56 +01:00
id : \ '' . (( int ) $object -> id ) . ' \ ' ,
2022-10-08 16:40:21 +02:00
token : \ '' . currentToken () . ' \ '
2014-10-12 14:20:49 +02:00
},
function () {
2022-10-09 20:52:50 +02:00
$ ( " #del_'. $htmlname .'_'. $object->id .' " ) . hide ();
$ ( " #set_'. $htmlname .'_'. $object->id .' " ) . show ();
2014-10-12 14:20:49 +02:00
// Disable another element
if ( input . disabled && input . disabled . length > 0 ) {
$ . each ( input . disabled , function ( key , value ) {
2015-05-12 14:47:33 +02:00
$ ( " # " + value ) . prop ( " disabled " , true );
2014-10-12 14:20:49 +02:00
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 > ' ;
2023-04-09 13:00:34 +02:00
}
2023-03-21 18:55:56 +01:00
$switchon = 'switch_on' ;
$switchoff = 'switch_off' ;
2023-04-09 13:00:34 +02:00
$cssswitchon = '' ;
$cssswitchoff = '' ;
2023-03-21 18:55:56 +01:00
$tmparray = explode ( ':' , $text_on );
if ( ! empty ( $tmparray [ 1 ])) {
$text_on = $tmparray [ 0 ];
$switchon = $tmparray [ 1 ];
2023-04-09 13:00:34 +02:00
if ( ! empty ( $tmparray [ 2 ])) {
$cssswitchon = $tmparray [ 2 ];
}
2023-03-21 18:55:56 +01:00
}
$tmparray = explode ( ':' , $text_off );
if ( ! empty ( $tmparray [ 1 ])) {
$text_off = $tmparray [ 0 ];
$switchoff = $tmparray [ 1 ];
2023-04-09 13:00:34 +02:00
if ( ! empty ( $tmparray [ 2 ])) {
$cssswitchoff = $tmparray [ 2 ];
}
2023-03-21 18:55:56 +01:00
}
2023-04-09 13:00:34 +02:00
if ( empty ( $conf -> use_javascript_ajax ) || $forcenojs ) {
2024-09-25 22:21:31 +02:00
$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 , '' , 0 , 0 , 0 , '' , $cssswitchoff ) . '</a>' ;
$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 , '' , 0 , 0 , 0 , '' , $cssswitchon ) . '</a>' ;
2023-04-09 13:00:34 +02:00
} else {
2024-09-25 22:21:31 +02:00
$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>' ;
$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>' ;
2023-04-09 13:00:34 +02:00
}
2014-10-18 18:48:13 +02:00
2020-09-07 10:18:17 +02:00
return $out ;
2014-10-12 14:20:49 +02:00
}