From e12ae1892c9aad0ed0515bcbe9ac9f336812c792 Mon Sep 17 00:00:00 2001 From: Chris Nighswonger Date: Tue, 1 Sep 2009 12:17:10 -0400 Subject: [PATCH] [9/40] Work on template interface. --- C4/Labels/Template.pm | 112 ++++---- .../prog/en/css/staff-global.css | 2 +- .../prog/en/includes/labels-new-toolbar.inc | 23 ++ .../modules/labels/label-edit-template.tmpl | 256 +++++++++--------- labels/label-edit-template.pl | 196 +++++++------- 5 files changed, 318 insertions(+), 271 deletions(-) create mode 100644 koha-tmpl/intranet-tmpl/prog/en/includes/labels-new-toolbar.inc diff --git a/C4/Labels/Template.pm b/C4/Labels/Template.pm index bf14aa50b6..f972ce6f64 100644 --- a/C4/Labels/Template.pm +++ b/C4/Labels/Template.pm @@ -27,24 +27,19 @@ use C4::Context; use C4::Debug; use C4::Labels::Profile 1.000000; use C4::Labels::PDF 1.000000; +use C4::Labels::Lib 1.000000 qw(get_unit_values); BEGIN { use version; our $VERSION = qv('1.0.0_1'); } -my $unit_values = { - POINT => 1, - INCH => 72, - MM => 2.83464567, - CM => 28.3464567, -}; - sub _check_params { my $given_params = {}; my $exit_code = 0; my @valid_template_params = ( - 'tmpl_code', - 'tmpl_desc', + 'profile_id', + 'template_code', + 'template_desc', 'page_width', 'page_height', 'label_width', @@ -58,8 +53,6 @@ sub _check_params { 'col_gap', 'row_gap', 'units', - 'font_size', - 'font', ); if (scalar(@_) >1) { $given_params = {@_}; @@ -81,16 +74,17 @@ sub _check_params { sub _conv_points { my $self = shift; - $self->{page_width} = $self->{page_width} * $unit_values->{$self->{units}}; - $self->{page_height} = $self->{page_height} * $unit_values->{$self->{units}}; - $self->{label_width} = $self->{label_width} * $unit_values->{$self->{units}}; - $self->{label_height} = $self->{label_height} * $unit_values->{$self->{units}}; - $self->{top_text_margin} = $self->{top_text_margin} * $unit_values->{$self->{units}}; - $self->{left_text_margin} = $self->{left_text_margin} * $unit_values->{$self->{units}}; - $self->{top_margin} = $self->{top_margin} * $unit_values->{$self->{units}}; - $self->{left_margin} = $self->{left_margin} * $unit_values->{$self->{units}}; - $self->{col_gap} = $self->{col_gap} * $unit_values->{$self->{units}}; - $self->{row_gap} = $self->{row_gap} * $unit_values->{$self->{units}}; + my @unit_value = grep {$_->{'type'} eq $self->{units}} get_unit_values(); + $self->{page_width} = $self->{page_width} * $unit_value[0]->{'value'}; + $self->{page_height} = $self->{page_height} * $unit_value[0]->{'value'}; + $self->{label_width} = $self->{label_width} * $unit_value[0]->{'value'}; + $self->{label_height} = $self->{label_height} * $unit_value[0]->{'value'}; + $self->{top_text_margin} = $self->{top_text_margin} * $unit_value[0]->{'value'}; + $self->{left_text_margin} = $self->{left_text_margin} * $unit_value[0]->{'value'}; + $self->{top_margin} = $self->{top_margin} * $unit_value[0]->{'value'}; + $self->{left_margin} = $self->{left_margin} * $unit_value[0]->{'value'}; + $self->{col_gap} = $self->{col_gap} * $unit_value[0]->{'value'}; + $self->{row_gap} = $self->{row_gap} * $unit_value[0]->{'value'}; return $self; } @@ -127,12 +121,13 @@ C4::Labels::Template - A class for creating and manipulating template objects in sub new { my $invocant = shift; if (_check_params(@_) eq 1) { - return 1; + return -1; } my $type = ref($invocant) || $invocant; my $self = { - tmpl_code => '', - tmpl_desc => '', + profile_id => '0', + template_code => 'DEFAULT TEMPLATE', + template_desc => 'Default description', page_width => 0, page_height => 0, label_width => 0, @@ -146,8 +141,6 @@ sub new { col_gap => 0, row_gap => 0, units => 'POINT', - font_size => 3, - font => 'TR', tmpl_stat => 0, # false if any data has changed and the db has not been updated @_, }; @@ -177,12 +170,12 @@ sub retrieve { my $invocant = shift; my %opts = @_; my $type = ref($invocant) || $invocant; - my $query = "SELECT * FROM labels_templates WHERE tmpl_id = ?"; + my $query = "SELECT * FROM labels_templates WHERE template_id = ?"; my $sth = C4::Context->dbh->prepare($query); $sth->execute($opts{template_id}); if ($sth->err) { syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr); - return 1; + return -1; } my $self = $sth->fetchrow_hashref; $self = _conv_points($self) if (($opts{convert} && $opts{convert} == 1) || $opts{profile_id}); @@ -192,26 +185,39 @@ sub retrieve { return $self; } -=head2 C4::Labels::Template->delete(tmpl_id => template_id) | $template->delete() +=head2 C4::Labels::Template::delete(template_id => template_id) | $template->delete() Invoking the delete method attempts to delete the template from the database. The method returns 0 upon success and 1 upon failure. Errors are logged to the syslog. examples: my $exitstat = $template->delete(); # to delete the record behind the $template object - my $exitstat = C4::Labels::Template->delete(tmpl_id => 1); # to delete template record 1 + my $exitstat = C4::Labels::Template::delete(template_id => 1); # to delete template record 1 =cut sub delete { - my $self = shift; - if (!$self->{tmpl_id}) { # If there is no template tmpl_id then we cannot delete it - syslog("LOG_ERR", "Cannot delete template as it has not been saved."); - return 1; + my $self = {}; + my %opts = (); + my $call_type = ''; + my $query_param = ''; + if (ref($_[0])) { + $self = shift; # check to see if this is a method call + $call_type = 'C4::Labels::Template->delete'; + $query_param = $self->{'template_id'}; + } + else { + %opts = @_; + $call_type = 'C4::Labels::Template::delete'; + $query_param = $opts{'template_id'}; + } + if ($query_param eq '') { # If there is no template id then we cannot delete it + syslog("LOG_ERR", "%s : Cannot delete layout as the template id is invalid or non-existant.", $call_type); + return -1; } - my $query = "DELETE FROM labels_templates WHERE tmpl_id = ?"; + my $query = "DELETE FROM labels_templates WHERE template_id = ?"; my $sth = C4::Context->dbh->prepare($query); - $sth->execute($self->{tmpl_id}); + $sth->execute($query_param); $self->{tmpl_stat} = 0; return 0; } @@ -219,8 +225,8 @@ sub delete { =head2 $template->save() Invoking the I method attempts to insert the template into the database if the template is new and - update the existing template record if the template exists. The method returns the new record tmpl_id upon - success and -1 upon failure (This avotmpl_ids conflicting with a record tmpl_id of 1). Errors are logged to the syslog. + update the existing template record if the template exists. The method returns the new record template_id upon + success and -1 upon failure (This avotemplate_ids conflicting with a record template_id of 1). Errors are logged to the syslog. example: my $exitstat = $template->save(); # to save the record behind the $template object @@ -229,17 +235,17 @@ sub delete { sub save { my $self = shift; - if ($self->{'tmpl_id'}) { # if we have an tmpl_id, the record exists and needs UPDATE + if ($self->{'template_id'}) { # if we have an template_id, the record exists and needs UPDATE my @params; my $query = "UPDATE labels_templates SET "; foreach my $key (keys %{$self}) { - next if ($key eq 'tmpl_id') || ($key eq 'tmpl_stat'); + next if ($key eq 'template_id') || ($key eq 'tmpl_stat'); push (@params, $self->{$key}); $query .= "$key=?, "; } $query = substr($query, 0, (length($query)-2)); - push (@params, $self->{'tmpl_id'}); - $query .= " WHERE tmpl_id=?;"; + push (@params, $self->{'template_id'}); + $query .= " WHERE template_id=?;"; warn "DEBUG: Updating: $query\n" if $debug; my $sth = C4::Context->dbh->prepare($query); $sth->execute(@params); @@ -248,7 +254,7 @@ sub save { return -1; } $self->{tmpl_stat} = 1; - return $self->{'tmpl_id'}; + return $self->{'template_id'}; } else { # otherwise create a new record my @params; @@ -272,12 +278,12 @@ sub save { syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr); return -1; } - my $sth1 = C4::Context->dbh->prepare("SELECT MAX(tmpl_id) FROM labels_templates;"); + my $sth1 = C4::Context->dbh->prepare("SELECT MAX(template_id) FROM labels_templates;"); $sth1->execute(); - my $tmpl_id = $sth1->fetchrow_array; - $self->{tmpl_id} = $tmpl_id; + my $template_id = $sth1->fetchrow_array; + $self->{template_id} = $template_id; $self->{tmpl_stat} = 1; - return $tmpl_id; + return $template_id; } } @@ -293,14 +299,14 @@ sub save { sub get_attr { my $self = shift; if (_check_params(@_) eq 1) { - return 1; + return -1; } my ($attr) = @_; if (exists($self->{$attr})) { return $self->{$attr}; } else { - return 1; + return -1; } } @@ -316,10 +322,12 @@ sub get_attr { sub set_attr { my $self = shift; if (_check_params(@_) eq 1) { - return 1; + return -1; } - my ($attr, $value) = @_; - $self->{$attr} = $value; + my %attrs = @_; + foreach my $attrib (keys(%attrs)) { + $self->{$attrib} = $attrs{$attrib}; + }; } =head2 $template->get_text_wrap_cols() diff --git a/koha-tmpl/intranet-tmpl/prog/en/css/staff-global.css b/koha-tmpl/intranet-tmpl/prog/en/css/staff-global.css index 41ba3e90f3..508fdc3ec3 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/css/staff-global.css +++ b/koha-tmpl/intranet-tmpl/prog/en/css/staff-global.css @@ -777,7 +777,7 @@ fieldset.rows .inputnote { visibility:visible; /* you propably don't need to change this one */ display:block; } -#newbiblio a, #addchild a, #newentry a, #newshelf a, #newmenuc .first-child, #newsupplier .first-child, #newlabel a, #newtemplate a, #newlabelbatch a, #newpatroncardbatch a, #newprofile a, #newsubscription a, #newdictionary a, #neworder a { +#newbiblio a, #addchild a, #newentry a, #newshelf a, #newmenuc .first-child, #newsupplier .first-child, #newlabel a, #newtemplate a, #newbatch a, #newprofile a, #newsubscription a, #newdictionary a, #neworder a { padding-left : 34px; background-image: url("../../img/toolbar-new.gif"); background-position : center left; diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/labels-new-toolbar.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/labels-new-toolbar.inc new file mode 100644 index 0000000000..35553df11b --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/labels-new-toolbar.inc @@ -0,0 +1,23 @@ + + diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/labels/label-edit-template.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/labels/label-edit-template.tmpl index 1d4be5a053..aa2ff6132c 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/labels/label-edit-template.tmpl +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/labels/label-edit-template.tmpl @@ -1,126 +1,134 @@ -Koha › Tools › Labels - + + Koha › Tools › Labels › Templates + - - - - - -
- -
-
-
-
- -
-

Edit Label Template

-
-
Template Settings - -
    -
  1. Template ID:
  2. -
  3. " />
  4. -
  5. - -
  6. - " /> -
  7. -
  8. - " /> -
  9. -
  10. - " /> -
  11. -
  12. - " /> -
  13. -
  14. - " /> -
  15. -
  16. - " /> -
  17. -
-
-
-
-
-
    - -
  1. - " /> -
  2. -
  3. - " /> -
  4. -
  5. - " /> -
  6. -
  7. - " /> - -
  8. - -
  9. - -
  10. - - - - None Defined - -
  11. - -
  12. - -
  13. " />
  14. - -
  15. - - - - - - " />
  16. - -
-
-
-
- Cancel -
-
- -
-
-
- -
-
- + + + +
+
+
+
+
+
+

Edit Label Template

+
+
+
    +
  1. + Template ID:N/A +
  2. +
  3. + + " /> +
  4. +
  5. + + +
  6. +
  7. + + " /> +
  8. +
  9. + + " /> +
  10. +
  11. + + " /> +
  12. +
  13. + + " /> +
  14. +
  15. + + " /> +
  16. +
  17. + + " /> +
  18. +
+
+
+
+
+
    +
  1. + + " /> +
  2. +
  3. + + " /> +
  4. +
  5. + + " /> +
  6. +
  7. + + " /> +
  8. +
  9. + + " /> +
  10. +
  11. + + " /> +
  12. +
  13. + + +
  14. +
  15. + + + + + Click here to define a printer profile. + +
  16. +
+
+
+
+
+
+ Cancel + + + " /> + +
+
+
+
+
+
+ +
+
+ diff --git a/labels/label-edit-template.pl b/labels/label-edit-template.pl index 47b6bd7246..44a554f1fa 100755 --- a/labels/label-edit-template.pl +++ b/labels/label-edit-template.pl @@ -1,123 +1,131 @@ #!/usr/bin/perl +# +# Copyright 2006 Katipo Communications. +# Parts Copyright 2009 Foundations Bible College. +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, +# Suite 330, Boston, MA 02111-1307 USA use strict; +use warnings; +use Sys::Syslog qw(syslog); use CGI; -use C4::Auth; -use C4::Context; -use C4::Output; -use C4::Labels; use HTML::Template::Pro; -use POSIX; - -# use Data::Dumper; - -my $dbh = C4::Context->dbh; -my $query = new CGI; - -my $tmpl_id = $query->param('tmpl_id'); +use Data::Dumper; -my $width = $query->param('width'); -my $height = $query->param('height'); -my $topmargin = $query->param('topmargin'); -my $leftmargin = $query->param('leftmargin'); -my $columns = $query->param('columns'); -my $rows = $query->param('rows'); -my $colgap = $query->param('colgap'); -my $rowgap = $query->param('rowgap'); -my $prof_id = $query->param('prof_id'); +use C4::Auth; +use C4::Output; +use C4::Context; +use C4::Debug; +use C4::Labels::Lib 1.000000 qw(get_all_profiles get_unit_values); +use C4::Labels::Template 1.000000; +my $cgi = new CGI; my ( $template, $loggedinuser, $cookie ) = get_template_and_user( { template_name => "labels/label-edit-template.tmpl", - query => $query, + query => $cgi, type => "intranet", - authnotrequired => 1, + authnotrequired => 0, flagsrequired => { catalogue => 1 }, debug => 1, } ); -my $tmpl = GetSingleLabelTemplate($tmpl_id); -my $curprof = GetAssociatedProfile($tmpl_id); -my @prof = GetAllPrinterProfiles(); -my @proflist; +my $op = $cgi->param('op') || $ARGV[0] || ''; +my $template_id = $cgi->param('template_id') || $cgi->param('element_id') || $ARGV[1] || ''; +my $label_template = ''; +my $profile_list = ''; +my $units = get_unit_values(); -# Generate an array of hashes containing possible profiles for given template and mark the currently associated one... - -foreach my $prof (@prof) { - if ( $prof->{'tmpl_id'} eq $tmpl->{'tmpl_id'} && $prof->{'prof_id'} eq $curprof->{'prof_id'} ) { - push ( @proflist, {prof_id => $prof->{'prof_id'}, - printername => $prof->{'printername'}, - paper_bin => $prof->{'paper_bin'}, - selected => 1} ); - } - - elsif ( $prof->{'tmpl_id'} eq $tmpl->{'tmpl_id'} ) { - push ( @proflist, {prof_id => $prof->{'prof_id'}, - printername => $prof->{'printername'}, - paper_bin => $prof->{'paper_bin'}} ); +if ($op eq 'edit') { + $label_template = C4::Labels::Template->retrieve(template_id => $template_id); + $profile_list = get_all_profiles(field_list => 'profile_id,printer_name,paper_bin',filter => "template_id=$template_id OR template_id=''"); +} +elsif ($op eq 'save') { + my @params = ( profile_id => $cgi->param('profile_id') || '', + template_code => $cgi->param('template_code'), + template_desc => $cgi->param('template_desc'), + page_width => $cgi->param('page_width'), + page_height => $cgi->param('page_height'), + label_width => $cgi->param('label_width'), + label_height => $cgi->param('label_height'), + top_text_margin => $cgi->param('top_text_margin'), + left_text_margin=> $cgi->param('left_text_margin'), + top_margin => $cgi->param('top_margin'), + left_margin => $cgi->param('left_margin'), + cols => $cgi->param('cols'), + rows => $cgi->param('rows'), + col_gap => $cgi->param('col_gap'), + row_gap => $cgi->param('row_gap'), + units => $cgi->param('units'), + ); + if ($template_id) { # if a label_id was passed in, this is an update to an existing layout + $label_template = C4::Labels::Template->retrieve(template_id => $template_id); + my $profile = C4::Labels::Profile->retrieve(profile_id => $cgi->param('profile_id')); + $profile->set_attr(template_id => $label_template->get_attr('template_id')) if $label_template->get_attr('template_id') != $profile->get_attr('template_id'); + $label_template->set_attr(@params); + $label_template->save(); } - - elsif ( !$prof ) { - undef @proflist; + else { # if no label_id, this is a new layout so insert it + $label_template = C4::Labels::Template->new(@params); + my $template_id = $label_template->save(); + my $profile = C4::Labels::Profile->retrieve(profile_id => $cgi->param('profile_id')); + $profile->set_attr(template_id => $template_id) if $template_id != $profile->get_attr('template_id'); } + print $cgi->redirect("label-manage.pl?label_element=template"); + exit; } - -my @units = ( - { unit => 'INCH', desc => 'Inches' }, - { unit => 'CM', desc => 'Centimeters' }, - { unit => 'MM', desc => 'Millimeters' }, - { unit => 'POINT', desc => 'Postscript Points' }, -); - -foreach my $unit (@units) { - if ( $unit->{'unit'} eq $tmpl->{'units'} ) { - $unit->{'selected'} = 1; +else { # if we get here, this is a new layout + $label_template = C4::Labels::Template->new(); +} +if ($template_id) { + foreach my $profile (@$profile_list) { + if ($profile->{'profile_id'} == $label_template->get_attr('profile_id')) { + $profile->{'selected'} = 1; + } + else { + $profile->{'selected'} = 0; + } } } -my @fonts = ( #FIXME: There is probably a way to discover what additional fonts are installed on a user's system and generate this list dynamically... - { font => 'TR', name => 'Times Roman' }, - { font => 'TB', name => 'Times Bold' }, - { font => 'TI', name => 'Times Italic' }, - { font => 'TBI', name => 'Times Bold Italic' }, - { font => 'C', name => 'Courier' }, - { font => 'CB', name => 'Courier Bold' }, - { font => 'CO', name => 'Courier Oblique' }, - { font => 'CBO', name => 'Courier Bold Oblique' }, - { font => 'H', name => 'Helvetica' }, - { font => 'HB', name => 'Helvetica Bold' }, - { font => 'HO', name => 'Helvetica Oblique' }, - { font => 'HBO', name => 'Helvetica Bold Oblique' }, -); - -foreach my $font (@fonts) { - if ( $font->{'font'} eq $tmpl->{'font'} ) { - $font->{'selected'} = 1; +foreach my $unit (@$units) { + if ($unit->{'type'} eq $label_template->get_attr('units')) { + $unit->{'selected'} = 1; } } $template->param( - - proflist => \@proflist, - units => \@units, - fonts => \@fonts, - - tmpl_id => $tmpl->{'tmpl_id'}, - tmpl_code => $tmpl->{'tmpl_code'}, - tmpl_desc => $tmpl->{'tmpl_desc'}, - page_width => $tmpl->{'page_width'}, - page_height => $tmpl->{'page_height'}, - label_width => $tmpl->{'label_width'}, - label_height => $tmpl->{'label_height'}, - topmargin => $tmpl->{'topmargin'}, - leftmargin => $tmpl->{'leftmargin'}, - cols => $tmpl->{'cols'}, - rows => $tmpl->{'rows'}, - colgap => $tmpl->{'colgap'}, - rowgap => $tmpl->{'rowgap'}, - fontsize => $tmpl->{'fontsize'}, - active => $tmpl->{'active'}, + profile_list => $profile_list, + template_id => ($label_template->get_attr('template_id') > 0) ? $label_template->get_attr('template_id') : '', + template_code => $label_template->get_attr('template_code'), + template_desc => $label_template->get_attr('template_desc'), + page_width => $label_template->get_attr('page_width'), + page_height => $label_template->get_attr('page_height'), + label_width => $label_template->get_attr('label_width'), + label_height => $label_template->get_attr('label_height'), + top_text_margin => $label_template->get_attr('top_text_margin'), + left_text_margin => $label_template->get_attr('left_text_margin'), + top_margin => $label_template->get_attr('top_margin'), + left_margin => $label_template->get_attr('left_margin'), + cols => $label_template->get_attr('cols'), + rows => $label_template->get_attr('rows'), + col_gap => $label_template->get_attr('col_gap'), + row_gap => $label_template->get_attr('row_gap'), + units => $units, ); -output_html_with_http_headers $query, $cookie, $template->output; +output_html_with_http_headers $cgi, $cookie, $template->output; -- 2.39.5