tag.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Tag < ApplicationModel
  3. include Tag::WritesToTicketHistory
  4. include HasTransactionDispatcher
  5. belongs_to :tag_object, class_name: 'Tag::Object', optional: true
  6. belongs_to :tag_item, class_name: 'Tag::Item', optional: true
  7. =begin
  8. add tags for certain object
  9. Tag.tag_add(
  10. object: 'Ticket',
  11. o_id: ticket.id,
  12. item: 'some tag',
  13. created_by_id: current_user.id,
  14. )
  15. =end
  16. def self.tag_add(data)
  17. data[:item].strip!
  18. # lookups
  19. if data[:object]
  20. tag_object_id = Tag::Object.lookup_by_name_and_create(data[:object]).id
  21. end
  22. if data[:item]
  23. tag_item_id = Tag::Item.lookup_by_name_and_create(data[:item]).id
  24. end
  25. # return if duplicate
  26. current_tags = tag_list(data)
  27. return true if current_tags.include?(data[:item])
  28. # create history
  29. Tag.create(
  30. tag_object_id: tag_object_id,
  31. tag_item_id: tag_item_id,
  32. o_id: data[:o_id],
  33. created_by_id: data[:created_by_id],
  34. sourceable: data[:sourceable],
  35. )
  36. # touch reference
  37. touch_reference_by_params(data)
  38. true
  39. end
  40. =begin
  41. remove tags of certain object
  42. Tag.tag_remove(
  43. object: 'Ticket',
  44. o_id: ticket.id,
  45. item: 'some tag',
  46. created_by_id: current_user.id,
  47. )
  48. or by ids
  49. Tag.tag_remove(
  50. tag_object_id: 123,
  51. o_id: ticket.id,
  52. tag_item_id: 123,
  53. created_by_id: current_user.id,
  54. )
  55. =end
  56. def self.tag_remove(data)
  57. # lookups
  58. if data[:object]
  59. data[:tag_object_id] = Tag::Object.lookup_by_name_and_create(data[:object]).id
  60. else
  61. data[:object] = Tag::Object.lookup(id: data[:tag_object_id]).name
  62. end
  63. if data[:item]
  64. data[:item].strip!
  65. data[:tag_item_id] = Tag::Item.lookup_by_name_and_create(data[:item]).id
  66. end
  67. # create history
  68. result = Tag.where(
  69. tag_object_id: data[:tag_object_id],
  70. tag_item_id: data[:tag_item_id],
  71. o_id: data[:o_id],
  72. )
  73. result.each do |item|
  74. item.sourceable = data[:sourceable]
  75. item.destroy
  76. end
  77. # touch reference
  78. touch_reference_by_params(data)
  79. true
  80. end
  81. =begin
  82. remove all tags of certain object
  83. Tag.tag_destroy(
  84. object: 'Ticket',
  85. o_id: ticket.id,
  86. created_by_id: current_user.id,
  87. )
  88. =end
  89. def self.tag_destroy(data)
  90. # lookups
  91. if data[:object]
  92. data[:tag_object_id] = Tag::Object.lookup_by_name_and_create(data[:object]).id
  93. else
  94. data[:object] = Tag::Object.lookup(id: data[:tag_object_id]).name
  95. end
  96. # create history
  97. result = Tag.where(
  98. tag_object_id: data[:tag_object_id],
  99. o_id: data[:o_id],
  100. )
  101. result.each(&:destroy)
  102. true
  103. end
  104. =begin
  105. update tags for certain object
  106. Tag.tag_update(
  107. object: 'Ticket',
  108. o_id: ticket.id,
  109. items: ['some tag', ['another tag']],
  110. created_by_id: current_user.id,
  111. )
  112. =end
  113. def self.tag_update(object:, o_id:, items:, created_by_id: nil)
  114. given_tags = items.map(&:strip!)
  115. old_tags = tag_list(object: object, o_id: o_id)
  116. tag_object_id = Tag::Object.lookup_by_name_and_create(object).id
  117. added_tags = given_tags - old_tags
  118. removed_tags = old_tags - given_tags
  119. added_tags.each do |tag_name|
  120. tag_item_id = Tag::Item.lookup_by_name_and_create(tag_name).id
  121. Tag.create(
  122. tag_object_id: tag_object_id,
  123. tag_item_id: tag_item_id,
  124. o_id: o_id,
  125. created_by_id: created_by_id,
  126. )
  127. end
  128. if removed_tags.any?
  129. tag_item_ids = removed_tags.map { |tag_name| Tag::Item.lookup_by_name_and_create(tag_name).id }
  130. Tag
  131. .where(
  132. tag_object_id: tag_object_id,
  133. tag_item_id: tag_item_ids,
  134. o_id: o_id,
  135. )
  136. .destroy_all
  137. end
  138. # touch reference
  139. if added_tags.any? || removed_tags.any?
  140. touch_reference_by_params(object: object, o_id: o_id)
  141. end
  142. true
  143. end
  144. =begin
  145. tag list for certain object
  146. tags = Tag.tag_list(
  147. object: 'Ticket',
  148. o_id: ticket.id,
  149. )
  150. returns
  151. ['tag 1', 'tag2', ...]
  152. =end
  153. def self.tag_list(data)
  154. query = Tag.joins(:tag_item, :tag_object)
  155. if data.key?(:object)
  156. query = query.where(tag_objects: { name: data[:object] })
  157. end
  158. if data.key?(:o_id)
  159. query = query.where(o_id: data[:o_id])
  160. end
  161. query.reorder('tags.id, tag_items.name').pluck('tag_items.name')
  162. end
  163. =begin
  164. Lists references to objects with certain tag. Optionally filter by type.
  165. Returns array containing object class name and ID.
  166. @param tag [String] tag to lookup
  167. @param object [String] optional name of the class to search in
  168. @example
  169. references = Tag.tag_references(
  170. tag: 'Tag',
  171. object: 'Ticket'
  172. )
  173. references # [['Ticket', 1], ['Ticket', 4], ...]
  174. @return [Array<Array<String, Integer>>]
  175. =end
  176. def self.tag_references(tag:, object: nil)
  177. tag_item = Tag::Item.find_by name: tag
  178. return [] if tag_item.nil?
  179. output = Tag.where(tag_item: tag_item).joins(:tag_object)
  180. output = output.where(tag_objects: { name: object }) if object.present?
  181. output.pluck(:'tag_objects.name', :o_id)
  182. end
  183. def self.tag_allowed?(name:, user_id: 1)
  184. return true if Setting.get('tag_new').present?
  185. return true if User.lookup(id: user_id).permissions?('admin.tag')
  186. Tag::Item.lookup(name: name).present?
  187. end
  188. end