|
#{ select_tag object + '_' + method_prefix + '_month_sel', options_for_select(months_hash, default_value.month.to_s), :class => 'form', :style => 'border: 1px solid gray; font-size: 11px; padding: 0; margin: 0;', :onchange => h(draw_calendar) }
#{ select_tag object + '_' + method_prefix + '_year_sel', options_for_select((min_year..max_year).to_a, default_value.year), :class => 'form', :style => 'border: 1px solid gray; font-size: 11px; padding: 0; margin: 0;', :onchange => h(draw_calendar) }
|
|
#{ text_field object, method_prefix + '_year', :value => default_value.year, :style => 'display: none;' }
#{ text_field object, method_prefix + '_month', :value => default_value.month, :style => 'display: none;' }
#{ text_field object, method_prefix + '_day', :value => default_value.day, :style => 'display: none;' }
EOF
ret += javascript_tag(draw_calendar)
end
# Display a clickable ajax event calendar.
#
# Options:
# * :start_date => '06/01/2006', :end_date => '05/31/2007'
# * :show_selects => true (default :false)
# * :calendar_id => 1
# * :css_prefix => 'calendar_'
#
# TODOC: There are a number of somewhat complex prerequisites...
def event_calendar(options = {})
start_date = options[:start_date] || Time.now
end_date = options[:end_date] || 1.year.from_now
start_date = Time.parse(start_date) if start_date.is_a? String
end_date = Time.parse(end_date) if end_date.is_a? String
onchange = options[:onchange] || ''
min_year = start_date.year
max_year = min_year + 5
calendar_id = options[:calendar_id] || Calendar.find_first.id
css_prefix = options[:css_prefix] || 'calendar_'
popout_direction = options[:popout_direction] || :right
draw_calendar = "var ta = $('event_calendar_events_' + " +
"$('event_calendar_month_sel').value + '_' + $('event_calendar_year_sel').value); " +
"new Ajax.Request(" +
"'#{url_for :controller => "/util", :action => "calendar" }?" +
"month=' + $('event_calendar_month_sel').value + " +
"'&year=' + $('event_calendar_year_sel').value + " +
"'&min_time=#{start_date.to_i}&max_time=#{end_date.to_i}" +
"&calendar_id=#{calendar_id}&css_prefix=#{css_prefix}" +
"&popout_direction=#{popout_direction.to_s}', {asynchronous:true, evalScripts:true})"
# keep all this junk in sync with what's in UtilController
@month = Time.now.month
@year = Time.now.year
@min_time = start_date.to_i
@max_time = end_date.to_i
@calendar = Calendar.find(calendar_id)
@css_prefix = css_prefix
@popout_direction = popout_direction
first_of_month = Time.mktime(@year, @month, 1)
last_of_month = first_of_month.end_of_month
events = @calendar.events.find(:all, :conditions => [ 'start_date >= ? and start_date <= ?', first_of_month, last_of_month ])
@event_days = {}
events.each do |e|
@event_days[e.start_date.mday] = e
end
ret = <
|
#{ select 'event_calendar', 'month_sel', months_hash, { :selected => Time.now.month.to_s }, :class => 'form', :style => 'font-size: 11px;', :onchange => draw_calendar }
#{ select 'event_calendar', 'year_sel', (min_year..max_year).to_a, { :selected => Time.now.year }, :class => 'form', :style => 'font-size: 11px;', :onchange => draw_calendar }
#{render '/util/_calendar_month_year'}
|
|
|
#{render '/util/_calendar_days'}
|
EOF
ret += javascript_tag(draw_calendar)
end
def product_browser(object, method, options = {}) # :nodoc:
# allowed options, with defaults:
# - :can_select_department => false
# - :can_select category => false
# - :allow_multiple => false # not implemented
# - :offset_x => 0, :offset_y => 0
#
# Note that allowing dept and category will cause this helper to create multiple
# input fields, called object_method_department_id and object_method_category_id.
# It is your job to figure out which one is not blank and use that return value.
# Similarly, with allow multiple, you will need to parse the comma-separated list
# of ids yourself.
object = object.to_s
method = method.to_s
object_name = self.instance_variable_get('@' + object).send(method + '_object_name')
object_name = 'Select' if (object_name || '') == ''
ret = <#{object_name}
#{text_field object, method + '_object_name'}
#{text_field object, method + '_department_id'}
#{text_field object, method + '_category_id'}
#{text_field object, method + '_product_id'}
#{text_field object, method + '_product_option_id'}
#{text_field object, method + '_object_name_temp'}
#{text_field object, method + '_department_id_temp'}
#{text_field object, method + '_category_id_temp'}
#{text_field object, method + '_product_id_temp'}
#{text_field object, method + '_product_option_id_temp'}
EOF
return ret
end
# Note that this currently won't work for > 100 options
def ordered_hash(input) # :nodoc:
ret = Hash.new
i = 0
for k in input
ret[k[0]] = (i < 10 ? '0' + i.to_s : i.to_s) + k[1]
i += 1
end
ret.sort { |a,b| a[1] <=> b[1] }.map { |a| a[1].slice!(0...2) ; a }
end
# Returns an array of arrays (no, it is not named particularly well) containing the 50 US
# states and their abbreviations. Pass true as first arg to enable territories, etc. Intended
# for use with the select form helper.
def us_states_hash(include_territories = false)
states_plus_dc = {
"Alabama" => 'AL', "Alaska" => 'AK', "Arizona" => 'AZ', "Arkansas" => 'AR',
"California" => 'CA', "Colorado" => 'CO', "Connecticut" => 'CT',
"Delaware" => 'DE', "District Of Columbia" => 'DC',
"Florida" => 'FL',
"Georgia" => 'GA',
"Hawaii" => 'HI',
"Idaho" => 'ID', "Illinois" => 'IL', "Indiana" => 'IN', "Iowa" => 'IA',
"Kansas" => 'KS', "Kentucky" => 'KY',
"Louisiana" => 'LA',
"Maine" => 'ME', "Maryland" => 'MD', "Massachusetts" => 'MA', "Michigan" => 'MI',
"Minnesota" => 'MN', "Mississippi" => 'MS', "Missouri" => 'MO', "Montana" => 'MT',
"Nebraska" => 'NE', "Nevada" => 'NV', "New Hampshire" => 'NH', "New Jersey" => 'NJ',
"New Mexico" => 'NM', "New York" => 'NY', "North Carolina" => 'NC', "North Dakota" => 'ND',
"Ohio" => 'OH', "Oklahoma" => 'OK', "Oregon" => 'OR',
"Pennsylvania" => 'PA',
"Rhode Island" => 'RI',
"South Carolina" => 'SC', "South Dakota" => 'SD',
"Tennessee" => 'TN', "Texas" => 'TX',
"Utah" => 'UT',
"Vermont" => 'VT', "Virginia"=>'VA',
"Washington" => 'WA', "West Virginia" => 'WV', "Wisconsin" => 'WI', "Wyoming" => 'WY'
}
territories_etc = {
"Armed Forces Americas" => 'AA',
"Armed Forces Europe, Middle East, Africa and Canada" => 'AE',
"Armed Forces Pacific" => 'AP',
"American Samoa" => 'AS',
"Federated States of Micronesia" => 'FM',
"Guam" => 'GU',
"Marshall Islands" => 'MH',
"Northern Mariana Islands" => 'MP',
"Palau" => 'PW',
"Puerto Rico" => 'PR',
"Virgin Islands" => 'VI'
}
(include_territories ? states_plus_dc.merge(territories_etc) : states_plus_dc).to_a.sort { |a,b| a.last <=> b.last }
end
# Returns a hash containing the months of the year. Intended for use with the
# select form helper.
def months_hash
{ 'January' => '1', 'February' => '2', 'March' => '3', 'April' => '4',
'May' => '5', 'June' => '6', 'July' => '7', 'August' => '8',
'September' => '9', 'October' => '10', 'November' => '11',
'December' => '12' }.sort{ |a, b| a.last.to_i <=> b.last.to_i }
end
# Returns a hash containing the 3-character abbreviations of the months of
# the year. Intended for use with the select form helper.
def short_months_hash
{ 'Jan' => '1', 'Feb' => '2', 'Mar' => '3', 'Apr' => '4', 'May' => '5',
'Jun' => '6', 'Jul' => '7', 'Aug' => '8', 'Sep' => '9', 'Oct' => '10',
'Nov' => '11', 'Dec' => '12' }.sort{ |a, b| a.last.to_i <=> b.last.to_i }
end
def cropper_image_tag(options)
ret = image_tag(options[:url] || '', :id => "testImage")
ret += javascript_tag "cropper = new Cropper.Img('testImage', { minWidth: 0, minHeight: 0, captureKeys: false, onEndCrop: onEndCrop });"
end
def page_image_tag(page, filename)
image_tag File.join('content', page.path, File.basename(filename))
end
def copyright_year(year)
year_str, this_year = year.to_s, Time.now.year.to_s
year_str << "–#{this_year}" if ((year_str.to_i.to_s == year_str) and (year_str.to_i != this_year.to_i))
year_str.html_safe
end
end