2016-11-11 15:57:25 +00:00
|
|
|
class NotificationFactory::Renderer
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
examples how to use
|
|
|
|
|
|
|
|
message_subject = NotificationFactory::Renderer.new(
|
|
|
|
{
|
|
|
|
ticket: Ticket.first,
|
|
|
|
},
|
|
|
|
'de-de',
|
2016-11-13 18:33:12 +00:00
|
|
|
'some template <b>#{ticket.title}</b> {config.fqdn}',
|
2016-11-11 15:57:25 +00:00
|
|
|
false
|
|
|
|
).render
|
|
|
|
|
|
|
|
message_body = NotificationFactory::Renderer.new(
|
|
|
|
{
|
|
|
|
ticket: Ticket.first,
|
|
|
|
},
|
|
|
|
'de-de',
|
2016-11-13 18:33:12 +00:00
|
|
|
'some template <b>#{ticket.title}</b> #{config.fqdn}',
|
2016-11-11 15:57:25 +00:00
|
|
|
).render
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def initialize(objects, locale, template, escape = true)
|
|
|
|
@objects = objects
|
2017-11-15 14:06:53 +00:00
|
|
|
@locale = locale || Setting.get('locale_default') || 'en-us'
|
2016-11-13 18:33:12 +00:00
|
|
|
@template = NotificationFactory::Template.new(template, escape)
|
2016-11-11 15:57:25 +00:00
|
|
|
@escape = escape
|
|
|
|
end
|
|
|
|
|
|
|
|
def render
|
|
|
|
ERB.new(@template.to_s).result(binding)
|
|
|
|
end
|
|
|
|
|
|
|
|
# d - data of object
|
|
|
|
# d('user.firstname', htmlEscape)
|
|
|
|
def d(key, escape = nil)
|
|
|
|
|
|
|
|
# do validaton, ignore some methodes
|
2016-11-12 10:19:15 +00:00
|
|
|
return "\#{#{key} / not allowed}" if !data_key_valid?(key)
|
|
|
|
|
2016-11-13 18:33:12 +00:00
|
|
|
# aliases
|
|
|
|
map = {
|
|
|
|
'article.body' => 'article.body_as_text_with_quote.text2html',
|
|
|
|
}
|
|
|
|
if map[key]
|
|
|
|
key = map[key]
|
|
|
|
end
|
|
|
|
|
|
|
|
# escape in html mode
|
|
|
|
if escape
|
|
|
|
no_escape = {
|
|
|
|
'article.body_as_html' => true,
|
|
|
|
'article.body_as_text_with_quote.text2html' => true,
|
|
|
|
}
|
|
|
|
if no_escape[key]
|
|
|
|
escape = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-12 10:19:15 +00:00
|
|
|
value = nil
|
|
|
|
object_methods = key.split('.')
|
|
|
|
object_name = object_methods.shift
|
|
|
|
|
|
|
|
# if no object is given, just return
|
2017-11-23 08:09:44 +00:00
|
|
|
return '#{no such object}' if object_name.blank? # rubocop:disable Lint/InterpolationCheck
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-12 10:19:15 +00:00
|
|
|
object_refs = @objects[object_name] || @objects[object_name.to_sym]
|
2016-11-11 15:57:25 +00:00
|
|
|
|
2016-11-12 10:19:15 +00:00
|
|
|
# if object is not in avalable objects, just return
|
|
|
|
return "\#{#{object_name} / no such object}" if !object_refs
|
|
|
|
|
|
|
|
# if content of method is a complex datatype, just return
|
2017-11-23 08:09:44 +00:00
|
|
|
if object_methods.blank? && object_refs.class != String && object_refs.class != Float && object_refs.class != Integer
|
2016-11-12 10:19:15 +00:00
|
|
|
return "\#{#{key} / no such method}"
|
|
|
|
end
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-11 15:57:25 +00:00
|
|
|
object_methods_s = ''
|
2017-10-01 12:25:52 +00:00
|
|
|
object_methods.each do |method_raw|
|
2016-11-11 15:57:25 +00:00
|
|
|
|
|
|
|
method = method_raw.strip
|
|
|
|
|
|
|
|
if object_methods_s != ''
|
|
|
|
object_methods_s += '.'
|
|
|
|
end
|
|
|
|
object_methods_s += method
|
|
|
|
|
2016-11-12 10:19:15 +00:00
|
|
|
if object_methods_s == ''
|
|
|
|
value = "\#{#{object_name}.#{object_methods_s} / no such method}"
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
2016-11-11 15:57:25 +00:00
|
|
|
# if method exists
|
2016-11-12 10:19:15 +00:00
|
|
|
if !object_refs.respond_to?(method.to_sym)
|
2016-11-11 15:57:25 +00:00
|
|
|
value = "\#{#{object_name}.#{object_methods_s} / no such method}"
|
|
|
|
break
|
|
|
|
end
|
2016-11-13 18:33:12 +00:00
|
|
|
begin
|
|
|
|
object_refs = object_refs.send(method.to_sym)
|
|
|
|
rescue => e
|
|
|
|
object_refs = "\#{#{object_name}.#{object_methods_s} / e.message}"
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-11-11 15:57:25 +00:00
|
|
|
placeholder = if !value
|
|
|
|
object_refs
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
escaping(placeholder, escape)
|
|
|
|
end
|
|
|
|
|
|
|
|
# c - config
|
|
|
|
# c('fqdn', htmlEscape)
|
|
|
|
def c(key, escape = nil)
|
|
|
|
config = Setting.get(key)
|
|
|
|
escaping(config, escape)
|
|
|
|
end
|
|
|
|
|
|
|
|
# t - translation
|
|
|
|
# t('yes', htmlEscape)
|
|
|
|
def t(key, escape = nil)
|
|
|
|
translation = Translation.translate(@locale, key)
|
|
|
|
escaping(translation, escape)
|
|
|
|
end
|
|
|
|
|
|
|
|
# h - htmlEscape
|
|
|
|
# h('fqdn', htmlEscape)
|
|
|
|
def h(key)
|
|
|
|
return key if !key
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-11 15:57:25 +00:00
|
|
|
CGI.escapeHTML(key.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def escaping(key, escape)
|
|
|
|
return key if escape == false
|
|
|
|
return key if escape.nil? && !@escape
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-11 15:57:25 +00:00
|
|
|
h key
|
|
|
|
end
|
2016-11-12 10:19:15 +00:00
|
|
|
|
|
|
|
def data_key_valid?(key)
|
2017-03-31 06:54:05 +00:00
|
|
|
return false if key =~ /`|\.(|\s*)(save|destroy|delete|remove|drop|update|create|new|all|where|find|raise|dump|rollback|freeze)/i && key !~ /(update|create)d_(at|by)/i
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-12 10:19:15 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2016-11-11 15:57:25 +00:00
|
|
|
end
|