bugfix => escaping correctly values in SQL
[koha.git] / C4 / Print.pm
1 package C4::Print; #assumes C4/Print.pm
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 require Exporter;
23 #use C4::InterfaceCDK;
24
25 use C4::Context;
26
27
28 use vars qw($VERSION @ISA @EXPORT);
29
30 # set the version for version checking
31 $VERSION = 0.01;
32
33 =head1 NAME
34
35 C4::Print - Koha module dealing with printing
36
37 =head1 SYNOPSIS
38
39   use C4::Print;
40
41 =head1 DESCRIPTION
42
43 The functions in this module handle sending text to a printer.
44
45 =head1 FUNCTIONS
46
47 =over 2
48
49 =cut
50
51 @ISA = qw(Exporter);
52 @EXPORT = qw(&remoteprint &printreserve &printslip);
53
54 =item remoteprint
55
56   &remoteprint($env, $items, $borrower);
57
58 Prints the list of items in C<$items> to a printer.
59
60 C<$env> is a reference-to-hash. C<$env-E<gt>{queue}> specifies the
61 queue to print to; if it is empty or has the special value C<nulllp>,
62 C<&remoteprint> will print to the file F</tmp/kohaiss>.
63
64 C<$borrower> is a reference-to-hash giving information about a patron.
65 This may be gotten from C<&getpatroninformation>. The patron's name
66 will be printed in the output.
67
68 C<$items> is a reference-to-list, where each element is a
69 reference-to-hash describing a borrowed item. C<$items> may be gotten
70 from C<&currentissues>.
71
72 =cut
73 #'
74 # FIXME - It'd be nifty if this could generate pretty PostScript.
75 sub remoteprint {
76   my ($env,$items,$borrower)=@_;
77
78   (return) unless (C4::Context->boolean_preference('printcirculationslips'));
79   my $file=time;                # FIXME - Not used
80   my $queue = $env->{'queue'};
81   # FIXME - If 'queue' is undefined or empty, then presumably it should
82   # mean "use the default queue", whatever the default is. Presumably
83   # the default depends on the physical location of the machine.
84   # FIXME - Perhaps "print to file" should be a supported option. Just
85   # set the queue to "file" (or " file", if real queues aren't allowed
86   # to have spaces in them). Or perhaps if $queue eq "" and
87   # $env->{file} ne "", then that should mean "print to $env->{file}".
88   if ($queue eq "" || $queue eq 'nulllp') {
89     open (PRINTER,">/tmp/kohaiss");
90   } else {
91     # FIXME - This assumes that 'lpr' exists, and works as expected.
92     # This is a reasonable assumption, but only because every other
93     # printing package has a wrapper script called 'lpr'. It'd still
94     # be better to be able to customize this.
95     open(PRINTER, "| lpr -P $queue > /dev/null") or die "Couldn't write to queue:$queue!\n";
96   }
97 #  print $queue;
98   #open (FILE,">/tmp/$file");
99   my $i=0;
100   my $brdata = $env->{'brdata'};        # FIXME - Not used
101   # FIXME - This is HLT-specific. Put this stuff in a customizable
102   # site-specific file somewhere.
103   print PRINTER "Horowhenua Library Trust\r\n";
104 #  print PRINTER "$brdata->{'branchname'}\r\n";
105   print PRINTER "Phone: 368-1953\r\n";
106   print PRINTER "Fax:    367-9218\r\n";
107   print PRINTER "Email:  renewals\@library.org.nz\r\n\r\n\r\n";
108   print PRINTER "$borrower->{'cardnumber'}\r\n";
109   print PRINTER "$borrower->{'title'} $borrower->{'initials'} $borrower->{'surname'}\r\n";
110   # FIXME - Use   for ($i = 0; $items->[$i]; $i++)
111   # Or better yet,   foreach $item (@{$items})
112   while ($items->[$i]){
113 #    print $i;
114     my $itemdata = $items->[$i];
115     # FIXME - This is just begging for a Perl format.
116     print PRINTER "$i $itemdata->{'title'}\r\n";
117     print PRINTER "$itemdata->{'barcode'}";
118     print PRINTER " "x15;
119     print PRINTER "$itemdata->{'date_due'}\r\n";
120     $i++;
121   }
122   print PRINTER "\r\n\r\n\r\n\r\n\r\n\r\n\r\n";
123   if ($env->{'printtype'} eq "docket"){
124     #print chr(27).chr(105);
125   }
126   close PRINTER;
127   #system("lpr /tmp/$file");
128 }
129
130 sub printreserve {
131   my($env, $branchname, $bordata, $itemdata)=@_;
132   my $file=time;
133   my $printer = $env->{'printer'};
134   (return) unless (C4::Context->boolean_preference('printreserveslips'));
135   if ($printer eq "" || $printer eq 'nulllp') {
136     open (PRINTER,">>/tmp/kohares");
137   } else {
138     open (PRINTER, "| lpr -P $printer >/dev/null") or die "Couldn't write to queue:$!\n";
139   }
140   my @da = localtime(time());
141   my $todaysdate = "$da[2]:$da[1]  $da[3]/$da[4]/$da[5]";
142
143 #(1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]+1)).sprintf ("%0.2d", $datearr[3]);
144   my $slip = <<"EOF";
145 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
146 Date: $todaysdate;
147
148 ITEM RESERVED: 
149 $itemdata->{'title'} ($itemdata->{'author'})
150 barcode: $itemdata->{'barcode'}
151
152 COLLECT AT: $branchname
153
154 BORROWER:
155 $bordata->{'surname'}, $bordata->{'firstname'}
156 card number: $bordata->{'cardnumber'}
157 Phone: $bordata->{'phone'}
158 $bordata->{'streetaddress'}
159 $bordata->{'suburb'}
160 $bordata->{'town'}
161 $bordata->{'emailaddress'}
162
163
164 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
165 EOF
166     print PRINTER $slip;
167   close PRINTER;
168   return $slip;
169 }
170
171 =item printslip
172
173   &printslip($env, $text)
174
175 Prints the string C<$text> to a printer. C<$env-E<gt>{queue}>
176 specifies the queue to print to.
177
178 If C<$env-E<gt>{queue}> is empty or set to C<nulllp>, C<&printslip>
179 will print to the file F</tmp/kohares>.
180
181 =cut
182 #'
183 # FIXME - There's also a &printslip in circ/circulation.pl
184 sub printslip {
185   my($env, $slip)=@_;
186   my $printer = $env->{'printer'};
187   (return) unless (C4::Context->boolean_preference('printcirculationslips'));
188   if ($printer eq "" || $printer eq 'nulllp') {
189     open (PRINTER,">/tmp/kohares");
190   } else {
191     open (PRINTER, "| lpr -P $printer >/dev/null") or die "Couldn't write to queue:$!\n";
192   }
193   print PRINTER $slip;
194   close PRINTER;
195 }
196
197 END { }       # module clean-up code here (global destructor)
198
199 1;
200 __END__
201
202 =back
203
204 =head1 AUTHOR
205
206 Koha Developement team <info@koha.org>
207
208 =head1 SEE ALSO
209
210 C4::Circulation::Circ2(3)
211
212 =cut