module CmsApplicationHelper # Get an array of all times, useful in select's (5 minute interval by default) def all_times_array(interval = 5) a = [] (0..23).each do |h| (0..59).each do |m| next unless m % interval == 0 t = Time.mktime(2000, 1, 1, h, m) a << t.strftime("%I").to_i.to_s + t.strftime(":%M%p").downcase end end a end # Takes object_name and method_name as arguments (like other form helpers, such # as text_field) and returns html containing form_error.gif and form_loading.gif. # If there are no errors for the given field, form_error.gif is hidden using # style="display: none". If there are errors, form_error.gif is shown, and its # hover text lists the errors. def form_icons(object_name, method_name, options = {}) object_name = object_name.to_s method_name = method_name.to_s ret = '' errors = [] instance_variable_get("@#{object_name}").errors.each do |attr, msg| errors << msg if attr == method_name end options[:style] ||= '' if errors.size == 0 options[:style] << 'display: none;' end ret << "
#{message}
" output << "#{flash[:error] || @error}
" end if (flash[:notice] || @notice || '') != '' output << "#{flash[:notice] || @notice}
" end output end # Similar to button_to, but takes a url for a button image as its first argument. def image_button_to(source, options = {}, html_options = {}) html_options.stringify_keys! html_options[:type] = 'image' html_options[:src] = image_path(source) convert_boolean_attributes!(html_options, %w( disabled )) if confirm = html_options.delete("confirm") html_options["onclick"] = "return #{confirm_javascript_function(confirm)};" end url = options.is_a?(String) ? options : url_for(options) name ||= url "" end # Similar to submit_to_remote, but takes a url for a button image as its # first argument. def image_submit_to_remote(source, options = {}) options[:with] ||= 'Form.serialize(this.form)' options[:html] ||= {} options[:html][:type] = 'image' options[:html][:onclick] = "#{remote_function(options)}; return false;" options[:html][:src] = image_path(source) tag("input", options[:html], false) end # Creates a mailto: link that is encoded to prevent most harvesting attempts. def encoded_mail_to(email, link_text = nil) url = '' text = '' email.length.times do |i| url << (i % 2 == 0 ? sprintf("%%%x", email[i]) : email[i]) text << (i % 4 == 0 ? '' << email[i] << '' : email[i]) end "#{link_text || text}" end # Display a date picker with an ajax calendar. # # Options: # * :default_value => Time.now # * :start_date => '06/01/2006', :end_date => '05/31/2007' # * :exclude_days => [ :monday, :saturday, :sunday ] # * :blackout_ranges => [ ['06/04/2006', '06/18/2006'], ['08/16/2006', '09/01/2006'] ] (not implemented) # # TODOC: There are a number of somewhat complex prerequisites... def date_picker(object, method_prefix, options = {}) object = object.to_s method_prefix = method_prefix.to_s # set some arbitrary but sensible limits for now... start_date = options[:start_date] || 5.years.ago start_date = Time.parse(start_date) if start_date.kind_of? String end_date = options[:end_date] || 10.years.from_now end_date = Time.parse(end_date) if end_date.kind_of? String onchange = options[:onchange] || '' exclude_days = options[:exclude_days] || [] min_year = start_date.year max_year = end_date.year exclude_days.map! do |d| case (d) when :sunday then 0 when :monday then 1 when :tuesday then 2 when :wednesday then 3 when :thursday then 4 when :friday then 5 when :saturday then 6 else nil end end i = 0 while exclude_days.include?(start_date.wday) && i < 7 start_date += 1.day i += 1 end default_value = options[:default_value] || (instance_variable_get("@#{object}").send(method_prefix) rescue nil) default_value = Time.parse(default_value) if default_value.is_a?(String) default_value ||= start_date draw_calendar = "new Ajax.Updater('date_picker_#{object}_#{method_prefix}_days', '" + date_picker_url + "?" + "month=' + $('#{object}_#{method_prefix}_month_sel').value + " + "'&year=' + $('#{object}_#{method_prefix}_year_sel').value + " + "'&min_time=' + #{start_date.to_i} + " + "'&max_time=' + #{end_date.to_i} + " + "'&exclude_days=#{exclude_days.join(',')}' + " + "'&onchange=#{escape_javascript(options[:onchange])}' + " + "'&object=#{object}' + " + "'&method_prefix=#{method_prefix}', {asynchronous:true, evalScripts:true})" ret = <