Bugfix: Correcting conditional for branch code check
[koha.git] / patroncards / 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 CGI;
25 use Text::CSV_XS;
26 use XML::Simple;
27 use autouse 'Data::Dumper' => qw(Dumper);
28
29 use C4::Auth qw(get_template_and_user);
30 use C4::Output qw(output_html_with_http_headers);
31 use C4::Creators::Lib 1.000000 qw(get_barcode_types get_label_types get_font_types get_text_justification_types get_unit_values get_all_image_names);
32 use C4::Patroncards::Layout 1.000000;
33
34 my $cgi = new CGI;
35 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36     {
37         template_name   => "patroncards/edit-layout.tmpl",
38         query           => $cgi,
39         type            => "intranet",
40         authnotrequired => 0,
41         flagsrequired   => { catalogue => 1 },
42         debug           => 1,
43     }
44 );
45
46 my $op = $cgi->param('op') || 'new'; # make 'new' the default operation if none is submitted
47 my $layout_id = $cgi->param('layout_id') || $cgi->param('element_id') || '';
48 my $layout_choice = $cgi->param('layout_choice') || '';
49 my $layout = '';
50 my $layout_xml = undef;
51
52 my $units = get_unit_values();
53 my $font_types = get_font_types();
54 my $alignment_types = get_text_justification_types();
55 my $barcode_types = get_barcode_types();
56 my $image_sources = [
57     {type => 'none', name => 'None', selected => 1},
58     {type => 'patronimages', name => 'Patron Image', selected => 0},
59     {type => 'creator_images', name => 'Other Image', selected => 0},
60 ];
61 my $image_names = get_all_image_names();
62 unshift @$image_names, {type => 'none', name => 'Select Image', selected => 1};
63
64 sub _set_selected {
65     my ($selection, $source_list) = @_;
66     my @select_list = ();       # we must make a copy of the referent otherwise we modify the original which causes bad things to happen
67     my $selected = 0;
68     SET_SELECTED:
69     foreach my $type (@$source_list) {
70         if (($selection) && ($type->{'type'} eq $selection)) {  # even if there is no current selection we must still build the select box
71             $selected = 1;
72         }
73         else {
74             $selected = 0;
75         }
76         push @select_list, {type => $type->{'type'}, name => $type->{'name'}, selected => $selected};
77     };
78     return \@select_list;
79 }
80
81 if ($op eq 'edit') {
82     warn sprintf("Error performing '%s': No 'layout_id' passed in.", $op) unless ($layout_id);
83     $layout = C4::Patroncards::Layout->retrieve(layout_id => $layout_id);
84     $layout_xml = XMLin($layout->get_attr('layout_xml'), ForceArray => 1);
85 #       Handle text fields...
86     my $field_number = 0;
87     my @text_fields = ();
88     if ($layout_xml->{'text'}) {
89         while (scalar @{$layout_xml->{'text'}}) {
90             $field_number++;
91             push @text_fields, (
92                                 "field_" . $field_number => 1,      # indicate field as currently "selected" for display in form
93                                 "field_" . $field_number . "_text" => shift @{$layout_xml->{'text'}},
94                                 );
95             my $field_params = shift @{$layout_xml->{'text'}};
96             push @text_fields, (
97                                 "field_" . $field_number . "_llx" => $field_params->{'llx'},
98                                 "field_" . $field_number . "_lly" => $field_params->{'lly'},
99                                 "field_" . $field_number . "_font" => _set_selected($field_params->{'font'}, $font_types),
100                                 "field_" . $field_number . "_font_size" => $field_params->{'font_size'},
101                                 "field_" . $field_number . "_text_alignment" => _set_selected($field_params->{'text_alignment'}, $alignment_types),
102                                 );
103         }
104     }
105
106 #   Handle fields not currently used
107     UNUSED_TEXT_FIELDS:
108     for (my $field = $field_number + 1; $field < 4; $field++) {     # limit 3 text fields
109         push @text_fields, (
110                         "field_$field" . "_font" => get_font_types(),
111                         "field_$field" . "_text_alignment" => get_text_justification_types(),
112                         );
113     }
114
115 #   Handle images...
116     my $image_count = 0;
117     my @images = ();
118     foreach my $image (keys %{$layout_xml->{'images'}}) {
119         $image_count++;
120         push @images, ( $image . "_image" => "$image",
121                         $image . "_Dx" => $layout_xml->{'images'}->{$image}->{'Dx'},
122                         $image . "_Tx" => $layout_xml->{'images'}->{$image}->{'Tx'},
123                         $image . "_Ty" => $layout_xml->{'images'}->{$image}->{'Ty'},
124                         $image . "_image_source" => _set_selected($layout_xml->{'images'}->{$image}->{'data_source'}->[0]->{'image_source'}, $image_sources),
125                         $image . "_image_name" => _set_selected($layout_xml->{'images'}->{$image}->{'data_source'}->[0]->{'image_name'}, $image_names),
126                         );
127     }
128
129 #   Handle image fields not currently used
130     UNUSED_IMAGE_FIELDS:
131     for (my $image = $image_count + 1; $image < 3; $image++) {     #limit 2 images
132         push @images, (
133                         "image_$image" . "_image_source" => $image_sources,
134                         "image_$image" . "_image_name" => $image_names,
135                         );
136     }
137
138 #   Handle barcodes...
139     my @barcode = ();
140     foreach my $barcode_param (keys %{$layout_xml->{'barcode'}->[0]}) {
141         push @barcode, (($barcode_param eq 'type' ? ("barcode_" . $barcode_param => _set_selected($layout_xml->{'barcode'}->[0]->{'barcode_type'}, $barcode_types)) : ("barcode_" . $barcode_param => $layout_xml->{'barcode'}->[0]->{$barcode_param})));
142     }
143
144     $template->param(
145             layout_id       => $layout->get_attr('layout_id') > -1 ? $layout->get_attr('layout_id') : '',
146             layout_name     => $layout->get_attr('layout_name'),
147             page_side       => ($layout_xml->{'page_side'} eq 'F' ? 0 : 1),
148             units           => $units,
149             @barcode,
150             barcode_type    => get_barcode_types(),
151             @text_fields,
152             @images,
153             guidebox        => 0,
154     );
155     output_html_with_http_headers $cgi, $cookie, $template->output;
156     exit;
157 }
158 elsif  ($op eq 'save') {
159     my $format_string = undef;
160     my $layout = {};
161     my $layout_name = undef;
162     my $layout_id = undef;
163     my $text_lines = [];
164     my $array_index = 0;
165     my $image_select = 0;
166     my $field_enabled = 0;
167     CGI_PARAMS:
168     foreach my $parameter ($cgi->param()) {     # parse the field values and build a hash of the layout for conversion to xml and storage in the db
169         if ($parameter =~ m/^field_([0-9])_(.*)$/) {
170             my $field_number = $1;
171             my $field_data = $2;
172             $field_enabled = $field_number if $field_data eq 'enable';
173             next CGI_PARAMS unless $field_number == $field_enabled;
174             if ($field_data eq 'text') {
175                 push @$text_lines, $cgi->param($parameter);
176                 if ($array_index <= 0) {
177                     $array_index++;
178                 }
179                 else {
180                     $array_index += 2; # after hitting 1, increment by 2 so counting odds
181                 }
182             }
183             elsif ($array_index > 0) {
184                 $text_lines->[$array_index]->{$field_data} = $cgi->param($parameter);
185             }
186         }
187         elsif ($parameter =~ m/^barcode_(.*)$/) {
188             $field_enabled = $1 if $1 eq 'print';
189             next CGI_PARAMS unless $field_enabled eq 'print';
190             $layout->{'barcode'}->{$1} = $cgi->param($parameter);
191         }
192         elsif ($parameter =~m/^image_([0-9])_(.*)$/) {
193             my $image_number = $1;
194             my $image_data = $2;
195             $field_enabled = $image_number if $cgi->param("image_$image_number" . "_image_source") ne 'none';
196             next CGI_PARAMS unless $image_number == $field_enabled;
197             if ($image_data =~ m/^image_(.*)$/) {
198                 $layout->{'images'}->{"image_$image_number"}->{'data_source'}->{"image_$1"} = $cgi->param($parameter);
199             }
200             else {
201                 $layout->{'images'}->{"image_$image_number"}->{$image_data} = $cgi->param($parameter);
202             }
203         }
204         else {
205             $layout_name = $cgi->param($parameter) if $parameter eq 'layout_name';
206             $layout_id = $cgi->param($parameter) if $parameter eq 'layout_id';
207             $layout->{'units'} = $cgi->param($parameter) if $parameter eq 'units';
208             $layout->{'page_side'} = $cgi->param($parameter) if $parameter eq 'page_side';
209         }
210     }
211     $layout->{'text'} = $text_lines;
212     my @params = (layout_name => $layout_name, layout_id => $layout_id, layout_xml => XMLout($layout));
213     if ($layout_id) {   # if a label_id was passed in, this is an update to an existing layout
214         $layout = C4::Patroncards::Layout->retrieve(layout_id => $layout_id);
215         $layout->set_attr(@params);
216         $layout_id = $layout->save();
217     }
218     else {      # if no label_id, this is a new layout so insert it
219         $layout = C4::Patroncards::Layout->new(@params);
220         $layout_id = $layout->save();
221     }
222     print $cgi->redirect("manage.pl?card_element=layout" . ($layout_id == -1 ? "&element_id=$layout_id&op=$op&error=101" : ''));
223     exit;
224 }
225 elsif  ($op eq 'new') { # this is a new layout
226     $layout = C4::Patroncards::Layout->new();
227     my @fields = ();
228     for (my $field; $field < 4; $field++) {     # limit 3 text fields
229         push @fields, (
230                         "field_$field" . "_font" => get_font_types(),
231                         "field_$field" . "_text_alignment" => get_text_justification_types(),
232                         );
233     }
234
235     my @images = ();
236     for (my $image; $image < 3; $image++) {     #limit 2 images
237         push @images, (
238                         "image_$image" . "_image_source" => $image_sources,
239                         "image_$image" . "_image_name" => $image_names,
240                         );
241     }
242
243     $template->param(
244                     units               => get_unit_values(),
245                     @fields,
246                     barcode_type        => get_barcode_types(),
247                     @images,
248                     );
249
250 output_html_with_http_headers $cgi, $cookie, $template->output;
251 exit;
252 }
253 else { # trap unsupported operation here
254     warn sprintf("Unsupported operation type submitted: %s", $op);
255     print $cgi->redirect("manage.pl?card_element=layout&element_id=$layout_id&error=201");
256     exit;
257 }
258
259 __END__