PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/lib/ajax.lib.php

https://github.com/asterix14/dolibarr
PHP | 284 lines | 202 code | 20 blank | 62 comment | 1 complexity | 11e23ebbd28211ca05260ef40540388f MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2007-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2007-2011 Regis Houssin <regis@dolibarr.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. * or see http://www.gnu.org/
  18. */
  19. /**
  20. * \file htdocs/core/lib/ajax.lib.php
  21. * \brief Page called by Ajax request for produts
  22. */
  23. /**
  24. * Get value of an HTML field, do Ajax process and show result
  25. *
  26. * @param selected Preselecte value
  27. * @param htmlname HTML name of input field
  28. * @param url Url for request: /chemin/fichier.php
  29. * @param option More parameters on URL request
  30. * @param minLength Minimum number of chars to trigger that Ajax search
  31. * @param autoselect Automatic selection if just one value
  32. * @return string script complet
  33. */
  34. function ajax_autocompleter($selected='',$htmlname,$url,$option='',$minLength=2,$autoselect=0)
  35. {
  36. if (empty($minLength)) $minLength=1;
  37. $script = '<input type="hidden" name="'.$htmlname.'" id="'.$htmlname.'" value="'.$selected.'" />';
  38. $script.= '<script type="text/javascript">';
  39. $script.= 'jQuery(document).ready(function() {
  40. var autoselect = '.$autoselect.';
  41. jQuery("input#search_'.$htmlname.'").blur(function() {
  42. //console.log(this.value.length);
  43. if (this.value.length == 0)
  44. {
  45. jQuery("#search_'.$htmlname.'").val("");
  46. jQuery("#'.$htmlname.'").val("");
  47. }
  48. });
  49. jQuery("input#search_'.$htmlname.'").autocomplete({
  50. source: function( request, response ) {
  51. jQuery.get("'.$url.($option?'?'.$option:'').'", { '.$htmlname.': request.term }, function(data){
  52. response( jQuery.map( data, function( item ) {
  53. if (autoselect == 1 && data.length == 1) {
  54. jQuery("#search_'.$htmlname.'").val(item.value);
  55. jQuery("#'.$htmlname.'").val(item.key);
  56. }
  57. var label = item.label.toString();
  58. return { label: label, value: item.value, id: item.key}
  59. }));
  60. }, "json");
  61. },
  62. dataType: "json",
  63. minLength: '.$minLength.',
  64. select: function( event, ui ) {
  65. jQuery("#'.$htmlname.'").val(ui.item.id);
  66. }
  67. }).data( "autocomplete" )._renderItem = function( ul, item ) {
  68. return jQuery( "<li></li>" )
  69. .data( "item.autocomplete", item )
  70. .append( \'<a href="#"><span class="tag">\' + item.label + "</span></a>" )
  71. .appendTo(ul);
  72. };
  73. });';
  74. $script.= '</script>';
  75. return $script;
  76. }
  77. /**
  78. * Get value of field, do Ajax process and return result
  79. * @param htmlname nom et id du champ
  80. * @param fields other fields to autocomplete
  81. * @param url chemin du fichier de reponse : /chemin/fichier.php
  82. * @param option More parameters on URL request
  83. * @param minLength Minimum number of chars to trigger that Ajax search
  84. * @param autoselect Automatic selection if just one value
  85. * @return string script complet
  86. */
  87. function ajax_multiautocompleter($htmlname,$fields,$url,$option='',$minLength=2,$autoselect=0)
  88. {
  89. $script = '<!-- Autocomplete -->'."\n";
  90. $script.= '<script type="text/javascript">';
  91. $script.= 'jQuery(document).ready(function() {
  92. var fields = '.json_encode($fields).';
  93. var length = fields.length;
  94. var autoselect = '.$autoselect.';
  95. //alert(fields + " " + length);
  96. jQuery("input#'.$htmlname.'").autocomplete({
  97. dataType: "json",
  98. minLength: '.$minLength.',
  99. source: function( request, response ) {
  100. jQuery.getJSON( "'.$url.($option?'?'.$option:'').'", { '.$htmlname.': request.term }, function(data){
  101. response( jQuery.map( data, function( item ) {
  102. if (autoselect == 1 && data.length == 1) {
  103. jQuery("#'.$htmlname.'").val(item.value);
  104. // TODO move this to specific request
  105. if (item.states) {
  106. jQuery("#departement_id").html(item.states);
  107. }
  108. for (i=0;i<length;i++) {
  109. if (item[fields[i]]) { // If defined
  110. //alert(item[fields[i]]);
  111. jQuery("#" + fields[i]).val(item[fields[i]]);
  112. }
  113. }
  114. }
  115. return item
  116. }));
  117. });
  118. },
  119. select: function( event, ui ) {
  120. for (i=0;i<length;i++) {
  121. //alert(fields[i] + " = " + ui.item[fields[i]]);
  122. if (fields[i]=="selectpays_id")
  123. {
  124. if (ui.item[fields[i]] > 0) // Do not erase country if unknown
  125. {
  126. jQuery("#" + fields[i]).val(ui.item[fields[i]]);
  127. // If we set new country and new state, we need to set a new list of state to allow change
  128. if (ui.item.states && ui.item["departement_id"] != jQuery("#departement_id").value) {
  129. jQuery("#departement_id").html(ui.item.states);
  130. }
  131. }
  132. }
  133. else if (fields[i]=="departement_id")
  134. {
  135. if (ui.item[fields[i]] > 0) // Do not erase state if unknown
  136. {
  137. jQuery("#" + fields[i]).val(ui.item[fields[i]]); // This may fails if not correct country
  138. }
  139. }
  140. else if (ui.item[fields[i]]) { // If defined
  141. //alert(fields[i]);
  142. //alert(ui.item[fields[i]]);
  143. jQuery("#" + fields[i]).val(ui.item[fields[i]]);
  144. }
  145. }
  146. }
  147. });
  148. });';
  149. $script.= '</script>';
  150. return $script;
  151. }
  152. /**
  153. * Show an ajax dialog
  154. * @param title Title of dialog box
  155. * @param message Message of dialog box
  156. * @param w Width of dialog box
  157. * @param h height of dialog box
  158. */
  159. function ajax_dialog($title,$message,$w=350,$h=150)
  160. {
  161. global $langs;
  162. $msg.= '<div id="dialog-info" title="'.dol_escape_htmltag($title).'">';
  163. $msg.= $message;
  164. $msg.= '</div>'."\n";
  165. $msg.= '<script type="text/javascript">
  166. jQuery(function() {
  167. jQuery("#dialog-info").dialog({
  168. resizable: false,
  169. height:'.$h.',
  170. width:'.$w.',
  171. modal: true,
  172. buttons: {
  173. Ok: function() {
  174. jQuery(this ).dialog(\'close\');
  175. }
  176. }
  177. });
  178. });
  179. </script>';
  180. $msg.= "\n";
  181. return $msg;
  182. }
  183. /**
  184. * Convert a select html field into an ajax combobox
  185. *
  186. * @param htmlname Name of html field
  187. * @return string Return html string to convert a select field into a combo
  188. */
  189. function ajax_combobox($htmlname)
  190. {
  191. $msg.= '<script type="text/javascript">
  192. $(function() {
  193. $("#'.$htmlname.'" ).combobox();
  194. });
  195. </script>';
  196. $msg.= "\n";
  197. return $msg;
  198. }
  199. /**
  200. * On/off button for constant
  201. *
  202. * @param code Name of constant
  203. * @param input Input element
  204. * TODO add different method for other input (show/hide, disable, ..)
  205. */
  206. function ajax_constantonoff($code,$input=array())
  207. {
  208. global $conf, $langs;
  209. $out= '<script type="text/javascript">
  210. $(function() {
  211. var input='.json_encode($input).';
  212. // Set constant
  213. $( "#set_'.$code.'" ).click(function() {
  214. $.get( "'.DOL_URL_ROOT.'/core/ajax/constantonoff.php", {
  215. action: \'set\',
  216. name: \''.$code.'\'
  217. },
  218. function() {
  219. $("#set_'.$code.'" ).hide();
  220. $("#del_'.$code.'" ).show();
  221. // Enable another object
  222. if (input.length > 0) {
  223. $.each(input, function(key,value) {
  224. $("#" + value).removeAttr("disabled");
  225. if ( $( "#" + value).hasClass("butActionRefused") == true ) {
  226. $("#" + value).removeClass("butActionRefused");
  227. $("#" + value).addClass("butAction");
  228. }
  229. });
  230. }
  231. });
  232. });
  233. // Del constant
  234. $( "#del_'.$code.'" ).click(function() {
  235. $.get( "'.DOL_URL_ROOT.'/core/ajax/constantonoff.php", {
  236. action: \'del\',
  237. name: \''.$code.'\'
  238. },
  239. function() {
  240. $("#del_'.$code.'" ).hide();
  241. $("#set_'.$code.'" ).show();
  242. // Disable another object
  243. if (input.length > 0) {
  244. $.each(input, function(key,value) {
  245. $("#" + value).attr("disabled", true);
  246. if ( $( "#" + value).hasClass("butAction") == true ) {
  247. $("#" + value).removeClass("butAction");
  248. $("#" + value).addClass("butActionRefused");
  249. }
  250. });
  251. }
  252. });
  253. });
  254. });
  255. </script>';
  256. $out.= '<span id="set_'.$code.'" class="linkobject '.($conf->global->$code?'hideobject':'').'">'.img_picto($langs->trans("Disabled"),'switch_off').'</span>';
  257. $out.= '<span id="del_'.$code.'" class="linkobject '.($conf->global->$code?'':'hideobject').'">'.img_picto($langs->trans("Enabled"),'switch_on').'</span>';
  258. return $out;
  259. }
  260. ?>