diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 5e2302e4f..613f4265a 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -232,6 +232,51 @@ class Ticket < ApplicationModel return false end +# Ticket.search( +# :current_user => 123, +# :query => 'search something', +# :limit => 15, +# ) + def self.search (params) + + # get params + query = params[:query] + limit = params[:limit] || 12 + current_user = params[:current_user] + + conditions = [] + if current_user.is_role('Agent') + group_ids = Group.select( 'groups.id' ).joins(:users). + where( 'groups_users.user_id = ?', current_user.id ). + where( 'groups.active = ?', true ). + map( &:id ) + conditions = [ 'group_id IN (?)', group_ids ] + else + if !current_user.organization || ( !current_user.organization.shared || current_user.organization.shared == false ) + conditions = [ 'customer_id = ?', current_user.id ] + else + conditions = [ '( customer_id = ? OR organization_id = ? )', current_user.id, current_user.organization.id ] + end + end + + # do query + tickets_all = Ticket.select('DISTINCT(tickets.id)'). + where(conditions). + where( '( `tickets`.`title` LIKE ? OR `tickets`.`number` LIKE ? OR `ticket_articles`.`body` LIKE ? OR `ticket_articles`.`from` LIKE ? OR `ticket_articles`.`to` LIKE ? OR `ticket_articles`.`subject` LIKE ?)', "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%" ). + joins(:articles). + limit(limit). + order('`tickets`.`created_at` DESC') + + # build result list + tickets = [] + users = {} + tickets_all.each do |ticket| + ticket_tmp = Ticket.lookup( :id => ticket.id ) + tickets.push ticket_tmp + end + + return tickets + end # Ticket.overview_list( # :current_user => 123, # ) @@ -659,21 +704,4 @@ class Ticket < ApplicationModel class Number end - class Flag < ApplicationModel - end - - class Priority < ApplicationModel - self.table_name = 'ticket_priorities' - validates :name, :presence => true - end - - class StateType < ApplicationModel - has_many :states, :class_name => 'Ticket::State' - validates :name, :presence => true - end - - class State < ApplicationModel - belongs_to :state_type, :class_name => 'Ticket::StateType' - validates :name, :presence => true - end end \ No newline at end of file diff --git a/app/models/ticket/flag.rb b/app/models/ticket/flag.rb new file mode 100644 index 000000000..8b71b0740 --- /dev/null +++ b/app/models/ticket/flag.rb @@ -0,0 +1,2 @@ +class Ticket::Flag < ApplicationModel +end diff --git a/app/models/ticket/priority.rb b/app/models/ticket/priority.rb new file mode 100644 index 000000000..37340366f --- /dev/null +++ b/app/models/ticket/priority.rb @@ -0,0 +1,4 @@ +class Ticket::Priority < ApplicationModel + self.table_name = 'ticket_priorities' + validates :name, :presence => true +end \ No newline at end of file diff --git a/app/models/ticket/state.rb b/app/models/ticket/state.rb new file mode 100644 index 000000000..34c1cb09f --- /dev/null +++ b/app/models/ticket/state.rb @@ -0,0 +1,4 @@ +class Ticket::State < ApplicationModel + belongs_to :state_type, :class_name => 'Ticket::StateType' + validates :name, :presence => true +end \ No newline at end of file diff --git a/app/models/ticket/state_type.rb b/app/models/ticket/state_type.rb new file mode 100644 index 000000000..6fea7b174 --- /dev/null +++ b/app/models/ticket/state_type.rb @@ -0,0 +1,4 @@ +class Ticket::StateType < ApplicationModel + has_many :states, :class_name => 'Ticket::State' + validates :name, :presence => true +end \ No newline at end of file