[22/40] General code cleanup of new labels module code.
authorChris Nighswonger <cnighswonger@foundations.edu>
Mon, 10 Aug 2009 16:45:43 +0000 (12:45 -0400)
committerChris Nighswonger <cnighswonger@foundations.edu>
Tue, 1 Sep 2009 19:51:39 +0000 (15:51 -0400)
C4/Labels/Batch.pm
labels/label-edit-batch.pl
labels/label-edit-layout.pl
labels/label-edit-profile.pl
labels/label-edit-template.pl
labels/label-home.pl
labels/label-item-search.pl
labels/label-manage.pl
labels/label-manager.pl [deleted file]

index 05a6a2a..b9071b4 100644 (file)
@@ -65,15 +65,21 @@ C4::Labels::Batch - A class for creating and manipulating batch objects in Koha
 
 =head1 METHODS
 
-=head2 C4::Labels::Batch->new(layout_id => layout_id, template_id => template_id, profile_id => profile_id)
+=head2 C4::Labels::Batch->new()
 
-    Invoking the I<new> method constructs a new batch object with no items.
+    Invoking the I<new> method constructs a new batch object with no items. It is possible to pre-populate the batch with items and a branch code by passing them
+    as in the second example below.
+
+    B<NOTE:> The items list must be an arrayref pointing to an array of hashes containing a key/data pair after this fashion: {item_number => item_number}. The order of
+    the array elements determines the order of the items in the batch.
 
     example:
-        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
+        C<my $batch = C4::Labels::Batch->new(); # Creates and returns a new batch object>
+
+        C<my $batch = C4::Labels::Batch->new(items => $arrayref, branch_code => branch_code) #    Creates and returns a new batch object containing the items passed in
+                                                                                                with the branch code passed in.>
 
-    B<NOTE:> This batch is I<not> written to the database untill $batch->save() is invoked. You have been warned!
+    B<NOTE:> This batch is I<not> written to the database until C<$batch->save()> is invoked. You have been warned!
 
 =cut
 
index 1eae99b..1278df9 100755 (executable)
@@ -23,17 +23,12 @@ use warnings;
 use vars qw($debug);
 
 use Sys::Syslog qw(syslog);
-use Switch qw(Perl6);
 use CGI;
 use HTML::Template::Pro;
-use Data::Dumper;
-use JSON;
 
-use C4::Auth;
-use C4::Output;
-use C4::Context;
+use C4::Auth qw(get_template_and_user);
+use C4::Output qw(output_html_with_http_headers);
 use C4::Branch qw(get_branch_code_from_name);
-use C4::Debug;
 use C4::Labels::Lib 1.000000 qw(get_label_summary html_table);
 use C4::Labels::Batch 1.000000;
 
@@ -48,6 +43,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
         debug           => 1,
     }
 );
+
 my $err = 0;
 my $errstr = undef;
 my $duplicate_count = undef;
@@ -61,9 +57,10 @@ my $display_columns = [ {_label_number  => {label => 'Label Number', link_field
                         {select         => {label => 'Select', value => '_label_id'}},
                       ];
 my $op = $cgi->param('op') || undef;
-my @label_ids = $cgi->param('label_id') if $cgi->param('label_id');
 my $batch_id = $cgi->param('element_id') || $cgi->param('batch_id') || undef;
+my @label_ids = $cgi->param('label_id') if $cgi->param('label_id');
 my @item_numbers = $cgi->param('item_number') if $cgi->param('item_number');
+
 my $branch_code = get_branch_code_from_name($template->param('LoginBranchname'));
 
 if ($op eq 'remove') {
index 7aaf448..d08f065 100755 (executable)
 
 use strict;
 use warnings;
+
 use Sys::Syslog qw(syslog);
 use CGI;
 use HTML::Template::Pro;
-use Data::Dumper;
 use POSIX;
 use Text::CSV_XS;
 
-use C4::Auth;
-use C4::Output;
-use C4::Context;
-use C4::Debug;
+use C4::Auth qw(get_template_and_user);
+use C4::Output qw(output_html_with_http_headers);
 use C4::Labels::Lib 1.000000 qw(get_barcode_types get_label_types get_font_types get_text_justification_types);
 use C4::Labels::Layout 1.000000;
 
@@ -46,7 +44,6 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
     }
 );
 
-
 my $op = $cgi->param('op') || $ARGV[0] || '';
 my $layout_id = $cgi->param('layout_id') || $cgi->param('element_id') || $ARGV[1] || '';
 my $layout = '';
index e5d8bdd..93852ef 100755 (executable)
 
 use strict;
 use warnings;
+
 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::Auth qw(get_template_and_user);
+use C4::Output qw(output_html_with_http_headers);
 use C4::Labels::Lib 1.000000 qw(get_all_templates get_unit_values);
 use C4::Labels::Profile 1.000000;
 
@@ -43,11 +41,13 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
         debug           => 1,
     }
 );
-my $op = $cgi->param('op') || $ARGV[0] || '';
-my $profile_id = $cgi->param('profile_id') || $cgi->param('element_id') || $ARGV[1] || '';
-my $profile = '';
-my $template_list = '';
+
+my $op = $cgi->param('op');
+my $profile_id = $cgi->param('profile_id') || $cgi->param('element_id');
+my $profile = undef;
+my $template_list = undef;
 my @label_template = ();
+
 my $units = get_unit_values();
 
 if ($op eq 'edit') {
index 44a554f..1860a60 100755 (executable)
 
 use strict;
 use warnings;
+
 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::Auth qw(get_template_and_user);
+use C4::Output qw(output_html_with_http_headers);
 use C4::Labels::Lib 1.000000 qw(get_all_profiles get_unit_values);
 use C4::Labels::Template 1.000000;
 
@@ -44,10 +42,11 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
     }
 );
 
-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 $op = $cgi->param('op');
+my $template_id = $cgi->param('template_id') || $cgi->param('element_id');
+my $label_template = undef;
+my $profile_list = undef;
+
 my $units = get_unit_values();
 
 if ($op eq 'edit') {
index f60ab72..40a3621 100755 (executable)
@@ -1,12 +1,32 @@
 #!/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 CGI;
-use C4::Auth;
-use C4::Output;
-use C4::Context;
 use HTML::Template::Pro;
 
+use C4::Auth qw(get_template_and_user);
+use C4::Output qw(output_html_with_http_headers);
+
 my $cgi = new CGI;
 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
     {
index 52851dc..433ed14 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/perl
-
+#
 # Copyright 2000-2002 Katipo Communications
 #
 # This file is part of Koha.
 
 use strict;
 use warnings;
+use vars qw($debug $cgi_debug);
 
 use CGI;
 use HTML::Template::Pro;
 use List::Util qw( max min );
 use POSIX;
-use Data::Dumper;
 
-use C4::Auth;
+use C4::Auth qw(get_template_and_user);
+use C4::Output qw(output_html_with_http_headers);
 use C4::Context;
-use C4::Search;
-use C4::Auth;
-use C4::Output;
-use C4::Biblio;
-use C4::Items;
-use C4::Acquisition;
-use C4::Search;
 use C4::Dates;
-use C4::Koha;    # XXX subfield_is_koha_internal_p
+use C4::Search qw(SimpleSearch);
+use C4::Biblio qw(TransformMarcToKoha);
+use C4::Items qw(GetItemInfosOf get_itemnumbers_of);
+use C4::Koha qw(GetItemTypes);    # XXX subfield_is_koha_internal_p
 use C4::Labels::Lib qw(html_table);
 use C4::Debug;
 
@@ -48,29 +45,20 @@ BEGIN {
     }
 }
 
-# Creates a scrolling list with the associated default value.
-# Using more than one scrolling list in a CGI assigns the same default value to all the
-# scrolling lists on the page !?!? That's why this function was written.
-
 my $query = new CGI;
 
 my $type      = $query->param('type');
-my $op        = $query->param('op') || '';
+my $op        = $query->param('op');
 my $batch_id  = $query->param('batch_id');
-my $ccl_query = $query->param('ccl_query') || '';
-
-my $dbh = C4::Context->dbh;
-
+my $ccl_query = $query->param('ccl_query');
 my $startfrom = $query->param('startfrom') || 1;
-my ( $template, $loggedinuser, $cookie );
+my ($template, $loggedinuser, $cookie) = (undef, undef, undef);
 my (
     $total_hits,  $orderby, $results,  $total,  $error,
     $marcresults, $idx,     $datefrom, $dateto, $ccl_textbox
 );
-
 my $resultsperpage = C4::Context->preference('numSearchResults') || '20';
 my $show_results = 0;
-
 my $display_columns = [ {_add                   => {label => "Add Item", link_field => 1}},
                         {_item_call_number      => {label => "Call Number", link_field => 0}},
                         {_date_accessioned      => {label => "Accession Date", link_field => 0}},
@@ -79,7 +67,6 @@ my $display_columns = [ {_add                   => {label => "Add Item", link_fi
                       ];
 
 if ( $op eq "do_search" ) {
-    my @params = $query->param();
     $idx         = $query->param('idx');
     $ccl_textbox = $query->param('ccl_textbox');
     if ( $ccl_textbox && $idx ) {
index bb7902b..8c90f44 100755 (executable)
@@ -23,16 +23,12 @@ use warnings;
 use vars qw($debug);
 
 use Sys::Syslog qw(syslog);
-use Switch qw(Perl6);
 use CGI;
 use HTML::Template::Pro;
-use Data::Dumper;
 
-use C4::Auth;
-use C4::Output;
-use C4::Context;
+use C4::Auth qw(get_template_and_user);
+use C4::Output qw(output_html_with_http_headers);
 use autouse 'C4::Branch' => qw(get_branch_code_from_name);
-use C4::Debug;
 use C4::Labels::Lib 1.000000 qw(get_all_templates get_all_layouts get_all_profiles get_batch_summary html_table);
 use C4::Labels::Layout 1.000000;
 use C4::Labels::Template 1.000000;
@@ -78,31 +74,24 @@ my $display_columns = { layout =>   [  #db column       => display column
                                     ],
 };
 
-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] || '';
+my $label_element = $cgi->param('label_element');
+my $op = $cgi->param('op');
+my $element_id = $cgi->param('element_id');
 my $branch_code = ($label_element eq 'batch' ? get_branch_code_from_name($template->param('LoginBranchname')) : '');
 
 if ($op eq 'delete') {
-    given ($label_element) {
-        when 'layout'   {$error = C4::Labels::Layout::delete(layout_id => $element_id); last;}
-        when 'template' {$error = C4::Labels::Template::delete(template_id => $element_id); last;}
-        when 'profile'  {$error = C4::Labels::Profile::delete(profile_id => $element_id); last;}
-        when 'batch'    {$error = C4::Labels::Batch::delete(batch_id => $element_id, branch_code => $branch_code); last;}
-        default         {}      # FIXME: Some error trapping code 
-    }
-#    FIXME: this does not allow us to process any errors
-#    print $cgi->redirect("label-manage.pl?label_element=$label_element");
-#    exit;
+    if          ($label_element eq 'layout')    {$error = C4::Labels::Layout::delete(layout_id => $element_id);}
+    elsif       ($label_element eq 'template')  {$error = C4::Labels::Template::delete(template_id => $element_id);}
+    elsif       ($label_element eq 'profile')   {$error = C4::Labels::Profile::delete(profile_id => $element_id);}
+    elsif       ($label_element eq 'batch')     {$error = C4::Labels::Batch::delete(batch_id => $element_id, branch_code => $branch_code);}
+    else                                        {}      # FIXME: Some error trapping code 
 }
 
-given ($label_element) {
-    when 'layout'       {$db_rows = get_all_layouts();}
-    when 'template'     {$db_rows = get_all_templates();}
-    when 'profile'      {$db_rows = get_all_profiles();}
-    when 'batch'        {$db_rows = get_batch_summary(filter => "branch_code=\'$branch_code\'");}
-    default             {}      # FIXME: Some error trapping code
-}
+if      ($label_element eq 'layout')    {$db_rows = get_all_layouts();}
+elsif   ($label_element eq 'template')  {$db_rows = get_all_templates();}
+elsif   ($label_element eq 'profile')   {$db_rows = get_all_profiles();}
+elsif   ($label_element eq 'batch')     {$db_rows = get_batch_summary(filter => "branch_code=\'$branch_code\'");}
+else                                    {}      # FIXME: Some error trapping code
 
 my $table = html_table($display_columns->{$label_element}, $db_rows);
 
diff --git a/labels/label-manager.pl b/labels/label-manager.pl
deleted file mode 100755 (executable)
index 08432ca..0000000
+++ /dev/null
@@ -1,192 +0,0 @@
-#!/usr/bin/perl
-
-use strict;
-use CGI;
-use C4::Auth;
-use C4::Labels;
-use C4::Output;
-use HTML::Template::Pro;
-#use POSIX qw(ceil);
-#use Data::Dumper;
-#use Smart::Comments;
-
-use vars qw($debug);
-
-BEGIN { 
-       $debug = $ENV{DEBUG} || 0;
-}
-
-my $dbh            = C4::Context->dbh;
-my $query          = new CGI;
-$query->param('debug') and $debug = $query->param('debug');
-my $op             = $query->param('op');
-my $layout_id      = $query->param('layout_id');
-my $layoutname     = $query->param('layoutname');
-my $barcodetype    = $query->param('barcodetype');
-my $bcn            = $query->param('tx_barcode');
-my $title          = $query->param('tx_title');
-my $subtitle       = $query->param('tx_subtitle');
-my $isbn           = $query->param('tx_isbn');
-my $issn           = $query->param('tx_issn');
-my $itemtype       = $query->param('tx_itemtype');
-my $itemcallnumber = $query->param('tx_itemcallnumber');
-my $author         = $query->param('tx_author');
-my $tmpl_id        = $query->param('tmpl_id');
-my $summary        = $query->param('summary');
-my $startlabel     = $query->param('startlabel');
-my $printingtype   = $query->param('printingtype');
-my $guidebox       = $query->param('guidebox');
-my $fontsize       = $query->param('fontsize');
-my $callnum_split  = $query->param('callnum_split');
-my $text_justify   = $query->param('text_justify');
-my $formatstring   = $query->param('formatstring');
-my $batch_type     = $query->param('type');
-($batch_type and $batch_type eq 'patroncards') or $batch_type = 'labels';
-my @itemnumber;
-($batch_type eq 'labels') ? (@itemnumber = $query->param('itemnumber')) : (@itemnumber = $query->param('borrowernumber'));
-
-# little block for displaying active layout/template/batch in templates
-# ----------
-my $batch_id                    = $query->param('batch_id');
-my $active_layout               = get_active_layout();
-my $active_template             = GetActiveLabelTemplate();
-my $active_layout_name          = $active_layout->{'layoutname'};
-my $active_template_name        = $active_template->{'tmpl_code'};
-# ----------
-
-#if (!$batch_id ) {
-#    $batch_id  = get_highest_batch();
-#}
-
-#my $batch_type = "labels";      #FIXME: Hardcoded for testing/development...
-my @messages;
-my ($itemnumber) = @itemnumber if (scalar(@itemnumber) == 1);
-
-my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
-    {
-        template_name   => "labels/label-manager.tmpl",
-        query           => $query,
-        type            => "intranet",
-        authnotrequired => 0,
-        flagsrequired   => { catalogue => 1 },
-        debug           => 1,
-    }
-);
-
-if  ( $op eq 'save_layout' ) {
-       save_layout(
-               $barcodetype,    $title,  $subtitle, $isbn, 
-               $issn,    $itemtype,         $bcn,            $text_justify, 
-               $callnum_split, $itemcallnumber,      $author, 
-               $tmpl_id, $printingtype,   $guidebox,       $startlabel, $layoutname,
-               ,  $formatstring , $layout_id   
-       );
-       ### $layoutname
-       print $query->redirect("label-home.pl");
-       exit;
-}
-elsif  ( $op eq 'add_layout' ) {
-       add_layout(
-               $barcodetype,    $title, $subtitle,  $isbn, 
-               $issn,    $itemtype,         $bcn,            $text_justify, 
-               $callnum_split, $itemcallnumber,      $author, 
-               $tmpl_id, $printingtype,   $guidebox,       $startlabel, $layoutname,
-               $formatstring , $layout_id
-       );
-       ### $layoutname
-       print $query->redirect("label-home.pl");
-       exit;
-}
-
-# FIXME: The trinary conditionals here really need to be replaced with a more robust form of db abstraction -fbcit
-
-elsif ( $op eq 'add' ) {   # add item
-       my $query2 = ($batch_type eq 'patroncards') ? 
-               "INSERT INTO patroncards (borrowernumber, batch_id) values (?,?)" :
-               "INSERT INTO labels      (itemnumber,     batch_id) values (?,?)" ;
-       my $sth2   = $dbh->prepare($query2);
-       for my $inum (@itemnumber) {
-               # warn "INSERTing " . (($batch_type eq 'labels') ? 'itemnumber' : 'borrowernumber') . ":$inum for batch $batch_id";
-           $sth2->execute($inum, $batch_id);
-       }
-}
-elsif ( $op eq 'deleteall' ) {
-       my $query2 = "DELETE FROM $batch_type";
-       my $sth2   = $dbh->prepare($query2);
-       $sth2->execute();
-}
-elsif ( $op eq 'delete' ) {
-       my @labelids = $query->param((($batch_type eq 'labels') ? 'labelid' : 'cardid'));
-       scalar @labelids or push @messages, (($batch_type eq 'labels') ? "ERROR: No labelid(s) supplied for deletion." : "ERROR: No cardid(s) supplied for deletion.");
-       my $ins = "?," x (scalar @labelids);
-       $ins =~ s/\,$//;
-       my $query2 = "DELETE FROM $batch_type WHERE " . (($batch_type eq 'labels') ? 'labelid' : 'cardid') ." IN ($ins) ";
-       $debug and push @messages, "query2: $query2 -- (@labelids)";
-       my $sth2   = $dbh->prepare($query2);
-       $sth2->execute(@labelids);
-}
-elsif ( $op eq 'delete_batch' ) {
-       delete_batch($batch_id, $batch_type);
-       print $query->redirect("label-manager.pl?type=$batch_type");
-       exit;
-}
-elsif ( $op eq 'add_batch' ) {
-       $batch_id= add_batch($batch_type);
-}
-elsif ( $op eq 'set_active_layout' ) {
-       set_active_layout($layout_id);
-       print $query->redirect("label-home.pl");
-       exit;
-}
-elsif ( $op eq 'deduplicate' ) {
-    warn "\$batch_id=$batch_id and \$batch_type=$batch_type";
-       my ($return, $dberror) = deduplicate_batch($batch_id, $batch_type);
-       my $msg = (($return) ? "Removed $return" : "Error removing") . " duplicate items from Batch $batch_id." . (($dberror) ? " Database returned: $dberror" : "");
-       push @messages, $msg;
-}
-
-#  first lets do a read of the labels table , to get the a list of the
-# currently entered items to be prinited
-my @batches = get_batches($batch_type);
-my @resultsloop = ($batch_type eq 'labels') ? GetLabelItems($batch_id) : GetPatronCardItems($batch_id);
-#warn "$batches[0] (id $batch_id)";
-#warn Dumper(@resultsloop);
-
-#calc-ing number of sheets
-#my $number_of_results = scalar @resultsloop;
-#my $sheets_needed = ceil( ( --$number_of_results + $startrow ) / 8 ); # rounding up
-#my $tot_labels       = ( $sheets_needed * 8 );
-#my $start_results    = ( $number_of_results + $startrow );
-#my $labels_remaining = ( $tot_labels - $start_results );
-
-if (scalar @messages) {
-       $template->param(message => 1);
-       my @complex = ();
-       foreach (@messages) {
-               my %hash = (message_text => $_);
-               push @complex, \%hash;
-       }
-       $template->param(message_loop => \@complex);
-}
-if ($batch_type eq 'labels' or $batch_type eq 'patroncards') {
-       $template->param("batch_is_$batch_type" => 1);
-}
-$template->param(
-    batch_type                  => $batch_type,
-    batch_id                    => $batch_id,
-    batch_count                 => scalar @resultsloop,
-    active_layout_name          => $active_layout_name,
-    active_template_name        => $active_template_name,
-       outputformat                            => ( $active_layout->{'printingtype'} eq 'CSV' ) ? 'csv' : 'pdf' ,
-       layout_tx                                       => ( $active_layout->{'formatstring'}) ? 0 : 1 ,
-       layout_string                           => ( $active_layout->{'formatstring'}) ? 1 : 0 ,
-
-    resultsloop                 => \@resultsloop,
-    batches                     => \@batches,
-    tmpl_desc                   => $active_template->{'tmpl_desc'},
-
-    #  startrow         => $startrow,
-    #  sheets           => $sheets_needed,
-    #  labels_remaining => $labels_remaining,
-);
-output_html_with_http_headers $query, $cookie, $template->output;