notification.rb 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. # Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
  2. require 'event_buffer'
  3. require 'notification_factory'
  4. class Observer::Ticket::Notification < ActiveRecord::Observer
  5. observe :ticket, 'ticket::_article'
  6. def self.transaction
  7. # return if we run import mode
  8. return if Setting.get('import_mode')
  9. # get buffer
  10. list = EventBuffer.list
  11. # reset buffer
  12. EventBuffer.reset
  13. list.each { |event|
  14. # get current state of objects
  15. if event[:name] == 'Ticket::Article'
  16. article = Ticket::Article.lookup( :id => event[:id] )
  17. # next if article is already deleted
  18. next if !article
  19. ticket = article.ticket
  20. elsif event[:name] == 'Ticket'
  21. ticket = Ticket.lookup( :id => event[:id] )
  22. # next if ticket is already deleted
  23. next if !ticket
  24. article = ticket.articles[-1]
  25. else
  26. raise "unknown object for notification #{event[:name]}"
  27. end
  28. # send new ticket notification to agents
  29. if event[:name] == 'Ticket' && event[:type] == 'create'
  30. puts 'send new ticket notify to agent'
  31. send_notify(
  32. {
  33. :event => event,
  34. :recipient => 'to_work_on', # group|owner|to_work_on|customer
  35. :subject => 'New Ticket (#{ticket.title})',
  36. :body => 'Hi #{recipient.firstname},
  37. a new Ticket (#{ticket.title}) via i18n(#{article.ticket_article_type.name}).
  38. Group: #{ticket.group.name}
  39. Owner: #{ticket.owner.firstname} #{ticket.owner.lastname}
  40. State: i18n(#{ticket.ticket_state.name})
  41. From: #{article.from}
  42. <snip>
  43. #{article.body}
  44. </snip>
  45. #{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}/#{article.id}
  46. '
  47. },
  48. ticket,
  49. article
  50. )
  51. end
  52. # send new ticket notification to customers
  53. if event[:name] == 'Ticket' && event[:type] == 'create'
  54. # only for incoming emails
  55. next if article.ticket_article_type.name != 'email'
  56. puts 'send new ticket notify to customer'
  57. send_notify(
  58. {
  59. :event => event,
  60. :recipient => 'customer', # group|owner|to_work_on|customer
  61. :subject => 'New Ticket has been created! (#{ticket.title})',
  62. :body => 'Thanks for your email. A new ticket has been created.
  63. You wrote:
  64. <snip>
  65. #{article.body}
  66. </snip>
  67. Your email will be answered by a human ASAP
  68. Have fun with Zammad! :-)
  69. Your Zammad Team
  70. '
  71. },
  72. ticket,
  73. article
  74. )
  75. end
  76. # send follow up notification
  77. if event[:name] == 'Ticket::Article' && event[:type] == 'create'
  78. # only send article notifications after init article is created (handled by ticket create event)
  79. next if ticket.articles.count.to_i <= 1
  80. puts 'send new ticket::article notify'
  81. if article.ticket_article_sender.name == 'Customer'
  82. send_notify(
  83. {
  84. :event => event,
  85. :recipient => 'to_work_on', # group|owner|to_work_on|customer
  86. :subject => 'Follow Up (#{ticket.title})',
  87. :body => 'Hi #{recipient.firstname},
  88. a follow Up (#{ticket.title}) via i18n(#{article.ticket_article_type.name}).
  89. Group: #{ticket.group.name}
  90. Owner: #{ticket.owner.firstname} #{ticket.owner.lastname}
  91. State: i18n(#{ticket.ticket_state.name})
  92. From: #{article.from}
  93. <snip>
  94. #{article.body}
  95. </snip>
  96. #{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}/#{article.id}
  97. '
  98. },
  99. ticket,
  100. article
  101. )
  102. end
  103. # send new note notification to owner
  104. # if agent == created.id
  105. if article.ticket_article_sender.name == 'Agent' && article.created_by_id != article.ticket.owner_id
  106. send_notify(
  107. {
  108. :event => event,
  109. :recipient => 'owner', # group|owner|to_work_on
  110. :subject => 'Updated (#{ticket.title})',
  111. :body => 'Hi #{recipient.firstname},
  112. updated (#{ticket.title}) via i18n(#{article.ticket_article_type.name}).
  113. Group: #{ticket.group.name}
  114. Owner: #{ticket.owner.firstname} #{ticket.owner.lastname}
  115. State: i18n(#{ticket.ticket_state.name})
  116. From: #{article.from}
  117. <snip>
  118. #{article.body}
  119. </snip>
  120. #{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}/#{article.id}
  121. '
  122. },
  123. ticket,
  124. article
  125. )
  126. end
  127. end
  128. }
  129. end
  130. def self.send_notify(data, ticket, article)
  131. # find recipients
  132. recipients = []
  133. # group of agents to work on
  134. if data[:recipient] == 'group'
  135. recipients = ticket.agent_of_group()
  136. # owner
  137. elsif data[:recipient] == 'owner'
  138. if ticket.owner_id != 1
  139. recipients.push ticket.owner
  140. end
  141. # customer
  142. elsif data[:recipient] == 'customer'
  143. if ticket.customer_id != 1
  144. # temporarily disabled
  145. # recipients.push ticket.customer
  146. end
  147. # owner or group of agents to work on
  148. elsif data[:recipient] == 'to_work_on'
  149. if ticket.owner_id != 1
  150. recipients.push ticket.owner
  151. else
  152. recipients = ticket.agent_of_group()
  153. end
  154. end
  155. # send notifications
  156. recipient_list = ''
  157. notification_subject = ''
  158. recipients.each do |user|
  159. next if !user.email || user.email == ''
  160. # add recipient_list
  161. if recipient_list != ''
  162. recipient_list += ','
  163. end
  164. recipient_list += user.email.to_s
  165. # prepare subject & body
  166. notification = {}
  167. [:subject, :body].each { |key|
  168. notification[key.to_sym] = NotificationFactory.build(
  169. :locale => user.locale,
  170. :string => data[key.to_sym],
  171. :objects => {
  172. :ticket => ticket,
  173. :article => article,
  174. :recipient => user,
  175. }
  176. )
  177. }
  178. notification_subject = notification[:subject]
  179. # rebuild subject
  180. notification[:subject] = ticket.subject_build( notification[:subject] )
  181. # send notification
  182. NotificationFactory.send(
  183. :recipient => user,
  184. :subject => notification[:subject],
  185. :body => notification[:body]
  186. )
  187. end
  188. # add history record
  189. if recipient_list != ''
  190. History.add(
  191. :o_id => ticket.id,
  192. :history_type => 'notification',
  193. :history_object => 'Ticket',
  194. :value_from => notification_subject,
  195. :value_to => recipient_list,
  196. :created_by_id => article.created_by_id || 1
  197. )
  198. end
  199. end
  200. def after_create(record)
  201. # return if we run import mode
  202. return if Setting.get('import_mode')
  203. # puts 'CREATED!!!!'
  204. # puts record.inspect
  205. e = {
  206. :name => record.class.name,
  207. :type => 'create',
  208. :data => record,
  209. :id => record.id,
  210. }
  211. EventBuffer.add(e)
  212. end
  213. def before_update(record)
  214. # return if we run import mode
  215. return if Setting.get('import_mode')
  216. #puts 'before_update'
  217. current = record.class.find(record.id)
  218. # do not send anything if nothing has changed
  219. return if current.attributes == record.attributes
  220. # puts 'UPDATE!!!!!!!!'
  221. # puts 'current'
  222. # puts current.inspect
  223. # puts 'record'
  224. # puts record.inspect
  225. e = {
  226. :name => record.class.name,
  227. :type => 'update',
  228. :data => record,
  229. :id => record.id,
  230. }
  231. EventBuffer.add(e)
  232. end
  233. def after_update(record)
  234. # return if we run import mode
  235. return if Setting.get('import_mode')
  236. # puts 'after_update'
  237. # puts record.inspect
  238. # puts '-----'
  239. # puts @a.inspect
  240. # AuditTrail.new(record, "UPDATED")
  241. end
  242. end