[22/40] General code cleanup of new labels module code.
[koha.git] / labels / label-edit-layout.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
24 use Sys::Syslog qw(syslog);
25 use CGI;
26 use HTML::Template::Pro;
27 use POSIX;
28 use Text::CSV_XS;
29
30 use C4::Auth qw(get_template_and_user);
31 use C4::Output qw(output_html_with_http_headers);
32 use C4::Labels::Lib 1.000000 qw(get_barcode_types get_label_types get_font_types get_text_justification_types);
33 use C4::Labels::Layout 1.000000;
34
35 my $cgi = new CGI;
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
37     {
38         template_name   => "labels/label-edit-layout.tmpl",
39         query           => $cgi,
40         type            => "intranet",
41         authnotrequired => 0,
42         flagsrequired   => { catalogue => 1 },
43         debug           => 1,
44     }
45 );
46
47 my $op = $cgi->param('op') || $ARGV[0] || '';
48 my $layout_id = $cgi->param('layout_id') || $cgi->param('element_id') || $ARGV[1] || '';
49 my $layout = '';
50
51 sub _set_selected {
52     my ($type_list, $object, $data_type) = @_;
53     SET_SELECTED:
54     foreach my $type (@$type_list) {
55         if ($layout->get_attr($data_type)) {
56             if ($type->{'type'} eq $layout->get_attr($data_type)) {
57                 $type->{'selected'} = 1;
58             }
59         }
60         else {
61             $type->{'selected'} = 1;
62             last SET_SELECTED;
63         }
64     };
65     return $type_list;
66 }
67
68 sub _select_format_string {     # generate field table based on format_string
69     my $format_string = shift;
70     $format_string =~ s/(?<=,) (?![A-Z][a-z][0-9])//g;  # remove spaces between fields
71     my $table = [];
72     my $fields = [];
73     my ($row_index, $col_index, $field_index) = (0,0,0);
74     my $cols = 5;       # number of columns to wrap on
75     my $csv = Text::CSV_XS->new({ allow_whitespace => 1 });
76     my $status = $csv->parse($format_string);
77     my @text_fields = $csv->fields();
78     syslog("LOG_ERR", "labels/label-edit-layout.pl : Error parsing format_string. Parser returned: %s",$csv->error_input()) if $csv->error_input();
79     my $field_count = $#text_fields + 1;
80     POPULATE_TABLE:
81     foreach my $text_field (@text_fields) {
82         $$fields[$col_index] = {field_empty => 0, field_name => ($text_field . "_tbl"), field_label => $text_field, order => [{num => '', selected => 0}]};
83         for (my $order_i = 1; $order_i <= $field_count; $order_i++) {
84             $$fields[$col_index]{'order'}[$order_i] = {num => $order_i, selected => ($field_index == $order_i-1 ? 1 : 0)};
85         }
86         $col_index++;
87         $field_index++;
88         if ((($col_index > 0) && !($col_index % $cols)) || ($field_index == $field_count)) {    # wrap to new row
89             if (($field_index == $field_count) && ($row_index > 0)) { # in this case fill out row with empty fields
90                 while ($col_index < $cols) {
91                     $$fields[$col_index] = {field_empty => 1, field_name => '', field_label => '', order => [{num => '', selected => 0}]};
92                     $col_index++;
93                 }
94                 $$table[$row_index] = {text_fields => $fields};
95                 last POPULATE_TABLE;
96             }
97             $$table[$row_index] = {text_fields => $fields};
98             $row_index++;
99             $fields = [];
100             $col_index = 0;
101         }
102     }
103     return $table;
104 }
105
106 if ($op eq 'edit') {
107     syslog("LOG_ERR", "labels/label-edit-layout.pl : Error performing '%s': No 'layout_id' passed in.", $op) unless ($layout);
108     $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
109
110 }
111 elsif  ($op eq 'save') {
112     my $format_string = '';
113     if ($cgi->param('layout_choice') eq 'layout_table') {       # translate the field table into a format_string
114         my @layout_table = ();
115         foreach my $cgi_param ($cgi->param()) {
116             if (($cgi_param =~ m/^(.*)_tbl$/) && ($cgi->param($cgi_param))) {
117                 my $value = $cgi->param($cgi_param);
118                 $layout_table[$value - 1] = $1;
119             }
120         }
121         @layout_table = grep {$_} @layout_table;        # this removes numerically 'skipped' fields. ie. user omits a number in sequential order
122         $format_string = join ', ', @layout_table;
123         $cgi->param('format_string', $format_string);
124     }
125     my @params = (
126                     barcode_type    => $cgi->param('barcode_type'),
127                     printing_type   => $cgi->param('printing_type'),
128                     layout_name     => $cgi->param('layout_name'),
129                     guidebox        => ($cgi->param('guidebox') ? 1 : 0),
130                     font            => $cgi->param('font'),
131                     font_size       => $cgi->param('font_size'),
132                     callnum_split   => ($cgi->param('callnum_split') ? 1 : 0),
133                     text_justify    => $cgi->param('text_justify'),
134                     format_string   => $cgi->param('format_string'),
135     );
136     if ($layout_id) {   # if a label_id was passed in, this is an update to an existing layout
137         $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
138         $layout->set_attr(@params);
139         $layout->save();
140     }
141     else {      # if no label_id, this is a new layout so insert it
142         $layout = C4::Labels::Layout->new(@params);
143         $layout->save();
144     }
145     print $cgi->redirect("label-manage.pl?label_element=layout");
146     exit;
147 }
148 else {  # if we get here, this is a new layout
149     $layout = C4::Labels::Layout->new();
150 }
151
152 my $barcode_types = _set_selected(get_barcode_types(), $layout, 'barcode_type');
153 my $label_types = _set_selected(get_label_types(), $layout, 'printing_type');
154 my $font_types = _set_selected(get_font_types(), $layout, 'font');
155 my $text_justification_types = _set_selected(get_text_justification_types(), $layout, 'text_justify');
156 my $select_text_fields = _select_format_string($layout->get_attr('format_string'));
157
158 $template->param(
159         barcode_types   => $barcode_types,
160         label_types     => $label_types,
161         font_types      => $font_types,
162         text_justification_types    => $text_justification_types,
163         field_table     => $select_text_fields,
164         layout_id       => $layout->get_attr('layout_id') > -1 ? $layout->get_attr('layout_id') : '',
165         layout_name     => $layout->get_attr('layout_name'),
166         guidebox        => $layout->get_attr('guidebox'),
167         font_size       => $layout->get_attr('font_size'),
168         callnum_split   => $layout->get_attr('callnum_split'),
169         format_string   => $layout->get_attr('format_string'),
170 );
171 output_html_with_http_headers $cgi, $cookie, $template->output;