link.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # Copyright (C) 2012-2016 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. after_destroy :touch_link_references
  6. @map = {
  7. 'normal' => 'normal',
  8. 'parent' => 'child',
  9. 'child' => 'parent',
  10. }
  11. =begin
  12. links = Link.list(
  13. link_object: 'Ticket',
  14. link_object_value: 1
  15. )
  16. =end
  17. def self.list(data)
  18. linkobject = link_object_get( name: data[:link_object] )
  19. return if !linkobject
  20. items = []
  21. # get links for one site
  22. list = Link.where(
  23. 'link_object_source_id = ? AND link_object_source_value = ?', linkobject.id, data[:link_object_value]
  24. )
  25. list.each do |item|
  26. link = {}
  27. link['link_type'] = @map[ Link::Type.find( item.link_type_id ).name ]
  28. link['link_object'] = Link::Object.find( item.link_object_target_id ).name
  29. link['link_object_value'] = item.link_object_target_value
  30. items.push link
  31. end
  32. # get links for the other site
  33. list = Link.where(
  34. 'link_object_target_id = ? AND link_object_target_value = ?', linkobject.id, data[:link_object_value]
  35. )
  36. list.each do |item|
  37. link = {}
  38. link['link_type'] = Link::Type.find( item.link_type_id ).name
  39. link['link_object'] = Link::Object.find( item.link_object_source_id ).name
  40. link['link_object_value'] = item.link_object_source_value
  41. items.push link
  42. end
  43. items
  44. end
  45. =begin
  46. Link.add(
  47. link_type: 'normal',
  48. link_object_source: 'Ticket',
  49. link_object_source_value: 6,
  50. link_object_target: 'Ticket',
  51. link_object_target_value: 31
  52. )
  53. Link.add(
  54. link_types_id: 12,
  55. link_object_source_id: 1,
  56. link_object_source_value: 1,
  57. link_object_target_id: 1,
  58. link_object_target_value: 1
  59. )
  60. =end
  61. def self.add(data)
  62. if data.key?(:link_type)
  63. linktype = link_type_get(name: data[:link_type])
  64. data[:link_type_id] = linktype.id
  65. data.delete(:link_type)
  66. end
  67. if data.key?(:link_object_source)
  68. linkobject = link_object_get(name: data[:link_object_source])
  69. data[:link_object_source_id] = linkobject.id
  70. touch_reference_by_params(
  71. object: data[:link_object_source],
  72. o_id: data[:link_object_source_value],
  73. )
  74. data.delete(:link_object_source)
  75. end
  76. if data.key?(:link_object_target)
  77. linkobject = link_object_get(name: data[:link_object_target])
  78. data[:link_object_target_id] = linkobject.id
  79. touch_reference_by_params(
  80. object: data[:link_object_target],
  81. o_id: data[:link_object_target_value],
  82. )
  83. data.delete(:link_object_target)
  84. end
  85. Link.create(data)
  86. end
  87. =begin
  88. Link.remove(
  89. link_type: 'normal',
  90. link_object_source: 'Ticket',
  91. link_object_source_value: 6,
  92. link_object_target: 'Ticket',
  93. link_object_target_value: 31
  94. )
  95. =end
  96. def self.remove(data)
  97. if data.key?(:link_object_source)
  98. linkobject = link_object_get(name: data[:link_object_source])
  99. data[:link_object_source_id] = linkobject.id
  100. end
  101. if data.key?(:link_object_target)
  102. linkobject = link_object_get(name: data[:link_object_target])
  103. data[:link_object_target_id] = linkobject.id
  104. end
  105. # from one site
  106. if data.key?(:link_type)
  107. linktype = link_type_get(name: data[:link_type])
  108. data[:link_type_id] = linktype.id
  109. end
  110. Link.where(
  111. link_type_id: data[:link_type_id],
  112. link_object_source_id: data[:link_object_source_id],
  113. link_object_source_value: data[:link_object_source_value],
  114. link_object_target_id: data[:link_object_target_id],
  115. link_object_target_value: data[:link_object_target_value]
  116. ).destroy_all
  117. # from the other site
  118. if data.key?(:link_type)
  119. linktype = link_type_get(name: @map[ data[:link_type] ])
  120. data[:link_type_id] = linktype.id
  121. end
  122. Link.where(
  123. link_type_id: data[:link_type_id],
  124. link_object_target_id: data[:link_object_source_id],
  125. link_object_target_value: data[:link_object_source_value],
  126. link_object_source_id: data[:link_object_target_id],
  127. link_object_source_value: data[:link_object_target_value]
  128. ).destroy_all
  129. end
  130. =begin
  131. Link.remove_all(
  132. link_object: 'Ticket',
  133. link_object_value: 6,
  134. )
  135. =end
  136. def self.remove_all(data)
  137. if data.key?(:link_object)
  138. linkobject = link_object_get(name: data[:link_object])
  139. data[:link_object_id] = linkobject.id
  140. end
  141. Link.where(
  142. link_object_target_id: data[:link_object_id],
  143. link_object_target_value: data[:link_object_value],
  144. ).destroy_all
  145. Link.where(
  146. link_object_source_id: data[:link_object_id],
  147. link_object_source_value: data[:link_object_value],
  148. ).destroy_all
  149. true
  150. end
  151. def touch_link_references
  152. Link.touch_reference_by_params(
  153. object: Link::Object.lookup(id: link_object_source_id).name,
  154. o_id: link_object_source_value,
  155. )
  156. Link.touch_reference_by_params(
  157. object: Link::Object.lookup(id: link_object_target_id).name,
  158. o_id: link_object_target_value,
  159. )
  160. end
  161. def self.link_type_get(data)
  162. linktype = Link::Type.find_by(name: data[:name])
  163. if !linktype
  164. linktype = Link::Type.create(name: data[:name])
  165. end
  166. linktype
  167. end
  168. def self.link_object_get(data)
  169. linkobject = Link::Object.find_by(name: data[:name])
  170. if !linkobject
  171. linkobject = Link::Object.create(name: data[:name])
  172. end
  173. linkobject
  174. end
  175. end
  176. class Link::Type < ApplicationModel
  177. validates :name, presence: true
  178. end
  179. class Link::Object < ApplicationModel
  180. validates :name, presence: true
  181. end