200) $searchtext=substr($searchtext,0,200); $searchtype=$_REQUEST['searchtype']; $subsearchtype=$_REQUEST['subsearchtype']; $check_perms = sprintf('(not rhed or %s) and (not bcs or %s) and (not dealer or %s)', ($auth->has_perm('gatnky')?'true':'false'), ($auth->has_perm('bcs')?'true':'false'), ($auth->has_perm('dealer')?'true':'false')); $isadmin = $auth->has_perm('admin'); unset($contact_id); if ($auth->logged_in()) { $contact_id=$auth->userinfo['contact_id']; } # make sure this only exists if there is an exact match on a part # number unset($exact_match); $contact_id=0; $entity_id=0; if ($auth->logged_in()) { $contact_id=$auth->userinfo['contact_id']; $entity_id=$auth->userinfo['entity_id']; } $prodtypes=array('U'=>'Units','P'=>'Parts','A'=>'Accessories'); $searchtypes=array('full'=>'products', 'desc'=>'product description', 'part'=>'part number', 'vendor'=>'vendor name', 'category'=>'product category', 'vendcat'=>'vendor and category', 'brand'=>'brand name', 'brandcat'=>'brand and category'); $subsearchtypes=array('vendor'=>'vendor name', 'category'=>'product category', 'vendcat'=>'vendor and category', 'brand'=>'brand name', 'brandcat'=>'brand and category'); if ($s_category_id && !$category_name) { $db->query(sprintf(" select category_name from categories where category_id=%d and %s ", $s_category_id, $check_perms)); if ($db->next_record()) { extract($db->Record, EXTR_OVERWRITE); } } if ($s_brand_id && !$brand_name) { $db->query(sprintf(" select brand_name from brands where id=%d ", $s_brand_id)); if ($db->next_record()) { extract($db->Record, EXTR_OVERWRITE); } } if ($s_vendor_id && !$vendor_name) { $db->query(sprintf(" select vendor_name from vendors where vendor_id=%d ", $s_vendor_id)); if ($db->next_record()) { extract($db->Record, EXTR_OVERWRITE); } } # If the search type is unknown, just show the form if (!$search_id && (!$searchtypes[$searchtype] || ($subsearchtype && !$subsearchtypes[$subsearchtype]))) { page_header(); print "\n"; show_search(); page_footer(); exit; } # Nothing to search for, show the form if ((!$searchtext || $searchtext=='Enter Part # or Description') && !$search_id && !$s_brand_id && !$s_vendor_id && !$s_category_id) { page_header(); print "\n"; show_search(); page_footer(); exit; } # Check to see if the search exists, otherwise we'll search again $new_search=true; if ($search_id) { $db->query(sprintf(" select first_item, total_rows, overflow, searchtext, searchtype, subsearchtype, vendor_group_id as s_vendor_id, brand_id as s_brand_id, category_id as s_category_id from searches where search_id=%d and (sid='%s' or %s or contact_id=%d or entity_id=%d) ", $search_id, pg_escape_string(session_id()), $isadmin?'true':'false', $contact_id?$contact_id:-1, $entity_id?$entity_id:-1)); if ($db->next_record()) { extract($db->Record, EXTR_OVERWRITE); $db->query(sprintf(" select search_id from search_results where sequence=%d ", $first_item)); if ($db->next_record() && $db->f('search_id')==$search_id) { $new_search=false; } } else { unset($search_id); } } if ($new_search && ($subsearchtype || $searchtype=='vendor' || $searchtype=='category' || $searchtype=='vendcat' || $searchtype=='brand' || $searchtype=='brandcat')) { $products=array(); $epns=array(); if ($subsearchtype) { $type=$subsearchtype; } else { $type=$searchtype; } unset($query); if ($type=='vendor' && $s_vendor_id) { # We don't check the vendor id in the table because we're # really using the vendor_group_id. At some point, we might # actually split out vendor_id and vendor_group_id, but # assume that for now when we see vendor id, it's really a # group id. $table='items_in_vendor'; $query=sprintf(" select start_seq, end_seq from iiv_lookup where vendor_group_id=%d ", $s_vendor_id); } elseif ($type=='category' && $s_category_id) { $table='items_in_category'; $query=sprintf(" select start_seq, end_seq, category_name from iic_lookup l, categories c where l.category_id=%d and c.category_id=l.category_id and %s ", $s_category_id, $check_perms); } elseif ($type=='vendcat' && $s_vendor_id && $s_category_id) { $table='items_in_vendcat'; $query=sprintf(" select start_seq, end_seq, category_name from iivc_lookup l, categories c where l.category_id=%d and l.vendor_group_id=%d and c.category_id=l.category_id and %s ", $s_category_id, $s_vendor_id, $check_perms); } elseif ($type=='brand' && $s_brand_id) { $table='items_in_brand'; $query=sprintf(" select start_seq, end_seq from iib_lookup where brand_id=%d ", $s_brand_id); } elseif ($type=='brandcat' && $s_brand_id && $s_category_id) { $table='items_in_brandcat'; $query=sprintf(" select start_seq, end_seq, category_name from iibc_lookup l, categories c where l.category_id=%d and l.brand_id=%d and c.category_id=l.category_id and %s ", $s_category_id, $s_brand_id, $check_perms); } else { page_header(); show_search(); page_footer(); exit; } $db->query($query); if ($db->next_record()) { extract($db->Record, EXTR_OVERWRITE); } else { if ($isadmin) { page_header(); print "
\n${query}\n\n"; } die_msg("
Item not found during search.
"); } if ($subsearchtype) { $subtable=", ${table} t"; $subwc=sprintf(' and p.eclipse_part_number=t.eclipse_part_number and t.sequence between %d and %d ', $start_seq, $end_seq); # I have subtable and subwc to pass on to the next section. } else { # $table is set above $first_item=$start_seq; $total_rows=$end_seq-$start_seq+1; #if (!$start_seq || !$end_seq) { # # this is to make the query fail # $page_start=-1; $page_end=-1; #} else { # $page_start=($page*$items_per_page)+$start_seq; # $page_end=$page_start+$items_per_page-1; # if ($page_start>$end_seq) { # # went too far # die_msg("You've moved past # the last page.
"); # } elseif ($page_end>$end_seq) { # $page_end=$end_seq; # } #} # #$rowcount=$end_seq-$start_seq+1; } } if ($new_search && $searchtext && ($searchtype=='desc' || $searchtype=='part' || $searchtype=='full')) { if ($search_id) { $new_search_id=false; } else { $new_search_id=true; $db->query("select nextval('searches_search_id_seq') as search_id"); if ($db->next_record()) { $search_id=$db->f('search_id'); } else { #ACK! die_msg("An unexpected error has arisn, and we cannot process your search at this time.
\n"); } } unset($search_query); if ($searchtype=='desc') { $search_query=sprintf(" insert into search_results (search_id, mta_part_number, eclipse_part_number) select %d as search_id, mta_part_number, p.eclipse_part_number from products p %s where description like '%%%s%%' and goto_part_number='' and %s %s order by prodtype desc, description, mta_part_number limit %d ", $search_id, $subtable, strtoupper(pg_escape_string($searchtext)), $check_perms, $subwc, $limit+1); } elseif ($searchtype=='part') { $searchtext=strtoupper(trim($searchtext)); $searchtext2=preg_replace('/-/',' ',$searchtext); if ($searchtext!=$searchtext2) { $wc=sprintf("(mta_part_number like '%s%%' or mta_part_number like '%s%%')", pg_escape_string($searchtext), pg_escape_string($searchtext2)); } else { $wc=sprintf("mta_part_number like '%s%%'", pg_escape_string($searchtext)); } $search_query=sprintf(" insert into search_results (search_id, mta_part_number, eclipse_part_number) select %d as search_id, mta_part_number, p.eclipse_part_number from products p %s where %s and %s %s order by prodtype desc, description, mta_part_number limit %d ", $search_id, $subtable, $wc, $check_perms, $subwc, $limit+1); } elseif ($searchtype=='full') { # The raw query # select mta_part_number, (case when tier1 @@ 'OIL&DRAIN' # then 1000 else 0 end) + (case when tier2 @@ 'OIL&DRAIN' then 100 # else 0 end) as ranking # from search_rankings # where tier1 @@ 'OIL&DRAIN' or tier2 @@ 'OIL&DRAIN' # order by ranking, mta_part_number # limit 201; $searchtext=strtolower($searchtext); $s=''; $wordcount=0; $words=preg_split('/\s+/',$searchtext); for ($i=0 ; $iDue to a fatal error within our search application, your search cannot be completed.
\n"; page_footer(); exit; } if ($total_rows>$limit) { $total_rows=$limit; $overflow='t'; } else { $overflow='f'; } } else { $first_item=0; $total_rows=0; $overflow='f'; } $search_query=preg_replace('/\t/m',' ',$search_query,-1); if ($new_search_id) { $db->query(sprintf(" insert into searches (search_id, entity_id, contact_id, sid, searchtype, searchtext, subsearchtype, vendor_group_id, brand_id, category_id, search_time, first_item, total_rows, overflow, query_text, query_time) values (%d, %d, %d, '%s', '%s', '%s', '%s', %d, %d, %d, now(), %d, %d, '%s', '%s', %f)", $search_id, $entity_id, $contact_id, session_id(), pg_escape_string($searchtype), pg_escape_string($searchtext), pg_escape_string($subsearchtype), $s_vendor_id, $s_brand_id, $s_category_id, $first_item, $total_rows, $overflow, pg_escape_string($search_query), $query_time)); } else { # There's already a record in "searches", so we'll just update it $db->query(sprintf(" update searches set first_item=%d, total_rows=%d, overflow='%s', query_text='%s', query_time=%f where search_id=%d ", $first_item, $total_rows, $overflow, pg_escape_string($search_query), $query_time, $search_id)); } if ($total_rows) { header('Status: 302 Moved Temporarily'); header(sprintf('Location: search.php?page=%d&search_id=%d&searchtext=%s&searchtype=%s',$page,$search_id,urlencode($searchtext),urlencode($searchtype))); exit; } else { page_header(); show_static('dead search'); show_search(); page_footer(); exit; } } if ($search_id) { # We grabbed the relevant record from searches back up around line 80, # so here we need to $products=array(); $epns=array(); # Check for an exact match if ($page==0 && $total_rows>1 && ($searchtype=='part' || ($searchtype=='full' && !preg_match('/ /',$searchtext)))) { $searchtext=strtoupper($searchtext); $searchtext2=preg_replace('/-/',' ',$searchtext); $db->query(sprintf(" select distinct eclipse_part_number, mta_part_number, description, unit_of_measure, uom_quantity, price_line, buy_line, weight, vendor_id, status, discontinued, goto_part_number, prodtype from products where mta_part_number in ('%s','%s') and %s ", pg_escape_string($searchtext), pg_escape_string($searchtext2), $check_perms)); if ($db->num_rows()) { # grab the exact match while ($db->next_record()) { $exact_match=$db->Record; $exact_match['prodtype']='Exact Match'; $products[]=$exact_match; $epns[]=$exact_match['eclipse_part_number']; } } } #$rowcount=$total_rows; $table='search_results'; } if ($table && $first_item && $total_rows) { if (!$first_item) { # this is to make the query fail $page_start=-1; $page_end=-1; } else { $page_start=($page*$items_per_page)+$first_item; $page_end=$page_start+$items_per_page-1; if ($page_end>=$first_item+$total_rows) { $page_end=$first_item+$total_rows-1; } if ($page_start>$page_end) { die_msg("Attempt to move past the last page.
"); } } $db->query(sprintf(" select distinct sequence, p.eclipse_part_number, p.mta_part_number, description, unit_of_measure, uom_quantity, price_line, buy_line, weight, p.status, vendor_id, discontinued, goto_part_number %s from products p, %s t where p.eclipse_part_number=t.eclipse_part_number and t.sequence between %d and %d order by t.sequence ",$searchtype=='full'?'':', prodtype', $table, $page_start, $page_end)); while ($db->next_record()) { $rec=$db->Record; if ($searchtype=='full') { if ($exact_match) { $rec['prodtype']='Other Matching Items'; } else { $rec['prodtype']='Matching Items'; } } else { $rec['prodtype']=$prodtypes[$rec['prodtype']]; } $products[]=$rec; $epns[]=$db->f('eclipse_part_number'); } } $decorations='left_menu'; $page_title = iso_htmlentities($vendor_name . $brand_name); page_header(); if ($searchtype=='vendcat') { printf('";
}
if ($s_brand_id) {
$db->query(sprintf("
select category_name, category_id from categories
where %s and category_id in
(select category_id from iibc_lookup
where brand_id=%d)
order by category_name
", $check_perms, $s_brand_id));
$numcats=$db->num_rows();
if ($numcats==1 && !$s_category_id) {
$db->next_record();
printf("Category: %s\n", iso_htmlentities($db->f('category_name'))); } elseif ($numcats>1) { print '\n"; } } if ($s_category_id) { $db->query(sprintf(" select distinct brand_name, id as brand_id from brands where brand_name!='' and id in (select brand_id from iibc_lookup where category_id=%d) order by brand_name ", $s_category_id)); if ($db->num_rows()==1 && !$s_brand_id) { $db->next_record(); printf("Brand: %s\n", iso_htmlentities($db->f('brand_name'))); } elseif ($db->num_rows()>1) { if ($numcats>1 && $s_brand_id && $s_category_id) { print " | --OR-- | "; } print '\n"; } } if ($s_brand_id && $s_category_id) { print " |
";
}
if ($s_vendor_id) {
$db->query(sprintf("
select category_name, category_id from categories
where %s and category_id in
(select category_id from iivc_lookup
where vendor_group_id=%d)
order by category_name
", $check_perms, $s_vendor_id));
$numcats=$db->num_rows();
if ($numcats==1 && !$s_category_id) {
$db->next_record();
printf("Category: %s\n", iso_htmlentities($db->f('category_name'))); } elseif ($numcats>1) { print '\n"; } } if ($s_category_id) { $db->query(sprintf(" select distinct vendor_name, vendor_group_id from vendors where vendor_name!='' and vendor_id in (select vendor_group_id from iivc_lookup where category_id=%d) order by vendor_name ", $s_category_id)); if ($db->num_rows()==1 && !$s_vendor_id) { $db->next_record(); printf("Vendor: %s\n", iso_htmlentities($db->f('vendor_name'))); } elseif ($db->num_rows()>1) { if ($numcats>1 && $s_vendor_id && $s_category_id) { print " | --OR-- | "; } print '\n"; } } if ($s_vendor_id && $s_category_id) { print " |
Search Navigation:\n"; if ($page>0) { // previous link make_search_url($page-1,'Previous'); } if ($page>=$page_limit*.7) { $start_page=max(min($page-($page_limit/2),$page_total-$page_limit),0); } else { $start_page=0; } if ($start_page>0) { make_search_url(0,'1'); if ($start_page>2) { make_search_url(max($page-10,0),'...'); } elseif ($start_page==2) { make_search_url(1,'2'); } } // pages for ($num=$start_page; $num<$start_page+$page_limit && $num<$page_total; $num++) { if ($num==$page) { printf("%d\n",$num+1); } else { make_search_url($num,$num+1); } } if ($num<$page_total) { if ($num<$page_total-2) { make_search_url(min($page+10,$page_total-1),'...'); } elseif ($num==$page_total-2) { make_search_url($page_total-2,$page_total-1); } make_search_url($page_total-1,$page_total); } if ($page<$page_total-1) { // next link make_search_url($page+1,'Next'); } if ($overflow=='t') { show_static('search too broad'); } print "
\n"; } } page_header(); // show_search(); page_footer(); function show_search() { global $auth,$db,$searchtype,$subsearchtype,$searchtext; global $s_vendor_id,$s_category_id,$category_name,$vendor_name; global $s_brand_id, $brand_name; printf('