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>
|
2012-12-30 15:13:49 +01:00
|
|
|
* Copyright (C) 2007-2012 Regis Houssin <regis.houssin@capnetworks.com>
|
2012-07-18 15:17:04 +02:00
|
|
|
* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.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
|
2011-08-01 01:24:38 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2007-12-29 18:06:53 +01:00
|
|
|
* or see http://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
|
2009-03-28 20:11:38 +01:00
|
|
|
* \brief Page called by Ajax request for produts
|
2007-12-29 18:06:53 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2010-12-18 13:00:45 +01:00
|
|
|
* Get value of an HTML field, do Ajax process and show result
|
2011-11-09 22:10:58 +01:00
|
|
|
*
|
2012-02-04 14:39:47 +01:00
|
|
|
* @param string $selected Preselecte value
|
|
|
|
|
* @param string $htmlname HTML name of input field
|
|
|
|
|
* @param string $url Url for request: /chemin/fichier.php
|
2012-08-17 16:58:57 +02:00
|
|
|
* @param string $urloption More parameters on URL request
|
2012-02-04 14:39:47 +01:00
|
|
|
* @param int $minLength Minimum number of chars to trigger that Ajax search
|
|
|
|
|
* @param int $autoselect Automatic selection if just one value
|
2013-11-17 23:25:25 +01:00
|
|
|
* @param array $ajaxoptions Multiple options array (Ex: array('update'=>array('field1','field2'...)) will reset field1 and field2 once select done
|
2012-02-04 14:39:47 +01:00
|
|
|
* @return string Script
|
2009-03-28 20:11:38 +01:00
|
|
|
*/
|
2012-08-17 16:58:57 +02:00
|
|
|
function ajax_autocompleter($selected, $htmlname, $url, $urloption='', $minLength=2, $autoselect=0, $ajaxoptions=array())
|
2007-12-29 18:06:53 +01:00
|
|
|
{
|
2011-05-31 22:36:06 +02:00
|
|
|
if (empty($minLength)) $minLength=1;
|
|
|
|
|
|
2010-10-27 11:00:37 +02:00
|
|
|
$script = '<input type="hidden" name="'.$htmlname.'" id="'.$htmlname.'" value="'.$selected.'" />';
|
2009-08-28 00:20:01 +02:00
|
|
|
|
2007-12-29 18:06:53 +01:00
|
|
|
$script.= '<script type="text/javascript">';
|
2012-08-17 16:58:57 +02:00
|
|
|
$script.= '$(document).ready(function() {
|
2011-02-07 15:15:56 +01:00
|
|
|
var autoselect = '.$autoselect.';
|
2012-08-17 16:58:57 +02:00
|
|
|
var options = '.json_encode($ajaxoptions).';
|
2012-08-22 17:42:40 +02:00
|
|
|
|
|
|
|
|
// Remove product id before select another product
|
2012-11-20 12:09:52 +01:00
|
|
|
// use keyup instead change to avoid loosing the product id
|
2012-11-26 13:33:18 +01:00
|
|
|
$("input#search_'.$htmlname.'").keydown(function() {
|
|
|
|
|
//console.log(\'purge_id_after_keydown\');
|
|
|
|
|
$("#'.$htmlname.'").val("");
|
|
|
|
|
});
|
|
|
|
|
$("input#search_'.$htmlname.'").change(function() {
|
|
|
|
|
//console.log(\'keyup\');
|
|
|
|
|
$("#'.$htmlname.'").trigger("change");
|
2012-08-22 17:42:40 +02:00
|
|
|
});
|
|
|
|
|
// Check when keyup
|
|
|
|
|
$("input#search_'.$htmlname.'").onDelayedKeyup({ handler: function() {
|
|
|
|
|
if ($(this).val().length == 0)
|
|
|
|
|
{
|
|
|
|
|
$("#search_'.$htmlname.'").val("");
|
|
|
|
|
$("#'.$htmlname.'").val("").trigger("change");
|
|
|
|
|
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
|
|
|
});
|
2012-08-17 16:58:57 +02:00
|
|
|
$("input#search_'.$htmlname.'").autocomplete({
|
2010-10-08 17:28:18 +02:00
|
|
|
source: function( request, response ) {
|
2012-08-17 16:58:57 +02:00
|
|
|
$.get("'.$url.($urloption?'?'.$urloption:'').'", { '.$htmlname.': request.term }, function(data){
|
2012-08-22 17:42:40 +02:00
|
|
|
response($.map( data, function( item ) {
|
2011-02-07 15:15:56 +01:00
|
|
|
if (autoselect == 1 && data.length == 1) {
|
2012-08-17 16:58:57 +02:00
|
|
|
$("#search_'.$htmlname.'").val(item.value);
|
|
|
|
|
$("#'.$htmlname.'").val(item.key).trigger("change");
|
2010-10-11 12:07:11 +02:00
|
|
|
}
|
2010-10-11 23:32:53 +02:00
|
|
|
var label = item.label.toString();
|
2012-08-17 18:17:15 +02:00
|
|
|
var update = {};
|
|
|
|
|
if (options.update) {
|
|
|
|
|
$.each(options.update, function(key, value) {
|
|
|
|
|
update[key] = item[value];
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-08-22 17:42:40 +02:00
|
|
|
var textarea = {};
|
|
|
|
|
if (options.update_textarea) {
|
|
|
|
|
$.each(options.update_textarea, function(key, value) {
|
|
|
|
|
textarea[key] = item[value];
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return { label: label, value: item.value, id: item.key, update: update, textarea: textarea, disabled: item.disabled }
|
2010-10-09 18:44:49 +02:00
|
|
|
}));
|
|
|
|
|
}, "json");
|
|
|
|
|
},
|
2010-10-11 23:32:53 +02:00
|
|
|
dataType: "json",
|
2010-10-27 11:00:37 +02:00
|
|
|
minLength: '.$minLength.',
|
2010-10-08 10:20:57 +02:00
|
|
|
select: function( event, ui ) {
|
2012-11-26 13:33:18 +01:00
|
|
|
//console.log(\'set value of id with \'+ui.item.id);
|
2012-08-17 16:58:57 +02:00
|
|
|
$("#'.$htmlname.'").val(ui.item.id).trigger("change");
|
2012-08-17 18:17:15 +02:00
|
|
|
// Disable an element
|
2012-08-22 17:42:40 +02:00
|
|
|
if (options.option_disabled) {
|
2012-08-17 16:58:57 +02:00
|
|
|
if (ui.item.disabled) {
|
2012-08-22 17:42:40 +02:00
|
|
|
$("#" + options.option_disabled).attr("disabled", "disabled");
|
2012-08-17 16:58:57 +02:00
|
|
|
if (options.error) {
|
|
|
|
|
$.jnotify(options.error, "error", true);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2012-08-22 17:42:40 +02:00
|
|
|
$("#" + options.option_disabled).removeAttr("disabled");
|
2012-08-17 16:58:57 +02:00
|
|
|
}
|
|
|
|
|
}
|
2012-08-22 17:42:40 +02:00
|
|
|
if (options.disabled) {
|
|
|
|
|
$.each(options.disabled, function(key, value) {
|
|
|
|
|
$("#" + value).attr("disabled", "disabled");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (options.show) {
|
|
|
|
|
$.each(options.show, function(key, value) {
|
|
|
|
|
$("#" + value).show().trigger("show");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// Update an input
|
2012-08-17 18:17:15 +02:00
|
|
|
if (ui.item.update) {
|
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) {
|
2012-08-22 17:42:40 +02:00
|
|
|
$("#" + key).val(value).trigger("change");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (ui.item.textarea) {
|
|
|
|
|
$.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
|
|
|
});
|
|
|
|
|
}
|
2010-10-08 10:20:57 +02:00
|
|
|
}
|
2010-10-11 10:43:33 +02:00
|
|
|
}).data( "autocomplete" )._renderItem = function( ul, item ) {
|
2012-08-17 16:58:57 +02:00
|
|
|
return $( "<li></li>" )
|
2010-10-11 10:43:33 +02:00
|
|
|
.data( "item.autocomplete", item )
|
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
|
|
|
};
|
2010-10-08 10:20:57 +02:00
|
|
|
});';
|
2010-03-04 09:28:30 +01:00
|
|
|
$script.= '</script>';
|
|
|
|
|
|
|
|
|
|
return $script;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-12 19:14:53 +02:00
|
|
|
/**
|
2010-10-13 11:40:03 +02:00
|
|
|
* Get value of field, do Ajax process and return result
|
2011-12-28 21:30:33 +01:00
|
|
|
*
|
2012-02-04 14:39:47 +01:00
|
|
|
* @param string $htmlname Name of field
|
|
|
|
|
* @param string $fields other fields to autocomplete
|
|
|
|
|
* @param string $url Chemin du fichier de reponse : /chemin/fichier.php
|
|
|
|
|
* @param string $option More parameters on URL request
|
|
|
|
|
* @param int $minLength Minimum number of chars to trigger that Ajax search
|
|
|
|
|
* @param int $autoselect Automatic selection if just one value
|
|
|
|
|
* @return string Script
|
2010-10-12 19:14:53 +02:00
|
|
|
*/
|
2011-02-07 15:15:56 +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";
|
2010-10-12 19:14:53 +02:00
|
|
|
$script.= '<script type="text/javascript">';
|
|
|
|
|
$script.= 'jQuery(document).ready(function() {
|
2011-11-05 12:45:13 +01:00
|
|
|
var fields = '.json_encode($fields).';
|
2010-10-13 09:38:48 +02:00
|
|
|
var length = fields.length;
|
2011-02-07 15:15:56 +01:00
|
|
|
var autoselect = '.$autoselect.';
|
2010-10-13 09:38:48 +02:00
|
|
|
//alert(fields + " " + length);
|
2010-10-22 10:11:51 +02:00
|
|
|
|
2010-10-13 09:38:48 +02:00
|
|
|
jQuery("input#'.$htmlname.'").autocomplete({
|
2010-10-13 11:32:53 +02:00
|
|
|
dataType: "json",
|
2011-02-07 15:15:56 +01:00
|
|
|
minLength: '.$minLength.',
|
2010-10-12 19:14:53 +02:00
|
|
|
source: function( request, response ) {
|
2010-10-13 11:32:53 +02:00
|
|
|
jQuery.getJSON( "'.$url.($option?'?'.$option:'').'", { '.$htmlname.': request.term }, function(data){
|
|
|
|
|
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
|
|
|
}
|
2010-10-13 11:32:53 +02:00
|
|
|
for (i=0;i<length;i++) {
|
2010-10-31 21:09:50 +01:00
|
|
|
if (item[fields[i]]) { // If defined
|
|
|
|
|
//alert(item[fields[i]]);
|
|
|
|
|
jQuery("#" + fields[i]).val(item[fields[i]]);
|
2010-10-13 11:32:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return item
|
|
|
|
|
}));
|
|
|
|
|
});
|
2010-10-13 10:34:49 +02:00
|
|
|
},
|
2010-10-12 19:14:53 +02:00
|
|
|
select: function( event, ui ) {
|
2010-10-31 21:09:50 +01:00
|
|
|
|
2010-10-13 09:38:48 +02:00
|
|
|
for (i=0;i<length;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
|
|
|
{
|
|
|
|
|
if (ui.item[fields[i]] > 0) // Do not erase country if unknown
|
|
|
|
|
{
|
|
|
|
|
jQuery("#" + fields[i]).val(ui.item[fields[i]]);
|
|
|
|
|
// If we set new country and new state, we need to set a new list of state to allow change
|
2013-09-20 14:34:14 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-20 14:34:14 +02:00
|
|
|
else if (fields[i]=="state_id" || fields[i]=="state_id")
|
2010-10-31 21:09:50 +01:00
|
|
|
{
|
|
|
|
|
if (ui.item[fields[i]] > 0) // Do not erase state if unknown
|
|
|
|
|
{
|
|
|
|
|
jQuery("#" + fields[i]).val(ui.item[fields[i]]); // This may fails if not correct country
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (ui.item[fields[i]]) { // If defined
|
|
|
|
|
//alert(fields[i]);
|
|
|
|
|
//alert(ui.item[fields[i]]);
|
|
|
|
|
jQuery("#" + fields[i]).val(ui.item[fields[i]]);
|
2010-10-13 09:38:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
2010-10-12 19:14:53 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});';
|
|
|
|
|
$script.= '</script>';
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
* @return void
|
2010-10-03 23:43:03 +02:00
|
|
|
*/
|
|
|
|
|
function ajax_dialog($title,$message,$w=350,$h=150)
|
|
|
|
|
{
|
|
|
|
|
global $langs;
|
2010-10-11 23:32:53 +02:00
|
|
|
|
2010-10-03 23:43:03 +02:00
|
|
|
$msg.= '<div id="dialog-info" title="'.dol_escape_htmltag($title).'">';
|
|
|
|
|
$msg.= $message;
|
|
|
|
|
$msg.= '</div>'."\n";
|
|
|
|
|
$msg.= '<script type="text/javascript">
|
|
|
|
|
jQuery(function() {
|
|
|
|
|
jQuery("#dialog-info").dialog({
|
|
|
|
|
resizable: false,
|
|
|
|
|
height:'.$h.',
|
|
|
|
|
width:'.$w.',
|
|
|
|
|
modal: true,
|
|
|
|
|
buttons: {
|
|
|
|
|
Ok: function() {
|
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
|
|
|
|
2010-10-03 23:43:03 +02:00
|
|
|
$msg.= "\n";
|
2010-10-11 23:32:53 +02:00
|
|
|
|
2010-10-03 23:43:03 +02:00
|
|
|
return $msg;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-25 16:24:43 +02:00
|
|
|
/**
|
2012-10-27 00:23:16 +02:00
|
|
|
* Convert a html select field into an ajax combobox
|
2011-07-28 20:57:23 +02:00
|
|
|
*
|
2013-04-04 13:58:33 +02:00
|
|
|
* @param string $htmlname Name of html select field
|
|
|
|
|
* @param array $event Event options. Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/contacts.php',1), 'htmlname'=>'contactid', 'params'=>array('add-customer-contact'=>'disabled')))
|
|
|
|
|
* @param int $minLengthToAutocomplete Minimum length of input string to start autocomplete
|
|
|
|
|
* @return string Return html string to convert a select field into a combo
|
2011-04-25 16:24:43 +02:00
|
|
|
*/
|
2013-01-29 15:22:07 +01:00
|
|
|
function ajax_combobox($htmlname, $event=array(), $minLengthToAutocomplete=0)
|
2011-04-25 16:24:43 +02:00
|
|
|
{
|
2013-04-03 00:36:31 +02:00
|
|
|
global $conf;
|
|
|
|
|
|
|
|
|
|
if (! empty($conf->browser->phone)) return ''; // combobox disabled for smartphones (does not works)
|
2014-02-08 02:35:59 +01:00
|
|
|
if (! empty($conf->global->MAIN_DISABLE_AJAX_COMBOX)) return '';
|
2013-04-03 00:36:31 +02:00
|
|
|
|
2013-06-16 12:23:00 +02:00
|
|
|
/* Some properties for combobox:
|
|
|
|
|
minLengthToAutocomplete: 2,
|
2013-06-16 21:33:05 +02:00
|
|
|
comboboxContainerClass: "comboboxContainer",
|
|
|
|
|
comboboxValueContainerClass: "comboboxValueContainer",
|
|
|
|
|
comboboxValueContentClass: "comboboxValueContent",
|
|
|
|
|
comboboxDropDownClass: "comboboxDropDownContainer",
|
|
|
|
|
comboboxDropDownButtonClass: "comboboxDropDownButton",
|
|
|
|
|
comboboxDropDownItemClass: "comboboxItem",
|
|
|
|
|
comboboxDropDownItemHoverClass: "comboboxItemHover",
|
|
|
|
|
comboboxDropDownGroupItemHeaderClass: "comboboxGroupItemHeader",
|
|
|
|
|
comboboxDropDownGroupItemContainerClass: "comboboxGroupItemContainer",
|
|
|
|
|
animationType: "slide",
|
|
|
|
|
width: "500px" */
|
2013-06-16 12:23:00 +02:00
|
|
|
|
2012-07-10 22:10:12 +02:00
|
|
|
$msg = '<script type="text/javascript">
|
2011-04-25 16:24:43 +02:00
|
|
|
$(function() {
|
2012-03-15 16:52:18 +01:00
|
|
|
$("#'.$htmlname.'").combobox({
|
2013-01-29 15:22:07 +01:00
|
|
|
minLengthToAutocomplete : '.$minLengthToAutocomplete.',
|
2012-03-15 16:52:18 +01:00
|
|
|
selected : function(event,ui) {
|
|
|
|
|
var obj = '.json_encode($event).';
|
2012-07-10 22:10:12 +02:00
|
|
|
$.each(obj, function(key,values) {
|
2012-03-15 16:52:18 +01:00
|
|
|
if (values.method.length) {
|
|
|
|
|
getMethod(values);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2012-07-10 22:10:12 +02:00
|
|
|
|
2012-03-15 16:52:18 +01:00
|
|
|
function getMethod(obj) {
|
|
|
|
|
var id = $("#'.$htmlname.'").val();
|
|
|
|
|
var method = obj.method;
|
|
|
|
|
var url = obj.url;
|
|
|
|
|
var htmlname = obj.htmlname;
|
2012-03-18 14:06:55 +01:00
|
|
|
$.getJSON(url,
|
2012-03-15 16:52:18 +01:00
|
|
|
{
|
|
|
|
|
action: method,
|
|
|
|
|
id: id,
|
|
|
|
|
htmlname: htmlname
|
|
|
|
|
},
|
|
|
|
|
function(response) {
|
2012-03-18 14:06:55 +01:00
|
|
|
$.each(obj.params, function(key,action) {
|
|
|
|
|
if (key.length) {
|
|
|
|
|
var num = response.num;
|
|
|
|
|
if (num > 0) {
|
|
|
|
|
$("#" + key).removeAttr(action);
|
|
|
|
|
} else {
|
|
|
|
|
$("#" + key).attr(action, action);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
$("select#" + htmlname).html(response.value);
|
2012-03-15 16:52:18 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});';
|
|
|
|
|
$msg.= "</script>\n";
|
2011-05-25 15:27:07 +02:00
|
|
|
|
2011-04-25 16:24:43 +02:00
|
|
|
return $msg;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-15 22:21:34 +02:00
|
|
|
/**
|
|
|
|
|
* On/off button for constant
|
2011-07-28 20:57:23 +02:00
|
|
|
*
|
2013-03-13 23:05:34 +01:00
|
|
|
* @param string $code Name of constant
|
|
|
|
|
* @param array $input Array of type->list of CSS element to switch. Example: array('disabled'=>array(0=>'cssid'))
|
|
|
|
|
* @param int $entity Entity to set
|
|
|
|
|
* @param int $revertonoff Revert on/off
|
2012-01-11 21:12:15 +01:00
|
|
|
* @return void
|
2011-05-15 22:21:34 +02:00
|
|
|
*/
|
2013-03-13 23:05:34 +01:00
|
|
|
function ajax_constantonoff($code, $input=array(), $entity=null, $revertonoff=0)
|
2011-05-15 22:21:34 +02:00
|
|
|
{
|
|
|
|
|
global $conf, $langs;
|
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);
|
2011-05-25 15:27:07 +02:00
|
|
|
|
2013-02-24 18:08:27 +01:00
|
|
|
$out= "\n<!-- Ajax code to switch constant ".$code." -->".'
|
|
|
|
|
<script type="text/javascript">
|
2014-03-10 10:53:20 +01:00
|
|
|
$(document).ready(function() {
|
2012-01-11 21:12:15 +01:00
|
|
|
var input = '.json_encode($input).';
|
2012-08-13 11:42:50 +02:00
|
|
|
var url = \''.DOL_URL_ROOT.'/core/ajax/constantonoff.php\';
|
2012-08-13 20:53:48 +02:00
|
|
|
var code = \''.$code.'\';
|
|
|
|
|
var entity = \''.$entity.'\';
|
|
|
|
|
var yesButton = "'.dol_escape_js($langs->transnoentities("Yes")).'";
|
|
|
|
|
var noButton = "'.dol_escape_js($langs->transnoentities("No")).'";
|
2011-07-28 20:57:23 +02:00
|
|
|
|
2011-07-10 20:24:18 +02:00
|
|
|
// Set constant
|
2012-08-13 20:53:48 +02:00
|
|
|
$("#set_" + code).click(function() {
|
|
|
|
|
if (input.alert && input.alert.set) {
|
2013-02-24 18:08:27 +01:00
|
|
|
if (input.alert.set.yesButton) yesButton = input.alert.set.yesButton;
|
|
|
|
|
if (input.alert.set.noButton) noButton = input.alert.set.noButton;
|
2012-08-13 20:53:48 +02:00
|
|
|
confirmConstantAction("set", url, code, input, input.alert.set, entity, yesButton, noButton);
|
|
|
|
|
} else {
|
|
|
|
|
setConstant(url, code, input, entity);
|
|
|
|
|
}
|
2011-05-15 22:21:34 +02:00
|
|
|
});
|
2011-07-28 20:57:23 +02:00
|
|
|
|
2011-07-10 20:24:18 +02:00
|
|
|
// Del constant
|
2012-08-13 20:53:48 +02:00
|
|
|
$("#del_" + code).click(function() {
|
|
|
|
|
if (input.alert && input.alert.del) {
|
2013-02-24 18:08:27 +01:00
|
|
|
if (input.alert.del.yesButton) yesButton = input.alert.del.yesButton;
|
|
|
|
|
if (input.alert.del.noButton) noButton = input.alert.del.noButton;
|
2012-08-13 20:53:48 +02:00
|
|
|
confirmConstantAction("del", url, code, input, input.alert.del, entity, yesButton, noButton);
|
|
|
|
|
} else {
|
|
|
|
|
delConstant(url, code, input, entity);
|
|
|
|
|
}
|
2011-05-15 22:21:34 +02:00
|
|
|
});
|
|
|
|
|
});
|
2013-02-24 18:08:27 +01:00
|
|
|
</script>'."\n";
|
2011-05-25 15:27:07 +02:00
|
|
|
|
2012-08-13 20:53:48 +02:00
|
|
|
$out.= '<div id="confirm_'.$code.'" title="" style="display: none;"></div>';
|
2013-03-13 23:05:34 +01:00
|
|
|
$out.= '<span id="set_'.$code.'" class="linkobject '.(! empty($conf->global->$code)?'hideobject':'').'">'.($revertonoff?img_picto($langs->trans("Enabled"),'switch_on'):img_picto($langs->trans("Disabled"),'switch_off')).'</span>';
|
|
|
|
|
$out.= '<span id="del_'.$code.'" class="linkobject '.(! empty($conf->global->$code)?'':'hideobject').'">'.($revertonoff?img_picto($langs->trans("Disabled"),'switch_off'):img_picto($langs->trans("Enabled"),'switch_on')).'</span>';
|
2013-02-24 18:08:27 +01:00
|
|
|
$out.="\n";
|
2011-05-25 15:27:07 +02:00
|
|
|
|
2011-05-15 22:21:34 +02:00
|
|
|
return $out;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-27 13:33:31 +02:00
|
|
|
?>
|