#!/usr/bin/env ruby
load "/home/ben/src/lib.rb/pphtml.rb"
require "yaml"
require "cgi"
require "pp"
require "date"
class Poll
attr_reader :head
def initialize
@head = {}
@data = {}
@comment = []
store
end
def head_to_html
ret = "
\n"
@head.sort.each{|columntitle,columndescription|
ret += "
#{columntitle}
\n"
}
ret += "
Last Edit
\n"
# ret += "
"
# ret += "\n"
# ret += "
\n"
ret += "
\n"
ret
end
def add_remove_column_htmlform
return <
END
end
def to_html
ret = "
\n"
ret += "\n"
ret += "
"
ret += "
"
unless @comment.empty?
ret += ""
end
ret += "
\n"
ret
end
def add_participant(name, agreed)
name = CGI.escapeHTML(name.strip)
@data[name] = {"timestamp" => Time.now}
@head.each_key{|columntitle|
@data[name][columntitle] = agreed[columntitle.to_s]
}
store
end
def delete(name)
@data.delete(CGI.escapeHTML(name.strip))
store
end
def store
File.open("#{SITE}.yaml", 'w') do |out|
out << "# This is a dudle poll file\n"
out << self.to_yaml
out.chmod(0660)
end
end
def add_comment name, comment
@comment << [Time.now, CGI.escapeHTML(name), CGI.escapeHTML(comment.strip).gsub("\r\n"," ")]
store
end
def add_remove_column name, description
add_remove_parsed_column CGI.escapeHTML(name.strip), CGI.escapeHTML(description.strip)
end
def add_remove_parsed_column columntitle, description
if @head.include?(columntitle)
@head.delete(columntitle)
else
@head[columntitle] = description
end
store
true
end
end
class DatePoll < Poll
def head_to_html
ret = "
\n"
ret
end
def add_remove_column_htmlform
if $cgi.include?("__add_remove_column_month")
begin
startdate = Date.parse("#{$cgi["__add_remove_column_month"]}-1")
rescue ArgumentError
olddate = $cgi.params["__add_remove_column_month"][1]
case $cgi["__add_remove_column_month"]
when "<<"
startdate = Date.parse("#{olddate}-1")-365
when "<"
startdate = Date.parse("#{olddate}-1")-1
when ">"
startdate = Date.parse("#{olddate}-1")+31
when ">>"
startdate = Date.parse("#{olddate}-1")+366
end
startdate = Date.parse("#{startdate.year}-#{startdate.month}-1")
end
else
startdate = Date.parse("#{Date.today.year}-#{Date.today.month}-1")
end
ret = <
END
ret
end
def add_remove_column name,description
begin
parsed_name = Date.parse("#{$cgi["__add_remove_column_month"]}-#{name}")
rescue ArgumentError
return false
end
add_remove_parsed_column(parsed_name,CGI.escapeHTML(description))
end
end
if __FILE__ == $0
$cgi = CGI.new
CONTENTTYPE = "text/html; charset=utf-8"
#CONTENTTYPE = "application/xhtml+xml; charset=utf-8"
puts "Content-type: #{CONTENTTYPE}"
if $cgi.include?("__utf") || $cgi.cookies["utf"][0]
YES = CGI.escapeHTML('✔')
NO = CGI.escapeHTML('✘')
MAYBE = CGI.escapeHTML('?')
BACK = CGI.escapeHTML("↩")
puts "Set-Cookie: utf=true; path=; expires=#{(Time.now+1*60*60*24*365).getgm.strftime("%a, %d %b %Y %H:%M:%S %Z")}"
else
YES = CGI.escapeHTML('OK')
NO = CGI.escapeHTML('-')
MAYBE = CGI.escapeHTML('?')
BACK = CGI.escapeHTML("<-")
end
puts <
HEAD
$cgi.params.each{|k,v|
if "" == v[0].to_s && !(k =~ /^__/)
if defined?(SITE)
puts "FEHLER, meld dich bei Ben!"
exit
else
SITE = k
end
end
}
if defined?(SITE) and File.exist?(SITE + ".yaml" ) and table = YAML::load_file(SITE + ".yaml")
puts <
dudle - #{SITE}
HEAD
if $cgi.include?("__add_participant")
agreed = {}
$cgi.params.each{|k,v|
if k =~ /^__add_participant_checked_/
agreed[k.gsub(/^__add_participant_checked_/,"")] = v[0]
end
}
table.add_participant($cgi["__add_participant"],agreed)
end
table.delete($cgi["__delete"]) if $cgi.include?("__delete")
if $cgi.include?("__add_remove_column")
puts "Could not add/remove column #{$cgi["__add_remove_column"]}" unless table.add_remove_column($cgi["__add_remove_column"],$cgi["__columndescription"])
end
table.add_comment($cgi["__commentname"],$cgi.params["__comment"][0]) if $cgi.include?("__comment")
puts table.to_html
puts "
"
puts "To change a line, add a new person with the same name!"
puts "
"
puts "
"
puts "
"
puts ""
puts "
"
puts "
"
puts table.add_remove_column_htmlform
puts "
"
puts "
"
puts ""
puts "
"
puts "
"
else
if defined?($cgi["__create_poll"])
SITE=$cgi["__create_poll"]
case $cgi["__poll_type"]
when "Poll"
Poll.new
when "DatePoll"
DatePoll.new
end
end
puts <
dudle
HEAD
puts "