Fix for bug 4928, problematic 78x display in OPAC
[koha.git] / labels / label-edit-layout.pl
bloba1879af19061e0be7cecce26fa4361e9a5d8bdd9
1 #!/usr/bin/perl
3 # Copyright 2006 Katipo Communications.
4 # Parts Copyright 2009 Foundations Bible College.
6 # This file is part of Koha.
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.
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.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 use strict;
22 use warnings;
24 use CGI;
25 use POSIX;
26 use Text::CSV_XS;
28 use C4::Auth qw(get_template_and_user);
29 use C4::Output qw(output_html_with_http_headers);
30 use C4::Creators 1.000000;
31 use C4::Labels 1.000000;
33 my $cgi = new CGI;
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36 template_name => "labels/label-edit-layout.tmpl",
37 query => $cgi,
38 type => "intranet",
39 authnotrequired => 0,
40 flagsrequired => { catalogue => 1 },
41 debug => 1,
45 my $op = $cgi->param('op') || $ARGV[0] || '';
46 my $layout_id = $cgi->param('layout_id') || $cgi->param('element_id') || $ARGV[1] || '';
47 my $layout_choice = $cgi->param('layout_choice') || '';
48 my $layout = '';
50 sub _set_selected {
51 my ($type_list, $object, $data_type) = @_;
52 SET_SELECTED:
53 foreach my $type (@$type_list) {
54 if ($layout->get_attr($data_type)) {
55 if ($type->{'type'} eq $layout->get_attr($data_type)) {
56 $type->{'selected'} = 1;
59 else {
60 $type->{'selected'} = 1;
61 last SET_SELECTED;
64 return $type_list;
67 sub _select_format_string { # generate field table based on format_string
68 my $format_string = shift;
69 $format_string =~ s/(?<=,) (?![A-Z][a-z][0-9])//g; # remove spaces between fields
70 my $table = [];
71 my $fields = [];
72 my ($row_index, $col_index, $field_index) = (0,0,0);
73 my $cols = 5; # number of columns to wrap on
74 my $csv = Text::CSV_XS->new({ allow_whitespace => 1 });
75 my $status = $csv->parse($format_string);
76 my @text_fields = $csv->fields();
77 warn sprintf('Error parsing format_string. Parser returned: %s', $csv->error_input()) if $csv->error_input();
78 my $field_count = $#text_fields + 1;
79 POPULATE_TABLE:
80 foreach my $text_field (@text_fields) {
81 $$fields[$col_index] = {field_empty => 0, field_name => ($text_field . "_tbl"), field_label => $text_field, order => [{num => '', selected => 0}]};
82 for (my $order_i = 1; $order_i <= $field_count; $order_i++) {
83 $$fields[$col_index]{'order'}[$order_i] = {num => $order_i, selected => ($field_index == $order_i-1 ? 1 : 0)};
85 $col_index++;
86 $field_index++;
87 if ((($col_index > 0) && !($col_index % $cols)) || ($field_index == $field_count)) { # wrap to new row
88 if (($field_index == $field_count) && ($row_index > 0)) { # in this case fill out row with empty fields
89 while ($col_index < $cols) {
90 $$fields[$col_index] = {field_empty => 1, field_name => '', field_label => '', order => [{num => '', selected => 0}]};
91 $col_index++;
93 $$table[$row_index] = {text_fields => $fields};
94 last POPULATE_TABLE;
96 $$table[$row_index] = {text_fields => $fields};
97 $row_index++;
98 $fields = [];
99 $col_index = 0;
102 return $table;
105 if ($op eq 'edit') {
106 warn sprintf("Error performing '%s': No 'layout_id' passed in.", $op) unless ($layout_id);
107 $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
110 elsif ($op eq 'save') {
111 my $format_string = '';
112 if ($layout_choice eq 'layout_table') { # translate the field table into a format_string
113 my @layout_table = ();
114 foreach my $cgi_param ($cgi->param()) {
115 if (($cgi_param =~ m/^(.*)_tbl$/) && ($cgi->param($cgi_param))) {
116 my $value = $cgi->param($cgi_param);
117 $layout_table[$value - 1] = $1;
120 @layout_table = grep {$_} @layout_table; # this removes numerically 'skipped' fields. ie. user omits a number in sequential order
121 $format_string = join ', ', @layout_table;
122 $cgi->param('format_string', $format_string);
124 my @params = (
125 barcode_type => $cgi->param('barcode_type'),
126 printing_type => $cgi->param('printing_type'),
127 layout_name => $cgi->param('layout_name'),
128 guidebox => ($cgi->param('guidebox') ? 1 : 0),
129 font => $cgi->param('font'),
130 font_size => $cgi->param('font_size'),
131 callnum_split => ($cgi->param('callnum_split') ? 1 : 0),
132 text_justify => $cgi->param('text_justify'),
133 format_string => $cgi->param('format_string'),
135 if ($layout_id) { # if a label_id was passed in, this is an update to an existing layout
136 $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
137 $layout->set_attr(@params);
138 $layout_id = $layout->save();
140 else { # if no label_id, this is a new layout so insert it
141 $layout = C4::Labels::Layout->new(@params);
142 $layout_id = $layout->save();
144 print $cgi->redirect("label-manage.pl?label_element=layout" . ($layout_id == -1 ? "&element_id=$layout_id&op=$op&error=1" : ''));
145 exit;
147 else { # if we get here, this is a new layout
148 $layout = C4::Labels::Layout->new();
151 my $barcode_types = _set_selected(get_barcode_types(), $layout, 'barcode_type');
152 my $label_types = _set_selected(get_label_types(), $layout, 'printing_type');
153 my $font_types = _set_selected(get_font_types(), $layout, 'font');
154 my $text_justification_types = _set_selected(get_text_justification_types(), $layout, 'text_justify');
155 my $select_text_fields = _select_format_string($layout->get_attr('format_string'));
157 $template->param(
158 barcode_types => $barcode_types,
159 label_types => $label_types,
160 font_types => $font_types,
161 text_justification_types => $text_justification_types,
162 field_table => $select_text_fields,
163 layout_id => $layout->get_attr('layout_id') > -1 ? $layout->get_attr('layout_id') : '',
164 layout_name => $layout->get_attr('layout_name'),
165 guidebox => $layout->get_attr('guidebox'),
166 font_size => $layout->get_attr('font_size'),
167 callnum_split => $layout->get_attr('callnum_split'),
168 format_string => $layout->get_attr('format_string'),
169 layout_string => 1, # FIXME: This should not be hard-coded; It should perhaps be yet another syspref... CN
171 output_html_with_http_headers $cgi, $cookie, $template->output;