plugin rewrited to works with addbiblio.
[koha.git] / cataloguing / value_builder / macles.pl
1 #!/usr/bin/perl 
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
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 2 of the License, or (at your option) any later
12 # version.
13 #
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.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 use CGI;
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 =head1
35
36 plugin_parameters : other parameters added when the plugin is called by the dopop function
37
38 =cut
39 sub plugin_parameters {
40 my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
41 return "";
42 }
43
44 sub plugin_javascript {
45 my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
46 my $function_name= "macles".(int(rand(100000))+1);
47 my $res="
48 <script>
49 function Focus$function_name(subfield_managed) {
50 return 1;
51 }
52
53 function Blur$function_name(subfield_managed) {
54         return 1;
55 }
56
57 function Clic$function_name(i) {
58         newin=window.open(\"plugin_launcher.pl?plugin_name=macles.pl&index=\"+i,\"MACLES\",',toolbar=false,scrollbars=yes');
59
60 }
61 </script>
62 ";
63
64 return ($function_name,$res);
65 }
66
67 sub plugin {
68 my ($input) = @_;
69         my %env;
70
71 #       my $input = new CGI;
72         my $index= $input->param('index');
73
74
75         my $dbh = C4::Context->dbh;
76     my $rq=$dbh->prepare("SELECT authorised_value, lib from authorised_values where category=\"MACLES\" order by authorised_value DESC");
77     #tabs
78     $rq->execute;
79     my @BIGLOOP;
80     my @innerloop;
81     my (%numbers,%cells,@colhdr,@rowhdr,@multiplelines,@lists,$table);
82     while (my $tab = $rq->fetchrow_hashref){
83 #       if (! utf8::is_utf8($tab->{lib})) {
84 #         utf8::decode($tab->{lib});
85 #       }
86 #       warn $tab->{lib};
87       my $number=substr($tab->{authorised_value},0,1);
88       if ($tab->{authorised_value}=~/[0-9]XX/){
89         $numbers{$number}->{'hdr_tab'}=$tab->{lib};
90         $numbers{$number}->{'Table'}=($number=~/[1-7]/);
91       } elsif ($tab->{authorised_value}=~/.X./){
92         $tab->{authorised_value}=~s/X/\./;
93         $table=1;
94         unshift @{$numbers{$number}->{"col_hdr"}},{"colvalue"=>$tab->{authorised_value},"collib"=>$tab->{lib}};
95       } elsif ($tab->{authorised_value}=~/..X/){
96         $tab->{authorised_value}=~s/X/\./;
97         unshift @{$numbers{$number}->{"row_hdr"}},{"rowvalue"=>$tab->{authorised_value},"rowlib"=>$tab->{lib}}
98       } elsif ($tab->{'authorised_value'}=~/,/){
99         my @listval=split /,/,$tab->{'authorised_value'};
100 #          $tab->{authorised_value}=~s/,/","/g;
101 #         $tab->{authorised_value}="(".$tab->{authorised_value}.")";
102         my %mulrows;
103         foreach my $val (@listval){
104           unshift @{$numbers{$number}->{$val}},$tab->{'lib'};
105           my $mulrow=substr($val,0,2);
106           $mulrows{$mulrow}=1;
107         }
108         foreach my $mulrow (sort keys %mulrows){
109           unshift @{$numbers{$number}->{$mulrow}},{'listlib' => $tab->{'lib'},'listvalue' => $tab->{'authorised_value'}};
110         }
111       } else {
112         unshift @{$numbers{$number}->{$tab->{'authorised_value'}}},$tab->{'lib'};
113       }
114 #        use Data::Dumper;warn "BIGLOOP IN".Dumper(@BIGLOOP);
115     }
116     foreach my $num (sort keys %numbers){
117       my @tmpcolhdr = @{$numbers{$num}->{'col_hdr'}} if ($numbers{$num}->{'col_hdr'});
118       my @tmprowhdr = @{$numbers{$num}->{"row_hdr"}} if ($numbers{$num}->{'row_hdr'});
119       my @lines;
120       my @lists;
121       my %BIGLOOPcell;
122       foreach my $row (@tmprowhdr){
123         my $tmprowvalue=$row->{rowvalue};
124         my $rowcode=$1 if $tmprowvalue=~/[0-9]([0-9])\./;
125         my @cells;
126         if (scalar(@tmpcolhdr)>0){
127         #cas du tableau bidim
128           foreach my $col (@tmpcolhdr){
129             my $tmpcolvalue=$col->{colvalue};
130             my $colcode=$1 if $tmpcolvalue=~/[0-9]\.([0-9])/;
131             my %cell;
132             $cell{celvalue}=$num.$rowcode.$colcode;
133             $cell{rowvalue}=$tmprowvalue;
134             $cell{colvalue}=$tmpcolvalue;
135             if ($numbers{$num}->{$num.$rowcode.$colcode}){
136               foreach (@{$numbers{$num}->{$num.$rowcode.$colcode}}){
137                 push @{$cell{libs}},{'lib'=>$_};
138               }
139             }else {
140               push @{$cell{libs}},{'lib'=>$num.$rowcode.$colcode};
141             }
142             push @cells,\%cell;
143           }
144           if ($numbers{$num}->{$num.$rowcode}){
145             my @tmpliblist=@{$numbers{$num}->{$num.$rowcode}};
146             push @lists,{'lib'=>$row->{rowlib},'liblist'=>\@tmpliblist};
147           }
148         } else {
149         #Cas de la liste simple
150           foreach my $key (sort keys %{$numbers{$num}}){
151             my %cell;
152             if ($key=~/$num$rowcode[0-9]/){
153               $cell{celvalue}=$key;
154               foreach my $lib (@{$numbers{$num}->{$key}}){
155                 push @{$cell{'libs'}},{'lib'=>$lib};
156               }
157               push @cells,\%cell;
158             }
159           }
160         }
161         push @lines,{'cells'=>\@cells,'rowvalue'=>$row->{rowvalue},'rowlib'=>$row->{rowlib}};
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.tmpl",
174                     query => $input,
175                     type => "intranet",
176                     authnotrequired => 0,
177                     flagsrequired => {editcatalogue => 1},
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 }
184 1;
185
186