Bug 11491: add option to supply field names in reports web service output
[koha.git] / labels / label-edit-layout.pl
blobe814e3b795023d409760c0f5d8310716735447cc
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;
27 use C4::Auth qw(get_template_and_user);
28 use C4::Output qw(output_html_with_http_headers);
29 use C4::Creators;
30 use C4::Labels;
32 my $cgi = new CGI;
33 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
35 template_name => "labels/label-edit-layout.tmpl",
36 query => $cgi,
37 type => "intranet",
38 authnotrequired => 0,
39 flagsrequired => { catalogue => 1 },
40 debug => 1,
44 my $op = $cgi->param('op') || '';
45 my $layout_id = $cgi->param('layout_id') || $cgi->param('element_id') || '';
46 my $layout_choice = $cgi->param('layout_choice') || '';
47 our $layout = '';
49 sub _set_selected {
50 my ($type_list, $object, $data_type) = @_;
51 SET_SELECTED:
52 foreach my $type (@$type_list) {
53 if ($layout->get_attr($data_type)) {
54 if ($type->{'type'} eq $layout->get_attr($data_type)) {
55 $type->{'selected'} = 1;
58 else {
59 $type->{'selected'} = 1;
60 last SET_SELECTED;
63 return $type_list;
66 sub _select_format_string { # generate field table based on format_string
67 my $format_string = shift;
69 my @text_fields = grep /\w/, split /\s*,\s/, $format_string;
70 my %tf = map {$_ => 1} @text_fields;
71 my @missing_fields = grep { !$tf{$_} } @{ C4::Labels::Layout->PRESET_FIELDS };
73 my $field_count = scalar(@text_fields) + scalar( @missing_fields);
75 my @fields;
76 my $field_index = 1;
77 foreach my $f (@text_fields) {
78 push @fields, {field_name => ($f . "_tbl"), field_label => $f, order => $field_index};
79 $field_index++;
81 foreach my $f (@missing_fields) {
82 push @fields, {field_name => ($f . "_tbl"), field_label => $f};
84 return (\@fields, $field_count);
87 if ($op eq 'edit') {
88 warn sprintf("Error performing '%s': No 'layout_id' passed in.", $op) unless ($layout_id);
89 $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
92 elsif ($op eq 'save') {
93 my $format_string = '';
94 if ($layout_choice eq 'layout_table') { # translate the field table into a format_string
95 my %layout_table;
96 foreach my $cgi_param ($cgi->param()) {
97 if (($cgi_param =~ m/^(.*)_tbl$/) && ($cgi->param($cgi_param))) {
98 my $value = $cgi->param($cgi_param);
99 $layout_table{$1} = $value;
102 $format_string = join ', ', sort { $layout_table{$a} <=> $layout_table{$b} } keys %layout_table;
103 $cgi->param('format_string', $format_string);
105 my @params = (
106 barcode_type => $cgi->param('barcode_type') || 'CODE39',
107 printing_type => $cgi->param('printing_type') || 'BAR',
108 layout_name => $cgi->param('layout_name') || 'DEFAULT',
109 guidebox => ($cgi->param('guidebox') ? 1 : 0),
110 font => $cgi->param('font') || 'TR',
111 font_size => $cgi->param('font_size') || 3,
112 callnum_split => ($cgi->param('callnum_split') ? 1 : 0),
113 text_justify => $cgi->param('text_justify') || 'L',
114 format_string => $cgi->param('format_string') || 'title, author, isbn, issn, itemtype, barcode, itemcallnumber',
116 if ($layout_id) { # if a label_id was passed in, this is an update to an existing layout
117 $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
118 $layout->set_attr(@params);
119 $layout_id = $layout->save();
121 else { # if no label_id, this is a new layout so insert it
122 $layout = C4::Labels::Layout->new(@params);
123 $layout_id = $layout->save();
125 print $cgi->redirect("label-manage.pl?label_element=layout" . ($layout_id == -1 ? "&element_id=$layout_id&op=$op&error=1" : ''));
126 exit;
128 else { # if we get here, this is a new layout
129 $layout = C4::Labels::Layout->new();
132 my $barcode_types = _set_selected(get_barcode_types(), $layout, 'barcode_type');
133 my $label_types = _set_selected(get_label_types(), $layout, 'printing_type');
134 my $font_types = _set_selected(get_font_types(), $layout, 'font');
135 my $text_justification_types = _set_selected(get_text_justification_types(), $layout, 'text_justify');
136 my ($select_text_fields, $select_text_fields_cnt) = _select_format_string($layout->get_attr('format_string'));
138 $template->param(
139 barcode_types => $barcode_types,
140 label_types => $label_types,
141 font_types => $font_types,
142 text_justification_types => $text_justification_types,
143 fields => $select_text_fields,
144 field_count => $select_text_fields_cnt,
145 layout_id => $layout->get_attr('layout_id') > -1 ? $layout->get_attr('layout_id') : '',
146 layout_name => $layout->get_attr('layout_name'),
147 guidebox => $layout->get_attr('guidebox'),
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;