history.rb 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class History < ApplicationModel
  3. load 'history/assets.rb'
  4. include History::Assets
  5. self.table_name = 'histories'
  6. belongs_to :history_type, class_name: 'History::Type'
  7. belongs_to :history_object, class_name: 'History::Object'
  8. belongs_to :history_attribute, class_name: 'History::Attribute'
  9. =begin
  10. add a new history entry for an object
  11. History.add(
  12. history_type: 'updated',
  13. history_object: 'Ticket',
  14. history_attribute: 'state',
  15. o_id: ticket.id,
  16. id_to: 3,
  17. id_from: 2,
  18. value_from: 'open',
  19. value_to: 'pending reminder',
  20. created_by_id: 1,
  21. created_at: '2013-06-04 10:00:00',
  22. updated_at: '2013-06-04 10:00:00'
  23. )
  24. =end
  25. def self.add(data)
  26. # return if we run import mode
  27. return if Setting.get('import_mode') && !data[:id]
  28. # lookups
  29. if data[:history_type]
  30. history_type = type_lookup(data[:history_type])
  31. end
  32. if data[:history_object]
  33. history_object = object_lookup(data[:history_object])
  34. end
  35. related_history_object_id = nil
  36. if data[:related_history_object]
  37. related_history_object = object_lookup(data[:related_history_object])
  38. related_history_object_id = related_history_object.id
  39. end
  40. history_attribute_id = nil
  41. if data[:history_attribute]
  42. history_attribute = attribute_lookup(data[:history_attribute])
  43. history_attribute_id = history_attribute.id
  44. end
  45. # create history
  46. record = {
  47. id: data[:id],
  48. o_id: data[:o_id],
  49. history_type_id: history_type.id,
  50. history_object_id: history_object.id,
  51. history_attribute_id: history_attribute_id,
  52. related_history_object_id: related_history_object_id,
  53. related_o_id: data[:related_o_id],
  54. value_from: data[:value_from],
  55. value_to: data[:value_to],
  56. id_from: data[:id_from],
  57. id_to: data[:id_to],
  58. created_at: data[:created_at],
  59. created_by_id: data[:created_by_id]
  60. }
  61. history_record = nil
  62. if data[:id]
  63. history_record = History.find_by(id: data[:id])
  64. end
  65. if history_record
  66. history_record.update!(record)
  67. else
  68. record_new = History.create(record)
  69. if record[:id]
  70. record_new.id = record[:id]
  71. end
  72. record_new.save
  73. end
  74. end
  75. =begin
  76. remove whole history entries of an object
  77. History.remove('Ticket', 123)
  78. =end
  79. def self.remove(requested_object, requested_object_id)
  80. history_object = History::Object.find_by(name: requested_object)
  81. return if !history_object
  82. History.where(
  83. history_object_id: history_object.id,
  84. o_id: requested_object_id,
  85. ).destroy_all
  86. end
  87. =begin
  88. return all history entries of an object
  89. history_list = History.list('Ticket', 123)
  90. returns
  91. history_list = [
  92. { ... },
  93. { ... },
  94. { ... },
  95. { ... },
  96. ]
  97. return all history entries of an object and it's related history objects
  98. history_list = History.list('Ticket', 123, true)
  99. returns
  100. history_list = [
  101. { ... },
  102. { ... },
  103. { ... },
  104. { ... },
  105. ]
  106. return all history entries of an object and it's assets
  107. history = History.list('Ticket', 123, nil, true)
  108. returns
  109. history = {
  110. list: list,
  111. assets: assets,
  112. }
  113. =end
  114. def self.list(requested_object, requested_object_id, related_history_object = nil, assets = nil)
  115. if !related_history_object
  116. history_object = object_lookup(requested_object)
  117. history = History.where(history_object_id: history_object.id)
  118. .where(o_id: requested_object_id)
  119. .order('created_at ASC, id ASC')
  120. else
  121. history_object_requested = object_lookup(requested_object)
  122. history_object_related = object_lookup(related_history_object)
  123. history = History.where(
  124. '((history_object_id = ? AND o_id = ?) OR (history_object_id = ? AND related_o_id = ? ))',
  125. history_object_requested.id,
  126. requested_object_id,
  127. history_object_related.id,
  128. requested_object_id,
  129. )
  130. .order('created_at ASC, id ASC')
  131. end
  132. asset_list = {}
  133. list = []
  134. history.each do |item|
  135. if assets
  136. asset_list = item.assets(asset_list)
  137. end
  138. data = item.attributes
  139. data['object'] = object_lookup_id(data['history_object_id']).name
  140. data['type'] = type_lookup_id(data['history_type_id']).name
  141. data.delete('history_object_id')
  142. data.delete('history_type_id')
  143. if data['history_attribute_id']
  144. data['attribute'] = attribute_lookup_id(data['history_attribute_id']).name
  145. end
  146. data.delete('history_attribute_id')
  147. data.delete('updated_at')
  148. if data['id_to'].nil? && data['id_from'].nil?
  149. data.delete('id_to')
  150. data.delete('id_from')
  151. end
  152. if data['value_to'].nil? && data['value_from'].nil?
  153. data.delete('value_to')
  154. data.delete('value_from')
  155. end
  156. if !data['related_history_object_id'].nil?
  157. data['related_object'] = object_lookup_id(data['related_history_object_id']).name
  158. end
  159. data.delete('related_history_object_id')
  160. if data['related_o_id'].nil?
  161. data.delete('related_o_id')
  162. end
  163. list.push data
  164. end
  165. if assets
  166. return {
  167. list: list,
  168. assets: asset_list,
  169. }
  170. end
  171. list
  172. end
  173. def self.type_lookup_id(id)
  174. History::Type.lookup(id: id)
  175. end
  176. def self.type_lookup(name)
  177. # lookup
  178. history_type = History::Type.lookup(name: name)
  179. if history_type
  180. return history_type
  181. end
  182. # create
  183. History::Type.create(
  184. name: name
  185. )
  186. end
  187. def self.object_lookup_id(id)
  188. History::Object.lookup(id: id)
  189. end
  190. def self.object_lookup(name)
  191. # lookup
  192. history_object = History::Object.lookup(name: name)
  193. if history_object
  194. return history_object
  195. end
  196. # create
  197. History::Object.create(
  198. name: name
  199. )
  200. end
  201. def self.attribute_lookup_id(id)
  202. History::Attribute.lookup(id: id)
  203. end
  204. def self.attribute_lookup(name)
  205. # lookup
  206. history_attribute = History::Attribute.lookup(name: name)
  207. if history_attribute
  208. return history_attribute
  209. end
  210. # create
  211. History::Attribute.create(
  212. name: name
  213. )
  214. end
  215. class Object < ApplicationModel
  216. end
  217. class Type < ApplicationModel
  218. end
  219. class Attribute < ApplicationModel
  220. end
  221. end