Fifth installment Patron card generation feature
[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 @itemnumber     = $query->param('itemnumber');
44 my $batch_type     = $query->param('type');
45
46 # little block for displaying active layout/template/batch in templates
47 # ----------
48 my $batch_id                    = $query->param('batch_id');
49 my $active_layout               = get_active_layout();
50 my $active_template             = GetActiveLabelTemplate();
51 my $active_layout_name          = $active_layout->{'layoutname'};
52 my $active_template_name        = $active_template->{'tmpl_code'};
53 # ----------
54
55 #if (!$batch_id ) {
56 #    $batch_id  = get_highest_batch();
57 #}
58
59 #my $batch_type = "labels";      #FIXME: Hardcoded for testing/development...
60 my @messages;
61 my ($itemnumber) = @itemnumber if (scalar(@itemnumber) == 1);
62
63 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
64     {
65         template_name   => "labels/label-manager.tmpl",
66         query           => $query,
67         type            => "intranet",
68         authnotrequired => 0,
69         flagsrequired   => { catalogue => 1 },
70         debug           => 1,
71     }
72 );
73
74 if ( $op eq 'save_conf' ) {    # this early sub is depreciated, use save_layout()
75         SaveConf(
76                 $barcodetype,    $title,  $isbn, 
77                 $issn,    $itemtype,         $bcn,            $dcn, 
78                 $classif, $subclass,         $itemcallnumber,      $author, 
79                 $tmpl_id, $printingtype,   $guidebox,       $startlabel, $layoutname
80         );
81         print $query->redirect("label-home.pl");
82         exit;
83 }
84 elsif  ( $op eq 'save_layout' ) {
85         save_layout(
86                 $barcodetype,    $title,  $subtitle, $isbn, 
87                 $issn,    $itemtype,         $bcn,            $dcn, 
88                 $classif, $subclass,         $itemcallnumber,      $author, 
89                 $tmpl_id, $printingtype,   $guidebox,       $startlabel, $layoutname,
90                 $layout_id
91                 );
92         ### $layoutname
93         print $query->redirect("label-home.pl");
94         exit;
95 }
96 elsif  ( $op eq 'add_layout' ) {
97         add_layout(
98                 $barcodetype,    $title, $subtitle,  $isbn, 
99                 $issn,    $itemtype,         $bcn,            $dcn, 
100                 $classif, $subclass,         $itemcallnumber,      $author, 
101                 $tmpl_id, $printingtype,   $guidebox,       $startlabel, $layoutname,
102                 $layout_id
103         );
104         ### $layoutname
105         print $query->redirect("label-home.pl");
106         exit;
107 }
108
109 # FIXME: The trinary conditionals here really need to be replaced with a more robust form of db abstraction -fbcit
110
111 elsif ( $op eq 'add' ) {   # add item
112         my $query2 = "INSERT INTO $batch_type ( " . (($batch_type eq 'labels') ? 'itemnumber' : 'borrowernumber') . ", batch_id ) values ( ?,? )";
113         my $sth2   = $dbh->prepare($query2);
114         for my $inum (@itemnumber) {
115                 $sth2->execute($inum, $batch_id);
116         }
117         $sth2->finish;
118 }
119 elsif ( $op eq 'deleteall' ) {
120         my $query2 = "DELETE FROM $batch_type";
121         my $sth2   = $dbh->prepare($query2);
122         $sth2->execute();
123         $sth2->finish;
124 }
125 elsif ( $op eq 'delete' ) {
126         my @labelids = $query->param((($batch_type eq 'labels') ? 'labelid' : 'cardid'));
127         scalar @labelids or push @messages, (($batch_type eq 'labels') ? "ERROR: No labelid(s) supplied for deletion." : "ERROR: No cardid(s) supplied for deletion.");
128         my $ins = "?," x (scalar @labelids);
129         $ins =~ s/\,$//;
130         my $query2 = "DELETE FROM $batch_type WHERE " . (($batch_type eq 'labels') ? 'labelid' : 'cardid') ." IN ($ins) ";
131         $debug and push @messages, "query2: $query2 -- (@labelids)";
132         my $sth2   = $dbh->prepare($query2);
133         $sth2->execute(@labelids);
134         $sth2->finish;
135 }
136 elsif ( $op eq 'delete_batch' ) {
137         delete_batch($batch_id, $batch_type);
138         print $query->redirect("label-manager.pl");
139         exit;
140 }
141 elsif ( $op eq 'add_batch' ) {
142         $batch_id= add_batch($batch_type);
143 }
144 elsif ( $op eq 'set_active_layout' ) {
145         set_active_layout($layout_id);
146         print $query->redirect("label-home.pl");
147         exit;
148 }
149 elsif ( $op eq 'deduplicate' ) {
150         my $return = deduplicate_batch($batch_id);
151         my $msg = (($return) ? "Removed $return" : "Error revoving") . " duplicate items from Batch $batch_id";
152         push @messages, $msg;
153 }
154
155 #  first lets do a read of the labels table , to get the a list of the
156 # currently entered items to be prinited
157 my @batches = get_batches($batch_type);
158 my @resultsloop = ($batch_type eq 'labels') ? GetLabelItems($batch_id) : GetPatronCardItems($batch_id);
159 #warn "$batches[0] (id $batch_id)";
160 #warn Dumper(@resultsloop);
161
162 #calc-ing number of sheets
163 #my $number_of_results = scalar @resultsloop;
164 #my $sheets_needed = ceil( ( --$number_of_results + $startrow ) / 8 ); # rounding up
165 #my $tot_labels       = ( $sheets_needed * 8 );
166 #my $start_results    = ( $number_of_results + $startrow );
167 #my $labels_remaining = ( $tot_labels - $start_results );
168
169 if (scalar @messages) {
170         $template->param(message => 1);
171         my @complex = ();
172         foreach (@messages) {
173                 my %hash = (message_text => $_);
174                 push @complex, \%hash;
175         }
176         $template->param(message_loop => \@complex);
177 }
178 $template->param(
179     batch_type                  => $batch_type,
180     batch_id                    => $batch_id,
181     batch_count                 => scalar @resultsloop,
182     active_layout_name          => $active_layout_name,
183     active_template_name        => $active_template_name,
184
185     resultsloop                 => \@resultsloop,
186     batches                     => \@batches,
187     tmpl_desc                   => $active_template->{'tmpl_desc'},
188
189     #  startrow         => $startrow,
190     #  sheets           => $sheets_needed,
191     #  labels_remaining => $labels_remaining,
192 );
193 output_html_with_http_headers $query, $cookie, $template->output;