state.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Ticket::State < ApplicationModel
  3. include HasDefaultModelUserRelations
  4. include CanBeImported
  5. include ChecksHtmlSanitized
  6. include HasCollectionUpdate
  7. include HasSearchIndexBackend
  8. belongs_to :state_type, class_name: 'Ticket::StateType', inverse_of: :states, optional: true
  9. belongs_to :next_state, class_name: 'Ticket::State', optional: true
  10. after_create :ensure_defaults
  11. after_update :ensure_defaults
  12. after_destroy :ensure_defaults
  13. after_destroy :update_object_manager_attribute
  14. after_save :update_object_manager_attribute
  15. validates :name, presence: true, uniqueness: { case_sensitive: false }
  16. validates :note, length: { maximum: 250 }
  17. sanitized_html :note
  18. attr_accessor :callback_loop
  19. =begin
  20. looks up states for a given category
  21. 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
  22. returns:
  23. state object list
  24. =end
  25. scope :by_category, lambda { |category|
  26. joins(:state_type)
  27. .where(ticket_state_types: { name: Ticket::StateType.names_in_category(category) })
  28. }
  29. def self.by_category_ids(category)
  30. by_category(category).pluck(:id)
  31. end
  32. def ensure_defaults
  33. return if callback_loop
  34. %w[default_create default_follow_up].each do |default_field|
  35. states_with_default = Ticket::State.where(default_field => true)
  36. next if states_with_default.count == 1
  37. if states_with_default.count.zero?
  38. state = Ticket::State.where(active: true).reorder(id: :asc).first
  39. state[default_field] = true
  40. state.callback_loop = true
  41. state.save!
  42. next
  43. end
  44. Ticket::State.all.each do |local_state|
  45. next if local_state.id == id
  46. next if local_state[default_field] == false
  47. local_state[default_field] = false
  48. local_state.callback_loop = true
  49. local_state.save!
  50. next
  51. end
  52. end
  53. end
  54. def self.update_state_field_configuration
  55. attr = ObjectManager::Attribute.get(
  56. object: 'Ticket',
  57. name: 'state_id',
  58. )
  59. active_states = Ticket::State.where(active: true)
  60. attr.data_option[:filter] = active_states.by_category_ids(:viewable)
  61. attr.screens[:create_middle]['ticket.agent'][:filter] = active_states.by_category_ids(:viewable_agent_new)
  62. attr.screens[:create_middle]['ticket.customer'][:filter] = active_states.by_category_ids(:viewable_customer_new)
  63. attr.screens[:edit]['ticket.agent'][:filter] = active_states.by_category_ids(:viewable_agent_edit)
  64. attr.screens[:edit]['ticket.customer'][:filter] = active_states.by_category_ids(:viewable_customer_edit)
  65. attr.screens[:overview_bulk]['ticket.agent'][:filter] = active_states.by_category_ids(:viewable_agent_edit)
  66. attr.save!
  67. end
  68. private
  69. def update_object_manager_attribute
  70. return if !Setting.get('system_init_done')
  71. return if callback_loop
  72. self.class.update_state_field_configuration
  73. end
  74. end