Bug 30477: Add new UNIMARC installer translation files
[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
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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 use Modern::Perl;
50
51 use CGI qw ( -utf8 );
52 use C4::Koha qw( GetAuthorisedValues );
53 use C4::Auth qw( get_template_and_user );
54 use C4::Context;
55 use C4::Output qw( output_html_with_http_headers );
56 use C4::Search qw( new_record_from_zebra );
57 use C4::Biblio qw( TransformMarcToKoha );
58
59 use Koha::ItemTypes;
60 use Koha::SearchEngine;
61 use Koha::SearchEngine::Search;
62
63 my $input = CGI->new;
64 my $op = $input->param('op') || q{};
65 my $dbh = C4::Context->dbh;
66
67 my $startfrom = $input->param('startfrom');
68 $startfrom = 0 unless $startfrom;
69 my ( $template, $loggedinuser, $cookie );
70 my $resultsperpage;
71
72 my $itype_or_itemtype =
73   ( C4::Context->preference("item-level_itypes") ) ? 'itype' : 'itemtype';
74
75 my $query = $input->param('q');
76
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         {
82             template_name   => "serials/result.tt",
83             query           => $input,
84             type            => "intranet",
85             flagsrequired   => { catalogue => 1, serials => '*' },
86         }
87     );
88
89     # add the limits if applicable
90     my $itemtypelimit = $input->param('itemtypelimit');
91     my $ccodelimit    = $input->param('ccodelimit');
92     my $op = 'AND';
93     $query .= " $op $itype_or_itemtype:$itemtypelimit" if $itemtypelimit;
94     $query .= " $op ccode:$ccodelimit" if $ccodelimit;
95     $resultsperpage = $input->param('resultsperpage');
96     $resultsperpage = 20 if ( !defined $resultsperpage );
97
98     my $searcher = Koha::SearchEngine::Search->new({index => $Koha::SearchEngine::BIBLIOS_INDEX});
99     my ( $error, $marcrecords, $total_hits ) =
100       $searcher->simple_search_compat( $query, $startfrom * $resultsperpage, $resultsperpage );
101     my $total = 0;
102     if ( defined $marcrecords ) {
103         $total = scalar @{$marcrecords};
104     }
105
106     if ( defined $error ) {
107         $template->param( query_error => $error );
108         warn "error: " . $error;
109         output_html_with_http_headers $input, $cookie, $template->output;
110         exit;
111     }
112     my @results;
113
114     for ( my $i = 0 ; $i < $total ; $i++ ) {
115         my %resultsloop;
116         my $marcrecord = C4::Search::new_record_from_zebra( 'biblioserver', $marcrecords->[$i] );
117         my $biblio = TransformMarcToKoha( $marcrecord, '' );
118
119         #build the hash for the template.
120         $resultsloop{highlight}       = ( $i % 2 ) ? (1) : (0);
121         $resultsloop{title}           = $biblio->{'title'};
122         $resultsloop{subtitle}        = $biblio->{'subtitle'};
123         $resultsloop{medium}          = $biblio->{'medium'};
124         $resultsloop{part_number}     = $biblio->{'part_number'};
125         $resultsloop{part_name}       = $biblio->{'part_name'};
126         $resultsloop{biblionumber}    = $biblio->{'biblionumber'};
127         $resultsloop{author}          = $biblio->{'author'};
128         $resultsloop{publishercode}   = $biblio->{'publishercode'};
129         $resultsloop{publicationyear} = $biblio->{'publicationyear'} ? $biblio->{'publicationyear'} : $biblio->{'copyrightdate'};
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     my @numbers = ();
143
144     if ( $total_hits > $resultsperpage ) {
145         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
146             if ( $i < 16 ) {
147                 my $highlight = 0;
148                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
149                 push @numbers,
150                   {
151                     number     => $i,
152                     highlight  => $highlight,
153                     searchdata => \@results,
154                     startfrom  => ( $i - 1 )
155                   };
156             }
157         }
158     }
159
160     my $from = 0;
161     $from = $startfrom * $resultsperpage + 1 if ( $total_hits > 0 );
162     my $to;
163
164     if ( $total_hits < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
165         $to = $total;
166     }
167     else {
168         $to = ( ( $startfrom + 1 ) * $resultsperpage );
169     }
170     $template->param(
171         query          => $query,
172         resultsloop    => \@results,
173         startfrom      => $startfrom,
174         displaynext    => $displaynext,
175         displayprev    => $displayprev,
176         resultsperpage => $resultsperpage,
177         startfromnext  => $startfrom + 1,
178         startfromprev  => $startfrom - 1,
179         total          => $total_hits,
180         from           => $from,
181         to             => $to,
182         numbers        => \@numbers,
183     );
184 }    # end of if ($op eq "do_search" & $query)
185 else {
186     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
187         {
188             template_name   => "serials/subscription-bib-search.tt",
189             query           => $input,
190             type            => "intranet",
191             flagsrequired   => { catalogue => 1, serials => '*' },
192         }
193     );
194
195     # load the itemtypes
196     my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
197     my @itemtypesloop;
198     # FIXME This is uselessly complex, the iterator should be send to the template
199     # FIXME The translated_description should be used
200     foreach my $thisitemtype (
201         sort {
202             $itemtypes->{$a}->{'description'}
203               cmp $itemtypes->{$b}->{'description'}
204         } keys %$itemtypes
205       )
206     {
207         my %row = (
208             code        => $thisitemtype,
209             description => $itemtypes->{$thisitemtype}->{'description'},
210         );
211         push @itemtypesloop, \%row;
212     }
213
214     # load Collection Codes
215     my $authvalues = GetAuthorisedValues('CCODE');
216     my @ccodesloop;
217     for my $thisauthvalue ( sort { $a->{'lib'} cmp $b->{'lib'} } @$authvalues )
218     {
219         my %row = (
220             code        => $thisauthvalue->{'authorised_value'},
221             description => $thisauthvalue->{'lib'},
222         );
223         push @ccodesloop, \%row;
224     }
225
226     $template->param(
227         itemtypeloop => \@itemtypesloop,
228         ccodeloop    => \@ccodesloop,
229         no_query     => $op eq "do_search" ? 1 : 0,
230     );
231 }
232
233 # Print the page
234 output_html_with_http_headers $input, $cookie, $template->output;
235
236 # Local Variables:
237 # tab-width: 4
238 # End: