Now, the API...
[koha.git] / C4 / Output.pm
1 package 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 require Exporter;
9
10 use C4::Database;
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 
19              &mktablehdr &mktableft &mktablerow &mklink
20              &startmenu &endmenu &mkheadr 
21              &center &endcenter 
22              &mkform &mkform2 &bold
23              &gotopage &mkformnotable &mkform3
24              &getkeytableselectoptions
25              &picktemplate);
26 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
27
28 # your exported package globals go here,
29 # as well as any optionally exported functions
30
31 @EXPORT_OK   = qw($Var1 %Hashit);
32
33
34 # non-exported package globals go here
35 use vars qw(@more $stuff);
36
37 # initalize package globals, first exported ones
38
39 my $Var1   = '';
40 my %Hashit = ();
41
42
43 # then the others (which are still accessible as $Some::Module::stuff)
44 my $stuff  = '';
45 my @more   = ();
46
47 # all file-scoped lexicals must be created before
48 # the functions below that use them.
49
50 #
51 # Change this value to reflect where you will store your includes
52 #
53 my %configfile;
54 open (KC, "/etc/koha.conf");
55 while (<KC>) {
56     chomp;
57     (next) if (/^\s*#/);
58     if (/(.*)\s*=\s*(.*)/) {
59         my $variable=$1;
60         my $value=$2;
61
62         $variable =~ s/^\s*//g;
63         $variable =~ s/\s*$//g;
64         $value    =~ s/^\s*//g;
65         $value    =~ s/\s*$//g;
66         $configfile{$variable}=$value;
67     } # if
68 } # while
69 close(KC);
70
71 my $path=$configfile{'includes'};
72 ($path) || ($path="/usr/local/www/hdl/htdocs/includes");
73
74 # make all your functions, whether exported or not;
75
76 sub picktemplate {
77   my ($includes, $base) = @_;
78   my $dbh=C4Connect;
79   my $templates;
80   opendir (D, "$includes/templates");
81   my @dirlist=readdir D;
82   foreach (@dirlist) {
83     (next) if (/^\./);
84     #(next) unless (/\.tmpl$/);
85     (next) unless (-e "$includes/templates/$_/$base");
86     $templates->{$_}=1;
87   }                                                         
88   my $sth=$dbh->prepare("select value from systempreferences where
89   variable='template'");
90   $sth->execute;
91   my ($preftemplate) = $sth->fetchrow;
92   $sth->finish;
93   $dbh->disconnect;
94   if ($templates->{$preftemplate}) {
95     return $preftemplate;
96   } else {
97     return 'default';
98   }
99   
100 }
101                                     
102 sub startpage() {
103   return("<html>\n");
104 }
105
106 sub gotopage($) {
107   my ($target) = shift;
108   #print "<br>goto target = $target<br>";
109   my $string = "<META HTTP-EQUIV=Refresh CONTENT=\"0;URL=http:$target\">";
110   return $string;
111 }
112
113
114 sub startmenu($) {
115   # edit the paths in here
116   my ($type)=shift;
117   if ($type eq 'issue') {
118     open (FILE,"$path/issues-top.inc") || die;
119   } elsif ($type eq 'opac') {
120     open (FILE,"$path/opac-top.inc") || die;
121   } elsif ($type eq 'member') {
122     open (FILE,"$path/members-top.inc") || die;
123   } elsif ($type eq 'acquisitions'){
124     open (FILE,"$path/acquisitions-top.inc") || die;
125   } elsif ($type eq 'report'){
126     open (FILE,"$path/reports-top.inc") || die;
127   } elsif ($type eq 'circulation') {
128     open (FILE,"$path/circulation-top.inc") || die;
129   } else {
130     open (FILE,"$path/cat-top.inc") || die;
131   }
132   my @string=<FILE>;
133   close FILE;
134   # my $count=@string;
135   # $string[$count]="<BLOCKQUOTE>";
136   return @string;
137 }
138
139
140 sub endmenu {
141   my ($type) = @_;
142   if ( ! defined $type ) { $type=''; }
143   if ($type eq 'issue') {
144     open (FILE,"$path/issues-bottom.inc") || die;
145   } elsif ($type eq 'opac') {
146     open (FILE,"$path/opac-bottom.inc") || die;
147   } elsif ($type eq 'member') {
148     open (FILE,"$path/members-bottom.inc") || die;
149   } elsif ($type eq 'acquisitions') {
150     open (FILE,"$path/acquisitions-bottom.inc") || die;
151   } elsif ($type eq 'report') {
152     open (FILE,"$path/reports-bottom.inc") || die;
153   } elsif ($type eq 'circulation') {
154     open (FILE,"$path/circulation-bottom.inc") || die;
155   } else {
156     open (FILE,"$path/cat-bottom.inc") || die;
157   }
158   my @string=<FILE>;
159   close FILE;
160   return @string;
161 }
162
163 sub mktablehdr() {
164     return("<table border=0 cellspacing=0 cellpadding=5>\n");
165 }
166
167
168 sub mktablerow {
169     #the last item in data may be a backgroundimage
170     
171     # FIXME
172     # should this be a foreach (1..$cols) loop?
173
174   my ($cols,$colour,@data)=@_;
175   my $i=0;
176   my $string="<tr valign=top bgcolor=$colour>";
177   while ($i <$cols){
178       if (defined $data[$cols]) { # if there is a background image
179           $string.="<td background=\"$data[$cols]\">";
180       } else { # if there's no background image
181           $string.="<td>";
182       }
183       if (! defined $data[$i]) {$data[$i]="";}
184       if ($data[$i] eq "") {
185           $string.=" &nbsp; </td>";
186       } else {
187           $string.="$data[$i]</td>";
188       } 
189       $i++;
190   }
191   $string=$string."</tr>\n";
192   return($string);
193 }
194
195 sub mktableft() {
196   return("</table>\n");
197 }
198
199 sub mkform{
200   my ($action,%inputs)=@_;
201   my $string="<form action=$action method=post>\n";
202   $string=$string.mktablehdr();
203   my $key;
204   my @keys=sort keys %inputs;
205   
206   my $count=@keys;
207   my $i2=0;
208   while ( $i2<$count) {
209     my $value=$inputs{$keys[$i2]};
210     my @data=split('\t',$value);
211     #my $posn = shift(@data);
212     if ($data[0] eq 'hidden'){
213       $string=$string."<input type=hidden name=$keys[$i2] value=\"$data[1]\">\n";
214     } else {
215       my $text;
216       if ($data[0] eq 'radio') {
217         $text="<input type=radio name=$keys[$i2] value=$data[1]>$data[1]
218         <input type=radio name=$keys[$i2] value=$data[2]>$data[2]";
219       } 
220       if ($data[0] eq 'text') {
221         $text="<input type=$data[0] name=$keys[$i2] value=\"$data[1]\">";
222       }
223       if ($data[0] eq 'textarea') {
224         $text="<textarea name=$keys[$i2] wrap=physical cols=40 rows=4>$data[1]</textarea>";
225       }
226       if ($data[0] eq 'select') {
227         $text="<select name=$keys[$i2]>";
228         my $i=1;
229         while ($data[$i] ne "") {
230           my $val = $data[$i+1];
231           $text = $text."<option value=$data[$i]>$val";
232           $i = $i+2;
233         }
234         $text=$text."</select>";
235       } 
236       $string=$string.mktablerow(2,'white',$keys[$i2],$text);
237       #@order[$posn] =mktablerow(2,'white',$keys[$i2],$text);
238     }
239     $i2++;
240   }
241   #$string=$string.join("\n",@order);
242   $string=$string.mktablerow(2,'white','<input type=submit>','<input type=reset>');
243   $string=$string.mktableft;
244   $string=$string."</form>";
245 }
246
247 sub mkform3 {
248   my ($action, %inputs) = @_;
249   my $string = "<form action=\"$action\" method=\"post\">\n";
250   $string   .= mktablehdr();
251   my $key;
252   my @keys = sort(keys(%inputs));
253   my @order;
254   my $count = @keys;
255   my $i2 = 0;
256   while ($i2 < $count) {
257     my $value=$inputs{$keys[$i2]};
258     my @data=split('\t',$value);
259     my $posn = $data[2];
260     if ($data[0] eq 'hidden'){
261       $order[$posn]="<input type=hidden name=$keys[$i2] value=\"$data[1]\">\n";
262     } else {
263       my $text;
264       if ($data[0] eq 'radio') {
265         $text="<input type=radio name=$keys[$i2] value=$data[1]>$data[1]
266         <input type=radio name=$keys[$i2] value=$data[2]>$data[2]";
267       } 
268       if ($data[0] eq 'text') {
269         $text="<input type=$data[0] name=$keys[$i2] value=\"$data[1]\" size=40>";
270       }
271       if ($data[0] eq 'textarea') {
272         $text="<textarea name=$keys[$i2] cols=40 rows=4>$data[1]</textarea>";
273       }
274       if ($data[0] eq 'select') {
275         $text="<select name=$keys[$i2]>";
276         my $i=1;
277         while ($data[$i] ne "") {
278           my $val = $data[$i+1];
279           $text = $text."<option value=$data[$i]>$val";
280           $i = $i+2;
281         }
282         $text=$text."</select>";
283       } 
284 #      $string=$string.mktablerow(2,'white',$keys[$i2],$text);
285       $order[$posn]=mktablerow(2,'white',$keys[$i2],$text);
286     }
287     $i2++;
288   }
289   my $temp=join("\n",@order);
290   $string=$string.$temp;
291   $string=$string.mktablerow(1,'white','<input type=submit>');
292   $string=$string.mktableft;
293   $string=$string."</form>";
294 }
295
296 sub mkformnotable{
297   my ($action,@inputs)=@_;
298   my $string="<form action=$action method=post>\n";
299   my $count=@inputs;
300   for (my $i=0; $i<$count; $i++){
301     if ($inputs[$i][0] eq 'hidden'){
302       $string=$string."<input type=hidden name=$inputs[$i][1] value=\"$inputs[$i][2]\">\n";
303     }
304     if ($inputs[$i][0] eq 'radio') {
305       $string.="<input type=radio name=$inputs[1] value=$inputs[$i][2]>$inputs[$i][2]";
306     } 
307     if ($inputs[$i][0] eq 'text') {
308       $string.="<input type=$inputs[$i][0] name=$inputs[$i][1] value=\"$inputs[$i][2]\">";
309     }
310     if ($inputs[$i][0] eq 'textarea') {
311         $string.="<textarea name=$inputs[$i][1] wrap=physical cols=40 rows=4>$inputs[$i][2]</textarea>";
312     }
313     if ($inputs[$i][0] eq 'reset'){
314       $string.="<input type=reset name=$inputs[$i][1] value=\"$inputs[$i][2]\">";
315     }    
316     if ($inputs[$i][0] eq 'submit'){
317       $string.="<input type=submit name=$inputs[$i][1] value=\"$inputs[$i][2]\">";
318     }    
319   }
320   $string=$string."</form>";
321 }
322
323 sub mkform2{
324     # FIXME
325     # no POD and no tests yet.  Once tests are written,
326     # this function can be cleaned up with the following steps:
327     #  turn the while loop into a foreach loop
328     #  pull the nested if,elsif structure back up to the main level
329     #  pull the code for the different kinds of inputs into separate
330     #   functions
331   my ($action,%inputs)=@_;
332   my $string="<form action=$action method=post>\n";
333   $string=$string.mktablehdr();
334   my $key;
335   my @order;
336   while ( my ($key, $value) = each %inputs) {
337     my @data=split('\t',$value);
338     my $posn = shift(@data);
339     my $reqd = shift(@data);
340     my $ltext = shift(@data);    
341     if ($data[0] eq 'hidden'){
342       $string=$string."<input type=hidden name=$key value=\"$data[1]\">\n";
343     } else {
344       my $text;
345       if ($data[0] eq 'radio') {
346         $text="<input type=radio name=$key value=$data[1]>$data[1]
347         <input type=radio name=$key value=$data[2]>$data[2]";
348       } elsif ($data[0] eq 'text') {
349         my $size = $data[1];
350         if ($size eq "") {
351           $size=40;
352         }
353         $text="<input type=$data[0] name=$key size=$size value=\"$data[2]\">";
354       } elsif ($data[0] eq 'textarea') {
355         my @size=split("x",$data[1]);
356         if ($data[1] eq "") {
357           $size[0] = 40;
358           $size[1] = 4;
359         }
360         $text="<textarea name=$key wrap=physical cols=$size[0] rows=$size[1]>$data[2]</textarea>";
361       } elsif ($data[0] eq 'select') {
362         $text="<select name=$key>";
363         my $sel=$data[1];
364         my $i=2;
365         while ($data[$i] ne "") {
366           my $val = $data[$i+1];
367           $text = $text."<option value=\"$data[$i]\"";
368           if ($data[$i] eq $sel) {
369              $text = $text." selected";
370           }   
371           $text = $text.">$val";
372           $i = $i+2;
373         }
374         $text=$text."</select>";
375       }
376       if ($reqd eq "R") {
377         $ltext = $ltext." (Req)";
378         }
379       $order[$posn] =mktablerow(2,'white',$ltext,$text);
380     }
381   }
382   $string=$string.join("\n",@order);
383   $string=$string.mktablerow(2,'white','<input type=submit>','<input type=reset>');
384   $string=$string.mktableft;
385   $string=$string."</form>";
386 }
387
388 =pod
389
390 =head2 &endpage
391
392  &endpage does not expect any arguments, it returns the string:
393    </body></html>\n
394
395 =cut
396
397 sub endpage() {
398   return("</body></html>\n");
399 }
400
401 =pod
402
403 =head2 &mklink
404
405  &mklink expects two arguments, the url to link to and the text of the link.
406  It returns this string:
407    <a href="$url">$text</a>
408  where $url is the first argument and $text is the second.
409
410 =cut
411
412 sub mklink($$) {
413   my ($url,$text)=@_;
414   my $string="<a href=\"$url\">$text</a>";
415   return ($string);
416 }
417
418 =pod
419
420 =head2 &mkheadr
421
422  &mkeadr expects two strings, a type and the text to use in the header.
423  types are:
424
425 =over
426
427 =item 1  ends with <br>
428
429 =item 2  no special ending tag
430
431 =item 3  ends with <p>
432
433 =back
434
435  Other than this, the return value is the same:
436    <FONT SIZE=6><em>$text</em></FONT>$string
437  Where $test is the text passed in and $string is the tag generated from 
438  the type value.
439
440 =cut
441
442 sub mkheadr {
443     # FIXME
444     # would it be better to make this more generic by accepting an optional
445     # argument with a closing tag instead of a numeric type?
446
447   my ($type,$text)=@_;
448   my $string;
449   if ($type eq '1'){
450     $string="<FONT SIZE=6><em>$text</em></FONT><br>";
451   }
452   if ($type eq '2'){
453     $string="<FONT SIZE=6><em>$text</em></FONT><br>";
454   }
455   if ($type eq '3'){
456     $string="<FONT SIZE=6><em>$text</em></FONT><p>";
457   }
458   return ($string);
459 }
460
461 =pod
462
463 =head2 &center and &endcenter
464
465  &center and &endcenter take no arguments and return html tags <CENTER> and
466  </CENTER> respectivley.
467
468 =cut
469
470 sub center() {
471   return ("<CENTER>\n");
472 }  
473
474 sub endcenter() {
475   return ("</CENTER>\n");
476 }  
477
478 =pod
479
480 =head2 &bold
481
482  &bold requires that a single string be passed in by the caller.  &bold 
483  will return "<b>$text</b>" where $text is the string passed in.
484
485 =cut
486
487 sub bold($) {
488   my ($text)=shift;
489   return("<b>$text</b>");
490 }
491
492 #---------------------------------------------
493 # Create an HTML option list for a <SELECT> form tag by using
494 #    values from a DB file
495 sub getkeytableselectoptions {
496         use strict;
497         # inputs
498         my (
499                 $dbh,           # DBI handle
500                 $tablename,     # name of table containing list of choices
501                 $keyfieldname,  # column name of code to use in option list
502                 $descfieldname, # column name of descriptive field
503                 $showkey,       # flag to show key in description
504                 $default,       # optional default key
505         )=@_;
506         my $selectclause;       # return value
507
508         my (
509                 $sth, $query, 
510                 $key, $desc, $orderfieldname,
511         );
512         my $debug=0;
513
514         requireDBI($dbh,"getkeytableselectoptions");
515
516         if ( $showkey ) {
517                 $orderfieldname=$keyfieldname;
518         } else {
519                 $orderfieldname=$descfieldname;
520         }
521         $query= "select $keyfieldname,$descfieldname
522                 from $tablename
523                 order by $orderfieldname ";
524         print "<PRE>Query=$query </PRE>\n" if $debug; 
525         $sth=$dbh->prepare($query);
526         $sth->execute;
527         while ( ($key, $desc) = $sth->fetchrow) {
528             if ($showkey || ! $desc ) { $desc="$key - $desc"; }
529             $selectclause.="<option";
530             if (defined $default && $default eq $key) {
531                 $selectclause.=" selected";
532             }
533             $selectclause.=" value='$key'>$desc\n";
534             print "<PRE>Sel=$selectclause </PRE>\n" if $debug; 
535         }
536         return $selectclause;
537 } # sub getkeytableselectoptions
538
539 #---------------------------------
540
541 END { }       # module clean-up code here (global destructor)
542     
543
544