Bug 16794: Group the 2 action buttons into the same column
[koha.git] / cataloguing / value_builder / macles.pl
1 #!/usr/bin/perl 
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Output;
26 use C4::Auth;
27
28 # use Data::Dumper;
29 use vars qw( $tagslib);
30 use vars qw( $authorised_values_sth);
31 use vars qw( $is_a_modif );
32 use utf8;
33
34 sub plugin_javascript {
35 my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
36 my $function_name= "macles".(int(rand(100000))+1);
37 my $res="
38 <script type=\"text/javascript\">
39 //<![CDATA[
40
41 function Clic$function_name(i) {
42         newin=window.open(\"../cataloguing/plugin_launcher.pl?plugin_name=macles.pl&index=\"+i,\"MACLES\",',toolbar=false,scrollbars=yes');
43
44 }
45 //]]>
46 </script>
47 ";
48
49 return ($function_name,$res);
50 }
51
52 sub plugin {
53 my ($input) = @_;
54
55 #       my $input = new CGI;
56         my $index= $input->param('index');
57
58
59         my $dbh = C4::Context->dbh;
60     my $rq=$dbh->prepare("SELECT authorised_value, lib from authorised_values where category=\"MACLES\" order by authorised_value DESC");
61     #tabs
62     $rq->execute;
63     my @BIGLOOP;
64     my (%numbers,@lists,$table);
65     while (my $tab = $rq->fetchrow_hashref){
66       my $number=substr($tab->{authorised_value},0,1);
67       if ($tab->{authorised_value}=~/[0-9]XX/){
68         $numbers{$number}->{'hdr_tab'}=$tab->{lib};
69         $numbers{$number}->{'Table'}=($number=~/[1-7]/);
70       } elsif ($tab->{authorised_value}=~/.X./){
71         $tab->{authorised_value}=~s/X/\./;
72         $table=1;
73         unshift @{$numbers{$number}->{"col_hdr"}},{"colvalue"=>$tab->{authorised_value},"collib"=>$tab->{lib}};
74       } elsif ($tab->{authorised_value}=~/..X/){
75         $tab->{authorised_value}=~s/X/\./;
76         unshift @{$numbers{$number}->{"row_hdr"}},{"rowvalue"=>$tab->{authorised_value},"rowlib"=>$tab->{lib}}
77       } elsif ($tab->{'authorised_value'}=~/,/){
78         my @listval=split /,/,$tab->{'authorised_value'};
79 #          $tab->{authorised_value}=~s/,/","/g;
80 #         $tab->{authorised_value}="(".$tab->{authorised_value}.")";
81         my %mulrows;
82         foreach my $val (@listval){
83           unshift @{$numbers{$number}->{$val}},$tab->{'lib'};
84           my $mulrow=substr($val,0,2);
85           $mulrows{$mulrow}=1;
86         }
87         foreach my $mulrow (sort keys %mulrows){
88           unshift @{$numbers{$number}->{$mulrow}},{'listlib' => $tab->{'lib'},'listvalue' => $tab->{'authorised_value'}};
89         }
90       } else {
91         unshift @{$numbers{$number}->{$tab->{'authorised_value'}}},$tab->{'lib'};
92       }
93 #        use Data::Dumper;warn "BIGLOOP IN".Dumper(@BIGLOOP);
94     }
95     foreach my $num ( sort keys %numbers ) {
96         my @tmpcolhdr;
97         my @tmprowhdr;
98         @tmpcolhdr = @{ $numbers{$num}->{'col_hdr'} }
99           if ( $numbers{$num}->{'col_hdr'} );
100         @tmprowhdr = @{ $numbers{$num}->{"row_hdr"} }
101           if ( $numbers{$num}->{'row_hdr'} );
102         my @lines;
103         my @lists;
104         my %BIGLOOPcell;
105         foreach my $row (@tmprowhdr) {
106             my $tmprowvalue = $row->{rowvalue};
107             my $rowcode;
108             $rowcode = $1 if $tmprowvalue =~ /[0-9]([0-9])\./;
109             my @cells;
110             if ( scalar(@tmpcolhdr) > 0 ) {
111
112                 #cas du tableau bidim
113                 foreach my $col (@tmpcolhdr) {
114                     my $tmpcolvalue = $col->{colvalue};
115                     my $colcode;
116                     $colcode = $1 if $tmpcolvalue =~ /[0-9]\.([0-9])/;
117                     my %cell;
118                     $cell{celvalue} = $num . $rowcode . $colcode;
119                     $cell{rowvalue} = $tmprowvalue;
120                     $cell{colvalue} = $tmpcolvalue;
121                     if ( $numbers{$num}->{ $num . $rowcode . $colcode } ) {
122
123                         foreach (
124                             @{ $numbers{$num}->{ $num . $rowcode . $colcode } }
125                           )
126                         {
127                             push @{ $cell{libs} }, { 'lib' => $_ };
128                         }
129                     }
130                     else {
131                         push @{ $cell{libs} },
132                           { 'lib' => $num . $rowcode . $colcode };
133                     }
134                     push @cells, \%cell;
135                 }
136                 if ( $numbers{$num}->{ $num . $rowcode } ) {
137                     my @tmpliblist = @{ $numbers{$num}->{ $num . $rowcode } };
138                     push @lists,
139                       { 'lib' => $row->{rowlib}, 'liblist' => \@tmpliblist };
140                 }
141             }
142             else {
143
144                 #Cas de la liste simple
145                 foreach my $key ( sort keys %{ $numbers{$num} } ) {
146                     my %cell;
147                     if ( $key =~ /$num$rowcode[0-9]/ ) {
148                         $cell{celvalue} = $key;
149                         foreach my $lib ( @{ $numbers{$num}->{$key} } ) {
150                             push @{ $cell{'libs'} }, { 'lib' => $lib };
151                         }
152                         push @cells, \%cell;
153                     }
154                 }
155             }
156             push @lines,
157               {
158                 'cells'    => \@cells,
159                 'rowvalue' => $row->{rowvalue},
160                 'rowlib'   => $row->{rowlib}
161               };
162         }
163         $BIGLOOPcell{'Lists'}   = \@lists     if ( scalar(@lists) > 0 );
164         $BIGLOOPcell{'lines'}   = \@lines     if ( scalar(@lines) > 0 );
165         $BIGLOOPcell{'col_hdr'} = \@tmpcolhdr if ( scalar(@tmpcolhdr) > 0 );
166         $BIGLOOPcell{'Table'}   = $numbers{$num}->{'Table'};
167         $BIGLOOPcell{'hdr_tab'} = $numbers{$num}->{'hdr_tab'};
168         $BIGLOOPcell{'number'}  = $num;
169         push @BIGLOOP, \%BIGLOOPcell;
170     }
171 #     warn "BIGLOOP OUT".Dumper(@BIGLOOP);
172     my ($template, $loggedinuser, $cookie)
173         = get_template_and_user({template_name => "cataloguing/value_builder/macles.tt",
174                     query => $input,
175                     type => "intranet",
176                     authnotrequired => 0,
177                     flagsrequired => {editcatalogue => '*'},
178                     debug => 1,
179                     });
180     $template->param(BIGLOOP=>\@BIGLOOP);
181         $template->param("index"=>$index);
182         output_html_with_http_headers $input, $cookie, $template->output;
183 }