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
|
|
|
|
2015-01-02 22:34:37 +00:00
|
|
|
data[:string].gsub!( / \#\{ \s* ( .+? ) \s* \} /xm ) { |placeholder|
|
2014-10-19 18:26:01 +00:00
|
|
|
|
|
|
|
# store possible callback to work with
|
|
|
|
# and check if it's valid for execution
|
2015-01-02 22:55:37 +00:00
|
|
|
original_string = $&
|
|
|
|
callback = $1
|
2012-10-02 05:46:08 +00:00
|
|
|
|
2015-01-02 22:34:37 +00:00
|
|
|
object_name = nil
|
|
|
|
object_method = nil
|
2014-10-19 18:26:01 +00:00
|
|
|
|
2015-01-02 22:34:37 +00:00
|
|
|
if callback =~ /\A ( [\w]+ )\.( [\w\.]+ ) \z/x
|
2012-04-21 19:23:48 +00:00
|
|
|
object_name = $1
|
|
|
|
object_method = $2
|
2015-01-02 22:34:37 +00:00
|
|
|
end
|
2014-10-19 18:26:01 +00:00
|
|
|
|
2015-01-02 22:34:37 +00:00
|
|
|
# do validaton, ignore some methodes
|
|
|
|
if callback =~ /(`|\.(|\s*)(save|destroy|delete|remove|drop|update\(|update_att|create\(|new|all|where|find))/i
|
|
|
|
placeholder = "#{original_string} (not allowed)"
|
2014-10-19 18:26:01 +00:00
|
|
|
|
2015-01-02 22:34:37 +00:00
|
|
|
# get value based on object_name and object_method
|
|
|
|
elsif object_name && object_method
|
2014-12-28 13:02:23 +00:00
|
|
|
|
2015-01-02 22:34:37 +00:00
|
|
|
# use config params
|
|
|
|
if object_name == 'config'
|
|
|
|
placeholder = Setting.get(object_method)
|
|
|
|
|
|
|
|
# if object_name dosn't exist
|
|
|
|
elsif !data[:objects][object_name.to_sym]
|
|
|
|
placeholder = "\#{#{object_name} / no such object}"
|
|
|
|
else
|
|
|
|
value = nil
|
|
|
|
object_refs = data[:objects][object_name.to_sym]
|
|
|
|
object_methods = object_method.split('.')
|
|
|
|
object_methods_s = ''
|
|
|
|
object_methods.each {|method|
|
|
|
|
if object_methods_s != ''
|
|
|
|
object_methods_s += '.'
|
|
|
|
end
|
|
|
|
object_methods_s += method
|
|
|
|
|
|
|
|
# if method exists
|
|
|
|
if !object_refs.respond_to?( method.to_sym )
|
|
|
|
value = "\#{#{object_name}.#{object_methods_s} / no such method}"
|
|
|
|
break
|
|
|
|
end
|
|
|
|
object_refs = object_refs.send( method.to_sym )
|
|
|
|
|
|
|
|
# add body quote
|
|
|
|
if object_name == 'article' && method == 'body'
|
|
|
|
if data[:objects][:article].content_type == 'text/html'
|
2015-01-04 21:28:58 +00:00
|
|
|
object_refs = object_refs.html2text.chomp
|
2015-01-02 22:34:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
if !value
|
|
|
|
placeholder = object_refs
|
|
|
|
else
|
|
|
|
placeholder = value
|
|
|
|
end
|
2014-10-19 18:26:01 +00:00
|
|
|
end
|
2013-01-03 10:47:39 +00:00
|
|
|
end
|
2015-01-02 22:34:37 +00:00
|
|
|
placeholder
|
2012-04-21 19:23:48 +00:00
|
|
|
}
|
2013-01-04 14:28:55 +00:00
|
|
|
|
|
|
|
# translate
|
2015-02-10 14:59:21 +00:00
|
|
|
data[:string].gsub!( /i18n\((|.+?)\)/ ) { |placeholder|
|
2014-10-19 18:26:01 +00:00
|
|
|
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(
|
2015-01-07 12:11:30 +00:00
|
|
|
:recipient => User.find(123),
|
|
|
|
:subject => 'sime subject',
|
|
|
|
:body => 'some body',
|
|
|
|
:content_type => '', # optional, e. g. 'text/html'
|
2014-12-28 13:02:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
=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})"
|
2015-01-01 00:26:17 +00:00
|
|
|
|
2015-01-07 12:11:30 +00:00
|
|
|
content_type = 'text/plain'
|
|
|
|
if data[:content_type]
|
|
|
|
content_type = data[:content_type]
|
|
|
|
end
|
|
|
|
|
2015-01-01 00:26:17 +00:00
|
|
|
Channel::EmailSend.send(
|
2012-04-21 19:23:48 +00:00
|
|
|
{
|
|
|
|
# :in_reply_to => self.in_reply_to,
|
2015-01-03 22:53:07 +00:00
|
|
|
:from => sender,
|
|
|
|
:to => data[:recipient][:email],
|
|
|
|
:subject => data[:subject],
|
|
|
|
:body => data[:body],
|
2015-01-07 12:11:30 +00:00
|
|
|
:content_type => content_type,
|
2012-04-21 19:23:48 +00:00
|
|
|
},
|
|
|
|
true
|
|
|
|
)
|
|
|
|
end
|
2014-12-28 13:02:23 +00:00
|
|
|
end
|