2014-02-03 19:23:00 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
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
|
2013-09-23 14:44:43 +00:00
|
|
|
recent_view_object = self.object_lookup( object.class.to_s )
|
2013-05-21 22:41:32 +00:00
|
|
|
|
|
|
|
# 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
|
2013-09-23 14:44:43 +00:00
|
|
|
data['recent_view_object'] = self.object_lookup_id( data['recent_view_object_id'] ).name
|
2013-05-21 22:41:32 +00:00
|
|
|
data.delete( 'history_object_id' )
|
|
|
|
list.push data
|
|
|
|
}
|
2014-07-13 18:52:32 +00:00
|
|
|
list
|
2013-05-21 22:41:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.list_fulldata( user, limit = 10 )
|
|
|
|
recent_viewed = self.list( user, limit )
|
|
|
|
|
|
|
|
# get related users
|
2013-08-19 06:29:49 +00:00
|
|
|
assets = {}
|
|
|
|
ticket_ids = []
|
2013-05-21 22:41:32 +00:00
|
|
|
recent_viewed.each {|item|
|
|
|
|
|
|
|
|
# load article ids
|
2013-06-12 15:59:58 +00:00
|
|
|
# if item.recent_view_object == 'Ticket'
|
2013-08-19 06:29:49 +00:00
|
|
|
ticket = Ticket.find( item['o_id'] )
|
|
|
|
ticket_ids.push ticket.id
|
2013-06-12 15:59:58 +00:00
|
|
|
# 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-08-19 06:29:49 +00:00
|
|
|
assets = ticket.assets(assets)
|
2013-05-21 22:41:32 +00:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
:recent_viewed => recent_viewed,
|
2013-08-19 06:29:49 +00:00
|
|
|
:ticket_ids => ticket_ids,
|
|
|
|
:assets => assets,
|
2013-05-21 22:41:32 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-09-23 14:44:43 +00:00
|
|
|
def self.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
|
2013-09-29 21:00:40 +00:00
|
|
|
history_object = RecentView::Object.lookup( :id => id )
|
2013-06-12 15:59:58 +00:00
|
|
|
@@cache_object[ id ] = history_object
|
2013-09-29 21:00:40 +00:00
|
|
|
history_object
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2013-05-21 22:41:32 +00:00
|
|
|
|
2013-09-23 14:44:43 +00:00
|
|
|
def self.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
|
2013-09-29 21:00:40 +00:00
|
|
|
recent_view_object = RecentView::Object.lookup( :name => name )
|
2013-06-12 15:59:58 +00:00
|
|
|
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
|
2013-09-29 21:00:40 +00:00
|
|
|
recent_view_object
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
|
|
|
|
2013-05-21 22:41:32 +00:00
|
|
|
class Object < ApplicationModel
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|