+//----------------------------------------------------------------------------
+// Universal combo generator
+// $sql must return selector values and selector texts in columns 0 & 1
+// Options are merged with defaults.
+
+function combo_input($name, $selected_id, $sql, $valfield, $namefield,
+ $options=null)
+{
+ global $Ajax;
+
+ $opts = array( // default options
+ 'where'=> array(), // additional constraints
+ 'order' => $namefield, // list sort order
+ // special option parameters
+ 'spec_option'=>false, // option text or false
+ 'spec_id' => 0, // option id
+ // submit on select parameters
+ 'select_submit' => false, //submit on select: true/false
+ 'async' => true, // select update via ajax (true) vs _page_body reload
+ // search box parameters
+ 'sel_hint' => null,
+ 'search_box' => false, // name or true/false
+ 'type' => 0, // type of extended selector:
+ // 0 - with (optional) visible search box, search by id
+ // 1 - with hidden search box, search by option text
+ // 2 - TODO reverse: box with hidden selector available via enter; this
+ // would be convenient for optional ad hoc adding of new item
+ 'search_submit' => true, //search submit button: true/false
+ 'size' => 8, // size and max of box tag
+ 'max' => 50,
+ 'cells' => false, // combo displayed as 2 <td></td> cells
+ // if _focus is not set on next field before combo_input call
+ // set this to false to avoid deadlock via onblur handler
+ 'next_focus' => (!isset($_POST['_focus']) ||
+ (isset($_POST["_{$name}_button"]) && $_POST['_focus']==$name)) ? false : $_POST['_focus'],
+ 'format' => null, // format functions for regular options
+ 'disabled' => false, // FIX todo
+ 'box_hint' => null // box/selectors hints; null = std see below
+ );
+ // ------ merge options with defaults ----------
+ $opts = array_merge($opts, $options);
+ if (!is_array($opts['where'])) $opts['where'] = array($opts['where']);
+
+ $search_box = $opts['search_box']===true ? '_'.$name.'_edit' : $opts['search_box'];
+ $search_submit = $opts['search_submit']===true ? '_'.$name.'_button' : $opts['search_submit'];
+ $select_submit = $opts['select_submit']===true ? '_'.$name.'_update' : $opts['select_submit'];
+ $spec_id = $opts['spec_id'];
+ $spec_option = $opts['spec_option'];
+ $by_id = ($opts['type'] == 0);
+ $class = $by_id ? 'combo':'combo2';
+
+ if ($opts['sel_hint'] === null)
+ $opts['sel_hint'] = $by_id || $search_box==false ?
+ '' : _('Press Space tab for search pattern entry');
+
+ if ($opts['box_hint'] === null)
+ $opts['box_hint'] =
+ $search_box ? ($by_id ? _('Enter code fragment to search or * for all')
+ : _('Enter description fragment to search or * for all')) :'';
+
+ if ($selected_id == null) {
+ $selected_id = get_post($name);
+ }
+ $txt = get_post($search_box);
+ $rel = '';
+ $limit = '';
+
+ if (isset($_POST[$select_submit])) {
+ if (!$opts['async'])
+ $Ajax->activate('_page_body');
+ else
+ $Ajax->activate($name);
+ set_focus($opts['next_focus']);
+ }
+ if ($search_box && $opts['search_submit']) {
+ // search related sql modifications
+
+ $rel = "rel='$search_box'"; // set relation to list
+
+ // if selected from list - set focus on next field
+ if (isset($_POST[$search_submit])) {
+ $Ajax->activate($name);
+ set_focus($name);
+// if ($txt == '' && get_post($name, $spec_id)!=$spec_id)
+// $opts['where'][] = $valfield."='$name'";
+ }
+ if ($txt == '') {
+ if ($spec_option === false)
+ $limit = ' LIMIT 1';
+ else
+ $opts['where'][] = $valfield . "='". get_post($name, $spec_id)."'";
+ }
+ else
+ if ($txt != '*') {
+ $opts['where'][] = ($by_id ? $valfield : $namefield) . " LIKE '%{$txt}%'";
+ }
+ }
+ // sql completion
+ if (count($opts['where'])) {
+ $sql .= strpos($sql, 'WHERE')==false ? ' WHERE ':' AND ';
+ $sql .= '('. implode($opts['where'], ' AND ') . ')';
+ }
+ if ($opts['order'] != false) {
+ $sql .= ' ORDER BY '.$opts['order'];
+ }