module RedmineUp module CalendarHelper # Simple class to compute the start and end dates of a calendar class Calendar attr_reader :startdt, :enddt def initialize(date, options={}, lang = current_language, period = :month) @date = date @events = [] @ending_events_by_days = {} @starting_events_by_days = {} # ADDED FOR CONTACTS-DEALS @start_date_field = options[:start_date_field] || "start_date" @due_date_field = options[:due_date_field] || "due_date" # /ADDED FOR CONTACTS-DEALS set_language_if_valid lang case period when :month @startdt = Date.civil(date.year, date.month, 1) @enddt = (@startdt >> 1)-1 # starts from the first day of the week @startdt = @startdt - (@startdt.cwday - first_wday)%7 # ends on the last day of the week @enddt = @enddt + (last_wday - @enddt.cwday)%7 when :week @startdt = date - (date.cwday - first_wday)%7 @enddt = date + (last_wday - date.cwday)%7 else raise 'Invalid period' end end def format_month (@startdt..@enddt).to_a end def week_number(day) (day + (11 - day.cwday) % 7).cweek end def day_css_classes(day) css = day.month==month ? +'even' : +'odd' css << " today" if User.current.today == day css << " nwday" if non_working_week_days.include?(day.cwday) css end # Sets calendar events def events=(events) @events = events # CHANGED FOR CONTACTS-DEALS @ending_events_by_days = @events.group_by {|event| event.send(@start_date_field) ? event.send(@start_date_field).to_date : event.due_date} @starting_events_by_days = @events.group_by {|event| event.send(@due_date_field) ? event.send(@due_date_field).to_date : event.start_date} # /CHANGED FOR CONTACTS-DEALS end # Returns events for the given day def events_on(day) ((@ending_events_by_days[day] || []) + (@starting_events_by_days[day] || [])).uniq end # Calendar current month def month @date.month end # Return the first day of week # 1 = Monday ... 7 = Sunday def first_wday case Setting.start_of_week.to_i when 1 @first_dow ||= (1 - 1)%7 + 1 when 6 @first_dow ||= (6 - 1)%7 + 1 when 7 @first_dow ||= (7 - 1)%7 + 1 else @first_dow ||= (l(:general_first_day_of_week).to_i - 1)%7 + 1 end end def last_wday @last_dow ||= (first_wday + 5)%7 + 1 end def abbr_day_name(day) ::I18n.t('date.abbr_day_names')[day % 7] end def current_language ::I18n.locale end def set_language_if_valid(lang) if l = find_language(lang) ::I18n.locale = l end end def find_language(lang) @@languages_lookup ||= valid_languages.inject({}) do |k, v| k[v.to_s.downcase] = v k end @@languages_lookup[lang.to_s.downcase] end def valid_languages ::I18n.available_locales end def l(*args) case args.size when 1 ::I18n.t(*args) when 2 if args.last.is_a?(Hash) ::I18n.t(*args.first, **args.last) elsif args.last.is_a?(String) ::I18n.t(args.first, :value => args.last) else ::I18n.t(args.first, :count => args.last) end else raise "Translation string with multiple values: #{args.first}" end end def non_working_week_days @non_working_week_days ||= begin days = Setting.non_working_week_days if days.is_a?(Array) && days.size < 7 days.map(&:to_i) else [] end end end end end end unless ActionView::Base.included_modules.include?(RedmineUp::CalendarHelper) ActionView::Base.send(:include, RedmineUp::CalendarHelper) end