X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=blobdiff_plain;ds=sidebyside;f=includes%2Fui%2Fui_lists.inc;h=785ab74604515bae2e260e1fe6599615bf692faf;hb=c935a4090e3f152dd5549217d69b5b5036197024;hp=ba5f42a465d03561d3f176215ddcbb1245ed21d6;hpb=5af53b13622ca77bc851cccaa087d7e2aedc3401;p=fa-stable.git
diff --git a/includes/ui/ui_lists.inc b/includes/ui/ui_lists.inc
index ba5f42a4..785ab746 100644
--- a/includes/ui/ui_lists.inc
+++ b/includes/ui/ui_lists.inc
@@ -1,316 +1,363 @@
";
-//$search_button = " ";
+$_search_button = " ";
+
+$_select_button = " ";
$all_items = reserved_words::get_all();
-// TDB for all list functions : if there is no data, display a link to the releveant
-// page to add an item, eg. for locations, if no locations, link to Add Location
+//----------------------------------------------------------------------------
+// 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
cells
+ 'search' => array(), // sql field names to search
+ '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(!count($opts['search'])) {
+ $opts['search'] = array($by_id ? $valfield : $namefield);
+ }
+ if ($opts['sel_hint'] === null)
+ $opts['sel_hint'] = $by_id || $search_box==false ?
+ '' : _('Press Space tab for search pattern entry');
-function supplier_list($name, $selected_id, $all_option=false, $submit_on_change=false)
-{
- global $all_items;
+ 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')) :'';
- default_focus($name);
- if ($selected_id == null)
- $selected_id = ((!isset($_POST[$name]) || $_POST[$name] == "") ? "" : $_POST[$name]);
- $no_supplier_list = get_company_pref('no_supplier_list');
- if ($no_supplier_list)
- {
- global $search_button;
- $edit_name = $name."_edit";
- $edit_button = $name."_button";
+ if ($selected_id == null) {
+ $selected_id = get_post($name);
+ }
+ $txt = get_post($search_box);
+ $rel = '';
+ $limit = '';
- $val = (isset($_POST[$edit_name]) && $_POST[$edit_name] != "" ? $_POST[$edit_name] : "");
- if (isset($_POST[$edit_button]))
- {
- $selected_id = $_POST[$name] = "";
- }
+ if (isset($_POST[$select_submit])) {
+ 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 selected from list - set focus on next field
+ if (isset($_POST[$search_submit])) {
+ $Ajax->activate($name);
+ }
+ if ($txt == '') {
+ if ($spec_option === false)
+ $limit = ' LIMIT 1';
+ else
+ $opts['where'][] = $valfield . "='". get_post($name, $spec_id)."'";
+ }
+ else
+ if ($txt != '*') {
- if ($selected_id != "")
- {
- $val = DEFVAL;
- $supplier_sql = "SELECT supplier_id, supp_name, curr_code FROM ".TB_PREF."suppliers WHERE supplier_id=$selected_id";
- $supplier_result = db_query($supplier_sql);
+ foreach($opts['search'] as $i=> $s)
+ $opts['search'][$i] = $s . " LIKE '%{$txt}%'";
+ $opts['where'][] = '('. implode($opts['search'], ' OR ') . ')';
}
- else
- {
- if ($val != "" && $val != DEFVAL)
- {
- $supplier_sql = "SELECT supplier_id, supp_name, curr_code FROM ".TB_PREF."suppliers WHERE supp_name LIKE '%{$val}%' ORDER BY supp_name";
- $supplier_result = db_query($supplier_sql);
+ }
+ }
+ // 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'];
+ }
+
+ $sql .= $limit;
+ // ------ make selector ----------
+ $selector = '';
+ $first_id = $first_opt = '';
+ $found = false;
+//if($name=='code_id') display_error($sql);
+ 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);
+ $sel = '';
+ if (isset($_POST[$search_submit])) {
+ if (($by_id ? $value : $contact_row[1]) === $txt)
+ $selected_id = $value;
}
- else
- $supplier_result = false;
+ // if no initial selection - set the first item
+ if ($selected_id === "") {
+ $selected_id = $value;
+ }
+ if ($selected_id == $value) {
+ $sel = 'selected';
+ $found = $value;
+ $_POST[$name] = $selected_id;
+ }
+ if ($first_opt == false) {
+ $first_id = $value;
+ $first_opt = $descr;
+ continue;
+ }
+ $selector .= "\n";
}
-
- echo " ";
-
- echo sprintf($search_button, $edit_button);
+ db_free_result($result);
}
- else
- {
- $supplier_sql = "SELECT supplier_id, supp_name, curr_code FROM ".TB_PREF."suppliers ORDER BY supp_name";
- $supplier_result = db_query($supplier_sql);
+ // add first option - selected also if no match for selected_id was found
+ // and no special option defined
+ $sel = $spec_option===false || $found === $first_id ? 'selected' : '';
+ $selector = "\n"
+ . $selector;
+
+ // Prepend special option.
+ if ($spec_option !== false) { // if special option used - add it
+ $first_id = $spec_id;
+ $first_opt = $spec_option;
+ if (isset($_POST[$search_submit])) {
+ if ($txt == '')
+ $selected_id = $spec_id;
+ }
+ $sel = $found===false ? 'selected' : '';
+ $selector = "\n"
+ . $selector;
}
- if ($submit_on_change==true)
- echo "