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