+ if ($selected_id == null) {
+ $selected_id = get_post($name, (string)$opts['default']);
+ }
+ if(!is_array($selected_id))
+ $selected_id = array((string)$selected_id); // code is generalized for multiple selection support
+
+ $txt = get_post($search_box);
+ $rel = '';
+ $limit = '';
+ if (isset($_POST['_'.$name.'_update'])) { // select list or search box change
+ if ($by_id) $txt = $_POST[$name];
+
+ if (!$opts['async'])
+ $Ajax->activate('_page_body');
+ else
+ $Ajax->activate($name);
+ }
+ if (isset($_POST[$search_button])) {
+ if (!$opts['async'])
+ $Ajax->activate('_page_body');
+ else
+ $Ajax->activate($name);
+ }
+ if ($search_box) {
+ // search related sql modifications
+
+ $rel = "rel='$search_box'"; // set relation to list
+ if ($opts['search_submit']) {
+ if (isset($_POST[$search_button])) {
+ $selected_id = array(); // ignore selected_id while search
+ if (!$opts['async'])
+ $Ajax->activate('_page_body');
+ else
+ $Ajax->activate($name);
+ }
+ if ($txt == '') {
+ if ($spec_option === false && $selected_id == array())
+ $limit = ' LIMIT 1';
+ else
+ $opts['where'][] = $valfield . "=". db_escape(get_post($name, $spec_id));
+ }
+ else
+ if ($txt != '*') {
+
+ foreach($opts['search'] as $i=> $s)
+ $opts['search'][$i] = $s . " LIKE "
+ .db_escape(($class=='combo3' ? '' : '%').$txt.'%');
+ $opts['where'][] = '('. implode(' OR ', $opts['search']) . ')';
+ }
+ }
+ }
+
+ // sql completion
+ if (count($opts['where'])) {
+ $where = strpos($sql, 'WHERE')==false ? ' WHERE ':' AND ';
+ $where .= '('. implode(' AND ', $opts['where']) . ')';
+ $group_pos = strpos($sql, 'GROUP BY');
+ if ($group_pos) {
+ $group = substr($sql, $group_pos);
+ $sql = substr($sql, 0, $group_pos) . $where.' '.$group;
+ } else {
+ $sql .= $where;
+ }
+ }
+ if ($opts['order'] != false) {
+ if (!is_array($opts['order']))
+ $opts['order'] = array($opts['order']);
+ $sql .= ' ORDER BY '.implode(',',$opts['order']);
+ }
+
+ $sql .= $limit;
+ // ------ make selector ----------
+ $selector = $first_opt = '';
+ $first_id = false;
+ $found = false;
+ $lastcat = null;
+ $edit = false;
+ $pname = false;
+ if (($type === "customer" || $type === "supplier") && !empty($SysPrefs->prefs['shortname_name_in_list']))
+ $pname = true;
+ if($result = db_query($sql)) {
+ while ($contact_row = db_fetch($result)) {
+ $value = $contact_row[0];
+ $descr = $opts['format']==null ? $contact_row[1] :
+ call_user_func($opts['format'], $contact_row, $pname);
+ $sel = '';
+ if (get_post($search_button) && ($txt == $value)) {
+ $selected_id[] = $value;
+ }
+
+ if (in_array((string)$value, $selected_id, true)) {
+ $sel = 'selected';
+ $found = $value;
+ $edit = $opts['editable'] && $contact_row['editable']
+ && (@$_POST[$search_box] == $value)
+ ? $contact_row[1] : false; // get non-formatted description
+ if ($edit)
+ break; // selected field is editable - abandon list construction
+ }
+ // show selected option even if inactive
+ if (!$opts['show_inactive'] && @$contact_row['inactive'] && $sel==='') {
+ continue;
+ } else
+ $optclass = @$contact_row['inactive'] ? "class='inactive'" : '';
+
+ if ($first_id === false) {
+ $first_id = $value;
+ $first_opt = $descr;
+ }
+ $cat = $contact_row[$opts['category']];
+ if ($opts['category'] !== false && $cat != $lastcat){
+ if ($lastcat!==null)
+ $selector .= "</optgroup>";
+ $selector .= "<optgroup label='".$cat."'>\n";
+ $lastcat = $cat;
+ }
+ $selector .= "<option $sel $optclass value='$value'>$descr</option>\n";
+ }
+ if ($lastcat!==null)
+ $selector .= "</optgroup>";
+ db_free_result($result);
+ }
+
+ // Prepend special option.
+ if ($spec_option !== false) { // if special option used - add it
+ $first_id = $spec_id;
+ $first_opt = $spec_option;
+ $sel = $found===false ? 'selected' : '';
+ $optclass = @$contact_row['inactive'] ? "class='inactive'" : '';
+ $selector = "<option $sel value='$first_id'>$first_opt</option>\n"
+ . $selector;
+ }
+
+ if ($found===false) {
+ $selected_id = array($first_id);
+ }
+
+ $_POST[$name] = $multi ? $selected_id : $selected_id[0];
+
+ if ($SysPrefs->use_popup_search)
+ $selector = "<select id='$name' autocomplete='off' ".($multi ? "multiple" : '')
+ . ($opts['height']!==false ? ' size="'.$opts['height'].'"' : '')
+ . "$disabled name='$name".($multi ? '[]':'')."' class='$class' title='"
+ . $opts['sel_hint']."' $rel>".$selector."</select>\n";