From 41a516f1c476abade8fddf6ee3cb5b873ee8f301 Mon Sep 17 00:00:00 2001 From: atm-ph Date: Fri, 29 Jun 2018 11:48:54 +0200 Subject: [PATCH] New parameter to transform select into multiselect --- htdocs/core/class/html.form.class.php | 63 +++++++++++++++++---------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index 9a9d3279e03..99f567f5434 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -1001,9 +1001,10 @@ class Form * @param string $selected_input_value Value of preselected input text (for use with ajax) * @param int $hidelabel Hide label (0=no, 1=yes, 2=show search icon (before) and placeholder, 3 search icon after) * @param array $ajaxoptions Options for ajax_autocompleter + * @param bool $multiple add [] in the name of element and add 'multiple' attribut (not working with ajax_autocompleter) * @return string HTML string with select box for thirdparty. */ - function select_company($selected='', $htmlname='socid', $filter='', $showempty='', $showtype=0, $forcecombo=0, $events=array(), $limit=0, $morecss='minwidth100', $moreparam='', $selected_input_value='', $hidelabel=1, $ajaxoptions=array()) + function select_company($selected='', $htmlname='socid', $filter='', $showempty='', $showtype=0, $forcecombo=0, $events=array(), $limit=0, $morecss='minwidth100', $moreparam='', $selected_input_value='', $hidelabel=1, $ajaxoptions=array(), $multiple=false) { global $conf,$user,$langs; @@ -1040,7 +1041,7 @@ class Form else { // Immediate load of all database - $out.=$this->select_thirdparty_list($selected, $htmlname, $filter, $showempty, $showtype, $forcecombo, $events, '', 0, $limit, $morecss, $moreparam); + $out.=$this->select_thirdparty_list($selected, $htmlname, $filter, $showempty, $showtype, $forcecombo, $events, '', 0, $limit, $morecss, $moreparam, $multiple); } return $out; @@ -1062,16 +1063,20 @@ class Form * @param int $limit Limit number of answers * @param string $morecss Add more css styles to the SELECT component * @param string $moreparam Add more parameters onto the select tag. For example 'style="width: 95%"' to avoid select2 component to go over parent container + * @param bool $multiple add [] in the name of element and add 'multiple' attribut * @return string HTML string with */ - function select_thirdparty_list($selected='',$htmlname='socid',$filter='',$showempty='', $showtype=0, $forcecombo=0, $events=array(), $filterkey='', $outputmode=0, $limit=0, $morecss='minwidth100', $moreparam='') + function select_thirdparty_list($selected='',$htmlname='socid',$filter='',$showempty='', $showtype=0, $forcecombo=0, $events=array(), $filterkey='', $outputmode=0, $limit=0, $morecss='minwidth100', $moreparam='', $multiple=false) { global $conf,$user,$langs; $out=''; $num=0; $outarray=array(); - + + if ($selected === '') $selected = array(); + else if (!is_array($selected)) $selected = array($selected); + // Clean $filter that may contains sql conditions so sql code if (function_exists('test_sql_and_script_inject')) $filter = test_sql_and_script_inject($filter, 3); @@ -1120,7 +1125,7 @@ class Form } // Construct $out and $outarray - $out.= ''."\n"; $textifempty=''; // Do not use textifempty = ' ' or ' ' here, or search on key will search on ' key'. @@ -1169,7 +1174,7 @@ class Form if (empty($outputmode)) { - if ($selected > 0 && $selected == $obj->rowid) + if (in_array($obj->rowid,$selected)) { $out.= ''; } @@ -1320,15 +1325,19 @@ class Form * @param array $events Event options. Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/contacts.php',1), 'htmlname'=>'contactid', 'params'=>array('add-customer-contact'=>'disabled'))) * @param string $moreparam Add more parameters onto the select tag. For example 'style="width: 95%"' to avoid select2 component to go over parent container * @param string $htmlid Html id to use instead of htmlname + * @param bool $multiple add [] in the name of element and add 'multiple' attribut * @return int <0 if KO, Nb of contact in list if OK */ - function selectcontacts($socid, $selected='', $htmlname='contactid', $showempty=0, $exclude='', $limitto='', $showfunction=0, $moreclass='', $options_only=false, $showsoc=0, $forcecombo=0, $events=array(), $moreparam='', $htmlid='') + function selectcontacts($socid, $selected='', $htmlname='contactid', $showempty=0, $exclude='', $limitto='', $showfunction=0, $moreclass='', $options_only=false, $showsoc=0, $forcecombo=0, $events=array(), $moreparam='', $htmlid='', $multiple=false) { global $conf,$langs; $langs->load('companies'); if (empty($htmlid)) $htmlid = $htmlname; + + if ($selected === '') $selected = array(); + else if (!is_array($selected)) $selected = array($selected); $out=''; // On recherche les societes @@ -1353,9 +1362,9 @@ class Form $out .= ajax_combobox($htmlid, $events, $conf->global->CONTACT_USE_SEARCH_TO_SELECT); } - if ($htmlname != 'none' || $options_only) $out.= ''; + if ($showempty == 1 && !$multiple) $out.= ''; + if ($showempty == 2) $out.= ''; $num = $this->db->num_rows($resql); $i = 0; if ($num) @@ -1363,7 +1372,6 @@ class Form include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php'; $contactstatic=new Contact($this->db); - if (!is_array($selected)) $selected = array($selected); while ($i < $num) { $obj = $this->db->fetch_object($resql); @@ -1413,7 +1421,7 @@ class Form } else { - $out.= ''; + $out.= ''; } if ($htmlname != 'none' || $options_only) { @@ -1469,16 +1477,20 @@ class Form * @param string $morecss More css * @param int $noactive Show only active users (this will also happened whatever is this option if USER_HIDE_INACTIVE_IN_COMBOBOX is on). * @param int $outputmode 0=HTML select string, 1=Array + * @param bool $multiple add [] in the name of element and add 'multiple' attribut * @return string HTML select string * @see select_dolgroups */ - function select_dolusers($selected='', $htmlname='userid', $show_empty=0, $exclude=null, $disabled=0, $include='', $enableonly='', $force_entity='0', $maxlength=0, $showstatus=0, $morefilter='', $show_every=0, $enableonlytext='', $morecss='', $noactive=0, $outputmode=0) + function select_dolusers($selected='', $htmlname='userid', $show_empty=0, $exclude=null, $disabled=0, $include='', $enableonly='', $force_entity='0', $maxlength=0, $showstatus=0, $morefilter='', $show_every=0, $enableonlytext='', $morecss='', $noactive=0, $outputmode=0, $multiple=false) { global $conf,$user,$langs; - + // If no preselected user defined, we take current user if ((is_numeric($selected) && ($selected < -2 || empty($selected))) && empty($conf->global->SOCIETE_DISABLE_DEFAULT_SALESREPRESENTATIVE)) $selected=$user->id; + if ($selected === '') $selected = array(); + else if (!is_array($selected)) $selected = array($selected); + $excludeUsers=null; $includeUsers=null; @@ -1551,12 +1563,12 @@ class Form $out .= ajax_combobox($htmlname); // do not use maxwidthonsmartphone by default. Set it by caller so auto size to 100% will work when not defined - $out.= ''; + if ($show_empty && !$multiple) $out.= ''."\n"; + if ($show_every) $out.= ''."\n"; $userstatic=new User($this->db); - + while ($i < $num) { $obj = $this->db->fetch_object($resql); @@ -1568,7 +1580,7 @@ class Form $disableline=''; if (is_array($enableonly) && count($enableonly) && ! in_array($obj->rowid,$enableonly)) $disableline=($enableonlytext?$enableonlytext:'1'); - if ((is_object($selected) && $selected->id == $obj->rowid) || (! is_object($selected) && $selected == $obj->rowid)) + if ((is_object($selected) && $selected->id == $obj->rowid) || (! is_object($selected) && in_array($obj->rowid,$selected) )) { $out.= '