skyzyx
2/26/2009 - 6:04 AM

Generates a SELECT box from a start month to an end month

#
# SELECT pulldown from a start month to an end month.
# If the options[:name] is passed a parameter in the URL, the SELECT will default to the matching OPTION entry. 
# Otherwise, it will default to the one with the end date.
# 
# options: {
# 	:name, 
# 	:start,
# 	:end (optional; defaults to current month)
# }
#
def select_month(options)
    output = ""
	output.concat "<select name=\"#{options[:name]}\">\n"

	dstart = Time.parse(options[:start])
	dcurrent = dstart

	if options[:end].nil?
		dend = Time.now
	else
		dend = Time.parse(options[:end])
	end

	while dcurrent.strftime('%Y%m').to_i <= dend.strftime('%Y%m').to_i do
		period = dend.strftime('%Y_%m')
		unless params[options[:name]].nil?
			period = params[options[:name]]
		end

		selected = ""
		if dcurrent.strftime('%Y_%m') == period
			selected = "selected=\"selected\""
		end

		output.concat "<option value=\"#{dcurrent.strftime('%Y_%m')}\" #{selected}>#{dcurrent.strftime('%B %Y')}</option>\n"
		dcurrent += 1.month
	end

	output.concat "</select>\n"

    return output
end