edit.rb 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class FormUpdater::Updater::Ticket::Edit < FormUpdater::Updater
  3. include FormUpdater::Concerns::AppliesTaskbarState
  4. include FormUpdater::Concerns::ChecksCoreWorkflow
  5. include FormUpdater::Concerns::HasSecurityOptions
  6. include FormUpdater::Concerns::StoresTaskbarState
  7. include FormUpdater::Updater::Ticket::Concerns::HasOwnerId
  8. core_workflow_screen 'edit'
  9. apply_state_group_keys %w[ticket article]
  10. store_state_collect_group_key 'ticket'
  11. store_state_group_keys ['article']
  12. def resolve
  13. set_default_follow_up_state if !meta[:initial]
  14. super
  15. end
  16. def object_type
  17. ::Ticket
  18. end
  19. def handle_updater_flags
  20. flags[:newArticlePresent] = result['articleType'].present? || (!meta.dig(:additional_data, 'applyTaskbarState') && data.dig('article', 'articleType').present?)
  21. end
  22. def after_store_taskbar_preperation(state)
  23. # Remove owner_id when it's the system user and this is also the current ticket value.
  24. if object && state['ticket']&.key?('owner_id') && state.dig('ticket', 'owner_id').nil? && object.owner_id == 1
  25. state['ticket'].delete('owner_id')
  26. end
  27. return if state.dig('article', 'articleType').nil?
  28. state['article']['type'] = state['article'].delete('articleType')
  29. end
  30. private
  31. def article_body_empty?
  32. !data.dig('article', 'body') || data['article']['body'].empty?
  33. end
  34. def state_changed?
  35. data['state_id'] != object.state.id
  36. end
  37. def customer?
  38. return false if current_user.permissions?('ticket.agent') && current_user.groups.access(:read).include?(object.group)
  39. return true if current_user.permissions?('ticket.customer')
  40. false
  41. end
  42. def set_resultant_field_values
  43. # Prevent multiple changes to the default follow-up state.
  44. result['isDefaultFollowUpStateSet'][:value] = true
  45. result['state_id'][:value] = ::Ticket::State.find_by(default_follow_up: true)&.id
  46. end
  47. # Hanlding for customer to change the closed state to open, when a new article will be started.
  48. # Additional handling of reseting state when body will be removed again.
  49. def set_default_follow_up_state
  50. result_initialize_field('state_id')
  51. result_initialize_field('isDefaultFollowUpStateSet')
  52. # Set default state if body is present.
  53. return if article_body_empty?
  54. # And the state was not changed.
  55. return if state_changed?
  56. # And we are in the customer context.
  57. return if !customer?
  58. # And the default state was not set before.
  59. return if data['isDefaultFollowUpStateSet']
  60. # And only if the ticket is not in the default create state (e.g. "new").
  61. return if object.state.default_create
  62. set_resultant_field_values
  63. end
  64. end