2013-06-12 15:59:58 +00:00
|
|
|
# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2013-01-01 20:29:26 +00:00
|
|
|
class History < ApplicationModel
|
2013-08-19 06:29:49 +00:00
|
|
|
include History::Assets
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
self.table_name = 'histories'
|
|
|
|
belongs_to :history_type, :class_name => 'History::Type'
|
|
|
|
belongs_to :history_object, :class_name => 'History::Object'
|
|
|
|
belongs_to :history_attribute, :class_name => 'History::Attribute'
|
2013-06-12 15:59:58 +00:00
|
|
|
# before_validation :check_type, :check_object
|
|
|
|
# attr_writer :history_type, :history_object
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2012-11-07 23:47:05 +00:00
|
|
|
@@cache_type = {}
|
|
|
|
@@cache_object = {}
|
|
|
|
@@cache_attribute = {}
|
|
|
|
|
2013-06-14 08:05:42 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
add a new history entry for an object
|
|
|
|
|
|
|
|
History.add(
|
|
|
|
:history_type => 'updated',
|
|
|
|
:history_object => 'Ticket',
|
|
|
|
:history_attribute => 'ticket_state',
|
|
|
|
:o_id => ticket.id,
|
|
|
|
:id_to => 3,
|
|
|
|
:id_from => 2,
|
|
|
|
:value_from => 'open',
|
|
|
|
:value_to => 'pending',
|
|
|
|
:created_by_id => 1,
|
|
|
|
:created_at => '2013-06-04 10:00:00',
|
|
|
|
:updated_at => '2013-06-04 10:00:00'
|
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.add(data)
|
2012-07-10 08:09:58 +00:00
|
|
|
|
|
|
|
# lookups
|
2012-11-07 23:47:05 +00:00
|
|
|
if data[:history_type]
|
2013-09-23 14:44:43 +00:00
|
|
|
history_type = self.type_lookup( data[:history_type] )
|
2012-07-10 08:09:58 +00:00
|
|
|
end
|
2012-11-07 23:47:05 +00:00
|
|
|
if data[:history_object]
|
2013-09-23 14:44:43 +00:00
|
|
|
history_object = self.object_lookup( data[:history_object] )
|
2012-07-10 08:09:58 +00:00
|
|
|
end
|
|
|
|
related_history_object_id = nil
|
|
|
|
if data[:related_history_object]
|
2013-09-23 14:44:43 +00:00
|
|
|
related_history_object = self.object_lookup( data[:related_history_object] )
|
2012-07-10 08:09:58 +00:00
|
|
|
related_history_object_id = related_history_object.id
|
|
|
|
end
|
|
|
|
history_attribute_id = nil
|
|
|
|
if data[:history_attribute]
|
2012-11-07 23:47:05 +00:00
|
|
|
history_attribute = self.history_attribute_lookup( data[:history_attribute] )
|
2012-07-10 08:09:58 +00:00
|
|
|
history_attribute_id = history_attribute.id
|
|
|
|
end
|
|
|
|
|
|
|
|
# create history
|
2012-12-24 13:55:43 +00:00
|
|
|
record = {
|
|
|
|
:id => data[:id],
|
2012-07-10 08:09:58 +00:00
|
|
|
:o_id => data[:o_id],
|
|
|
|
:history_type_id => history_type.id,
|
|
|
|
:history_object_id => history_object.id,
|
|
|
|
:history_attribute_id => history_attribute_id,
|
|
|
|
:related_history_object_id => related_history_object_id,
|
|
|
|
:related_o_id => data[:related_o_id],
|
|
|
|
:value_from => data[:value_from],
|
|
|
|
:value_to => data[:value_to],
|
|
|
|
:id_from => data[:id_from],
|
|
|
|
:id_to => data[:id_to],
|
2012-12-24 13:55:43 +00:00
|
|
|
:created_at => data[:created_at],
|
2012-07-10 08:09:58 +00:00
|
|
|
:created_by_id => data[:created_by_id]
|
2012-12-24 13:55:43 +00:00
|
|
|
}
|
|
|
|
history_record = nil
|
|
|
|
if data[:id]
|
|
|
|
history_record = History.where( :id => data[:id] ).first
|
|
|
|
end
|
|
|
|
if history_record
|
|
|
|
history_record.update_attributes(record)
|
|
|
|
else
|
|
|
|
record_new = History.create(record)
|
2013-05-27 07:25:34 +00:00
|
|
|
if record[:id]
|
|
|
|
record_new.id = record[:id]
|
|
|
|
end
|
2012-12-24 13:55:43 +00:00
|
|
|
record_new.save
|
|
|
|
end
|
2012-07-10 08:09:58 +00:00
|
|
|
end
|
|
|
|
|
2013-06-14 08:05:42 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
remove whole history entries of an object
|
|
|
|
|
|
|
|
History.remove( 'Ticket', 123 )
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2013-06-04 12:52:56 +00:00
|
|
|
def self.remove( requested_object, requested_object_id )
|
|
|
|
history_object = History::Object.where( :name => requested_object ).first
|
|
|
|
History.where(
|
|
|
|
:history_object_id => history_object.id,
|
|
|
|
:o_id => requested_object_id,
|
|
|
|
).destroy_all
|
2012-07-10 08:09:58 +00:00
|
|
|
end
|
|
|
|
|
2013-06-14 08:05:42 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
return all histoy entries of an object
|
|
|
|
|
|
|
|
history_list = History.list( 'Ticket', 123 )
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2013-06-04 12:52:56 +00:00
|
|
|
def self.list( requested_object, requested_object_id, related_history_object = nil )
|
2012-07-10 08:09:58 +00:00
|
|
|
if !related_history_object
|
2013-09-23 14:44:43 +00:00
|
|
|
history_object = self.object_lookup( requested_object )
|
2012-11-07 23:47:05 +00:00
|
|
|
history = History.where( :history_object_id => history_object.id ).
|
2013-06-12 15:59:58 +00:00
|
|
|
where( :o_id => requested_object_id ).
|
|
|
|
where( :history_type_id => History::Type.where( :name => ['created', 'updated', 'notification', 'email', 'added', 'removed'] ) ).
|
|
|
|
order('created_at ASC, id ASC')
|
2012-07-10 08:09:58 +00:00
|
|
|
else
|
2013-09-23 14:44:43 +00:00
|
|
|
history_object_requested = self.object_lookup( requested_object )
|
|
|
|
history_object_related = self.object_lookup( related_history_object )
|
2012-07-10 08:09:58 +00:00
|
|
|
history = History.where(
|
2013-06-12 15:59:58 +00:00
|
|
|
'((history_object_id = ? AND o_id = ?) OR (history_object_id = ? AND related_o_id = ? )) AND history_type_id IN (?)',
|
|
|
|
history_object_requested.id,
|
|
|
|
requested_object_id,
|
|
|
|
history_object_related.id,
|
|
|
|
requested_object_id,
|
|
|
|
History::Type.where( :name => ['created', 'updated', 'notification', 'email', 'added', 'removed'] )
|
|
|
|
).
|
|
|
|
order('created_at ASC, id ASC')
|
2012-07-10 08:09:58 +00:00
|
|
|
end
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2013-06-14 08:05:42 +00:00
|
|
|
return history
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2012-10-18 19:29:56 +00:00
|
|
|
def self.activity_stream( user, limit = 10 )
|
2013-06-12 15:59:58 +00:00
|
|
|
# g = Group.where( :active => true ).joins(:users).where( 'users.id' => user.id )
|
|
|
|
# stream = History.select("distinct(histories.o_id), created_by_id, history_attribute_id, history_type_id, history_object_id, value_from, value_to").
|
|
|
|
# where( :history_type_id => History::Type.where( :name => ['created', 'updated']) ).
|
2012-04-10 14:06:46 +00:00
|
|
|
stream = History.select("distinct(histories.o_id), created_by_id, history_type_id, history_object_id").
|
2013-06-12 15:59:58 +00:00
|
|
|
where( :history_object_id => History::Object.where( :name => [ 'Ticket', 'Ticket::Article' ] ) ).
|
|
|
|
where( :history_type_id => History::Type.where( :name => [ 'created', 'updated' ]) ).
|
|
|
|
order('created_at DESC, id DESC').
|
|
|
|
limit(limit)
|
2012-04-20 12:25:13 +00:00
|
|
|
datas = []
|
2012-04-10 14:06:46 +00:00
|
|
|
stream.each do |item|
|
2012-04-20 12:25:13 +00:00
|
|
|
data = item.attributes
|
2013-09-23 14:44:43 +00:00
|
|
|
data['history_object'] = self.object_lookup_id( data['history_object_id'] ).name
|
|
|
|
data['history_type'] = self.type_lookup_id( data['history_type_id'] ).name
|
2012-07-10 08:09:58 +00:00
|
|
|
data.delete('history_object_id')
|
|
|
|
data.delete('history_type_id')
|
2012-04-20 12:25:13 +00:00
|
|
|
datas.push data
|
2013-06-12 15:59:58 +00:00
|
|
|
# item['history_attribute'] = item.history_attribute
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-04-20 12:25:13 +00:00
|
|
|
return datas
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2012-10-18 19:29:56 +00:00
|
|
|
def self.activity_stream_fulldata( user, limit = 10 )
|
2012-07-23 22:22:23 +00:00
|
|
|
activity_stream = History.activity_stream( user, limit )
|
|
|
|
|
|
|
|
# get related users
|
2013-08-19 06:29:49 +00:00
|
|
|
assets = {}
|
2012-07-23 22:22:23 +00:00
|
|
|
activity_stream.each {|item|
|
|
|
|
|
|
|
|
# load article ids
|
|
|
|
if item['history_object'] == 'Ticket'
|
2013-08-19 06:29:49 +00:00
|
|
|
ticket = Ticket.find( item['o_id'] )
|
|
|
|
assets = ticket.assets(assets)
|
2012-07-23 22:22:23 +00:00
|
|
|
end
|
|
|
|
if item['history_object'] == 'Ticket::Article'
|
2013-08-19 06:29:49 +00:00
|
|
|
article = Ticket::Article.find( item['o_id'] )
|
|
|
|
assets = article.assets(assets)
|
2012-07-23 22:22:23 +00:00
|
|
|
end
|
|
|
|
if item['history_object'] == 'User'
|
2013-08-19 06:29:49 +00:00
|
|
|
user = User.find( item['o_id'] )
|
|
|
|
assets = user.assets(assets)
|
2012-07-23 22:22:23 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
:activity_stream => activity_stream,
|
2013-08-19 06:29:49 +00:00
|
|
|
:assets => assets,
|
2012-07-23 22:22:23 +00:00
|
|
|
}
|
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
|
|
|
|
private
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2013-09-23 14:44:43 +00:00
|
|
|
def self.type_lookup_id( id )
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# use cache
|
|
|
|
return @@cache_type[ id ] if @@cache_type[ id ]
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# lookup
|
|
|
|
history_type = History::Type.find(id)
|
|
|
|
@@cache_type[ id ] = history_type
|
|
|
|
return history_type
|
|
|
|
end
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2013-09-23 14:44:43 +00:00
|
|
|
def self.type_lookup( name )
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# use cache
|
|
|
|
return @@cache_type[ name ] if @@cache_type[ name ]
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# lookup
|
|
|
|
history_type = History::Type.where( :name => name ).first
|
|
|
|
if history_type
|
2012-11-07 23:47:05 +00:00
|
|
|
@@cache_type[ name ] = history_type
|
|
|
|
return history_type
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# create
|
|
|
|
history_type = History::Type.create(
|
|
|
|
:name => name
|
|
|
|
)
|
|
|
|
@@cache_type[ name ] = history_type
|
|
|
|
return history_type
|
|
|
|
end
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2013-09-23 14:44:43 +00:00
|
|
|
def self.object_lookup_id( id )
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# use cache
|
|
|
|
return @@cache_object[ id ] if @@cache_object[ id ]
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# lookup
|
|
|
|
history_object = History::Object.find(id)
|
|
|
|
@@cache_object[ id ] = history_object
|
|
|
|
return history_object
|
|
|
|
end
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2013-09-23 14:44:43 +00:00
|
|
|
def self.object_lookup( name )
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# use cache
|
|
|
|
return @@cache_object[ name ] if @@cache_object[ name ]
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# lookup
|
|
|
|
history_object = History::Object.where( :name => name ).first
|
|
|
|
if history_object
|
2012-11-07 23:47:05 +00:00
|
|
|
@@cache_object[ name ] = history_object
|
|
|
|
return history_object
|
|
|
|
end
|
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# create
|
|
|
|
history_object = History::Object.create(
|
|
|
|
:name => name
|
|
|
|
)
|
|
|
|
@@cache_object[ name ] = history_object
|
|
|
|
return history_object
|
|
|
|
end
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
def self.history_attribute_lookup( name )
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# use cache
|
|
|
|
return @@cache_attribute[ name ] if @@cache_attribute[ name ]
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# lookup
|
|
|
|
history_attribute = History::Attribute.where( :name => name ).first
|
|
|
|
if history_attribute
|
2012-11-07 23:47:05 +00:00
|
|
|
@@cache_attribute[ name ] = history_attribute
|
|
|
|
return history_attribute
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# create
|
|
|
|
history_attribute = History::Attribute.create(
|
|
|
|
:name => name
|
|
|
|
)
|
|
|
|
@@cache_attribute[ name ] = history_attribute
|
|
|
|
return history_attribute
|
|
|
|
end
|
|
|
|
|
2013-09-25 19:50:28 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
nothing to do on destroying history entries
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def destroy_dependencies
|
|
|
|
end
|
|
|
|
|
2013-01-01 20:29:26 +00:00
|
|
|
class Object < ApplicationModel
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
2013-01-01 20:29:26 +00:00
|
|
|
class Type < ApplicationModel
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
2013-01-01 20:29:26 +00:00
|
|
|
class Attribute < ApplicationModel
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|