attributes.rb 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. # Copyright (C) 2012-2022 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. attribute[:screens].dig(@payload['screen'], type)
  89. end
  90. def request_id_default
  91. payload['request_id']
  92. end
  93. # dont cache this else the result object will work with references and cache bugs occur
  94. def visibility_default
  95. object_elements.each_with_object({}) do |attribute, result|
  96. result[ attribute[:name] ] = screen_value(attribute, 'shown') == false ? 'remove' : 'show'
  97. end
  98. end
  99. def attribute_mandatory?(attribute)
  100. return screen_value(attribute, 'required').present? if !screen_value(attribute, 'required').nil?
  101. return screen_value(attribute, 'null').blank? if !screen_value(attribute, 'null').nil?
  102. false
  103. end
  104. # dont cache this else the result object will work with references and cache bugs occur
  105. def mandatory_default
  106. object_elements.each_with_object({}) do |attribute, result|
  107. result[ attribute[:name] ] = attribute_mandatory?(attribute)
  108. end
  109. end
  110. # dont cache this else the result object will work with references and cache bugs occur
  111. def auto_select_default
  112. object_elements.each_with_object({}) do |attribute, result|
  113. next if !attribute[:only_shown_if_selectable]
  114. result[ attribute[:name] ] = true
  115. end
  116. end
  117. # dont cache this else the result object will work with references and cache bugs occur
  118. def readonly_default
  119. object_elements.each_with_object({}) do |attribute, result|
  120. result[ attribute[:name] ] = false
  121. end
  122. end
  123. def select_default
  124. @result_object.result[:select] || {}
  125. end
  126. def fill_in_default
  127. @result_object.result[:fill_in] || {}
  128. end
  129. def eval_default
  130. []
  131. end
  132. def matched_workflows_default
  133. @result_object.result[:matched_workflows] || []
  134. end
  135. def rerun_count_default
  136. @result_object.result[:rerun_count] || 0
  137. end
  138. def options_array(options)
  139. result = []
  140. options.each do |option|
  141. result << option['value']
  142. if option['children'].present?
  143. result += options_array(option['children'])
  144. end
  145. end
  146. result
  147. end
  148. def options_hash(options)
  149. options.keys
  150. end
  151. def options_relation(attribute)
  152. key = "#{attribute[:relation]}_#{attribute[:name]}"
  153. @options_relation ||= {}
  154. @options_relation[key] ||= "CoreWorkflow::Attributes::#{attribute[:relation]}".constantize.new(attributes: self, attribute: attribute)
  155. @options_relation[key].values
  156. end
  157. def attribute_filter?(attribute)
  158. screen_value(attribute, 'filter').present?
  159. end
  160. def attribute_options_array?(attribute)
  161. attribute[:options].present? && attribute[:options].instance_of?(Array)
  162. end
  163. def attribute_options_hash?(attribute)
  164. attribute[:options].present? && attribute[:options].instance_of?(Hash)
  165. end
  166. def attribute_options_relation?(attribute)
  167. attribute[:tag] == 'select' && attribute[:relation].present?
  168. end
  169. def values(attribute)
  170. values = nil
  171. if attribute_filter?(attribute)
  172. values = screen_value(attribute, 'filter')
  173. elsif attribute_options_array?(attribute)
  174. values = options_array(attribute[:options])
  175. elsif attribute_options_hash?(attribute)
  176. values = options_hash(attribute[:options])
  177. elsif attribute_options_relation?(attribute)
  178. values = options_relation(attribute)
  179. end
  180. values
  181. end
  182. def values_empty(attribute, values)
  183. return values if values == ['']
  184. saved_value = saved_attribute_value(attribute)
  185. if saved_value.present?
  186. values |= Array(saved_value).map(&:to_s)
  187. end
  188. if attribute[:nulloption] && values.exclude?('')
  189. values.unshift('')
  190. end
  191. values
  192. end
  193. def restrict_values_default
  194. result = {}
  195. object_elements.each do |attribute|
  196. values = values(attribute)
  197. next if values.blank?
  198. values = values_empty(attribute, values)
  199. result[ attribute[:name] ] = values.map(&:to_s)
  200. end
  201. result
  202. end
  203. def saved_attribute_value(attribute)
  204. # special case for owner_id
  205. return if saved_only&.class == Ticket && attribute[:name] == 'owner_id'
  206. saved_only&.try(attribute[:name])
  207. end
  208. end