state.rb 2.9 KB

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