edit.rb 3.1 KB

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