Bug 1172: Added OPACPatronDetails system preference
[koha.git] / labels / label-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 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.
12 #
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.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use warnings;
23
24 use CGI;
25 use POSIX;
26 use Text::CSV_XS;
27
28 use C4::Auth qw(get_template_and_user);
29 use C4::Output qw(output_html_with_http_headers);
30 use C4::Labels::Lib 1.000000 qw(get_barcode_types get_label_types get_font_types get_text_justification_types);
31 use C4::Labels::Layout 1.000000;
32
33 my $cgi = new CGI;
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
35     {
36         template_name   => "labels/label-edit-layout.tmpl",
37         query           => $cgi,
38         type            => "intranet",
39         authnotrequired => 0,
40         flagsrequired   => { catalogue => 1 },
41         debug           => 1,
42     }
43 );
44
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 = '';
48
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;
56             }
57         }
58         else {
59             $type->{'selected'} = 1;
60             last SET_SELECTED;
61         }
62     };
63     return $type_list;
64 }
65
66 sub _select_format_string {     # generate field table based on format_string
67     my $format_string = shift;
68     $format_string =~ s/(?<=,) (?![A-Z][a-z][0-9])//g;  # remove spaces between fields
69     my $table = [];
70     my $fields = [];
71     my ($row_index, $col_index, $field_index) = (0,0,0);
72     my $cols = 5;       # number of columns to wrap on
73     my $csv = Text::CSV_XS->new({ allow_whitespace => 1 });
74     my $status = $csv->parse($format_string);
75     my @text_fields = $csv->fields();
76     warn sprintf('Error parsing format_string. Parser returned: %s', $csv->error_input()) if $csv->error_input();
77     my $field_count = $#text_fields + 1;
78     POPULATE_TABLE:
79     foreach my $text_field (@text_fields) {
80         $$fields[$col_index] = {field_empty => 0, field_name => ($text_field . "_tbl"), field_label => $text_field, order => [{num => '', selected => 0}]};
81         for (my $order_i = 1; $order_i <= $field_count; $order_i++) {
82             $$fields[$col_index]{'order'}[$order_i] = {num => $order_i, selected => ($field_index == $order_i-1 ? 1 : 0)};
83         }
84         $col_index++;
85         $field_index++;
86         if ((($col_index > 0) && !($col_index % $cols)) || ($field_index == $field_count)) {    # wrap to new row
87             if (($field_index == $field_count) && ($row_index > 0)) { # in this case fill out row with empty fields
88                 while ($col_index < $cols) {
89                     $$fields[$col_index] = {field_empty => 1, field_name => '', field_label => '', order => [{num => '', selected => 0}]};
90                     $col_index++;
91                 }
92                 $$table[$row_index] = {text_fields => $fields};
93                 last POPULATE_TABLE;
94             }
95             $$table[$row_index] = {text_fields => $fields};
96             $row_index++;
97             $fields = [];
98             $col_index = 0;
99         }
100     }
101     return $table;
102 }
103
104 if ($op eq 'edit') {
105     warn sprintf("Error performing '%s': No 'layout_id' passed in.", $op) unless ($layout_id);
106     $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
107
108 }
109 elsif  ($op eq 'save') {
110     my $format_string = '';
111     if ($cgi->param('layout_choice') eq 'layout_table') {       # translate the field table into a format_string
112         my @layout_table = ();
113         foreach my $cgi_param ($cgi->param()) {
114             if (($cgi_param =~ m/^(.*)_tbl$/) && ($cgi->param($cgi_param))) {
115                 my $value = $cgi->param($cgi_param);
116                 $layout_table[$value - 1] = $1;
117             }
118         }
119         @layout_table = grep {$_} @layout_table;        # this removes numerically 'skipped' fields. ie. user omits a number in sequential order
120         $format_string = join ', ', @layout_table;
121         $cgi->param('format_string', $format_string);
122     }
123     my @params = (
124                     barcode_type    => $cgi->param('barcode_type'),
125                     printing_type   => $cgi->param('printing_type'),
126                     layout_name     => $cgi->param('layout_name'),
127                     guidebox        => ($cgi->param('guidebox') ? 1 : 0),
128                     font            => $cgi->param('font'),
129                     font_size       => $cgi->param('font_size'),
130                     callnum_split   => ($cgi->param('callnum_split') ? 1 : 0),
131                     text_justify    => $cgi->param('text_justify'),
132                     format_string   => $cgi->param('format_string'),
133     );
134     if ($layout_id) {   # if a label_id was passed in, this is an update to an existing layout
135         $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
136         $layout->set_attr(@params);
137         $layout->save();
138     }
139     else {      # if no label_id, this is a new layout so insert it
140         $layout = C4::Labels::Layout->new(@params);
141         $layout->save();
142     }
143     print $cgi->redirect("label-manage.pl?label_element=layout");
144     exit;
145 }
146 else {  # if we get here, this is a new layout
147     $layout = C4::Labels::Layout->new();
148 }
149
150 my $barcode_types = _set_selected(get_barcode_types(), $layout, 'barcode_type');
151 my $label_types = _set_selected(get_label_types(), $layout, 'printing_type');
152 my $font_types = _set_selected(get_font_types(), $layout, 'font');
153 my $text_justification_types = _set_selected(get_text_justification_types(), $layout, 'text_justify');
154 my $select_text_fields = _select_format_string($layout->get_attr('format_string'));
155
156 $template->param(
157         barcode_types   => $barcode_types,
158         label_types     => $label_types,
159         font_types      => $font_types,
160         text_justification_types    => $text_justification_types,
161         field_table     => $select_text_fields,
162         layout_id       => $layout->get_attr('layout_id') > -1 ? $layout->get_attr('layout_id') : '',
163         layout_name     => $layout->get_attr('layout_name'),
164         guidebox        => $layout->get_attr('guidebox'),
165         font_size       => $layout->get_attr('font_size'),
166         callnum_split   => $layout->get_attr('callnum_split'),
167         format_string   => $layout->get_attr('format_string'),
168 );
169 output_html_with_http_headers $cgi, $cookie, $template->output;