2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2016-05-03 00:36:44 +00:00
|
|
|
|
|
|
|
class Transaction::Trigger
|
|
|
|
|
|
|
|
=begin
|
|
|
|
{
|
|
|
|
object: 'Ticket',
|
|
|
|
type: 'update',
|
|
|
|
object_id: 123,
|
2016-08-20 19:29:22 +00:00
|
|
|
interface_handle: 'application_server', # application_server|websocket|scheduler
|
2016-05-03 00:36:44 +00:00
|
|
|
changes: {
|
|
|
|
'attribute1' => [before, now],
|
|
|
|
'attribute2' => [before, now],
|
2016-07-16 21:43:08 +00:00
|
|
|
},
|
|
|
|
created_at: Time.zone.now,
|
2016-05-03 00:36:44 +00:00
|
|
|
user_id: 123,
|
|
|
|
},
|
|
|
|
=end
|
|
|
|
|
|
|
|
def initialize(item, params = {})
|
|
|
|
@item = item
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
|
|
|
|
|
|
|
# return if we run import mode
|
|
|
|
return if Setting.get('import_mode')
|
|
|
|
|
|
|
|
return if @item[:object] != 'Ticket'
|
|
|
|
|
2017-03-03 08:28:45 +00:00
|
|
|
triggers = if Rails.configuration.db_case_sensitive
|
|
|
|
Trigger.where(active: true).order('LOWER(name)')
|
|
|
|
else
|
|
|
|
Trigger.where(active: true).order(:name)
|
|
|
|
end
|
2017-11-23 08:09:44 +00:00
|
|
|
return if triggers.blank?
|
2016-05-03 00:36:44 +00:00
|
|
|
|
|
|
|
ticket = Ticket.lookup(id: @item[:object_id])
|
|
|
|
return if !ticket
|
|
|
|
if @item[:article_id]
|
|
|
|
article = Ticket::Article.lookup(id: @item[:article_id])
|
|
|
|
end
|
|
|
|
|
2016-05-04 09:45:05 +00:00
|
|
|
original_user_id = UserInfo.current_user_id
|
2016-11-18 07:25:07 +00:00
|
|
|
|
2017-03-24 23:00:41 +00:00
|
|
|
Transaction.execute(reset_user_id: true, disable: ['Transaction::Trigger', 'Transaction::Notification']) do
|
2017-10-01 12:25:52 +00:00
|
|
|
triggers.each do |trigger|
|
2016-11-18 07:25:07 +00:00
|
|
|
condition = trigger.condition
|
|
|
|
|
2017-03-03 08:28:45 +00:00
|
|
|
# check if one article attribute is used
|
|
|
|
one_has_changed_done = false
|
|
|
|
article_selector = false
|
2017-11-23 08:09:44 +00:00
|
|
|
trigger.condition.each_key do |key|
|
2017-03-03 08:28:45 +00:00
|
|
|
(object_name, attribute) = key.split('.', 2)
|
|
|
|
next if object_name != 'article'
|
|
|
|
next if attribute == 'id'
|
|
|
|
article_selector = true
|
2016-06-30 20:04:48 +00:00
|
|
|
end
|
2017-03-03 08:28:45 +00:00
|
|
|
if article && article_selector
|
|
|
|
one_has_changed_done = true
|
|
|
|
end
|
|
|
|
if article && @item[:type] == 'update'
|
|
|
|
one_has_changed_done = true
|
2016-11-18 07:25:07 +00:00
|
|
|
end
|
2016-07-16 21:43:08 +00:00
|
|
|
|
2017-03-03 08:28:45 +00:00
|
|
|
# check ticket "has changed" options
|
|
|
|
has_changed_done = true
|
2016-11-18 07:25:07 +00:00
|
|
|
condition.each do |key, value|
|
|
|
|
next if !value
|
|
|
|
next if !value['operator']
|
|
|
|
next if !value['operator']['has changed']
|
|
|
|
|
2017-03-03 08:28:45 +00:00
|
|
|
# remove condition item, because it has changed
|
2016-11-18 07:25:07 +00:00
|
|
|
(object_name, attribute) = key.split('.', 2)
|
2017-03-03 08:28:45 +00:00
|
|
|
next if object_name != 'ticket'
|
|
|
|
next if !@item[:changes]
|
|
|
|
next if !@item[:changes].key?(attribute)
|
|
|
|
condition.delete(key)
|
|
|
|
one_has_changed_done = true
|
|
|
|
end
|
2016-11-18 07:25:07 +00:00
|
|
|
|
2017-03-03 08:28:45 +00:00
|
|
|
# check if we have not matching "has changed" attributes
|
2017-11-23 08:09:44 +00:00
|
|
|
condition.each_value do |value|
|
2017-03-03 08:28:45 +00:00
|
|
|
next if !value
|
|
|
|
next if !value['operator']
|
|
|
|
next if !value['operator']['has changed']
|
|
|
|
has_changed_done = false
|
2016-11-18 07:25:07 +00:00
|
|
|
break
|
|
|
|
end
|
|
|
|
|
2017-03-03 08:28:45 +00:00
|
|
|
# check ticket action
|
|
|
|
if condition['ticket.action']
|
|
|
|
next if condition['ticket.action']['operator'] == 'is' && condition['ticket.action']['value'] != @item[:type]
|
|
|
|
next if condition['ticket.action']['operator'] != 'is' && condition['ticket.action']['value'] == @item[:type]
|
|
|
|
condition.delete('ticket.action')
|
|
|
|
end
|
|
|
|
next if !has_changed_done
|
|
|
|
|
|
|
|
# check in min one attribute of condition has changed on update
|
|
|
|
one_has_changed_condition = false
|
|
|
|
if @item[:type] == 'update'
|
|
|
|
|
|
|
|
# verify if ticket condition exists
|
2017-11-23 08:09:44 +00:00
|
|
|
condition.each_key do |key|
|
2017-03-03 08:28:45 +00:00
|
|
|
(object_name, attribute) = key.split('.', 2)
|
|
|
|
next if object_name != 'ticket'
|
|
|
|
one_has_changed_condition = true
|
|
|
|
next if !@item[:changes] || !@item[:changes].key?(attribute)
|
|
|
|
one_has_changed_done = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
next if one_has_changed_condition && !one_has_changed_done
|
|
|
|
end
|
2016-11-18 07:25:07 +00:00
|
|
|
|
2017-03-03 08:28:45 +00:00
|
|
|
# check if ticket selector is matching
|
2016-11-18 07:25:07 +00:00
|
|
|
condition['ticket.id'] = {
|
2016-05-03 13:51:07 +00:00
|
|
|
operator: 'is',
|
2016-11-18 07:25:07 +00:00
|
|
|
value: ticket.id,
|
2016-05-03 13:51:07 +00:00
|
|
|
}
|
2016-11-18 07:25:07 +00:00
|
|
|
next if article_selector && !article
|
2017-03-03 08:28:45 +00:00
|
|
|
|
|
|
|
# check if article selector is matching
|
2016-11-18 07:25:07 +00:00
|
|
|
if article_selector
|
|
|
|
condition['article.id'] = {
|
|
|
|
operator: 'is',
|
|
|
|
value: article.id,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-03-03 08:28:45 +00:00
|
|
|
# verify is condition is matching
|
2016-11-18 07:25:07 +00:00
|
|
|
ticket_count, tickets = Ticket.selectors(condition, 1)
|
2017-04-27 14:57:19 +00:00
|
|
|
next if ticket_count.blank?
|
2016-11-18 07:25:07 +00:00
|
|
|
next if ticket_count.zero?
|
|
|
|
next if tickets.first.id != ticket.id
|
2017-03-03 08:28:45 +00:00
|
|
|
user_id = ticket.updated_by_id
|
|
|
|
if article
|
|
|
|
user_id = article.updated_by_id
|
2016-05-03 00:36:44 +00:00
|
|
|
end
|
2017-03-03 08:28:45 +00:00
|
|
|
ticket.perform_changes(trigger.perform, 'trigger', @item, user_id)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-11-18 07:25:07 +00:00
|
|
|
end
|
2016-05-04 09:45:05 +00:00
|
|
|
UserInfo.current_user_id = original_user_id
|
2016-05-03 00:36:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|