2013-06-12 15:59:58 +00:00
|
|
|
# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2013-05-21 22:41:32 +00:00
|
|
|
class RecentView < ApplicationModel
|
|
|
|
belongs_to :recent_view_object, :class_name => 'RecentView::Object'
|
|
|
|
|
|
|
|
@@cache_object = {}
|
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
def self.log( object, user )
|
2013-05-21 22:41:32 +00:00
|
|
|
|
|
|
|
# lookups
|
|
|
|
recent_view_object = self.recent_view_object_lookup( object.class.to_s )
|
|
|
|
|
|
|
|
# create entry
|
|
|
|
record = {
|
|
|
|
:o_id => object.id,
|
|
|
|
:recent_view_object_id => recent_view_object.id,
|
|
|
|
:created_by_id => user.id,
|
|
|
|
}
|
|
|
|
RecentView.create(record)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.log_destroy( requested_object, requested_object_id )
|
|
|
|
RecentView.where( :recent_view_object_id => RecentView::Object.where( :name => requested_object ) ).
|
2013-06-12 15:59:58 +00:00
|
|
|
where( :o_id => requested_object_id ).
|
|
|
|
destroy_all
|
2013-05-21 22:41:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.list( user, limit = 10 )
|
|
|
|
recent_views = RecentView.where( :created_by_id => user.id ).
|
2013-06-12 15:59:58 +00:00
|
|
|
order('created_at DESC, id DESC').
|
|
|
|
limit(limit)
|
2013-05-21 22:41:32 +00:00
|
|
|
|
|
|
|
list = []
|
|
|
|
recent_views.each { |item|
|
|
|
|
data = item.attributes
|
|
|
|
data['recent_view_object'] = self.recent_view_object_lookup_id( data['recent_view_object_id'] ).name
|
|
|
|
data.delete( 'history_object_id' )
|
|
|
|
list.push data
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.list_fulldata( user, limit = 10 )
|
|
|
|
recent_viewed = self.list( user, limit )
|
|
|
|
|
|
|
|
# get related users
|
|
|
|
users = {}
|
|
|
|
tickets = []
|
|
|
|
recent_viewed.each {|item|
|
|
|
|
|
|
|
|
# load article ids
|
2013-06-12 15:59:58 +00:00
|
|
|
# if item.recent_view_object == 'Ticket'
|
|
|
|
ticket = Ticket.find( item['o_id'] ).attributes
|
|
|
|
tickets.push ticket
|
|
|
|
# end
|
|
|
|
# if item.recent_view_object 'Ticket::Article'
|
|
|
|
# tickets.push Ticket::Article.find(item.o_id)
|
|
|
|
# end
|
|
|
|
# if item.recent_view_object 'User'
|
|
|
|
# tickets.push User.find(item.o_id)
|
|
|
|
# end
|
|
|
|
|
2013-05-21 22:41:32 +00:00
|
|
|
# load users
|
|
|
|
if !users[ ticket['owner_id'] ]
|
|
|
|
users[ ticket['owner_id'] ] = User.user_data_full( ticket['owner_id'] )
|
|
|
|
end
|
|
|
|
if !users[ ticket['created_by_id'] ]
|
|
|
|
users[ ticket['created_by_id'] ] = User.user_data_full( ticket['created_by_id'] )
|
|
|
|
end
|
|
|
|
if !users[ item['created_by_id'] ]
|
|
|
|
users[ item['created_by_id'] ] = User.user_data_full( item['created_by_id'] )
|
|
|
|
end
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
:recent_viewed => recent_viewed,
|
|
|
|
:tickets => tickets,
|
|
|
|
:users => users,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
def self.recent_view_object_lookup_id( id )
|
2013-05-21 22:41:32 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# use cache
|
|
|
|
return @@cache_object[ id ] if @@cache_object[ id ]
|
2013-05-21 22:41:32 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# lookup
|
|
|
|
history_object = RecentView::Object.find(id)
|
|
|
|
@@cache_object[ id ] = history_object
|
|
|
|
return history_object
|
|
|
|
end
|
2013-05-21 22:41:32 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
def self.recent_view_object_lookup( name )
|
2013-05-21 22:41:32 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# use cache
|
|
|
|
return @@cache_object[ name ] if @@cache_object[ name ]
|
2013-05-21 22:41:32 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# lookup
|
|
|
|
recent_view_object = RecentView::Object.where( :name => name ).first
|
|
|
|
if recent_view_object
|
2013-05-21 22:41:32 +00:00
|
|
|
@@cache_object[ name ] = recent_view_object
|
|
|
|
return recent_view_object
|
|
|
|
end
|
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# create
|
|
|
|
recent_view_object = RecentView::Object.create(
|
|
|
|
:name => name
|
|
|
|
)
|
|
|
|
@@cache_object[ name ] = recent_view_object
|
|
|
|
return recent_view_object
|
|
|
|
end
|
|
|
|
|
2013-05-21 22:41:32 +00:00
|
|
|
class Object < ApplicationModel
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|