object_manager.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class ObjectManager
  3. =begin
  4. add a new activity entry for an object
  5. ObjectManager.listObjects()
  6. =end
  7. def self.listObjects
  8. ['Ticket', 'TicketArticle', 'Group', 'Organization', 'User']
  9. end
  10. end
  11. class ObjectManager::Attribute < ApplicationModel
  12. self.table_name = 'object_manager_attributes'
  13. belongs_to :object_lookup, :class_name => 'ObjectLookup'
  14. validates :name, :presence => true
  15. store :screens
  16. store :data_option
  17. =begin
  18. add a new activity entry for an object
  19. ObjectManager::Attribute.add(
  20. :object => 'Ticket',
  21. :name => 'group_id',
  22. :frontend => 'Group',
  23. :data_type => 'select',
  24. :data_option => {
  25. :relation => 'Group',
  26. :relation_condition => { :access => 'rw' },
  27. :multiple => false,
  28. :null => true,
  29. },
  30. :editable => false,
  31. :active => true,
  32. :screens => {
  33. :create => {
  34. '-all-' => {
  35. :required => true,
  36. },
  37. },
  38. :edit => {
  39. :Agent => {
  40. :required => true,
  41. },
  42. },
  43. },
  44. :pending_migration => false,
  45. :position => 20,
  46. :created_by_id => 1,
  47. :updated_by_id => 1,
  48. :created_at => '2014-06-04 10:00:00',
  49. :updated_at => '2014-06-04 10:00:00',
  50. )
  51. =end
  52. def self.add(data)
  53. # lookups
  54. if data[:object]
  55. data[:object_lookup_id] = ObjectLookup.by_name( data[:object] )
  56. end
  57. data.delete(:object)
  58. # check newest entry - is needed
  59. result = ObjectManager::Attribute.where(
  60. :object_lookup_id => data[:object_lookup_id],
  61. :name => data[:name],
  62. ).first
  63. if result
  64. # raise "ERROR: attribute #{data[:name]} for #{data[:object]} already exists"
  65. return result.update_attributes(data)
  66. end
  67. # create history
  68. ObjectManager::Attribute.create(data)
  69. end
  70. =begin
  71. get list of object attributes
  72. attribute_list = ObjectManager::Attribute.by_object('Ticket', user)
  73. returns:
  74. [
  75. { :name => 'api_key', :display => 'API KEY', :tag => 'input', :null => true, :edit => true, :maxlength => 32 },
  76. { :name => 'api_ip_regexp', :display => 'API IP RegExp', :tag => 'input', :null => true, :edit => true },
  77. { :name => 'api_ip_max', :display => 'API IP Max', :tag => 'input', :null => true, :edit => true },
  78. ]
  79. =end
  80. def self.by_object(object, user)
  81. # lookups
  82. if object
  83. object_lookup_id = ObjectLookup.by_name( object )
  84. end
  85. # get attributes in right order
  86. result = ObjectManager::Attribute.where(
  87. :object_lookup_id => object_lookup_id,
  88. :active => true,
  89. ).order('position ASC')
  90. attributes = []
  91. result.each {|item|
  92. data = {
  93. :name => item.name,
  94. :display => item.display,
  95. :tag => item.data_type,
  96. #:null => item.null,
  97. }
  98. if item.screens
  99. data[:screen] = {}
  100. item.screens.each {|screen, roles_options |
  101. data[:screen][screen] = {}
  102. roles_options.each {|role, options|
  103. if role == '-all-'
  104. data[:screen][screen] = options
  105. elsif user && user.is_role(role)
  106. data[:screen][screen] = options
  107. end
  108. }
  109. }
  110. end
  111. if item.data_option
  112. data = data.merge( item.data_option )
  113. end
  114. attributes.push data
  115. }
  116. attributes
  117. end
  118. end