MT 1587 : CSV export for cart and shelves, with the ability to define different expor...
[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 use warnings;
52
53 use CGI;
54 use C4::Koha;
55 use C4::Auth;
56 use C4::Context;
57 use C4::Output;
58 use C4::Search;
59 use C4::Biblio;
60
61 my $input=new CGI;
62 # my $type=$query->param('type');
63 my $op = $input->param('op') || q{};
64 my $dbh = C4::Context->dbh;
65
66 my $startfrom=$input->param('startfrom');
67 $startfrom=0 unless $startfrom;
68 my ($template, $loggedinuser, $cookie);
69 my $resultsperpage;
70
71 my $query = $input->param('q');
72 # don't run the search if no search term !
73 if ($op eq "do_search" && $query) {
74
75     # add the itemtype limit if applicable
76     my $itemtypelimit = $input->param('itemtypelimit');
77     if ( $itemtypelimit ) {
78         my $index = C4::Context->preference("item-level_itypes") ? 'itype' : 'itemtype';
79         $query .= " AND $index=$itemtypelimit";
80     }
81
82     $resultsperpage= $input->param('resultsperpage');
83     $resultsperpage = 20 if(!defined $resultsperpage);
84
85     my ($error, $marcrecords, $total_hits) = SimpleSearch($query, $startfrom*$resultsperpage, $resultsperpage);
86     my $total = scalar @$marcrecords;
87
88     if (defined $error) {
89         $template->param(query_error => $error);
90         warn "error: ".$error;
91         output_html_with_http_headers $input, $cookie, $template->output;
92         exit;
93     }
94     my @results;
95
96     for(my $i=0;$i<$total;$i++) {
97         my %resultsloop;
98         my $marcrecord = MARC::File::USMARC::decode($marcrecords->[$i]);
99         my $biblio = TransformMarcToKoha(C4::Context->dbh,$marcrecord,'');
100
101         #build the hash for the template.
102         $resultsloop{highlight}       = ($i % 2)?(1):(0);
103         $resultsloop{title}           = $biblio->{'title'};
104         $resultsloop{subtitle}        = $biblio->{'subtitle'};
105         $resultsloop{biblionumber}    = $biblio->{'biblionumber'};
106         $resultsloop{author}          = $biblio->{'author'};
107         $resultsloop{publishercode}   = $biblio->{'publishercode'};
108         $resultsloop{publicationyear} = $biblio->{'publicationyear'};
109
110         push @results, \%resultsloop;
111     }
112
113     ($template, $loggedinuser, $cookie)
114         = get_template_and_user({template_name => "serials/result.tmpl",
115                 query => $input,
116                 type => "intranet",
117                 authnotrequired => 0,
118                 flagsrequired => {serials => 1},
119                 flagsrequired => {catalogue => 1},
120                 debug => 1,
121                 });
122
123     # multi page display gestion
124     my $displaynext=0;
125     my $displayprev=$startfrom;
126     if(($total_hits - (($startfrom+1)*($resultsperpage))) > 0 ){
127         $displaynext = 1;
128     }
129
130
131     my @numbers = ();
132
133     if ($total_hits>$resultsperpage)
134     {
135         for (my $i=1; $i<$total/$resultsperpage+1; $i++)
136         {
137             if ($i<16)
138             {
139                 my $highlight=0;
140                 ($startfrom==($i-1)) && ($highlight=1);
141                 push @numbers, { number => $i,
142                     highlight => $highlight ,
143                     searchdata=> \@results,
144                     startfrom => ($i-1)};
145             }
146         }
147     }
148
149     my $from = 0;
150     $from = $startfrom*$resultsperpage+1 if($total_hits > 0);
151     my $to;
152
153     if($total_hits < (($startfrom+1)*$resultsperpage))
154     {
155         $to = $total;
156     } else {
157         $to = (($startfrom+1)*$resultsperpage);
158     }
159     $template->param(
160                             query => $query,
161                             resultsloop => \@results,
162                             startfrom=> $startfrom,
163                             displaynext=> $displaynext,
164                             displayprev=> $displayprev,
165                             resultsperpage => $resultsperpage,
166                             startfromnext => $startfrom+1,
167                             startfromprev => $startfrom-1,
168                             total=>$total_hits,
169                             from=>$from,
170                             to=>$to,
171                             numbers=>\@numbers,
172                             );
173 } # end of if ($op eq "do_search" & $query)
174  elsif ($op eq "do_search") {
175     ($template, $loggedinuser, $cookie)
176         = get_template_and_user({template_name => "serials/subscription-bib-search.tmpl",
177                 query => $input,
178                 type => "intranet",
179                 authnotrequired => 0,
180                 flagsrequired => {catalogue => 1, serials=>1},
181                 debug => 1,
182                 });
183     # load the itemtypes
184     my $itemtypes = GetItemTypes;
185     my @itemtypesloop;
186     my $selected=1;
187     my $cnt;
188     foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'} } keys %$itemtypes ) {
189         my %row =(
190                     code => $thisitemtype,
191                     selected => $selected,
192                     description => $itemtypes->{$thisitemtype}->{'description'},
193                 );
194         $selected = 0 if ($selected) ;
195         push @itemtypesloop, \%row;
196     }
197     $template->param(itemtypeloop => \@itemtypesloop);
198     $template->param("no_query" => 1);
199 }
200  else {
201     ($template, $loggedinuser, $cookie)
202         = get_template_and_user({template_name => "serials/subscription-bib-search.tmpl",
203                 query => $input,
204                 type => "intranet",
205                 authnotrequired => 0,
206                 flagsrequired => {catalogue => 1, serials=>1},
207                 debug => 1,
208                 });
209     # load the itemtypes
210     my $itemtypes = GetItemTypes;
211     my @itemtypesloop;
212     my $selected=1;
213     my $cnt;
214     foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'} } keys %$itemtypes ) {
215         my %row =(
216                     code => $thisitemtype,
217                     selected => $selected,
218                     description => $itemtypes->{$thisitemtype}->{'description'},
219                 );
220         $selected = 0 if ($selected) ;
221         push @itemtypesloop, \%row;
222     }
223     $template->param(itemtypeloop => \@itemtypesloop);
224     $template->param("no_query" => 0);
225 }
226
227 # Print the page
228 output_html_with_http_headers $input, $cookie, $template->output;
229
230 # Local Variables:
231 # tab-width: 4
232 # End: