Bug 17600: Standardize our EXPORT_OK
[srvgit] / cataloguing / value_builder / cn_browser.pl
1 #!/usr/bin/perl
2
3 # Converted to new plugin style (Bug 13437)
4
5 # Copyright 2015 Koha Development Team
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 use Modern::Perl;
23
24 use C4::Auth qw( get_template_and_user );
25 use C4::ClassSource qw( GetClassSort );
26 use C4::Output qw( output_html_with_http_headers );
27
28 use Koha::ClassSources;
29
30 my $builder = sub {
31     my ( $params ) = @_;
32     my $function_name = $params->{id};
33     my $res = "
34 <script>
35
36 function Click$function_name(i) {
37     q = document.getElementById('$params->{id}');
38     window.open(\"../cataloguing/plugin_launcher.pl?plugin_name=cn_browser.pl&popup&q=\"+encodeURIComponent(q.value),\"cnbrowser\",\"width=500,height=400,toolbar=false,scrollbars=yes\");
39 }
40
41 </script>
42 ";
43     return $res;
44 };
45
46 my $launcher = sub {
47     my ( $params ) = @_;
48     my $cgi = $params->{cgi};
49     my $results_per_page = 30;
50     my $current_page = $cgi->param('page') || 1;
51
52     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
53         {   template_name   => "cataloguing/value_builder/cn_browser.tt",
54             query           => $cgi,
55             type            => "intranet",
56             flagsrequired   => { catalogue => 1 },
57         }
58     );
59
60     my $dbh = C4::Context->dbh;
61     my $sth;
62     my @cn;
63     my $query;
64     my $real_limit = $results_per_page / 2;
65     my $rows_lt    = 999;
66     my $rows_gt    = 999;
67     my $search;
68     my $globalGreen = 0;
69     my $lt          = '';
70     my $gt          = '';
71     my $q;
72
73     if ( $q = $cgi->param('q') ) {
74         $search = $q;
75     }
76     if ( $cgi->param('lt') ) {
77         $lt     = $cgi->param('lt');
78         $search = $lt;
79     }
80     if ( $cgi->param('gt') ) {
81         $gt     = $cgi->param('gt');
82         $search = $gt;
83     }
84
85     my $cn_source = $cgi->param('cn_source') || C4::Context->preference("DefaultClassificationSource");
86     my @class_sources = Koha::ClassSources->search({ used => 1});
87
88     #Don't show half the results of show lt or gt
89     $real_limit = $results_per_page if $search ne $q;
90     my $cn_sort = GetClassSort( $cn_source, undef, $search );
91
92     my $red = 0;
93     if ( $search ne $gt ) {
94         my $green = 0;
95
96         #Results before the cn_sort
97         $query = "SELECT b.title, b.subtitle, itemcallnumber, biblionumber, barcode, cn_sort, branchname, author
98         FROM items AS i
99         JOIN biblio AS b USING (biblionumber)
100         LEFT OUTER JOIN branches ON (branches.branchcode = homebranch)
101         WHERE cn_sort < ?
102         AND itemcallnumber != ''
103         ORDER BY cn_sort DESC, itemnumber
104         LIMIT $real_limit;";
105         $sth = $dbh->prepare($query);
106         $sth->execute($cn_sort);
107         while ( my $data = $sth->fetchrow_hashref ) {
108             if ( $data->{itemcallnumber} eq $q ) {
109                 $data->{background} = 'red';
110                 $red = 1;
111             } elsif ( $data->{cn_sort} lt $cn_sort && !$green && !$red ) {
112                 if ( $#cn != -1 ) {
113                     unshift @cn, { 'background' => 'green' };
114                     $globalGreen = 1;
115                 }
116                 $green = 1;
117             }
118             unshift @cn, $data;
119         }
120         $rows_lt = $sth->rows;
121     }
122
123     if ( $search ne $lt ) {
124         my $green = 0;
125
126         #Results after the cn_sort
127         $query = "SELECT b.title, b.subtitle, itemcallnumber, biblionumber, barcode, cn_sort, branchname, author
128         FROM items AS i
129         JOIN biblio AS b USING (biblionumber)
130         LEFT OUTER JOIN branches ON (branches.branchcode = homebranch)
131         WHERE i.cn_sort >= '$cn_sort'
132         AND itemcallnumber != ''
133         ORDER BY cn_sort, itemnumber
134         LIMIT $real_limit";
135         $sth = $dbh->prepare($query);
136         $sth->execute();
137
138         while ( my $data = $sth->fetchrow_hashref ) {
139             if ( $data->{itemcallnumber} eq $q ) {
140                 $data->{background} = 'red';
141                 $red = 1;
142             } elsif ( $data->{cn_sort} gt $cn_sort && !$green && !$red && !$globalGreen ) {
143                 push @cn, { 'background' => 'green' };
144                 $green = 1;
145             }
146             push @cn, $data;
147         }
148         $rows_gt = $sth->rows;
149
150         if ( !$green && !$red && !$globalGreen ) {
151             push @cn, { 'background' => 'green' };
152         }
153
154         $sth->finish;
155     }
156
157     $template->param( 'q'       => $q );
158     $template->param( 'cn_loop' => \@cn ) if $#cn != -1;
159     $template->param( 'popup'   => defined( $cgi->param('popup') ) );
160     $template->param( 'cn_source' => $cn_source ) if $cn_source;
161     $template->param( 'class_sources' => \@class_sources );
162
163
164     output_html_with_http_headers $cgi, $cookie, $template->output;
165 };
166
167 return { builder => $builder, launcher => $launcher };