subject.rb 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. module Ticket::Subject
  3. =begin
  4. build new subject with ticket number in there
  5. ticket = Ticket.find(123)
  6. result = ticket.subject_build('some subject', is_reply_true_false)
  7. returns
  8. result = "[Ticket#1234567] some subject"
  9. =end
  10. def subject_build(subject, is_reply = false)
  11. # clena subject
  12. subject = subject_clean(subject)
  13. ticket_hook = Setting.get('ticket_hook')
  14. ticket_hook_divider = Setting.get('ticket_hook_divider')
  15. ticket_subject_re = Setting.get('ticket_subject_re')
  16. if is_reply && !ticket_subject_re.empty?
  17. subject = "#{ticket_subject_re}: #{subject}"
  18. end
  19. # none position
  20. if Setting.get('ticket_hook_position') == 'none'
  21. return subject
  22. end
  23. # right position
  24. if Setting.get('ticket_hook_position') == 'right'
  25. return subject + " [#{ticket_hook}#{ticket_hook_divider}#{number}]"
  26. end
  27. # left position
  28. "[#{ticket_hook}#{ticket_hook_divider}#{number}] " + subject
  29. end
  30. =begin
  31. clean subject remove ticket number and other not needed chars
  32. ticket = Ticket.find(123)
  33. result = ticket.subject_clean('[Ticket#1234567] some subject')
  34. returns
  35. result = "some subject"
  36. =end
  37. def subject_clean(subject)
  38. ticket_hook = Setting.get('ticket_hook')
  39. ticket_hook_divider = Setting.get('ticket_hook_divider')
  40. ticket_subject_size = Setting.get('ticket_subject_size')
  41. # remove all possible ticket hook formats with []
  42. subject = subject.gsub(/\[#{ticket_hook}: #{number}\](\s+?|)/, '')
  43. subject = subject.gsub(/\[#{ticket_hook}:#{number}\](\s+?|)/, '')
  44. subject = subject.gsub(/\[#{ticket_hook}#{ticket_hook_divider}#{number}\](\s+?|)/, '')
  45. # remove all possible ticket hook formats without []
  46. subject = subject.gsub(/#{ticket_hook}: #{number}(\s+?|)/, '')
  47. subject = subject.gsub(/#{ticket_hook}:#{number}(\s+?|)/, '')
  48. subject = subject.gsub(/#{ticket_hook}#{ticket_hook_divider}#{number}(\s+?|)/, '')
  49. # remove leading "..:\s" and "..[\d+]:\s" e. g. "Re: " or "Re[5]: "
  50. subject = subject.gsub(/^(..(\[\d+\])?:\s)+/, '')
  51. # resize subject based on config
  52. if subject.length > ticket_subject_size.to_i
  53. subject = subject[ 0, ticket_subject_size.to_i ] + '[...]'
  54. end
  55. subject.gsub!(/^[[:space:]]+/, '')
  56. subject.gsub!(/[[:space:]]+$/, '')
  57. subject
  58. end
  59. end