subject.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Copyright (C) 2012-2014 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')
  7. returns
  8. result = "[Ticket#1234567] some subject"
  9. =end
  10. def subject_build (subject)
  11. # clena subject
  12. subject = self.subject_clean(subject)
  13. ticket_hook = Setting.get('ticket_hook')
  14. ticket_hook_divider = Setting.get('ticket_hook_divider')
  15. # none position
  16. if Setting.get('ticket_hook_position') == 'none'
  17. return subject
  18. end
  19. # right position
  20. if Setting.get('ticket_hook_position') == 'right'
  21. return subject + " [#{ticket_hook}#{ticket_hook_divider}#{self.number}] "
  22. end
  23. # left position
  24. return "[#{ticket_hook}#{ticket_hook_divider}#{self.number}] " + subject
  25. end
  26. =begin
  27. clean subject remove ticket number and other not needed chars
  28. ticket = Ticket.find(123)
  29. result = ticket.subject_clean('[Ticket#1234567] some subject')
  30. returns
  31. result = "some subject"
  32. =end
  33. def subject_clean (subject)
  34. ticket_hook = Setting.get('ticket_hook')
  35. ticket_hook_divider = Setting.get('ticket_hook_divider')
  36. ticket_subject_size = Setting.get('ticket_subject_size')
  37. # remove all possible ticket hook formats with []
  38. subject = subject.gsub /\[#{ticket_hook}: #{self.number}\](\s+?|)/, ''
  39. subject = subject.gsub /\[#{ticket_hook}:#{self.number}\](\s+?|)/, ''
  40. subject = subject.gsub /\[#{ticket_hook}#{ticket_hook_divider}#{self.number}\](\s+?|)/, ''
  41. # remove all possible ticket hook formats without []
  42. subject = subject.gsub /#{ticket_hook}: #{self.number}(\s+?|)/, ''
  43. subject = subject.gsub /#{ticket_hook}:#{self.number}(\s+?|)/, ''
  44. subject = subject.gsub /#{ticket_hook}#{ticket_hook_divider}#{self.number}(\s+?|)/, ''
  45. # remove leading "..:\s" and "..[\d+]:\s" e. g. "Re: " or "Re[5]: "
  46. subject = subject.gsub /^(..(\[\d+\])?:\s)+/, ''
  47. # resize subject based on config
  48. if subject.length > ticket_subject_size.to_i
  49. subject = subject[ 0, ticket_subject_size.to_i ] + '[...]'
  50. end
  51. return subject
  52. end
  53. end