[12/40] Work on label managment 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 CGI;
27 use HTML::Template::Pro;
28 use Data::Dumper;
29
30 use C4::Auth;
31 use C4::Output;
32 use C4::Context;
33 use C4::Debug;
34 use C4::Labels::Lib 1.000000 qw(get_all_templates get_all_layouts get_all_profiles get_barcode_types get_label_types get_column_names get_table_names SELECT);
35 use C4::Labels::Layout 1.000000;
36 use C4::Labels::Template 1.000000;
37 use C4::Labels::Profile 1.000000;
38
39 my $cgi = new CGI;
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41     {
42         template_name   => "labels/label-manage.tmpl",
43         query           => $cgi,
44         type            => "intranet",
45         authnotrequired => 0,
46         flagsrequired   => { catalogue => 1 },
47         debug           => 1,
48     }
49 );
50
51 my $error = 0;
52 my $db_rows = {};
53 my $column_names = [];
54 my $table_names = [];
55
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 };
76
77 my $label_element = $cgi->param('label_element') || $ARGV[0];
78 my $op = $cgi->param('op') || $ARGV[1] || '';
79 my $element_id = $cgi->param('element_id') || $ARGV[2] || '';
80
81 sub _build_table {
82     my $headers = shift;
83     my $data = shift;
84     my $element = shift;
85     my $table = [];
86     my $fields = [];
87     my @db_columns = ();
88     my ($row_index, $col_index) = (0,0);
89     my $cols = 0;       # number of columns to wrap on
90     my $field_count = 0;
91     POPULATE_HEADER:
92     foreach my $db_column (@$headers) {
93         my @key = keys %$db_column;
94         push (@db_columns, $key[0]);
95         $$fields[$col_index] = {select_field => 0, field_name => ($key[0]), field_label => $db_column->{$key[0]}};
96         $field_count++;
97         $col_index++;
98     }
99     $$table[$row_index] = {header_fields => $fields};
100     $cols = $col_index;
101     $field_count *= scalar(@$data);     # total fields to be displayed in the table
102     $col_index = 0;
103     $row_index++;
104     $fields = [];
105     POPULATE_TABLE:
106     foreach my $db_row (@$data) {
107         my $element_id = 0;
108         POPULATE_ROW:
109         foreach my $db_column (@db_columns) {
110             if ($db_column =~ m/^_((.*)_(.*$))/) {
111                 my $table_name = get_table_names($2);
112                 my $record_set = SELECT($1, @$table_name[0], $2 . "_id = " . $db_row->{$2 . "_id"});
113                 $db_row->{$db_column} = $$record_set[0]{$1};
114             }
115             if (grep {$db_column eq $_} keys %$db_row) {
116                 $element_id = $db_row->{$db_column} if $db_column =~ m/id/;
117                 $$fields[$col_index] = {select_field => 0, field_name => ($db_column . "_tbl"), field_value => $db_row->{$db_column}};
118                 $col_index++;
119             }
120         }
121         $$fields[$col_index] = {select_field => 1, field_name => 'select', field_value => $element_id};
122         $$table[$row_index] = {text_fields => $fields};
123         $col_index = 0;
124         $row_index++;
125         $fields = [];
126     }
127     return $table;
128 }
129
130 if ($op eq 'delete') {
131     $error = C4::Labels::Layout::delete(layout_id => $element_id) if $label_element eq 'layout';
132     $error = C4::Labels::Template::delete(template_id => $element_id) if $label_element eq 'template';
133     $error = C4::Labels::Profile::delete(profile_id => $element_id) if $label_element eq 'profile';
134 }
135
136 $table_names = get_table_names($label_element);
137 $column_names = get_column_names(@$table_names[0]);
138 $db_rows = get_all_layouts() if $label_element eq 'layout';
139 $db_rows = get_all_templates() if $label_element eq 'template';
140 $db_rows = get_all_profiles() if $label_element eq 'profile';
141
142 my $table = _build_table($display_columns->{$label_element}, $db_rows, $label_element);
143
144 $template->param(error => $error) if ($error ne 0);
145 $template->param(
146                 op              => $op,
147                 element_id      => $element_id,
148                 table_loop      => $table,
149                 label_element   => $label_element,
150                 label_element_title     => ($label_element eq 'layout' ? 'Layouts' :
151                                             $label_element eq 'template' ? 'Templates' :
152                                             $label_element eq  'profile' ? 'Profiles' :
153                                             ''
154                                             ),
155 );
156
157 output_html_with_http_headers $cgi, $cookie, $template->output;