fix for bug 1771: Template errors with remote itemtype image
[koha.git] / serials / subscription-bib-search.pl
1 #!/usr/bin/perl
2 # WARNING: 4-character tab stops here
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21
22 =head1 NAME
23
24 subscription-bib-search.pl
25
26 =head1 DESCRIPTION
27
28 this script search among all existing subscriptions.
29
30 =head1 PARAMETERS
31
32 =over 4
33
34 =item op
35 op use to know the operation to do on this template.
36  * do_search : to search the subscription.
37
38 Note that if op = do_search there are some others params specific to the search :
39     marclist,and_or,excluding,operator,value
40
41 =item startfrom
42 to multipage gestion.
43
44
45 =back
46
47 =cut
48
49
50 use strict;
51 require Exporter;
52 use CGI;
53 use C4::Koha;
54 use C4::Auth;
55 use C4::Context;
56 use C4::Output;
57 use C4::Search;
58 use C4::Biblio;
59
60 my $input=new CGI;
61 # my $type=$query->param('type');
62 my $op = $input->param('op');
63 my $dbh = C4::Context->dbh;
64
65 my $startfrom=$input->param('startfrom');
66 $startfrom=0 unless $startfrom;
67 my ($template, $loggedinuser, $cookie);
68 my $resultsperpage;
69
70 my $query = $input->param('q');
71 # don't run the search if no search term !
72 if ($op eq "do_search" && $query) {
73
74     # add the itemtype limit if applicable
75     my $itemtypelimit = $input->param('itemtypelimit');
76     $query .= " AND itype=$itemtypelimit" if $itemtypelimit;
77     
78     $resultsperpage= $input->param('resultsperpage');
79     $resultsperpage = 19 if(!defined $resultsperpage);
80
81     my ($error, $marcrecords, $total_hits) = SimpleSearch($query, $startfrom, $resultsperpage);
82     my $total = scalar @$marcrecords;
83
84     if (defined $error) {
85         $template->param(query_error => $error);
86         warn "error: ".$error;
87         output_html_with_http_headers $input, $cookie, $template->output;
88         exit;
89     }
90     my @results;
91     
92     for(my $i=0;$i<$total;$i++) {
93         my %resultsloop;
94         my $marcrecord = MARC::File::USMARC::decode($marcrecords->[$i]);
95         my $biblio = TransformMarcToKoha(C4::Context->dbh,$marcrecord,'');
96
97         #build the hash for the template.
98         $resultsloop{highlight}       = ($i % 2)?(1):(0);
99         $resultsloop{title}           = $biblio->{'title'};
100         $resultsloop{subtitle}        = $biblio->{'subtitle'};
101         $resultsloop{biblionumber}    = $biblio->{'biblionumber'};
102         $resultsloop{author}          = $biblio->{'author'};
103         $resultsloop{publishercode}   = $biblio->{'publishercode'};
104         $resultsloop{publicationyear} = $biblio->{'publicationyear'};
105
106         push @results, \%resultsloop;
107     }
108     
109     ($template, $loggedinuser, $cookie)
110         = get_template_and_user({template_name => "serials/result.tmpl",
111                 query => $input,
112                 type => "intranet",
113                 authnotrequired => 0,
114                 flagsrequired => {serials => 1},
115                 flagsrequired => {catalogue => 1},
116                 debug => 1,
117                 });
118
119     # multi page display gestion
120     my $displaynext=0;
121     my $displayprev=$startfrom;
122     if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
123         $displaynext = 1;
124     }
125
126
127     my @numbers = ();
128
129     if ($total>$resultsperpage)
130     {
131         for (my $i=1; $i<$total/$resultsperpage+1; $i++)
132         {
133             if ($i<16)
134             {
135                 my $highlight=0;
136                 ($startfrom==($i-1)) && ($highlight=1);
137                 push @numbers, { number => $i,
138                     highlight => $highlight ,
139                     searchdata=> \@results,
140                     startfrom => ($i-1)};
141             }
142         }
143     }
144
145     my $from = $startfrom*$resultsperpage+1;
146     my $to;
147
148     if($total < (($startfrom+1)*$resultsperpage))
149     {
150         $to = $total;
151     } else {
152         $to = (($startfrom+1)*$resultsperpage);
153     }
154     $template->param(
155                             query => $query,
156                             resultsloop => \@results,
157                             startfrom=> $startfrom,
158                             displaynext=> $displaynext,
159                             displayprev=> $displayprev,
160                             resultsperpage => $resultsperpage,
161                             startfromnext => $startfrom+1,
162                             startfromprev => $startfrom-1,
163                             total=>$total,
164                             from=>$from,
165                             to=>$to,
166                             numbers=>\@numbers,
167                             );
168 } # end of if ($op eq "do_search" & $query)
169  elsif ($op eq "do_search") {
170     ($template, $loggedinuser, $cookie)
171         = get_template_and_user({template_name => "serials/subscription-bib-search.tmpl",
172                 query => $input,
173                 type => "intranet",
174                 authnotrequired => 0,
175                 flagsrequired => {catalogue => 1, serials=>1},
176                 debug => 1,
177                 });
178     # load the itemtypes
179     my $itemtypes = GetItemTypes;
180     my @itemtypesloop;
181     my $selected=1;
182     my $cnt;
183     foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'} } keys %$itemtypes ) {
184         my %row =(
185                     code => $thisitemtype,
186                     selected => $selected,
187                     description => $itemtypes->{$thisitemtype}->{'description'},
188                 );
189         $selected = 0 if ($selected) ;
190         push @itemtypesloop, \%row;
191     }
192     $template->param(itemtypeloop => \@itemtypesloop);
193     $template->param("no_query" => 1);
194 }
195  else {
196     ($template, $loggedinuser, $cookie)
197         = get_template_and_user({template_name => "serials/subscription-bib-search.tmpl",
198                 query => $input,
199                 type => "intranet",
200                 authnotrequired => 0,
201                 flagsrequired => {catalogue => 1, serials=>1},
202                 debug => 1,
203                 });
204     # load the itemtypes
205     my $itemtypes = GetItemTypes;
206     my @itemtypesloop;
207     my $selected=1;
208     my $cnt;
209     foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'} } keys %$itemtypes ) {
210         my %row =(
211                     code => $thisitemtype,
212                     selected => $selected,
213                     description => $itemtypes->{$thisitemtype}->{'description'},
214                 );
215         $selected = 0 if ($selected) ;
216         push @itemtypesloop, \%row;
217     }
218     $template->param(itemtypeloop => \@itemtypesloop);
219     $template->param("no_query" => 0);
220 }
221
222 # Print the page
223 output_html_with_http_headers $input, $cookie, $template->output;
224
225 # Local Variables:
226 # tab-width: 4
227 # End: