Labels update- print any marc field and add csv output.
[koha.git] / labels / label-manager.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use CGI;
5 use C4::Auth;
6 use C4::Labels;
7 use C4::Output;
8 use HTML::Template::Pro;
9 #use POSIX qw(ceil);
10 #use Data::Dumper;
11 #use Smart::Comments;
12
13 use vars qw($debug);
14
15 BEGIN { 
16         $debug = $ENV{DEBUG} || 0;
17 }
18
19 my $dbh            = C4::Context->dbh;
20 my $query          = new CGI;
21 $query->param('debug') and $debug = $query->param('debug');
22 my $op             = $query->param('op');
23 my $layout_id      = $query->param('layout_id');
24 my $layoutname     = $query->param('layoutname');
25 my $barcodetype    = $query->param('barcodetype');
26 my $bcn            = $query->param('tx_barcode');
27 my $title          = $query->param('tx_title');
28 my $subtitle       = $query->param('tx_subtitle');
29 my $isbn           = $query->param('tx_isbn');
30 my $issn           = $query->param('tx_issn');
31 my $itemtype       = $query->param('tx_itemtype');
32 my $dcn            = $query->param('tx_dewey');
33 my $classif        = $query->param('tx_classif');
34 my $itemcallnumber = $query->param('tx_itemcallnumber');
35 my $subclass       = $query->param('tx_subclass');
36 my $author         = $query->param('tx_author');
37 my $tmpl_id        = $query->param('tmpl_id');
38 my $summary        = $query->param('summary');
39 my $startlabel     = $query->param('startlabel');
40 my $printingtype   = $query->param('printingtype');
41 my $guidebox       = $query->param('guidebox');
42 my $fontsize       = $query->param('fontsize');
43 my $formatstring        = $query->param('formatstring');
44 my @itemnumber;
45 ($query->param('type') eq 'labels') ? (@itemnumber = $query->param('itemnumber')) : (@itemnumber = $query->param('borrowernumber'));
46 my $batch_type     = $query->param('type');
47
48 # little block for displaying active layout/template/batch in templates
49 # ----------
50 my $batch_id                    = $query->param('batch_id');
51 my $active_layout               = get_active_layout();
52 my $active_template             = GetActiveLabelTemplate();
53 my $active_layout_name          = $active_layout->{'layoutname'};
54 my $active_template_name        = $active_template->{'tmpl_code'};
55 # ----------
56
57 #if (!$batch_id ) {
58 #    $batch_id  = get_highest_batch();
59 #}
60
61 #my $batch_type = "labels";      #FIXME: Hardcoded for testing/development...
62 my @messages;
63 my ($itemnumber) = @itemnumber if (scalar(@itemnumber) == 1);
64
65 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
66     {
67         template_name   => "labels/label-manager.tmpl",
68         query           => $query,
69         type            => "intranet",
70         authnotrequired => 0,
71         flagsrequired   => { catalogue => 1 },
72         debug           => 1,
73     }
74 );
75
76 #if ( $op eq 'save_conf' ) {    # this early sub is depreciated, use save_layout()
77 #       SaveConf(
78 #               $barcodetype,    $title,  $isbn, 
79 #               $issn,    $itemtype,         $bcn,            $dcn, 
80 #               $classif, $subclass,         $itemcallnumber,      $author, 
81 #               $tmpl_id, $printingtype,   $guidebox,       $startlabel, $layoutname
82 #       );
83 #       print $query->redirect("label-home.pl");
84 #       exit;
85 #}
86 #elsif  ( $op eq 'save_layout' ) {
87 if  ( $op eq 'save_layout' ) {
88         save_layout(
89                 $barcodetype,    $title,  $subtitle, $isbn, 
90                 $issn,    $itemtype,         $bcn,            $dcn, 
91                 $classif, $subclass,         $itemcallnumber,      $author, 
92                 $tmpl_id, $printingtype,   $guidebox,       $startlabel, $layoutname,
93                 ,  $formatstring , $layout_id   
94         );
95         ### $layoutname
96         print $query->redirect("label-home.pl");
97         exit;
98 }
99 elsif  ( $op eq 'add_layout' ) {
100         add_layout(
101                 $barcodetype,    $title, $subtitle,  $isbn, 
102                 $issn,    $itemtype,         $bcn,            $dcn, 
103                 $classif, $subclass,         $itemcallnumber,      $author, 
104                 $tmpl_id, $printingtype,   $guidebox,       $startlabel, $layoutname,
105                 $formatstring , $layout_id
106         );
107         ### $layoutname
108         print $query->redirect("label-home.pl");
109         exit;
110 }
111
112 # FIXME: The trinary conditionals here really need to be replaced with a more robust form of db abstraction -fbcit
113
114 elsif ( $op eq 'add' ) {   # add item
115         my $query2 = "INSERT INTO $batch_type ( " . (($batch_type eq 'labels') ? 'itemnumber' : 'borrowernumber') . ", batch_id ) values ( ?,? )";
116         my $sth2   = $dbh->prepare($query2);
117         for my $inum (@itemnumber) {
118             # warn "INSERTing " . (($batch_type eq 'labels') ? 'itemnumber' : 'borrowernumber') . ":$inum for batch $batch_id";
119             $sth2->execute($inum, $batch_id);
120         }
121         $sth2->finish;
122 }
123 elsif ( $op eq 'deleteall' ) {
124         my $query2 = "DELETE FROM $batch_type";
125         my $sth2   = $dbh->prepare($query2);
126         $sth2->execute();
127         $sth2->finish;
128 }
129 elsif ( $op eq 'delete' ) {
130         my @labelids = $query->param((($batch_type eq 'labels') ? 'labelid' : 'cardid'));
131         scalar @labelids or push @messages, (($batch_type eq 'labels') ? "ERROR: No labelid(s) supplied for deletion." : "ERROR: No cardid(s) supplied for deletion.");
132         my $ins = "?," x (scalar @labelids);
133         $ins =~ s/\,$//;
134         my $query2 = "DELETE FROM $batch_type WHERE " . (($batch_type eq 'labels') ? 'labelid' : 'cardid') ." IN ($ins) ";
135         $debug and push @messages, "query2: $query2 -- (@labelids)";
136         my $sth2   = $dbh->prepare($query2);
137         $sth2->execute(@labelids);
138         $sth2->finish;
139 }
140 elsif ( $op eq 'delete_batch' ) {
141         delete_batch($batch_id, $batch_type);
142         print $query->redirect("label-manager.pl?type=$batch_type");
143         exit;
144 }
145 elsif ( $op eq 'add_batch' ) {
146         $batch_id= add_batch($batch_type);
147 }
148 elsif ( $op eq 'set_active_layout' ) {
149         set_active_layout($layout_id);
150         print $query->redirect("label-home.pl");
151         exit;
152 }
153 elsif ( $op eq 'deduplicate' ) {
154     warn "\$batch_id=$batch_id and \$batch_type=$batch_type";
155         my ($return, $dberror) = deduplicate_batch($batch_id, $batch_type);
156         my $msg = (($return) ? "Removed $return" : "Error removing") . " duplicate items from Batch $batch_id." . (($dberror) ? " Database returned: $dberror" : "");
157         push @messages, $msg;
158 }
159
160 #  first lets do a read of the labels table , to get the a list of the
161 # currently entered items to be prinited
162 my @batches = get_batches($batch_type);
163 my @resultsloop = ($batch_type eq 'labels') ? GetLabelItems($batch_id) : GetPatronCardItems($batch_id);
164 #warn "$batches[0] (id $batch_id)";
165 #warn Dumper(@resultsloop);
166
167 #calc-ing number of sheets
168 #my $number_of_results = scalar @resultsloop;
169 #my $sheets_needed = ceil( ( --$number_of_results + $startrow ) / 8 ); # rounding up
170 #my $tot_labels       = ( $sheets_needed * 8 );
171 #my $start_results    = ( $number_of_results + $startrow );
172 #my $labels_remaining = ( $tot_labels - $start_results );
173
174 if (scalar @messages) {
175         $template->param(message => 1);
176         my @complex = ();
177         foreach (@messages) {
178                 my %hash = (message_text => $_);
179                 push @complex, \%hash;
180         }
181         $template->param(message_loop => \@complex);
182 }
183 $template->param(
184     type                        => $batch_type,
185     batch_id                    => $batch_id,
186     batch_count                 => scalar @resultsloop,
187     active_layout_name          => $active_layout_name,
188     active_template_name        => $active_template_name,
189         outputformat                            => ( $active_layout->{'printingtype'} eq 'CSV' ) ? 'csv' : 'pdf' ,
190         layout_tx                                       => ( $active_layout->{'formatstring'}) ? 0 : 1 ,
191         layout_string                           => ( $active_layout->{'formatstring'}) ? 1 : 0 ,
192
193     resultsloop                 => \@resultsloop,
194     batches                     => \@batches,
195     tmpl_desc                   => $active_template->{'tmpl_desc'},
196
197     #  startrow         => $startrow,
198     #  sheets           => $sheets_needed,
199     #  labels_remaining => $labels_remaining,
200 );
201 output_html_with_http_headers $query, $cookie, $template->output;