object_manager.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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', 'User', 'Organization' ] #, 'Group' ]
  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. list of all attributes
  19. result = ObjectManager::Attribute.list_full
  20. result = [
  21. {
  22. :name => 'some name',
  23. :display => '...',
  24. }.
  25. ],
  26. =end
  27. def self.list_full
  28. result = ObjectManager::Attribute.all
  29. attributes = []
  30. assets = {}
  31. result.each {|item|
  32. attribute = item.attributes
  33. attribute[:object] = ObjectLookup.by_id( item.object_lookup_id )
  34. attribute.delete('object_lookup_id')
  35. attributes.push attribute
  36. }
  37. attributes
  38. end
  39. =begin
  40. add a new activity entry for an object
  41. ObjectManager::Attribute.add(
  42. :object => 'Ticket',
  43. :name => 'group_id',
  44. :frontend => 'Group',
  45. :data_type => 'select',
  46. :data_option => {
  47. :relation => 'Group',
  48. :relation_condition => { :access => 'rw' },
  49. :multiple => false,
  50. :null => true,
  51. },
  52. :editable => false,
  53. :active => true,
  54. :screens => {
  55. :create => {
  56. '-all-' => {
  57. :required => true,
  58. },
  59. },
  60. :edit => {
  61. :Agent => {
  62. :required => true,
  63. },
  64. },
  65. },
  66. :pending_migration => false,
  67. :position => 20,
  68. :created_by_id => 1,
  69. :updated_by_id => 1,
  70. :created_at => '2014-06-04 10:00:00',
  71. :updated_at => '2014-06-04 10:00:00',
  72. )
  73. =end
  74. def self.add(data)
  75. # lookups
  76. if data[:object]
  77. data[:object_lookup_id] = ObjectLookup.by_name( data[:object] )
  78. end
  79. data.delete(:object)
  80. # check newest entry - is needed
  81. result = ObjectManager::Attribute.where(
  82. :object_lookup_id => data[:object_lookup_id],
  83. :name => data[:name],
  84. ).first
  85. if result
  86. # raise "ERROR: attribute #{data[:name]} for #{data[:object]} already exists"
  87. return result.update_attributes(data)
  88. end
  89. # create history
  90. ObjectManager::Attribute.create(data)
  91. end
  92. =begin
  93. get user based list of object attributes
  94. attribute_list = ObjectManager::Attribute.by_object('Ticket', user)
  95. returns:
  96. [
  97. { :name => 'api_key', :display => 'API KEY', :tag => 'input', :null => true, :edit => true, :maxlength => 32 },
  98. { :name => 'api_ip_regexp', :display => 'API IP RegExp', :tag => 'input', :null => true, :edit => true },
  99. { :name => 'api_ip_max', :display => 'API IP Max', :tag => 'input', :null => true, :edit => true },
  100. ]
  101. =end
  102. def self.by_object(object, user)
  103. # lookups
  104. if object
  105. object_lookup_id = ObjectLookup.by_name( object )
  106. end
  107. # get attributes in right order
  108. result = ObjectManager::Attribute.where(
  109. :object_lookup_id => object_lookup_id,
  110. :active => true,
  111. ).order('position ASC')
  112. attributes = []
  113. result.each {|item|
  114. data = {
  115. :name => item.name,
  116. :display => item.display,
  117. :tag => item.data_type,
  118. #:null => item.null,
  119. }
  120. if item.screens
  121. data[:screen] = {}
  122. item.screens.each {|screen, roles_options |
  123. data[:screen][screen] = {}
  124. roles_options.each {|role, options|
  125. if role == '-all-'
  126. data[:screen][screen] = options
  127. elsif user && user.is_role(role)
  128. data[:screen][screen] = options
  129. end
  130. }
  131. }
  132. end
  133. if item.data_option
  134. data = data.merge( item.data_option )
  135. end
  136. attributes.push data
  137. }
  138. attributes
  139. end
  140. end