2016-10-19 03:11:36 +00:00
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
2015-04-27 23:19:26 +00:00
module Ticket::Search
2013-08-17 21:10:11 +00:00
= begin
2015-08-16 00:53:27 +00:00
search tickets preferences
result = Ticket . search_preferences ( user_model )
returns if user has permissions to search
result = {
prio : 3000 ,
direct_search_index : false
}
returns if user has no permissions to search
result = false
= end
2015-08-16 09:14:24 +00:00
def search_preferences ( _current_user )
{
prio : 3000 ,
direct_search_index : false ,
}
end
2015-08-16 00:53:27 +00:00
= begin
2014-11-10 07:34:20 +00:00
search tickets via search index
2013-08-17 21:10:11 +00:00
result = Ticket . search (
2015-06-30 13:36:04 +00:00
current_user : User . find ( 123 ) ,
query : 'search something' ,
limit : 15 ,
2013-08-17 21:10:11 +00:00
)
returns
result = [ ticket_model1 , ticket_model2 ]
2014-11-10 07:34:20 +00:00
search tickets via search index
2014-08-18 11:49:08 +00:00
result = Ticket . search (
2015-06-30 13:36:04 +00:00
current_user : User . find ( 123 ) ,
query : 'search something' ,
limit : 15 ,
full : false ,
2014-08-18 11:49:08 +00:00
)
returns
result = [ 1 , 3 , 5 , 6 , 7 ]
2014-11-10 07:34:20 +00:00
search tickets via database
result = Ticket . search (
2015-06-30 13:36:04 +00:00
current_user : User . find ( 123 ) ,
2016-09-11 08:46:01 +00:00
query : 'some query' , # query or condition is required
2015-06-30 13:36:04 +00:00
condition : {
2015-09-17 18:39:51 +00:00
'tickets.owner_id' = > {
operator : 'is' ,
value : user . id ,
} ,
'tickets.state_id' = > {
operator : 'is' ,
value : Ticket :: State . where (
state_type_id : Ticket :: StateType . where (
name : [
'pending reminder' ,
'pending action' ,
] ,
) . map ( & :id ) ,
} ,
2015-06-22 19:32:29 +00:00
) ,
} ,
2015-06-30 13:36:04 +00:00
limit : 15 ,
full : false ,
2014-11-10 07:34:20 +00:00
)
returns
result = [ 1 , 3 , 5 , 6 , 7 ]
2013-08-17 21:10:11 +00:00
= end
2015-08-16 09:14:24 +00:00
def search ( params )
2013-08-17 21:10:11 +00:00
2015-04-27 23:19:26 +00:00
# get params
query = params [ :query ]
2016-09-11 08:46:01 +00:00
condition = params [ :condition ]
2015-04-27 23:19:26 +00:00
limit = params [ :limit ] || 12
current_user = params [ :current_user ]
full = false
2017-12-14 13:19:24 +00:00
if params [ :full ] == true || params [ :full ] == 'true' || ! params . key? ( :full )
2015-04-27 23:19:26 +00:00
full = true
end
2013-08-17 21:10:11 +00:00
2015-04-27 23:19:26 +00:00
# try search index backend
2017-12-13 18:20:37 +00:00
if condition . blank? && SearchIndexBackend . enabled?
2015-04-27 23:19:26 +00:00
query_extention = { }
query_extention [ 'bool' ] = { }
query_extention [ 'bool' ] [ 'must' ] = [ ]
2016-08-12 16:39:09 +00:00
if current_user . permissions? ( 'ticket.agent' )
2017-06-16 20:43:09 +00:00
group_ids = current_user . group_ids_access ( 'read' )
2015-04-27 23:19:26 +00:00
access_condition = {
2017-06-16 20:43:09 +00:00
'query_string' = > { 'default_field' = > 'group_id' , 'query' = > " \" #{ group_ids . join ( '" OR "' ) } \" " }
2015-04-27 23:19:26 +00:00
}
else
2016-01-15 17:22:57 +00:00
access_condition = if ! current_user . organization || ( ! current_user . organization . shared || current_user . organization . shared == false )
{
2016-09-09 21:10:27 +00:00
'query_string' = > { 'default_field' = > 'customer_id' , 'query' = > current_user . id }
2016-01-15 17:22:57 +00:00
}
# customer_id: XXX
# conditions = [ 'customer_id = ?', current_user.id ]
else
{
2016-09-09 21:10:27 +00:00
'query_string' = > { 'query' = > " customer_id: #{ current_user . id } OR organization_id: #{ current_user . organization . id } " }
2016-01-15 17:22:57 +00:00
}
# customer_id: XXX OR organization_id: XXX
# conditions = [ '( customer_id = ? OR organization_id = ? )', current_user.id, current_user.organization.id ]
end
2014-02-02 18:58:31 +00:00
end
2016-01-15 17:22:57 +00:00
query_extention [ 'bool' ] [ 'must' ] . push access_condition
2016-01-20 01:48:54 +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 = [ ]
2017-10-01 12:25:52 +00:00
items . each do | item |
2015-04-27 23:19:26 +00:00
ids . push item [ :id ]
2017-10-01 12:25:52 +00:00
end
2014-08-18 11:49:08 +00:00
return ids
end
2015-04-27 21:27:51 +00:00
tickets = [ ]
2017-10-01 12:25:52 +00:00
items . each do | item |
2016-12-13 13:58:13 +00:00
ticket = Ticket . lookup ( id : item [ :id ] )
next if ! ticket
tickets . push ticket
2017-10-01 12:25:52 +00:00
end
2015-04-27 23:19:26 +00:00
return tickets
end
# fallback do sql query
2017-06-16 20:43:09 +00:00
access_condition = Ticket . access_condition ( current_user , 'read' )
2015-04-27 23:19:26 +00:00
# do query
# - stip out * we already search for *query* -
if query
2015-08-21 23:55:59 +00:00
query . delete! '*'
2016-01-20 01:48:54 +00:00
tickets_all = Ticket . select ( 'DISTINCT(tickets.id), tickets.created_at' )
2016-01-15 17:22:57 +00:00
. where ( access_condition )
2016-01-20 01:48:54 +00:00
. 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 } % " )
2016-01-15 17:22:57 +00:00
. joins ( :articles )
2016-01-20 01:48:54 +00:00
. order ( 'tickets.created_at DESC' )
2016-01-15 17:22:57 +00:00
. limit ( limit )
2015-04-27 23:19:26 +00:00
else
2016-12-14 02:13:30 +00:00
query_condition , bind_condition , tables = selector2sql ( condition )
2016-01-20 01:48:54 +00:00
tickets_all = Ticket . select ( 'DISTINCT(tickets.id), tickets.created_at' )
2016-12-14 02:27:08 +00:00
. joins ( tables )
2016-01-15 17:22:57 +00:00
. where ( access_condition )
. where ( query_condition , * bind_condition )
2016-01-20 01:48:54 +00:00
. order ( 'tickets.created_at DESC' )
2016-01-15 17:22:57 +00:00
. limit ( limit )
2015-04-27 23:19:26 +00:00
end
# build result list
if ! full
ids = [ ]
2017-10-01 12:25:52 +00:00
tickets_all . each do | ticket |
2015-04-27 23:19:26 +00:00
ids . push ticket . id
2017-10-01 12:25:52 +00:00
end
2015-04-27 23:19:26 +00:00
return ids
2013-08-17 21:10:11 +00:00
end
2015-04-27 23:19:26 +00:00
tickets = [ ]
2017-10-01 12:25:52 +00:00
tickets_all . each do | ticket |
2016-01-20 01:48:54 +00:00
tickets . push Ticket . lookup ( id : ticket . id )
2017-10-01 12:25:52 +00:00
end
2015-04-27 23:19:26 +00:00
tickets
2013-08-17 21:10:11 +00:00
end
2014-02-03 19:23:00 +00:00
end