attributes.rb 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'digest/md5'
  3. class CoreWorkflow::Attributes
  4. attr_accessor :user, :payload, :assets
  5. def initialize(result_object:)
  6. @result_object = result_object
  7. @user = result_object.user
  8. @payload = result_object.payload
  9. @assets = result_object.assets
  10. end
  11. def payload_class
  12. @payload['class_name'].constantize
  13. end
  14. def selected_only
  15. # params loading and preparing is very expensive so cache it
  16. checksum = Digest::MD5.hexdigest(Marshal.dump(@payload['params']))
  17. return @selected_only[checksum] if @selected_only.present? && @selected_only[checksum]
  18. @selected_only = {}
  19. @selected_only[checksum] = begin
  20. clean_params = payload_class.association_name_to_id_convert(@payload['params'])
  21. clean_params = payload_class.param_cleanup(clean_params, true, false, false)
  22. payload_class.new(clean_params)
  23. end
  24. end
  25. def selectable_field?(key)
  26. return if key == 'id'
  27. return if !@payload['params'].key?(key)
  28. # some objects have no attributes like "CoreWorkflow"-object as well.
  29. # attributes only exists in the frontend so we skip this check
  30. return true if object_elements.blank?
  31. object_elements_hash.key?(key)
  32. end
  33. def overwrite_selected(result)
  34. selected_attributes = selected_only.attributes
  35. selected_attributes.each_key do |key|
  36. next if !selectable_field?(key)
  37. # special behaviour for owner id
  38. if key == 'owner_id' && selected_attributes[key].nil?
  39. selected_attributes[key] = 1
  40. end
  41. result[key.to_sym] = selected_attributes[key]
  42. end
  43. result
  44. end
  45. def exists?
  46. return if @payload['params']['id'].blank?
  47. @exists ||= payload_class.exists?(id: @payload['params']['id'])
  48. end
  49. def overwritten
  50. # params loading and preparing is very expensive so cache it
  51. checksum = Digest::MD5.hexdigest(Marshal.dump(@payload['params']))
  52. return @overwritten[checksum] if @overwritten.present? && @overwritten[checksum]
  53. @overwritten = {}
  54. @overwritten[checksum] = begin
  55. result = saved_only(dump: true)
  56. overwrite_selected(result)
  57. end
  58. end
  59. def selected
  60. if exists?
  61. overwritten
  62. else
  63. selected_only
  64. end
  65. end
  66. def saved_only(dump: false)
  67. return if !exists?
  68. # dont use lookup here because the cache will not
  69. # know about new attributes and make crashes
  70. @saved_only ||= payload_class.find_by(id: @payload['params']['id'])
  71. return @saved_only if !dump
  72. # we use marshal here because clone still uses references and dup can't
  73. # detect changes for the rails object
  74. Marshal.load(Marshal.dump(@saved_only))
  75. end
  76. def saved
  77. @saved ||= saved_only || payload_class.new
  78. end
  79. def object_elements
  80. @object_elements ||= ObjectManager::Object.new(@payload['class_name']).attributes(@user, saved_only, data_only: false).each_with_object([]) do |element, result|
  81. result << element.data.merge(screens: element.screens)
  82. end
  83. end
  84. def object_elements_hash
  85. @object_elements_hash ||= object_elements.index_by { |x| x[:name] }
  86. end
  87. def screen_value(attribute, type)
  88. screen_value = attribute[:screens].dig(@payload['screen'], type)
  89. return screen_value if !screen_value.nil?
  90. attribute[type.to_sym]
  91. end
  92. def request_id_default
  93. payload['request_id']
  94. end
  95. # dont cache this else the result object will work with references and cache bugs occur
  96. def visibility_default
  97. object_elements.each_with_object({}) do |attribute, result|
  98. result[ attribute[:name] ] = screen_value(attribute, 'shown') == false ? 'remove' : 'show'
  99. end
  100. end
  101. def attribute_mandatory?(attribute)
  102. return screen_value(attribute, 'required').present? if !screen_value(attribute, 'required').nil?
  103. return screen_value(attribute, 'null').blank? if !screen_value(attribute, 'null').nil?
  104. false
  105. end
  106. # dont cache this else the result object will work with references and cache bugs occur
  107. def mandatory_default
  108. object_elements.each_with_object({}) do |attribute, result|
  109. result[ attribute[:name] ] = attribute_mandatory?(attribute)
  110. end
  111. end
  112. # dont cache this else the result object will work with references and cache bugs occur
  113. def auto_select_default
  114. object_elements.each_with_object({}) do |attribute, result|
  115. next if !attribute[:only_shown_if_selectable]
  116. result[ attribute[:name] ] = true
  117. end
  118. end
  119. # dont cache this else the result object will work with references and cache bugs occur
  120. def readonly_default
  121. object_elements.each_with_object({}) do |attribute, result|
  122. result[ attribute[:name] ] = false
  123. end
  124. end
  125. def select_default
  126. @result_object.result[:select] || {}
  127. end
  128. def fill_in_default
  129. @result_object.result[:fill_in] || {}
  130. end
  131. def eval_default
  132. []
  133. end
  134. def matched_workflows_default
  135. @result_object.result[:matched_workflows] || []
  136. end
  137. def rerun_count_default
  138. @result_object.result[:rerun_count] || 0
  139. end
  140. def options_array(options)
  141. result = []
  142. options.each do |option|
  143. result << option['value']
  144. if option['children'].present?
  145. result += options_array(option['children'])
  146. end
  147. end
  148. result
  149. end
  150. def options_hash(options)
  151. options.keys
  152. end
  153. def options_relation(attribute)
  154. key = "#{attribute[:relation]}_#{attribute[:name]}"
  155. @options_relation ||= {}
  156. @options_relation[key] ||= "CoreWorkflow::Attributes::#{attribute[:relation]}".constantize.new(attributes: self, attribute: attribute)
  157. @options_relation[key].values
  158. end
  159. def attribute_filter?(attribute)
  160. screen_value(attribute, 'filter').present?
  161. end
  162. def attribute_options_array?(attribute)
  163. attribute[:options].present? && attribute[:options].instance_of?(Array)
  164. end
  165. def attribute_options_hash?(attribute)
  166. attribute[:options].present? && attribute[:options].instance_of?(Hash)
  167. end
  168. def attribute_options_relation?(attribute)
  169. attribute[:tag] == 'select' && attribute[:relation].present?
  170. end
  171. def values(attribute)
  172. values = nil
  173. if attribute_filter?(attribute)
  174. values = screen_value(attribute, 'filter')
  175. elsif attribute_options_array?(attribute)
  176. values = options_array(attribute[:options])
  177. elsif attribute_options_hash?(attribute)
  178. values = options_hash(attribute[:options])
  179. elsif attribute_options_relation?(attribute)
  180. values = options_relation(attribute)
  181. end
  182. values
  183. end
  184. def values_empty(attribute, values)
  185. return values if values == ['']
  186. saved_value = saved_attribute_value(attribute)
  187. if saved_value.present?
  188. values |= Array(saved_value).map(&:to_s)
  189. end
  190. if screen_value(attribute, 'nulloption') && values.exclude?('')
  191. values.unshift('')
  192. end
  193. values
  194. end
  195. def restrict_values_default
  196. result = {}
  197. object_elements.each do |attribute|
  198. values = values(attribute)
  199. next if values.blank?
  200. values = values_empty(attribute, values)
  201. result[ attribute[:name] ] = values.map(&:to_s)
  202. end
  203. result
  204. end
  205. def all_options_default
  206. object_elements.each_with_object({}) do |attribute, result|
  207. next if !attribute_options_array?(attribute) && !attribute_options_hash?(attribute)
  208. result[ attribute[:name] ] = attribute[:options]
  209. end
  210. end
  211. def saved_attribute_value(attribute)
  212. # special case for owner_id
  213. return if saved_only&.class == Ticket && attribute[:name] == 'owner_id'
  214. saved_only&.try(attribute[:name])
  215. end
  216. end