Bug 18417: (follow-up) Document new shortcuts in dropdown
[koha.git] / cataloguing / value_builder / cn_browser.pl
blob51767a92f34066bf01caf6bac01693a8ad4a9cf2
1 #!/usr/bin/perl
3 # Converted to new plugin style (Bug 13437)
5 # Copyright 2015 Koha Development Team
7 # This file is part of Koha.
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.
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.
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.
22 use Modern::Perl;
23 use CGI;
25 use C4::Auth;
26 use C4::ClassSource;
27 use C4::Output;
29 my $builder = sub {
30 my ( $params ) = @_;
31 my $function_name = $params->{id};
32 my $res = "
33 <script type=\"text/javascript\">
34 //<![CDATA[
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\");
41 //]]>
42 </script>
44 return $res;
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;
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 },
62 my $cn_sort;
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;
77 if ( $q = $cgi->param('q') ) {
78 $search = $q;
80 if ( $cgi->param('lt') ) {
81 $lt = $cgi->param('lt');
82 $search = $lt;
84 if ( $cgi->param('gt') ) {
85 $gt = $cgi->param('gt');
86 $search = $gt;
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 );
94 my $red = 0;
95 if ( $search ne $gt ) {
96 my $green = 0;
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;
118 $green = 1;
120 unshift @cn, $data;
122 $rows_lt = $sth->rows;
125 if ( $search ne $lt ) {
126 my $green = 0;
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();
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;
148 push @cn, $data;
150 $rows_gt = $sth->rows;
152 if ( !$green && !$red && !$globalGreen ) {
153 push @cn, { 'background' => 'green' };
156 $sth->finish;
159 $template->param( 'q' => $q );
160 $template->param( 'cn_loop' => \@cn ) if $#cn != -1;
161 $template->param( 'popup' => defined( $cgi->param('popup') ) );
163 output_html_with_http_headers $cgi, $cookie, $template->output;
166 return { builder => $builder, launcher => $launcher };