renderer.rb 5.7 KB

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