Bug 26384: Fix executable flags
[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
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 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             flagsrequired   => { catalogue => 1 },
60         }
61     );
62
63     my $dbh = C4::Context->dbh;
64     my $sth;
65     my @cn;
66     my $query;
67     my $real_limit = $results_per_page / 2;
68     my $rows_lt    = 999;
69     my $rows_gt    = 999;
70     my $search;
71     my $globalGreen = 0;
72     my $lt          = '';
73     my $gt          = '';
74     my $q;
75
76     if ( $q = $cgi->param('q') ) {
77         $search = $q;
78     }
79     if ( $cgi->param('lt') ) {
80         $lt     = $cgi->param('lt');
81         $search = $lt;
82     }
83     if ( $cgi->param('gt') ) {
84         $gt     = $cgi->param('gt');
85         $search = $gt;
86     }
87
88     my $cn_source = $cgi->param('cn_source') || C4::Context->preference("DefaultClassificationSource");
89     my @class_sources = Koha::ClassSources->search({ used => 1});
90
91     #Don't show half the results of show lt or gt
92     $real_limit = $results_per_page if $search ne $q;
93     my $cn_sort = GetClassSort( $cn_source, undef, $search );
94
95     my $red = 0;
96     if ( $search ne $gt ) {
97         my $green = 0;
98
99         #Results before the cn_sort
100         $query = "SELECT b.title, b.subtitle, itemcallnumber, biblionumber, barcode, cn_sort, branchname, author
101         FROM items AS i
102         JOIN biblio AS b USING (biblionumber)
103         LEFT OUTER JOIN branches ON (branches.branchcode = homebranch)
104         WHERE cn_sort < ?
105         AND itemcallnumber != ''
106         ORDER BY cn_sort DESC, itemnumber
107         LIMIT $real_limit;";
108         $sth = $dbh->prepare($query);
109         $sth->execute($cn_sort);
110         while ( my $data = $sth->fetchrow_hashref ) {
111             if ( $data->{itemcallnumber} eq $q ) {
112                 $data->{background} = 'red';
113                 $red = 1;
114             } elsif ( $data->{cn_sort} lt $cn_sort && !$green && !$red ) {
115                 if ( $#cn != -1 ) {
116                     unshift @cn, { 'background' => 'green' };
117                     $globalGreen = 1;
118                 }
119                 $green = 1;
120             }
121             unshift @cn, $data;
122         }
123         $rows_lt = $sth->rows;
124     }
125
126     if ( $search ne $lt ) {
127         my $green = 0;
128
129         #Results after the cn_sort
130         $query = "SELECT b.title, b.subtitle, itemcallnumber, biblionumber, barcode, cn_sort, branchname, author
131         FROM items AS i
132         JOIN biblio AS b USING (biblionumber)
133         LEFT OUTER JOIN branches ON (branches.branchcode = homebranch)
134         WHERE i.cn_sort >= '$cn_sort'
135         AND itemcallnumber != ''
136         ORDER BY cn_sort, itemnumber
137         LIMIT $real_limit";
138         $sth = $dbh->prepare($query);
139         $sth->execute();
140
141         while ( my $data = $sth->fetchrow_hashref ) {
142             if ( $data->{itemcallnumber} eq $q ) {
143                 $data->{background} = 'red';
144                 $red = 1;
145             } elsif ( $data->{cn_sort} gt $cn_sort && !$green && !$red && !$globalGreen ) {
146                 push @cn, { 'background' => 'green' };
147                 $green = 1;
148             }
149             push @cn, $data;
150         }
151         $rows_gt = $sth->rows;
152
153         if ( !$green && !$red && !$globalGreen ) {
154             push @cn, { 'background' => 'green' };
155         }
156
157         $sth->finish;
158     }
159
160     $template->param( 'q'       => $q );
161     $template->param( 'cn_loop' => \@cn ) if $#cn != -1;
162     $template->param( 'popup'   => defined( $cgi->param('popup') ) );
163     $template->param( 'cn_source' => $cn_source ) if $cn_source;
164     $template->param( 'class_sources' => \@class_sources );
165
166
167     output_html_with_http_headers $cgi, $cookie, $template->output;
168 };
169
170 return { builder => $builder, launcher => $launcher };