2015-01-03 22:53:07 +00:00
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
2014-08-26 00:18:31 +00:00
|
|
|
|
class Observer::Ticket::Notification::BackgroundJob
|
2014-08-25 23:58:04 +00:00
|
|
|
|
def initialize(params)
|
|
|
|
|
@ticket_id = params[:ticket_id]
|
|
|
|
|
@article_id = params[:article_id]
|
2014-08-26 08:18:59 +00:00
|
|
|
|
@type = params[:type]
|
2015-01-03 22:53:07 +00:00
|
|
|
|
@changes = params[:changes]
|
2014-08-25 23:58:04 +00:00
|
|
|
|
end
|
|
|
|
|
def perform
|
|
|
|
|
ticket = Ticket.find(@ticket_id)
|
2015-01-03 22:53:07 +00:00
|
|
|
|
if @article_id
|
|
|
|
|
article = Ticket::Article.find(@article_id)
|
|
|
|
|
end
|
2014-08-25 23:58:04 +00:00
|
|
|
|
|
|
|
|
|
# find recipients
|
|
|
|
|
recipients = []
|
|
|
|
|
|
2015-01-03 22:53:07 +00:00
|
|
|
|
=begin
|
2014-08-25 23:58:04 +00:00
|
|
|
|
# group of agents to work on
|
|
|
|
|
if data[:recipient] == 'group'
|
|
|
|
|
recipients = ticket.agent_of_group()
|
|
|
|
|
|
2015-01-02 15:50:31 +00:00
|
|
|
|
# owner
|
2014-08-25 23:58:04 +00:00
|
|
|
|
elsif data[:recipient] == 'owner'
|
|
|
|
|
if ticket.owner_id != 1
|
|
|
|
|
recipients.push ticket.owner
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-02 15:50:31 +00:00
|
|
|
|
# customer
|
2014-08-25 23:58:04 +00:00
|
|
|
|
elsif data[:recipient] == 'customer'
|
|
|
|
|
if ticket.customer_id != 1
|
|
|
|
|
# temporarily disabled
|
|
|
|
|
# recipients.push ticket.customer
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-02 15:50:31 +00:00
|
|
|
|
# owner or group of agents to work on
|
2014-08-25 23:58:04 +00:00
|
|
|
|
elsif data[:recipient] == 'to_work_on'
|
|
|
|
|
if ticket.owner_id != 1
|
|
|
|
|
recipients.push ticket.owner
|
|
|
|
|
else
|
|
|
|
|
recipients = ticket.agent_of_group()
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-01-03 22:53:07 +00:00
|
|
|
|
=end
|
|
|
|
|
|
|
|
|
|
if ticket.owner_id != 1
|
|
|
|
|
recipients.push ticket.owner
|
|
|
|
|
else
|
|
|
|
|
recipients = ticket.agent_of_group()
|
|
|
|
|
end
|
2014-08-25 23:58:04 +00:00
|
|
|
|
|
|
|
|
|
# send notifications
|
2015-01-03 22:53:07 +00:00
|
|
|
|
recipient_list = ''
|
2014-08-25 23:58:04 +00:00
|
|
|
|
recipients.each do |user|
|
2015-01-02 15:50:31 +00:00
|
|
|
|
|
2015-01-02 15:53:59 +00:00
|
|
|
|
next if ticket.updated_by_id == user.id
|
|
|
|
|
next if !user.active
|
2015-01-02 15:50:31 +00:00
|
|
|
|
|
|
|
|
|
# create desktop notification
|
|
|
|
|
|
|
|
|
|
# create online notification
|
2014-08-26 07:59:59 +00:00
|
|
|
|
OnlineNotification.add(
|
2014-08-26 08:18:59 +00:00
|
|
|
|
:type => @type,
|
2014-08-26 07:59:59 +00:00
|
|
|
|
:object => 'Ticket',
|
|
|
|
|
:o_id => ticket.id,
|
|
|
|
|
:seen => false,
|
2015-01-03 22:53:07 +00:00
|
|
|
|
:created_by_id => ticket.created_by_id || 1,
|
2014-08-26 07:59:59 +00:00
|
|
|
|
:user_id => user.id,
|
|
|
|
|
)
|
|
|
|
|
|
2015-01-02 15:50:31 +00:00
|
|
|
|
# create email notification
|
2014-08-25 23:58:04 +00:00
|
|
|
|
next if !user.email || user.email == ''
|
|
|
|
|
|
|
|
|
|
# add recipient_list
|
|
|
|
|
if recipient_list != ''
|
|
|
|
|
recipient_list += ','
|
|
|
|
|
end
|
|
|
|
|
recipient_list += user.email.to_s
|
|
|
|
|
|
2015-01-04 12:52:14 +00:00
|
|
|
|
changes = self.human_changes(user, ticket)
|
|
|
|
|
next if !changes || changes.empty?
|
|
|
|
|
|
2015-01-03 22:53:07 +00:00
|
|
|
|
# get user based notification template
|
|
|
|
|
# if create, send create message / block update messages
|
|
|
|
|
if @type == 'create'
|
2015-01-04 12:52:14 +00:00
|
|
|
|
template = self.template_create(user, ticket, article, changes)
|
2015-01-03 22:53:07 +00:00
|
|
|
|
elsif @type == 'update'
|
2015-01-04 12:52:14 +00:00
|
|
|
|
template = self.template_update(user, ticket, article, changes)
|
2015-01-03 22:53:07 +00:00
|
|
|
|
else
|
|
|
|
|
raise "unknown type for notification #{@type}"
|
|
|
|
|
end
|
|
|
|
|
|
2014-08-25 23:58:04 +00:00
|
|
|
|
# prepare subject & body
|
|
|
|
|
notification = {}
|
|
|
|
|
[:subject, :body].each { |key|
|
|
|
|
|
notification[key.to_sym] = NotificationFactory.build(
|
2015-01-03 22:53:07 +00:00
|
|
|
|
:locale => user.preferences[:locale],
|
|
|
|
|
:string => template[key],
|
2014-08-25 23:58:04 +00:00
|
|
|
|
:objects => {
|
|
|
|
|
:ticket => ticket,
|
|
|
|
|
:article => article,
|
|
|
|
|
:recipient => user,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# rebuild subject
|
|
|
|
|
notification[:subject] = ticket.subject_build( notification[:subject] )
|
|
|
|
|
|
|
|
|
|
# send notification
|
2015-01-03 22:53:07 +00:00
|
|
|
|
puts "send ticket notifiaction to agent (#{@type}/#{ticket.id}/#{user.email})"
|
|
|
|
|
|
2014-08-25 23:58:04 +00:00
|
|
|
|
NotificationFactory.send(
|
|
|
|
|
:recipient => user,
|
|
|
|
|
:subject => notification[:subject],
|
|
|
|
|
:body => notification[:body]
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# add history record
|
|
|
|
|
if recipient_list != ''
|
|
|
|
|
History.add(
|
2015-01-02 23:46:11 +00:00
|
|
|
|
:o_id => ticket.id,
|
|
|
|
|
:history_type => 'notification',
|
|
|
|
|
:history_object => 'Ticket',
|
|
|
|
|
:value_to => recipient_list,
|
2015-01-03 22:53:07 +00:00
|
|
|
|
:created_by_id => ticket.updated_by_id || 1
|
2014-08-25 23:58:04 +00:00
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-01-03 22:53:07 +00:00
|
|
|
|
|
2015-01-04 12:52:14 +00:00
|
|
|
|
def human_changes(user, record)
|
|
|
|
|
|
|
|
|
|
return {} if !@changes
|
|
|
|
|
|
|
|
|
|
# only show allowed attributes
|
|
|
|
|
attribute_list = ObjectManager::Attribute.by_object_as_hash('Ticket', user)
|
2015-01-04 15:39:57 +00:00
|
|
|
|
#puts "AL #{attribute_list.inspect}"
|
2015-01-04 12:52:14 +00:00
|
|
|
|
user_related_changes = {}
|
2015-01-04 15:39:57 +00:00
|
|
|
|
@changes.each {|key, value|
|
|
|
|
|
|
|
|
|
|
# if no config exists, use all attributes
|
|
|
|
|
if !attribute_list || attribute_list.empty?
|
|
|
|
|
user_related_changes[key] = value
|
|
|
|
|
|
|
|
|
|
# if config exists, just use existing attributes for user
|
|
|
|
|
elsif attribute_list[key.to_s]
|
2015-01-04 12:52:14 +00:00
|
|
|
|
user_related_changes[key] = value
|
|
|
|
|
end
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
changes = {}
|
|
|
|
|
user_related_changes.each {|key, value|
|
|
|
|
|
|
|
|
|
|
# get attribute name
|
|
|
|
|
attribute_name = key.to_s
|
|
|
|
|
object_manager_attribute = attribute_list[attribute_name]
|
|
|
|
|
if attribute_name[-3,3] == '_id'
|
|
|
|
|
attribute_name = attribute_name[ 0, attribute_name.length-3 ]
|
|
|
|
|
end
|
|
|
|
|
if key == attribute_name
|
|
|
|
|
changes[key] = value
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
value_id = []
|
|
|
|
|
value_str = [ value[0], value[1] ]
|
|
|
|
|
if key.to_s[-3,3] == '_id'
|
|
|
|
|
value_id[0] = value[0]
|
|
|
|
|
value_id[1] = value[1]
|
|
|
|
|
|
|
|
|
|
if record.respond_to?( attribute_name ) && record.send(attribute_name)
|
|
|
|
|
relation_class = record.send(attribute_name).class
|
|
|
|
|
if relation_class && value_id[0]
|
|
|
|
|
relation_model = relation_class.lookup( :id => value_id[0] )
|
|
|
|
|
if relation_model
|
|
|
|
|
if relation_model['name']
|
|
|
|
|
value_str[0] = relation_model['name']
|
|
|
|
|
elsif relation_model.respond_to?('fullname')
|
|
|
|
|
value_str[0] = relation_model.send('fullname')
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
if relation_class && value_id[1]
|
|
|
|
|
relation_model = relation_class.lookup( :id => value_id[1] )
|
|
|
|
|
if relation_model
|
|
|
|
|
if relation_model['name']
|
|
|
|
|
value_str[1] = relation_model['name']
|
|
|
|
|
elsif relation_model.respond_to?('fullname')
|
|
|
|
|
value_str[1] = relation_model.send('fullname')
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
display = attribute_name
|
|
|
|
|
if object_manager_attribute && object_manager_attribute[:display]
|
|
|
|
|
display = object_manager_attribute[:display]
|
|
|
|
|
end
|
|
|
|
|
if object_manager_attribute && object_manager_attribute[:translate]
|
|
|
|
|
changes[display] = ["i18n(#{value_str[0].to_s})", "i18n(#{value_str[1].to_s})"]
|
|
|
|
|
else
|
|
|
|
|
changes[display] = [value_str[0].to_s, value_str[1].to_s]
|
|
|
|
|
end
|
|
|
|
|
}
|
|
|
|
|
changes
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def template_create(user, ticket, article, ticket_changes)
|
2015-01-03 22:53:07 +00:00
|
|
|
|
article_content = ''
|
|
|
|
|
if article
|
|
|
|
|
article_content = '<snip>
|
|
|
|
|
#{article.body}
|
|
|
|
|
</snip>'
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-04 12:52:14 +00:00
|
|
|
|
if user.preferences[:locale] =~ /^de/i
|
2015-01-03 22:53:07 +00:00
|
|
|
|
subject = 'Neues Ticket (#{ticket.title})'
|
|
|
|
|
body = 'Hallo #{recipient.firstname},
|
|
|
|
|
|
2015-01-04 15:39:57 +00:00
|
|
|
|
es wurde ein neues Ticket (#{ticket.title}) von "#{ticket.updated_by.fullname}" erstellt.
|
2015-01-03 22:53:07 +00:00
|
|
|
|
|
|
|
|
|
Gruppe: #{ticket.group.name}
|
|
|
|
|
Besitzer: #{ticket.owner.fullname}
|
|
|
|
|
Status: i18n(#{ticket.state.name})
|
|
|
|
|
|
|
|
|
|
' + article_content + '
|
|
|
|
|
|
|
|
|
|
'
|
|
|
|
|
else
|
|
|
|
|
|
|
|
|
|
subject = 'New Ticket (#{ticket.title})'
|
|
|
|
|
body = 'Hi #{recipient.firstname},
|
|
|
|
|
|
2015-01-04 15:39:57 +00:00
|
|
|
|
a new Ticket (#{ticket.title}) has been created by "#{ticket.updated_by.fullname}".
|
2015-01-03 22:53:07 +00:00
|
|
|
|
|
|
|
|
|
Group: #{ticket.group.name}
|
|
|
|
|
Owner: #{ticket.owner.fullname}
|
|
|
|
|
State: i18n(#{ticket.state.name})
|
|
|
|
|
|
|
|
|
|
' + article_content + '
|
|
|
|
|
|
|
|
|
|
'
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-04 12:52:14 +00:00
|
|
|
|
body = template_header(user) + body.chomp.text2html
|
|
|
|
|
body += template_footer(user, ticket, article)
|
2015-01-03 22:53:07 +00:00
|
|
|
|
|
|
|
|
|
template = {
|
|
|
|
|
:subject => subject,
|
|
|
|
|
:body => body,
|
|
|
|
|
}
|
|
|
|
|
template
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-04 12:52:14 +00:00
|
|
|
|
def template_update(user, ticket, article, ticket_changes)
|
2015-01-03 22:53:07 +00:00
|
|
|
|
changes = ''
|
|
|
|
|
ticket_changes.each {|key,value|
|
2015-01-04 12:52:14 +00:00
|
|
|
|
changes += "i18n(#{key}): #{value[0]} -> #{value[1]}\n"
|
2015-01-03 22:53:07 +00:00
|
|
|
|
}
|
|
|
|
|
article_content = ''
|
|
|
|
|
if article
|
|
|
|
|
article_content = '<snip>
|
|
|
|
|
#{article.body}
|
|
|
|
|
</snip>'
|
|
|
|
|
end
|
2015-01-04 12:52:14 +00:00
|
|
|
|
if user.preferences[:locale] =~ /^de/i
|
2015-01-03 22:53:07 +00:00
|
|
|
|
subject = 'Ticket aktualisiert (#{ticket.title})'
|
|
|
|
|
body = 'Hallo #{recipient.firstname},
|
|
|
|
|
|
2015-01-04 15:39:57 +00:00
|
|
|
|
Ticket (#{ticket.title}) wurde von "#{ticket.updated_by.fullname}" aktualisiert.
|
2015-01-03 22:53:07 +00:00
|
|
|
|
|
|
|
|
|
Änderungen:
|
|
|
|
|
' + changes + '
|
|
|
|
|
|
|
|
|
|
' + article_content + '
|
|
|
|
|
|
|
|
|
|
'
|
|
|
|
|
else
|
|
|
|
|
subject = 'Updated Ticket (#{ticket.title})'
|
|
|
|
|
body = 'Hi #{recipient.firstname},
|
|
|
|
|
|
2015-01-04 15:39:57 +00:00
|
|
|
|
Ticket (#{ticket.title}) has been updated by "#{ticket.updated_by.fullname}".
|
2015-01-03 22:53:07 +00:00
|
|
|
|
|
|
|
|
|
Changes:
|
|
|
|
|
' + changes + '
|
|
|
|
|
|
|
|
|
|
' + article_content + '
|
|
|
|
|
|
|
|
|
|
'
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-04 12:52:14 +00:00
|
|
|
|
body = template_header(user) + body.chomp.text2html
|
|
|
|
|
body += template_footer(user,ticket, article)
|
2015-01-03 22:53:07 +00:00
|
|
|
|
|
|
|
|
|
template = {
|
|
|
|
|
:subject => subject,
|
|
|
|
|
:body => body,
|
|
|
|
|
}
|
|
|
|
|
template
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-04 12:52:14 +00:00
|
|
|
|
def template_header(user)
|
2015-01-03 22:53:07 +00:00
|
|
|
|
'
|
|
|
|
|
<style type="text/css">
|
|
|
|
|
p, table, div, td {
|
|
|
|
|
max-width: 600px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
body{
|
|
|
|
|
width:90% !important;
|
|
|
|
|
-webkit-text-size-adjust:90%;
|
|
|
|
|
-ms-text-size-adjust:90%;
|
|
|
|
|
font-family: \'Helvetica Neue\', Helvetica, Arial, sans-serif;
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
}
|
|
|
|
|
img {
|
|
|
|
|
outline:none; text-decoration:none; -ms-interpolation-mode: bicubic;
|
|
|
|
|
}
|
|
|
|
|
a img {
|
|
|
|
|
border:none;
|
|
|
|
|
}
|
|
|
|
|
table td {
|
|
|
|
|
border-collapse: collapse;
|
|
|
|
|
}
|
|
|
|
|
table {
|
|
|
|
|
border-collapse:collapse; mso-table-lspace:0pt; mso-table-rspace:0pt;
|
|
|
|
|
}
|
|
|
|
|
.header {
|
|
|
|
|
color: #aaaaaa;
|
|
|
|
|
border-bottom-style:solid;
|
|
|
|
|
border-bottom-width:1px;
|
|
|
|
|
border-bottom-color:#aaaaaa;
|
|
|
|
|
width: 100%;
|
|
|
|
|
max-width: 600px;
|
|
|
|
|
padding-bottom: 6px;
|
|
|
|
|
margin-bottom: 16px;
|
|
|
|
|
padding-top: 6px;
|
|
|
|
|
font-size: 16px;
|
|
|
|
|
}
|
|
|
|
|
.footer {
|
|
|
|
|
color: #aaaaaa;
|
|
|
|
|
border-top-style:solid;
|
|
|
|
|
border-top-width:1px;
|
|
|
|
|
border-top-color:#aaaaaa;
|
|
|
|
|
width: 100%;
|
|
|
|
|
max-width: 600px;
|
|
|
|
|
padding-top: 6px;
|
|
|
|
|
margin-top: 16px;
|
|
|
|
|
padding-botton: 6px;
|
|
|
|
|
}
|
|
|
|
|
.footer a {
|
|
|
|
|
color: #aaaaaa;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
<div class="header">
|
|
|
|
|
#{config.product_name} i18n(Notification)
|
|
|
|
|
</div>
|
|
|
|
|
'
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-04 12:52:14 +00:00
|
|
|
|
def template_footer(user, ticket, article)
|
2015-01-03 22:53:07 +00:00
|
|
|
|
'
|
|
|
|
|
<a href="#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}">i18n(View the Ticket directly here)</a>
|
|
|
|
|
|
|
|
|
|
<div class="footer">
|
|
|
|
|
<a href="#{config.http_type}://#{config.fqdn}/#profile/notifications">i18n(Manage your notifications settings)</a>
|
|
|
|
|
</div>
|
|
|
|
|
'
|
|
|
|
|
end
|
2014-08-25 23:58:04 +00:00
|
|
|
|
end
|