fillup_from_general.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Observer::Ticket::Article::FillupFromGeneral < ActiveRecord::Observer
  3. observe 'ticket::_article'
  4. def before_create(record)
  5. # return if we run import mode
  6. return true if Setting.get('import_mode')
  7. # only do fill of from if article got created via application_server (e. g. not
  8. # if article and sender type is set via *.postmaster)
  9. return if ApplicationHandleInfo.postmaster?
  10. # set from on all article types excluding email|twitter status|twitter direct-message|facebook feed post|facebook feed comment
  11. return true if record.type_id.blank?
  12. type = Ticket::Article::Type.lookup(id: record.type_id)
  13. return true if type['name'] == 'email'
  14. # from will be set by channel backend
  15. return true if type['name'] == 'twitter status'
  16. return true if type['name'] == 'twitter direct-message'
  17. return true if type['name'] == 'facebook feed post'
  18. return true if type['name'] == 'facebook feed comment'
  19. user_id = record.created_by_id
  20. if record.origin_by_id.present?
  21. # in case the customer is using origin_by_id, force it to current session user
  22. # and set sender to Customer
  23. if !record.created_by.permissions?('ticket.agent')
  24. record.origin_by_id = record.created_by_id
  25. record.sender_id = Ticket::Article::Sender.lookup(name: 'Customer').id
  26. end
  27. # in case origin_by_id is customer, force it to set sender to Customer
  28. if record.origin_by != record.created_by_id && !record.origin_by.permissions?('ticket.agent')
  29. record.sender_id = Ticket::Article::Sender.lookup(name: 'Customer').id
  30. user_id = record.origin_by_id
  31. end
  32. end
  33. return true if user_id.blank?
  34. user = User.find(user_id)
  35. if type.name == 'web' || type.name == 'phone'
  36. record.from = "#{user.firstname} #{user.lastname} <#{user.email}>"
  37. return
  38. end
  39. record.from = "#{user.firstname} #{user.lastname}"
  40. end
  41. end