porting sort_by stuff from staff to opac
[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;
9 use POSIX;
10
11 #use Data::Dumper;
12 #use Smart::Comments;
13
14 my $dbh            = C4::Context->dbh;
15 my $query          = new CGI;
16 my $op             = $query->param('op');
17 my $layout_id =    $query->param('layout_id');
18 my $layoutname =    $query->param('layoutname');
19 my $barcodetype    = $query->param('barcodetype');
20
21
22 my $bcn          = $query->param('tx_barcode');
23 my $author          = $query->param('tx_author');
24
25 my $title          = $query->param('tx_title');
26 my $subtitle          = $query->param('tx_subtitle');
27 my $isbn           = $query->param('tx_isbn');
28 my $issn           = $query->param('tx_issn');
29 my $itemtype       = $query->param('tx_itemtype');
30 my $dcn            = $query->param('tx_dewey');
31 my $classif        = $query->param('tx_classif');
32 my $itemcallnumber = $query->param('tx_itemcallnumber');
33 my $subclass       = $query->param('tx_subclass');
34 my $author         = $query->param('tx_author');
35 my $tmpl_id        = $query->param('tmpl_id');
36 my $summary        = $query->param('summary');
37 my $startlabel     = $query->param('startlabel');
38 my $printingtype   = $query->param('printingtype');
39 my $guidebox       = $query->param('guidebox');
40 my $fontsize       = $query->param('fontsize');
41 my @itemnumber     = $query->param('itemnumber');
42
43
44 # little block for displaying active layout/template/batch in templates
45 # ----------
46 my $batch_id     = $query->param('batch_id');
47 my $active_layout = get_active_layout();
48 my $active_template = GetActiveLabelTemplate();
49 my $active_layout_name = $active_layout->{'layoutname'};
50 my $active_template_name = $active_template->{'tmpl_code'};
51 # ----------
52
53 #if (!$batch_id ) {
54 #    $batch_id  = get_highest_batch();
55 #}
56
57 my ($itemnumber) = @itemnumber if (scalar(@itemnumber) == 1);
58
59 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
60     {
61         template_name   => "labels/label-manager.tmpl",
62         query           => $query,
63         type            => "intranet",
64         authnotrequired => 1,
65         flagsrequired   => { catalogue => 1 },
66         debug           => 1,
67     }
68 );
69
70 if ( $op eq 'save_conf' ) {    # this early sub is depreciated, use save_layout()
71    
72    SaveConf(
73         $barcodetype,    $title,  $isbn, 
74         $issn,    $itemtype,         $bcn,            $dcn, 
75        $classif, $subclass,         $itemcallnumber,      $author, 
76         $tmpl_id, $printingtype,   $guidebox,       $startlabel, $layoutname
77     );
78      print $query->redirect("label-home.pl");
79     exit;
80 }
81
82
83 elsif  ( $op eq 'save_layout' ) {
84     save_layout(
85         $barcodetype,    $title,  $subtitle, $isbn, 
86         $issn,    $itemtype,         $bcn,            $dcn, 
87        $classif, $subclass,         $itemcallnumber,      $author, 
88         $tmpl_id, $printingtype,   $guidebox,       $startlabel, $layoutname,
89         $layout_id
90     );
91
92 ### $layoutname
93
94      print $query->redirect("label-home.pl");
95     exit;
96 }
97 elsif  ( $op eq 'add_layout' ) {
98 add_layout(
99         $barcodetype,    $title, $subtitle,  $isbn, 
100         $issn,    $itemtype,         $bcn,            $dcn, 
101        $classif, $subclass,         $itemcallnumber,      $author, 
102         $tmpl_id, $printingtype,   $guidebox,       $startlabel, $layoutname,
103         $layout_id
104     );
105
106 ### $layoutname
107
108      print $query->redirect("label-home.pl");
109     exit;
110 }
111
112
113
114
115
116 elsif ( $op eq 'add' ) {   # add item
117     my $query2 = "INSERT INTO labels ( itemnumber, batch_id ) values ( ?,? )";
118     my $sth2   = $dbh->prepare($query2);
119     for my $inum (@itemnumber) {
120                 $sth2->execute($inum, $batch_id);
121     }
122         $sth2->finish;
123 }
124 elsif ( $op eq 'deleteall' ) {
125     my $query2 = "DELETE FROM labels";
126     my $sth2   = $dbh->prepare($query2);
127     $sth2->execute();
128     $sth2->finish;
129 }
130 elsif ( $op eq 'delete' ) {
131     my $query2 = "DELETE FROM labels where itemnumber = ?";
132     my $sth2   = $dbh->prepare($query2);
133     $sth2->execute($itemnumber);
134     $sth2->finish;
135 }
136
137
138 elsif ( $op eq 'delete_batch' ) {
139     delete_batch($batch_id);
140      print $query->redirect("label-manager.pl?batch_id=");
141          exit;
142 }
143
144 elsif ( $op eq 'add_batch' ) {
145     $batch_id= add_batch();
146
147 }
148
149 elsif ( $op eq 'set_active_layout' ) {
150     set_active_layout($layout_id);
151      print $query->redirect("label-home.pl");
152     exit;
153 }
154
155
156 #  first lets do a read of the labels table , to get the a list of the
157 # currently entered items to be prinited
158 #use Data::Dumper;
159 my @batches = get_batches();
160 my @resultsloop = get_label_items($batch_id);
161 #warn $batches[0];
162 #warn $batch_id;
163 #warn Dumper(@resultsloop);
164
165 my $tmpl =GetActiveLabelTemplate();
166 ### $tmpl
167 #calc-ing number of sheets
168 #my $number_of_results = scalar @resultsloop;
169 #my $sheets_needed = ( ( --$number_of_results + $startrow ) / 8 );
170 #$sheets_needed = ceil($sheets_needed);    # rounding up int's
171
172 #my $tot_labels       = ( $sheets_needed * 8 );
173 #my $start_results    = ( $number_of_results + $startrow );
174 #my $labels_remaining = ( $tot_labels - $start_results );
175
176 $template->param(
177     batch_id => $batch_id,
178         batch_count => scalar @resultsloop,
179     active_layout_name => $active_layout_name,
180     active_template_name => $active_template_name,
181
182     resultsloop => \@resultsloop,
183     batches => \@batches,
184         tmpl_desc => $tmpl->{'tmpl_desc'},
185
186     #  startrow         => $startrow,
187     #  sheets           => $sheets_needed,
188     #  labels_remaining => $labels_remaining,
189 );
190 output_html_with_http_headers $query, $cookie, $template->output;