history.rb 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. # Copyright (C) 2012-2014 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. # before_validation :check_type, :check_object
  10. # attr_writer :history_type, :history_object
  11. @@cache_type = {}
  12. @@cache_object = {}
  13. @@cache_attribute = {}
  14. =begin
  15. add a new history entry for an object
  16. History.add(
  17. :history_type => 'updated',
  18. :history_object => 'Ticket',
  19. :history_attribute => 'state',
  20. :o_id => ticket.id,
  21. :id_to => 3,
  22. :id_from => 2,
  23. :value_from => 'open',
  24. :value_to => 'pending',
  25. :created_by_id => 1,
  26. :created_at => '2013-06-04 10:00:00',
  27. :updated_at => '2013-06-04 10:00:00'
  28. )
  29. =end
  30. def self.add(data)
  31. # return if we run import mode
  32. return if Setting.get('import_mode')
  33. # lookups
  34. if data[:history_type]
  35. history_type = self.type_lookup( data[:history_type] )
  36. end
  37. if data[:history_object]
  38. history_object = self.object_lookup( data[:history_object] )
  39. end
  40. related_history_object_id = nil
  41. if data[:related_history_object]
  42. related_history_object = self.object_lookup( data[:related_history_object] )
  43. related_history_object_id = related_history_object.id
  44. end
  45. history_attribute_id = nil
  46. if data[:history_attribute]
  47. history_attribute = self.attribute_lookup( data[:history_attribute] )
  48. history_attribute_id = history_attribute.id
  49. end
  50. # create history
  51. record = {
  52. :id => data[:id],
  53. :o_id => data[:o_id],
  54. :history_type_id => history_type.id,
  55. :history_object_id => history_object.id,
  56. :history_attribute_id => history_attribute_id,
  57. :related_history_object_id => related_history_object_id,
  58. :related_o_id => data[:related_o_id],
  59. :value_from => data[:value_from],
  60. :value_to => data[:value_to],
  61. :id_from => data[:id_from],
  62. :id_to => data[:id_to],
  63. :created_at => data[:created_at],
  64. :created_by_id => data[:created_by_id]
  65. }
  66. history_record = nil
  67. if data[:id]
  68. history_record = History.where( :id => data[:id] ).first
  69. end
  70. if history_record
  71. history_record.update_attributes(record)
  72. else
  73. record_new = History.create(record)
  74. if record[:id]
  75. record_new.id = record[:id]
  76. end
  77. record_new.save
  78. end
  79. end
  80. =begin
  81. remove whole history entries of an object
  82. History.remove( 'Ticket', 123 )
  83. =end
  84. def self.remove( requested_object, requested_object_id )
  85. history_object = History::Object.where( :name => requested_object ).first
  86. return if !history_object
  87. History.where(
  88. :history_object_id => history_object.id,
  89. :o_id => requested_object_id,
  90. ).destroy_all
  91. end
  92. =begin
  93. return all history entries of an object
  94. history_list = History.list( 'Ticket', 123 )
  95. returns
  96. history_list = [
  97. { ... },
  98. { ... },
  99. { ... },
  100. { ... },
  101. ]
  102. return all history entries of an object and it's related history objects
  103. history_list = History.list( 'Ticket', 123, true )
  104. returns
  105. history_list = [
  106. { ... },
  107. { ... },
  108. { ... },
  109. { ... },
  110. ]
  111. return all history entries of an object and it's assets
  112. history = History.list( 'Ticket', 123, nil, true )
  113. returns
  114. history = {
  115. :list => list,
  116. :assets => assets,
  117. }
  118. =end
  119. def self.list( requested_object, requested_object_id, related_history_object = nil, assets = nil )
  120. if !related_history_object
  121. history_object = self.object_lookup( requested_object )
  122. history = History.where( :history_object_id => history_object.id ).
  123. where( :o_id => requested_object_id ).
  124. order('created_at ASC, id ASC')
  125. else
  126. history_object_requested = self.object_lookup( requested_object )
  127. history_object_related = self.object_lookup( related_history_object )
  128. history = History.where(
  129. '((history_object_id = ? AND o_id = ?) OR (history_object_id = ? AND related_o_id = ? ))',
  130. history_object_requested.id,
  131. requested_object_id,
  132. history_object_related.id,
  133. requested_object_id,
  134. ).
  135. order('created_at ASC, id ASC')
  136. end
  137. asset_list = {}
  138. list = []
  139. history.each do |item|
  140. if assets
  141. asset_list = item.assets( asset_list )
  142. end
  143. data = item.attributes
  144. data['object'] = self.object_lookup_id( data['history_object_id'] ).name
  145. data['type'] = self.type_lookup_id( data['history_type_id'] ).name
  146. data.delete('history_object_id')
  147. data.delete('history_type_id')
  148. if data['history_attribute_id']
  149. data['attribute'] = self.attribute_lookup_id( data['history_attribute_id'] ).name
  150. end
  151. data.delete('history_attribute_id')
  152. data.delete( 'updated_at' )
  153. if data['id_to'] == nil && data['id_from'] == nil
  154. data.delete( 'id_to' )
  155. data.delete( 'id_from' )
  156. end
  157. if data['value_to'] == nil && data['value_from'] == nil
  158. data.delete( 'value_to' )
  159. data.delete( 'value_from' )
  160. end
  161. if data['related_history_object_id'] != nil
  162. data['related_object'] = self.object_lookup_id( data['related_history_object_id'] ).name
  163. end
  164. data.delete( 'related_history_object_id' )
  165. if data['related_o_id'] == nil
  166. data.delete( 'related_o_id' )
  167. end
  168. list.push data
  169. end
  170. if assets
  171. return {
  172. :list => list,
  173. :assets => asset_list,
  174. }
  175. end
  176. list
  177. end
  178. private
  179. def self.type_lookup_id( id )
  180. # use cache
  181. return @@cache_type[ id ] if @@cache_type[ id ]
  182. # lookup
  183. history_type = History::Type.lookup( :id => id )
  184. @@cache_type[ id ] = history_type
  185. return history_type
  186. end
  187. def self.type_lookup( name )
  188. # use cache
  189. return @@cache_type[ name ] if @@cache_type[ name ]
  190. # lookup
  191. history_type = History::Type.lookup( :name => name )
  192. if history_type
  193. @@cache_type[ name ] = history_type
  194. return history_type
  195. end
  196. # create
  197. history_type = History::Type.create(
  198. :name => name
  199. )
  200. @@cache_type[ name ] = history_type
  201. return history_type
  202. end
  203. def self.object_lookup_id( id )
  204. # use cache
  205. return @@cache_object[ id ] if @@cache_object[ id ]
  206. # lookup
  207. history_object = History::Object.lookup( :id => id )
  208. @@cache_object[ id ] = history_object
  209. return history_object
  210. end
  211. def self.object_lookup( name )
  212. # use cache
  213. return @@cache_object[ name ] if @@cache_object[ name ]
  214. # lookup
  215. history_object = History::Object.lookup( :name => name )
  216. if history_object
  217. @@cache_object[ name ] = history_object
  218. return history_object
  219. end
  220. # create
  221. history_object = History::Object.create(
  222. :name => name
  223. )
  224. @@cache_object[ name ] = history_object
  225. return history_object
  226. end
  227. def self.attribute_lookup_id( id )
  228. # use cache
  229. return @@cache_attribute[ id ] if @@cache_attribute[ id ]
  230. # lookup
  231. history_attribute = History::Attribute.lookup( :id => id )
  232. @@cache_attribute[ id ] = history_attribute
  233. return history_attribute
  234. end
  235. def self.attribute_lookup( name )
  236. # use cache
  237. return @@cache_attribute[ name ] if @@cache_attribute[ name ]
  238. # lookup
  239. history_attribute = History::Attribute.lookup( :name => name )
  240. if history_attribute
  241. @@cache_attribute[ name ] = history_attribute
  242. return history_attribute
  243. end
  244. # create
  245. history_attribute = History::Attribute.create(
  246. :name => name
  247. )
  248. @@cache_attribute[ name ] = history_attribute
  249. return history_attribute
  250. end
  251. private
  252. =begin
  253. nothing to do on destroying history entries
  254. =end
  255. def destroy_dependencies
  256. end
  257. class Object < ApplicationModel
  258. end
  259. class Type < ApplicationModel
  260. end
  261. class Attribute < ApplicationModel
  262. end
  263. end