* @package AgileBill * @version 1.4.93 */ class CORE_search { var $recent_js; var $recent_menu; var $saved_js; var $saved_menu; /** * Create a new search record. * * @return void * @since Version 1.0 * @param array Contains the elements of the search query */ function add($arr) { $db = &DB(); # determine the search id: $this->id = $db->GenID(AGILE_DB_PREFIX . 'search_id'); # safely store the SQL Query: $sql = $db->qstr($arr['sql']); # set the time when this record expires $date_expire = (time() + (SEARCH_EXPIRE*60)); # create the search record $q = "INSERT INTO " . AGILE_DB_PREFIX . "search SET id = '" . $this->id . "', site_id = '" . DEFAULT_SITE . "', session_id = '" . SESS . "', account_id = '" . SESS_ACCOUNT . "', module = '" . $arr['module'] . "', date_orig = '" . time() . "', date_expire = '" . $date_expire . "', full_sql = $sql, order_by = '" . $arr['order_by'] . "', limit_no = '" . $arr['limit'] . "', results = '" . $arr['results'] . "'"; $result = $db->Execute($q); # error reporting if ($result === false) { global $C_debug; $C_debug->error('search.inc.php','add', $db->ErrorMsg()); } } /** * Retrieves a specific search record, and sets the values to the object. * * @return void * @since Version 1.0 * @todo Complete the search refresh feature * @param int Contians the Search Id to be retrieved */ function get($id) { # get the details for this search $db = &DB(); $q = "SELECT * FROM " . AGILE_DB_PREFIX . "search WHERE id = '" . $id . "' AND site_id = '" . DEFAULT_SITE . "'"; $result = $db->Execute($q); # error reporting if ($result === false) { global $C_debug; $C_debug->error('search.inc.php','get', $db->ErrorMsg()); } # get the search values $this->id = $id; $this->account = $result->fields['account_id']; $this->session = $result->fields['session_id']; $this->date_orig = $result->fields['date_orig']; $this->date_expire = $result->fields['date_expire']; $this->sql = $result->fields['full_sql']; $this->order_by = $result->fields['order_by']; $this->limit = $result->fields['limit_no']; # check if this search has expired: if($this->date_expire <= time()) { # refresh the search # $this->results = $this->refresh($id); # echo "
this search has expired! Refreshing....
"; $this->results = $result->fields['results']; } else { # use the existing result count $this->results = $result->fields['results']; } return; } /** * Refreshes the result count of a specific search and stores the new results in the search record, * and returns the new search result count. * * @return int Contains the new search results count * @since Version 1.0 * @todo Complete the search refresh code * @param int Contians the Search Id to be refreshed * @return int The new search results count */ function refresh($id) { } /** * Saves the current search for later retreival. * * @return void * @since Version 1.0 * @todo Add some error checking for previously used nicknames, identical searches, etc. * @param int Contians the Search Id to be saved * @param string Contains the name of the Module this search was for * @param string Contains search nickname to remember this search as */ function save($search_id,$module,$name) { # save the search $db = &DB(); # determine the search id: $this->id = $db->GenID('search_saved'); $n = $db->qstr($name); # generate the insert statement $q = "INSERT INTO " . AGILE_DB_PREFIX . "search_saved SET id = '$this->id', site_id = '" . DEFAULT_SITE . "', search_id = '$search_id', account_id = '" . SESS_ACCOUNT . "', session_id = '" . SESS . "', date_orig = '" . time() . "', date_last = '" . time() . "', date_expire = '', module = '$module', name = $n"; $result = $db->Execute($q); # error reporting if ($result === false) { global $C_debug; $C_debug->error('search.inc.php','refresh', $db->ErrorMsg()); } } /** * Build the recent search menu and JavaScript * * @return void * @since Version 1.0 * @param string Contains the name of the Module to find recent searches for */ function build_recent($module) { # disable for now return 0; if(isset($this->arr)) unset ($this->arr); # get the recent searches $db = &DB(); $q = "SELECT id, date_orig, date_expire, full_sql, order_by, limit_no FROM " . AGILE_DB_PREFIX . "search WHERE session_id = '" . SESS . "' OR account_id = '" . SESS_ACCOUNT . "' AND module = '$module' AND date_expire >= '" . time() . "' AND site_id = '" . DEFAULT_SITE . "'"; $result = $db->Execute($q); # error reporting if ($result === false) { global $C_debug; $C_debug->error('search.inc.php','build_recent', $db->ErrorMsg()); } $results = $result->RecordCount(); $i = 0; while (!$result->EOF) { # get the fields for this loop $sql = $result->fields['full_sql']; # remove the WHERE $sql = trim($sql); $sql = eregi_replace("WHERE","",$sql); $sql = eregi_replace("AND$","",$sql); $sql = trim($sql); # replace any sql statements before we split the string $sql = ereg_replace(" = ","===",$sql); $sql = ereg_replace(" LIKE ","===",$sql); # determine the number of fields $ii=0; if(ereg(" AND ", $sql)) { $sql = explode(" AND ",$sql); $this_fields = count($sql); # loop for($count=0; $count < $this_fields; $count++) { # do each field $sqls = explode("==",$sql[$count]); $field[$count][name] = $sqls[0]; $field[$count][value] = ereg_replace("'","",$sqls[1]); $field[$count][value] = ereg_replace("=","",$field[$count][value]); # check that the name & value are both set... if($field[$count][value] != '' && $field[$count][name] != '') { if(!isset($this->arr[$i][$ii][limit])) { $this->arr[$i][$ii][search] = $result->fields['id']; $this->arr[$i][$ii][limit] = $result->fields['limit_no']; $this->arr[$i][$ii][order] = $result->fields['order_by']; } $this->arr[$i][$ii][0] = $field[$count][name]; $this->arr[$i][$ii][1] = $field[$count][value]; # echo "

Field/Name: " . $this->arr[$i][$ii][0] . " -> " . $this->arr[$i][$ii][1]; $ii++; # set the menu up for Smarty $this->recent_menu[$i] = $result->fields; } } } else { # field count $this_fields = 1; # do this one field $sqls = explode("==",$sql); $field[name] = $sqls[0]; $field[value] = ereg_replace("'","",$sqls[1]); $field[value] = ereg_replace("=","",$field[value]); # check that the name & value are both set... if($field[value] != '' && $field[name] != '') { if(!isset($this->arr[$i][$ii][limit])) { $this->arr[$i][$ii][search] = $result->fields['id']; $this->arr[$i][$ii][limit] = $result->fields['limit_no']; $this->arr[$i][$ii][order] = $result->fields['order_by']; } $this->arr[$i][$ii][0] = $field[name]; $this->arr[$i][$ii][1] = $field[value]; # echo "

Field/Name: " . $field[name] . " -> " . $field[value]; $ii++; # set the menu up for Smarty $this->recent_menu[$i] = $result->fields; } } # continue loop $result->MoveNext(); if ($ii > 0) $i++; } # finish the JS: if($i > 0 && $ii > 0) { # build the JavaScript $this->recent_js = ' "; } else { $this->recent_js = FALSE; } } # end of functino /** * Build the saved search menu and JavaScript * * @return void * @since Version 1.0 * @param string Contains the name of the Module to find saved searches for */ function build_saved($module) { # disable for now return 0; if(isset($this->arr)) unset ($this->arr); # get the saved searches # get the recent searches $db1 = &DB(); $q = "SELECT id, search_id, name FROM " . AGILE_DB_PREFIX . "search_saved WHERE session_id = '" . SESS . "' OR account_id = '" . SESS_ACCOUNT . "' AND module = '$module' AND site_id = '" . DEFAULT_SITE . "' ORDER BY name ASC"; $result1 = $db1->Execute($q); # error reporting if ($result1 === false) { global $C_debug; $C_debug->sql_error($db1->ErrorMsg()); } $i=0; while (!$result1->EOF) { # get the information for this search $db = &DB(); $q = "SELECT id, full_sql, order_by, limit_no FROM " . AGILE_DB_PREFIX . "search WHERE id = '" . $result1->fields['search_id'] . "' AND site_id = '" . DEFAULT_SITE . "'"; $result = $db->Execute($q); # error reporting if ($result === false) { global $C_debug; $C_debug->error('search.inc.php','build_saved', $db->ErrorMsg()); } # get the fields for this loop $sql = $result->fields['full_sql']; # remove the WHERE $sql = trim($sql); $sql = eregi_replace("WHERE","",$sql); $sql = eregi_replace("AND$","",$sql); $sql = trim($sql); # replace any sql statements before we split the string $sql = ereg_replace(" = ","===",$sql); $sql = ereg_replace(" LIKE ","===",$sql); # determine the number of fields $ii=0; if(ereg(" AND ", $sql)) { $sql = explode(" AND ",$sql); $this_fields = count($sql); # loop for($count=0; $count < $this_fields; $count++) { # do each field $sqls = explode("==",$sql[$count]); $field[$count][name] = $sqls[0]; $field[$count][value] = ereg_replace("'","",$sqls[1]); $field[$count][value] = ereg_replace("=","",$field[$count][value]); # check that the name & value are both set... if($field[$count][value] != '' && $field[$count][name] != '') { if(!isset($this->arr[$i][$ii][limit])) { $this->arr[$i][$ii][search] = $result->fields['id']; $this->arr[$i][$ii][limit] = $result->fields['limit_no']; $this->arr[$i][$ii][order] = $result->fields['order_by']; } $this->arr[$i][$ii][0] = $field[$count][name]; $this->arr[$i][$ii][1] = $field[$count][value]; # echo "

Field/Name: " . $this->arr[$i][$ii][0] . " -> " . $this->arr[$i][$ii][1]; $ii++; $this->saved_menu[$i] = $result->fields; $this->saved_menu[$i]["name"] = $result1->fields["name"]; } } } else { # field count $this_fields = 1; # do this one field $sqls = explode("==",$sql); $field[name] = $sqls[0]; $field[value] = ereg_replace("'","",$sqls[1]); $field[value] = ereg_replace("=","",$field[value]); # check that the name & value are both set... if($field[value] != '' && $field[name] != '') { if(!isset($this->arr[$i][$ii][limit])) { $this->arr[$i][$ii][search] = $result->fields['id']; $this->arr[$i][$ii][limit] = $result->fields['limit_no']; $this->arr[$i][$ii][order] = $result->fields['order_by']; } $this->arr[$i][$ii][0] = $field[name]; $this->arr[$i][$ii][1] = $field[value]; # echo "

Field/Name: " . $field[name] . " -> " . $field[value]; $ii++; # set the menu up for Smarty $this->saved_menu[$i] = $result->fields; $this->saved_menu[$i]["name"] = $result1->fields["name"]; } } $result1->MoveNext(); if ($ii > 0) $i++; } # finish the JS: if($i > 0 && $ii > 0) { # build the JavaScript $this->saved_js = ' "; } else { $this->saved_js = FALSE; } } } ?>