Fixed a few warnings.
[koha.git] / C4 / Output.pm
1 package C4::Output; #asummes C4/Output
2
3 #package to deal with marking up output
4 #You will need to edit parts of this pm
5 #set the value of path to be where your html lives
6
7 use strict;
8 use warnings;
9 use C4::Database;
10 require Exporter;
11
12 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
13
14 # set the version for version checking
15 $VERSION = 0.01;
16
17 @ISA = qw(Exporter);
18 @EXPORT = qw(&startpage &endpage &mktablehdr &mktableft &mktablerow &mklink
19 &startmenu &endmenu &mkheadr &center &endcenter &mkform &mkform2 &bold
20 &gotopage &mkformnotable &mkform3 picktemplate);
21 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
22
23 # your exported package globals go here,
24 # as well as any optionally exported functions
25
26 @EXPORT_OK   = qw($Var1 %Hashit);
27
28
29 # non-exported package globals go here
30 use vars qw(@more $stuff);
31
32 # initalize package globals, first exported ones
33
34 my $Var1   = '';
35 my %Hashit = ();
36
37
38 # then the others (which are still accessible as $Some::Module::stuff)
39 my $stuff  = '';
40 my @more   = ();
41
42 # all file-scoped lexicals must be created before
43 # the functions below that use them.
44
45 #
46 # Change this value to reflect where you will store your includes
47 #
48 my %configfile;
49 open (KC, "/etc/koha.conf");
50 while (<KC>) {
51     chomp;
52     (next) if (/^\s*#/);
53     if (/(.*)\s*=\s*(.*)/) {
54         my $variable=$1;
55         my $value=$2;
56
57         $variable =~ s/^\s*//g;
58         $variable =~ s/\s*$//g;
59         $value    =~ s/^\s*//g;
60         $value    =~ s/\s*$//g;
61         $configfile{$variable}=$value;
62     } # if
63 } # while
64 close(KC);
65
66 my $path=$configfile{'includes'};
67 ($path) || ($path="/usr/local/www/hdl/htdocs/includes");
68
69 # make all your functions, whether exported or not;
70
71 sub picktemplate {
72   my ($includes, $base) = @_;
73   my $dbh=C4Connect;
74   my $templates;
75   opendir (D, "$includes/templates");
76   my @dirlist=readdir D;
77   foreach (@dirlist) {
78     (next) if (/^\./);
79     #(next) unless (/\.tmpl$/);
80     (next) unless (-e "$includes/templates/$_/$base");
81     $templates->{$_}=1;
82   }                                                         
83   my $sth=$dbh->prepare("select value from systempreferences where
84   variable='template'");
85   $sth->execute;
86   my ($preftemplate) = $sth->fetchrow;
87   $sth->finish;
88   $dbh->disconnect;
89   if ($templates->{$preftemplate}) {
90     return $preftemplate;
91   } else {
92     return 'default';
93   }
94   
95 }
96                                     
97
98
99 sub startpage{
100   return("<html>\n");
101 }
102
103 sub gotopage{
104   my ($target) = @_;
105   print "<br>goto target = $target<br>";
106   my $string = "<META HTTP-EQUIV=Refresh CONTENT=\"0;URL=http:$target\">";
107   return $string;
108 }
109
110
111 sub startmenu{
112   # edit the paths in here
113   my ($type)=@_;
114   if ($type eq 'issue') {
115     open (FILE,"$path/issues-top.inc") || die;
116   } elsif ($type eq 'opac') {
117     open (FILE,"$path/opac-top.inc") || die;
118   } elsif ($type eq 'member') {
119     open (FILE,"$path/members-top.inc") || die;
120   } elsif ($type eq 'acquisitions'){
121     open (FILE,"$path/acquisitions-top.inc")
122       || die "Cannot open $path/acquisitions-top.inc";
123   } elsif ($type eq 'report'){
124     open (FILE,"$path/reports-top.inc") || die;
125   } elsif ($type eq 'circulation') {
126     open (FILE,"$path/circulation-top.inc") || die;
127   } else {
128     open (FILE,"$path/cat-top.inc") || die;
129   }
130   my @string=<FILE>;
131   close FILE;
132   my $count=@string;
133   #  $string[$count]="<BLOCKQUOTE>";
134   return @string;
135 }
136
137
138 sub endmenu {
139   my ($type) = @_;
140   if ($type eq 'issue') {
141     open (FILE,"$path/issues-bottom.inc") || die;
142   } elsif ($type eq 'opac') {
143     open (FILE,"$path/opac-bottom.inc") || die;
144   } elsif ($type eq 'member') {
145     open (FILE,"$path/members-bottom.inc") || die;
146   } elsif ($type eq 'acquisitions') {
147     open (FILE,"$path/acquisitions-bottom.inc") || die;
148   } elsif ($type eq 'report') {
149     open (FILE,"$path/reports-bottom.inc") || die;
150   } elsif ($type eq 'circulation') {
151     open (FILE,"$path/circulation-bottom.inc") || die;
152   } else {
153     open (FILE,"$path/cat-bottom.inc") || die;
154   }
155   my @string=<FILE>;
156   close FILE;
157   return @string;
158 }
159
160 sub mktablehdr {
161     return("<table border=0 cellspacing=0 cellpadding=5>\n");
162 }
163
164
165 sub mktablerow {
166     #the last item in data may be a backgroundimage
167     
168     # FIXME
169     # should this be a foreach (1..$cols) loop?
170
171   my ($cols,$colour,@data)=@_;
172   my $i=0;
173   my $string="<tr valign=top bgcolor=$colour>";
174   while ($i <$cols){
175     if ($data[$cols] ne ''){
176     #check for backgroundimage
177       $string.="<td background=\"$data[$cols]\">";
178     } else {
179       $string.="<td>";
180     }
181     if ($data[$i] eq "") {
182       $string.=" &nbsp; </td>";
183     } else {
184       $string.="$data[$i]</td>";
185     } 
186     $i++;
187   }
188   $string=$string."</tr>\n";
189   return($string);
190 }
191
192 sub mktableft {
193   return("</table>\n");
194 }
195
196 sub mkform{
197   my ($action,%inputs)=@_;
198   my $string="<form action=$action method=post>\n";
199   $string=$string.mktablehdr();
200   my $key;
201   my @keys=sort keys %inputs;
202   
203   my $count=@keys;
204   my $i2=0;
205   while ( $i2<$count) {
206     my $value=$inputs{$keys[$i2]};
207     my @data=split('\t',$value);
208     #my $posn = shift(@data);
209     if ($data[0] eq 'hidden'){
210       $string=$string."<input type=hidden name=$keys[$i2] value=\"$data[1]\">\n";
211     } else {
212       my $text;
213       if ($data[0] eq 'radio') {
214         $text="<input type=radio name=$keys[$i2] value=$data[1]>$data[1]
215         <input type=radio name=$keys[$i2] value=$data[2]>$data[2]";
216       } 
217       if ($data[0] eq 'text') {
218         $text="<input type=$data[0] name=$keys[$i2] value=\"$data[1]\">";
219       }
220       if ($data[0] eq 'textarea') {
221         $text="<textarea name=$keys[$i2] wrap=physical cols=40 rows=4>$data[1]</textarea>";
222       }
223       if ($data[0] eq 'select') {
224         $text="<select name=$keys[$i2]>";
225         my $i=1;
226         while ($data[$i] ne "") {
227           my $val = $data[$i+1];
228           $text = $text."<option value=$data[$i]>$val";
229           $i = $i+2;
230         }
231         $text=$text."</select>";
232       } 
233       $string=$string.mktablerow(2,'white',$keys[$i2],$text);
234       #@order[$posn] =mktablerow(2,'white',$keys[$i2],$text);
235     }
236     $i2++;
237   }
238   #$string=$string.join("\n",@order);
239   $string=$string.mktablerow(2,'white','<input type=submit>','<input type=reset>');
240   $string=$string.mktableft;
241   $string=$string."</form>";
242 }
243
244 sub mkform3 {
245   my ($action, %inputs) = @_;
246   my $string = "<form action=\"$action\" method=\"post\">\n";
247   $string   .= mktablehdr();
248   my $key;
249   my @keys = sort(keys(%inputs));
250   my @order;  
251   my $count = @keys;
252   my $i2 = 0;
253   while ($i2 < $count) {
254     my $value=$inputs{$keys[$i2]};
255     my @data=split('\t',$value);
256     my $posn = $data[2];
257     if ($data[0] eq 'hidden'){
258       $order[$posn]="<input type=hidden name=$keys[$i2] value=\"$data[1]\">\n";
259     } else {
260       my $text;
261       if ($data[0] eq 'radio') {
262         $text="<input type=radio name=$keys[$i2] value=$data[1]>$data[1]
263         <input type=radio name=$keys[$i2] value=$data[2]>$data[2]";
264       } 
265       if ($data[0] eq 'text') {
266         $text="<input type=$data[0] name=$keys[$i2] value=\"$data[1]\" size=40>";
267       }
268       if ($data[0] eq 'textarea') {
269         $text="<textarea name=$keys[$i2] cols=40 rows=4>$data[1]</textarea>";
270       }
271       if ($data[0] eq 'select') {
272         $text="<select name=$keys[$i2]>";
273         my $i=1;
274         while ($data[$i] ne "") {
275           my $val = $data[$i+1];
276           $text = $text."<option value=$data[$i]>$val";
277           $i = $i+2;
278         }
279         $text=$text."</select>";
280       } 
281 #      $string=$string.mktablerow(2,'white',$keys[$i2],$text);
282       $order[$posn]=mktablerow(2,'white',$keys[$i2],$text);
283     }
284     $i2++;
285   }
286   my $temp=join("\n",@order);
287   $string=$string.$temp;
288   $string=$string.mktablerow(1,'white','<input type=submit>');
289   $string=$string.mktableft;
290   $string=$string."</form>";
291 }
292
293 sub mkformnotable{
294   my ($action,@inputs)=@_;
295   my $string="<form action=$action method=post>\n";
296   my $count=@inputs;
297   for (my $i=0; $i<$count; $i++){
298     if ($inputs[$i][0] eq 'hidden'){
299       $string=$string."<input type=hidden name=$inputs[$i][1] value=\"$inputs[$i][2]\">\n";
300     }
301     if ($inputs[$i][0] eq 'radio') {
302       $string.="<input type=radio name=$inputs[1] value=$inputs[$i][2]>$inputs[$i][2]";
303     } 
304     if ($inputs[$i][0] eq 'text') {
305       $string.="<input type=$inputs[$i][0] name=$inputs[$i][1] value=\"$inputs[$i][2]\">";
306     }
307     if ($inputs[$i][0] eq 'textarea') {
308         $string.="<textarea name=$inputs[$i][1] wrap=physical cols=40 rows=4>$inputs[$i][2]</textarea>";
309     }
310     if ($inputs[$i][0] eq 'reset'){
311       $string.="<input type=reset name=$inputs[$i][1] value=\"$inputs[$i][2]\">";
312     }    
313     if ($inputs[$i][0] eq 'submit'){
314       $string.="<input type=submit name=$inputs[$i][1] value=\"$inputs[$i][2]\">";
315     }    
316   }
317   $string=$string."</form>";
318 }
319
320 sub mkform2{
321   my ($action,%inputs)=@_;
322   my $string="<form action=$action method=post>\n";
323   $string=$string.mktablehdr();
324   my $key;
325   my @order;
326   while ( my ($key, $value) = each %inputs) {
327     my @data=split('\t',$value);
328     my $posn = shift(@data);
329     my $reqd = shift(@data);
330     my $ltext = shift(@data);    
331     if ($data[0] eq 'hidden'){
332       $string=$string."<input type=hidden name=$key value=\"$data[1]\">\n";
333     } else {
334       my $text;
335       if ($data[0] eq 'radio') {
336         $text="<input type=radio name=$key value=$data[1]>$data[1]
337         <input type=radio name=$key value=$data[2]>$data[2]";
338       } elsif ($data[0] eq 'text') {
339         my $size = $data[1];
340         if ($size eq "") {
341           $size=40;
342         }
343         $text="<input type=$data[0] name=$key size=$size value=\"$data[2]\">";
344       } elsif ($data[0] eq 'textarea') {
345         my @size=split("x",$data[1]);
346         if ($data[1] eq "") {
347           $size[0] = 40;
348           $size[1] = 4;
349         }
350         $text="<textarea name=$key wrap=physical cols=$size[0] rows=$size[1]>$data[2]</textarea>";
351       } elsif ($data[0] eq 'select') {
352         $text="<select name=$key>";
353         my $sel=$data[1];
354         my $i=2;
355         while ($data[$i] ne "") {
356           my $val = $data[$i+1];
357           $text = $text."<option value=\"$data[$i]\"";
358           if ($data[$i] eq $sel) {
359              $text = $text." selected";
360           }   
361           $text = $text.">$val";
362           $i = $i+2;
363         }
364         $text=$text."</select>";
365       }
366       if ($reqd eq "R") {
367         $ltext = $ltext." (Req)";
368         }
369       $order[$posn] =mktablerow(2,'white',$ltext,$text);
370     }
371   }
372   $string=$string.join("\n",@order);
373   $string=$string.mktablerow(2,'white','<input type=submit>','<input type=reset>');
374   $string=$string.mktableft;
375   $string=$string."</form>";
376 }
377
378
379 sub endpage{
380   return("</body></html>\n");
381 }
382
383 sub mklink {
384   my ($url,$text)=@_;
385   my $string="<a href=\"$url\">$text</a>";
386   return ($string);
387 }
388
389 sub mkheadr {
390   my ($type,$text)=@_;
391   my $string;
392   if ($type eq '1'){
393     $string="<FONT SIZE=6><em>$text</em></FONT><br>";
394   }
395   if ($type eq '2'){
396     $string="<FONT SIZE=6><em>$text</em></FONT>";
397   }
398     if ($type eq '3'){
399     $string="<FONT SIZE=6><em>$text</em></FONT><p>";
400   }
401   return ($string);
402 }
403
404 sub center {
405   return ("<CENTER>\n");
406 }  
407
408 sub endcenter {
409   return ("</CENTER>\n");
410 }  
411
412 sub bold {
413   my ($text)=@_;
414   my $string="<b>$text</b>";
415   return($string);
416 }
417
418
419
420
421 END { }       # module clean-up code here (global destructor)
422     
423
424