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