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