Bug 22343: (follow-up) Add a hint to the debug form option
[koha.git] / labels / label-edit-layout.pl
blobac907f3beac1101d223a1cd121cf895fb1633e62
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
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.
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.
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>.
21 use Modern::Perl;
23 use CGI qw ( -utf8 );
24 use POSIX;
26 use C4::Auth qw(get_template_and_user);
27 use C4::Output qw(output_html_with_http_headers);
28 use C4::Creators;
29 use C4::Labels;
31 my $cgi = new CGI;
32 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
34 template_name => "labels/label-edit-layout.tt",
35 query => $cgi,
36 type => "intranet",
37 flagsrequired => { catalogue => 1 },
38 debug => 1,
42 my $op = $cgi->param('op') || '';
43 my $layout_id = $cgi->param('layout_id') || $cgi->param('element_id') || '';
44 my $layout_choice = $cgi->param('layout_choice') || '';
45 our $layout = '';
47 sub _set_selected {
48 my ($type_list, $object, $data_type) = @_;
49 SET_SELECTED:
50 foreach my $type (@$type_list) {
51 if ($layout->get_attr($data_type)) {
52 if ($type->{'type'} eq $layout->get_attr($data_type)) {
53 $type->{'selected'} = 1;
56 else {
57 $type->{'selected'} = 1;
58 last SET_SELECTED;
61 return $type_list;
64 sub _select_format_string { # generate field table based on format_string
65 my $format_string = shift;
67 my @text_fields = grep /\w/, split /\s*,\s/, $format_string;
68 my %tf = map {$_ => 1} @text_fields;
69 my @missing_fields = grep { !$tf{$_} } @{ C4::Labels::Layout->PRESET_FIELDS };
71 my $field_count = scalar(@text_fields) + scalar( @missing_fields);
73 my @fields;
74 my $field_index = 1;
75 foreach my $f (@text_fields) {
76 push @fields, {field_name => ($f . "_tbl"), field_label => $f, order => $field_index};
77 $field_index++;
79 foreach my $f (@missing_fields) {
80 push @fields, {field_name => ($f . "_tbl"), field_label => $f};
82 return (\@fields, $field_count);
85 if ($op eq 'edit') {
86 warn sprintf("Error performing '%s': No 'layout_id' passed in.", $op) unless ($layout_id);
87 $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
90 elsif ($op eq 'save') {
91 my $format_string = '';
92 if ($layout_choice eq 'layout_table') { # translate the field table into a format_string
93 my %layout_table;
94 foreach my $cgi_param ($cgi->param()) {
95 if (($cgi_param =~ m/^(.*)_tbl$/) && ($cgi->param($cgi_param))) {
96 my $value = $cgi->param($cgi_param);
97 $layout_table{$1} = $value;
100 $format_string = join ', ', sort { $layout_table{$a} <=> $layout_table{$b} } keys %layout_table;
101 $cgi->param('format_string', $format_string);
103 my @params = (
104 barcode_type => scalar $cgi->param('barcode_type') || 'CODE39',
105 printing_type => scalar $cgi->param('printing_type') || 'BAR',
106 layout_name => scalar $cgi->param('layout_name') || 'DEFAULT',
107 guidebox => ($cgi->param('guidebox') ? 1 : 0),
108 oblique_title => ($cgi->param('oblique_title') ? 1 : 0),
109 font => scalar $cgi->param('font') || 'TR',
110 font_size => scalar $cgi->param('font_size') || 3,
111 callnum_split => ($cgi->param('callnum_split') ? 1 : 0),
112 text_justify => scalar $cgi->param('text_justify') || 'L',
113 format_string => scalar $cgi->param('format_string') || 'title, author, isbn, issn, itemtype, barcode, itemcallnumber',
115 if ($layout_id) { # if a label_id was passed in, this is an update to an existing layout
116 $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
117 $layout->set_attr(@params);
118 $layout_id = $layout->save();
120 else { # if no label_id, this is a new layout so insert it
121 $layout = C4::Labels::Layout->new(@params);
122 $layout_id = $layout->save();
124 print $cgi->redirect("label-manage.pl?label_element=layout" . ($layout_id == -1 ? "&element_id=$layout_id&op=$op&error=1" : ''));
125 exit;
127 else { # if we get here, this is a new layout
128 $layout = C4::Labels::Layout->new();
131 my $barcode_types = _set_selected(get_barcode_types(), $layout, 'barcode_type');
132 my $label_types = _set_selected(get_label_types(), $layout, 'printing_type');
133 my $font_types = _set_selected(get_font_types(), $layout, 'font');
134 my $text_justification_types = _set_selected(get_text_justification_types(), $layout, 'text_justify');
135 my ($select_text_fields, $select_text_fields_cnt) = _select_format_string($layout->get_attr('format_string'));
137 $template->param(
138 barcode_types => $barcode_types,
139 label_types => $label_types,
140 font_types => $font_types,
141 text_justification_types => $text_justification_types,
142 fields => $select_text_fields,
143 field_count => $select_text_fields_cnt,
144 layout_id => $layout->get_attr('layout_id') > -1 ? $layout->get_attr('layout_id') : '',
145 layout_name => $layout->get_attr('layout_name'),
146 guidebox => $layout->get_attr('guidebox'),
147 oblique_title => $layout->get_attr('oblique_title'),
148 font_size => $layout->get_attr('font_size'),
149 callnum_split => $layout->get_attr('callnum_split'),
150 format_string => $layout->get_attr('format_string'),
151 layout_string => 1, # FIXME: This should not be hard-coded; It should perhaps be yet another syspref... CN
153 output_html_with_http_headers $cgi, $cookie, $template->output;