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