tickets_controller.rb 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class TicketsController < ApplicationController
  3. before_filter :authentication_check
  4. # GET /api/v1/tickets
  5. def index
  6. @tickets = Ticket.all
  7. render :json => @tickets
  8. end
  9. # GET /api/v1/tickets/1
  10. def show
  11. @ticket = Ticket.find( params[:id] )
  12. # permissin check
  13. return if !ticket_permission(@ticket)
  14. render :json => @ticket
  15. end
  16. # POST /api/v1/tickets
  17. def create
  18. ticket = Ticket.new( Ticket.param_validation( params[:ticket] ) )
  19. # check if article is given
  20. if !params[:article]
  21. render :json => 'article hash is missing', :status => :unprocessable_entity
  22. return
  23. end
  24. # create ticket
  25. if !ticket.save
  26. render :json => ticket.errors, :status => :unprocessable_entity
  27. return
  28. end
  29. # create tags if given
  30. if params[:tags] && !params[:tags].empty?
  31. tags = params[:tags].split /,/
  32. tags.each {|tag|
  33. Tag.tag_add(
  34. :object => 'Ticket',
  35. :o_id => ticket.id,
  36. :item => tag,
  37. :created_by_id => current_user.id,
  38. )
  39. }
  40. end
  41. # create article if given
  42. if params[:article]
  43. article_create( ticket, params[:article] )
  44. end
  45. render :json => ticket, :status => :created
  46. end
  47. # PUT /api/v1/tickets/1
  48. def update
  49. ticket = Ticket.find(params[:id])
  50. # permissin check
  51. return if !ticket_permission(ticket)
  52. if ticket.update_attributes( Ticket.param_validation( params[:ticket] ) )
  53. if params[:article]
  54. article_create( ticket, params[:article] )
  55. end
  56. render :json => ticket, :status => :ok
  57. else
  58. render :json => ticket.errors, :status => :unprocessable_entity
  59. end
  60. end
  61. # DELETE /api/v1/tickets/1
  62. def destroy
  63. ticket = Ticket.find( params[:id] )
  64. # permissin check
  65. return if !ticket_permission(ticket)
  66. ticket.destroy
  67. head :ok
  68. end
  69. # GET /api/v1/ticket_customer
  70. # GET /api/v1/tickets_customer
  71. def ticket_customer
  72. # return result
  73. result = Ticket::ScreenOptions.list_by_customer(
  74. :customer_id => params[:customer_id],
  75. :limit => 15,
  76. )
  77. render :json => {
  78. :tickets => result
  79. }
  80. end
  81. # GET /api/v1/ticket_history/1
  82. def ticket_history
  83. # get ticket data
  84. ticket = Ticket.find( params[:id] )
  85. # permissin check
  86. return if !ticket_permission( ticket )
  87. # get history of ticket
  88. history = ticket.history_get(true)
  89. # return result
  90. render :json => history
  91. end
  92. # GET /api/v1/ticket_related/1
  93. def ticket_related
  94. ticket = Ticket.find( params[:ticket_id] )
  95. assets = ticket.assets({})
  96. # open tickets by customer
  97. group_ids = Group.select( 'groups.id' ).joins(:users).
  98. where( 'groups_users.user_id = ?', current_user.id ).
  99. where( 'groups.active = ?', true ).
  100. map( &:id )
  101. access_condition = [ 'group_id IN (?)', group_ids ]
  102. ticket_list = Ticket.where(
  103. :customer_id => ticket.customer_id,
  104. :state_id => Ticket::State.by_category( 'open' )
  105. )
  106. .where(access_condition)
  107. .where( 'id != ?', [ ticket.id ] )
  108. .order('created_at DESC')
  109. .limit(6)
  110. # get related assets
  111. ticket_ids_by_customer = []
  112. ticket_list.each {|ticket|
  113. ticket_ids_by_customer.push ticket.id
  114. assets = ticket.assets(assets)
  115. }
  116. ticket_ids_recent_viewed = []
  117. ticket_recent_view = RecentView.list( current_user, 8, 'Ticket' )
  118. ticket_recent_view.each {|item|
  119. if item['object'] == 'Ticket'
  120. ticket_ids_recent_viewed.push item['o_id']
  121. ticket = Ticket.find( item['o_id'] )
  122. assets = ticket.assets(assets)
  123. end
  124. }
  125. # return result
  126. render :json => {
  127. :assets => assets,
  128. :ticket_ids_by_customer => ticket_ids_by_customer,
  129. :ticket_ids_recent_viewed => ticket_ids_recent_viewed,
  130. }
  131. end
  132. # GET /api/v1/ticket_merge/1/1
  133. def ticket_merge
  134. # check master ticket
  135. ticket_master = Ticket.where( :number => params[:master_ticket_number] ).first
  136. if !ticket_master
  137. render :json => {
  138. :result => 'faild',
  139. :message => 'No such master ticket number!',
  140. }
  141. return
  142. end
  143. # permissin check
  144. return if !ticket_permission(ticket_master)
  145. # check slave ticket
  146. ticket_slave = Ticket.where( :id => params[:slave_ticket_id] ).first
  147. if !ticket_slave
  148. render :json => {
  149. :result => 'faild',
  150. :message => 'No such slave ticket!',
  151. }
  152. return
  153. end
  154. # permissin check
  155. return if !ticket_permission( ticket_slave )
  156. # check diffetent ticket ids
  157. if ticket_slave.id == ticket_master.id
  158. render :json => {
  159. :result => 'faild',
  160. :message => 'Can\'t merge ticket with it self!',
  161. }
  162. return
  163. end
  164. # merge ticket
  165. success = ticket_slave.merge_to(
  166. {
  167. :ticket_id => ticket_master.id,
  168. :created_by_id => current_user.id,
  169. }
  170. )
  171. # return result
  172. render :json => {
  173. :result => 'success',
  174. :master_ticket => ticket_master.attributes,
  175. :slave_ticket => ticket_slave.attributes,
  176. }
  177. end
  178. # GET /api/v1/ticket_full/1
  179. def ticket_full
  180. # permission check
  181. ticket = Ticket.find( params[:id] )
  182. return if !ticket_permission( ticket )
  183. # get signature
  184. signature = {}
  185. if ticket.group.signature
  186. signature = ticket.group.signature.attributes
  187. # replace tags
  188. signature['body'] = NotificationFactory.build(
  189. :locale => current_user.locale,
  190. :string => signature['body'],
  191. :objects => {
  192. :ticket => ticket,
  193. :user => current_user,
  194. }
  195. )
  196. end
  197. # get attributes to update
  198. attributes_to_change = Ticket::ScreenOptions.attributes_to_change( :user => current_user, :ticket => ticket )
  199. # get related users
  200. assets = attributes_to_change[:assets]
  201. assets = ticket.assets(assets)
  202. # get related articles
  203. articles = Ticket::Article.where( :ticket_id => params[:id] )
  204. # get related users
  205. article_ids = []
  206. articles.each {|article|
  207. # ignore internal article if customer is requesting
  208. next if article.internal == true && is_role('Customer')
  209. # load article ids
  210. article_ids.push article.id
  211. # load assets
  212. assets = article.assets(assets)
  213. }
  214. # get links
  215. links = Link.list(
  216. :link_object => 'Ticket',
  217. :link_object_value => ticket.id,
  218. )
  219. link_list = []
  220. links.each { |item|
  221. link_list.push item
  222. if item['link_object'] == 'Ticket'
  223. linked_ticket = Ticket.lookup( :id => item['link_object_value'] )
  224. assets = linked_ticket.assets(assets)
  225. end
  226. }
  227. # get tags
  228. tags = Tag.tag_list(
  229. :object => 'Ticket',
  230. :o_id => ticket.id,
  231. )
  232. # return result
  233. render :json => {
  234. :ticket_id => ticket.id,
  235. :ticket_article_ids => article_ids,
  236. :signature => signature,
  237. :assets => assets,
  238. :links => link_list,
  239. :tags => tags,
  240. :form_meta => attributes_to_change,
  241. :edit_form => attributes_to_change,
  242. }
  243. end
  244. # GET /api/v1/ticket_create/1
  245. def ticket_create
  246. # get attributes to update
  247. attributes_to_change = Ticket::ScreenOptions.attributes_to_change(
  248. :user => current_user,
  249. :ticket_id => params[:ticket_id],
  250. :article_id => params[:article_id]
  251. )
  252. assets = attributes_to_change[:assets]
  253. # split data
  254. split = {}
  255. if params[:ticket_id] && params[:article_id]
  256. ticket = Ticket.find( params[:ticket_id] )
  257. split[:ticket_id] = ticket.id
  258. assets = ticket.assets(assets)
  259. # get related articles
  260. article = Ticket::Article.find( params[:article_id] )
  261. split[:article_id] = article.id
  262. assets = article.assets(assets)
  263. end
  264. # return result
  265. render :json => {
  266. :split => split,
  267. :assets => assets,
  268. :form_meta => {
  269. :filter => attributes_to_change[:filter],
  270. :dependencies => attributes_to_change[:dependencies],
  271. }
  272. }
  273. end
  274. # GET /api/v1/tickets/search
  275. def search
  276. # permit nested conditions
  277. params.require(:condition).permit!
  278. # build result list
  279. tickets = Ticket.search(
  280. :limit => params[:limit],
  281. :query => params[:term],
  282. :condition => params[:condition],
  283. :current_user => current_user,
  284. :detail => params[:detail]
  285. )
  286. assets = {}
  287. ticket_result = []
  288. tickets.each do |ticket|
  289. ticket_result.push ticket.id
  290. assets = ticket.assets(assets)
  291. end
  292. # return result
  293. render :json => {
  294. :tickets => ticket_result,
  295. :tickets_count => tickets.count,
  296. :assets => assets,
  297. }
  298. end
  299. private
  300. def article_create(ticket, params)
  301. puts params.inspect
  302. # create article if given
  303. form_id = params[:form_id]
  304. params.delete(:form_id)
  305. article = Ticket::Article.new( Ticket::Article.param_validation( params ) )
  306. article.ticket_id = ticket.id
  307. # find attachments in upload cache
  308. if form_id
  309. article.attachments = Store.list(
  310. :object => 'UploadCache',
  311. :o_id => form_id,
  312. )
  313. end
  314. if !article.save
  315. render :json => article.errors, :status => :unprocessable_entity
  316. return
  317. end
  318. # remove attachments from upload cache
  319. if form_id
  320. Store.remove(
  321. :object => 'UploadCache',
  322. :o_id => form_id,
  323. )
  324. end
  325. end
  326. end