calendar_subscriptions.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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]
  13. return if @user.preferences[:calendar_subscriptions].empty?
  14. @preferences = @preferences.merge(@user.preferences[:calendar_subscriptions])
  15. end
  16. def all
  17. events_data = []
  18. @preferences.each do |object_name, _sub_structure|
  19. result = generic_call(object_name)
  20. events_data = events_data + result
  21. end
  22. to_ical(events_data)
  23. end
  24. def generic(object_name, method_name = 'all')
  25. events_data = generic_call(object_name, method_name)
  26. to_ical(events_data)
  27. end
  28. def generic_call(object_name, method_name = 'all')
  29. method_name ||= 'all'
  30. events_data = []
  31. if @preferences[ object_name ] && !@preferences[ object_name ].empty?
  32. sub_class_name = object_name.to_s.capitalize
  33. object = Object.const_get("CalendarSubscriptions::#{sub_class_name}")
  34. instance = object.new(@user, @preferences[ object_name ])
  35. method = instance.method(method_name)
  36. events_data += method.call
  37. end
  38. events_data
  39. end
  40. def to_ical(events_data)
  41. cal = Icalendar::Calendar.new
  42. events_data.each do |event_data|
  43. cal.event do |e|
  44. e.dtstart = event_data[:dtstart]
  45. e.dtend = event_data[:dtend]
  46. if event_data[:alarm]
  47. e.alarm do |a|
  48. a.action = 'DISPLAY'
  49. a.summary = event_data[:alarm][:summary]
  50. a.trigger = event_data[:alarm][:trigger]
  51. end
  52. end
  53. e.summary = event_data[:summary]
  54. e.description = event_data[:description]
  55. e.ip_class = 'PRIVATE'
  56. end
  57. end
  58. cal.to_ical
  59. end
  60. end