--- make_poll.pl 2003/04/24 17:55:17 1.5 +++ make_poll.pl 2003/09/24 20:35:48 1.9 @@ -19,7 +19,7 @@ my @Modes = qw(object pass skip); -my $dir; +my $poll; my $dowarn = 1; my $pitanje_nr = 0; # curr. pitanje @@ -53,6 +53,12 @@ # this is usename in database my $db_user="dpavlin"; +# This option allows users to fill poll without using invitation URL. +# That also means it's unpossible for them to return to exiting poll +# because they don't have thair own unique ID. Howver, it enables simple +# polls to be conducted by just publishing URL to them. +my $without_invitation=0; + #------------------------------------------------------------------ sub suck_file { @@ -74,18 +80,18 @@ sub php_header { my ($page_nr,@sql_update) = @_; my $out='$sql"; $result=pg_Exec($conn,fix_sql($sql)); - } elseif($do_stranice != $PHP_SELF) { + } elseif($do_stranice != $PHP_SELF && isset($do_uri) && isset($a)) { Header("Location: $do_uri?a=$a"); exit; } @@ -123,18 +129,18 @@ die "Can't read $xmlfile" unless -r $xmlfile; -if (defined $dir) { - die "$dir isn't a directory" unless -d $dir; +if (defined $poll) { + die "$poll isn't a directory" unless -d $poll; } else { $xmlfile =~ m!([^/.]+)(?:\.[^/.]*)?$!; - $dir = $1; - if (-e $dir) { - die "$dir exists but isn't a directory" - unless -d $dir; + $poll = $1; + if (-e $poll) { + die "$poll exists but isn't a directory" + unless -d $poll; } else { - mkdir $dir, 0755; + mkdir $poll, 0755; } } @@ -178,7 +184,7 @@ print "p[$page_nr] "; -open(PAGE, ">$dir/$last_fn") or die "Couldn't open $last_fn for writing:\n$!"; +open(PAGE, ">$poll/$last_fn") or die "Couldn't open $last_fn for writing:\n$!"; print PAGE php_header($page_nr,@prelast_sql_update); my $next_fn=sprintf("%02d.php",$page_nr); $last_page=~s/##NEXTPAGE##/$next_fn/; @@ -186,34 +192,38 @@ close(PAGE); $page_nr++; -open(PAGE, ">$dir/$next_fn") or die "Couldn't open $next_fn for writing:\n$!"; +open(PAGE, ">$poll/$next_fn") or die "Couldn't open $next_fn for writing:\n$!"; print PAGE php_header($page_nr,@last_sql_update); print PAGE "$html_header $html_kraj $html_footer"; close(PAGE); # dump sql structure -open(SQL,">$dir/$dir.sql") || die "$dir.sql: $!"; +open(SQL,">$poll/$poll.sql") || die "$poll.sql: $!"; +print SQL "drop database ".$prefix.$poll.";\n"; +print SQL "create database ".$prefix.$poll.";\n"; +print SQL "\\connect ".$prefix.$poll.";\n"; print SQL "create table poslani ( member_id int4 not null, unesen timestamp default now() );\n"; -print SQL "create table $dir (do_stranice text default null, ",join(",\n",@sql_create),");\n"; +print SQL "create table $poll (do_stranice text default null, ",join(",\n",@sql_create),");\n"; close(SQL); # dump common.php -open(PHP,">$dir/common.php") || die "common.php: $!"; -$common_php =~ s/##DB##/$dir/g; -my $db_name = $prefix.$dir; +open(PHP,">$poll/common.php") || die "common.php: $!"; +$common_php =~ s/##DB##/$poll/g; +my $db_name = $prefix.$poll; $common_php =~ s/##DB_NAME##/$db_name/g; $common_php =~ s/##PREFIX##/$prefix/g; $common_php =~ s/##DB_USER##/$db_user/g; $common_php =~ s/##PREFIX##/$prefix/g; my $members_db = $prefix."members"; $common_php =~ s/##MEMBERS_DB##/$members_db/g; +$common_php =~ s/##WITHOUT_INVITATION##/$without_invitation/g; print PHP $common_php; close(PHP); -open(PHP,">$dir/head.php") || die "head.php: $!"; +open(PHP,">$poll/head.php") || die "head.php: $!"; my $max_page = $page_nr - 1; $head_php=~ s/##MAXPAGE##/$max_page/; $head_php=~ s/##TEXT##/Ispunili ste %02d%% ankete/; @@ -221,7 +231,7 @@ close(PHP); # 01.php -> index.php -rename "$dir/01.php","$dir/index.php" || die "can't rename '$dir/01.php' to index.php"; +rename "$poll/01.php","$poll/index.php" || die "can't rename '$poll/01.php' to index.php"; ################ ## End of main @@ -229,7 +239,8 @@ # return unique name of pitanje sub new_pit { - my $out="p".$pitanje_nr.$p_suffix; + my $out="p".$pitanje_nr; + $out .= "_".$p_suffix if ($p_suffix); $curr_suffix=$p_suffix; $p_suffix++; return $out; @@ -303,16 +314,16 @@ &$subname($xp, $el, \%atts, \$new_closure); } else { - $body .= $xp->recognized_string; + $body .= x($xp->recognized_string); $new_closure = sub { my ($xp, $text) = @_; if (defined $text) { - $body .= $text; + $body .= x($text); } else { - $body .= ""; + $body .= x(""); } }; } @@ -434,6 +445,7 @@ $str; } # End sgml_escape + ################################################################ package Poll; @@ -451,30 +463,8 @@ print "p[$page_nr] "; if (defined $last_fn) { - open(PAGE, ">$dir/$last_fn") or die "Couldn't open $last_fn for writing:\n$!"; - if ($page_nr == 2) { - print PAGE '$sql"; - $result=pg_Exec($conn,fix_sql($sql)); - $lastoid=pg_getlastoid($result); - $result = pg_Exec($conn,fix_sql("select id from '.$dir.' where oid=$lastoid")); - $row=pg_fetch_row($result,0); - $id=$row[0]; -?>'; - - } else { - print PAGE php_header($page_nr,@prelast_sql_update); - } # last_sql_update - - + open(PAGE, ">$poll/$last_fn") or die "Couldn't open $last_fn for writing:\n$!"; + print PAGE php_header($page_nr,@prelast_sql_update); my $next_fn=sprintf("%02d.php",$page_nr); $last_page=~s/##NEXTPAGE##/$next_fn/; print PAGE $last_page; @@ -764,6 +754,10 @@ } } +print "\n\nTo create database for poll $poll use:\n\n"; +print "\$ psql template1 < $poll/$poll.sql\n\n"; +print "THIS WILL DISTROY ALL DATA IN EXISTING DATABASE ".$prefix.$poll." !!\n"; + # read configuration data # # FIX: write actually this :-) @@ -775,6 +769,7 @@ my ($xp, $text) = @_; $db_user=x($attref->{db_user}); $prefix=x($attref->{prefix}); + $without_invitation=x($attref->{without_invitation}); } }