20220411082942_issue1573_multi_orga.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Issue1573MultiOrga < ActiveRecord::Migration[6.1]
  3. def change
  4. # return if it's a new setup
  5. return if !Setting.exists?(name: 'system_init_done')
  6. add_user_organization
  7. remove_ticket_organization_readonly
  8. add_organization_to_overview
  9. fix_organization_screens
  10. end
  11. def add_user_organization
  12. UserInfo.current_user_id = 1
  13. ObjectManager::Attribute.add(
  14. force: true,
  15. object: 'User',
  16. name: 'organization_ids',
  17. display: 'Secondary organizations',
  18. data_type: 'autocompletion_ajax',
  19. data_option: {
  20. multiple: true,
  21. nulloption: true,
  22. null: true,
  23. relation: 'Organization',
  24. item_class: 'formGroup--halfSize',
  25. display_limit: 3,
  26. },
  27. editable: false,
  28. active: true,
  29. screens: {
  30. signup: {},
  31. invite_agent: {},
  32. invite_customer: {
  33. '-all-' => {
  34. null: true,
  35. },
  36. },
  37. edit: {
  38. '-all-' => {
  39. null: true,
  40. },
  41. },
  42. create: {
  43. '-all-' => {
  44. null: true,
  45. },
  46. },
  47. view: {
  48. '-all-' => {
  49. shown: true,
  50. },
  51. },
  52. },
  53. to_create: false,
  54. to_migrate: false,
  55. to_delete: false,
  56. position: 901,
  57. )
  58. end
  59. def remove_ticket_organization_readonly
  60. attribute = ObjectManager::Attribute.find_by(name: 'organization_id', object_lookup_id: ObjectLookup.by_name('Ticket'))
  61. attribute.data_type = 'autocompletion_ajax_customer_organization'
  62. attribute.data_option.delete(:readonly)
  63. attribute.data_option[:permission] = ['ticket.agent', 'ticket.customer']
  64. attribute.save!(validate: false)
  65. end
  66. def overview_customer_index(overview, view)
  67. overview.view[view].index('customer') || (overview.view[view].count - 1)
  68. end
  69. def add_organization_to_overview
  70. overview = Overview.find_by(link: 'my_organization_tickets')
  71. return if overview.blank?
  72. %w[d s m].each do |view|
  73. next if overview.view[view].blank?
  74. next if overview.view[view].include?('organization')
  75. idx = overview_customer_index(overview, view)
  76. overview.view[view].insert(idx + 1, 'organization')
  77. end
  78. overview.save!
  79. end
  80. def fix_organization_screens
  81. ObjectManager::Attribute.where(object_lookup_id: ObjectLookup.by_name('Organization'), editable: false).each do |attribute|
  82. customer = false
  83. if attribute.name == 'name'
  84. customer = true
  85. end
  86. attribute.screens[:view] = {
  87. 'ticket.agent' => {
  88. shown: true,
  89. },
  90. 'ticket.customer' => {
  91. shown: customer,
  92. }
  93. }
  94. attribute.save!
  95. end
  96. end
  97. end