Bug 16203: Convert item plugins to new style (see bug 10480)
[koha.git] / 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 my $builder = sub {
30     my ( $params ) = @_;
31     my $function_name = $params->{id};
32     my $res = "
33 <script type=\"text/javascript\">
34 //<![CDATA[
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=\"+q.value,\"cnbrowser\",\"width=500,height=400,toolbar=false,scrollbars=yes\");
39 }
40
41 //]]>
42 </script>
43 ";
44     return $res;
45 };
46
47 my $launcher = sub {
48     my ( $params ) = @_;
49     my $cgi = $params->{cgi};
50     my $results_per_page = 30;
51     my $current_page = $cgi->param('page') || 1;
52
53     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
54         {   template_name   => "cataloguing/value_builder/cn_browser.tt",
55             query           => $cgi,
56             type            => "intranet",
57             authnotrequired => 0,
58             flagsrequired   => { catalogue => 1 },
59         }
60     );
61
62     my $cn_sort;
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     #Don't show half the results of show lt or gt
90     $real_limit = $results_per_page if $search ne $q;
91     $cn_sort = GetClassSort( undef, undef, $search );
92     my $cn_sort_q = GetClassSort( undef, undef, $q );
93
94     my $red = 0;
95     if ( $search ne $gt ) {
96         my $green = 0;
97
98         #Results before the cn_sort
99         $query = "SELECT b.title, itemcallnumber, biblionumber, barcode, cn_sort, branchname, author
100         FROM items AS i
101         JOIN biblio AS b USING (biblionumber)
102         LEFT OUTER JOIN branches ON (branches.branchcode = homebranch)
103         WHERE cn_sort < ?
104         AND itemcallnumber != ''
105         ORDER BY cn_sort DESC, itemnumber
106         LIMIT $real_limit;";
107         $sth = $dbh->prepare($query);
108         $sth->execute($cn_sort);
109         while ( my $data = $sth->fetchrow_hashref ) {
110             if ( $data->{itemcallnumber} eq $q ) {
111                 $data->{background} = 'red';
112                 $red = 1;
113             } elsif ( ( GetClassSort( undef, undef, $data->{itemcallnumber} ) lt $cn_sort_q ) && !$green && !$red ) {
114                 if ( $#cn != -1 ) {
115                     unshift @cn, { 'background' => 'green' };
116                     $globalGreen = 1;
117                 }
118                 $green = 1;
119             }
120             unshift @cn, $data;
121         }
122         $rows_lt = $sth->rows;
123     }
124
125     if ( $search ne $lt ) {
126         my $green = 0;
127
128         #Results after the cn_sort
129         $query = "SELECT b.title, itemcallnumber, biblionumber, i.cn_sort, branchname, author
130         FROM items AS i
131         JOIN biblio AS b USING (biblionumber)
132         LEFT OUTER JOIN branches ON (branches.branchcode = homebranch)
133         WHERE i.cn_sort >= '$cn_sort'
134         AND itemcallnumber != ''
135         ORDER BY cn_sort, itemnumber
136         LIMIT $real_limit";
137         $sth = $dbh->prepare($query);
138         $sth->execute();
139
140         while ( my $data = $sth->fetchrow_hashref ) {
141             if ( $data->{itemcallnumber} eq $q ) {
142                 $data->{background} = 'red';
143                 $red = 1;
144             } elsif ( ( GetClassSort( undef, undef, $data->{itemcallnumber} ) gt $cn_sort_q ) && !$green && !$red && !$globalGreen ) {
145                 push @cn, { 'background' => 'green' };
146                 $green = 1;
147             }
148             push @cn, $data;
149         }
150         $rows_gt = $sth->rows;
151
152         if ( !$green && !$red && !$globalGreen ) {
153             push @cn, { 'background' => 'green' };
154         }
155
156         $sth->finish;
157     }
158
159     $template->param( 'q'       => $q );
160     $template->param( 'cn_loop' => \@cn ) if $#cn != -1;
161     $template->param( 'popup'   => defined( $cgi->param('popup') ) );
162
163     output_html_with_http_headers $cgi, $cookie, $template->output;
164 };
165
166 return { builder => $builder, launcher => $launcher };