[14/40] Work on batch management interface.
[koha.git] / labels / label-manage.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2006 Katipo Communications.
4 # Parts Copyright 2009 Foundations Bible College.
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 warnings;
23 use vars qw($debug);
24
25 use Sys::Syslog qw(syslog);
26 use Switch qw(Perl6);
27 use CGI;
28 use HTML::Template::Pro;
29 use Data::Dumper;
30
31 use C4::Auth;
32 use C4::Output;
33 use C4::Context;
34 use autouse 'C4::Branch' => qw(get_branch_code_from_name);
35 use C4::Debug;
36 use C4::Labels::Lib 1.000000 qw(get_all_templates get_all_layouts get_all_profiles get_batch_summary get_barcode_types get_label_types get_column_names get_table_names SELECT);
37 use C4::Labels::Layout 1.000000;
38 use C4::Labels::Template 1.000000;
39 use C4::Labels::Profile 1.000000;
40 use C4::Labels::Batch 1.000000;
41
42 my $cgi = new CGI;
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44     {
45         template_name   => "labels/label-manage.tmpl",
46         query           => $cgi,
47         type            => "intranet",
48         authnotrequired => 0,
49         flagsrequired   => { catalogue => 1 },
50         debug           => 1,
51     }
52 );
53
54 my $error = 0;
55 my $db_rows = {};
56 my $display_columns = { layout =>   [  #db column       => display column 
57                                         {layout_id       => 'Layout ID'},
58                                         {layout_name     => 'Layout'},
59                                         {barcode_type    => 'Barcode Type'},
60                                         {printing_type   => 'Print Type'},
61                                         {format_string   => 'Fields to Print'},
62                                         {select          => 'Select'},
63                                     ],
64                         template => [   {template_id     => 'Template ID'},
65                                         {template_code   => 'Template Name'},
66                                         {template_desc   => 'Description'},
67                                         {select          => 'Select'},
68                                     ],
69                         profile =>  [   {profile_id      => 'Profile ID'},
70                                         {printer_name    => 'Printer Name'},
71                                         {paper_bin       => 'Paper Bin'},
72                                         {_template_code  => 'Template Name'},     # this display column does not have a corrisponding db column in the profile table, hence the underscore
73                                         {select          => 'Select'},
74                                     ],
75                         batch =>    [   {batch_id        => 'Batch ID'},
76                                         {_item_count     => 'Item Count'},
77                                         {select          => 'Select'},
78                                     ],
79 };
80
81 my $label_element = $cgi->param('label_element') || $ARGV[0];
82 my $op = $cgi->param('op') || $ARGV[1] || '';
83 my $element_id = $cgi->param('element_id') || $ARGV[2] || '';
84 my $branch_code = ($label_element eq 'batch' ? get_branch_code_from_name($template->param('LoginBranchname')) : '');
85
86 sub _build_table {
87     my $headers = shift;
88     my $data = shift;
89     my $table = [];
90     my $fields = [];
91     my @db_columns = ();
92     my ($row_index, $col_index) = (0,0);
93     my $cols = 0;       # number of columns to wrap on
94     my $field_count = 0;
95     POPULATE_HEADER:
96     foreach my $db_column (@$headers) {
97         my @key = keys %$db_column;
98         push (@db_columns, $key[0]);
99         $$fields[$col_index] = {select_field => 0, field_name => ($key[0]), field_label => $db_column->{$key[0]}};
100         $field_count++;
101         $col_index++;
102     }
103     $$table[$row_index] = {header_fields => $fields};
104     $cols = $col_index;
105     $field_count *= scalar(@$data);     # total fields to be displayed in the table
106     $col_index = 0;
107     $row_index++;
108     $fields = [];
109     POPULATE_TABLE:
110     foreach my $db_row (@$data) {
111         my $element_id = 0;
112         POPULATE_ROW:
113         foreach my $db_column (@db_columns) {
114             if (grep {$db_column eq $_} keys %$db_row) {
115                 $element_id = $db_row->{$db_column} if $db_column =~ m/id/;
116                 $$fields[$col_index] = {select_field => 0, field_name => ($db_column . "_tbl"), field_value => $db_row->{$db_column}};
117                 $col_index++;
118                 next POPULATE_ROW;
119             }
120             elsif ($db_column =~ m/^_((.*)_(.*$))/) {   # this a special case
121                 my $table_name = get_table_names($2);
122                 my $record_set = SELECT($1, @$table_name[0], $2 . "_id = " . $db_row->{$2 . "_id"});
123                 $$fields[$col_index] = {select_field => 0, field_name => ($db_column . "_tbl"), field_value => $$record_set[0]{$1}};
124                 $col_index++;
125                 next POPULATE_ROW;
126             }
127         }
128         $$fields[$col_index] = {select_field => 1, field_name => 'select', field_value => $element_id};
129         $$table[$row_index] = {text_fields => $fields};
130         $col_index = 0;
131         $row_index++;
132         $fields = [];
133     }
134     return $table;
135 }
136
137 if ($op eq 'delete') {
138     given ($label_element) {
139         when 'layout'   {$error = C4::Labels::Layout::delete(layout_id => $element_id); last;}
140         when 'template' {$error = C4::Labels::Template::delete(template_id => $element_id); last;}
141         when 'profile'  {$error = C4::Labels::Profile::delete(profile_id => $element_id); last;}
142         when 'batch'    {$error = C4::Labels::Batch::delete(batch_id => $element_id); last;}
143         default         {}      # FIXME: Some error trapping code 
144     }
145 }
146
147 given ($label_element) {
148     when 'layout'       {$db_rows = get_all_layouts();}
149     when 'template'     {$db_rows = get_all_templates();}
150     when 'profile'      {$db_rows = get_all_profiles();}
151     when 'batch'        {$db_rows = get_batch_summary(filter => "branch_code=\'$branch_code\'");}
152     default             {}      # FIXME: Some error trapping code
153 }
154
155 my $table = _build_table($display_columns->{$label_element}, $db_rows);
156
157 $template->param(error => $error) if ($error ne 0);
158 $template->param(print => 1) if ($label_element eq 'batch');
159 $template->param(
160                 op              => $op,
161                 element_id      => $element_id,
162                 table_loop      => $table,
163                 label_element   => $label_element,
164                 label_element_title     => ($label_element eq 'layout' ? 'Layouts' :
165                                             $label_element eq 'template' ? 'Templates' :
166                                             $label_element eq 'profile' ? 'Profiles' :
167                                             $label_element eq 'batch' ? 'Batches' :
168                                             ''
169                                             ),
170 );
171
172 output_html_with_http_headers $cgi, $cookie, $template->output;