Bug 7679: Fix several bugs in circulation statistics wizard
[koha.git] / cataloguing / value_builder / cn_browser.pl
blob9599666f9de54f571b8d847691b8a538ee37c1b5
1 use Modern::Perl;
2 no warnings 'redefine';
4 use CGI;
5 use C4::Auth;
6 use C4::ClassSource;
7 use C4::Output;
9 sub plugin_javascript {
10 my ( $dbh, $record, $tagslib, $field_number, $tabloop ) = @_;
11 my $function_name = "328" . ( int( rand(100000) ) + 1 );
12 my $res = "
13 <script type=\"text/javascript\">
14 //<![CDATA[
16 function Clic$function_name(i) {
17 q = document.getElementById('$field_number');
18 window.open(\"../cataloguing/plugin_launcher.pl?plugin_name=cn_browser.pl&popup&q=\"+q.value,\"cnbrowser\",\"width=500,height=400,toolbar=false,scrollbars=yes\");
21 //]]>
22 </script>
25 return ( $function_name, $res );
28 sub plugin {
29 my ($input) = @_;
30 my $cgi = new CGI;
31 my $params = $cgi->Vars;
32 my $results_per_page = 30;
33 my $current_page = $cgi->param('page') || 1;
35 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36 { template_name => "cataloguing/value_builder/cn_browser.tt",
37 query => $cgi,
38 type => "intranet",
39 authnotrequired => 0,
40 flagsrequired => { catalogue => 1 },
44 my $cn_sort;
46 my $dbh = C4::Context->dbh;
47 my $sth;
48 my @cn;
49 my $query;
50 my $real_limit = $results_per_page / 2;
51 my $rows_lt = 999;
52 my $rows_gt = 999;
53 my $search;
54 my $globalGreen = 0;
55 my $lt = '';
56 my $gt = '';
57 my $q;
59 if ( $q = $cgi->param('q') ) {
60 $search = $q;
62 if ( $cgi->param('lt') ) {
63 $lt = $cgi->param('lt');
64 $search = $lt;
66 if ( $cgi->param('gt') ) {
67 $gt = $cgi->param('gt');
68 $search = $gt;
71 #Don't show half the results of show lt or gt
72 $real_limit = $results_per_page if $search ne $q;
73 $cn_sort = GetClassSort( undef, undef, $search );
74 my $cn_sort_q = GetClassSort( undef, undef, $q );
76 my $red = 0;
77 if ( $search ne $gt ) {
78 my $green = 0;
80 #Results before the cn_sort
81 $query = "SELECT b.title, itemcallnumber, biblionumber, barcode, cn_sort, branchname, author
82 FROM items AS i
83 JOIN biblio AS b USING (biblionumber)
84 LEFT OUTER JOIN branches ON (branches.branchcode = homebranch)
85 WHERE cn_sort < ?
86 AND itemcallnumber != ''
87 ORDER BY cn_sort DESC, itemnumber
88 LIMIT $real_limit;";
89 $sth = $dbh->prepare($query);
90 $sth->execute($cn_sort);
91 while ( my $data = $sth->fetchrow_hashref ) {
92 if ( $data->{itemcallnumber} eq $q ) {
93 $data->{background} = 'red';
94 $red = 1;
95 } elsif ( ( GetClassSort( undef, undef, $data->{itemcallnumber} ) lt $cn_sort_q ) && !$green && !$red ) {
96 if ( $#cn != -1 ) {
97 unshift @cn, { 'background' => 'green' };
98 $globalGreen = 1;
100 $green = 1;
102 unshift @cn, $data;
104 $rows_lt = $sth->rows;
107 if ( $search ne $lt ) {
108 my $green = 0;
110 #Results after the cn_sort
111 $query = "SELECT b.title, itemcallnumber, biblionumber, i.cn_sort, branchname, author
112 FROM items AS i
113 JOIN biblio AS b USING (biblionumber)
114 LEFT OUTER JOIN branches ON (branches.branchcode = homebranch)
115 WHERE i.cn_sort >= '$cn_sort'
116 AND itemcallnumber != ''
117 ORDER BY cn_sort, itemnumber
118 LIMIT $real_limit";
119 $sth = $dbh->prepare($query);
120 $sth->execute();
122 while ( my $data = $sth->fetchrow_hashref ) {
123 if ( $data->{itemcallnumber} eq $q ) {
124 $data->{background} = 'red';
125 $red = 1;
126 } elsif ( ( GetClassSort( undef, undef, $data->{itemcallnumber} ) gt $cn_sort_q ) && !$green && !$red && !$globalGreen ) {
127 push @cn, { 'background' => 'green' };
128 $green = 1;
130 push @cn, $data;
132 $rows_gt = $sth->rows;
134 if ( !$green && !$red && !$globalGreen ) {
135 push @cn, { 'background' => 'green' };
138 $sth->finish;
141 $template->param( 'q' => $q );
142 $template->param( 'cn_loop' => \@cn ) if $#cn != -1;
143 $template->param( 'popup' => defined( $cgi->param('popup') ) );
145 output_html_with_http_headers $cgi, $cookie, $template->output;