creates_ticket_articles.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module CreatesTicketArticles
  3. extend ActiveSupport::Concern
  4. private
  5. def article_create(ticket, params)
  6. # create article if given
  7. form_id = params[:form_id]
  8. params.delete(:form_id)
  9. subtype = params.delete(:subtype)
  10. # check min. params
  11. raise Exceptions::UnprocessableEntity, __("Need at least an 'article body' field.") if params[:body].blank?
  12. # fill default values
  13. if params[:type_id].blank? && params[:type].blank?
  14. params[:type_id] = Ticket::Article::Type.lookup(name: 'note').id
  15. end
  16. if params[:sender_id].blank? && params[:sender].blank?
  17. sender = 'Customer'
  18. if current_user.permissions?('ticket.agent')
  19. sender = 'Agent'
  20. end
  21. params[:sender_id] = Ticket::Article::Sender.lookup(name: sender).id
  22. end
  23. # remember time accounting
  24. time_unit = params[:time_unit]
  25. clean_params = Ticket::Article.association_name_to_id_convert(params)
  26. clean_params = Ticket::Article.param_cleanup(clean_params, true)
  27. # overwrite params
  28. if !current_user.permissions?('ticket.agent')
  29. clean_params[:sender_id] = Ticket::Article::Sender.lookup(name: 'Customer').id
  30. clean_params.delete(:sender)
  31. clean_params.delete(:origin_by_id)
  32. type = Ticket::Article::Type.lookup(id: clean_params[:type_id])
  33. if !type.name.match?(%r{^(note|web)$})
  34. clean_params[:type_id] = Ticket::Article::Type.lookup(name: 'note').id
  35. end
  36. clean_params.delete(:type)
  37. clean_params[:internal] = false
  38. end
  39. article = Ticket::Article.new(clean_params)
  40. article.ticket_id = ticket.id
  41. # store dataurl images to store
  42. attachments_inline = []
  43. if article.body && article.content_type =~ %r{text/html}i
  44. (article.body, attachments_inline) = HtmlSanitizer.replace_inline_images(article.body, ticket.id)
  45. end
  46. # find attachments in upload cache
  47. attachments = []
  48. if form_id
  49. attachments += UploadCache.new(form_id).attachments
  50. end
  51. # store inline attachments
  52. attachments_inline.each do |attachment|
  53. attachments << {
  54. data: attachment[:data],
  55. filename: attachment[:filename],
  56. preferences: attachment[:preferences],
  57. }
  58. end
  59. # add attachments as param
  60. if params[:attachments].present?
  61. required_keys = %w[mime-type filename data]
  62. preferences_keys = %w[charset mime-type]
  63. params[:attachments].each_with_index do |attachment, index|
  64. # validation
  65. required_keys.each do |key|
  66. next if attachment[key]
  67. raise Exceptions::UnprocessableEntity, "Attachment needs '#{key}' param for attachment with index '#{index}'"
  68. end
  69. preferences = {}
  70. preferences_keys.each do |key|
  71. next if !attachment[key]
  72. store_key = key.tr('-', '_').camelize.gsub(%r{(.+)([A-Z])}, '\1_\2').tr('_', '-')
  73. preferences[store_key] = attachment[key]
  74. end
  75. begin
  76. base64_data = attachment[:data].gsub(%r{[\r\n]}, '')
  77. attachment_data = Base64.strict_decode64(base64_data)
  78. rescue ArgumentError
  79. raise Exceptions::UnprocessableEntity, "Invalid base64 for attachment with index '#{index}'"
  80. end
  81. attachments << {
  82. data: attachment_data,
  83. filename: attachment[:filename],
  84. preferences: preferences,
  85. }
  86. end
  87. end
  88. article.attachments = attachments
  89. # set subtype of present
  90. article.preferences[:subtype] = subtype if subtype.present?
  91. article.save!
  92. # account time
  93. if time_unit.present?
  94. Ticket::TimeAccounting.create!(
  95. ticket_id: article.ticket_id,
  96. ticket_article_id: article.id,
  97. time_unit: time_unit
  98. )
  99. end
  100. return article if form_id.blank?
  101. # clear in-progress state from taskbar
  102. Taskbar
  103. .where(user_id: current_user.id)
  104. .first { |taskbar| taskbar.persisted_form_id == form_id }
  105. &.update!(state: {})
  106. # remove temporary attachment cache
  107. UploadCache.new(form_id).destroy
  108. article
  109. end
  110. end