* Copyright (C) 2007-2015 Regis Houssin * Copyright (C) 2012 Christophe Battarel * * 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 * the Free Software Foundation; either version 3 of the License, or * (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 * along with this program. If not, see . * or see http://www.gnu.org/ */ /** * \file htdocs/core/lib/ajax.lib.php * \brief Page called by Ajax request for produts */ /** * 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). * The HTML field must be an input text with id=search_$htmlname. * This use the jQuery "autocomplete" function. * * @param string $selected Preselecte value * @param string $htmlname HTML name of input field * @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) * @param string $urloption 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 * @param array $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'=> * @return string Script */ function ajax_autocompleter($selected, $htmlname, $url, $urloption='', $minLength=2, $autoselect=0, $ajaxoptions=array()) { if (empty($minLength)) $minLength=1; // Input search_htmlname is original field // Input htmlname is a second input field used when using ajax autocomplete. $script = ''; $script.= ''."\n"; $script.= ''; return $script; } /** * 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). * 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. * * @param string $htmlname HTML name of input field * @param string $fields Other fields to autocomplete * @param string $url URL for ajax request : /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 */ function ajax_multiautocompleter($htmlname, $fields, $url, $option='', $minLength=2, $autoselect=0) { $script = ''."\n"; $script.= ''; return $script; } /** * Show an ajax dialog * * @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 */ function ajax_dialog($title,$message,$w=350,$h=150) { global $langs; $newtitle=dol_textishtml($title)?dol_string_nohtmltag($title,1):$title; $msg= '
'; $msg.= $message; $msg.= '
'."\n"; $msg.= ''; $msg.= "\n"; return $msg; } /** * Make content of an input box selected when we click into input field. * * @param string $htmlname Id of html object * @param string $addlink Add a 'link to' after */ function ajax_autoselect($htmlname, $addlink='') { global $langs; $out = ''; if ($addlink) $out.=' '.$langs->trans("Link").''; return $out; } /** * 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. * * @param string $htmlname Name of html select field ('myid' or '.myclass') * @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'))) * @param int $minLengthToAutocomplete Minimum length of input string to start autocomplete * @param int $forcefocus Force focus on field * @param string $widthTypeOfAutocomplete 'resolve' or 'off' * @return string Return html string to convert a select field into a combo, or '' if feature has been disabled for some reason. */ function ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve') { global $conf; if (! empty($conf->browser->phone)) return ''; // select2 disabled for smartphones with standard browser (does not works, popup appears outside screen) if (! empty($conf->dol_use_jmobile)) return ''; // select2 works with jmobile but it breaks the autosize feature of jmobile. if (! empty($conf->global->MAIN_DISABLE_AJAX_COMBOX)) return ''; if (empty($conf->use_javascript_ajax)) return ''; if (empty($minLengthToAutocomplete)) $minLengthToAutocomplete=0; $tmpplugin='select2'; $msg=' \n"; return $msg; } /** * On/off button for constant * * @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 * @param bool $strict Use only "disabled" with delConstant and "enabled" with setConstant * @return string */ function ajax_constantonoff($code, $input=array(), $entity=null, $revertonoff=0, $strict=0) { global $conf, $langs; $entity = ((isset($entity) && is_numeric($entity) && $entity >= 0) ? $entity : $conf->entity); if (empty($conf->use_javascript_ajax)) { if (empty($conf->global->$code)) print ''.img_picto($langs->trans("Disabled"),'off').''; else print ''.img_picto($langs->trans("Enabled"),'on').''; } else { $out= "\n".' '."\n"; $out.= ''; $out.= ''.($revertonoff?img_picto($langs->trans("Enabled"),'switch_on'):img_picto($langs->trans("Disabled"),'switch_off')).''; $out.= ''.($revertonoff?img_picto($langs->trans("Disabled"),'switch_off'):img_picto($langs->trans("Enabled"),'switch_on')).''; $out.="\n"; } return $out; } /** * On/off button for object * * @param int $object Id product to set * @param string $code Name of constant : status or status_buy for product by example * @param string $field Name of database field : tosell or tobuy for product by example * @param string $text_on Text if on * @param string $text_off Text if off * @param array $input Array of type->list of CSS element to switch. Example: array('disabled'=>array(0=>'cssid')) * @return void */ function ajax_object_onoff($object, $code, $field, $text_on, $text_off, $input=array()) { global $langs; $out= ''; $out.= ''.img_picto($langs->trans($text_off),'switch_off').''; $out.= ''.img_picto($langs->trans($text_on),'switch_on').''; return $out; }