[10/40] Work on Label Tool Homepage.
[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 $itemcallnumber = $query->param('tx_itemcallnumber');
33 my $author         = $query->param('tx_author');
34 my $tmpl_id        = $query->param('tmpl_id');
35 my $summary        = $query->param('summary');
36 my $startlabel     = $query->param('startlabel');
37 my $printingtype   = $query->param('printingtype');
38 my $guidebox       = $query->param('guidebox');
39 my $fontsize       = $query->param('fontsize');
40 my $callnum_split  = $query->param('callnum_split');
41 my $text_justify   = $query->param('text_justify');
42 my $formatstring   = $query->param('formatstring');
43 my $batch_type     = $query->param('type');
44 ($batch_type and $batch_type eq 'patroncards') or $batch_type = 'labels';
45 my @itemnumber;
46 ($batch_type eq 'labels') ? (@itemnumber = $query->param('itemnumber')) : (@itemnumber = $query->param('borrowernumber'));
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_layout' ) {
77         save_layout(
78                 $barcodetype,    $title,  $subtitle, $isbn, 
79                 $issn,    $itemtype,         $bcn,            $text_justify, 
80                 $callnum_split, $itemcallnumber,      $author, 
81                 $tmpl_id, $printingtype,   $guidebox,       $startlabel, $layoutname,
82                 ,  $formatstring , $layout_id   
83         );
84         ### $layoutname
85         print $query->redirect("label-home.pl");
86         exit;
87 }
88 elsif  ( $op eq 'add_layout' ) {
89         add_layout(
90                 $barcodetype,    $title, $subtitle,  $isbn, 
91                 $issn,    $itemtype,         $bcn,            $text_justify, 
92                 $callnum_split, $itemcallnumber,      $author, 
93                 $tmpl_id, $printingtype,   $guidebox,       $startlabel, $layoutname,
94                 $formatstring , $layout_id
95         );
96         ### $layoutname
97         print $query->redirect("label-home.pl");
98         exit;
99 }
100
101 # FIXME: The trinary conditionals here really need to be replaced with a more robust form of db abstraction -fbcit
102
103 elsif ( $op eq 'add' ) {   # add item
104         my $query2 = ($batch_type eq 'patroncards') ? 
105                 "INSERT INTO patroncards (borrowernumber, batch_id) values (?,?)" :
106                 "INSERT INTO labels      (itemnumber,     batch_id) values (?,?)" ;
107         my $sth2   = $dbh->prepare($query2);
108         for my $inum (@itemnumber) {
109                 # warn "INSERTing " . (($batch_type eq 'labels') ? 'itemnumber' : 'borrowernumber') . ":$inum for batch $batch_id";
110             $sth2->execute($inum, $batch_id);
111         }
112 }
113 elsif ( $op eq 'deleteall' ) {
114         my $query2 = "DELETE FROM $batch_type";
115         my $sth2   = $dbh->prepare($query2);
116         $sth2->execute();
117 }
118 elsif ( $op eq 'delete' ) {
119         my @labelids = $query->param((($batch_type eq 'labels') ? 'labelid' : 'cardid'));
120         scalar @labelids or push @messages, (($batch_type eq 'labels') ? "ERROR: No labelid(s) supplied for deletion." : "ERROR: No cardid(s) supplied for deletion.");
121         my $ins = "?," x (scalar @labelids);
122         $ins =~ s/\,$//;
123         my $query2 = "DELETE FROM $batch_type WHERE " . (($batch_type eq 'labels') ? 'labelid' : 'cardid') ." IN ($ins) ";
124         $debug and push @messages, "query2: $query2 -- (@labelids)";
125         my $sth2   = $dbh->prepare($query2);
126         $sth2->execute(@labelids);
127 }
128 elsif ( $op eq 'delete_batch' ) {
129         delete_batch($batch_id, $batch_type);
130         print $query->redirect("label-manager.pl?type=$batch_type");
131         exit;
132 }
133 elsif ( $op eq 'add_batch' ) {
134         $batch_id= add_batch($batch_type);
135 }
136 elsif ( $op eq 'set_active_layout' ) {
137         set_active_layout($layout_id);
138         print $query->redirect("label-home.pl");
139         exit;
140 }
141 elsif ( $op eq 'deduplicate' ) {
142     warn "\$batch_id=$batch_id and \$batch_type=$batch_type";
143         my ($return, $dberror) = deduplicate_batch($batch_id, $batch_type);
144         my $msg = (($return) ? "Removed $return" : "Error removing") . " duplicate items from Batch $batch_id." . (($dberror) ? " Database returned: $dberror" : "");
145         push @messages, $msg;
146 }
147
148 #  first lets do a read of the labels table , to get the a list of the
149 # currently entered items to be prinited
150 my @batches = get_batches($batch_type);
151 my @resultsloop = ($batch_type eq 'labels') ? GetLabelItems($batch_id) : GetPatronCardItems($batch_id);
152 #warn "$batches[0] (id $batch_id)";
153 #warn Dumper(@resultsloop);
154
155 #calc-ing number of sheets
156 #my $number_of_results = scalar @resultsloop;
157 #my $sheets_needed = ceil( ( --$number_of_results + $startrow ) / 8 ); # rounding up
158 #my $tot_labels       = ( $sheets_needed * 8 );
159 #my $start_results    = ( $number_of_results + $startrow );
160 #my $labels_remaining = ( $tot_labels - $start_results );
161
162 if (scalar @messages) {
163         $template->param(message => 1);
164         my @complex = ();
165         foreach (@messages) {
166                 my %hash = (message_text => $_);
167                 push @complex, \%hash;
168         }
169         $template->param(message_loop => \@complex);
170 }
171 if ($batch_type eq 'labels' or $batch_type eq 'patroncards') {
172         $template->param("batch_is_$batch_type" => 1);
173 }
174 $template->param(
175     batch_type                  => $batch_type,
176     batch_id                    => $batch_id,
177     batch_count                 => scalar @resultsloop,
178     active_layout_name          => $active_layout_name,
179     active_template_name        => $active_template_name,
180         outputformat                            => ( $active_layout->{'printingtype'} eq 'CSV' ) ? 'csv' : 'pdf' ,
181         layout_tx                                       => ( $active_layout->{'formatstring'}) ? 0 : 1 ,
182         layout_string                           => ( $active_layout->{'formatstring'}) ? 1 : 0 ,
183
184     resultsloop                 => \@resultsloop,
185     batches                     => \@batches,
186     tmpl_desc                   => $active_template->{'tmpl_desc'},
187
188     #  startrow         => $startrow,
189     #  sheets           => $sheets_needed,
190     #  labels_remaining => $labels_remaining,
191 );
192 output_html_with_http_headers $query, $cookie, $template->output;