Merge branch 'MT1105' into public_master
[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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use CGI;
23 use C4::Context;
24 use C4::Output;
25 use C4::Auth;
26
27 # use Data::Dumper;
28 use vars qw( $tagslib);
29 use vars qw( $authorised_values_sth);
30 use vars qw( $is_a_modif );
31 use utf8;
32
33 =head1
34
35 plugin_parameters : other parameters added when the plugin is called by the dopop function
36
37 =cut
38 sub plugin_parameters {
39 my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
40 return "";
41 }
42
43 sub plugin_javascript {
44 my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
45 my $function_name= "macles".(int(rand(100000))+1);
46 my $res="
47 <script type=\"text/javascript\">
48 //<![CDATA[
49
50 function Focus$function_name(subfield_managed) {
51 return 1;
52 }
53
54 function Blur$function_name(subfield_managed) {
55         return 1;
56 }
57
58 function Clic$function_name(i) {
59         newin=window.open(\"../cataloguing/plugin_launcher.pl?plugin_name=macles.pl&index=\"+i,\"MACLES\",',toolbar=false,scrollbars=yes');
60
61 }
62 //]]>
63 </script>
64 ";
65
66 return ($function_name,$res);
67 }
68
69 sub plugin {
70 my ($input) = @_;
71         my %env;
72
73 #       my $input = new CGI;
74         my $index= $input->param('index');
75
76
77         my $dbh = C4::Context->dbh;
78     my $rq=$dbh->prepare("SELECT authorised_value, lib from authorised_values where category=\"MACLES\" order by authorised_value DESC");
79     #tabs
80     $rq->execute;
81     my @BIGLOOP;
82     my @innerloop;
83     my (%numbers,%cells,@colhdr,@rowhdr,@multiplelines,@lists,$table);
84     while (my $tab = $rq->fetchrow_hashref){
85 #       if (! utf8::is_utf8($tab->{lib})) {
86 #         utf8::decode($tab->{lib});
87 #       }
88 #       warn $tab->{lib};
89       my $number=substr($tab->{authorised_value},0,1);
90       if ($tab->{authorised_value}=~/[0-9]XX/){
91         $numbers{$number}->{'hdr_tab'}=$tab->{lib};
92         $numbers{$number}->{'Table'}=($number=~/[1-7]/);
93       } elsif ($tab->{authorised_value}=~/.X./){
94         $tab->{authorised_value}=~s/X/\./;
95         $table=1;
96         unshift @{$numbers{$number}->{"col_hdr"}},{"colvalue"=>$tab->{authorised_value},"collib"=>$tab->{lib}};
97       } elsif ($tab->{authorised_value}=~/..X/){
98         $tab->{authorised_value}=~s/X/\./;
99         unshift @{$numbers{$number}->{"row_hdr"}},{"rowvalue"=>$tab->{authorised_value},"rowlib"=>$tab->{lib}}
100       } elsif ($tab->{'authorised_value'}=~/,/){
101         my @listval=split /,/,$tab->{'authorised_value'};
102 #          $tab->{authorised_value}=~s/,/","/g;
103 #         $tab->{authorised_value}="(".$tab->{authorised_value}.")";
104         my %mulrows;
105         foreach my $val (@listval){
106           unshift @{$numbers{$number}->{$val}},$tab->{'lib'};
107           my $mulrow=substr($val,0,2);
108           $mulrows{$mulrow}=1;
109         }
110         foreach my $mulrow (sort keys %mulrows){
111           unshift @{$numbers{$number}->{$mulrow}},{'listlib' => $tab->{'lib'},'listvalue' => $tab->{'authorised_value'}};
112         }
113       } else {
114         unshift @{$numbers{$number}->{$tab->{'authorised_value'}}},$tab->{'lib'};
115       }
116 #        use Data::Dumper;warn "BIGLOOP IN".Dumper(@BIGLOOP);
117     }
118     foreach my $num (sort keys %numbers){
119       my @tmpcolhdr = @{$numbers{$num}->{'col_hdr'}} if ($numbers{$num}->{'col_hdr'});
120       my @tmprowhdr = @{$numbers{$num}->{"row_hdr"}} if ($numbers{$num}->{'row_hdr'});
121       my @lines;
122       my @lists;
123       my %BIGLOOPcell;
124       foreach my $row (@tmprowhdr){
125         my $tmprowvalue=$row->{rowvalue};
126         my $rowcode=$1 if $tmprowvalue=~/[0-9]([0-9])\./;
127         my @cells;
128         if (scalar(@tmpcolhdr)>0){
129         #cas du tableau bidim
130           foreach my $col (@tmpcolhdr){
131             my $tmpcolvalue=$col->{colvalue};
132             my $colcode=$1 if $tmpcolvalue=~/[0-9]\.([0-9])/;
133             my %cell;
134             $cell{celvalue}=$num.$rowcode.$colcode;
135             $cell{rowvalue}=$tmprowvalue;
136             $cell{colvalue}=$tmpcolvalue;
137             if ($numbers{$num}->{$num.$rowcode.$colcode}){
138               foreach (@{$numbers{$num}->{$num.$rowcode.$colcode}}){
139                 push @{$cell{libs}},{'lib'=>$_};
140               }
141             }else {
142               push @{$cell{libs}},{'lib'=>$num.$rowcode.$colcode};
143             }
144             push @cells,\%cell;
145           }
146           if ($numbers{$num}->{$num.$rowcode}){
147             my @tmpliblist=@{$numbers{$num}->{$num.$rowcode}};
148             push @lists,{'lib'=>$row->{rowlib},'liblist'=>\@tmpliblist};
149           }
150         } else {
151         #Cas de la liste simple
152           foreach my $key (sort keys %{$numbers{$num}}){
153             my %cell;
154             if ($key=~/$num$rowcode[0-9]/){
155               $cell{celvalue}=$key;
156               foreach my $lib (@{$numbers{$num}->{$key}}){
157                 push @{$cell{'libs'}},{'lib'=>$lib};
158               }
159               push @cells,\%cell;
160             }
161           }
162         }
163         push @lines,{'cells'=>\@cells,'rowvalue'=>$row->{rowvalue},'rowlib'=>$row->{rowlib}};
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.tmpl",
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 }
186 1;
187
188