From 99b48369216ad53a441b8a96b2165f4cd683511d Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 4 Jan 2013 09:09:59 +0100 Subject: [PATCH] Show only possibe ticket states. --- .../_application_controller.js.coffee | 2 +- .../controllers/agent_ticket_create.js.coffee | 2 +- .../javascripts/app/models/ticket.js.coffee | 1 - app/controllers/tickets_controller.rb | 86 ++++++------ app/models/ticket.rb | 122 ++++++++++-------- lib/session.rb | 2 +- 6 files changed, 112 insertions(+), 103 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_application_controller.js.coffee b/app/assets/javascripts/app/controllers/_application_controller.js.coffee index 5c2927968..dcd310e8b 100644 --- a/app/assets/javascripts/app/controllers/_application_controller.js.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller.js.coffee @@ -80,7 +80,7 @@ class App.Controller extends Spine.Controller { name: 'last_contact_customer', callback: @frontendTime }, { name: 'first_response', callback: @frontendTime }, { name: 'close_time', callback: @frontendTime }, - { name: 'article_count', title: 'group'}, + { name: 'article_count', }, ] shown_all_attributes = [] for all_attribute in all_attributes diff --git a/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee b/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee index c2841ba01..377182fd2 100644 --- a/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee +++ b/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee @@ -92,7 +92,7 @@ class Index extends App.Controller # set defaults defaults = template['options'] || {} if !( 'ticket_state_id' of defaults ) - defaults['ticket_state_id'] = App.Collection.findByAttribute( 'TicketState', 'name', 'new' ).id + defaults['ticket_state_id'] = App.Collection.findByAttribute( 'TicketState', 'name', 'open' ).id if !( 'ticket_priority_id' of defaults ) defaults['ticket_priority_id'] = App.Collection.findByAttribute( 'TicketPriority', 'name', '2 normal' ).id diff --git a/app/assets/javascripts/app/models/ticket.js.coffee b/app/assets/javascripts/app/models/ticket.js.coffee index a25312b88..f311ae2aa 100644 --- a/app/assets/javascripts/app/models/ticket.js.coffee +++ b/app/assets/javascripts/app/models/ticket.js.coffee @@ -17,5 +17,4 @@ class App.Ticket extends App.Model { name: 'first_response', display: 'First response', tag: 'time', null: true, style: 'width: 12%' }, { name: 'close_time', display: 'Close time', tag: 'time', null: true, style: 'width: 12%' }, { name: 'article_count', display: 'Article#', style: 'width: 12%' }, - ] \ No newline at end of file diff --git a/app/controllers/tickets_controller.rb b/app/controllers/tickets_controller.rb index eb61af342..6cb8b78fa 100644 --- a/app/controllers/tickets_controller.rb +++ b/app/controllers/tickets_controller.rb @@ -323,14 +323,6 @@ class TicketsController < ApplicationController users[ticket.created_by_id] = User.user_data_full( ticket.created_by_id ) end - owner_ids = [] - ticket.agent_of_group.each { |user| - owner_ids.push user.id - if !users[user.id] - users[user.id] = User.user_data_full( user.id ) - end - } - # log object as viewed log_view( ticket ) @@ -349,6 +341,23 @@ class TicketsController < ApplicationController ) end + # get attributes to update + attributes_to_change = Ticket.attributes_to_change( :user => current_user, :ticket => ticket ) + + attributes_to_change[:owner_id].each { |user_id| + if !users[user_id] + users[user_id] = User.user_data_full( user_id ) + end + } + + attributes_to_change[:group_id__owner_id].each {|group_id, user_ids| + user_ids.each {|user_id| + if !users[user_id] + users[user_id] = User.user_data_full( user_id ) + end + } + } + # get related articles ticket = ticket.attributes ticket[:article_ids] = [] @@ -377,52 +386,44 @@ class TicketsController < ApplicationController end } - # get groups - group_ids = [] - Group.where( :active => true ).each { |group| - group_ids.push group.id - } - agents = {} - Ticket.agents.each { |user| - agents[ user.id ] = 1 - } - groups_users = {} - group_ids.each {|group_id| - groups_users[ group_id ] = [] - Group.find(group_id).users.each {|user| - next if !agents[ user.id ] - groups_users[ group_id ].push user.id - if !users[user.id] - users[user.id] = User.user_data_full( user.id ) - end - } - } - # return result render :json => { :ticket => ticket, :articles => articles_used, :signature => signature, :users => users, - :edit_form => { - :group_id__owner_id => groups_users, - :owner_id => owner_ids, - } + :edit_form => attributes_to_change, } end # GET /ticket_create/1 def ticket_create - # get attributes - create_attributes = Ticket.create_attributes( - :current_user_id => current_user.id, + # get attributes to update + attributes_to_change = Ticket.attributes_to_change( + :user => current_user, +# :ticket_id => params[:ticket_id], +# :article_id => params[:article_id] ) + users = {} + attributes_to_change[:owner_id].each { |user_id| + if !users[user_id] + users[user_id] = User.user_data_full( user_id ) + end + } + + attributes_to_change[:group_id__owner_id].each {|group_id, user_ids| + user_ids.each {|user_id| + if !users[user_id] + users[user_id] = User.user_data_full( user_id ) + end + } + } + # split data ticket = nil articles = nil - users = {} if params[:ticket_id] && params[:article_id] ticket = Ticket.find( params[:ticket_id] ) @@ -444,7 +445,7 @@ class TicketsController < ApplicationController users[user.id] = User.user_data_full( user.id ) end } - + # get related articles ticket[:article_ids] = [ params[:article_id] ] @@ -459,18 +460,12 @@ class TicketsController < ApplicationController end end - create_attributes[:owner_id].each {|user_id| - if !users[user_id] - users[user_id] = User.user_data_full( user_id ) - end - } - # return result render :json => { :ticket => ticket, :articles => [ article ], :users => users, - :edit_form => create_attributes, + :edit_form => attributes_to_change, } end @@ -522,5 +517,4 @@ class TicketsController < ApplicationController } end - end diff --git a/app/models/ticket.rb b/app/models/ticket.rb index ddcf966f0..1d6e6f6c6 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -26,6 +26,75 @@ class Ticket < ApplicationModel User.where( :active => true ).joins(:roles).where( 'roles.name' => 'Agent', 'roles.active' => true ).uniq() end + def self.attributes_to_change(params) + if params[:ticket_id] + params[:ticket] = self.find( params[:ticket_id] ) + end + if params[:article_id] + params[:article] = self.find( params[:article_id] ) + end + + # get ticket states + ticket_state_ids = [] + if params[:ticket] + ticket_state_type = params[:ticket].ticket_state.state_type + end + ticket_state_types = ['open', 'closed', 'pending action', 'pending reminder'] + if ticket_state_type && !ticket_state_types.include?(ticket_state_type.name) + ticket_state_ids.push params[:ticket].ticket_state.id + end + ticket_state_types.each {|type| + ticket_state_type = Ticket::StateType.where( :name => type ).first + if ticket_state_type + ticket_state_type.states.each {|ticket_state| + ticket_state_ids.push ticket_state.id + } + end + } + + # get owner + owner_ids = [] + if params[:ticket] + params[:ticket].agent_of_group.each { |user| + owner_ids.push user.id + } + end + + # get group + group_ids = [] + Group.where( :active => true ).each { |group| + group_ids.push group.id + } + + # get group / user relations + agents = {} + Ticket.agents.each { |user| + agents[ user.id ] = 1 + } + groups_users = {} + group_ids.each {|group_id| + groups_users[ group_id ] = [] + Group.find( group_id ).users.each {|user| + next if !agents[ user.id ] + groups_users[ group_id ].push user.id + } + } + + # get priorities + ticket_priority_ids = [] + Ticket::Priority.where( :active => true ).each { |priority| + ticket_priority_ids.push priority.id + } + + return { + :ticket_state_id => ticket_state_ids, + :ticket_priority_id => ticket_priority_ids, + :owner_id => owner_ids, + :group_id => group_ids, + :group_id__owner_id => groups_users, + } + end + def merge_to(data) # update articles @@ -300,59 +369,6 @@ class Ticket < ApplicationModel end -# Ticket.create_attributes( -# :current_user_id => 123, -# ) - def self.create_attributes (data) - - # get groups - group_ids = [] - Group.where( :active => true ).each { |group| - group_ids.push group.id - } - - # get related users -# users = {} - user_ids = [] - agents = {} - Ticket.agents.each { |user| - agents[ user.id ] = 1 - user_ids.push user.id - } - groups_users = {} - group_ids.each {|group_id| - groups_users[ group_id ] = [] - Group.find(group_id).users.each {|user| - next if !agents[ user.id ] - groups_users[ group_id ].push user.id -# if !users[user.id] -# users[user.id] = User.user_data_full(user.id) -# end - } - } - - # get states - ticket_state_ids = [] - Ticket::State.where( :active => true ).each { |state| - ticket_state_ids.push state.id - } - - # get priorities - ticket_priority_ids = [] - Ticket::Priority.where( :active => true ).each { |priority| - ticket_priority_ids.push priority.id - } - - return { -# :users => users, - :owner_id => user_ids, - :group_id__owner_id => groups_users, - :group_id => group_ids, - :ticket_state_id => ticket_state_ids, - :ticket_priority_id => ticket_priority_ids, - } - end - def self.number_adapter # load backend based on config diff --git a/lib/session.rb b/lib/session.rb index 9bef738e8..a515ffd40 100644 --- a/lib/session.rb +++ b/lib/session.rb @@ -311,7 +311,7 @@ class UserState # create_attributes cache_key = @cache_key + '_ticket_create_attributes' if CacheIn.expired(cache_key) - ticket_create_attributes = Ticket.create_attributes( + ticket_create_attributes = Ticket.attributes_to_change( :current_user_id => user.id, ) ticket_create_attributes_cache = CacheIn.get( cache_key, { :re_expire => true } )