Commiting a sample change to the default template to allow users to pick
[koha_gimpoz] / 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::Acquisition;
13 use C4::Biblio;
14 use C4::Search;
15
16 my $classlist='';
17
18 my $dbh=C4::Context->dbh;
19 my $sth=$dbh->prepare("select description,itemtype from itemtypes order by description");
20 $sth->execute;
21 while (my ($description,$itemtype) = $sth->fetchrow) {
22     $classlist.="<option value=\"$itemtype\">$description</option>\n";
23 }
24
25
26 my $query = new CGI;
27 my $op = $query->param("op");
28 my $type=$query->param('type');
29
30 my $startfrom=$query->param('startfrom');
31 $startfrom=0 if(!defined $startfrom);
32 my ($template, $loggedinuser, $cookie);
33 my $resultsperpage;
34
35 if ($op eq "do_search") {
36         my @marclist = $query->param('marclist');
37         my @and_or = $query->param('and_or');
38         my @excluding = $query->param('excluding');
39         my @operator = $query->param('operator');
40         my @value = $query->param('value');
41
42         $resultsperpage= $query->param('resultsperpage');
43         $resultsperpage = 19 if(!defined $resultsperpage);
44         my $orderby = $query->param('orderby');
45
46         # builds tag and subfield arrays
47         my @tags;
48
49         foreach my $marc (@marclist) {
50                 if ($marc) {
51                         my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,$marc);
52                         if ($tag) {
53                                 push @tags,$dbh->quote("$tag$subfield");
54                         } else {
55                                 push @tags, $dbh->quote(substr($marc,0,4));
56                         }
57                 } else {
58                         push @tags, "";
59                 }
60         }
61         findseealso($dbh,\@tags);
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                                                         orderby => $orderby,
126                                                         startfromnext => $startfrom+1,
127                                                         startfromprev => $startfrom-1,
128                                                         searchdata=>\@field_data,
129                                                         total=>$total,
130                                                         from=>$from,
131                                                         to=>$to,
132                                                         numbers=>\@numbers,
133                                                         );
134
135 } else {
136         ($template, $loggedinuser, $cookie)
137                 = get_template_and_user({template_name => "opac-search.tmpl",
138                                         query => $query,
139                                         type => "opac",
140                                         authnotrequired => 1,
141                                 });
142         
143         
144         $sth=$dbh->prepare("Select itemtype,description from itemtypes order by description");
145         $sth->execute;
146         my  @itemtype;
147         my %itemtypes;
148         push @itemtype, "";
149         $itemtypes{''} = "";
150         while (my ($value,$lib) = $sth->fetchrow_array) {
151                 push @itemtype, $value;
152                 $itemtypes{$value}=$lib;
153         }
154         
155         my $CGIitemtype=CGI::scrolling_list( -name     => 'value',
156                                 -values   => \@itemtype,
157                                 -labels   => \%itemtypes,
158                                 -size     => 1,
159                                 -multiple => 0 );
160         $sth->finish;
161         
162         my @branches;
163         my @select_branch;
164         my %select_branches;
165         my ($count2,@branches)=branches();
166         push @select_branch, "";
167         $select_branches{''} = "";
168         for (my $i=0;$i<$count2;$i++){
169                 push @select_branch, $branches[$i]->{'branchcode'};#
170                 $select_branches{$branches[$i]->{'branchcode'}} = $branches[$i]->{'branchname'};
171         }
172         my $CGIbranch=CGI::scrolling_list( -name     => 'value',
173                                 -values   => \@select_branch,
174                                 -labels   => \%select_branches,
175                                 -size     => 1,
176                                 -multiple => 0 );
177         $sth->finish;
178         my @options;
179     my $counter=0;
180         foreach my $language (getalllanguages()) {
181                             next if $language eq 'images';
182                             my $selected='0';
183 #                            next if $currently_selected_languages->{$language};
184                             push @options, { language => $language, counter => $counter };
185                             $counter++;
186                     }
187     
188         $template->param(classlist => $classlist,
189                                         CGIitemtype => $CGIitemtype,
190                                         CGIbranch => $CGIbranch,
191                                         languages => \@options,
192         );
193 }
194
195 output_html_with_http_headers $query, $cookie, $template->output;