merging rel-1-2 and main
[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 ( ! defined $type ) { $type=''; }
141   if ($type eq 'issue') {
142     open (FILE,"$path/issues-bottom.inc") || die;
143   } elsif ($type eq 'opac') {
144     open (FILE,"$path/opac-bottom.inc") || die;
145   } elsif ($type eq 'member') {
146     open (FILE,"$path/members-bottom.inc") || die;
147   } elsif ($type eq 'acquisitions') {
148     open (FILE,"$path/acquisitions-bottom.inc") || die;
149   } elsif ($type eq 'report') {
150     open (FILE,"$path/reports-bottom.inc") || die;
151   } elsif ($type eq 'circulation') {
152     open (FILE,"$path/circulation-bottom.inc") || die;
153   } else {
154     open (FILE,"$path/cat-bottom.inc") || die;
155   }
156   my @string=<FILE>;
157   close FILE;
158   return @string;
159 }
160
161 sub mktablehdr {
162     return("<table border=0 cellspacing=0 cellpadding=5>\n");
163 }
164
165
166 sub mktablerow {
167     #the last item in data may be a backgroundimage
168     
169     # FIXME
170     # should this be a foreach (1..$cols) loop?
171
172   my ($cols,$colour,@data)=@_;
173   my $i=0;
174   my $string="<tr valign=top bgcolor=$colour>";
175   while ($i <$cols){
176     if ($data[$cols] ne ''){
177     #check for backgroundimage
178       $string.="<td background=\"$data[$cols]\">";
179     } else {
180       $string.="<td>";
181     }
182     if ($data[$i] eq "") {
183       $string.=" &nbsp; </td>";
184     } else {
185       $string.="$data[$i]</td>";
186     } 
187     $i++;
188   }
189   $string=$string."</tr>\n";
190   return($string);
191 }
192
193 sub mktableft {
194   return("</table>\n");
195 }
196
197 sub mkform{
198   my ($action,%inputs)=@_;
199   my $string="<form action=$action method=post>\n";
200   $string=$string.mktablehdr();
201   my $key;
202   my @keys=sort keys %inputs;
203   
204   my $count=@keys;
205   my $i2=0;
206   while ( $i2<$count) {
207     my $value=$inputs{$keys[$i2]};
208     my @data=split('\t',$value);
209     #my $posn = shift(@data);
210     if ($data[0] eq 'hidden'){
211       $string=$string."<input type=hidden name=$keys[$i2] value=\"$data[1]\">\n";
212     } else {
213       my $text;
214       if ($data[0] eq 'radio') {
215         $text="<input type=radio name=$keys[$i2] value=$data[1]>$data[1]
216         <input type=radio name=$keys[$i2] value=$data[2]>$data[2]";
217       } 
218       if ($data[0] eq 'text') {
219         $text="<input type=$data[0] name=$keys[$i2] value=\"$data[1]\">";
220       }
221       if ($data[0] eq 'textarea') {
222         $text="<textarea name=$keys[$i2] wrap=physical cols=40 rows=4>$data[1]</textarea>";
223       }
224       if ($data[0] eq 'select') {
225         $text="<select name=$keys[$i2]>";
226         my $i=1;
227         while ($data[$i] ne "") {
228           my $val = $data[$i+1];
229           $text = $text."<option value=$data[$i]>$val";
230           $i = $i+2;
231         }
232         $text=$text."</select>";
233       } 
234       $string=$string.mktablerow(2,'white',$keys[$i2],$text);
235       #@order[$posn] =mktablerow(2,'white',$keys[$i2],$text);
236     }
237     $i2++;
238   }
239   #$string=$string.join("\n",@order);
240   $string=$string.mktablerow(2,'white','<input type=submit>','<input type=reset>');
241   $string=$string.mktableft;
242   $string=$string."</form>";
243 }
244
245 sub mkform3 {
246   my ($action, %inputs) = @_;
247   my $string = "<form action=\"$action\" method=\"post\">\n";
248   $string   .= mktablehdr();
249   my $key;
250   my @keys = sort(keys(%inputs));
251   my @order;  
252   my $count = @keys;
253   my $i2 = 0;
254   while ($i2 < $count) {
255     my $value=$inputs{$keys[$i2]};
256     my @data=split('\t',$value);
257     my $posn = $data[2];
258     if ($data[0] eq 'hidden'){
259       $order[$posn]="<input type=hidden name=$keys[$i2] value=\"$data[1]\">\n";
260     } else {
261       my $text;
262       if ($data[0] eq 'radio') {
263         $text="<input type=radio name=$keys[$i2] value=$data[1]>$data[1]
264         <input type=radio name=$keys[$i2] value=$data[2]>$data[2]";
265       } 
266       if ($data[0] eq 'text') {
267         $text="<input type=$data[0] name=$keys[$i2] value=\"$data[1]\" size=40>";
268       }
269       if ($data[0] eq 'textarea') {
270         $text="<textarea name=$keys[$i2] cols=40 rows=4>$data[1]</textarea>";
271       }
272       if ($data[0] eq 'select') {
273         $text="<select name=$keys[$i2]>";
274         my $i=1;
275         while ($data[$i] ne "") {
276           my $val = $data[$i+1];
277           $text = $text."<option value=$data[$i]>$val";
278           $i = $i+2;
279         }
280         $text=$text."</select>";
281       } 
282 #      $string=$string.mktablerow(2,'white',$keys[$i2],$text);
283       $order[$posn]=mktablerow(2,'white',$keys[$i2],$text);
284     }
285     $i2++;
286   }
287   my $temp=join("\n",@order);
288   $string=$string.$temp;
289   $string=$string.mktablerow(1,'white','<input type=submit>');
290   $string=$string.mktableft;
291   $string=$string."</form>";
292 }
293
294 sub mkformnotable{
295   my ($action,@inputs)=@_;
296   my $string="<form action=$action method=post>\n";
297   my $count=@inputs;
298   for (my $i=0; $i<$count; $i++){
299     if ($inputs[$i][0] eq 'hidden'){
300       $string=$string."<input type=hidden name=$inputs[$i][1] value=\"$inputs[$i][2]\">\n";
301     }
302     if ($inputs[$i][0] eq 'radio') {
303       $string.="<input type=radio name=$inputs[1] value=$inputs[$i][2]>$inputs[$i][2]";
304     } 
305     if ($inputs[$i][0] eq 'text') {
306       $string.="<input type=$inputs[$i][0] name=$inputs[$i][1] value=\"$inputs[$i][2]\">";
307     }
308     if ($inputs[$i][0] eq 'textarea') {
309         $string.="<textarea name=$inputs[$i][1] wrap=physical cols=40 rows=4>$inputs[$i][2]</textarea>";
310     }
311     if ($inputs[$i][0] eq 'reset'){
312       $string.="<input type=reset name=$inputs[$i][1] value=\"$inputs[$i][2]\">";
313     }    
314     if ($inputs[$i][0] eq 'submit'){
315       $string.="<input type=submit name=$inputs[$i][1] value=\"$inputs[$i][2]\">";
316     }    
317   }
318   $string=$string."</form>";
319 }
320
321 sub mkform2{
322   my ($action,%inputs)=@_;
323   my $string="<form action=$action method=post>\n";
324   $string=$string.mktablehdr();
325   my $key;
326   my @order;
327   while ( my ($key, $value) = each %inputs) {
328     my @data=split('\t',$value);
329     my $posn = shift(@data);
330     my $reqd = shift(@data);
331     my $ltext = shift(@data);    
332     if ($data[0] eq 'hidden'){
333       $string=$string."<input type=hidden name=$key value=\"$data[1]\">\n";
334     } else {
335       my $text;
336       if ($data[0] eq 'radio') {
337         $text="<input type=radio name=$key value=$data[1]>$data[1]
338         <input type=radio name=$key value=$data[2]>$data[2]";
339       } elsif ($data[0] eq 'text') {
340         my $size = $data[1];
341         if ($size eq "") {
342           $size=40;
343         }
344         $text="<input type=$data[0] name=$key size=$size value=\"$data[2]\">";
345       } elsif ($data[0] eq 'textarea') {
346         my @size=split("x",$data[1]);
347         if ($data[1] eq "") {
348           $size[0] = 40;
349           $size[1] = 4;
350         }
351         $text="<textarea name=$key wrap=physical cols=$size[0] rows=$size[1]>$data[2]</textarea>";
352       } elsif ($data[0] eq 'select') {
353         $text="<select name=$key>";
354         my $sel=$data[1];
355         my $i=2;
356         while ($data[$i] ne "") {
357           my $val = $data[$i+1];
358           $text = $text."<option value=\"$data[$i]\"";
359           if ($data[$i] eq $sel) {
360              $text = $text." selected";
361           }   
362           $text = $text.">$val";
363           $i = $i+2;
364         }
365         $text=$text."</select>";
366       }
367       if ($reqd eq "R") {
368         $ltext = $ltext." (Req)";
369         }
370       $order[$posn] =mktablerow(2,'white',$ltext,$text);
371     }
372   }
373   $string=$string.join("\n",@order);
374   $string=$string.mktablerow(2,'white','<input type=submit>','<input type=reset>');
375   $string=$string.mktableft;
376   $string=$string."</form>";
377 }
378
379
380 sub endpage{
381   return("</body></html>\n");
382 }
383
384 sub mklink {
385   my ($url,$text)=@_;
386   my $string="<a href=\"$url\">$text</a>";
387   return ($string);
388 }
389
390 sub mkheadr {
391   my ($type,$text)=@_;
392   my $string;
393   if ($type eq '1'){
394     $string="<FONT SIZE=6><em>$text</em></FONT><br>";
395   }
396   if ($type eq '2'){
397     $string="<FONT SIZE=6><em>$text</em></FONT>";
398   }
399     if ($type eq '3'){
400     $string="<FONT SIZE=6><em>$text</em></FONT><p>";
401   }
402   return ($string);
403 }
404
405 sub center {
406   return ("<CENTER>\n");
407 }  
408
409 sub endcenter {
410   return ("</CENTER>\n");
411 }  
412
413 sub bold {
414   my ($text)=@_;
415   my $string="<b>$text</b>";
416   return($string);
417 }
418
419
420
421
422 END { }       # module clean-up code here (global destructor)
423     
424
425