2015-06-22 15:15:16 +00:00
|
|
|
# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2015-06-25 14:17:57 +00:00
|
|
|
class ICal
|
2015-06-22 15:15:16 +00:00
|
|
|
|
2015-06-25 14:17:57 +00:00
|
|
|
def initialize(user)
|
|
|
|
@user = user
|
|
|
|
@preferences = {}
|
2015-06-24 17:49:01 +00:00
|
|
|
|
|
|
|
|
2015-06-25 14:17:57 +00:00
|
|
|
if @user.preferences[:ical] && !@user.preferences[:ical].empty?
|
|
|
|
@preferences = @user.preferences[:ical]
|
|
|
|
end
|
|
|
|
end
|
2015-06-24 17:49:01 +00:00
|
|
|
|
2015-06-25 14:17:57 +00:00
|
|
|
def all
|
|
|
|
events_data = []
|
|
|
|
@preferences.each { |object_name, _sub_structure|
|
|
|
|
result = generic_call( object_name )
|
|
|
|
events_data = events_data + result
|
2015-06-24 17:49:01 +00:00
|
|
|
}
|
2015-06-25 14:17:57 +00:00
|
|
|
to_ical( events_data )
|
|
|
|
end
|
2015-06-24 17:49:01 +00:00
|
|
|
|
2015-06-25 14:17:57 +00:00
|
|
|
def generic(object_name, method_name = 'all')
|
|
|
|
|
|
|
|
events_data = generic_call( object_name, method_name )
|
2015-06-24 17:49:01 +00:00
|
|
|
to_ical( events_data )
|
|
|
|
end
|
|
|
|
|
2015-06-25 14:17:57 +00:00
|
|
|
def generic_call(object_name, method_name = 'all')
|
|
|
|
|
|
|
|
method_name ||= 'all'
|
|
|
|
|
|
|
|
events_data = []
|
|
|
|
if @preferences[ object_name ] && !@preferences[ object_name ].empty?
|
|
|
|
sub_class_name = object_name.to_s.capitalize
|
|
|
|
object = Object.const_get('ICal').const_get("ICal#{sub_class_name}")
|
|
|
|
instance = object.new( @user, @preferences[ object_name ] )
|
|
|
|
method = instance.method( method_name )
|
|
|
|
events_data += method.call
|
|
|
|
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|
|
|
|
|
e.dtstart = event_data[:dtstart]
|
|
|
|
e.dtend = event_data[:dtend]
|
|
|
|
e.summary = event_data[:summary]
|
|
|
|
e.description = event_data[:description]
|
|
|
|
e.ip_class = 'PRIVATE'
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
cal.to_ical
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|