2012-04-21 19:23:48 +00:00
|
|
|
module NotificationFactory
|
2014-12-28 13:02:23 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
result_string = NotificationFactory.build(
|
|
|
|
:string => 'Hi #{recipient.firstname},',
|
|
|
|
:objects => {
|
|
|
|
:ticket => ticket,
|
|
|
|
:recipient => User.find(2),
|
|
|
|
},
|
|
|
|
:locale => 'en',
|
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-04-21 19:23:48 +00:00
|
|
|
def self.build(data)
|
2013-01-03 10:47:39 +00:00
|
|
|
|
2014-10-19 18:26:01 +00:00
|
|
|
data[:string].gsub!( / \#\{ \s* ( .+? ) \s* \} /x ) { |placeholder|
|
|
|
|
|
|
|
|
# store possible callback to work with
|
|
|
|
# and check if it's valid for execution
|
|
|
|
original_string = $&
|
|
|
|
callback = $1
|
|
|
|
callback_valid = nil
|
2012-10-02 05:46:08 +00:00
|
|
|
|
2012-04-21 19:23:48 +00:00
|
|
|
# use config params
|
2014-10-19 18:26:01 +00:00
|
|
|
callback.gsub!( /\A config\.( [\w\.]+ ) \z/x ) { |item|
|
|
|
|
callback_valid = true
|
|
|
|
name = $1
|
|
|
|
item = "Setting.get('#{name}')"
|
2012-04-21 19:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# use object params
|
2014-10-19 18:26:01 +00:00
|
|
|
callback.gsub!( /\A ( [\w]+ )( \.[\w\.]+ ) \z/x ) { |item|
|
|
|
|
|
2012-04-21 19:23:48 +00:00
|
|
|
object_name = $1
|
|
|
|
object_method = $2
|
2014-10-19 18:26:01 +00:00
|
|
|
|
2014-12-28 13:02:23 +00:00
|
|
|
if data[:objects][object_name.to_sym]
|
|
|
|
callback_valid = true
|
|
|
|
item = "data[:objects]['#{object_name}'.to_sym]#{object_method}"
|
|
|
|
else
|
|
|
|
item = item
|
|
|
|
end
|
|
|
|
}
|
2014-10-19 18:26:01 +00:00
|
|
|
|
2014-12-28 13:02:23 +00:00
|
|
|
# use quoted text
|
|
|
|
callback.gsub!( /\A ( data\[:objects\]\['article'\.to_sym\] ) \.body \z/x ) { |item|
|
2014-10-19 18:26:01 +00:00
|
|
|
callback_valid = true
|
2014-12-28 13:02:23 +00:00
|
|
|
if data[:objects][:article].content_type == 'text/html'
|
|
|
|
item = item + '.html2text.message_quote.chomp'
|
|
|
|
else
|
|
|
|
item = item + '.word_wrap( :line_width => 82 ).message_quote.chomp'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# do validaton, ignore some methodes
|
|
|
|
callback.gsub!( /\.(save|destroy|delete|remove|drop|update|create\(|new|all|where|find)/ix ) { |item|
|
|
|
|
callback_valid = false
|
2012-04-21 19:23:48 +00:00
|
|
|
}
|
|
|
|
|
2014-10-19 18:26:01 +00:00
|
|
|
if callback_valid
|
|
|
|
# replace value
|
|
|
|
begin
|
|
|
|
placeholder = eval callback
|
|
|
|
rescue Exception => e
|
|
|
|
Rails.logger.error "Evaluation error caused by callback '#{callback}'"
|
|
|
|
Rails.logger.error e.inspect
|
|
|
|
end
|
|
|
|
else
|
|
|
|
placeholder = original_string
|
2013-01-03 10:47:39 +00:00
|
|
|
end
|
2012-04-21 19:23:48 +00:00
|
|
|
}
|
2013-01-04 14:28:55 +00:00
|
|
|
|
|
|
|
# translate
|
2014-10-19 18:26:01 +00:00
|
|
|
data[:string].gsub!( /i18n\((.+?)\)/ ) { |placeholder|
|
|
|
|
string = $1
|
|
|
|
locale = data[:locale] || 'en'
|
|
|
|
placeholder = Translation.translate( locale, string )
|
2013-01-04 14:28:55 +00:00
|
|
|
}
|
|
|
|
|
2014-12-28 13:02:23 +00:00
|
|
|
data[:string]
|
2012-04-21 19:23:48 +00:00
|
|
|
end
|
2012-10-02 05:46:08 +00:00
|
|
|
|
2014-12-28 13:02:23 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
success = NotificationFactory.send(
|
|
|
|
:to => 'somebody@example.com',
|
|
|
|
:subject => 'sime subject',
|
|
|
|
:body => 'some body'
|
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-04-21 19:23:48 +00:00
|
|
|
def self.send(data)
|
|
|
|
sender = Setting.get('notification_sender')
|
2014-10-26 12:17:00 +00:00
|
|
|
Rails.logger.info "NOTICE: SEND NOTIFICATION TO: #{data[:recipient][:email]} (from #{sender})"
|
2014-10-22 21:00:11 +00:00
|
|
|
Channel::EmailSend.new.send(
|
2012-04-21 19:23:48 +00:00
|
|
|
{
|
|
|
|
# :in_reply_to => self.in_reply_to,
|
|
|
|
:from => sender,
|
2012-05-03 18:47:16 +00:00
|
|
|
:to => data[:recipient][:email],
|
2012-04-21 19:23:48 +00:00
|
|
|
:subject => data[:subject],
|
2014-06-08 22:01:20 +00:00
|
|
|
:body => data[:body],
|
2012-04-21 19:23:48 +00:00
|
|
|
},
|
|
|
|
true
|
|
|
|
)
|
|
|
|
end
|
2014-12-28 13:02:23 +00:00
|
|
|
end
|