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