+}
+elsif ($op eq 'save') {
+ my $format_string = '';
+ if ($layout_choice eq 'layout_table') { # translate the field table into a format_string
+ my %layout_table;
+ foreach my $cgi_param ($cgi->param()) {
+ if (($cgi_param =~ m/^(.*)_tbl$/) && ($cgi->param($cgi_param))) {
+ my $value = $cgi->param($cgi_param);
+ $layout_table{$1} = $value;
+ }
+ }
+ $format_string = join ', ', sort { $layout_table{$a} <=> $layout_table{$b} } keys %layout_table;
+ $cgi->param('format_string', $format_string);
+ }
+ my @params = (
+ barcode_type => $cgi->param('barcode_type') || 'CODE39',
+ printing_type => $cgi->param('printing_type') || 'BAR',
+ layout_name => $cgi->param('layout_name') || 'DEFAULT',
+ guidebox => ($cgi->param('guidebox') ? 1 : 0),
+ font => $cgi->param('font') || 'TR',
+ font_size => $cgi->param('font_size') || 3,
+ callnum_split => ($cgi->param('callnum_split') ? 1 : 0),
+ text_justify => $cgi->param('text_justify') || 'L',
+ format_string => $cgi->param('format_string') || 'title, author, isbn, issn, itemtype, barcode, itemcallnumber',
+ );
+ if ($layout_id) { # if a label_id was passed in, this is an update to an existing layout
+ $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
+ $layout->set_attr(@params);
+ $layout_id = $layout->save();
+ }
+ else { # if no label_id, this is a new layout so insert it
+ $layout = C4::Labels::Layout->new(@params);
+ $layout_id = $layout->save();
+ }
+ print $cgi->redirect("label-manage.pl?label_element=layout" . ($layout_id == -1 ? "&element_id=$layout_id&op=$op&error=1" : ''));
+ exit;
+}
+else { # if we get here, this is a new layout
+ $layout = C4::Labels::Layout->new();
+}