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