From 3129c52ff794acc853363d4b731cb6f67089153a Mon Sep 17 00:00:00 2001 From: tgarip1957 Date: Fri, 1 Sep 2006 22:25:05 +0000 Subject: [PATCH] A basic search script catalogue-search.pl demonstrates the use of koha_attr table to create PQF free searches --- catalogue/catalogue-search.pl | 228 ++++++++++++++++++++++++++++++++++ catalogue/moredetail.pl | 118 ++++++++++++++---- 2 files changed, 325 insertions(+), 21 deletions(-) create mode 100644 catalogue/catalogue-search.pl diff --git a/catalogue/catalogue-search.pl b/catalogue/catalogue-search.pl new file mode 100644 index 0000000000..a583d78b22 --- /dev/null +++ b/catalogue/catalogue-search.pl @@ -0,0 +1,228 @@ +#!/usr/bin/perl +use strict; +require Exporter; +use CGI; +#use C4::Interface::CGI::KOHACGI; +use C4::Search; +use C4::Auth; +use C4::Interface::CGI::Output; +use C4::Biblio; +use C4::Koha; +use POSIX qw(ceil floor); + +my $query = new CGI; +my $dbh = C4::Context->dbh; +$query->charset('UTF8'); +my $op = $query->param('op'); #show the search form or execute the search + +my $format=$query->param('MARC'); +my ($template, $borrowernumber, $cookie); + +# get all the common search variables, +my @value=$query->param('value'); +my @kohafield=$query->param('kohafield'); +my @and_or=$query->param('and_or'); +my @relation=$query->param('relation'); +my $order=$query->param('order'); +my $reorder=$query->param('reorder'); +my $number_of_results=$query->param('number_of_results'); +my $zoom=$query->param('zoom'); +my $ascend=$query->param('asc'); +# collect all the fields ... +my %search; +my @forminputs; #this is for the links to navigate among the results +my (@searchdesc, %hashdesc); #this is to show the description of the current search +my @fields = ('value', 'kohafield', 'and_or', 'relation','order','barcode','biblionumber','itemnumber','asc','from'); + +foreach my $field (@fields) { + $search{$field} = $query->param($field); + if ($search{$field}) { + push @forminputs, { field=>$field ,value=> $search{$field}} unless ($field eq 'reorder'); + } +} + + +$hashdesc{'query'} = join " , ", @value; +push @searchdesc,\%hashdesc; + + +############################################################################ +if ($op eq "do_search"){ + +#this fields is just to allow the user come back to the search form with all the values previously entered +$search{'search_type'} = $query->param('search_type'); +push @forminputs, {field => 'search_type', value => $search{'search_type'}}; + + + ($template, $borrowernumber, $cookie) + = get_template_and_user({template_name => "catalogue/catalogue_searchresults.tmpl", + query => $query, + type => "intranet", + authnotrequired => 1, + }); + + $search{'from'} = 'intranet'; + $search{'borrowernumber'} = $borrowernumber; + $search{'remote_IP'} = $query->remote_addr(); + $search{'remote_URL'} = $query->url(-query=>1); + $search{'searchdesc'} = \@searchdesc; + $template->param(FORMINPUTS => \@forminputs); + $template->param(reorder => $query->param('reorder')); + + # do the searchs .... + $number_of_results = 10 unless $number_of_results; + my $startfrom=$query->param('startfrom'); + ($startfrom) || ($startfrom=0); +my ($count,@results); +if (!$zoom){ +## using sql search for barcode,biblionumber or itemnumber only useful for libraian interface + ($count, @results) =sqlsearch($dbh,\%search); +}else{ +my $sortorder=$order.",".$ascend if $order; + ($count,@results) =ZEBRAsearch_kohafields(\@kohafield,\@value, \@relation,$sortorder, \@and_or, 1,$reorder,$startfrom, $number_of_results,"intranet"); +} + if ( $count eq "error"){ + $template->param(error =>1); + goto "show"; + } + my $num = scalar(@results) - 1; +if ( $count == 1){ + # if its a barcode search by definition we will only have one result. + # And if we have a result + # lets jump straight to the detail.pl page + if ($format eq '1') { + print $query->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?type=intra&biblionumber=$results[0]->{'biblionumber'}"); + }else{ + print $query->redirect("/cgi-bin/koha/catalogue/detail.pl?type=intra&biblionumber=$results[0]->{'biblionumber'}"); + } +} + # sorting out which results to display. + # the result number to star to show + $template->param(starting => $startfrom+$number_of_results); + $template->param(endinging => $startfrom+1); + $template->param(startfrom => $startfrom+1); + # the result number to end to show + ($startfrom+$num<=$count) ? ($template->param(endat => $startfrom+$num+1)) : ($template->param(endat => $count)); + # the total results searched + $template->param(numrecords => $count); + + $template->param(searchdesc => \@searchdesc ); + $template->param(SEARCH_RESULTS => \@results); + + #this is to show the images numbers to navigate among the results, if it has to show the number highlighted or not + my $numbers; + @$numbers = (); + my $pg = 1; + if (defined($query->param('pg'))) { + $pg = $query->param('pg'); + } + my $start = 0; + + $start = ($pg - 1) * $number_of_results; + my $pages = ceil($count / $number_of_results); + my $total_pages = ceil($count / $number_of_results); + + if ($pg > 1) { + my $url = $pg - 1; + push @$numbers, { number => "<<", + highlight => 0 , + startfrom => 0, + pg => '1' }; + push @$numbers, { number => "<", + highlight => 0 , forminputs=>\@forminputs, + startfrom => ($url-1)*$number_of_results+1, + pg => $url }; + } + my $current_ten = $pg / 10; + if ($current_ten == 0) { + $current_ten = 0.1; # In case it´s in ten = 0 + } + my $from = $current_ten * 10; # Calculate the initial page + my $end_ten = $from + 9; + my $to; + if ($pages > $end_ten) { + $to = $end_ten; + } else { + $to = $pages; + } + for (my $i = $from; $i <= $to ; $i++) { + if ($i == $pg) { + if ($count > $number_of_results) { + push @$numbers, { number => $i, + highlight => 1 , forminputs=>\@forminputs, + startfrom => ($i-1)*$number_of_results , + pg => $i }; + } + } else { + push @$numbers, { number => $i, + highlight => 0 , forminputs=>\@forminputs, + startfrom => ($i-1)*$number_of_results , + pg => $i }; + } + } + if ($pg < $pages) { + my $url = $pg + 1; + push @$numbers, { number => ">", + highlight => 0 , forminputs=>\@forminputs, + startfrom => ($url-1)*$number_of_results, + pg => $url }; + push @$numbers, { number => ">>", + highlight => 0 , forminputs=>\@forminputs, + startfrom => ($total_pages-1)*$number_of_results, + pg => $total_pages}; + } +# push @$numbers,{forminputs=>@forminputs}; + $template->param(numbers =>$numbers); + + #show the virtual shelves + #my $results = &GetShelfList($borrowernumber); + #$template->param(shelvescount => scalar(@{$results})); + #$template->param(shelves => $results); + +######## +if ($format eq '1') { + $template->param(script => "catalogue/MARCdetail.pl"); +}else{ + $template->param(script => "catalogue/detail.pl"); +} + +}else{ ## No search yet +($template, $borrowernumber, $cookie) + = get_template_and_user({template_name => "catalogue/catalogue_search.tmpl", + query => $query, + type => "intranet", + authnotrequired => 1, + }); +#show kohafields + my $kohafield = $query->param('kohafield'); + my ($fieldcount,@kohafields)=getkohafields(); + foreach my $row (@kohafields) { + if ($kohafield eq $row->{'marctokoha'}) { + $row->{'sel'} = 1; + } + } + $template->param(kohafields => \@kohafields); +##show sorting fields +my @sorts; + $order=$query->param('order'); + foreach my $sort (@kohafields) { + if ($sort->{sorts}){ + push @sorts,$sort; + if ($order eq $sort->{'marctokoha'}) { + $sort->{'sel'} = 1; + } + } + } + $template->param(sorts => \@sorts); + +my $search_type = $query->param('search_type'); + if ((!$search_type) || ($search_type eq 'zoom')) { + $template->param(zoom_search => 1); + } else{ + $template->param(sql_search => 1); + } +} + +show: +output_html_with_http_headers $query, $cookie, $template->output(); + diff --git a/catalogue/moredetail.pl b/catalogue/moredetail.pl index edd7c30772..736d142af5 100755 --- a/catalogue/moredetail.pl +++ b/catalogue/moredetail.pl @@ -20,7 +20,6 @@ # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, # Suite 330, Boston, MA 02111-1307 USA -use HTML::Template; use strict; require Exporter; use C4::Koha; @@ -31,16 +30,17 @@ use C4::Output; # contains gettemplate use C4::Auth; use C4::Interface::CGI::Output; use C4::Date; - +use C4::Context; +use C4::Biblio; +use C4::Accounts2; +use C4::Circulation::Circ2; +use MARC::Record; +my $dbh=C4::Context->dbh; my $query=new CGI; -# FIXME subject is not exported to the template? -my $subject=$query->param('subject'); -# if its a subject we need to use the subject.tmpl my ($template, $loggedinuser, $cookie) = get_template_and_user({ - template_name => ($subject? 'catalogue/subject.tmpl': - 'catalogue/moredetail.tmpl'), + template_name => ( 'catalogue/moredetail.tmpl'), query => $query, type => "intranet", authnotrequired => 0, @@ -48,12 +48,17 @@ my ($template, $loggedinuser, $cookie) = get_template_and_user({ }); # get variables +my $op=$query->param('op'); +my $lost=$query->param('lost'); +my $withdrawn=$query->param('withdrawn'); +my $override=$query->param('override'); +my $itemnumber=$query->param('itemnumber'); +my $barcode=$query->param('barcode'); -my $biblionumber=$query->param('bib'); my $title=$query->param('title'); -my $bi=$query->param('bi'); - -my $data=bibitemdata($bi); +my $biblionumber=$query->param('biblionumber'); +my ($record,)=MARCgetbiblio($dbh,$biblionumber); +my $data=MARCmarc2koha($dbh,$record,"biblios"); my $dewey = $data->{'dewey'}; # FIXME Dewey is a string, not a number, & we should use a function $dewey =~ s/0+$//; @@ -68,12 +73,82 @@ $data->{'dewey'}=$dewey; my @results; -my (@items)=itemissues($bi); +my @items; +if ($op eq "update"){ +my $env; +##Do Lost or Withdraw here +my $flag=0; + my ($resbor,$resrec)=C4::Reserves2::CheckReserves($env,$dbh,$itemnumber); +if ($override ne "yes"){ + if ($resbor){ +# print $query->header; + $template->param(error => "This item has a reserve on it"); + $template->param(biblionumber =>$biblionumber); + $template->param(itemnumber =>$itemnumber); + $template->param(lost =>$lost); + $template->param(withdrawn =>$withdrawn); + $flag=1; + } + my $sth=$dbh->prepare("Select * from issues where (itemnumber=?) and (returndate is null)"); + $sth->execute($itemnumber); + + if (my $data=$sth->fetchrow_hashref) { + $template->param(biblionumber =>$biblionumber); + $template->param(itemnumber =>$itemnumber); + $template->param(error => "This item is On Loan to a member"); + $template->param(lost =>$lost); + $template->param(withdrawn =>$withdrawn); + $flag=2; + } +} +if ($flag != 0 && $override ne "yes"){ + + }else { + ##UPDATE here +my $sth=$dbh->prepare("update items set itemlost=? ,wthdrawn=? where itemnumber=?"); +$sth->execute($lost,$withdrawn,$itemnumber); +$sth->finish; +MARCmoditemonefield($dbh,$biblionumber,$itemnumber,'wthdrawn',$withdrawn,1); +MARCmoditemonefield($dbh,$biblionumber,$itemnumber,'itemlost',$lost); + + if ($lost ==1 && $flag ==2){ + my $sth=$dbh->prepare("Select * from issues where (itemnumber=?) and (returndate is null)"); + $sth->execute($itemnumber); + my $data=$sth->fetchrow_hashref; + if ($data->{'borrowernumber'} ne '') { + #item on issue add replacement cost to borrowers record + my $accountno=getnextacctno($env,$data->{'borrowernumber'},$dbh); + my $item=getiteminformation($env, $itemnumber); + my $sth2=$dbh->prepare("Insert into accountlines + (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber) + values + (?,?,now(),?,?,'L',?,?)"); + $sth2->execute($data->{'borrowernumber'},$accountno,$item->{'replacementprice'}, + "Lost Item $item->{'title'} $item->{'barcode'}", + $item->{'replacementprice'},$itemnumber); + $sth2->finish; + } + } + if ($flag==1){ + foreach my $res ($resrec){ + C4::Reserves2::CancelReseve(undef,$res->{itemnumber},$res->{borrowernumber}); + } + } + + } +} +my @itemrecords=MARCgetallitems($dbh,$biblionumber); +foreach my $itemrecord (@itemrecords){ + +my $items = MARCmarc2koha($dbh,$itemrecord,"holdings"); +$items->{itemtype}=$data->{itemtype}; +$items->{biblionumber}=$biblionumber; +$items=itemissues($dbh,$items,$biblionumber); +push @items,$items; +} my $count=@items; $data->{'count'}=$count; - -my $ordernum = GetOrderNumber($biblionumber,$bi); -my $order = GetOrder($ordernum); +my ($order,$ordernum)=GetOrder($biblionumber,$barcode); my $env; $env->{itemcount}=1; @@ -88,13 +163,14 @@ foreach my $item (@items){ $item->{'ordernumber'} = $ordernum; $item->{'booksellerinvoicenumber'} = $order->{'booksellerinvoicenumber'}; - if ($item->{'date_due'} eq 'Available'){ - $item->{'issue'}= 0; - } else { - $item->{'date_due'} = format_date($item->{'date_due'}); - $item->{'issue'}= 1; - $item->{'borrowernumber'} = $item->{'borrower'}; + if ($item->{'date_due'} gt '0000-00-00'){ + $item->{'date_due'} = format_date($item->{'date_due'}); +$item->{'issue'}= 1; + $item->{'borrowernumber'} = $item->{'borrowernumber'}; $item->{'cardnumber'} = $item->{'card'}; + + } else { + $item->{'issue'}= 0; } } -- 2.39.5