renderer.rb 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. class NotificationFactory::Renderer
  2. =begin
  3. examples how to use
  4. message_subject = NotificationFactory::Renderer.new(
  5. objects: {
  6. ticket: Ticket.first,
  7. },
  8. locale: 'de-de',
  9. timezone: 'America/Port-au-Prince',
  10. template: 'some template <b>#{ticket.title}</b> {config.fqdn}',
  11. escape: false
  12. ).render
  13. message_body = NotificationFactory::Renderer.new(
  14. objects: {
  15. ticket: Ticket.first,
  16. },
  17. locale: 'de-de',
  18. timezone: 'America/Port-au-Prince',
  19. template: 'some template <b>#{ticket.title}</b> #{config.fqdn}',
  20. ).render
  21. =end
  22. def initialize(objects:, template:, locale: nil, timezone: nil, escape: true)
  23. @objects = objects
  24. @locale = locale || Locale.default
  25. @timezone = timezone || Setting.get('timezone_default')
  26. @template = NotificationFactory::Template.new(template, escape)
  27. @escape = escape
  28. end
  29. def render
  30. ERB.new(@template.to_s).result(binding)
  31. end
  32. # d - data of object
  33. # d('user.firstname', htmlEscape)
  34. def d(key, escape = nil)
  35. # do validation, ignore some methods
  36. return "\#{#{key} / not allowed}" if !data_key_valid?(key)
  37. # aliases
  38. map = {
  39. 'article.body' => 'article.body_as_text_with_quote.text2html',
  40. 'ticket.tags' => 'ticket.tag_list',
  41. }
  42. if map[key]
  43. key = map[key]
  44. end
  45. # escape in html mode
  46. if escape
  47. no_escape = {
  48. 'article.body_as_html' => true,
  49. 'article.body_as_text_with_quote.text2html' => true,
  50. }
  51. if no_escape[key]
  52. escape = false
  53. end
  54. end
  55. value = nil
  56. object_methods = key.split('.')
  57. object_name = object_methods.shift
  58. # if no object is given, just return
  59. return '#{no such object}' if object_name.blank? # rubocop:disable Lint/InterpolationCheck
  60. object_refs = @objects[object_name] || @objects[object_name.to_sym]
  61. # if object is not in available objects, just return
  62. return "\#{#{object_name} / no such object}" if !object_refs
  63. # if content of method is a complex datatype, just return
  64. if object_methods.blank? && object_refs.class != String && object_refs.class != Float && object_refs.class != Integer
  65. return "\#{#{key} / no such method}"
  66. end
  67. previous_object_refs = ''
  68. object_methods_s = ''
  69. object_methods.each do |method_raw|
  70. method = method_raw.strip
  71. if method == 'value'
  72. temp = object_refs
  73. object_refs = display_value(previous_object_refs, method, object_methods_s, object_refs)
  74. previous_object_refs = temp
  75. end
  76. if object_methods_s != ''
  77. object_methods_s += '.'
  78. end
  79. object_methods_s += method
  80. next if method == 'value'
  81. if object_methods_s == ''
  82. value = "\#{#{object_name}.#{object_methods_s} / no such method}"
  83. break
  84. end
  85. arguments = nil
  86. if %r{\A(?<method_id>[^(]+)\((?<parameter>[^)]+)\)\z} =~ method
  87. if parameter != parameter.to_i.to_s
  88. value = "\#{#{object_name}.#{object_methods_s} / invalid parameter: #{parameter}}"
  89. break
  90. end
  91. begin
  92. arguments = Array(parameter.to_i)
  93. method = method_id
  94. rescue
  95. value = "\#{#{object_name}.#{object_methods_s} / #{e.message}}"
  96. break
  97. end
  98. end
  99. # if method exists
  100. if !object_refs.respond_to?(method.to_sym)
  101. value = "\#{#{object_name}.#{object_methods_s} / no such method}"
  102. break
  103. end
  104. begin
  105. previous_object_refs = object_refs
  106. object_refs = object_refs.send(method.to_sym, *arguments)
  107. # body_as_html should trigger the cloning of all inline attachments from the parent article (issue #2399)
  108. if method.to_sym == :body_as_html && previous_object_refs.respond_to?(:should_clone_inline_attachments)
  109. previous_object_refs.should_clone_inline_attachments = true
  110. end
  111. rescue => e
  112. value = "\#{#{object_name}.#{object_methods_s} / #{e.message}}"
  113. break
  114. end
  115. end
  116. placeholder = value || object_refs
  117. escaping(convert_to_timezone(placeholder), escape)
  118. end
  119. # c - config
  120. # c('fqdn', htmlEscape)
  121. def c(key, escape = nil)
  122. config = Setting.get(key)
  123. escaping(config, escape)
  124. end
  125. # t - translation
  126. # t('yes', htmlEscape)
  127. def t(key, escape = nil)
  128. translation = Translation.translate(@locale, key)
  129. escaping(translation, escape)
  130. end
  131. # h - htmlEscape
  132. # h(htmlEscape)
  133. def h(value)
  134. return value if !value
  135. CGI.escapeHTML(convert_to_timezone(value).to_s)
  136. end
  137. private
  138. def convert_to_timezone(value)
  139. return Translation.timestamp(@locale, @timezone, value) if value.instance_of?(ActiveSupport::TimeWithZone)
  140. return Translation.date(@locale, value) if value.instance_of?(Date)
  141. value
  142. end
  143. def escaping(key, escape)
  144. return escaping(key.join(', '), escape) if key.respond_to?(:join)
  145. return key if escape == false
  146. return key if escape.nil? && !@escape
  147. h key
  148. end
  149. def data_key_valid?(key)
  150. return false if key =~ %r{`|\.(|\s*)(save|destroy|delete|remove|drop|update|create|new|all|where|find|raise|dump|rollback|freeze)}i && key !~ %r{(update|create)d_(at|by)}i
  151. true
  152. end
  153. def display_value(object, method_name, previous_method_names, key)
  154. return key if method_name != 'value' ||
  155. !key.instance_of?(String)
  156. attributes = ObjectManager::Attribute
  157. .where(object_lookup_id: ObjectLookup.by_name(object.class.to_s))
  158. .where(name: previous_method_names.split('.').last)
  159. return key if attributes.count.zero? || attributes.first.data_type != 'select'
  160. attributes.first.data_option['options'][key] || key
  161. end
  162. end