subject.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Ticket::Subject
  3. =begin
  4. build new subject with ticket number in there
  5. ticket = Ticket.find(123)
  6. prefix_mode = :reply # :forward, nil
  7. result = ticket.subject_build('some subject', prefix_mode)
  8. returns
  9. result = "[Ticket#1234567] some subject"
  10. =end
  11. def subject_build(subject, prefix_mode = nil)
  12. # clean subject
  13. subject_parts = [subject_clean(subject)]
  14. # add hook
  15. case Setting.get('ticket_hook_position')
  16. when 'left'
  17. subject_parts.unshift subject_build_hook
  18. when 'right'
  19. subject_parts.push subject_build_hook
  20. end
  21. # add prefix
  22. subject_parts
  23. .unshift(subject_build_prefix(prefix_mode))
  24. .compact!
  25. subject_parts.join ' '
  26. end
  27. =begin
  28. clean subject remove ticket number and other not needed chars
  29. ticket = Ticket.find(123)
  30. result = ticket.subject_clean('[Ticket#1234567] some subject')
  31. returns
  32. result = "some subject"
  33. =end
  34. def subject_clean(subject)
  35. return '' if subject.blank?
  36. ticket_hook = Regexp.escape Setting.get('ticket_hook')
  37. ticket_hook_divider = Regexp.escape Setting.get('ticket_hook_divider')
  38. ticket_subject_size = Setting.get('ticket_subject_size')
  39. # remove all possible ticket hook formats with [], () or without any wrapping
  40. [ ['\[', '\]'], ['\(', '\)'], [''] ].each do |wrapping|
  41. subject = subject
  42. .gsub(%r{#{wrapping.first}#{ticket_hook}((: ?)|#{ticket_hook_divider})#{number}#{wrapping.last}(\s+?|)}, '')
  43. end
  44. # remove leading "..:\s" and "..[\d+]:\s" e. g. "Re: " or "Re[5]: "
  45. subject = subject.gsub(%r{^(..(\[\d+\])?:\s)+}, '')
  46. # resize subject based on config
  47. if subject.length > ticket_subject_size.to_i
  48. subject = "#{subject[ 0, ticket_subject_size.to_i ]}[...]"
  49. end
  50. subject.strip!
  51. subject
  52. end
  53. private
  54. def subject_build_hook
  55. ticket_hook = Setting.get('ticket_hook')
  56. ticket_hook_divider = Setting.get('ticket_hook_divider')
  57. "[#{ticket_hook}#{ticket_hook_divider}#{number}]"
  58. end
  59. def subject_build_prefix(prefix_mode)
  60. prefix = case prefix_mode
  61. when 'reply'
  62. Setting.get('ticket_subject_re')
  63. when 'forward'
  64. Setting.get('ticket_subject_fwd')
  65. end
  66. prefix.present? ? "#{prefix}:" : nil
  67. end
  68. end