comma taken out
[koha.git] / opac / opac-search.pl
1 #!/usr/bin/perl
2 use strict;
3 require Exporter;
4
5 use C4::Auth;
6 use C4::Interface::CGI::Output;
7 use C4::Context;
8 use CGI;
9 use C4::Database;
10 use HTML::Template;
11 use C4::SearchMarc;
12 use C4::Catalogue;
13 use C4::Biblio;
14
15 my $classlist='';
16
17 my $dbh=C4::Context->dbh;
18 my $sth=$dbh->prepare("select description,itemtype from itemtypes order by description");
19 $sth->execute;
20 while (my ($description,$itemtype) = $sth->fetchrow) {
21     $classlist.="<option value=\"$itemtype\">$description</option>\n";
22 }
23
24
25 my $query = new CGI;
26 my $op = $query->param("op");
27 my $type=$query->param('type');
28
29 my $startfrom=$query->param('startfrom');
30 $startfrom=0 if(!defined $startfrom);
31 my ($template, $loggedinuser, $cookie);
32 my $resultsperpage;
33
34 if ($op eq "do_search") {
35         my @marclist = $query->param('marclist');
36         my @and_or = $query->param('and_or');
37         my @excluding = $query->param('excluding');
38         my @operator = $query->param('operator');
39         my @value = $query->param('value');
40
41         $resultsperpage= $query->param('resultsperpage');
42         $resultsperpage = 19 if(!defined $resultsperpage);
43         my $orderby = $query->param('orderby');
44
45         # builds tag and subfield arrays
46         my @tags;
47
48         foreach my $marc (@marclist) {
49                 if ($marc) {
50                         my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,$marc);
51                         if ($tag) {
52                                 push @tags,$dbh->quote("$tag$subfield");
53                         } else {
54                                 push @tags, $dbh->quote(substr($marc,0,4));
55                         }
56                 } else {
57                         push @tags, "";
58                 }
59         }
60         findseealso($dbh,\@tags);
61         warn "IN THERE";
62         my ($results,$total) = catalogsearch($dbh, \@tags,\@and_or,
63                                                                                 \@excluding, \@operator, \@value,
64                                                                                 $startfrom*$resultsperpage, $resultsperpage,$orderby);
65
66         ($template, $loggedinuser, $cookie)
67                 = get_template_and_user({template_name => "opac-searchresults.tmpl",
68                                 query => $query,
69                                 type => 'opac',
70                                 authnotrequired => 1,
71                                 debug => 1,
72                                 });
73
74         # multi page display gestion
75         my $displaynext=0;
76         my $displayprev=$startfrom;
77         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
78                 $displaynext = 1;
79         }
80
81         my @field_data = ();
82
83
84         for(my $i = 0 ; $i <= $#marclist ; $i++)
85         {
86                 push @field_data, { term => "marclist", val=>$marclist[$i] };
87                 push @field_data, { term => "and_or", val=>$and_or[$i] };
88                 push @field_data, { term => "excluding", val=>$excluding[$i] };
89                 push @field_data, { term => "operator", val=>$operator[$i] };
90                 push @field_data, { term => "value", val=>$value[$i] };
91         }
92
93         my @numbers = ();
94
95         if ($total>$resultsperpage)
96         {
97                 for (my $i=1; $i<$total/$resultsperpage+1; $i++)
98                 {
99                         if ($i<16)
100                         {
101                         my $highlight=0;
102                         ($startfrom==($i-1)) && ($highlight=1);
103                         push @numbers, { number => $i,
104                                         highlight => $highlight ,
105                                         searchdata=> \@field_data,
106                                         startfrom => ($i-1)};
107                         }
108         }
109         }
110
111         my $from = $startfrom*$resultsperpage+1;
112         my $to;
113
114         if($total < (($startfrom+1)*$resultsperpage))
115         {
116                 $to = $total;
117         } else {
118                 $to = (($startfrom+1)*$resultsperpage);
119         }
120         $template->param(results => $results,
121                                                         startfrom=> $startfrom,
122                                                         displaynext=> $displaynext,
123                                                         displayprev=> $displayprev,
124                                                         resultsperpage => $resultsperpage,
125                                                         startfromnext => $startfrom+1,
126                                                         startfromprev => $startfrom-1,
127                                                         searchdata=>\@field_data,
128                                                         total=>$total,
129                                                         from=>$from,
130                                                         to=>$to,
131                                                         numbers=>\@numbers,
132                                                         );
133
134 } else {
135         ($template, $loggedinuser, $cookie)
136                 = get_template_and_user({template_name => "opac-search.tmpl",
137                                         query => $query,
138                                         type => "opac",
139                                         authnotrequired => 1,
140                                 });
141         
142         
143         $sth=$dbh->prepare("Select itemtype,description from itemtypes order by description");
144         $sth->execute;
145         my  @itemtype;
146         my %itemtypes;
147         push @itemtype, "";
148         $itemtypes{''} = "";
149         while (my ($value,$lib) = $sth->fetchrow_array) {
150                 push @itemtype, $value;
151                 $itemtypes{$value}=$lib;
152         }
153         
154         my $CGIitemtype=CGI::scrolling_list( -name     => 'value',
155                                 -values   => \@itemtype,
156                                 -labels   => \%itemtypes,
157                                 -size     => 1,
158                                 -multiple => 0 );
159         $sth->finish;
160         
161         my @branches;
162         my @select_branch;
163         my %select_branches;
164         my ($count2,@branches)=branches();
165         push @select_branch, "";
166         $select_branches{''} = "";
167         for (my $i=0;$i<$count2;$i++){
168                 push @select_branch, $branches[$i]->{'branchcode'};#
169                 $select_branches{$branches[$i]->{'branchcode'}} = $branches[$i]->{'branchname'};
170         }
171         my $CGIbranch=CGI::scrolling_list( -name     => 'value',
172                                 -values   => \@select_branch,
173                                 -labels   => \%select_branches,
174                                 -size     => 1,
175                                 -multiple => 0 );
176         $sth->finish;
177         
178         $template->param(classlist => $classlist,
179                                         CGIitemtype => $CGIitemtype,
180                                         CGIbranch => $CGIbranch,
181         );
182 }
183
184 output_html_with_http_headers $query, $cookie, $template->output;