2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2015-06-26 12:00:10 +00:00
|
|
|
|
|
|
|
class CalendarSubscriptions::Tickets
|
|
|
|
|
|
|
|
def initialize(user, preferences)
|
|
|
|
@user = user
|
|
|
|
@preferences = preferences
|
2016-02-25 15:30:33 +00:00
|
|
|
@tzid = 'UTC'
|
2015-06-26 12:00:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def all
|
|
|
|
|
|
|
|
events_data = []
|
2017-11-15 14:06:53 +00:00
|
|
|
return events_data if @preferences.blank?
|
2015-06-26 12:00:10 +00:00
|
|
|
|
|
|
|
events_data += new_open
|
|
|
|
events_data += pending
|
|
|
|
events_data += escalation
|
|
|
|
|
|
|
|
events_data
|
|
|
|
end
|
|
|
|
|
2016-02-29 12:05:21 +00:00
|
|
|
def alarm?
|
|
|
|
|
|
|
|
alarm = false
|
|
|
|
|
2017-11-15 14:06:53 +00:00
|
|
|
return alarm if @preferences.blank?
|
2016-02-29 12:05:21 +00:00
|
|
|
return alarm if !@preferences[:alarm]
|
|
|
|
|
|
|
|
@preferences[:alarm]
|
|
|
|
end
|
|
|
|
|
2015-06-26 12:00:10 +00:00
|
|
|
def owner_ids(method)
|
|
|
|
|
|
|
|
owner_ids = []
|
|
|
|
|
2017-11-15 14:06:53 +00:00
|
|
|
return owner_ids if @preferences.blank?
|
|
|
|
return owner_ids if !@preferences[method]
|
|
|
|
return owner_ids if @preferences[method].blank?
|
2015-06-26 12:00:10 +00:00
|
|
|
|
2017-11-15 14:06:53 +00:00
|
|
|
preferences = @preferences[method]
|
2015-06-26 12:00:10 +00:00
|
|
|
|
|
|
|
if preferences[:own]
|
|
|
|
owner_ids = [ @user.id ]
|
|
|
|
end
|
|
|
|
if preferences[:not_assigned]
|
|
|
|
owner_ids.push( 1 )
|
|
|
|
end
|
|
|
|
|
|
|
|
owner_ids
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_open
|
|
|
|
|
|
|
|
events_data = []
|
|
|
|
owner_ids = owner_ids(:new_open)
|
2017-11-15 14:06:53 +00:00
|
|
|
return events_data if owner_ids.blank?
|
2015-06-26 12:00:10 +00:00
|
|
|
|
|
|
|
condition = {
|
2015-09-28 12:02:12 +00:00
|
|
|
'ticket.owner_id' => {
|
2015-09-17 18:39:51 +00:00
|
|
|
operator: 'is',
|
2018-12-19 17:31:51 +00:00
|
|
|
value: owner_ids,
|
2015-09-17 18:39:51 +00:00
|
|
|
},
|
2015-09-28 12:02:12 +00:00
|
|
|
'ticket.state_id' => {
|
2015-09-17 18:39:51 +00:00
|
|
|
operator: 'is',
|
2018-12-19 17:31:51 +00:00
|
|
|
value: Ticket::State.where(
|
2015-09-17 18:39:51 +00:00
|
|
|
state_type_id: Ticket::StateType.where(
|
2017-11-23 08:09:44 +00:00
|
|
|
name: %w[new open],
|
2015-09-17 18:39:51 +00:00
|
|
|
),
|
|
|
|
).map(&:id),
|
|
|
|
},
|
2015-06-26 12:00:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tickets = Ticket.search(
|
|
|
|
current_user: @user,
|
2018-12-19 17:31:51 +00:00
|
|
|
condition: condition,
|
2015-06-26 12:00:10 +00:00
|
|
|
)
|
|
|
|
|
2017-11-15 14:06:53 +00:00
|
|
|
user_locale = @user.preferences['locale'] || Setting.get('locale_default') || 'en-us'
|
2015-06-29 08:54:47 +00:00
|
|
|
translated_ticket = Translation.translate(user_locale, 'ticket')
|
|
|
|
|
2015-06-26 12:00:10 +00:00
|
|
|
events_data = []
|
|
|
|
tickets.each do |ticket|
|
|
|
|
|
|
|
|
event_data = {}
|
|
|
|
|
2015-06-29 08:54:47 +00:00
|
|
|
translated_state = Translation.translate(user_locale, ticket.state.name)
|
|
|
|
|
2016-02-25 15:30:33 +00:00
|
|
|
event_data[:dtstart] = Icalendar::Values::Date.new(Time.zone.today, 'tzid' => @tzid)
|
|
|
|
event_data[:dtend] = Icalendar::Values::Date.new(Time.zone.today, 'tzid' => @tzid)
|
2015-06-29 08:54:47 +00:00
|
|
|
event_data[:summary] = "#{translated_state} #{translated_ticket}: '#{ticket.title}'"
|
2015-06-26 12:00:10 +00:00
|
|
|
event_data[:description] = "T##{ticket.number}"
|
|
|
|
|
|
|
|
events_data.push event_data
|
|
|
|
end
|
|
|
|
|
|
|
|
events_data
|
|
|
|
end
|
|
|
|
|
|
|
|
def pending
|
|
|
|
|
|
|
|
events_data = []
|
|
|
|
owner_ids = owner_ids(:pending)
|
2017-11-15 14:06:53 +00:00
|
|
|
return events_data if owner_ids.blank?
|
2015-06-26 12:00:10 +00:00
|
|
|
|
|
|
|
condition = {
|
2015-09-28 12:02:12 +00:00
|
|
|
'ticket.owner_id' => {
|
2015-09-17 18:39:51 +00:00
|
|
|
operator: 'is',
|
2018-12-19 17:31:51 +00:00
|
|
|
value: owner_ids,
|
2015-09-17 18:39:51 +00:00
|
|
|
},
|
2015-09-28 12:02:12 +00:00
|
|
|
'ticket.state_id' => {
|
2015-09-17 18:39:51 +00:00
|
|
|
operator: 'is',
|
2018-12-19 17:31:51 +00:00
|
|
|
value: Ticket::State.where(
|
2015-09-17 18:39:51 +00:00
|
|
|
state_type_id: Ticket::StateType.where(
|
|
|
|
name: [
|
|
|
|
'pending reminder',
|
|
|
|
'pending action',
|
|
|
|
],
|
|
|
|
),
|
|
|
|
).map(&:id),
|
|
|
|
},
|
2015-06-26 12:00:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tickets = Ticket.search(
|
|
|
|
current_user: @user,
|
2018-12-19 17:31:51 +00:00
|
|
|
condition: condition,
|
2015-06-26 12:00:10 +00:00
|
|
|
)
|
|
|
|
|
2017-11-15 14:06:53 +00:00
|
|
|
user_locale = @user.preferences['locale'] || Setting.get('locale_default') || 'en-us'
|
2015-06-29 08:54:47 +00:00
|
|
|
translated_ticket = Translation.translate(user_locale, 'ticket')
|
2016-02-25 15:30:33 +00:00
|
|
|
customer = Translation.translate(user_locale, 'customer')
|
2015-06-29 08:54:47 +00:00
|
|
|
|
2015-06-26 12:00:10 +00:00
|
|
|
events_data = []
|
|
|
|
tickets.each do |ticket|
|
|
|
|
|
|
|
|
next if !ticket.pending_time
|
|
|
|
|
|
|
|
event_data = {}
|
|
|
|
|
|
|
|
pending_time = ticket.pending_time
|
|
|
|
if pending_time < Time.zone.today
|
|
|
|
pending_time = Time.zone.today
|
|
|
|
end
|
|
|
|
|
2015-06-29 08:54:47 +00:00
|
|
|
translated_state = Translation.translate(user_locale, ticket.state.name)
|
|
|
|
|
2016-02-25 15:30:33 +00:00
|
|
|
event_data[:dtstart] = Icalendar::Values::DateTime.new(pending_time, 'tzid' => @tzid)
|
|
|
|
event_data[:dtend] = Icalendar::Values::DateTime.new(pending_time, 'tzid' => @tzid)
|
|
|
|
event_data[:summary] = "#{translated_state} #{translated_ticket}: '#{ticket.title}' #{customer}: #{ticket.customer.longname}"
|
2015-06-26 12:00:10 +00:00
|
|
|
event_data[:description] = "T##{ticket.number}"
|
2016-02-29 12:05:21 +00:00
|
|
|
if alarm?
|
|
|
|
event_data[:alarm] = {
|
|
|
|
summary: event_data[:summary],
|
|
|
|
trigger: '-PT1M',
|
|
|
|
}
|
|
|
|
end
|
2015-06-26 12:00:10 +00:00
|
|
|
|
|
|
|
events_data.push event_data
|
|
|
|
end
|
|
|
|
|
|
|
|
events_data
|
|
|
|
end
|
|
|
|
|
|
|
|
def escalation
|
|
|
|
|
|
|
|
events_data = []
|
|
|
|
owner_ids = owner_ids(:escalation)
|
2017-11-15 14:06:53 +00:00
|
|
|
return events_data if owner_ids.blank?
|
2015-06-26 12:00:10 +00:00
|
|
|
|
2015-09-17 18:39:51 +00:00
|
|
|
condition = {
|
2018-12-19 17:31:51 +00:00
|
|
|
'ticket.owner_id' => {
|
2015-09-17 18:39:51 +00:00
|
|
|
operator: 'is',
|
2018-12-19 17:31:51 +00:00
|
|
|
value: owner_ids,
|
2015-09-17 18:39:51 +00:00
|
|
|
},
|
2016-09-14 07:15:30 +00:00
|
|
|
'ticket.escalation_at' => {
|
2015-09-17 18:39:51 +00:00
|
|
|
operator: 'is not',
|
2018-12-19 17:31:51 +00:00
|
|
|
value: nil,
|
2015-09-17 18:39:51 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-26 12:00:10 +00:00
|
|
|
|
|
|
|
tickets = Ticket.search(
|
|
|
|
current_user: @user,
|
2018-12-19 17:31:51 +00:00
|
|
|
condition: condition,
|
2015-06-26 12:00:10 +00:00
|
|
|
)
|
|
|
|
|
2017-11-15 14:06:53 +00:00
|
|
|
user_locale = @user.preferences['locale'] || Setting.get('locale_default') || 'en-us'
|
2015-06-29 08:54:47 +00:00
|
|
|
translated_ticket_escalation = Translation.translate(user_locale, 'ticket escalation')
|
2016-02-25 15:30:33 +00:00
|
|
|
customer = Translation.translate(user_locale, 'customer')
|
2015-06-29 08:54:47 +00:00
|
|
|
|
2015-06-26 12:00:10 +00:00
|
|
|
tickets.each do |ticket|
|
|
|
|
|
2016-09-14 07:15:30 +00:00
|
|
|
next if !ticket.escalation_at
|
2015-06-26 12:00:10 +00:00
|
|
|
|
|
|
|
event_data = {}
|
|
|
|
|
2016-09-14 07:15:30 +00:00
|
|
|
escalation_at = ticket.escalation_at
|
|
|
|
if escalation_at < Time.zone.today
|
|
|
|
escalation_at = Time.zone.today
|
2015-06-26 12:00:10 +00:00
|
|
|
end
|
|
|
|
|
2016-09-14 07:15:30 +00:00
|
|
|
event_data[:dtstart] = Icalendar::Values::DateTime.new(escalation_at, 'tzid' => @tzid)
|
|
|
|
event_data[:dtend] = Icalendar::Values::DateTime.new(escalation_at, 'tzid' => @tzid)
|
2016-02-26 12:19:57 +00:00
|
|
|
event_data[:summary] = "#{translated_ticket_escalation}: '#{ticket.title}' #{customer}: #{ticket.customer.longname}"
|
2015-06-26 12:00:10 +00:00
|
|
|
event_data[:description] = "T##{ticket.number}"
|
2016-02-29 12:05:21 +00:00
|
|
|
if alarm?
|
|
|
|
event_data[:alarm] = {
|
|
|
|
summary: event_data[:summary],
|
|
|
|
trigger: '-PT1M',
|
|
|
|
}
|
|
|
|
end
|
2015-06-26 12:00:10 +00:00
|
|
|
|
|
|
|
events_data.push event_data
|
|
|
|
end
|
|
|
|
|
|
|
|
events_data
|
|
|
|
end
|
|
|
|
end
|