From 744c263c6cef39b460852fb42f09d706e2bcda68 Mon Sep 17 00:00:00 2001 From: Marc de Lima Lucio Date: Wed, 21 Mar 2018 15:08:20 +0100 Subject: [PATCH] NEW: selectArrayFilter() + use in left menu search --- htdocs/core/class/html.form.class.php | 117 ++++++++++++++++++++++++++ htdocs/main.inc.php | 15 ++-- 2 files changed, 124 insertions(+), 8 deletions(-) diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index 23758a1de5eaf..57d742116a09b 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -5630,6 +5630,123 @@ static function selectArrayAjax($htmlname, $url, $id='', $moreparam='', $morepar return $out; } + /** + * Return a HTML select string, built from an array of key+value but content returned into select come from an Ajax call of an URL. + * Note: Do not apply langs->trans function on returned content of Ajax service, content may be entity encoded twice. + * + * @param string $htmlname Name of html select area + * @param string $array Array (key=>array('text'=>'A text', 'url'=>'An url'), ...) + * @param string $id Preselected key + * @param string $moreparam Add more parameters onto the select tag + * @param int $disableFiltering If set to 1, results are not filtered with searched string + * @param int $disabled Html select box is disabled + * @param int $minimumInputLength Minimum Input Length + * @param string $morecss Add more class to css styles + * @param int $callurlonselect If set to 1, some code is added so an url return by the ajax is called when value is selected. + * @param string $placeholder String to use as placeholder + * @param integer $acceptdelayedhtml 1 if caller request to have html js content not returned but saved into global $delayedhtmlcontent (so caller can show it at end of page to avoid flash FOUC effect) + * @return string HTML select string + * @see ajax_combobox in ajax.lib.php + */ + static function selectArrayFilter($htmlname, $array, $id='', $moreparam='', $disableFiltering=0, $disabled=0, $minimumInputLength=1, $morecss='', $callurlonselect=0, $placeholder='', $acceptdelayedhtml=0) + { + global $conf, $langs; + global $delayedhtmlcontent; + + // TODO Use an internal dolibarr component instead of select2 + if (empty($conf->global->MAIN_USE_JQUERY_MULTISELECT) && ! defined('REQUIRE_JQUERY_MULTISELECT')) return ''; + + $out=''; + + $formattedarrayresult = array(); + + foreach($array as $key => $value) { + $o = new stdClass(); + $o->id = $key; + $o->text = $value['text']; + $o->url = $value['url']; + $formattedarrayresult[] = $o; + } + + $tmpplugin='select2'; + $outdelayed="\n".' + '; + + if ($acceptdelayedhtml) + { + $delayedhtmlcontent.=$outdelayed; + } + else + { + $out.=$outdelayed; + } + return $out; + } + /** * Show a multiselect form from an array. * diff --git a/htdocs/main.inc.php b/htdocs/main.inc.php index 115155e629a94..3aaabe4cf72b0 100644 --- a/htdocs/main.inc.php +++ b/htdocs/main.inc.php @@ -1619,19 +1619,18 @@ function left_menu($menu_array_before, $helppagename='', $notused='', $menu_arra if ($conf->browser->layout == 'phone') $conf->global->MAIN_USE_OLD_SEARCH_FORM=1; // Select into select2 is awfull on smartphone. TODO Is this still true with select2 v4 ? print "\n"; + + if (! is_object($form)) $form=new Form($db); + $selected=-1; + $usedbyinclude=1; + include_once DOL_DOCUMENT_ROOT.'/core/ajax/selectsearchbox.php'; + if ($conf->use_javascript_ajax && empty($conf->global->MAIN_USE_OLD_SEARCH_FORM)) { - if (! is_object($form)) $form=new Form($db); - $selected=-1; - $searchform.=$form->selectArrayAjax('searchselectcombo', DOL_URL_ROOT.'/core/ajax/selectsearchbox.php', $selected, '', '', 0, 1, 'vmenusearchselectcombo', 1, $langs->trans("Search"), 1); + $searchform.=$form->selectArrayFilter('searchselectcombo', $arrayresult, $selected, '', 1, 0, 1, 'vmenusearchselectcombo' , 1, $langs->trans("Search"), 1); } else { - if (! is_object($form)) $form=new Form($db); - $selected=-1; - $usedbyinclude=1; - include_once DOL_DOCUMENT_ROOT.'/core/ajax/selectsearchbox.php'; - foreach($arrayresult as $key => $val) { //$searchform.=printSearchForm($val['url'], $val['url'], $val['label'], 'maxwidth100', 'sall', $val['shortcut'], 'searchleft', img_picto('',$val['img']));