state.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Ticket::State < ApplicationModel
  3. include CanBeImported
  4. include ChecksLatestChangeObserved
  5. belongs_to :state_type, class_name: 'Ticket::StateType', inverse_of: :states
  6. belongs_to :next_state, class_name: 'Ticket::State'
  7. after_create :ensure_defaults
  8. after_update :ensure_defaults
  9. after_destroy :ensure_defaults
  10. validates :name, presence: true
  11. attr_accessor :callback_loop
  12. =begin
  13. looks up states for a given category
  14. states = Ticket::State.by_category(:open) # :open|:closed|:work_on|:work_on_all|:viewable|:viewable_agent_new|:viewable_agent_edit|:viewable_customer_new|:viewable_customer_edit|:pending_reminder|:pending_action|:pending|:merged
  15. returns:
  16. state object list
  17. =end
  18. def self.by_category(category)
  19. case category.to_sym
  20. when :open
  21. state_types = ['new', 'open', 'pending reminder', 'pending action']
  22. when :pending_reminder
  23. state_types = ['pending reminder']
  24. when :pending_action
  25. state_types = ['pending action']
  26. when :pending
  27. state_types = ['pending reminder', 'pending action']
  28. when :work_on
  29. state_types = %w[new open]
  30. when :work_on_all
  31. state_types = ['new', 'open', 'pending reminder']
  32. when :viewable
  33. state_types = ['new', 'open', 'pending reminder', 'pending action', 'closed', 'removed']
  34. when :viewable_agent_new
  35. state_types = ['new', 'open', 'pending reminder', 'pending action', 'closed']
  36. when :viewable_agent_edit
  37. state_types = ['open', 'pending reminder', 'pending action', 'closed']
  38. when :viewable_customer_new
  39. state_types = %w[new closed]
  40. when :viewable_customer_edit
  41. state_types = %w[open closed]
  42. when :closed
  43. state_types = %w[closed]
  44. when :merged
  45. state_types = %w[merged]
  46. end
  47. raise "Unknown category '#{category}'" if state_types.blank?
  48. Ticket::State.where(
  49. state_type_id: Ticket::StateType.where(name: state_types)
  50. )
  51. end
  52. =begin
  53. check if state is ignored for escalation
  54. state = Ticket::State.lookup(name: 'state name')
  55. result = state.ignore_escalation?
  56. returns:
  57. true/false
  58. =end
  59. def ignore_escalation?
  60. return true if ignore_escalation
  61. false
  62. end
  63. def ensure_defaults
  64. return if callback_loop
  65. %w[default_create default_follow_up].each do |default_field|
  66. states_with_default = Ticket::State.where(default_field => true)
  67. next if states_with_default.count == 1
  68. if states_with_default.count.zero?
  69. state = Ticket::State.where(active: true).order(id: :asc).first
  70. state[default_field] = true
  71. state.callback_loop = true
  72. state.save!
  73. next
  74. end
  75. Ticket::State.all.each do |local_state|
  76. next if local_state.id == id
  77. next if local_state[default_field] == false
  78. local_state[default_field] = false
  79. local_state.callback_loop = true
  80. local_state.save!
  81. next
  82. end
  83. end
  84. end
  85. end