cleaning up formatting of searchResults()

This commit is contained in:
kados 2006-06-08 07:21:42 +00:00
parent 09a2024937
commit 0abe02f785
1 changed files with 155 additions and 145 deletions

View File

@ -11,6 +11,7 @@ use CGI;
my $query=new CGI;
my $op = $query->param('op'); #show the search form or execute the search
my $cql_query = $query->param('cql_query');
my @pqf_query_history = $query->param('pqf_query_history');
my @newresults;
my ($template,$borrowernumber,$cookie);
my @forminputs; #this is for the links to navigate among the results when they are more than the maximum number of results per page
@ -29,13 +30,14 @@ if ($op eq 'get_results') { # Yea, we're searching, load the results template
$number_of_results = 20 unless ($number_of_results); #this could be a parameter with 20 50 or 100 results per page
my $startfrom = $query->param('startfrom');
($startfrom) || ($startfrom=0);
# push @forminputs, {field => "results_per_page", value => $number_of_results};
## OK, We're searching
# STEP 1. We're a CGI script,so first thing to do is get the
# query into PQF format so we can use the Koha API properly
my ($error,$pqf_sort_by, $pqf_prox_ops, $pqf_bool_ops, $pqf_query) = cgi2pqf($query);
warn "AFTER CGI: $pqf_sort_by $pqf_prox_ops $pqf_bool_ops $pqf_query";
# implement a query history
push @pqf_query_history, { field => 'pqf_query', value => $pqf_query};
# lets store the query details in an array for later
push @forminputs, { field => "cql_query" , value => $cql_query} ;
@ -43,7 +45,7 @@ if ($op eq 'get_results') { # Yea, we're searching, load the results template
push @forminputs, { field => 'pqf_prox_ops', value => $pqf_prox_ops};
push @forminputs, { field => 'pqf_bool_ops' , value => $pqf_bool_ops};
push @forminputs, { field => 'pqf_query' , value => $pqf_query };
$searchdesc=$cql_query.$pqf_query; # FIXME: this should be a more use-friendly string
$searchdesc=$cql_query.$pqf_prox_ops.$pqf_bool_ops.$pqf_query; # FIXME: this should be a more use-friendly string
# STEP 2. OK, now we have PQF, so we can pass off the query to
# the API
@ -66,6 +68,7 @@ if ($op eq 'get_results') { # Yea, we're searching, load the results template
# the total results searched
$template->param(total => $count);
$template->param(FORMINPUTS => \@forminputs);
$template->param(PQF_QUERY_HISTORY => \@pqf_query_history);
$template->param(searchdesc => $searchdesc );
$template->param(results_per_page => $number_of_results );
$template->param(SEARCH_RESULTS => \@newresults);
@ -133,8 +136,25 @@ if ($op eq 'get_results') { # Yea, we're searching, load the results template
pg => $total_pages};
}
$template->param(numbers => $numbers);
$template->param( pqf_sort_by => $pqf_sort_by,
pqf_query => "$pqf_prox_ops $pqf_bool_ops $pqf_query",
numbers => $numbers);
$template->param('Disable_Dictionary'=>C4::Context->preference("Disable_Dictionary")) if (C4::Context->preference("Disable_Dictionary"));
$template->param(
#classlist => $classlist,
suggestion => C4::Context->preference("suggestion"),
virtualshelves => C4::Context->preference("virtualshelves"),
LibraryName => C4::Context->preference("LibraryName"),
OpacNav => C4::Context->preference("OpacNav"),
opaccredits => C4::Context->preference("opaccredits"),
AmazonContent => C4::Context->preference("AmazonContent"),
opacsmallimage => C4::Context->preference("opacsmallimage"),
opaclayoutstylesheet => C4::Context->preference("opaclayoutstylesheet"),
opaccolorstylesheet => C4::Context->preference("opaccolorstylesheet"),
);
## OK, we're not searching, load the search template
} else {
($template, $borrowernumber, $cookie)
@ -144,6 +164,8 @@ if ($op eq 'get_results') { # Yea, we're searching, load the results template
authnotrequired => 1,
});
# pass on the query history
$template->param(PQF_QUERY_HISTORY => \@pqf_query_history);
use C4::Koha;
my $dbh = C4::Context->dbh;
##Itemtypes (Collection Codes)
@ -206,7 +228,7 @@ sub searchZOOM {
my $dbh = C4::Context->dbh;
my $zconn=C4::Context->Zconn("biblioserver");
warn ($type,$query,$num,$startfrom) ;
#warn ($type,$query,$num,$startfrom) ;
if ($zconn eq "error") {
return("error with connection",undef); #FIXME: better error handling
}
@ -214,7 +236,7 @@ sub searchZOOM {
my $zoom_query_obj;
eval {
if ($type eq 'cql') {
$zoom_query_obj = new ZOOM::Query::CQL($query,$zconn);
$zoom_query_obj = new ZOOM::Query::CQL2RPN($query,$zconn);
} else {
$zoom_query_obj = new ZOOM::Query::PQF($query);
}
@ -246,7 +268,6 @@ sub searchZOOM {
# build a valid PQF query from the CGI form
sub cgi2pqf {
my ($query) = @_;
warn "CGI 2 PQF conversion";
my @default_attributes = ('sort_by');
# attributes specific to the advanced search - a search_point is actually a combination of
# several bib1 attributes
@ -264,6 +285,7 @@ sub cgi2pqf {
my $cql_query = $query->param('cql_query');
my $pqf_query = $query->param('pqf_query');
my @pqf_query_array;
my @counting_pqf_query_array;
@ -294,7 +316,7 @@ sub cgi2pqf {
$pqf_sort_by .= " ".$query->param($def_attr);
}
# these are attributes specific to this query_form, set many times per query
# First, process the 'operators' and put them in a separate variable
# First, process the 'operators' and put them in an array
# proximity and boolean
foreach my $spec_attr (@specific_attributes) {
for (my $i=1;$i<15;$i++) {
@ -316,9 +338,9 @@ sub cgi2pqf {
}
}
}
# by now, we have two variables: $pqf_bool_ops (boolean) and $pqf_prox_ops (proximity)
# by now, we have two operator arrays: @pqf_bool_ops_array (boolean) and @pqf_prox_ops_array (proximity)
# Now, process the attributes
# Next, we process the attributes (operands)
for (my $i=1;$i<15;$i++) {
foreach my $spec_attr (@specific_attributes) {
if ($query->param("query$i")) {
@ -334,6 +356,10 @@ sub cgi2pqf {
}
}
# we have to make sure that there # of operators == # of operands-1
# because second, third, etc queries, come in with an operator attached
# ...but if there is no previous queries, it should be dropped ...
# that's what we're doing here
my $count_pqf_query = @counting_pqf_query_array;
my $count_pqf_bool_ops = @pqf_bool_ops_array;
@ -341,20 +367,18 @@ sub cgi2pqf {
for (my $i=$count_pqf_query;$i>=0;$i--) {
$pqf_bool_ops.=" ".$pqf_bool_ops_array[$i];
}
foreach my $que(@pqf_query_array) {
$pqf_query .=" ".$que;
}
} else {
warn "problem example:".$count_pqf_bool_ops." ".$count_pqf_query;
for (my $i=$count_pqf_query;$i>=1;$i--) {
$pqf_bool_ops.=" ".$pqf_bool_ops_array[$i];
}
foreach my $que(@pqf_query_array) {
$pqf_query .=" ".$que;
}
}
foreach my $que(@pqf_query_array) {
$pqf_query .=" ".$que;
}
foreach my $prox(@pqf_prox_ops_array) {
$pqf_prox_ops.=" ".$prox;
}
# OK, done with that, now lets have a look
warn "Boolean Operators: ".$pqf_bool_ops if $pqf_bool_ops;
warn "Proximigy Operators: ".$pqf_prox_ops if $pqf_prox_ops;
warn "Sort by: ".$pqf_sort_by;
@ -366,147 +390,133 @@ sub cgi2pqf {
sub searchResults {
my ($num,@marcresults)=@_;
use C4::Date;
my ($num,@marcresults)=@_;
use C4::Date;
my $dbh= C4::Context->dbh;
my $toggle;
my $even=1;
my @newresults;
#Build brancnames hash
#find branchname
#get branch information.....
my %branches;
my $bsth=$dbh->prepare("SELECT branchcode,branchname FROM branches");
$bsth->execute();
while (my $bdata=$bsth->fetchrow_hashref){
$branches{$bdata->{'branchcode'}}= $bdata->{'branchname'};
my $dbh= C4::Context->dbh;
my $toggle;
my $even=1;
my @newresults;
}
#search item field code
my $sth =
$dbh->prepare(
"select tagfield from marc_subfield_structure where kohafield like 'items.itemnumber'"
);
$sth->execute;
my ($itemtag) = $sth->fetchrow;
## find column names of items related to MARC
my $sth2=$dbh->prepare("SHOW COLUMNS from items");
$sth2->execute;
my %subfieldstosearch;
while ((my $column)=$sth2->fetchrow){
my ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.".$column,"");
$subfieldstosearch{$column}=$tagsubfield;
}
for ( my $i=0; $i<$num ; $i++){
my $marcrecord;
$marcrecord = MARC::File::USMARC::decode($marcresults[$i]);
my $oldbiblio = MARCmarc2koha($dbh,$marcrecord,'');
if ($i % 2) {
$toggle="#ffffcc";
} else {
$toggle="white";
#Build brancnames hash
#find branchname
#get branch information.....
my %branches;
my $bsth=$dbh->prepare("SELECT branchcode,branchname FROM branches");
$bsth->execute();
while (my $bdata=$bsth->fetchrow_hashref){
$branches{$bdata->{'branchcode'}}= $bdata->{'branchname'};
}
$oldbiblio->{'toggle'}=$toggle;
my @fields = $marcrecord->field($itemtag);
my @items;
my $item;
my %counts;
$counts{'total'}=0;
#search item field code
my $sth = $dbh->prepare(
"select tagfield from marc_subfield_structure where kohafield like 'items.itemnumber'"
);
$sth->execute;
my ($itemtag) = $sth->fetchrow;
## find column names of items related to MARC
my $sth2=$dbh->prepare("SHOW COLUMNS from items");
$sth2->execute;
my %subfieldstosearch;
while ((my $column)=$sth2->fetchrow){
my ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.".$column,"");
$subfieldstosearch{$column}=$tagsubfield;
}
for ( my $i=0; $i<$num ; $i++){
my $marcrecord;
$marcrecord = MARC::File::USMARC::decode($marcresults[$i]);
my $oldbiblio = MARCmarc2koha($dbh,$marcrecord,'');
if ($i % 2) {
$toggle="#ffffcc";
} else {
$toggle="white";
}
$oldbiblio->{'toggle'}=$toggle;
my @fields = $marcrecord->field($itemtag);
my @items;
my $item;
my %counts;
$counts{'total'}=0;
#
##Loop for each item field
foreach my $field (@fields) {
foreach my $code ( keys %subfieldstosearch ) {
foreach my $field (@fields) {
foreach my $code ( keys %subfieldstosearch ) {
$item->{$code}=$field->subfield($subfieldstosearch{$code});
}
$item->{$code}=$field->subfield($subfieldstosearch{$code});
}
my $status;
$item->{'branchname'}=$branches{$item->{'holdingbranch'}};
$item->{'date_due'}=$item->{onloan};
$status="Lost" if ($item->{itemlost});
$status="Withdrawn" if ($item->{wthdrawn});
$status="Due:".format_date($item->{onloan}) if ($item->{onloan}>0 );
# $status="On Loan" if ($item->{onloan} );
if ($item->{'location'}){
$status = $item->{'branchname'}."[".$item->{'location'}."]" unless defined $status;
}else{
$status = $item->{'branchname'} unless defined $status;
}
$counts{$status}++;
$counts{'total'}++;
push @items,$item;
my $status;
$item->{'branchname'}=$branches{$item->{'holdingbranch'}};
$item->{'date_due'}=$item->{onloan};
$status="Lost" if ($item->{itemlost});
$status="Withdrawn" if ($item->{wthdrawn});
$status="Due:".format_date($item->{onloan}) if ($item->{onloan}>0 );
# $status="On Loan" if ($item->{onloan} );
if ($item->{'location'}){
$status = $item->{'branchname'}."[".$item->{'location'}."]" unless defined $status;
}else{
$status = $item->{'branchname'} unless defined $status;
}
$counts{$status}++;
$counts{'total'}++;
push @items,$item;
}
my $norequests = 1;
my $noitems = 1;
if (@items) {
$noitems = 0;
foreach my $itm (@items) {
$norequests = 0 unless $itm->{'itemnotforloan'};
}
my $norequests = 1;
my $noitems = 1;
if (@items) {
$noitems = 0;
foreach my $itm (@items) {
$norequests = 0 unless $itm->{'itemnotforloan'};
}
$oldbiblio->{'noitems'} = $noitems;
$oldbiblio->{'norequests'} = $norequests;
$oldbiblio->{'even'} = $even = not $even;
$oldbiblio->{'itemcount'} = $counts{'total'};
my $totalitemcounts = 0;
foreach my $key (keys %counts){
if ($key ne 'total'){
$totalitemcounts+= $counts{$key};
$oldbiblio->{'locationhash'}->{$key}=$counts{$key};
}
}
$oldbiblio->{'noitems'} = $noitems;
$oldbiblio->{'norequests'} = $norequests;
$oldbiblio->{'even'} = $even = not $even;
$oldbiblio->{'itemcount'} = $counts{'total'};
my $totalitemcounts = 0;
foreach my $key (keys %counts){
if ($key ne 'total'){
$totalitemcounts+= $counts{$key};
$oldbiblio->{'locationhash'}->{$key}=$counts{$key};
}
my ($locationtext, $locationtextonly, $notavailabletext) = ('','','');
foreach (sort keys %{$oldbiblio->{'locationhash'}}) {
if ($_ eq 'notavailable') {
$notavailabletext="Not available";
my $c=$oldbiblio->{'locationhash'}->{$_};
$oldbiblio->{'not-available-p'}=$c;
} else {
$locationtext.="$_";
my $c=$oldbiblio->{'locationhash'}->{$_};
if ($_ eq 'Item Lost') {
$oldbiblio->{'lost-p'} = $c;
} elsif ($_ eq 'Withdrawn') {
$oldbiblio->{'withdrawn-p'} = $c;
} elsif ($_ eq 'On Loan') {
$oldbiblio->{'on-loan-p'} = $c;
} else {
$locationtextonly.= $_;
$locationtextonly.= " ($c)<br> " if $totalitemcounts > 1;
}
if ($totalitemcounts>1) {
$locationtext.=" ($c)<br> ";
}
}
}
if ($notavailabletext) {
$locationtext.= $notavailabletext;
}
my ($locationtext, $locationtextonly, $notavailabletext) = ('','','');
foreach (sort keys %{$oldbiblio->{'locationhash'}}) {
if ($_ eq 'notavailable') {
$notavailabletext="Not available";
my $c=$oldbiblio->{'locationhash'}->{$_};
$oldbiblio->{'not-available-p'}=$c;
} else {
$locationtext=~s/, $//;
$locationtext.="$_";
my $c=$oldbiblio->{'locationhash'}->{$_};
if ($_ eq 'Item Lost') {
$oldbiblio->{'lost-p'} = $c;
} elsif ($_ eq 'Withdrawn') {
$oldbiblio->{'withdrawn-p'} = $c;
} elsif ($_ eq 'On Loan') {
$oldbiblio->{'on-loan-p'} = $c;
} else {
$locationtextonly.= $_;
$locationtextonly.= " ($c)<br> " if $totalitemcounts > 1;
}
if ($totalitemcounts>1) {
$locationtext.=" ($c)<br> ";
}
}
$oldbiblio->{'location'} = $locationtext;
$oldbiblio->{'location-only'} = $locationtextonly;
$oldbiblio->{'use-location-flags-p'} = 1;
}
if ($notavailabletext) {
$locationtext.= $notavailabletext;
} else {
$locationtext=~s/, $//;
}
$oldbiblio->{'location'} = $locationtext;
$oldbiblio->{'location-only'} = $locationtextonly;
$oldbiblio->{'use-location-flags-p'} = 1;
push (@newresults, $oldbiblio);
}
return @newresults;
}
return @newresults;
}