################################ # Author: Benjamin Kellermann # # Licence: CC-by-sa 3.0 # # see Licence # ################################ require "date" require "poll" class DatePoll < Poll def sort_data fields datefields = fields.collect{|field| field == "timestamp" || field == "name" ? field : Date.parse(field) } super datefields end def head_to_html ret = "\n" monthhead = Hash.new(0) @head.sort.each{|curdate,curdescription| monthhead["#{curdate.year}-#{curdate.mon.to_s.rjust(2,"0")} "] += 1 } monthhead.sort.each{|title,count| year, month = title.split("-").collect{|e| e.to_i} ret += "#{Date::ABBR_MONTHNAMES[month]} #{year}\n" } ret += "Name\n" @head.sort.each{|curdate,curdescription| ret += "#{Date::ABBR_DAYNAMES[curdate.wday]}, #{curdate.day}\n" } ret += "Last Edit\n" 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 CGI.unescapeHTML(YEARBACK) startdate = Date.parse("#{olddate}-1")-365 when CGI.unescapeHTML(MONTHBACK) startdate = Date.parse("#{olddate}-1")-1 when CGI.unescapeHTML(MONTHFORWARD) startdate = Date.parse("#{olddate}-1")+31 when CGI.unescapeHTML(YEARFORWARD) startdate = Date.parse("#{olddate}-1")+366 else exit end startdate = Date.parse("#{startdate.year}-#{startdate.month}-1") end else startdate = Date.parse("#{Date.today.year}-#{Date.today.month}-1") end ret = < END def navi val "" end [YEARBACK,MONTHBACK].each{|val| ret += navi(val)} ret += "" [MONTHFORWARD, YEARFORWARD].each{|val| ret += navi(val)} ret += "\n" 7.times{|i| ret += "" } ret += "\n" ((startdate.wday+7-1)%7).times{ ret += "" } d = startdate while (d.month == startdate.month) do klasse = "notchoosen" klasse = "disabled" if d < Date.today klasse = "choosen" if @head.include?(d) ret += "\n" ret += "\n" if d.wday == 0 d = d.next end ret += <
" + "" + "#{Date::ABBR_MONTHNAMES[startdate.month]} #{startdate.year}
#{Date::ABBR_DAYNAMES[(i+1)%7]}
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 require 'test/unit' require 'pp' class DatePoll def store comment end end SITE="gbfuaibe" require "cgi" CGI_PARAMS={"add_remove_column_month" => ["2008-02"]} CGI_COOKIES={} $cgi = CGI.new class DatePollTest < Test::Unit::TestCase def setup @poll = DatePoll.new(SITE, false) end def test_add_remove_column assert(!@poll.add_remove_column("foo", "bar")) assert(!@poll.add_remove_column("31", "31.02.2008 ;--)")) assert(@poll.add_remove_column("20", "correct date")) assert_equal("correct date",@poll.head[Date.parse("2008-02-20")]) assert(@poll.add_remove_column("20", "foobar")) assert(@poll.head.empty?) end end end