history_log_base.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. module ApplicationModel::HistoryLogBase
  3. =begin
  4. create history entry for this object
  5. organization = Organization.find(123)
  6. result = organization.history_log( 'created', user_id )
  7. returns
  8. result = true # false
  9. =end
  10. def history_log (type, user_id, data = {})
  11. data[:o_id] = self['id']
  12. data[:history_type] = type
  13. data[:history_object] = self.class.name
  14. data[:related_o_id] = nil
  15. data[:related_history_object] = nil
  16. data[:created_by_id] = user_id
  17. History.add(data)
  18. end
  19. =begin
  20. get history log for this object
  21. organization = Organization.find(123)
  22. result = organization.history_get()
  23. returns
  24. result = [
  25. {
  26. :type => 'created',
  27. :object => 'Organization',
  28. :created_by_id => 3,
  29. :created_at => "2013-08-19 20:41:33",
  30. },
  31. {
  32. :type => 'updated',
  33. :object => 'Organization',
  34. :attribute => 'note',
  35. :o_id => 1,
  36. :id_to => nil,
  37. :id_from => nil,
  38. :value_from => "some note",
  39. :value_to => "some other note",
  40. :created_by_id => 3,
  41. :created_at => "2013-08-19 20:41:33",
  42. },
  43. ]
  44. to get history log for this object with all assets
  45. organization = Organization.find(123)
  46. result = organization.history_get(true)
  47. returns
  48. result = {
  49. :history => [
  50. { ... },
  51. { ... },
  52. ],
  53. :assets => {
  54. ...
  55. }
  56. }
  57. =end
  58. def history_get(fulldata = false)
  59. if !fulldata
  60. return History.list( self.class.name, self['id'] )
  61. end
  62. # get related objects
  63. history = History.list( self.class.name, self['id'], nil, true )
  64. history[:list].each {|item|
  65. record = Kernel.const_get( item['object'] ).find( item['o_id'] )
  66. history[:assets] = record.assets( history[:assets] )
  67. if item['related_object']
  68. record = Kernel.const_get( item['related_object'] ).find( item['related_o_id'] )
  69. history[:assets] = record.assets( history[:assets] )
  70. end
  71. }
  72. return {
  73. :history => history[:list],
  74. :assets => history[:assets],
  75. }
  76. end
  77. end