notification_factory.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. module NotificationFactory
  2. =begin
  3. result_string = NotificationFactory.build(
  4. :string => 'Hi #{recipient.firstname},',
  5. :objects => {
  6. :ticket => ticket,
  7. :recipient => User.find(2),
  8. },
  9. :locale => 'en',
  10. )
  11. =end
  12. def self.build(data)
  13. data[:string].gsub!( / \#\{ \s* ( .+? ) \s* \} /xm ) { |placeholder|
  14. # store possible callback to work with
  15. # and check if it's valid for execution
  16. original_string = $&
  17. callback = $1
  18. object_name = nil
  19. object_method = nil
  20. if callback =~ /\A ( [\w]+ )\.( [\w\.]+ ) \z/x
  21. object_name = $1
  22. object_method = $2
  23. end
  24. # do validaton, ignore some methodes
  25. if callback =~ /(`|\.(|\s*)(save|destroy|delete|remove|drop|update\(|update_att|create\(|new|all|where|find))/i
  26. placeholder = "#{original_string} (not allowed)"
  27. # get value based on object_name and object_method
  28. elsif object_name && object_method
  29. # use config params
  30. if object_name == 'config'
  31. placeholder = Setting.get(object_method)
  32. # if object_name dosn't exist
  33. elsif !data[:objects][object_name.to_sym]
  34. placeholder = "\#{#{object_name} / no such object}"
  35. else
  36. value = nil
  37. object_refs = data[:objects][object_name.to_sym]
  38. object_methods = object_method.split('.')
  39. object_methods_s = ''
  40. object_methods.each {|method|
  41. if object_methods_s != ''
  42. object_methods_s += '.'
  43. end
  44. object_methods_s += method
  45. # if method exists
  46. if !object_refs.respond_to?( method.to_sym )
  47. value = "\#{#{object_name}.#{object_methods_s} / no such method}"
  48. break
  49. end
  50. object_refs = object_refs.send( method.to_sym )
  51. # add body quote
  52. if object_name == 'article' && method == 'body'
  53. if data[:objects][:article].content_type == 'text/html'
  54. object_refs = object_refs.html2text.chomp
  55. end
  56. end
  57. }
  58. if !value
  59. placeholder = object_refs
  60. else
  61. placeholder = value
  62. end
  63. end
  64. end
  65. placeholder
  66. }
  67. # translate
  68. data[:string].gsub!( /i18n\((.+?)\)/ ) { |placeholder|
  69. string = $1
  70. locale = data[:locale] || 'en'
  71. placeholder = Translation.translate( locale, string )
  72. }
  73. data[:string]
  74. end
  75. =begin
  76. success = NotificationFactory.send(
  77. :to => 'somebody@example.com',
  78. :subject => 'sime subject',
  79. :body => 'some body'
  80. )
  81. =end
  82. def self.send(data)
  83. sender = Setting.get('notification_sender')
  84. Rails.logger.info "NOTICE: SEND NOTIFICATION TO: #{data[:recipient][:email]} (from #{sender})"
  85. Channel::EmailSend.send(
  86. {
  87. # :in_reply_to => self.in_reply_to,
  88. :from => sender,
  89. :to => data[:recipient][:email],
  90. :subject => data[:subject],
  91. :body => data[:body],
  92. :content_type => 'text/html',
  93. },
  94. true
  95. )
  96. end
  97. end