Bug 16203: Convert item plugins to new style (see bug 10480)
[koha.git] / 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 type=\"text/javascript\">
34 //<![CDATA[
35 function Click$function_name(i) {
36     newin=window.open(\"../cataloguing/plugin_launcher.pl?plugin_name=macles.pl&index=\"+i,\"MACLES\",',toolbar=false,scrollbars=yes');
37 }
38 //]]>
39 </script>
40 ";
41     return $res;
42 };
43
44 my $launcher = sub {
45     my ( $params ) = @_;
46     my $input = $params->{cgi};
47         my $index= $input->param('index');
48
49         my $dbh = C4::Context->dbh;
50     my $rq=$dbh->prepare("SELECT authorised_value, lib from authorised_values where category=\"MACLES\" order by authorised_value DESC");
51     #tabs
52     $rq->execute;
53     my @BIGLOOP;
54     my (%numbers,@lists,$table);
55     while (my $tab = $rq->fetchrow_hashref){
56       my $number=substr($tab->{authorised_value},0,1);
57       if ($tab->{authorised_value}=~/[0-9]XX/){
58         $numbers{$number}->{'hdr_tab'}=$tab->{lib};
59         $numbers{$number}->{'Table'}=($number=~/[1-7]/);
60       } elsif ($tab->{authorised_value}=~/.X./){
61         $tab->{authorised_value}=~s/X/\./;
62         $table=1;
63         unshift @{$numbers{$number}->{"col_hdr"}},{"colvalue"=>$tab->{authorised_value},"collib"=>$tab->{lib}};
64       } elsif ($tab->{authorised_value}=~/..X/){
65         $tab->{authorised_value}=~s/X/\./;
66         unshift @{$numbers{$number}->{"row_hdr"}},{"rowvalue"=>$tab->{authorised_value},"rowlib"=>$tab->{lib}}
67       } elsif ($tab->{'authorised_value'}=~/,/){
68         my @listval=split /,/,$tab->{'authorised_value'};
69 #          $tab->{authorised_value}=~s/,/","/g;
70 #         $tab->{authorised_value}="(".$tab->{authorised_value}.")";
71         my %mulrows;
72         foreach my $val (@listval){
73           unshift @{$numbers{$number}->{$val}},$tab->{'lib'};
74           my $mulrow=substr($val,0,2);
75           $mulrows{$mulrow}=1;
76         }
77         foreach my $mulrow (sort keys %mulrows){
78           unshift @{$numbers{$number}->{$mulrow}},{'listlib' => $tab->{'lib'},'listvalue' => $tab->{'authorised_value'}};
79         }
80       } else {
81         unshift @{$numbers{$number}->{$tab->{'authorised_value'}}},$tab->{'lib'};
82       }
83     }
84     foreach my $num ( sort keys %numbers ) {
85         my @tmpcolhdr;
86         my @tmprowhdr;
87         @tmpcolhdr = @{ $numbers{$num}->{'col_hdr'} }
88           if ( $numbers{$num}->{'col_hdr'} );
89         @tmprowhdr = @{ $numbers{$num}->{"row_hdr"} }
90           if ( $numbers{$num}->{'row_hdr'} );
91         my @lines;
92         my @lists;
93         my %BIGLOOPcell;
94         foreach my $row (@tmprowhdr) {
95             my $tmprowvalue = $row->{rowvalue};
96             my $rowcode;
97             $rowcode = $1 if $tmprowvalue =~ /[0-9]([0-9])\./;
98             my @cells;
99             if ( scalar(@tmpcolhdr) > 0 ) {
100
101                 #cas du tableau bidim
102                 foreach my $col (@tmpcolhdr) {
103                     my $tmpcolvalue = $col->{colvalue};
104                     my $colcode;
105                     $colcode = $1 if $tmpcolvalue =~ /[0-9]\.([0-9])/;
106                     my %cell;
107                     $cell{celvalue} = $num . $rowcode . $colcode;
108                     $cell{rowvalue} = $tmprowvalue;
109                     $cell{colvalue} = $tmpcolvalue;
110                     if ( $numbers{$num}->{ $num . $rowcode . $colcode } ) {
111
112                         foreach (
113                             @{ $numbers{$num}->{ $num . $rowcode . $colcode } }
114                           )
115                         {
116                             push @{ $cell{libs} }, { 'lib' => $_ };
117                         }
118                     }
119                     else {
120                         push @{ $cell{libs} },
121                           { 'lib' => $num . $rowcode . $colcode };
122                     }
123                     push @cells, \%cell;
124                 }
125                 if ( $numbers{$num}->{ $num . $rowcode } ) {
126                     my @tmpliblist = @{ $numbers{$num}->{ $num . $rowcode } };
127                     push @lists,
128                       { 'lib' => $row->{rowlib}, 'liblist' => \@tmpliblist };
129                 }
130             }
131             else {
132
133                 #Cas de la liste simple
134                 foreach my $key ( sort keys %{ $numbers{$num} } ) {
135                     my %cell;
136                     if ( $key =~ /$num$rowcode[0-9]/ ) {
137                         $cell{celvalue} = $key;
138                         foreach my $lib ( @{ $numbers{$num}->{$key} } ) {
139                             push @{ $cell{'libs'} }, { 'lib' => $lib };
140                         }
141                         push @cells, \%cell;
142                     }
143                 }
144             }
145             push @lines,
146               {
147                 'cells'    => \@cells,
148                 'rowvalue' => $row->{rowvalue},
149                 'rowlib'   => $row->{rowlib}
150               };
151         }
152         $BIGLOOPcell{'Lists'}   = \@lists     if ( scalar(@lists) > 0 );
153         $BIGLOOPcell{'lines'}   = \@lines     if ( scalar(@lines) > 0 );
154         $BIGLOOPcell{'col_hdr'} = \@tmpcolhdr if ( scalar(@tmpcolhdr) > 0 );
155         $BIGLOOPcell{'Table'}   = $numbers{$num}->{'Table'};
156         $BIGLOOPcell{'hdr_tab'} = $numbers{$num}->{'hdr_tab'};
157         $BIGLOOPcell{'number'}  = $num;
158         push @BIGLOOP, \%BIGLOOPcell;
159     }
160
161     my ($template, $loggedinuser, $cookie)
162         = get_template_and_user({template_name => "cataloguing/value_builder/macles.tt",
163                     query => $input,
164                     type => "intranet",
165                     authnotrequired => 0,
166                     flagsrequired => {editcatalogue => '*'},
167                     debug => 1,
168                     });
169     $template->param(BIGLOOP=>\@BIGLOOP);
170         $template->param("index"=>$index);
171         output_html_with_http_headers $input, $cookie, $template->output;
172 };
173
174 return { builder => $builder, launcher => $launcher };