aboutsummaryrefslogtreecommitdiff
path: root/timepoll.rb
blob: 1b79d55bac2e95babb06d2970fb0083bb2ae16de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
################################
# Author:  Benjamin Kellermann #
# License: CC-by-sa 3.0        #
#          see License         #
################################

require "date"
require "poll"
require "time"

class TimePoll < Poll
	def sort_data fields
		datefields = fields.collect{|field| 
			field == "timestamp" || field == "name" ? field : Time.parse(field) 
		}
		super datefields
	end 
	# returns a sorted array, containing the big units and how often each small is in the big one
	# small and big must be formated for strftime
	# ex: head_count("%Y-%m", "-%d") returns an array like [["2009-03",2],["2009-04",3]]
	def head_count(big,small)
		ret = Hash.new(0)
		@head.keys.collect{|curdate|
			Time.parse(curdate.strftime(big + small))
		}.uniq.each{|day|
			ret[day.strftime(big)] += 1
		}
		ret.sort
	end

	def head_to_html(config = false)
		ret = "<tr><td></td>"
		head_count("%Y-%m","-%d %H:%M%Z").each{|title,count|
			year, month = title.split("-").collect{|e| e.to_i}
			ret += "<th colspan='#{count}'>#{Date::ABBR_MONTHNAMES[month]} #{year}</th>\n"
		}
		ret += "</tr><tr><td></td>"
		head_count("%Y-%m-%d", " %H:%M%Z").each{|title,count|
			ret += "<th colspan='#{count}'>#{Date.parse(title).strftime("%a, %d")}</th>\n"
		}
		ret += "</tr><tr><th><a href='?sort=name'>Name</a></th>"
		@head.keys.sort.each{|curdate|
			ret += "<th><a href='?sort=#{CGI.escapeHTML(CGI.escape(curdate.to_s))}'>#{curdate.strftime("%H:%M")}</a></th>\n"
		}
		ret += "<th><a href='.'>Last Edit</a></th></tr>"
		ret
	end
	def add_remove_column_htmlform
		if $cgi.include?("add_remove_column_month")
			begin
				startdate = Time.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 = Time.parse("#{olddate}-1")-365
				when CGI.unescapeHTML(MONTHBACK)
					startdate = Time.parse("#{olddate}-1")-1
				when CGI.unescapeHTML(MONTHFORWARD)
					startdate = Time.parse("#{olddate}-1")+31
				when CGI.unescapeHTML(YEARFORWARD)
					startdate = Time.parse("#{olddate}-1")+366
				else
					exit
				end
				startdate = Time.parse(startdate.strftime("%Y-%m-1"))
			end
		else
			startdate = Time.parse(Date.today.strftime("%Y-%m-1"))
		end
		ret = <<END
<form method='post' action=''>
<div style="float: left; margin-right: 20px">
<table><tr>
END
		def navi val
			"<th style='padding:0px'>" +
				"<input class='navigation' type='submit' name='add_remove_column_month' value='#{val}' />" +
				"</th>"
		end
		[YEARBACK,MONTHBACK].each{|val| ret += navi(val)}
		ret += "<th colspan='3'>#{Date::ABBR_MONTHNAMES[startdate.month]} #{startdate.year}</th>"
		[MONTHFORWARD, YEARFORWARD].each{|val| ret += navi(val)}
		 
		ret += "</tr><tr>\n"

		7.times{|i| ret += "<th class='weekday'>#{Date::ABBR_DAYNAMES[(i+1)%7]}</th>" }
		ret += "</tr><tr>\n"
		
		((startdate.wday+7-1)%7).times{
			ret += "<td></td>"
		}
		d = startdate
		while (d.month == startdate.month) do
			klasse = "notchoosen"
			klasse = "disabled" if d < Time.now - 60*60*24
			klasse = "choosen" if @head.keys.collect{|t|t.strftime("%Y-%m-%d")}.include?(d.strftime("%Y-%m-%d"))
			ret += "<td class='calendarday'><input class='#{klasse}' type='submit' name='add_remove_column' value='#{d.day}' /></td>\n"
			ret += "</tr><tr>\n" if d.wday == 0
			d += 60*60*24
		end
		ret += <<END
</tr></table>
<input type='hidden' name='add_remove_column_month' value='#{startdate.strftime("%Y-%m")}' />
</div>
</form>
END
		
		ret += "<div><table><tr>"

		head_count("%Y-%m", "-%d").each{|title,count|
			year,month = title.split("-").collect{|e| e.to_i}
			ret += "<th colspan='#{count}'>#{Date::ABBR_MONTHNAMES[month]} #{year}</th>\n"
		}

		ret += "</tr><tr>"

		head_count("%Y-%m-%d","").each{|title,count|
			ret += "<th>#{Date.parse(title).strftime("%a, %d")}</th>\n"
		}

		ret += "</tr>"


		days = @head.sort.collect{|day,descr| 
				Date.parse(day.strftime("%Y-%m-%d"))
			}.uniq
		
		@head.keys.collect{|time|
			time.strftime("%H:%M")
		}.uniq.sort.each{|time|
			ret +="<tr>\n"
			days.each{|date|
				timestamp = Time.parse("#{date} #{time} #{Time.now.zone}")
				klasse = "notchoosen"
				klasse = "disabled" if timestamp < Time.now
				klasse = "choosen" if @head.include?(timestamp)
				ret += <<END
<td class='calendarday'>
	<form method='post' action="config.cgi">
		<div>
			<!--Timestamp: #{timestamp.to_s} -->
			<input class='#{klasse}' type='submit' name='add_remove_column' value='#{time}' />
			<input type='hidden' name='add_remove_column_day' value='#{timestamp.day}' />
			<input type='hidden' name='add_remove_column_month' value='#{timestamp.strftime("%Y-%m")}' />
		</div>
	</form>
</td>
END
			}
			ret += "</tr>\n"
		}

		ret += "<tr>"
		days.each{|d|
			ret += <<END
	<td>
		<form method='post' action='config.cgi'>
			<div>
				<input type='hidden' name='add_remove_column_day' value='#{d.day}' />
				<input type='hidden' name='add_remove_column_month' value='#{d.strftime("%Y-%m")}' />
				<input name='add_remove_column' type="text" maxlength="7" style="width: 5ex" /><br />
				<input name="add_remove_column" type="submit" value="Add" style="width: 100%" />
			</div>
		</form>
	</td>
END
		}

		ret += <<END
		</tr>
	</table>
</div>
END
		ret
	end
	def add_remove_column col,description
		if $cgi.include?("add_remove_column_day")
			begin
				parsed_date = YAML::load(Time.parse("#{$cgi["add_remove_column_month"]}-#{$cgi["add_remove_column_day"]} #{col} #{Time.now.zone}").to_yaml)
			rescue ArgumentError
				return false
			end
		else
			begin
				earlytime = @head.keys.collect{|t|t.strftime("%H:%M")}.sort[0]
				parsed_date = YAML::load(Time.parse("#{$cgi["add_remove_column_month"]}-#{col} #{earlytime} #{Time.now.zone}").to_yaml)
			rescue ArgumentError
				return false
			end
		end
		add_remove_parsed_column(parsed_date,CGI.escapeHTML(description))
	end
end

if __FILE__ == $0
require 'test/unit'
require 'pp'
class TimePoll
	def store comment
	end
end

SITE="gbfuaibe"

require "cgi"
CGI_PARAMS={"add_remove_column_month" => ["2008-02"]}
CGI_COOKIES={}	
$cgi = CGI.new

class TimePollTest < Test::Unit::TestCase
	def setup
		@poll = TimePoll.new(SITE)
	end
	#TODO
	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