history.rb 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. # Copyright (C) 2012-2013 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. # lookups
  31. if data[:history_type]
  32. history_type = self.history_type_lookup( data[:history_type] )
  33. end
  34. if data[:history_object]
  35. history_object = self.history_object_lookup( data[:history_object] )
  36. end
  37. related_history_object_id = nil
  38. if data[:related_history_object]
  39. related_history_object = self.history_object_lookup( data[:related_history_object] )
  40. related_history_object_id = related_history_object.id
  41. end
  42. history_attribute_id = nil
  43. if data[:history_attribute]
  44. history_attribute = self.history_attribute_lookup( data[:history_attribute] )
  45. history_attribute_id = history_attribute.id
  46. end
  47. # create history
  48. record = {
  49. :id => data[:id],
  50. :o_id => data[:o_id],
  51. :history_type_id => history_type.id,
  52. :history_object_id => history_object.id,
  53. :history_attribute_id => history_attribute_id,
  54. :related_history_object_id => related_history_object_id,
  55. :related_o_id => data[:related_o_id],
  56. :value_from => data[:value_from],
  57. :value_to => data[:value_to],
  58. :id_from => data[:id_from],
  59. :id_to => data[:id_to],
  60. :created_at => data[:created_at],
  61. :created_by_id => data[:created_by_id]
  62. }
  63. history_record = nil
  64. if data[:id]
  65. history_record = History.where( :id => data[:id] ).first
  66. end
  67. if history_record
  68. history_record.update_attributes(record)
  69. else
  70. record_new = History.create(record)
  71. if record[:id]
  72. record_new.id = record[:id]
  73. end
  74. record_new.save
  75. end
  76. end
  77. =begin
  78. remove whole history entries of an object
  79. History.remove( 'Ticket', 123 )
  80. =end
  81. def self.remove( requested_object, requested_object_id )
  82. history_object = History::Object.where( :name => requested_object ).first
  83. History.where(
  84. :history_object_id => history_object.id,
  85. :o_id => requested_object_id,
  86. ).destroy_all
  87. end
  88. =begin
  89. return all histoy entries of an object
  90. history_list = History.list( 'Ticket', 123 )
  91. =end
  92. def self.list( requested_object, requested_object_id, related_history_object = nil )
  93. if !related_history_object
  94. history_object = self.history_object_lookup( requested_object )
  95. history = History.where( :history_object_id => history_object.id ).
  96. where( :o_id => requested_object_id ).
  97. where( :history_type_id => History::Type.where( :name => ['created', 'updated', 'notification', 'email', 'added', 'removed'] ) ).
  98. order('created_at ASC, id ASC')
  99. else
  100. history_object_requested = self.history_object_lookup( requested_object )
  101. history_object_related = self.history_object_lookup( related_history_object )
  102. history = History.where(
  103. '((history_object_id = ? AND o_id = ?) OR (history_object_id = ? AND related_o_id = ? )) AND history_type_id IN (?)',
  104. history_object_requested.id,
  105. requested_object_id,
  106. history_object_related.id,
  107. requested_object_id,
  108. History::Type.where( :name => ['created', 'updated', 'notification', 'email', 'added', 'removed'] )
  109. ).
  110. order('created_at ASC, id ASC')
  111. end
  112. return history
  113. end
  114. def self.activity_stream( user, limit = 10 )
  115. # g = Group.where( :active => true ).joins(:users).where( 'users.id' => user.id )
  116. # stream = History.select("distinct(histories.o_id), created_by_id, history_attribute_id, history_type_id, history_object_id, value_from, value_to").
  117. # where( :history_type_id => History::Type.where( :name => ['created', 'updated']) ).
  118. stream = History.select("distinct(histories.o_id), created_by_id, history_type_id, history_object_id").
  119. where( :history_object_id => History::Object.where( :name => [ 'Ticket', 'Ticket::Article' ] ) ).
  120. where( :history_type_id => History::Type.where( :name => [ 'created', 'updated' ]) ).
  121. order('created_at DESC, id DESC').
  122. limit(limit)
  123. datas = []
  124. stream.each do |item|
  125. data = item.attributes
  126. data['history_object'] = self.history_object_lookup_id( data['history_object_id'] ).name
  127. data['history_type'] = self.history_type_lookup_id( data['history_type_id'] ).name
  128. data.delete('history_object_id')
  129. data.delete('history_type_id')
  130. datas.push data
  131. # item['history_attribute'] = item.history_attribute
  132. end
  133. return datas
  134. end
  135. def self.activity_stream_fulldata( user, limit = 10 )
  136. activity_stream = History.activity_stream( user, limit )
  137. # get related users
  138. assets = {}
  139. activity_stream.each {|item|
  140. # load article ids
  141. if item['history_object'] == 'Ticket'
  142. ticket = Ticket.find( item['o_id'] )
  143. assets = ticket.assets(assets)
  144. end
  145. if item['history_object'] == 'Ticket::Article'
  146. article = Ticket::Article.find( item['o_id'] )
  147. assets = article.assets(assets)
  148. end
  149. if item['history_object'] == 'User'
  150. user = User.find( item['o_id'] )
  151. assets = user.assets(assets)
  152. end
  153. }
  154. return {
  155. :activity_stream => activity_stream,
  156. :assets => assets,
  157. }
  158. end
  159. private
  160. def self.history_type_lookup_id( id )
  161. # use cache
  162. return @@cache_type[ id ] if @@cache_type[ id ]
  163. # lookup
  164. history_type = History::Type.find(id)
  165. @@cache_type[ id ] = history_type
  166. return history_type
  167. end
  168. def self.history_type_lookup( name )
  169. # use cache
  170. return @@cache_type[ name ] if @@cache_type[ name ]
  171. # lookup
  172. history_type = History::Type.where( :name => name ).first
  173. if history_type
  174. @@cache_type[ name ] = history_type
  175. return history_type
  176. end
  177. # create
  178. history_type = History::Type.create(
  179. :name => name
  180. )
  181. @@cache_type[ name ] = history_type
  182. return history_type
  183. end
  184. def self.history_object_lookup_id( id )
  185. # use cache
  186. return @@cache_object[ id ] if @@cache_object[ id ]
  187. # lookup
  188. history_object = History::Object.find(id)
  189. @@cache_object[ id ] = history_object
  190. return history_object
  191. end
  192. def self.history_object_lookup( name )
  193. # use cache
  194. return @@cache_object[ name ] if @@cache_object[ name ]
  195. # lookup
  196. history_object = History::Object.where( :name => name ).first
  197. if history_object
  198. @@cache_object[ name ] = history_object
  199. return history_object
  200. end
  201. # create
  202. history_object = History::Object.create(
  203. :name => name
  204. )
  205. @@cache_object[ name ] = history_object
  206. return history_object
  207. end
  208. def self.history_attribute_lookup( name )
  209. # use cache
  210. return @@cache_attribute[ name ] if @@cache_attribute[ name ]
  211. # lookup
  212. history_attribute = History::Attribute.where( :name => name ).first
  213. if history_attribute
  214. @@cache_attribute[ name ] = history_attribute
  215. return history_attribute
  216. end
  217. # create
  218. history_attribute = History::Attribute.create(
  219. :name => name
  220. )
  221. @@cache_attribute[ name ] = history_attribute
  222. return history_attribute
  223. end
  224. class Object < ApplicationModel
  225. end
  226. class Type < ApplicationModel
  227. end
  228. class Attribute < ApplicationModel
  229. end
  230. end