/[wopi]/make_poll.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /make_poll.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.15 - (hide annotations)
Tue Oct 21 18:40:26 2003 UTC (20 years, 6 months ago) by dpavlin
Branch: MAIN
Changes since 1.14: +216 -184 lines
File MIME type: text/plain
translation to english, added markup tag to insert markup before and/or after
some tags

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

  ViewVC Help
Powered by ViewVC 1.1.26