link.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
  2. class Link < ApplicationModel
  3. belongs_to :link_type, :class_name => 'Link::Type'
  4. belongs_to :link_object, :class_name => 'Link::Object'
  5. @map = {
  6. 'normal' => 'normal',
  7. 'parent' => 'child',
  8. 'child' => 'parent',
  9. }
  10. =begin
  11. links = Link.list(
  12. :link_object => 'Ticket',
  13. :link_object_value => 1
  14. )
  15. =end
  16. def self.list(data)
  17. linkobject = self.link_object_get( :name => data[:link_object] )
  18. return if !linkobject
  19. items = []
  20. # get links for one site
  21. list = Link.where(
  22. 'link_object_source_id = ? AND link_object_source_value = ?', linkobject.id, data[:link_object_value]
  23. )
  24. list.each { |item|
  25. link = {}
  26. link['link_type'] = @map[ Link::Type.find( item.link_type_id ).name ]
  27. link['link_object'] = Link::Object.find( item.link_object_target_id ).name
  28. link['link_object_value'] = item.link_object_target_value
  29. items.push link
  30. }
  31. # get links for the other site
  32. list = Link.where(
  33. 'link_object_target_id = ? AND link_object_target_value = ?', linkobject.id, data[:link_object_value]
  34. )
  35. list.each { |item|
  36. link = {}
  37. link['link_type'] = Link::Type.find( item.link_type_id ).name
  38. link['link_object'] = Link::Object.find( item.link_object_source_id ).name
  39. link['link_object_value'] = item.link_object_source_value
  40. items.push link
  41. }
  42. return items
  43. end
  44. =begin
  45. Link.add(
  46. :link_type => 'normal',
  47. :link_object_source => 'Ticket',
  48. :link_object_source_value => 6,
  49. :link_object_target => 'Ticket',
  50. :link_object_target_value => 31
  51. )
  52. Link.add(
  53. :link_types_id => 12,
  54. :link_object_source_id => 1,
  55. :link_object_source_value => 1,
  56. :link_object_target_id => 1,
  57. :link_object_target_value => 1
  58. )
  59. =end
  60. def self.add(data)
  61. if data.has_key?(:link_type)
  62. linktype = self.link_type_get( :name => data[:link_type] )
  63. data[:link_type_id] = linktype.id
  64. data.delete( :link_type )
  65. end
  66. if data.has_key?(:link_object_source)
  67. linkobject = self.link_object_get( :name => data[:link_object_source] )
  68. data[:link_object_source_id] = linkobject.id
  69. data.delete( :link_object_source )
  70. end
  71. if data.has_key?(:link_object_target)
  72. linkobject = self.link_object_get( :name => data[:link_object_target] )
  73. data[:link_object_target_id] = linkobject.id
  74. data.delete( :link_object_target )
  75. end
  76. Link.create(data)
  77. end
  78. =begin
  79. Link.remove(
  80. :link_type => 'normal',
  81. :link_object_source => 'Ticket',
  82. :link_object_source_value => 6,
  83. :link_object_target => 'Ticket',
  84. :link_object_target_value => 31
  85. )
  86. =end
  87. def self.remove(data)
  88. if data.has_key?(:link_object_source)
  89. linkobject = self.link_object_get( :name => data[:link_object_source] )
  90. data[:link_object_source_id] = linkobject.id
  91. end
  92. if data.has_key?(:link_object_target)
  93. linkobject = self.link_object_get( :name => data[:link_object_target] )
  94. data[:link_object_target_id] = linkobject.id
  95. end
  96. # from one site
  97. if data.has_key?(:link_type)
  98. linktype = self.link_type_get( :name => data[:link_type] )
  99. data[:link_type_id] = linktype.id
  100. end
  101. links = Link.where(
  102. :link_type_id => data[:link_type_id],
  103. :link_object_source_id => data[:link_object_source_id],
  104. :link_object_source_value => data[:link_object_source_value],
  105. :link_object_target_id => data[:link_object_target_id],
  106. :link_object_target_value => data[:link_object_target_value]
  107. )
  108. links.each { |link|
  109. link.destroy
  110. }
  111. # from the other site
  112. if data.has_key?(:link_type)
  113. linktype = self.link_type_get( :name => @map[ data[:link_type] ] )
  114. data[:link_type_id] = linktype.id
  115. end
  116. links = Link.where(
  117. :link_type_id => data[:link_type_id],
  118. :link_object_target_id => data[:link_object_source_id],
  119. :link_object_target_value => data[:link_object_source_value],
  120. :link_object_source_id => data[:link_object_target_id],
  121. :link_object_source_value => data[:link_object_target_value]
  122. )
  123. links.each { |link|
  124. link.destroy
  125. }
  126. end
  127. private
  128. def self.link_type_get(data)
  129. linktype = Link::Type.where( :name => data[:name] ).first
  130. if !linktype
  131. linktype = Link::Type.create(
  132. :name => data[:name]
  133. )
  134. end
  135. return linktype
  136. end
  137. def self.link_object_get(data)
  138. linkobject = Link::Object.where( :name => data[:name] ).first
  139. if !linkobject
  140. linkobject = Link::Object.create(
  141. :name => data[:name]
  142. )
  143. end
  144. return linkobject
  145. end
  146. end
  147. class Link::Type < ApplicationModel
  148. validates :name, :presence => true
  149. end
  150. class Link::Object < ApplicationModel
  151. validates :name, :presence => true
  152. end