2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2015-06-22 15:15:16 +00:00
|
|
|
|
2015-06-26 12:00:10 +00:00
|
|
|
class CalendarSubscriptions
|
2015-06-22 15:15:16 +00:00
|
|
|
|
2015-06-25 14:17:57 +00:00
|
|
|
def initialize(user)
|
|
|
|
@user = user
|
|
|
|
@preferences = {}
|
2021-08-26 20:58:51 +00:00
|
|
|
@time_zone = Setting.get('timezone_default').presence || 'UTC'
|
2015-06-24 17:49:01 +00:00
|
|
|
|
2016-02-26 12:19:57 +00:00
|
|
|
default_preferences = Setting.where(area: 'Defaults::CalendarSubscriptions')
|
2017-10-01 12:25:52 +00:00
|
|
|
default_preferences.each do |calendar_subscription|
|
2015-06-24 17:49:01 +00:00
|
|
|
|
2021-05-12 11:37:44 +00:00
|
|
|
next if calendar_subscription.name !~ %r{\Adefaults_calendar_subscriptions_(.*)\z}
|
2015-06-26 12:00:10 +00:00
|
|
|
|
2020-09-30 09:07:01 +00:00
|
|
|
object_name = $1 # rubocop:disable Lint/OutOfRangeRegexpRef
|
2015-09-25 14:37:55 +00:00
|
|
|
@preferences[ object_name ] = calendar_subscription.state_current[:value]
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-06-26 12:00:10 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
return if @user.preferences[:calendar_subscriptions].blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-02-26 12:19:57 +00:00
|
|
|
@preferences = @preferences.merge(@user.preferences[:calendar_subscriptions])
|
2015-06-25 14:17:57 +00:00
|
|
|
end
|
2015-06-24 17:49:01 +00:00
|
|
|
|
2015-06-25 14:17:57 +00:00
|
|
|
def all
|
|
|
|
events_data = []
|
2017-11-23 08:09:44 +00:00
|
|
|
@preferences.each_key do |object_name|
|
2021-07-16 14:10:31 +00:00
|
|
|
result = generic_call(object_name)
|
|
|
|
events_data += result
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-02-26 12:19:57 +00:00
|
|
|
to_ical(events_data)
|
2015-06-25 14:17:57 +00:00
|
|
|
end
|
2015-06-24 17:49:01 +00:00
|
|
|
|
2015-06-25 14:17:57 +00:00
|
|
|
def generic(object_name, method_name = 'all')
|
2016-02-25 15:30:33 +00:00
|
|
|
events_data = generic_call(object_name, method_name)
|
|
|
|
to_ical(events_data)
|
2015-06-24 17:49:01 +00:00
|
|
|
end
|
|
|
|
|
2015-06-25 14:17:57 +00:00
|
|
|
def generic_call(object_name, method_name = 'all')
|
|
|
|
|
|
|
|
method_name ||= 'all'
|
|
|
|
|
|
|
|
events_data = []
|
2017-11-23 08:09:44 +00:00
|
|
|
if @preferences[ object_name ].present?
|
2015-06-25 14:17:57 +00:00
|
|
|
sub_class_name = object_name.to_s.capitalize
|
2019-01-06 18:41:29 +00:00
|
|
|
object = "CalendarSubscriptions::#{sub_class_name}".constantize
|
2021-08-26 20:58:51 +00:00
|
|
|
instance = object.new(@user, @preferences[ object_name ], @time_zone)
|
2016-02-26 12:19:57 +00:00
|
|
|
method = instance.method(method_name)
|
2015-06-26 12:00:10 +00:00
|
|
|
events_data += method.call
|
2015-06-25 14:17:57 +00:00
|
|
|
end
|
|
|
|
events_data
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_ical(events_data)
|
2015-06-22 15:15:16 +00:00
|
|
|
|
|
|
|
cal = Icalendar::Calendar.new
|
|
|
|
|
|
|
|
events_data.each do |event_data|
|
|
|
|
|
|
|
|
cal.event do |e|
|
2021-08-26 20:58:51 +00:00
|
|
|
e.dtstart = Icalendar::Values::DateTime.new(event_data[:dtstart], 'tzid' => @time_zone)
|
|
|
|
e.dtend = Icalendar::Values::DateTime.new(event_data[:dtend], 'tzid' => @time_zone)
|
2016-02-25 15:30:33 +00:00
|
|
|
if event_data[:alarm]
|
|
|
|
e.alarm do |a|
|
|
|
|
a.action = 'DISPLAY'
|
|
|
|
a.summary = event_data[:alarm][:summary]
|
|
|
|
a.trigger = event_data[:alarm][:trigger]
|
|
|
|
end
|
|
|
|
end
|
2015-06-22 15:15:16 +00:00
|
|
|
e.summary = event_data[:summary]
|
|
|
|
e.description = event_data[:description]
|
|
|
|
e.ip_class = 'PRIVATE'
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
cal.to_ical
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|