diff options
Diffstat (limited to 'app/helpers')
| -rw-r--r-- | app/helpers/newapplication_helper.rb | 8 | ||||
| -rw-r--r-- | app/helpers/time_tables_helper.rb | 177 |
2 files changed, 180 insertions, 5 deletions
diff --git a/app/helpers/newapplication_helper.rb b/app/helpers/newapplication_helper.rb index f5b898731..42fca489f 100644 --- a/app/helpers/newapplication_helper.rb +++ b/app/helpers/newapplication_helper.rb @@ -16,7 +16,7 @@ module NewapplicationHelper end columns.map do |k, v| - if ["ID Codif", "Oid", "OiD", "ID Reflex", "Arrêt de départ", "Arrêt d'arrivée"].include? k + if ["ID Codif", "Oid", "OiD", "ID Reflex", "Arrêt de départ", "Arrêt d'arrivée", "Période de validité englobante"].include? k hcont << content_tag(:th, k) else hcont << content_tag(:th, sortable_columns(collection, k)) @@ -48,7 +48,7 @@ module NewapplicationHelper else item.try(attribute) end - if attribute == 'name' + if attribute == 'name' or attribute == 'comment' lnk = [] unless item.class == Calendar or item.class == Referential @@ -58,7 +58,7 @@ module NewapplicationHelper lnk << item.route.line if item.class == Chouette::RoutingConstraintZone lnk << item if item.respond_to? :line_referential lnk << item.stop_area if item.respond_to? :stop_area - lnk << item if item.respond_to? :stop_points + lnk << item if item.respond_to? :stop_points or item.class.to_s == 'Chouette::TimeTable' elsif item.respond_to? :referential lnk << item.referential end @@ -109,7 +109,7 @@ module NewapplicationHelper polymorph_url << item.route.line if item.class == Chouette::RoutingConstraintZone polymorph_url << item if item.respond_to? :line_referential polymorph_url << item.stop_area if item.respond_to? :stop_area - polymorph_url << item if item.respond_to? :stop_points + polymorph_url << item if item.respond_to? :stop_points or item.class.to_s == 'Chouette::TimeTable' elsif item.respond_to? :referential polymorph_url << item.referential end diff --git a/app/helpers/time_tables_helper.rb b/app/helpers/time_tables_helper.rb index 9fdb791b1..b380a2b0a 100644 --- a/app/helpers/time_tables_helper.rb +++ b/app/helpers/time_tables_helper.rb @@ -1,3 +1,178 @@ +require 'date' + module TimeTablesHelper -end + def month_periode_enum(years) + start_date = Date.today - years.years + end_date = Date.today + years.years + (start_date..end_date).map(&:beginning_of_month).uniq.map(&:to_s) + end + + def new_alt_calendar(options = {}, &block) + raise(ArgumentError, "No year given") unless options.has_key?(:year) + raise(ArgumentError, "No month given") unless options.has_key?(:month) + + block ||= Proc.new {|d| nil} + + month_names = (!defined?(I18n) || I18n.t("date.month_names").include?("missing")) ? Date::MONTHNAMES.dup : I18n.t("date.month_names") + + defaults = { + :table_id => "calendar-#{options[:year]}-#{"%02d" % options[:month]}", + :table_class => 'calendar', + :month_name_class => 'monthName', + :other_month_class => 'outsideMonth', + :day_name_class => 'dayName', + :day_class => 'day', + :abbrev => false, + :first_day_of_week => 0, + :accessible => false, + :show_today => true, + :previous_month_text => nil, + :next_month_text => nil, + :month_header => true, + :calendar_title => month_names[options[:month]], + :summary => "Calendrier pour #{month_names[options[:month]]} #{options[:year]}" + } + options = defaults.merge options + + first = Date.civil(options[:year], options[:month], 1) + last = Date.civil(options[:year], options[:month], -1) + + first_weekday = first_day_of_week(options[:first_day_of_week]) + last_weekday = last_day_of_week(options[:first_day_of_week]) + + day_names = (!defined?(I18n) || I18n.t("date.day_names").include?("missing")) ? Date::DAYNAMES : I18n.t("date.day_names") + abbr_day_names = (!defined?(I18n) || I18n.t("date.abbr_day_names").include?("missing")) ? Date::ABBR_DAYNAMES : I18n.t("date.abbr_day_names") + week_days = (0..6).to_a + first_weekday.times do + week_days.push(week_days.shift) + end + + # TODO Use some kind of builder instead of straight HTML + cal = %(<table id="#{options[:table_id]}" class="#{options[:table_class]}" border="0" cellspacing="0" cellpadding="0" summary="#{options[:summary]}">) + cal << %(<thead>) + + if (options[:month_header]) + cal << %(<tr>) + cal << %(<th class='weekNumber' scope="col"></th>) + if options[:previous_month_text] or options[:next_month_text] + cal << %(<th colspan="2">#{options[:previous_month_text]}</th>) + colspan=3 + else + colspan=7 + end + cal << %(<th colspan="#{colspan}" class="#{options[:month_name_class]}">#{options[:calendar_title]}</th>) + cal << %(<th colspan="2">#{options[:next_month_text]}</th>) if options[:next_month_text] + cal << %(</tr>) + end + + cal << %(<tr class="#{options[:day_name_class]}">) + cal << %(<th class='weekNumber' scope="col"></th>) + + week_days.each do |wday| + cal << %(<th id="#{th_id(Date::DAYNAMES[wday], options[:table_id])}" scope="col">) + cal << (options[:abbrev] ? %(<abbr title="#{day_names[wday]}">#{t("calendars.days.#{Date::DAYNAMES[wday].downcase}")}</abbr>) : t("calendars.days.#{Date::DAYNAMES[wday].downcase}")) + cal << %(</th>) + end + + cal << "</tr></thead><tbody><tr>" + + # previous month + beginning_of_week(first, first_weekday).upto(first - 1) do |d| + cal << "<td class='weekNumber'>S#{d.strftime("%W").to_s}</td>" if d.wday == first_weekday + cal << generate_other_month_cell(d, options) + end unless first.wday == first_weekday + + first.upto(last) do |cur| + cell_text, cell_attrs = block.call(cur) + cell_text ||= cur.mday + cell_attrs ||= {} + cell_attrs[:headers] = th_id(cur, options[:table_id]) + cell_attrs[:class] ||= options[:day_class] + cell_attrs[:class] += " weekend" if [0, 6].include?(cur.wday) + today = (Time.respond_to?(:zone) && !(zone = Time.zone).nil? ? zone.now.to_date : Date.today) + cell_attrs[:class] += " today" if (cur == today) and options[:show_today] + + cal << "<td class='weekNumber'>S#{cur.strftime("%W").to_s}</td>" if cur.wday == first_weekday + + cal << generate_cell(cell_text, cell_attrs) + cal << "</tr><tr>" if cur.wday == last_weekday + end + + # next month + (last + 1).upto(beginning_of_week(last + 7, first_weekday) - 1) do |d| + cal << generate_other_month_cell(d, options) + end unless last.wday == last_weekday + + cal << "</tr></tbody></table>" + cal.respond_to?(:html_safe) ? cal.html_safe : cal + end + + private + + def first_day_of_week(day) + day + end + + def last_day_of_week(day) + if day > 0 + day - 1 + else + 6 + end + end + + def days_between(first, second) + if first > second + second + (7 - first) + else + second - first + end + end + + def beginning_of_week(date, start = 1) + days_to_beg = days_between(start, date.wday) + date - days_to_beg + end + + def generate_cell(cell_text, cell_attrs) + cell_attrs = cell_attrs.map {|k, v| %(#{k}="#{v}") }.join(" ") + "<td #{cell_attrs}>#{cell_text}</td>" + end + + def generate_other_month_cell(date, options) + cell_attrs = {} + cell_attrs[:headers] = th_id(date, options[:table_id]) + cell_attrs[:class] = options[:other_month_class] + cell_attrs[:class] += " weekend" if weekend?(date) + cell_attrs[:title] ||= date.strftime("%W").to_i if options[:first_day_of_week] == 1 + + cell_text = date.day + if options[:accessible] + cell_text += %(<span class="hidden"> #{month_names[date.month]}</span>) + end + + generate_cell(date.day, cell_attrs) + end + + # Calculates id for th element. + # derived from calendar_id and dow. + # + # Params: + # `day` can be either Date or DOW('Sunday', 'Monday') + def th_id(day, calendar_id) + return th_id(Date::DAYNAMES[day.wday], calendar_id) if day.is_a?(Date) + "#{calendar_id}-#{day[0..2].downcase}" + end + + def weekend?(date) + [0, 6].include?(date.wday) + end + + class Engine < Rails::Engine # :nodoc: + ActiveSupport.on_load(:action_view) do + include CalendarHelper + end + end if defined? Rails::Engine + +end |
