1 |
#!/usr/bin/perl -w |
2 |
# |
3 |
# Dobrica Pavlinusic <dpavlin@rot13.org> |
4 |
# |
5 |
# Originally made for proof. during April 2001; later released under GPL v2 |
6 |
# |
7 |
# 2003-04-dd general cleanup in preparation of release |
8 |
|
9 |
use strict; |
10 |
|
11 |
use XML::Parser; |
12 |
use common; |
13 |
use Carp; |
14 |
|
15 |
$|=1; |
16 |
|
17 |
my $Usage =<<'End_of_Usage;'; |
18 |
I will write usage information here. I promise! |
19 |
End_of_Usage; |
20 |
|
21 |
my @Modes = qw(object pass skip); |
22 |
|
23 |
my $poll; |
24 |
my $dowarn = 1; |
25 |
|
26 |
my $q_type = "q"; # q=question, u=unnumbered question |
27 |
my %question_nr; # curr. question numbers |
28 |
my $question_tag = ""; # originalni oblik broja questions |
29 |
my $page_nr = 1; # prvo question na strani |
30 |
|
31 |
my $p_suffix=""; # if more than one box per question |
32 |
|
33 |
my $curr_suffix=""; # trenutni suffix |
34 |
|
35 |
my @stack_que; # stack of questions (question, suffix) |
36 |
|
37 |
my @sql_create = ("id serial", |
38 |
"http_referer character varying(500)", |
39 |
"remote_addr character varying(15)", |
40 |
"user_agent character varying(300)", |
41 |
"unesen timestamp DEFAULT now()", |
42 |
"member_id int4 NOT NULL" |
43 |
); |
44 |
my @sql_update; |
45 |
my @last_sql_update; |
46 |
my @prelast_sql_update; |
47 |
|
48 |
my @php_addon; # php code to add on page header |
49 |
|
50 |
my ($last_fn,$last_page); |
51 |
|
52 |
# this is unique prefix for this installation |
53 |
my $prefix="wopi_"; |
54 |
|
55 |
# this is usename in database |
56 |
my $db_user="dpavlin"; |
57 |
|
58 |
# This option allows users to fill poll without using invitation URL. |
59 |
# That also means it's unpossible for them to return to exiting poll |
60 |
# because they don't have thair own unique ID. Howver, it enables simple |
61 |
# polls to be conducted by just publishing URL to them. |
62 |
my $without_invitation=0; |
63 |
|
64 |
# This will remove numbers before answers. That enables you to have |
65 |
# answers written like: |
66 |
# 1.1 red |
67 |
# 1.2 black |
68 |
# and users will see just "red" and "black" |
69 |
my $remove_nrs_in_answers=0; |
70 |
|
71 |
# This defines files which will be included in various places to produce |
72 |
# design. You could desing them using your faviourite html editor (vim :-) |
73 |
# and then split them into separate files |
74 |
|
75 |
my %include_files = ( |
76 |
# this file is included at top of each paAge |
77 |
'header' => "header.html", |
78 |
# this file is used to separate questions |
79 |
'separator' => "separator.html", |
80 |
# this file is used to show "submit" button, which under multi-page |
81 |
# polls will also bring next page |
82 |
'submit' => "next.html", |
83 |
# this file is included at bottom of each page |
84 |
'footer' => "footer.html", |
85 |
# this file will be showen after poll is completed |
86 |
'thanks' => "thanks.html" |
87 |
); |
88 |
|
89 |
# buffer for suck(_file)ed html files |
90 |
# and additional markup before and after tags |
91 |
my %html = ( |
92 |
'hr_before' => "<br></td></tr>", |
93 |
'hr_after' => "<tr><td></td><td><br>", |
94 |
'que_before' => "<p>", |
95 |
'que_after' => "</p>", |
96 |
'subque_before' => '<table width="100%" cellspacing="0" cellpadding="2" border="0">', |
97 |
'subque_after' => "</table>", |
98 |
'ans_before' => "<p>", |
99 |
'ans_after' => "</p>", |
100 |
'html_before' => "<p>", |
101 |
'html_after' => "</p>", |
102 |
|
103 |
); |
104 |
|
105 |
# name of database colums |
106 |
# for questions |
107 |
my $q_db_col = "q"; |
108 |
# for unnumbered questions |
109 |
my $u_db_col = "u"; |
110 |
|
111 |
|
112 |
#------------------------------------------------------------------ |
113 |
|
114 |
sub suck_file { |
115 |
my $file = shift || croak "suck_file called without argument"; |
116 |
open(H,$file) || die "can't open '$file': $!"; |
117 |
my $content; |
118 |
while (<H>) { $content .= $_; } ; |
119 |
close(H); |
120 |
return $content; |
121 |
} |
122 |
|
123 |
$html{'header'}=suck_file($include_files{'header'}); |
124 |
$html{'separator'}=suck_file($include_files{'separator'}); |
125 |
$html{'submit'}=suck_file($include_files{'submit'}); |
126 |
$html{'footer'}=suck_file($include_files{'footer'}); |
127 |
|
128 |
#------------------------------------------------------------------ |
129 |
|
130 |
sub php_header { |
131 |
my ($page_nr,@sql_update) = @_; |
132 |
my $out='<?php |
133 |
include_once("common.php"); |
134 |
if (isset($update)) { |
135 |
$member_id=id_decode($a); |
136 |
'; |
137 |
$out.=$php_addon[$page_nr-2] if (defined $php_addon[$page_nr-2]); |
138 |
$out.=' |
139 |
$sql="update '.$poll.' set '.join(",\n",@sql_update).', |
140 |
do_stranice=\'$PHP_SELF\' |
141 |
where id=$id"; |
142 |
# print "<pre>$sql</pre>"; |
143 |
$result=pg_Exec($conn,fix_sql($sql)); |
144 |
} elseif($do_stranice != $PHP_SELF && isset($do_uri) && isset($a)) { |
145 |
Header("Location: $do_uri?a=$a"); |
146 |
exit; |
147 |
} |
148 |
?>'; |
149 |
return $out; |
150 |
} |
151 |
|
152 |
#------------------------------------------------------------------ |
153 |
|
154 |
# first, define some constants |
155 |
my $common_php = suck_file("common.php"); |
156 |
|
157 |
#------------------------------------------------------------------ |
158 |
|
159 |
my $head_php=suck_file("head.php"); |
160 |
|
161 |
#------------------------------------------------------------------ |
162 |
|
163 |
$html{'thanks'}=suck_file($include_files{'thanks'}); |
164 |
|
165 |
#------------------------------------------------------------------ |
166 |
|
167 |
while (defined($ARGV[0]) and $ARGV[0] =~ /^-/) { |
168 |
my $opt = shift; |
169 |
|
170 |
if ($opt eq '-h') { |
171 |
print $Usage; |
172 |
exit; |
173 |
} |
174 |
} # End of option processing |
175 |
|
176 |
my $xmlfile = shift; |
177 |
|
178 |
die "No poll xml file provided!\n$Usage" unless defined $xmlfile; |
179 |
|
180 |
die "Can't read $xmlfile" unless -r $xmlfile; |
181 |
|
182 |
if (defined $poll) { |
183 |
die "$poll isn't a directory" unless -d $poll; |
184 |
} |
185 |
else { |
186 |
$xmlfile =~ m!([^/.]+)(?:\.[^/.]*)?$!; |
187 |
$poll = $1; |
188 |
if (-e $poll) { |
189 |
die "$poll exists but isn't a directory" |
190 |
unless -d $poll; |
191 |
} |
192 |
else { |
193 |
mkdir $poll, 0755; |
194 |
} |
195 |
} |
196 |
|
197 |
my $in_poll = 0; |
198 |
my $after_head = 0; |
199 |
|
200 |
my $Mode = 0; |
201 |
my $Mode_level = 0; |
202 |
|
203 |
my $Text; |
204 |
my $Markedup_Text; |
205 |
my $Object; |
206 |
my @Ostack = (); |
207 |
|
208 |
#my $intext = 0; |
209 |
my $closure; |
210 |
my @closure_stack = (); |
211 |
|
212 |
#my $style_link = ''; |
213 |
|
214 |
#my $index = 'index.html'; |
215 |
#my @slidetitle; |
216 |
my $body; |
217 |
#my $inlist = 0; |
218 |
|
219 |
#my @Titles; |
220 |
|
221 |
my $header; |
222 |
|
223 |
my $page_number = 0; |
224 |
|
225 |
my $p = new XML::Parser(ErrorContext => 3, |
226 |
Handlers => {Start => \&starthndl, |
227 |
End => \&endhndl, |
228 |
Char => \&text}); |
229 |
$p->parsefile($xmlfile); |
230 |
|
231 |
#---------------------------------------------------------- |
232 |
|
233 |
# dump last php page.... |
234 |
|
235 |
print "p[$page_nr] "; |
236 |
|
237 |
open(PAGE, ">$poll/$last_fn") or die "Couldn't open $last_fn for writing:\n$!"; |
238 |
print PAGE php_header($page_nr,@prelast_sql_update); |
239 |
my $next_fn=sprintf("%02d.php",$page_nr); |
240 |
$last_page=~s/##NEXTPAGE##/$next_fn/; |
241 |
print PAGE $last_page; |
242 |
close(PAGE); |
243 |
|
244 |
$page_nr++; |
245 |
open(PAGE, ">$poll/$next_fn") or die "Couldn't open $next_fn for writing:\n$!"; |
246 |
print PAGE php_header($page_nr,@last_sql_update); |
247 |
print PAGE "$html{'header'} $html{'thanks'} $html{'footer'}"; |
248 |
close(PAGE); |
249 |
|
250 |
# dump sql structure |
251 |
|
252 |
open(SQL,">$poll/$poll.sql") || die "$poll.sql: $!"; |
253 |
print SQL "drop database ".$prefix.$poll.";\n"; |
254 |
print SQL "create database ".$prefix.$poll.";\n"; |
255 |
print SQL "\\connect ".$prefix.$poll.";\n"; |
256 |
print SQL "create table poslani ( member_id int4 not null, unesen timestamp default now() );\n"; |
257 |
print SQL "create table $poll (do_stranice text default null, ",join(",\n",@sql_create),");\n"; |
258 |
close(SQL); |
259 |
|
260 |
# dump common.php |
261 |
|
262 |
open(PHP,">$poll/common.php") || die "common.php: $!"; |
263 |
$common_php =~ s/##DB##/$poll/g; |
264 |
my $db_name = $prefix.$poll; |
265 |
$common_php =~ s/##DB_NAME##/$db_name/g; |
266 |
$common_php =~ s/##PREFIX##/$prefix/g; |
267 |
$common_php =~ s/##DB_USER##/$db_user/g; |
268 |
$common_php =~ s/##PREFIX##/$prefix/g; |
269 |
my $members_db = $prefix."members"; |
270 |
$common_php =~ s/##MEMBERS_DB##/$members_db/g; |
271 |
$common_php =~ s/##WITHOUT_INVITATION##/$without_invitation/g; |
272 |
|
273 |
print PHP $common_php; |
274 |
close(PHP); |
275 |
|
276 |
open(PHP,">$poll/head.php") || die "head.php: $!"; |
277 |
my $max_page = $page_nr - 1; |
278 |
$head_php=~ s/##MAXPAGE##/$max_page/; |
279 |
$head_php=~ s/##TEXT##/Ispunili ste %02d%% ankete/; |
280 |
print PHP $head_php; |
281 |
close(PHP); |
282 |
|
283 |
# 01.php -> index.php |
284 |
rename "$poll/01.php","$poll/index.php" || die "can't rename '$poll/01.php' to index.php"; |
285 |
|
286 |
################ |
287 |
## End of main |
288 |
################ |
289 |
|
290 |
# return unique name of question |
291 |
sub new_que { |
292 |
my $out=$q_type.( $question_nr{$q_type} || 0 ); |
293 |
$out .= "_".$p_suffix if ($p_suffix); |
294 |
$curr_suffix=$p_suffix; |
295 |
$p_suffix++; |
296 |
return $out; |
297 |
} |
298 |
|
299 |
# current question |
300 |
sub curr_que { |
301 |
return $q_type.( $question_nr{$q_type} || 0 ).$curr_suffix; |
302 |
} |
303 |
|
304 |
#---------------------------------------------------------- |
305 |
|
306 |
sub starthndl { |
307 |
my ($xp, $el, %atts) = @_; |
308 |
|
309 |
# return unless ($in_poll or $el eq 'slideshow'); |
310 |
|
311 |
unless ($in_poll) { |
312 |
$in_poll = $xp->depth + 1; |
313 |
return; |
314 |
} |
315 |
|
316 |
if ($Mode) { |
317 |
if ($Mode eq 'pass') { |
318 |
$Markedup_Text .= "\n" . $xp->recognized_string; |
319 |
} elsif ($Mode eq 'object') { |
320 |
push(@Ostack, $Object); |
321 |
|
322 |
$Object = { |
323 |
_Atts => \%atts, |
324 |
_Text => '' |
325 |
}; |
326 |
bless $Object, "Slideobj::$el"; |
327 |
} |
328 |
|
329 |
# skip does nothing |
330 |
return; |
331 |
} |
332 |
|
333 |
unless ($after_head) { |
334 |
if ($el eq 'head') { |
335 |
$after_head = 1; |
336 |
start_mode($xp, 'object'); |
337 |
|
338 |
push(@closure_stack, $closure); |
339 |
$closure = sub { |
340 |
my ($xp, $text) = @_; |
341 |
|
342 |
unless (defined $text) { |
343 |
$header = $Object; |
344 |
} |
345 |
}; |
346 |
return; |
347 |
} |
348 |
|
349 |
# die "The head element must be the first thing in the slideshow"; |
350 |
} |
351 |
|
352 |
|
353 |
my $new_closure; |
354 |
|
355 |
my $subname = "Poll::$el"; |
356 |
|
357 |
if (defined &$subname) { |
358 |
no strict 'refs'; |
359 |
|
360 |
&$subname($xp, $el, \%atts, \$new_closure); |
361 |
} else { |
362 |
$body .= x($xp->recognized_string); |
363 |
$new_closure = sub { |
364 |
my ($xp, $text) = @_; |
365 |
|
366 |
if (defined $text) { |
367 |
$body .= x($text); |
368 |
} else { |
369 |
$body .= x("</$el>"); |
370 |
} |
371 |
}; |
372 |
} |
373 |
|
374 |
push(@closure_stack, $closure); |
375 |
$closure = $new_closure; |
376 |
} # End starthndl |
377 |
|
378 |
sub endhndl { |
379 |
my ($xp, $el) = @_; |
380 |
|
381 |
return unless $in_poll; |
382 |
|
383 |
my $lev = $xp->depth; |
384 |
|
385 |
if ($lev == $in_poll - 1) { |
386 |
$in_poll = 0; |
387 |
$xp->finish; |
388 |
return; |
389 |
} |
390 |
|
391 |
if ($Mode_level == $lev) { |
392 |
|
393 |
if ($Mode eq 'pass') { |
394 |
&$closure($xp, $Markedup_Text) if (defined $closure); |
395 |
} |
396 |
|
397 |
$Mode = $Mode_level = 0; |
398 |
} |
399 |
|
400 |
if ($Mode) { |
401 |
if ($Mode eq 'pass') { |
402 |
$Markedup_Text .= "</$el>"; |
403 |
} elsif ($Mode eq 'object') { |
404 |
my $this = $Object; |
405 |
if (2 == keys %$this) { |
406 |
$this = $this->{_Text}; |
407 |
} |
408 |
|
409 |
$Object = pop(@Ostack); |
410 |
|
411 |
my $slot = $Object->{$el}; |
412 |
if (defined $slot) { |
413 |
if (ref($slot) eq 'ARRAY') { |
414 |
push(@$slot, $this); |
415 |
} else { |
416 |
$Object->{$el} = [$slot, $this]; |
417 |
} |
418 |
} else { |
419 |
$Object->{$el} = $this; |
420 |
} |
421 |
} |
422 |
|
423 |
return; |
424 |
} |
425 |
|
426 |
&$closure($xp) if defined $closure; |
427 |
|
428 |
$closure = pop(@closure_stack); |
429 |
} # End endhndl |
430 |
|
431 |
#---------------------------------------------------------- |
432 |
|
433 |
sub text { |
434 |
my ($xp, $data) = @_; |
435 |
|
436 |
return unless $in_poll; |
437 |
|
438 |
if ($Mode) { |
439 |
|
440 |
if ($Mode eq 'pass') { |
441 |
my $safe = sgml_escape($data); |
442 |
|
443 |
$Text .= $safe; |
444 |
$Markedup_Text .= $safe; |
445 |
} elsif ($Mode eq 'object') { |
446 |
$Object->{_Text} .= $data if $data =~ /\S/; |
447 |
} |
448 |
|
449 |
return; |
450 |
} |
451 |
|
452 |
&$closure($xp, sgml_escape($data)) if (defined $closure); |
453 |
|
454 |
} # End text |
455 |
|
456 |
sub start_mode { |
457 |
my ($xp, $mode) = @_; |
458 |
|
459 |
if ($mode eq 'pass') { |
460 |
$Text = ''; |
461 |
$Markedup_Text = ''; |
462 |
} elsif ($mode eq 'object') { |
463 |
$Object = { |
464 |
_Atts => undef, |
465 |
_Text => undef |
466 |
}; |
467 |
} |
468 |
|
469 |
$Mode = $mode; |
470 |
$Mode_level = $xp->depth; |
471 |
} # End start_mode |
472 |
|
473 |
sub sgml_escape { |
474 |
my ($str) = @_; |
475 |
|
476 |
$str =~ s/\&/\&/g; |
477 |
$str =~ s/</\</g; |
478 |
$str =~ s/>/\>/g; |
479 |
|
480 |
$str; |
481 |
} # End sgml_escape |
482 |
|
483 |
################################################################ |
484 |
|
485 |
package Poll; |
486 |
|
487 |
sub page { |
488 |
package main; |
489 |
|
490 |
my ($xp, $el, $attref, $ncref) = @_; |
491 |
|
492 |
$$ncref = sub { |
493 |
my ($xp, $text) = @_; |
494 |
|
495 |
if (! defined $text) { |
496 |
|
497 |
print "p[$page_nr] "; |
498 |
|
499 |
if (defined $last_fn) { |
500 |
open(PAGE, ">$poll/$last_fn") or die "Couldn't open $last_fn for writing:\n$!"; |
501 |
print PAGE php_header($page_nr,@prelast_sql_update); |
502 |
my $next_fn=sprintf("%02d.php",$page_nr); |
503 |
$last_page=~s/##NEXTPAGE##/$next_fn/; |
504 |
print PAGE $last_page; |
505 |
close(PAGE); |
506 |
|
507 |
} |
508 |
@prelast_sql_update=@last_sql_update; |
509 |
@last_sql_update=@sql_update; |
510 |
@sql_update = (); |
511 |
|
512 |
$last_fn=sprintf("%02d.php",$page_nr); |
513 |
$last_page="$html{'header'} $body $html{'submit'} $html{'footer'}"; |
514 |
# delete vars for next page |
515 |
$page_nr++; |
516 |
$body=""; |
517 |
} |
518 |
} |
519 |
} # page |
520 |
|
521 |
sub nr { |
522 |
package main; |
523 |
|
524 |
my ($xp, $el, $attref, $ncref) = @_; |
525 |
|
526 |
$question_tag=""; |
527 |
|
528 |
$$ncref = sub { |
529 |
my ($xp, $text) = @_; |
530 |
if (defined($text)) { |
531 |
$body.=x($text); |
532 |
chomp $text; |
533 |
$question_tag .= x($text); |
534 |
} else { |
535 |
$question_nr{$q_type} = $question_tag; |
536 |
$question_nr{$q_type} =~ s/[^0-9a-zA-Z]//g; |
537 |
print "$question_nr{$q_type} "; |
538 |
} |
539 |
$p_suffix=""; |
540 |
}; |
541 |
} # nr |
542 |
|
543 |
|
544 |
sub hr { |
545 |
$body .= $html{'hr_before'}.$html{'separator'}.$html{'hr_after'}; |
546 |
} |
547 |
|
548 |
sub que { |
549 |
package main; |
550 |
|
551 |
my ($xp, $el, $attref, $ncref) = @_; |
552 |
|
553 |
my $nonum = x($attref->{unnumbered}); |
554 |
if ($nonum) { |
555 |
$q_type = $u_db_col; # unnumbered questions |
556 |
} else { |
557 |
$q_type = $q_db_col; |
558 |
} |
559 |
|
560 |
$question_nr{$q_type}++; |
561 |
|
562 |
# attribute markup_before override que_before |
563 |
my $markup_before = x($attref->{markup_before}); |
564 |
my $markup_after = x($attref->{markup_after}); |
565 |
|
566 |
if (defined($markup_before)) { |
567 |
$body.=$markup_before; |
568 |
} elsif ($html{'que_before'}) { |
569 |
$body.=$html{'que_before'} |
570 |
} |
571 |
|
572 |
$$ncref = sub { |
573 |
my ($xp, $text) = @_; |
574 |
|
575 |
if (defined $text) { |
576 |
$body.=x($text); |
577 |
} else { |
578 |
if (defined($markup_after)) { |
579 |
$body.=$markup_after; |
580 |
} elsif ($html{'que_after'}) { |
581 |
$body.=$html{'que_after'} |
582 |
} |
583 |
} |
584 |
} |
585 |
} |
586 |
|
587 |
sub subque { |
588 |
package main; |
589 |
|
590 |
my ($xp, $el, $attref, $ncref) = @_; |
591 |
|
592 |
my $markup_before = x($attref->{markup_before}); |
593 |
my $markup_after = x($attref->{markup_after}); |
594 |
|
595 |
if (defined($markup_before)) { |
596 |
$body.=$markup_before; |
597 |
} elsif ($html{'subque_before'}) { |
598 |
$body.=$html{'subque_before'} |
599 |
} |
600 |
|
601 |
$$ncref = sub { |
602 |
my ($xp, $text) = @_; |
603 |
|
604 |
if (defined $text) { |
605 |
$body.=x($text); |
606 |
} else { |
607 |
if (defined($markup_after)) { |
608 |
$body.=$markup_after; |
609 |
} elsif ($html{'subque_after'}) { |
610 |
$body.=$html{'subque_after'} |
611 |
} |
612 |
} |
613 |
} |
614 |
} |
615 |
|
616 |
|
617 |
sub ans { |
618 |
package main; |
619 |
|
620 |
my ($xp, $el, $attref, $ncref) = @_; |
621 |
|
622 |
my $markup_before = x($attref->{markup_before}); |
623 |
my $markup_after = x($attref->{markup_after}); |
624 |
|
625 |
if (defined($markup_before)) { |
626 |
$body.=$markup_before; |
627 |
} elsif ($html{'ans_before'}) { |
628 |
$body.=$html{'ans_before'} |
629 |
} |
630 |
|
631 |
$$ncref = sub { |
632 |
my ($xp, $text) = @_; |
633 |
|
634 |
if (defined $text) { |
635 |
$body .= x($text); |
636 |
} else { |
637 |
if (defined($markup_after)) { |
638 |
$body.=$markup_after; |
639 |
} elsif ($html{'ans_after'}) { |
640 |
$body.=$html{'ans_after'} |
641 |
} |
642 |
} |
643 |
} |
644 |
} |
645 |
|
646 |
sub php { |
647 |
package main; |
648 |
my ($xp, $el, $attref, $ncref) = @_; |
649 |
|
650 |
$body.="<?php\n"; |
651 |
|
652 |
$$ncref = sub { |
653 |
my ($xp, $text) = @_; |
654 |
|
655 |
if (defined $text) { |
656 |
$text=~s/ lt / < /g; |
657 |
$text=~s/ le / <= /g; |
658 |
$text=~s/ gt / > /g; |
659 |
$text=~s/ ge / >= /g; |
660 |
$body.=x($text); |
661 |
} else { |
662 |
$body.="\n?>\n"; |
663 |
} |
664 |
} |
665 |
} |
666 |
|
667 |
sub dropdown { |
668 |
package main; |
669 |
|
670 |
my ($xp, $el, $attref, $ncref) = @_; |
671 |
|
672 |
my @dropdown_data; |
673 |
|
674 |
my $default_value = x($attref->{default_value}) || 'null'; |
675 |
my $default_text = x($attref->{default_text}) || '-'; |
676 |
|
677 |
$$ncref = sub { |
678 |
my ($xp, $text) = @_; |
679 |
|
680 |
if (defined $text) { |
681 |
chomp $text; |
682 |
$text=~s/^\s*//g; |
683 |
$text=~s/^[\d\.\s]+//g if ($remove_nrs_in_answers); |
684 |
$text=~s/\s*$//g; |
685 |
push @dropdown_data,x($text) if ($text ne ""); |
686 |
} else { |
687 |
my $opt; |
688 |
my $id=1; |
689 |
my $p=new_que(); |
690 |
$body.="<select name=$p >\n"; |
691 |
$body.="<option value=\"$default_value\">$default_text</option>\n"; |
692 |
foreach $opt (@dropdown_data) { |
693 |
if (defined($opt) && $opt ne "") { |
694 |
$body.="<option value=$id>$opt</option>\n"; |
695 |
$id++; |
696 |
} |
697 |
} |
698 |
$body.="</select>\n"; |
699 |
|
700 |
push @sql_create,"$p int4"; |
701 |
push @sql_update,"$p=\$$p"; |
702 |
} |
703 |
} |
704 |
} |
705 |
|
706 |
sub textbox { |
707 |
package main; |
708 |
my ($xp, $el, $attref, $ncref) = @_; |
709 |
|
710 |
$$ncref = sub { |
711 |
my ($xp, $text) = @_; |
712 |
my $size=$attref->{size}; |
713 |
$size = 25 if (! defined $size || $size == 0); # default |
714 |
my $p=new_que(); |
715 |
$body.="<input type=text name=$p size=".x($size)." >\n"; |
716 |
push @sql_create,"$p text"; |
717 |
push @sql_update,"$p='\$$p'"; |
718 |
} |
719 |
} |
720 |
|
721 |
sub radiobuttons_tab { |
722 |
package main; |
723 |
my ($xp, $el, $attref, $ncref) = @_; |
724 |
|
725 |
$$ncref = sub { |
726 |
my ($xp, $text) = @_; |
727 |
if (! defined $text) { |
728 |
my $nr=$attref->{nr} || die "need <radiobuttons_tab nr=\"999\"> for number of buttons"; |
729 |
# shownumbers="before|after" |
730 |
my $shownumbers=lc(x($attref->{shownumbers})) || 'no'; |
731 |
my $showlabels=lc(x($attref->{showlabels})) || 'no'; |
732 |
my $class=lc(x($attref->{class})) || ''; |
733 |
$class=' class="'.$class.'"' if ($class); |
734 |
my $p=new_que(); |
735 |
for (my $i=1; $i<=$nr; $i++) { |
736 |
$body.="<td$class>"; |
737 |
$body.=$i if ($shownumbers eq "before"); |
738 |
if ($showlabels eq "before" && $attref->{"label_$i"}) { |
739 |
$body.=x($attref->{"label_$i"}); |
740 |
} |
741 |
$body.="<input type=radio name=$p value=$i>"; |
742 |
$body.=$i if ($shownumbers eq "after"); |
743 |
$body.="</td> "; |
744 |
} |
745 |
push @sql_create,"$p int4"; |
746 |
push @sql_update,"$p=\$$p"; |
747 |
} |
748 |
} |
749 |
} |
750 |
|
751 |
sub radiobuttons { |
752 |
package main; |
753 |
my ($xp, $el, $attref, $ncref) = @_; |
754 |
|
755 |
my @radiobuttons_data; |
756 |
|
757 |
$$ncref = sub { |
758 |
my ($xp, $text) = @_; |
759 |
|
760 |
if (defined $text) { |
761 |
chomp $text; |
762 |
$text=~s/^\s*//g; |
763 |
$text=~s/^[\d\.\s]+//g if ($remove_nrs_in_answers); |
764 |
$text=~s/\s*$//g; |
765 |
push @radiobuttons_data,x($text) if ($text ne ""); |
766 |
} else { |
767 |
my $opt; |
768 |
my $p=new_que(); |
769 |
my $id=1; |
770 |
foreach $opt (@radiobuttons_data) { |
771 |
if (defined($opt) && $opt ne "") { |
772 |
$body.="<input type=radio name=$p value=$id> $opt<br>\n"; |
773 |
$id++; |
774 |
} |
775 |
} |
776 |
push @sql_create,"$p int4"; |
777 |
push @sql_update,"$p=\$$p"; |
778 |
} |
779 |
} |
780 |
} |
781 |
sub checkbox { |
782 |
package main; |
783 |
my ($xp, $el, $attref, $ncref) = @_; |
784 |
|
785 |
$$ncref = sub { |
786 |
my ($xp, $text) = @_; |
787 |
my $p=new_que(); |
788 |
$body.="<input type=checkbox name=$p >\n"; |
789 |
push @sql_create,"$p text"; |
790 |
push @sql_update,"$p='\$$p'"; |
791 |
} |
792 |
} |
793 |
|
794 |
sub checkboxes { |
795 |
package main; |
796 |
|
797 |
my ($xp, $el, $attref, $ncref) = @_; |
798 |
|
799 |
my @checkboxes_data; |
800 |
|
801 |
$$ncref = sub { |
802 |
my ($xp, $text) = @_; |
803 |
|
804 |
|
805 |
if (defined $text) { |
806 |
chomp $text; |
807 |
$text=~s/^\s*//g; |
808 |
$text=~s/^[\d\.\s]+//g if ($remove_nrs_in_answers); |
809 |
$text=~s/\s*$//g; |
810 |
push @checkboxes_data,x($text) if ($text ne ""); |
811 |
} else { |
812 |
my $opt; |
813 |
my $base_p=new_que(); |
814 |
my $id=1; |
815 |
|
816 |
my $before=$attref->{before}; |
817 |
my $after=$attref->{after}; |
818 |
my $middle=$attref->{middle}; |
819 |
if (! $before && ! $after && ! $middle) { |
820 |
$middle=" "; |
821 |
$after="<br>"; |
822 |
} |
823 |
my $hide_description=$attref->{hide_description}; |
824 |
|
825 |
foreach $opt (@checkboxes_data) { |
826 |
if (defined($opt) && $opt ne "") { |
827 |
$p=$base_p."_".$id; |
828 |
$id++; |
829 |
$body .= x($before) if ($before); |
830 |
$body.="<input type=checkbox name=$p>"; |
831 |
$body .= x($middle) if ($middle); |
832 |
$body .= "$opt" if (! $hide_description); |
833 |
$body .= x($after) if ($after); |
834 |
$body.="\n"; |
835 |
|
836 |
push @sql_create,"$p boolean"; |
837 |
push @sql_update,"$p=\$$p"; |
838 |
} |
839 |
} |
840 |
$php_addon[$page_nr].="fix_checkboxes($base_p,".($id-1).");"; |
841 |
|
842 |
} |
843 |
} |
844 |
} |
845 |
|
846 |
# |
847 |
# insert arbitrary html |
848 |
# |
849 |
sub html { |
850 |
package main; |
851 |
|
852 |
my ($xp, $el, $attref, $ncref) = @_; |
853 |
|
854 |
$body.=$html{'html_before'} if ($html{'html_before'}); |
855 |
|
856 |
$$ncref = sub { |
857 |
my ($xp, $text) = @_; |
858 |
|
859 |
if (defined $text) { |
860 |
$body.=x($text); |
861 |
} elsif ($attref->{include}) { |
862 |
$body.=suck_file($attref->{include}); |
863 |
} else { |
864 |
$body.=$html{'html_after'} if ($html{'html_after'}); |
865 |
} |
866 |
} |
867 |
} |
868 |
|
869 |
# |
870 |
# markup tag can specify any markup which should be applied pre (before) |
871 |
# or post (after) any other tag which produces html output |
872 |
# |
873 |
|
874 |
sub markup { |
875 |
package main; |
876 |
|
877 |
my ($xp, $el, $attref, $ncref) = @_; |
878 |
|
879 |
$$ncref = sub { |
880 |
my ($xp, $text) = @_; |
881 |
|
882 |
my $tag=lc($attref->{tag}) || die 'markup need tag attribute: <markup tag="tag_name" pos="(before|after)">'; |
883 |
my $pos=lc($attref->{pos}) || die 'markup need pos attribute: <markup tag="tag_name" pos="(before|after)">'; |
884 |
|
885 |
return if (! defined $text); |
886 |
chomp($text); |
887 |
if ($text ne "") { |
888 |
$text =~ s/\&/\&/g; |
889 |
$text =~ s/\</</g; |
890 |
$text =~ s/\>/>/g; |
891 |
$text =~ s/^\s+//g; |
892 |
$text =~ s/\s+$//g; |
893 |
$html{$tag.'_'.$pos}=x($text); |
894 |
print "Using markup $pos $tag: ",x($text),"<--\n"; |
895 |
} |
896 |
} |
897 |
} |
898 |
|
899 |
# |
900 |
# print final instructions and exit |
901 |
# |
902 |
|
903 |
print "\n\nTo create database for poll $poll use:\n\n"; |
904 |
print "\$ psql template1 < $poll/$poll.sql\n\n"; |
905 |
print "THIS WILL DISTROY ALL DATA IN EXISTING DATABASE ".$prefix.$poll." !!\n"; |
906 |
|
907 |
# read configuration data |
908 |
sub config { |
909 |
package main; |
910 |
my ($xp, $el, $attref, $ncref) = @_; |
911 |
|
912 |
$$ncref = sub { |
913 |
my ($xp, $text) = @_; |
914 |
$db_user=x($attref->{db_user}); |
915 |
$prefix=x($attref->{prefix}); |
916 |
$without_invitation=x($attref->{without_invitation}) && |
917 |
print "Pool is without need for unique ID (and invitation URLs).\n"; |
918 |
$remove_nrs_in_answers=x($attref->{remove_nrs_in_answers}) && |
919 |
print "Numbers before answers will be removed.\n"; |
920 |
|
921 |
# fill in configuration about include files |
922 |
foreach my $file (qw(header separator submit footer thanks)) { |
923 |
if ($attref->{$file}) { |
924 |
$include_files{$file}=x($attref->{$file}); |
925 |
print "Using custom $file '$include_files{$file}'\n"; |
926 |
$html{$file} = suck_file($include_files{$file}); |
927 |
} |
928 |
} |
929 |
$q_db_col=x($attref->{q_db_col}) || 'q'; |
930 |
$u_db_col=x($attref->{u_db_col}) || 'u'; |
931 |
|
932 |
} |
933 |
} |
934 |
|
935 |
#--------------------------------------------------------------- |