trabajo-afectivo/lib/calendar_subscriptions.rb

80 lines
2.2 KiB
Ruby
Raw Permalink Normal View History

2022-01-01 13:38:12 +00:00
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
class CalendarSubscriptions
def initialize(user)
@user = user
@preferences = {}
@time_zone = Setting.get('timezone_default').presence || 'UTC'
2016-02-26 12:19:57 +00:00
default_preferences = Setting.where(area: 'Defaults::CalendarSubscriptions')
default_preferences.each do |calendar_subscription|
next if calendar_subscription.name !~ %r{\Adefaults_calendar_subscriptions_(.*)\z}
object_name = $1 # rubocop:disable Lint/OutOfRangeRegexpRef
2015-09-25 14:37:55 +00:00
@preferences[ object_name ] = calendar_subscription.state_current[:value]
end
2017-11-23 08:09:44 +00:00
return if @user.preferences[:calendar_subscriptions].blank?
2016-02-26 12:19:57 +00:00
@preferences = @preferences.merge(@user.preferences[:calendar_subscriptions])
end
def all
events_data = []
2017-11-23 08:09:44 +00:00
@preferences.each_key do |object_name|
result = generic_call(object_name)
events_data += result
end
2016-02-26 12:19:57 +00:00
to_ical(events_data)
end
def generic(object_name, method_name = 'all')
events_data = generic_call(object_name, method_name)
to_ical(events_data)
end
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?
sub_class_name = object_name.to_s.capitalize
object = "CalendarSubscriptions::#{sub_class_name}".constantize
instance = object.new(@user, @preferences[ object_name ], @time_zone)
2016-02-26 12:19:57 +00:00
method = instance.method(method_name)
events_data += method.call
end
events_data
end
def to_ical(events_data)
cal = Icalendar::Calendar.new
events_data.each do |event_data|
cal.event do |e|
e.dtstart = Icalendar::Values::DateTime.new(event_data[:dtstart], 'tzid' => @time_zone)
e.dtend = Icalendar::Values::DateTime.new(event_data[:dtend], 'tzid' => @time_zone)
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
e.summary = event_data[:summary]
e.description = event_data[:description]
e.ip_class = 'PRIVATE'
end
end
cal.to_ical
end
end