2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2017-03-09 11:44:51 +00:00
|
|
|
module CreatesTicketArticles
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def article_create(ticket, params)
|
|
|
|
|
|
|
|
# create article if given
|
|
|
|
form_id = params[:form_id]
|
|
|
|
params.delete(:form_id)
|
2018-05-18 10:59:08 +00:00
|
|
|
subtype = params.delete(:subtype)
|
2017-03-09 11:44:51 +00:00
|
|
|
|
|
|
|
# check min. params
|
2021-11-15 15:58:19 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, __('Need at least article: { body: "some text" }') if !params[:body]
|
2017-03-09 11:44:51 +00:00
|
|
|
|
|
|
|
# fill default values
|
2017-09-08 08:28:34 +00:00
|
|
|
if params[:type_id].blank? && params[:type].blank?
|
2017-03-09 11:44:51 +00:00
|
|
|
params[:type_id] = Ticket::Article::Type.lookup(name: 'note').id
|
|
|
|
end
|
2017-09-08 08:28:34 +00:00
|
|
|
if params[:sender_id].blank? && params[:sender].blank?
|
2017-03-09 11:44:51 +00:00
|
|
|
sender = 'Customer'
|
|
|
|
if current_user.permissions?('ticket.agent')
|
|
|
|
sender = 'Agent'
|
|
|
|
end
|
|
|
|
params[:sender_id] = Ticket::Article::Sender.lookup(name: sender).id
|
|
|
|
end
|
|
|
|
|
|
|
|
# remember time accounting
|
|
|
|
time_unit = params[:time_unit]
|
|
|
|
|
|
|
|
clean_params = Ticket::Article.association_name_to_id_convert(params)
|
|
|
|
clean_params = Ticket::Article.param_cleanup(clean_params, true)
|
|
|
|
|
|
|
|
# overwrite params
|
|
|
|
if !current_user.permissions?('ticket.agent')
|
|
|
|
clean_params[:sender_id] = Ticket::Article::Sender.lookup(name: 'Customer').id
|
|
|
|
clean_params.delete(:sender)
|
2017-04-25 14:06:23 +00:00
|
|
|
clean_params.delete(:origin_by_id)
|
2017-03-09 11:44:51 +00:00
|
|
|
type = Ticket::Article::Type.lookup(id: clean_params[:type_id])
|
2021-05-12 11:37:44 +00:00
|
|
|
if !type.name.match?(%r{^(note|web)$})
|
2017-03-09 11:44:51 +00:00
|
|
|
clean_params[:type_id] = Ticket::Article::Type.lookup(name: 'note').id
|
|
|
|
end
|
|
|
|
clean_params.delete(:type)
|
|
|
|
clean_params[:internal] = false
|
|
|
|
end
|
|
|
|
|
|
|
|
article = Ticket::Article.new(clean_params)
|
|
|
|
article.ticket_id = ticket.id
|
|
|
|
|
|
|
|
# store dataurl images to store
|
2017-03-10 05:34:51 +00:00
|
|
|
attachments_inline = []
|
|
|
|
if article.body && article.content_type =~ %r{text/html}i
|
2017-03-17 05:27:50 +00:00
|
|
|
(article.body, attachments_inline) = HtmlSanitizer.replace_inline_images(article.body, ticket.id)
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# find attachments in upload cache
|
2021-06-11 14:50:58 +00:00
|
|
|
attachments = []
|
2017-03-09 11:44:51 +00:00
|
|
|
if form_id
|
2021-06-11 14:50:58 +00:00
|
|
|
attachments += UploadCache.new(form_id).attachments
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
2018-05-18 10:59:08 +00:00
|
|
|
|
2017-03-10 05:34:51 +00:00
|
|
|
# store inline attachments
|
2017-10-01 12:25:52 +00:00
|
|
|
attachments_inline.each do |attachment|
|
2021-06-11 14:50:58 +00:00
|
|
|
attachments << {
|
2018-12-19 17:31:51 +00:00
|
|
|
data: attachment[:data],
|
|
|
|
filename: attachment[:filename],
|
2017-03-10 05:34:51 +00:00
|
|
|
preferences: attachment[:preferences],
|
2021-06-11 14:50:58 +00:00
|
|
|
}
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-03-10 05:34:51 +00:00
|
|
|
|
|
|
|
# add attachments as param
|
2017-09-08 08:28:34 +00:00
|
|
|
if params[:attachments].present?
|
2020-09-30 09:07:01 +00:00
|
|
|
required_keys = %w[mime-type filename data]
|
|
|
|
preferences_keys = %w[charset mime-type]
|
2017-10-01 12:25:52 +00:00
|
|
|
params[:attachments].each_with_index do |attachment, index|
|
2017-03-10 05:34:51 +00:00
|
|
|
|
|
|
|
# validation
|
2020-09-30 09:07:01 +00:00
|
|
|
required_keys.each do |key|
|
2017-03-10 05:34:51 +00:00
|
|
|
next if attachment[key]
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-03-10 05:34:51 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, "Attachment needs '#{key}' param for attachment with index '#{index}'"
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-03-10 05:34:51 +00:00
|
|
|
|
|
|
|
preferences = {}
|
2020-09-30 09:07:01 +00:00
|
|
|
preferences_keys.each do |key|
|
2017-03-10 05:34:51 +00:00
|
|
|
next if !attachment[key]
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2021-05-12 11:37:44 +00:00
|
|
|
store_key = key.tr('-', '_').camelize.gsub(%r{(.+)([A-Z])}, '\1_\2').tr('_', '-')
|
2017-03-10 05:34:51 +00:00
|
|
|
preferences[store_key] = attachment[key]
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-03-10 05:34:51 +00:00
|
|
|
|
2018-07-25 06:26:59 +00:00
|
|
|
begin
|
2021-05-12 11:37:44 +00:00
|
|
|
base64_data = attachment[:data].gsub(%r{[\r\n]}, '')
|
2018-07-25 06:26:59 +00:00
|
|
|
attachment_data = Base64.strict_decode64(base64_data)
|
2019-06-28 11:38:49 +00:00
|
|
|
rescue ArgumentError
|
2017-03-10 05:34:51 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, "Invalid base64 for attachment with index '#{index}'"
|
|
|
|
end
|
|
|
|
|
2021-06-11 14:50:58 +00:00
|
|
|
attachments << {
|
2018-12-19 17:31:51 +00:00
|
|
|
data: attachment_data,
|
|
|
|
filename: attachment[:filename],
|
2017-03-10 05:34:51 +00:00
|
|
|
preferences: preferences,
|
2021-06-11 14:50:58 +00:00
|
|
|
}
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-03-10 05:34:51 +00:00
|
|
|
end
|
|
|
|
|
2021-06-11 14:50:58 +00:00
|
|
|
article.attachments = attachments
|
|
|
|
|
|
|
|
# set subtype of present
|
|
|
|
article.preferences[:subtype] = subtype if subtype.present?
|
|
|
|
|
|
|
|
article.save!
|
|
|
|
|
2017-03-09 11:44:51 +00:00
|
|
|
# account time
|
|
|
|
if time_unit.present?
|
|
|
|
Ticket::TimeAccounting.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
ticket_id: article.ticket_id,
|
2017-03-09 11:44:51 +00:00
|
|
|
ticket_article_id: article.id,
|
2018-12-19 17:31:51 +00:00
|
|
|
time_unit: time_unit
|
2017-03-09 11:44:51 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-09-08 08:28:34 +00:00
|
|
|
return article if form_id.blank?
|
2017-03-09 11:44:51 +00:00
|
|
|
|
2018-05-03 14:09:33 +00:00
|
|
|
# clear in-progress state from taskbar
|
|
|
|
Taskbar
|
|
|
|
.where(user_id: current_user.id)
|
|
|
|
.first { |taskbar| taskbar.persisted_form_id == form_id }
|
|
|
|
&.update!(state: {})
|
|
|
|
|
2019-03-26 07:37:15 +00:00
|
|
|
# remove temporary attachment cache
|
|
|
|
UploadCache.new(form_id).destroy
|
2017-03-09 11:44:51 +00:00
|
|
|
|
|
|
|
article
|
|
|
|
end
|
2017-03-10 05:34:51 +00:00
|
|
|
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|