calendar_subscriptions.rb 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class CalendarSubscriptions
  3. def initialize(user)
  4. @user = user
  5. @preferences = {}
  6. default_preferences = Setting.where(area: 'Defaults::CalendarSubscriptions')
  7. default_preferences.each do |calendar_subscription|
  8. next if calendar_subscription.name !~ /\Adefaults_calendar_subscriptions_(.*)\z/
  9. object_name = $1
  10. @preferences[ object_name ] = calendar_subscription.state_current[:value]
  11. end
  12. return if @user.preferences[:calendar_subscriptions].blank?
  13. @preferences = @preferences.merge(@user.preferences[:calendar_subscriptions])
  14. end
  15. def all
  16. events_data = []
  17. @preferences.each_key do |object_name|
  18. result = generic_call(object_name)
  19. events_data = events_data + result
  20. end
  21. to_ical(events_data)
  22. end
  23. def generic(object_name, method_name = 'all')
  24. events_data = generic_call(object_name, method_name)
  25. to_ical(events_data)
  26. end
  27. def generic_call(object_name, method_name = 'all')
  28. method_name ||= 'all'
  29. events_data = []
  30. if @preferences[ object_name ].present?
  31. sub_class_name = object_name.to_s.capitalize
  32. object = Object.const_get("CalendarSubscriptions::#{sub_class_name}")
  33. instance = object.new(@user, @preferences[ object_name ])
  34. method = instance.method(method_name)
  35. events_data += method.call
  36. end
  37. events_data
  38. end
  39. def to_ical(events_data)
  40. cal = Icalendar::Calendar.new
  41. events_data.each do |event_data|
  42. cal.event do |e|
  43. e.dtstart = Icalendar::Values::DateTime.new(event_data[:dtstart], 'tzid' => 'UTC')
  44. e.dtend = Icalendar::Values::DateTime.new(event_data[:dtend], 'tzid' => 'UTC')
  45. if event_data[:alarm]
  46. e.alarm do |a|
  47. a.action = 'DISPLAY'
  48. a.summary = event_data[:alarm][:summary]
  49. a.trigger = event_data[:alarm][:trigger]
  50. end
  51. end
  52. e.summary = event_data[:summary]
  53. e.description = event_data[:description]
  54. e.ip_class = 'PRIVATE'
  55. end
  56. end
  57. cal.to_ical
  58. end
  59. end