trabajo-afectivo/app/controllers/ticket_overviews_controller.rb

455 lines
12 KiB
Ruby
Raw Normal View History

2012-04-10 14:06:46 +00:00
class TicketOverviewsController < ApplicationController
before_filter :authentication_check
# GET /tickets
def show
2012-05-11 09:21:41 +00:00
#sleep 2
2012-04-10 14:06:46 +00:00
# get navbar overview data
if !params[:view]
result = Ticket.overview(
:current_user_id => current_user.id,
)
2012-04-14 16:47:37 +00:00
render :json => result
2012-04-10 14:06:46 +00:00
return
end
# get real overview data
if params[:array]
overview = Ticket.overview(
:view => params[:view],
:current_user_id => current_user.id,
:array => true,
)
tickets = []
overview[:tickets].each {|ticket|
data = { :id => ticket.id }
tickets.push data
}
# return result
render :json => {
:overview => overview[:overview],
:tickets => tickets,
:tickets_count => overview[:tickets_count],
}
return
end
overview = Ticket.overview(
:view => params[:view],
:view_mode => params[:view_mode],
:current_user_id => current_user.id,
:start_page => params[:start_page],
2012-07-29 20:25:31 +00:00
:array => true,
)
2012-04-10 14:06:46 +00:00
# get related users
users = {}
tickets = []
overview[:tickets].each {|ticket|
2012-07-29 20:25:31 +00:00
data = Ticket.full_data(ticket.id)
tickets.push data
if !users[ data['owner_id'] ]
users[ data['owner_id'] ] = User.user_data_full( data['owner_id'] )
2012-04-10 14:06:46 +00:00
end
2012-07-29 20:25:31 +00:00
if !users[ data['customer_id'] ]
users[ data['customer_id'] ] = User.user_data_full( data['customer_id'] )
2012-04-10 14:06:46 +00:00
end
2012-07-29 20:25:31 +00:00
if !users[ data['created_by_id'] ]
users[ data['created_by_id'] ] = User.user_data_full( data['created_by_id'] )
2012-04-10 14:06:46 +00:00
end
}
# get data for bulk action
bulk_owners = Role.where( :name => ['Agent'] ).first.users.select(:id).where( :active => true ).uniq()
bulk_owner_ids = []
bulk_owners.each { |user|
bulk_owner_ids.push user.id
if !users[ user.id ]
2012-07-29 18:55:51 +00:00
users[ user.id ] = User.user_data_full( user.id )
2012-04-10 14:06:46 +00:00
end
}
# return result
2012-04-14 16:47:37 +00:00
render :json => {
:overview => overview[:overview],
2012-04-14 16:47:37 +00:00
:tickets => tickets,
:tickets_count => overview[:tickets_count],
2012-04-14 16:47:37 +00:00
:users => users,
:bulk => {
:owner_id => {
:id => bulk_owner_ids,
},
2012-04-14 16:47:37 +00:00
},
}
2012-04-10 14:06:46 +00:00
end
# GET /ticket_create/1
2012-04-10 14:06:46 +00:00
def ticket_create
2012-07-29 15:27:01 +00:00
# get attributes
(users, ticket_owner_ids, ticket_group_ids, ticket_state_ids, ticket_priority_ids) = Ticket.create_attributes(
:current_user_id => current_user.id,
)
2012-04-10 14:06:46 +00:00
2012-07-07 09:09:09 +00:00
# split data
ticket = nil
articles = nil
if params[:ticket_id] && params[:article_id]
ticket = Ticket.find( params[:ticket_id] )
# get related users
users = {}
if !users[ticket.owner_id]
2012-07-29 18:55:51 +00:00
users[ticket.owner_id] = User.user_data_full(ticket.owner_id)
2012-07-07 09:09:09 +00:00
end
if !users[ticket.customer_id]
2012-07-29 18:55:51 +00:00
users[ticket.customer_id] = User.user_data_full(ticket.customer_id)
2012-07-07 09:09:09 +00:00
end
if !users[ticket.created_by_id]
2012-07-29 18:55:51 +00:00
users[ticket.created_by_id] = User.user_data_full(ticket.created_by_id)
2012-07-07 09:09:09 +00:00
end
owner_ids = []
ticket.agent_of_group.each { |user|
owner_ids.push user.id
if !users[user.id]
2012-07-29 18:55:51 +00:00
users[user.id] = User.user_data_full(user.id)
2012-07-07 09:09:09 +00:00
end
}
# get related articles
ticket[:article_ids] = [ params[:article_id] ]
article = Ticket::Article.find( params[:article_id] )
# add attachment list to article
article['attachments'] = Store.list( :object => 'Ticket::Article', :o_id => article.id )
# load users
if !users[article.created_by_id]
2012-07-29 18:55:51 +00:00
users[article.created_by_id] = User.user_data_full(article.created_by_id)
2012-07-07 09:09:09 +00:00
end
end
2012-04-10 14:06:46 +00:00
# return result
2012-04-14 16:47:37 +00:00
render :json => {
2012-07-07 09:09:09 +00:00
:ticket => ticket,
:articles => [ article ],
2012-04-14 16:47:37 +00:00
:users => users,
:edit_form => {
:owner_id => {
:id => ticket_owner_ids
},
:group_id => {
:id => ticket_group_ids
},
:ticket_state_id => {
:id => ticket_state_ids
},
:ticket_priority_id => {
:id => ticket_priority_ids
2012-04-10 14:06:46 +00:00
}
}
2012-04-14 16:47:37 +00:00
}
2012-04-10 14:06:46 +00:00
end
# GET /ticket_full/1
def ticket_full
ticket = Ticket.find(params[:id])
# get related users
users = {}
if !users[ticket.owner_id]
2012-07-29 18:55:51 +00:00
users[ticket.owner_id] = User.user_data_full(ticket.owner_id)
2012-04-10 14:06:46 +00:00
end
if !users[ticket.customer_id]
2012-07-29 18:55:51 +00:00
users[ticket.customer_id] = User.user_data_full(ticket.customer_id)
2012-04-10 14:06:46 +00:00
end
if !users[ticket.created_by_id]
2012-07-29 18:55:51 +00:00
users[ticket.created_by_id] = User.user_data_full(ticket.created_by_id)
2012-04-10 14:06:46 +00:00
end
owner_ids = []
ticket.agent_of_group.each { |user|
owner_ids.push user.id
if !users[user.id]
2012-07-29 18:55:51 +00:00
users[user.id] = User.user_data_full(user.id)
2012-04-10 14:06:46 +00:00
end
}
# get related articles
ticket[:article_ids] = []
articles = ticket.articles || []
# get related users
articles.each {|article|
# load article ids
ticket[:article_ids].push article.id
# add attachment list to article
article['attachments'] = Store.list( :object => 'Ticket::Article', :o_id => article.id )
# load users
if !users[article.created_by_id]
2012-07-29 18:55:51 +00:00
users[article.created_by_id] = User.user_data_full(article.created_by_id)
2012-04-10 14:06:46 +00:00
end
}
# log object as viewed
log_view(ticket)
# return result
2012-04-14 16:47:37 +00:00
render :json => {
:ticket => ticket,
:articles => articles,
:users => users,
:edit_form => {
:owner_id => {
:id => owner_ids
2012-04-10 14:06:46 +00:00
}
}
2012-04-14 16:47:37 +00:00
}
2012-04-10 14:06:46 +00:00
end
# POST /ticket_attachment/new
def ticket_attachment_new
# puts '-------'
# puts params.inspect
# store file
2012-05-11 09:21:41 +00:00
# content_type = request.content_type
content_type = request[:content_type]
puts 'content_type: ' + content_type.inspect
if !content_type || content_type == 'application/octet-stream'
if MIME::Types.type_for(params[:qqfile]).first
content_type = MIME::Types.type_for(params[:qqfile]).first.content_type
else
content_type = 'application/octet-stream'
end
2012-04-10 14:06:46 +00:00
end
headers_store = {
'Content-Type' => content_type
}
Store.add(
:object => 'UploadCache::' + params[:form] + '::' + current_user.id.to_s,
:o_id => params[:form_id],
:data => request.body.read,
:filename => params[:qqfile],
:preferences => headers_store
)
# return result
2012-04-14 16:47:37 +00:00
render :json => {
:success => true,
}
2012-04-10 14:06:46 +00:00
end
# GET /ticket_attachment/1
def ticket_attachment
# permissin check
# find file
file = Store.find(params[:id])
send_data(
file.store_file.data,
:filename => file.filename,
2012-07-02 18:52:27 +00:00
:type => file.preferences['Content-Type'] || file.preferences['Mime-Type'],
2012-04-10 14:06:46 +00:00
:disposition => 'inline'
)
end
2012-07-02 12:47:09 +00:00
# GET /ticket_article_plain/1
def ticket_article_plain
# permissin check
list = Store.list(
:object => 'Ticket::Article::Mail',
:o_id => params[:id],
)
# find file
if list
file = Store.find(list.first)
send_data(
file.store_file.data,
:filename => file.filename,
:type => 'message/rfc822',
:disposition => 'inline'
)
end
end
2012-04-10 14:06:46 +00:00
# GET /ticket_customer
# GET /tickets_customer
def ticket_customer
# get closed/open states
ticket_state_list_open = Ticket::State.where(
:ticket_state_type_id => Ticket::StateType.where(:name => ['new','open', 'pending remidner', 'pending action'])
)
ticket_state_list_closed = Ticket::State.where(
:ticket_state_type_id => Ticket::StateType.where(:name => ['closed'] )
)
# get tickets
tickets_open = Ticket.where(
:customer_id => params[:customer_id],
:ticket_state_id => ticket_state_list_open
).limit(15).order('created_at DESC')
tickets_closed = Ticket.where(
:customer_id => params[:customer_id],
:ticket_state_id => ticket_state_list_closed
).limit(15).order('created_at DESC')
# tickets = Ticket.where(:customer_id => user_id).limit(15).order('created_at DESC')
# ticket_items = []
# tickets.each do |ticket|
# style = ''
# ticket_state_type = ticket.ticket_state.ticket_state_type.name
# if ticket_state_type == 'closed' || ticket_state_type == 'merged'
# style = 'text-decoration: line-through'
# end
# item = {
# :url => '#ticket/zoom/' + ticket.id.to_s,
# :name => 'T:' + ticket.number.to_s,
# :title => ticket.title,
# :style => style
# }
# ticket_items.push item
# end
# if ticket_items[0]
# topic = {
# :title => 'Tickets',
# :items => ticket_items
# }
# user['links'].push topic
# end
# return result
2012-04-14 16:47:37 +00:00
render :json => {
:tickets => {
:open => tickets_open,
:closed => tickets_closed
2012-04-10 14:06:46 +00:00
}
2012-04-14 16:47:37 +00:00
# :users => users,
}
2012-04-10 14:06:46 +00:00
end
# GET /ticket_history/1
def ticket_history
# get ticket data
ticket = Ticket.find(params[:id])
# get history of ticket
2012-07-10 08:09:58 +00:00
history = History.history_list( 'Ticket', params[:id], 'Ticket::Article' )
2012-04-10 14:06:46 +00:00
# get related users
users = {}
history.each do |item|
2012-07-29 18:55:51 +00:00
users[ item['created_by_id'] ] = User.user_data_full( item['created_by_id'] )
2012-07-10 08:09:58 +00:00
if item['history_object'] == 'Ticket::Article'
item['type'] = 'Article ' + item['type'].to_s
else
item['type'] = 'Ticket ' + item['type'].to_s
end
2012-04-10 14:06:46 +00:00
end
# fetch meta relations
history_objects = History::Object.all()
history_types = History::Type.all()
history_attributes = History::Attribute.all()
# return result
2012-04-14 16:47:37 +00:00
render :json => {
:ticket => ticket,
:users => users,
:history => history,
:history_objects => history_objects,
:history_types => history_types,
:history_attributes => history_attributes
}
2012-04-10 14:06:46 +00:00
end
# GET /ticket_merge/1/1
def ticket_merge
# check master ticket
ticket_master = Ticket.where( :number => params[:master_ticket_number] ).first
if !ticket_master
render :json => {
:result => 'faild',
:message => 'No such master ticket number!',
}
return
end
# check slave ticket
ticket_slave = Ticket.where( :id => params[:slave_ticket_id] ).first
if !ticket_slave
render :json => {
:result => 'faild',
:message => 'No such slave ticket!',
}
return
end
# check diffetent ticket ids
if ticket_slave.id == ticket_master.id
render :json => {
:result => 'faild',
:message => 'Can\'t merge ticket with it self!',
}
return
end
# merge ticket
success = ticket_slave.merge_to(
{
:ticket_id => ticket_master.id,
:created_by_id => current_user.id,
}
)
# return result
render :json => {
:result => 'success',
:master_ticket => ticket_master.attributes,
:slave_ticket => ticket_slave.attributes,
}
end
2012-04-10 14:06:46 +00:00
# GET /user_search
def user_search
# get params
query = params[:term]
limit = params[:limit] || 18
# do query
user_all = User.find(
:all,
2012-04-23 06:53:39 +00:00
:limit => limit,
2012-04-10 14:06:46 +00:00
:conditions => ['firstname LIKE ? or lastname LIKE ? or email LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%"],
2012-04-23 06:53:39 +00:00
:order => 'firstname'
2012-04-10 14:06:46 +00:00
)
# build result list
users = []
user_all.each do |user|
realname = user.firstname.to_s + ' ' + user.lastname.to_s
if user.email && user.email.to_s != ''
2012-04-10 14:06:46 +00:00
realname = realname + ' <' + user.email.to_s + '>'
end
a = { :id => user.id, :label => realname, :value => realname }
users.push a
end
# return result
2012-04-14 16:47:37 +00:00
render :json => users
2012-04-10 14:06:46 +00:00
end
end