history.rb 8.2 KB

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