2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
module Ticket::Subject
|
2013-08-17 20:04:57 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
build new subject with ticket number in there
|
|
|
|
|
|
|
|
ticket = Ticket.find(123)
|
2018-05-18 10:59:08 +00:00
|
|
|
prefix_mode = :reply # :forward, nil
|
|
|
|
result = ticket.subject_build('some subject', prefix_mode)
|
2013-08-17 20:04:57 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = "[Ticket#1234567] some subject"
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2018-05-18 10:59:08 +00:00
|
|
|
def subject_build(subject, prefix_mode = nil)
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
# clean subject
|
2018-05-18 10:59:08 +00:00
|
|
|
subject_parts = [subject_clean(subject)]
|
|
|
|
|
|
|
|
# add hook
|
|
|
|
case Setting.get('ticket_hook_position')
|
|
|
|
when 'left'
|
|
|
|
subject_parts.unshift subject_build_hook
|
|
|
|
when 'right'
|
|
|
|
subject_parts.push subject_build_hook
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2018-05-18 10:59:08 +00:00
|
|
|
# add prefix
|
|
|
|
subject_parts
|
|
|
|
.unshift(subject_build_prefix(prefix_mode))
|
|
|
|
.compact!
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2018-05-18 10:59:08 +00:00
|
|
|
subject_parts.join ' '
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
|
|
|
|
2013-08-17 20:04:57 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
clean subject remove ticket number and other not needed chars
|
|
|
|
|
|
|
|
ticket = Ticket.find(123)
|
|
|
|
result = ticket.subject_clean('[Ticket#1234567] some subject')
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = "some subject"
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-05-23 17:56:39 +00:00
|
|
|
def subject_clean(subject)
|
2019-10-08 13:13:27 +00:00
|
|
|
return '' if subject.blank?
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
ticket_hook = Setting.get('ticket_hook')
|
|
|
|
ticket_hook_divider = Setting.get('ticket_hook_divider')
|
|
|
|
ticket_subject_size = Setting.get('ticket_subject_size')
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# remove all possible ticket hook formats with []
|
2021-05-12 11:37:44 +00:00
|
|
|
subject = subject.gsub(%r{\[#{ticket_hook}: #{number}\](\s+?|)}, '')
|
|
|
|
subject = subject.gsub(%r{\[#{ticket_hook}:#{number}\](\s+?|)}, '')
|
|
|
|
subject = subject.gsub(%r{\[#{ticket_hook}#{ticket_hook_divider}#{number}\](\s+?|)}, '')
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# remove all possible ticket hook formats without []
|
2021-05-12 11:37:44 +00:00
|
|
|
subject = subject.gsub(%r{#{ticket_hook}: #{number}(\s+?|)}, '')
|
|
|
|
subject = subject.gsub(%r{#{ticket_hook}:#{number}(\s+?|)}, '')
|
|
|
|
subject = subject.gsub(%r{#{ticket_hook}#{ticket_hook_divider}#{number}(\s+?|)}, '')
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# remove leading "..:\s" and "..[\d+]:\s" e. g. "Re: " or "Re[5]: "
|
2021-05-12 11:37:44 +00:00
|
|
|
subject = subject.gsub(%r{^(..(\[\d+\])?:\s)+}, '')
|
2013-08-17 20:04:57 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# resize subject based on config
|
|
|
|
if subject.length > ticket_subject_size.to_i
|
2020-09-30 09:07:01 +00:00
|
|
|
subject = "#{subject[ 0, ticket_subject_size.to_i ]}[...]"
|
2015-04-27 21:27:51 +00:00
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
|
2017-10-24 20:34:52 +00:00
|
|
|
subject.strip!
|
2015-04-27 23:19:26 +00:00
|
|
|
subject
|
2013-08-17 20:04:57 +00:00
|
|
|
end
|
2018-05-18 10:59:08 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def subject_build_hook
|
|
|
|
ticket_hook = Setting.get('ticket_hook')
|
|
|
|
ticket_hook_divider = Setting.get('ticket_hook_divider')
|
|
|
|
|
|
|
|
"[#{ticket_hook}#{ticket_hook_divider}#{number}]"
|
|
|
|
end
|
|
|
|
|
|
|
|
def subject_build_prefix(prefix_mode)
|
|
|
|
prefix = case prefix_mode
|
|
|
|
when 'reply'
|
|
|
|
Setting.get('ticket_subject_re')
|
|
|
|
when 'forward'
|
|
|
|
Setting.get('ticket_subject_fwd')
|
|
|
|
end
|
|
|
|
|
|
|
|
prefix.present? ? "#{prefix}:" : nil
|
|
|
|
end
|
2014-02-03 19:23:00 +00:00
|
|
|
end
|