From d47afd597b7fe121aafd0255471dcf431dd3ad96 Mon Sep 17 00:00:00 2001 From: Chris Nighswonger Date: Fri, 24 Jul 2009 23:03:50 -0400 Subject: [PATCH] [12/40] Work on label managment interface. --- C4/Labels/Batch.pm | 26 +-- .../prog/en/modules/labels/label-manage.tmpl | 102 ++++++++++++ labels/label-manage.pl | 157 ++++++++++++++++++ 3 files changed, 272 insertions(+), 13 deletions(-) create mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/labels/label-manage.tmpl create mode 100755 labels/label-manage.pl diff --git a/C4/Labels/Batch.pm b/C4/Labels/Batch.pm index 0ba5681c2b..d10dd18c99 100644 --- a/C4/Labels/Batch.pm +++ b/C4/Labels/Batch.pm @@ -38,8 +38,8 @@ sub _check_params { my $exit_code = 0; my @valid_template_params = ( 'layout_id', - 'tmpl_id', - 'prof_id', + 'template_id', + 'profile_id', ); if (scalar(@_) >1) { $given_params = {@_}; @@ -67,12 +67,12 @@ C4::Labels::Batch - A class for creating and manipulating batch objects in Koha =head1 METHODS -=head2 C4::Labels::Batch->new(layout_id => layout_id, tmpl_id => template_id, prof_id => prof_id) +=head2 C4::Labels::Batch->new(layout_id => layout_id, template_id => template_id, profile_id => profile_id) Invoking the I method constructs a new batch object with no items. example: - my $batch = C4::Labels::Batch->new(layout_id => layout_id, tmpl_id => template_id, prof_id => prof_id); + my $batch = C4::Labels::Batch->new(layout_id => layout_id, template_id => template_id, profile_id => profile_id); # Creates and returns a new batch object B This batch is I written to the database untill $batch->save() is invoked. You have been warned! @@ -85,8 +85,8 @@ sub new { my $self = { batch_id => 0, layout_id => $params{layout_id}, - tmpl_id => $params{tmpl_id}, - prof_id => $params{prof_id}, + template_id => $params{template_id}, + profile_id => $params{profile_id}, items => [], batch_stat => 0, # False if any data has changed and the db has not been updated }; @@ -158,10 +158,10 @@ sub save { my $self = shift; if ($self->{batch_id} > 0) { foreach my $item_number (@$self->{items}) { - my $query = "UPDATE labels_batches SET item_number=?, layout_id=?, tmpl_id=?, prof_id=? WHERE batch_id=?;"; + my $query = "UPDATE labels_batches SET item_number=?, layout_id=?, template_id=?, profile_id=? WHERE batch_id=?;"; warn "DEBUG: Updating: $query\n" if $debug; my $sth->C4::Context->dbh->prepare($query); - $sth->execute($item_number, $self->{layout_id}, $self->{tmpl_id}, $self->{prof_id}, $self->{batch_id}); + $sth->execute($item_number, $self->{layout_id}, $self->{template_id}, $self->{profile_id}, $self->{batch_id}); if ($sth->err) { syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr); return -1; @@ -170,10 +170,10 @@ sub save { } else { foreach my $item_number (@$self->{items}) { - my $query = "INSERT INTO labels_batches (item_number, layout_id, tmpl_id, prof_id) VALUES (?,?,?,?);"; + my $query = "INSERT INTO labels_batches (item_number, layout_id, template_id, profile_id) VALUES (?,?,?,?);"; warn "DEBUG: Inserting: $query\n" if $debug; my $sth->C4::Context->dbh->prepare($query); - $sth->execute($item_number, $self->{layout_id}, $self->{tmpl_id}, $self->{prof_id}); + $sth->execute($item_number, $self->{layout_id}, $self->{template_id}, $self->{profile_id}); if ($sth->err) { syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr); return -1; @@ -201,7 +201,7 @@ sub save { my $template = C4::Labels::Template->retrieve(template_id => 1, convert => 1); # Retrieves template record 1, converts the units to points, and returns an object containing the record - my $template = C4::Labels::Template->retrieve(template_id => 1, prof_id => prof_id); # Retrieves template record 1, converts the units + my $template = C4::Labels::Template->retrieve(template_id => 1, profile_id => profile_id); # Retrieves template record 1, converts the units to points, applies the given profile id, and returns an object containing the record =cut @@ -223,8 +223,8 @@ sub retrieve { while (my $record = $sth->fetchrow_hashref) { $self->{batch_id} = $record->{batch_id}; # FIXME: seems a bit wasteful to re-initialize these every trip: is there a better way? $self->{layout_id} = $record->{layout_id}; - $self->{tmpl_id} = $record->{tmpl_id}; - $self->{prof_id} = $record->{prof_id}; + $self->{template_id} = $record->{template_id}; + $self->{profile_id} = $record->{profile_id}; push (@{$self->{items}}, $record->{item_number}); } $self->{batch_stat} = 1; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/labels/label-manage.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/labels/label-manage.tmpl new file mode 100644 index 0000000000..d434495e3c --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/labels/label-manage.tmpl @@ -0,0 +1,102 @@ + + Koha › Tools › Labels › Manage Label Elements + + + + + + + +
+
+
+
+ +
+
+
+

Currently Available

+ + + + + + + + + + + + + + + + + + + + +
">
+
+
+ + +
+
+ +
+
+ WARNING: An error was encountered and layout was not deleted. Please have your system administrator check the syslog for details. +
+
+ +
+
+
+
+ +
+
+ diff --git a/labels/label-manage.pl b/labels/label-manage.pl new file mode 100755 index 0000000000..adabe5359b --- /dev/null +++ b/labels/label-manage.pl @@ -0,0 +1,157 @@ +#!/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 vars qw($debug); + +use Sys::Syslog qw(syslog); +use CGI; +use HTML::Template::Pro; +use Data::Dumper; + +use C4::Auth; +use C4::Output; +use C4::Context; +use C4::Debug; +use C4::Labels::Lib 1.000000 qw(get_all_templates get_all_layouts get_all_profiles get_barcode_types get_label_types get_column_names get_table_names SELECT); +use C4::Labels::Layout 1.000000; +use C4::Labels::Template 1.000000; +use C4::Labels::Profile 1.000000; + +my $cgi = new CGI; +my ( $template, $loggedinuser, $cookie ) = get_template_and_user( + { + template_name => "labels/label-manage.tmpl", + query => $cgi, + type => "intranet", + authnotrequired => 0, + flagsrequired => { catalogue => 1 }, + debug => 1, + } +); + +my $error = 0; +my $db_rows = {}; +my $column_names = []; +my $table_names = []; + +my $display_columns = { layout => [ #db column => display column + {layout_id => 'Layout ID'}, + {layout_name => 'Layout'}, + {barcode_type => 'Barcode Type'}, + {printing_type => 'Print Type'}, + {format_string => 'Fields to Print'}, + {select => 'Select'}, + ], + template => [ {template_id => 'Template ID'}, + {template_code => 'Template Name'}, + {template_desc => 'Description'}, + {select => 'Select'}, + ], + profile => [ {profile_id => 'Profile ID'}, + {printer_name => 'Printer Name'}, + {paper_bin => 'Paper Bin'}, + {_template_code => 'Template Name'}, # this display column does not have a corrisponding db column in the profile table, hence the underscore + {select => 'Select'}, + ], +}; + +my $label_element = $cgi->param('label_element') || $ARGV[0]; +my $op = $cgi->param('op') || $ARGV[1] || ''; +my $element_id = $cgi->param('element_id') || $ARGV[2] || ''; + +sub _build_table { + my $headers = shift; + my $data = shift; + my $element = shift; + my $table = []; + my $fields = []; + my @db_columns = (); + my ($row_index, $col_index) = (0,0); + my $cols = 0; # number of columns to wrap on + my $field_count = 0; + POPULATE_HEADER: + foreach my $db_column (@$headers) { + my @key = keys %$db_column; + push (@db_columns, $key[0]); + $$fields[$col_index] = {select_field => 0, field_name => ($key[0]), field_label => $db_column->{$key[0]}}; + $field_count++; + $col_index++; + } + $$table[$row_index] = {header_fields => $fields}; + $cols = $col_index; + $field_count *= scalar(@$data); # total fields to be displayed in the table + $col_index = 0; + $row_index++; + $fields = []; + POPULATE_TABLE: + foreach my $db_row (@$data) { + my $element_id = 0; + POPULATE_ROW: + foreach my $db_column (@db_columns) { + if ($db_column =~ m/^_((.*)_(.*$))/) { + my $table_name = get_table_names($2); + my $record_set = SELECT($1, @$table_name[0], $2 . "_id = " . $db_row->{$2 . "_id"}); + $db_row->{$db_column} = $$record_set[0]{$1}; + } + if (grep {$db_column eq $_} keys %$db_row) { + $element_id = $db_row->{$db_column} if $db_column =~ m/id/; + $$fields[$col_index] = {select_field => 0, field_name => ($db_column . "_tbl"), field_value => $db_row->{$db_column}}; + $col_index++; + } + } + $$fields[$col_index] = {select_field => 1, field_name => 'select', field_value => $element_id}; + $$table[$row_index] = {text_fields => $fields}; + $col_index = 0; + $row_index++; + $fields = []; + } + return $table; +} + +if ($op eq 'delete') { + $error = C4::Labels::Layout::delete(layout_id => $element_id) if $label_element eq 'layout'; + $error = C4::Labels::Template::delete(template_id => $element_id) if $label_element eq 'template'; + $error = C4::Labels::Profile::delete(profile_id => $element_id) if $label_element eq 'profile'; +} + +$table_names = get_table_names($label_element); +$column_names = get_column_names(@$table_names[0]); +$db_rows = get_all_layouts() if $label_element eq 'layout'; +$db_rows = get_all_templates() if $label_element eq 'template'; +$db_rows = get_all_profiles() if $label_element eq 'profile'; + +my $table = _build_table($display_columns->{$label_element}, $db_rows, $label_element); + +$template->param(error => $error) if ($error ne 0); +$template->param( + op => $op, + element_id => $element_id, + table_loop => $table, + label_element => $label_element, + label_element_title => ($label_element eq 'layout' ? 'Layouts' : + $label_element eq 'template' ? 'Templates' : + $label_element eq 'profile' ? 'Profiles' : + '' + ), +); + +output_html_with_http_headers $cgi, $cookie, $template->output; -- 2.39.5