123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- class NotificationFactory::Renderer
- =begin
- examples how to use
- message_subject = NotificationFactory::Renderer.new(
- {
- ticket: Ticket.first,
- },
- 'de-de',
- 'some template <b><%= d "ticket.title", false %></b> <%= c "fqdn", false %>',
- false
- ).render
- message_body = NotificationFactory::Renderer.new(
- {
- ticket: Ticket.first,
- },
- 'de-de',
- 'some template <b><%= d "ticket.title", true %></b> <%= c "fqdn", true %>',
- ).render
- =end
- def initialize(objects, locale, template, escape = true)
- @objects = objects
- @locale = locale || 'en-us'
- @template = NotificationFactory::Template.new(template)
- @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
- return "\#{#{key} / not allowed}" if !data_key_valid?(key)
- value = nil
- object_methods = key.split('.')
- object_name = object_methods.shift
- # if no object is given, just return
- return "\#{no such object}" if object_name.empty?
- object_refs = @objects[object_name] || @objects[object_name.to_sym]
- # 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
- if object_methods.empty? && object_refs.class != String && object_refs.class != Float && object_refs.class != Fixnum
- return "\#{#{key} / no such method}"
- end
- object_methods_s = ''
- object_methods.each { |method_raw|
- method = method_raw.strip
- if object_methods_s != ''
- object_methods_s += '.'
- end
- object_methods_s += method
- if object_methods_s == ''
- value = "\#{#{object_name}.#{object_methods_s} / no such method}"
- break
- end
- # 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)
- }
- 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
- # a_html - article body in html
- # a_html(article)
- def a_html(article)
- content_type = d "#{article}.content_type", false
- if content_type =~ /html/
- return d "#{article}.body", false
- end
- d("#{article}.body", false).text2html
- end
- # a_text - article body in text
- # a_text(article)
- def a_text(article)
- content_type = d "#{article}.content_type", false
- body = d "#{article}.body", false
- if content_type =~ /html/
- body = body.html2text
- end
- (body.strip + "\n").gsub(/^(.*?)$/, '> \\1')
- end
- # h - htmlEscape
- # h('fqdn', htmlEscape)
- def h(key)
- return key if !key
- CGI.escapeHTML(key.to_s)
- end
- private
- def escaping(key, escape)
- return key if escape == false
- return key if escape.nil? && !@escape
- h key
- end
- def data_key_valid?(key)
- return false if key =~ /`|\.(|\s*)(save|destroy|delete|remove|drop|update|create|new|all|where|find)/i
- true
- end
- end
|