ajax.lib.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. <?php
  2. /* Copyright (C) 2007-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2007-2015 Regis Houssin <regis.houssin@capnetworks.com>
  4. * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. * or see http://www.gnu.org/
  19. */
  20. /**
  21. * \file htdocs/core/lib/ajax.lib.php
  22. * \brief Page called by Ajax request for produts
  23. */
  24. /**
  25. * Generic function that return javascript to add to a page to transform a common input field into an autocomplete field by calling an Ajax page (ex: /societe/ajaxcompanies.php).
  26. * The HTML field must be an input text with id=search_$htmlname.
  27. * This use the jQuery "autocomplete" function.
  28. *
  29. * @param string $selected Preselecte value
  30. * @param string $htmlname HTML name of input field
  31. * @param string $url Url 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)
  32. * @param string $urloption More parameters on URL request
  33. * @param int $minLength Minimum number of chars to trigger that Ajax search
  34. * @param int $autoselect Automatic selection if just one value
  35. * @param array $ajaxoptions Multiple options array
  36. * Ex: array('update'=>array('field1','field2'...)) will reset field1 and field2 once select done
  37. * Ex: array('disabled'=>
  38. * Ex: array('show'=>
  39. * Ex: array('update_textarea'=>
  40. * @return string Script
  41. */
  42. function ajax_autocompleter($selected, $htmlname, $url, $urloption='', $minLength=2, $autoselect=0, $ajaxoptions=array())
  43. {
  44. if (empty($minLength)) $minLength=1;
  45. // Input search_htmlname is original field
  46. // Input htmlname is a second input field used when using ajax autocomplete.
  47. $script = '<input type="hidden" name="'.$htmlname.'" id="'.$htmlname.'" value="'.$selected.'" />';
  48. $script.= '<!-- Javascript code for autocomplete of field '.$htmlname.' -->'."\n";
  49. $script.= '<script type="text/javascript">'."\n";
  50. $script.= '$(document).ready(function() {
  51. var autoselect = '.$autoselect.';
  52. var options = '.json_encode($ajaxoptions).';
  53. /* Remove product id before select another product use keyup instead of change to avoid loosing the product id. This is needed only for select of predefined product */
  54. /* TODO Check if we can remove this */
  55. $("input#search_'.$htmlname.'").keydown(function() {
  56. $("#'.$htmlname.'").val("");
  57. });
  58. /* I disable this. A call to trigger is already done later into the select action of the autocomplete code
  59. $("input#search_'.$htmlname.'").change(function() {
  60. console.log("Call the change trigger on input '.$htmlname.' because of a change on search_'.$htmlname.' was triggered");
  61. $("#'.$htmlname.'").trigger("change");
  62. });*/
  63. // Check options for secondary actions when keyup
  64. $("input#search_'.$htmlname.'").keyup(function() {
  65. if ($(this).val().length == 0)
  66. {
  67. $("#search_'.$htmlname.'").val("");
  68. $("#'.$htmlname.'").val("").trigger("change");
  69. if (options.option_disabled) {
  70. $("#" + options.option_disabled).removeAttr("disabled");
  71. }
  72. if (options.disabled) {
  73. $.each(options.disabled, function(key, value) {
  74. $("#" + value).removeAttr("disabled");
  75. });
  76. }
  77. if (options.update) {
  78. $.each(options.update, function(key, value) {
  79. $("#" + key).val("").trigger("change");
  80. });
  81. }
  82. if (options.show) {
  83. $.each(options.show, function(key, value) {
  84. $("#" + value).hide().trigger("hide");
  85. });
  86. }
  87. if (options.update_textarea) {
  88. $.each(options.update_textarea, function(key, value) {
  89. if (typeof CKEDITOR == "object" && typeof CKEDITOR.instances != "undefined" && CKEDITOR.instances[key] != "undefined") {
  90. CKEDITOR.instances[key].setData("");
  91. } else {
  92. $("#" + key).html("");
  93. }
  94. });
  95. }
  96. }
  97. });
  98. $("input#search_'.$htmlname.'").autocomplete({
  99. source: function( request, response ) {
  100. $.get("'.$url.($urloption?'?'.$urloption:'').'", { '.$htmlname.': request.term }, function(data){
  101. if (data != null)
  102. {
  103. response($.map( data, function(item) {
  104. if (autoselect == 1 && data.length == 1) {
  105. $("#search_'.$htmlname.'").val(item.value);
  106. $("#'.$htmlname.'").val(item.key).trigger("change");
  107. }
  108. var label = item.label.toString();
  109. var update = {};
  110. if (options.update) {
  111. $.each(options.update, function(key, value) {
  112. update[key] = item[value];
  113. });
  114. }
  115. var textarea = {};
  116. if (options.update_textarea) {
  117. $.each(options.update_textarea, function(key, value) {
  118. textarea[key] = item[value];
  119. });
  120. }
  121. return { label: label, value: item.value, id: item.key, update: update, textarea: textarea, disabled: item.disabled }
  122. }));
  123. }
  124. else console.error("Error: Ajax url '.$url.($urloption?'?'.$urloption:'').' has returned an empty page. Should be an empty json array.");
  125. }, "json");
  126. },
  127. dataType: "json",
  128. minLength: '.$minLength.',
  129. select: function( event, ui ) { // Function ran once new value has been selected into javascript combo
  130. console.log("Call change on input '.$htmlname.' because of select definition of autocomplete select call on input#search_'.$htmlname.'");
  131. 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");
  132. $("#'.$htmlname.'").val(ui.item.id).trigger("change"); // Select new value
  133. // Disable an element
  134. if (options.option_disabled) {
  135. if (ui.item.disabled) {
  136. $("#" + options.option_disabled).prop("disabled", true);
  137. if (options.error) {
  138. $.jnotify(options.error, "error", true); // Output with jnotify the error message
  139. }
  140. if (options.warning) {
  141. $.jnotify(options.warning, "warning", false); // Output with jnotify the warning message
  142. }
  143. } else {
  144. $("#" + options.option_disabled).removeAttr("disabled");
  145. }
  146. }
  147. if (options.disabled) {
  148. $.each(options.disabled, function(key, value) {
  149. $("#" + value).prop("disabled", true);
  150. });
  151. }
  152. if (options.show) {
  153. $.each(options.show, function(key, value) {
  154. $("#" + value).show().trigger("show");
  155. });
  156. }
  157. // Update an input
  158. if (ui.item.update) {
  159. // loop on each "update" fields
  160. $.each(ui.item.update, function(key, value) {
  161. $("#" + key).val(value).trigger("change");
  162. });
  163. }
  164. if (ui.item.textarea) {
  165. $.each(ui.item.textarea, function(key, value) {
  166. if (typeof CKEDITOR == "object" && typeof CKEDITOR.instances != "undefined" && CKEDITOR.instances[key] != "undefined") {
  167. CKEDITOR.instances[key].setData(value);
  168. CKEDITOR.instances[key].focus();
  169. } else {
  170. $("#" + key).html(value);
  171. $("#" + key).focus();
  172. }
  173. });
  174. }
  175. console.log("ajax_autocompleter new value selected, we trigger change on original component so field #search_'.$htmlname.'");
  176. $("#search_'.$htmlname.'").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.
  177. }
  178. ,delay: 500
  179. }).data("ui-autocomplete")._renderItem = function( ul, item ) {
  180. return $("<li>")
  181. .data( "ui-autocomplete-item", item ) // jQuery UI > 1.10.0
  182. .append( \'<a><span class="tag">\' + item.label + "</span></a>" )
  183. .appendTo(ul);
  184. };
  185. });';
  186. $script.= '</script>';
  187. return $script;
  188. }
  189. /**
  190. * Generic function that return javascript to add to a page to transform a common input field into an autocomplete field by calling an Ajax page (ex: core/ajax/ziptown.php).
  191. * The Ajax page can also returns several values (json format) to fill several input fields.
  192. * The HTML field must be an input text with id=$htmlname.
  193. * This use the jQuery "autocomplete" function.
  194. *
  195. * @param string $htmlname HTML name of input field
  196. * @param string $fields Other fields to autocomplete
  197. * @param string $url URL for ajax request : /chemin/fichier.php
  198. * @param string $option More parameters on URL request
  199. * @param int $minLength Minimum number of chars to trigger that Ajax search
  200. * @param int $autoselect Automatic selection if just one value
  201. * @return string Script
  202. */
  203. function ajax_multiautocompleter($htmlname, $fields, $url, $option='', $minLength=2, $autoselect=0)
  204. {
  205. $script = '<!-- Autocomplete -->'."\n";
  206. $script.= '<script type="text/javascript">';
  207. $script.= 'jQuery(document).ready(function() {
  208. var fields = '.json_encode($fields).';
  209. var nboffields = fields.length;
  210. var autoselect = '.$autoselect.';
  211. //alert(fields + " " + nboffields);
  212. jQuery("input#'.$htmlname.'").autocomplete({
  213. dataType: "json",
  214. minLength: '.$minLength.',
  215. source: function( request, response ) {
  216. jQuery.getJSON( "'.$url.($option?'?'.$option:'').'", { '.$htmlname.': request.term }, function(data){
  217. response( jQuery.map( data, function( item ) {
  218. if (autoselect == 1 && data.length == 1) {
  219. jQuery("#'.$htmlname.'").val(item.value);
  220. // TODO move this to specific request
  221. if (item.states) {
  222. jQuery("#state_id").html(item.states);
  223. }
  224. for (i=0;i<nboffields;i++) {
  225. if (item[fields[i]]) { // If defined
  226. //alert(item[fields[i]]);
  227. jQuery("#" + fields[i]).val(item[fields[i]]);
  228. }
  229. }
  230. }
  231. return item
  232. }));
  233. });
  234. },
  235. select: function( event, ui ) {
  236. needtotrigger = "";
  237. for (i=0;i<nboffields;i++) {
  238. //alert(fields[i] + " = " + ui.item[fields[i]]);
  239. if (fields[i]=="selectcountry_id")
  240. {
  241. if (ui.item[fields[i]] > 0) // Do not erase country if unknown
  242. {
  243. oldvalue=jQuery("#" + fields[i]).val();
  244. newvalue=ui.item[fields[i]];
  245. //alert(oldvalue+" "+newvalue);
  246. jQuery("#" + fields[i]).val(ui.item[fields[i]]);
  247. if (oldvalue != newvalue) // To force select2 to refresh visible content
  248. {
  249. needtotrigger="#" + fields[i];
  250. }
  251. // If we set new country and new state, we need to set a new list of state to allow change
  252. if (ui.item.states && ui.item["state_id"] != jQuery("#state_id").value) {
  253. jQuery("#state_id").html(ui.item.states);
  254. }
  255. }
  256. }
  257. else if (fields[i]=="state_id" || fields[i]=="state_id")
  258. {
  259. if (ui.item[fields[i]] > 0) // Do not erase state if unknown
  260. {
  261. oldvalue=jQuery("#" + fields[i]).val();
  262. newvalue=ui.item[fields[i]];
  263. //alert(oldvalue+" "+newvalue);
  264. jQuery("#" + fields[i]).val(ui.item[fields[i]]); // This may fails if not correct country
  265. if (oldvalue != newvalue) // To force select2 to refresh visible content
  266. {
  267. needtotrigger="#" + fields[i];
  268. }
  269. }
  270. }
  271. else if (ui.item[fields[i]]) { // If defined
  272. oldvalue=jQuery("#" + fields[i]).val();
  273. newvalue=ui.item[fields[i]];
  274. //alert(oldvalue+" "+newvalue);
  275. jQuery("#" + fields[i]).val(ui.item[fields[i]]);
  276. if (oldvalue != newvalue) // To force select2 to refresh visible content
  277. {
  278. needtotrigger="#" + fields[i];
  279. }
  280. }
  281. if (needtotrigger != "") // To force select2 to refresh visible content
  282. {
  283. // 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
  284. // This is required for example when changing zip with autocomplete that change the country
  285. jQuery(needtotrigger).delay(500).queue(function() {
  286. jQuery(this).trigger("change");
  287. });
  288. }
  289. }
  290. }
  291. });
  292. });';
  293. $script.= '</script>';
  294. return $script;
  295. }
  296. /**
  297. * Show an ajax dialog
  298. *
  299. * @param string $title Title of dialog box
  300. * @param string $message Message of dialog box
  301. * @param int $w Width of dialog box
  302. * @param int $h height of dialog box
  303. * @return void
  304. */
  305. function ajax_dialog($title,$message,$w=350,$h=150)
  306. {
  307. global $langs;
  308. $newtitle=dol_textishtml($title)?dol_string_nohtmltag($title,1):$title;
  309. $msg= '<div id="dialog-info" title="'.dol_escape_htmltag($newtitle).'">';
  310. $msg.= $message;
  311. $msg.= '</div>'."\n";
  312. $msg.= '<script type="text/javascript">
  313. jQuery(function() {
  314. jQuery("#dialog-info").dialog({
  315. resizable: false,
  316. height:'.$h.',
  317. width:'.$w.',
  318. modal: true,
  319. buttons: {
  320. Ok: function() {
  321. jQuery(this).dialog(\'close\');
  322. }
  323. }
  324. });
  325. });
  326. </script>';
  327. $msg.= "\n";
  328. return $msg;
  329. }
  330. /**
  331. * Make content of an input box selected when we click into input field.
  332. *
  333. * @param string $htmlname Id of html object
  334. * @param string $addlink Add a 'link to' after
  335. */
  336. function ajax_autoselect($htmlname, $addlink='')
  337. {
  338. global $langs;
  339. $out = '<script type="text/javascript">
  340. jQuery(document).ready(function () {
  341. jQuery("#'.$htmlname.'").click(function() { jQuery(this).select(); } );
  342. });
  343. </script>';
  344. if ($addlink) $out.=' <a href="'.$addlink.'" target="_blank">'.$langs->trans("Link").'</a>';
  345. return $out;
  346. }
  347. /**
  348. * Convert a html select field into an ajax combobox.
  349. * Use ajax_combobox() only for small combo list! If not, use instead ajax_autocompleter().
  350. * 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.
  351. *
  352. * @param string $htmlname Name of html select field ('myid' or '.myclass')
  353. * @param array $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')))
  354. * @param int $minLengthToAutocomplete Minimum length of input string to start autocomplete
  355. * @param int $forcefocus Force focus on field
  356. * @param string $widthTypeOfAutocomplete 'resolve' or 'off'
  357. * @return string Return html string to convert a select field into a combo, or '' if feature has been disabled for some reason.
  358. */
  359. function ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve')
  360. {
  361. global $conf;
  362. if (! empty($conf->browser->phone)) return ''; // select2 disabled for smartphones with standard browser (does not works, popup appears outside screen)
  363. if (! empty($conf->dol_use_jmobile)) return ''; // select2 works with jmobile but it breaks the autosize feature of jmobile.
  364. if (! empty($conf->global->MAIN_DISABLE_AJAX_COMBOX)) return '';
  365. if (empty($conf->use_javascript_ajax)) return '';
  366. if (empty($minLengthToAutocomplete)) $minLengthToAutocomplete=0;
  367. $tmpplugin='select2';
  368. $msg='<!-- JS CODE TO ENABLE '.$tmpplugin.' for id '.$htmlname.' -->
  369. <script type="text/javascript">
  370. $(document).ready(function () {
  371. $(\''.(preg_match('/^\./',$htmlname)?$htmlname:'#'.$htmlname).'\').'.$tmpplugin.'({
  372. dir: \'ltr\',
  373. width: \''.$widthTypeOfAutocomplete.'\', /* off or resolve */
  374. minimumInputLength: '.$minLengthToAutocomplete.'
  375. })';
  376. if ($forcefocus) $msg.= '.select2(\'focus\')';
  377. $msg.= ';'."\n";
  378. if (count($events)) // If an array of js events to do were provided.
  379. {
  380. $msg.= '
  381. jQuery("#'.$htmlname.'").change(function () {
  382. var obj = '.json_encode($events).';
  383. $.each(obj, function(key,values) {
  384. if (values.method.length) {
  385. runJsCodeForEvent'.$htmlname.'(values);
  386. }
  387. });
  388. });
  389. function runJsCodeForEvent'.$htmlname.'(obj) {
  390. console.log("Run runJsCodeForEvent'.$htmlname.'");
  391. var id = $("#'.$htmlname.'").val();
  392. var method = obj.method;
  393. var url = obj.url;
  394. var htmlname = obj.htmlname;
  395. var showempty = obj.showempty;
  396. $.getJSON(url,
  397. {
  398. action: method,
  399. id: id,
  400. htmlname: htmlname,
  401. showempty: showempty
  402. },
  403. function(response) {
  404. $.each(obj.params, function(key,action) {
  405. if (key.length) {
  406. var num = response.num;
  407. if (num > 0) {
  408. $("#" + key).removeAttr(action);
  409. } else {
  410. $("#" + key).attr(action, action);
  411. }
  412. }
  413. });
  414. $("select#" + htmlname).html(response.value);
  415. if (response.num) {
  416. var selecthtml_str = response.value;
  417. var selecthtml_dom=$.parseHTML(selecthtml_str);
  418. $("#inputautocomplete"+htmlname).val(selecthtml_dom[0][0].innerHTML);
  419. } else {
  420. $("#inputautocomplete"+htmlname).val("");
  421. }
  422. $("select#" + htmlname).change(); /* Trigger event change */
  423. }
  424. );
  425. }';
  426. }
  427. $msg.= '});'."\n";
  428. $msg.= "</script>\n";
  429. return $msg;
  430. }
  431. /**
  432. * On/off button for constant
  433. *
  434. * @param string $code Name of constant
  435. * @param array $input Array of type->list of CSS element to switch. Example: array('disabled'=>array(0=>'cssid'))
  436. * @param int $entity Entity to set
  437. * @param int $revertonoff Revert on/off
  438. * @param bool $strict Use only "disabled" with delConstant and "enabled" with setConstant
  439. * @return string
  440. */
  441. function ajax_constantonoff($code, $input=array(), $entity=null, $revertonoff=0, $strict=0)
  442. {
  443. global $conf, $langs;
  444. $entity = ((isset($entity) && is_numeric($entity) && $entity >= 0) ? $entity : $conf->entity);
  445. if (empty($conf->use_javascript_ajax))
  446. {
  447. if (empty($conf->global->$code)) print '<a href="'.$_SERVER['PHP_SELF'].'?action=set_'.$code.'&entity='.$entity.'">'.img_picto($langs->trans("Disabled"),'off').'</a>';
  448. else print '<a href="'.$_SERVER['PHP_SELF'].'?action=del_'.$code.'&entity='.$entity.'">'.img_picto($langs->trans("Enabled"),'on').'</a>';
  449. }
  450. else
  451. {
  452. $out= "\n<!-- Ajax code to switch constant ".$code." -->".'
  453. <script type="text/javascript">
  454. $(document).ready(function() {
  455. var input = '.json_encode($input).';
  456. var url = \''.DOL_URL_ROOT.'/core/ajax/constantonoff.php\';
  457. var code = \''.$code.'\';
  458. var entity = \''.$entity.'\';
  459. var strict = \''.$strict.'\';
  460. var yesButton = "'.dol_escape_js($langs->transnoentities("Yes")).'";
  461. var noButton = "'.dol_escape_js($langs->transnoentities("No")).'";
  462. // Set constant
  463. $("#set_" + code).click(function() {
  464. if (input.alert && input.alert.set) {
  465. if (input.alert.set.yesButton) yesButton = input.alert.set.yesButton;
  466. if (input.alert.set.noButton) noButton = input.alert.set.noButton;
  467. confirmConstantAction("set", url, code, input, input.alert.set, entity, yesButton, noButton, strict);
  468. } else {
  469. setConstant(url, code, input, entity);
  470. }
  471. });
  472. // Del constant
  473. $("#del_" + code).click(function() {
  474. if (input.alert && input.alert.del) {
  475. if (input.alert.del.yesButton) yesButton = input.alert.del.yesButton;
  476. if (input.alert.del.noButton) noButton = input.alert.del.noButton;
  477. confirmConstantAction("del", url, code, input, input.alert.del, entity, yesButton, noButton, strict);
  478. } else {
  479. delConstant(url, code, input, entity);
  480. }
  481. });
  482. });
  483. </script>'."\n";
  484. $out.= '<div id="confirm_'.$code.'" title="" style="display: none;"></div>';
  485. $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>';
  486. $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>';
  487. $out.="\n";
  488. }
  489. return $out;
  490. }
  491. /**
  492. * On/off button for object
  493. *
  494. * @param int $object Id product to set
  495. * @param string $code Name of constant : status or status_buy for product by example
  496. * @param string $field Name of database field : tosell or tobuy for product by example
  497. * @param string $text_on Text if on
  498. * @param string $text_off Text if off
  499. * @param array $input Array of type->list of CSS element to switch. Example: array('disabled'=>array(0=>'cssid'))
  500. * @return void
  501. */
  502. function ajax_object_onoff($object, $code, $field, $text_on, $text_off, $input=array())
  503. {
  504. global $langs;
  505. $out= '<script type="text/javascript">
  506. $(function() {
  507. var input = '.json_encode($input).';
  508. // Set constant
  509. $("#set_'.$code.'_'.$object->id.'").click(function() {
  510. $.get( "'.DOL_URL_ROOT.'/core/ajax/objectonoff.php", {
  511. action: \'set\',
  512. field: \''.$field.'\',
  513. value: \'1\',
  514. element: \''.$object->element.'\',
  515. id: \''.$object->id.'\'
  516. },
  517. function() {
  518. $("#set_'.$code.'_'.$object->id.'").hide();
  519. $("#del_'.$code.'_'.$object->id.'").show();
  520. // Enable another element
  521. if (input.disabled && input.disabled.length > 0) {
  522. $.each(input.disabled, function(key,value) {
  523. $("#" + value).removeAttr("disabled");
  524. if ($("#" + value).hasClass("butActionRefused") == true) {
  525. $("#" + value).removeClass("butActionRefused");
  526. $("#" + value).addClass("butAction");
  527. }
  528. });
  529. // Show another element
  530. } else if (input.showhide && input.showhide.length > 0) {
  531. $.each(input.showhide, function(key,value) {
  532. $("#" + value).show();
  533. });
  534. }
  535. });
  536. });
  537. // Del constant
  538. $("#del_'.$code.'_'.$object->id.'").click(function() {
  539. $.get( "'.DOL_URL_ROOT.'/core/ajax/objectonoff.php", {
  540. action: \'set\',
  541. field: \''.$field.'\',
  542. value: \'0\',
  543. element: \''.$object->element.'\',
  544. id: \''.$object->id.'\'
  545. },
  546. function() {
  547. $("#del_'.$code.'_'.$object->id.'").hide();
  548. $("#set_'.$code.'_'.$object->id.'").show();
  549. // Disable another element
  550. if (input.disabled && input.disabled.length > 0) {
  551. $.each(input.disabled, function(key,value) {
  552. $("#" + value).prop("disabled", true);
  553. if ($("#" + value).hasClass("butAction") == true) {
  554. $("#" + value).removeClass("butAction");
  555. $("#" + value).addClass("butActionRefused");
  556. }
  557. });
  558. // Hide another element
  559. } else if (input.showhide && input.showhide.length > 0) {
  560. $.each(input.showhide, function(key,value) {
  561. $("#" + value).hide();
  562. });
  563. }
  564. });
  565. });
  566. });
  567. </script>';
  568. $out.= '<span id="set_'.$code.'_'.$object->id.'" class="linkobject '.($object->$code==1?'hideobject':'').'">'.img_picto($langs->trans($text_off),'switch_off').'</span>';
  569. $out.= '<span id="del_'.$code.'_'.$object->id.'" class="linkobject '.($object->$code==1?'':'hideobject').'">'.img_picto($langs->trans($text_on),'switch_on').'</span>';
  570. return $out;
  571. }