calendar_subscriptions.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class CalendarSubscriptions
  3. def initialize(user)
  4. @user = user
  5. @preferences = {}
  6. @time_zone = Setting.get('timezone_default').presence || 'UTC'
  7. default_preferences = Setting.where(area: 'Defaults::CalendarSubscriptions')
  8. default_preferences.each do |calendar_subscription|
  9. next if calendar_subscription.name !~ %r{\Adefaults_calendar_subscriptions_(.*)\z}
  10. object_name = $1 # rubocop:disable Lint/OutOfRangeRegexpRef
  11. @preferences[ object_name ] = calendar_subscription.state_current[:value]
  12. end
  13. return if @user.preferences[:calendar_subscriptions].blank?
  14. @preferences = @preferences.merge(@user.preferences[:calendar_subscriptions])
  15. end
  16. def all
  17. events_data = []
  18. @preferences.each_key do |object_name|
  19. result = generic_call(object_name)
  20. 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 ].present?
  32. sub_class_name = object_name.to_s.capitalize
  33. object = "CalendarSubscriptions::#{sub_class_name}".constantize
  34. instance = object.new(@user, @preferences[ object_name ], @time_zone)
  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 = Icalendar::Values::DateTime.new(event_data[:dtstart], 'tzid' => @time_zone)
  45. e.dtend = Icalendar::Values::DateTime.new(event_data[:dtend], 'tzid' => @time_zone)
  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