2014-02-03 19:23:00 +00:00
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
2013-08-17 21:10:11 +00:00
module Ticket::Search
= begin
search tickets
result = Ticket . search (
:current_user = > User . find ( 123 ) ,
:query = > 'search something' ,
:limit = > 15 ,
)
returns
result = [ ticket_model1 , ticket_model2 ]
2014-08-18 11:49:08 +00:00
search tickets
result = Ticket . search (
:current_user = > User . find ( 123 ) ,
:query = > 'search something' ,
:limit = > 15 ,
:full = > 0
)
returns
result = [ 1 , 3 , 5 , 6 , 7 ]
2013-08-17 21:10:11 +00:00
= end
2013-08-17 21:10:36 +00:00
def search ( params )
2013-08-17 21:10:11 +00:00
# get params
query = params [ :query ]
limit = params [ :limit ] || 12
current_user = params [ :current_user ]
2014-08-18 11:49:08 +00:00
full = false
if params [ :full ] || ! params . has_key? ( :full )
full = true
end
2013-08-17 21:10:11 +00:00
2014-02-02 18:58:31 +00:00
# try search index backend
2014-08-18 11:49:08 +00:00
if ! params [ :detail ] && SearchIndexBackend . enabled?
2014-02-02 18:58:31 +00:00
query_extention = { }
query_extention [ 'bool' ] = { }
query_extention [ 'bool' ] [ 'must' ] = [ ]
if current_user . is_role ( 'Agent' )
groups = Group . joins ( :users ) .
2014-02-03 19:23:00 +00:00
where ( 'groups_users.user_id = ?' , current_user . id ) .
where ( 'groups.active = ?' , true )
group_condition = [ ]
groups . each { | group |
group_condition . push group . name
}
2014-08-18 11:49:08 +00:00
access_condition = {
2014-02-03 19:23:00 +00:00
'query_string' = > { 'default_field' = > 'Ticket.group.name' , 'query' = > " \" #{ group_condition . join ( '" OR "' ) } \" " }
2014-02-02 18:58:31 +00:00
}
2014-08-18 11:49:08 +00:00
query_extention [ 'bool' ] [ 'must' ] . push access_condition
2014-02-02 18:58:31 +00:00
else
if ! current_user . organization || ( ! current_user . organization . shared || current_user . organization . shared == false )
2014-08-18 11:49:08 +00:00
access_condition = {
2014-02-03 19:23:00 +00:00
'query_string' = > { 'default_field' = > 'Ticket.customer_id' , 'query' = > current_user . id }
2014-02-02 18:58:31 +00:00
}
2014-02-03 19:23:00 +00:00
# customer_id: XXX
# conditions = [ 'customer_id = ?', current_user.id ]
2014-02-02 18:58:31 +00:00
else
2014-08-18 11:49:08 +00:00
access_condition = {
2014-02-03 19:23:00 +00:00
'query_string' = > { 'query' = > " Ticket.customer_id: #{ current_user . id } OR Ticket.organization_id: #{ current_user . organization . id } " }
2014-02-02 18:58:31 +00:00
}
2014-02-03 19:23:00 +00:00
# customer_id: XXX OR organization_id: XXX
# conditions = [ '( customer_id = ? OR organization_id = ? )', current_user.id, current_user.organization.id ]
2014-02-02 18:58:31 +00:00
end
2014-08-18 11:49:08 +00:00
query_extention [ 'bool' ] [ 'must' ] . push access_condition
2014-02-02 18:58:31 +00:00
end
2014-09-19 21:35:40 +00:00
items = SearchIndexBackend . search ( query , limit , 'Ticket' , query_extention )
2014-08-18 11:49:08 +00:00
if ! full
2014-09-19 21:35:40 +00:00
ids = [ ]
items . each { | item |
ids . push item [ :id ]
}
2014-08-18 11:49:08 +00:00
return ids
end
2014-02-02 18:58:31 +00:00
tickets = [ ]
2014-09-19 21:35:40 +00:00
items . each { | item |
tickets . push Ticket . lookup ( :id = > item [ :id ] )
2014-02-02 18:58:31 +00:00
}
return tickets
end
# fallback do sql query
2014-08-18 11:49:08 +00:00
access_condition = [ ]
2013-08-17 21:10:11 +00:00
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 )
2014-08-18 11:49:08 +00:00
access_condition = [ 'group_id IN (?)' , group_ids ]
2013-08-17 21:10:11 +00:00
else
if ! current_user . organization || ( ! current_user . organization . shared || current_user . organization . shared == false )
2014-08-18 11:49:08 +00:00
access_condition = [ 'customer_id = ?' , current_user . id ]
2013-08-17 21:10:11 +00:00
else
2014-08-18 11:49:08 +00:00
access_condition = [ '( customer_id = ? OR organization_id = ? )' , current_user . id , current_user . organization . id ]
2013-08-17 21:10:11 +00:00
end
end
# do query
2014-02-11 13:09:23 +00:00
# - stip out * we already search for *query* -
2014-08-18 11:49:08 +00:00
if query
query . gsub! '*' , ''
tickets_all = Ticket . select ( 'DISTINCT(tickets.id)' ) .
where ( access_condition ) .
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 ) .
order ( '`tickets`.`created_at` DESC' ) .
limit ( limit )
else
tickets_all = Ticket . select ( 'DISTINCT(tickets.id)' ) .
where ( access_condition ) .
where ( params [ :condition ] ) .
order ( '`tickets`.`created_at` DESC' ) .
limit ( limit )
end
2013-08-17 21:10:11 +00:00
# build result list
2014-08-18 11:49:08 +00:00
if ! full
ids = [ ]
tickets_all . each { | ticket |
ids . push ticket . id
}
return ids
2013-08-17 21:10:11 +00:00
end
2014-08-18 11:49:08 +00:00
tickets = [ ]
tickets_all . each { | ticket |
tickets . push Ticket . lookup ( :id = > ticket . id )
}
return tickets
2013-08-17 21:10:11 +00:00
end
2014-02-03 19:23:00 +00:00
end