Bug 6874: License Text Update
[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         my %env;
55
56 #       my $input = new CGI;
57         my $index= $input->param('index');
58
59
60         my $dbh = C4::Context->dbh;
61     my $rq=$dbh->prepare("SELECT authorised_value, lib from authorised_values where category=\"MACLES\" order by authorised_value DESC");
62     #tabs
63     $rq->execute;
64     my @BIGLOOP;
65     my @innerloop;
66     my (%numbers,%cells,@colhdr,@rowhdr,@multiplelines,@lists,$table);
67     while (my $tab = $rq->fetchrow_hashref){
68       my $number=substr($tab->{authorised_value},0,1);
69       if ($tab->{authorised_value}=~/[0-9]XX/){
70         $numbers{$number}->{'hdr_tab'}=$tab->{lib};
71         $numbers{$number}->{'Table'}=($number=~/[1-7]/);
72       } elsif ($tab->{authorised_value}=~/.X./){
73         $tab->{authorised_value}=~s/X/\./;
74         $table=1;
75         unshift @{$numbers{$number}->{"col_hdr"}},{"colvalue"=>$tab->{authorised_value},"collib"=>$tab->{lib}};
76       } elsif ($tab->{authorised_value}=~/..X/){
77         $tab->{authorised_value}=~s/X/\./;
78         unshift @{$numbers{$number}->{"row_hdr"}},{"rowvalue"=>$tab->{authorised_value},"rowlib"=>$tab->{lib}}
79       } elsif ($tab->{'authorised_value'}=~/,/){
80         my @listval=split /,/,$tab->{'authorised_value'};
81 #          $tab->{authorised_value}=~s/,/","/g;
82 #         $tab->{authorised_value}="(".$tab->{authorised_value}.")";
83         my %mulrows;
84         foreach my $val (@listval){
85           unshift @{$numbers{$number}->{$val}},$tab->{'lib'};
86           my $mulrow=substr($val,0,2);
87           $mulrows{$mulrow}=1;
88         }
89         foreach my $mulrow (sort keys %mulrows){
90           unshift @{$numbers{$number}->{$mulrow}},{'listlib' => $tab->{'lib'},'listvalue' => $tab->{'authorised_value'}};
91         }
92       } else {
93         unshift @{$numbers{$number}->{$tab->{'authorised_value'}}},$tab->{'lib'};
94       }
95 #        use Data::Dumper;warn "BIGLOOP IN".Dumper(@BIGLOOP);
96     }
97     foreach my $num ( sort keys %numbers ) {
98         my @tmpcolhdr;
99         my @tmprowhdr;
100         @tmpcolhdr = @{ $numbers{$num}->{'col_hdr'} }
101           if ( $numbers{$num}->{'col_hdr'} );
102         @tmprowhdr = @{ $numbers{$num}->{"row_hdr"} }
103           if ( $numbers{$num}->{'row_hdr'} );
104         my @lines;
105         my @lists;
106         my %BIGLOOPcell;
107         foreach my $row (@tmprowhdr) {
108             my $tmprowvalue = $row->{rowvalue};
109             my $rowcode;
110             $rowcode = $1 if $tmprowvalue =~ /[0-9]([0-9])\./;
111             my @cells;
112             if ( scalar(@tmpcolhdr) > 0 ) {
113
114                 #cas du tableau bidim
115                 foreach my $col (@tmpcolhdr) {
116                     my $tmpcolvalue = $col->{colvalue};
117                     my $colcode;
118                     $colcode = $1 if $tmpcolvalue =~ /[0-9]\.([0-9])/;
119                     my %cell;
120                     $cell{celvalue} = $num . $rowcode . $colcode;
121                     $cell{rowvalue} = $tmprowvalue;
122                     $cell{colvalue} = $tmpcolvalue;
123                     if ( $numbers{$num}->{ $num . $rowcode . $colcode } ) {
124
125                         foreach (
126                             @{ $numbers{$num}->{ $num . $rowcode . $colcode } }
127                           )
128                         {
129                             push @{ $cell{libs} }, { 'lib' => $_ };
130                         }
131                     }
132                     else {
133                         push @{ $cell{libs} },
134                           { 'lib' => $num . $rowcode . $colcode };
135                     }
136                     push @cells, \%cell;
137                 }
138                 if ( $numbers{$num}->{ $num . $rowcode } ) {
139                     my @tmpliblist = @{ $numbers{$num}->{ $num . $rowcode } };
140                     push @lists,
141                       { 'lib' => $row->{rowlib}, 'liblist' => \@tmpliblist };
142                 }
143             }
144             else {
145
146                 #Cas de la liste simple
147                 foreach my $key ( sort keys %{ $numbers{$num} } ) {
148                     my %cell;
149                     if ( $key =~ /$num$rowcode[0-9]/ ) {
150                         $cell{celvalue} = $key;
151                         foreach my $lib ( @{ $numbers{$num}->{$key} } ) {
152                             push @{ $cell{'libs'} }, { 'lib' => $lib };
153                         }
154                         push @cells, \%cell;
155                     }
156                 }
157             }
158             push @lines,
159               {
160                 'cells'    => \@cells,
161                 'rowvalue' => $row->{rowvalue},
162                 'rowlib'   => $row->{rowlib}
163               };
164         }
165         $BIGLOOPcell{'Lists'}   = \@lists     if ( scalar(@lists) > 0 );
166         $BIGLOOPcell{'lines'}   = \@lines     if ( scalar(@lines) > 0 );
167         $BIGLOOPcell{'col_hdr'} = \@tmpcolhdr if ( scalar(@tmpcolhdr) > 0 );
168         $BIGLOOPcell{'Table'}   = $numbers{$num}->{'Table'};
169         $BIGLOOPcell{'hdr_tab'} = $numbers{$num}->{'hdr_tab'};
170         $BIGLOOPcell{'number'}  = $num;
171         push @BIGLOOP, \%BIGLOOPcell;
172     }
173 #     warn "BIGLOOP OUT".Dumper(@BIGLOOP);
174     my ($template, $loggedinuser, $cookie)
175         = get_template_and_user({template_name => "cataloguing/value_builder/macles.tt",
176                     query => $input,
177                     type => "intranet",
178                     authnotrequired => 0,
179                     flagsrequired => {editcatalogue => '*'},
180                     debug => 1,
181                     });
182     $template->param(BIGLOOP=>\@BIGLOOP);
183         $template->param("index"=>$index);
184         output_html_with_http_headers $input, $cookie, $template->output;
185 }