2014-02-03 19:23:00 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
2015-04-27 23:19:26 +00:00
|
|
|
module Ticket::Overviews
|
2013-08-17 20:04:57 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
all overview by user
|
|
|
|
|
2013-08-19 10:21:31 +00:00
|
|
|
result = Ticket::Overviews.all(
|
2013-08-17 20:04:57 +00:00
|
|
|
:current_user => User.find(123),
|
|
|
|
)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = [overview1, overview2]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
def self.all (data)
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get customer overviews
|
2015-05-08 08:15:45 +00:00
|
|
|
if data[:current_user].role?('Customer')
|
|
|
|
role = Role.find_by( name: 'Customer' )
|
2015-04-27 23:19:26 +00:00
|
|
|
if data[:current_user].organization_id && data[:current_user].organization.shared
|
|
|
|
overviews = Overview.where( role_id: role.id, active: true )
|
|
|
|
else
|
|
|
|
overviews = Overview.where( role_id: role.id, organization_shared: false, active: true )
|
2013-08-17 20:04:57 +00:00
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
return overviews
|
2015-04-27 21:27:51 +00:00
|
|
|
end
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get agent overviews
|
2015-05-08 08:15:45 +00:00
|
|
|
return if !data[:current_user].role?( 'Agent' )
|
|
|
|
role = Role.find_by( name: 'Agent' )
|
2015-04-27 23:19:26 +00:00
|
|
|
Overview.where( role_id: role.id, active: true )
|
|
|
|
end
|
|
|
|
|
2013-08-17 20:04:57 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
selected overview by user
|
|
|
|
|
2013-08-19 10:21:31 +00:00
|
|
|
result = Ticket::Overviews.list(
|
2015-09-17 18:39:51 +00:00
|
|
|
current_user: User.find(123),
|
|
|
|
view: 'some_view_url',
|
2013-08-17 20:04:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = {
|
2015-09-17 18:39:51 +00:00
|
|
|
tickets: tickets, # [ticket1, ticket2, ticket3]
|
|
|
|
tickets_count: tickets_count, # count of tickets
|
|
|
|
overview: overview_selected_raw, # overview attributes
|
2013-08-17 20:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
def self.list (data)
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-05-07 12:10:38 +00:00
|
|
|
overviews = all(data)
|
2015-04-27 23:19:26 +00:00
|
|
|
return if !overviews
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# build up attributes hash
|
|
|
|
overview_selected = nil
|
|
|
|
overview_selected_raw = nil
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
overviews.each { |overview|
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# remember selected view
|
|
|
|
if data[:view] && data[:view] == overview.link
|
|
|
|
overview_selected = overview
|
|
|
|
overview_selected_raw = Marshal.load( Marshal.dump(overview.attributes) )
|
|
|
|
end
|
|
|
|
}
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
if data[:view] && !overview_selected
|
2015-07-03 15:18:01 +00:00
|
|
|
fail "No such view '#{data[:view]}'"
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get only tickets with permissions
|
2015-09-17 18:39:51 +00:00
|
|
|
access_condition = Ticket.access_condition( data[:current_user] )
|
2015-04-27 21:27:51 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# overview meta for navbar
|
|
|
|
if !overview_selected
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# loop each overview
|
|
|
|
result = []
|
|
|
|
overviews.each { |overview|
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-10-14 14:33:26 +00:00
|
|
|
query_condition, bind_condition = Ticket.selector2sql(overview.condition, data[:current_user])
|
2015-09-17 18:39:51 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get count
|
2015-09-17 18:39:51 +00:00
|
|
|
count = Ticket.where( access_condition ).where( query_condition, *bind_condition ).count()
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get meta info
|
|
|
|
all = {
|
|
|
|
name: overview.name,
|
|
|
|
prio: overview.prio,
|
|
|
|
link: overview.link,
|
2015-04-27 21:27:51 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# push to result data
|
|
|
|
result.push all.merge( { count: count } )
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
end
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get result list
|
|
|
|
if data[:array]
|
|
|
|
order_by = overview_selected[:order][:by].to_s + ' ' + overview_selected[:order][:direction].to_s
|
|
|
|
if overview_selected.group_by && !overview_selected.group_by.empty?
|
|
|
|
order_by = overview_selected.group_by + '_id, ' + order_by
|
2013-08-17 20:04:57 +00:00
|
|
|
end
|
2015-09-17 18:39:51 +00:00
|
|
|
|
2015-10-14 14:33:26 +00:00
|
|
|
query_condition, bind_condition = Ticket.selector2sql(overview_selected.condition, data[:current_user])
|
2015-09-17 18:39:51 +00:00
|
|
|
|
|
|
|
tickets = Ticket.select('id')
|
|
|
|
.where( access_condition )
|
|
|
|
.where( query_condition, *bind_condition )
|
2015-05-01 12:27:57 +00:00
|
|
|
.order( order_by )
|
|
|
|
.limit( 500 )
|
2015-04-27 23:19:26 +00:00
|
|
|
|
|
|
|
ticket_ids = []
|
|
|
|
tickets.each { |ticket|
|
|
|
|
ticket_ids.push ticket.id
|
|
|
|
}
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-09-17 18:39:51 +00:00
|
|
|
tickets_count = Ticket.where( access_condition ).where( query_condition, *bind_condition ).count()
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
return {
|
|
|
|
ticket_ids: ticket_ids,
|
2015-04-27 13:42:53 +00:00
|
|
|
tickets_count: tickets_count,
|
|
|
|
overview: overview_selected_raw,
|
2013-08-17 20:04:57 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get tickets for overview
|
|
|
|
data[:start_page] ||= 1
|
2015-10-14 14:33:26 +00:00
|
|
|
query_condition, bind_condition = Ticket.selector2sql(overview_selected.condition, data[:current_user])
|
2015-09-17 18:39:51 +00:00
|
|
|
tickets = Ticket.where( access_condition )
|
|
|
|
.where( query_condition, *bind_condition )
|
2015-05-01 12:27:57 +00:00
|
|
|
.order( overview_selected[:order][:by].to_s + ' ' + overview_selected[:order][:direction].to_s )
|
2015-04-27 23:19:26 +00:00
|
|
|
|
2015-09-17 18:39:51 +00:00
|
|
|
tickets_count = Ticket.where( access_condition ).where( query_condition, *bind_condition ).count()
|
2015-04-27 23:19:26 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
tickets: tickets,
|
|
|
|
tickets_count: tickets_count,
|
|
|
|
overview: overview_selected_raw,
|
|
|
|
}
|
|
|
|
end
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2014-02-03 19:23:00 +00:00
|
|
|
end
|