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