2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-11-18 11:06:55 +00:00
|
|
|
class Tag < ApplicationModel
|
2021-03-01 08:18:40 +00:00
|
|
|
include Tag::WritesToTicketHistory
|
2021-05-20 06:59:02 +00:00
|
|
|
include HasTransactionDispatcher
|
2018-05-05 17:03:14 +00:00
|
|
|
|
2019-07-04 11:16:55 +00:00
|
|
|
belongs_to :tag_object, class_name: 'Tag::Object', optional: true
|
|
|
|
belongs_to :tag_item, class_name: 'Tag::Item', optional: true
|
2018-04-12 14:57:37 +00:00
|
|
|
|
|
|
|
# the noop is needed since Layout/EmptyLines detects
|
|
|
|
# the block commend below wrongly as the measurement of
|
|
|
|
# the wanted indentation of the rubocop re-enabling above
|
|
|
|
def noop; end
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2016-06-07 19:22:08 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
add tags for certain object
|
|
|
|
|
|
|
|
Tag.tag_add(
|
|
|
|
object: 'Ticket',
|
|
|
|
o_id: ticket.id,
|
|
|
|
item: 'some tag',
|
|
|
|
created_by_id: current_user.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
def self.tag_add(data)
|
2016-06-08 11:43:07 +00:00
|
|
|
data[:item].strip!
|
2012-11-18 11:06:55 +00:00
|
|
|
|
|
|
|
# lookups
|
|
|
|
if data[:object]
|
2016-06-08 11:43:07 +00:00
|
|
|
tag_object_id = Tag::Object.lookup_by_name_and_create(data[:object]).id
|
2012-11-18 11:06:55 +00:00
|
|
|
end
|
|
|
|
if data[:item]
|
2016-06-08 11:43:07 +00:00
|
|
|
tag_item_id = Tag::Item.lookup_by_name_and_create(data[:item]).id
|
2012-11-18 11:06:55 +00:00
|
|
|
end
|
|
|
|
|
2016-06-08 11:43:07 +00:00
|
|
|
# return if duplicate
|
2016-05-03 00:36:44 +00:00
|
|
|
current_tags = tag_list(data)
|
2016-06-08 11:43:07 +00:00
|
|
|
return true if current_tags.include?(data[:item])
|
2016-05-03 00:36:44 +00:00
|
|
|
|
2012-11-18 11:06:55 +00:00
|
|
|
# create history
|
|
|
|
Tag.create(
|
2015-04-27 13:42:53 +00:00
|
|
|
tag_object_id: tag_object_id,
|
2018-12-19 17:31:51 +00:00
|
|
|
tag_item_id: tag_item_id,
|
|
|
|
o_id: data[:o_id],
|
2015-04-27 13:42:53 +00:00
|
|
|
created_by_id: data[:created_by_id],
|
2012-11-18 11:06:55 +00:00
|
|
|
)
|
2016-06-07 19:22:08 +00:00
|
|
|
|
|
|
|
# touch reference
|
|
|
|
touch_reference_by_params(data)
|
2015-04-30 17:20:27 +00:00
|
|
|
true
|
2012-11-18 11:06:55 +00:00
|
|
|
end
|
|
|
|
|
2016-06-07 19:22:08 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
remove tags of certain object
|
|
|
|
|
2016-06-08 11:43:07 +00:00
|
|
|
Tag.tag_remove(
|
2016-06-07 19:22:08 +00:00
|
|
|
object: 'Ticket',
|
|
|
|
o_id: ticket.id,
|
|
|
|
item: 'some tag',
|
|
|
|
created_by_id: current_user.id,
|
|
|
|
)
|
|
|
|
|
2016-06-08 11:43:07 +00:00
|
|
|
or by ids
|
|
|
|
|
|
|
|
Tag.tag_remove(
|
|
|
|
tag_object_id: 123,
|
|
|
|
o_id: ticket.id,
|
|
|
|
tag_item_id: 123,
|
|
|
|
created_by_id: current_user.id,
|
|
|
|
)
|
|
|
|
|
2016-06-07 19:22:08 +00:00
|
|
|
=end
|
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
def self.tag_remove(data)
|
2012-11-18 11:06:55 +00:00
|
|
|
|
|
|
|
# lookups
|
|
|
|
if data[:object]
|
2016-06-08 11:43:07 +00:00
|
|
|
data[:tag_object_id] = Tag::Object.lookup_by_name_and_create(data[:object]).id
|
|
|
|
else
|
|
|
|
data[:object] = Tag::Object.lookup(id: data[:tag_object_id]).name
|
2012-11-18 11:06:55 +00:00
|
|
|
end
|
|
|
|
if data[:item]
|
2016-06-08 11:43:07 +00:00
|
|
|
data[:item].strip!
|
|
|
|
data[:tag_item_id] = Tag::Item.lookup_by_name_and_create(data[:item]).id
|
2012-11-18 11:06:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# create history
|
|
|
|
result = Tag.where(
|
2016-06-08 11:43:07 +00:00
|
|
|
tag_object_id: data[:tag_object_id],
|
2018-12-19 17:31:51 +00:00
|
|
|
tag_item_id: data[:tag_item_id],
|
|
|
|
o_id: data[:o_id],
|
2012-11-18 11:06:55 +00:00
|
|
|
)
|
2015-05-07 10:25:16 +00:00
|
|
|
result.each(&:destroy)
|
2016-06-07 19:22:08 +00:00
|
|
|
|
|
|
|
# touch reference
|
|
|
|
touch_reference_by_params(data)
|
2015-04-30 17:20:27 +00:00
|
|
|
true
|
2012-11-18 11:06:55 +00:00
|
|
|
end
|
|
|
|
|
2016-06-07 19:22:08 +00:00
|
|
|
=begin
|
|
|
|
|
2017-04-12 07:34:49 +00:00
|
|
|
remove all tags of certain object
|
|
|
|
|
|
|
|
Tag.tag_destroy(
|
|
|
|
object: 'Ticket',
|
|
|
|
o_id: ticket.id,
|
|
|
|
created_by_id: current_user.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.tag_destroy(data)
|
|
|
|
|
|
|
|
# lookups
|
|
|
|
if data[:object]
|
|
|
|
data[:tag_object_id] = Tag::Object.lookup_by_name_and_create(data[:object]).id
|
|
|
|
else
|
|
|
|
data[:object] = Tag::Object.lookup(id: data[:tag_object_id]).name
|
|
|
|
end
|
|
|
|
|
|
|
|
# create history
|
|
|
|
result = Tag.where(
|
|
|
|
tag_object_id: data[:tag_object_id],
|
2018-12-19 17:31:51 +00:00
|
|
|
o_id: data[:o_id],
|
2017-04-12 07:34:49 +00:00
|
|
|
)
|
|
|
|
result.each(&:destroy)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2016-06-07 19:22:08 +00:00
|
|
|
tag list for certain object
|
|
|
|
|
|
|
|
tags = Tag.tag_list(
|
|
|
|
object: 'Ticket',
|
|
|
|
o_id: ticket.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
['tag 1', 'tag2', ...]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-05-03 00:36:44 +00:00
|
|
|
def self.tag_list(data)
|
2019-07-03 16:02:29 +00:00
|
|
|
Tag.joins(:tag_item, :tag_object)
|
|
|
|
.where(o_id: data[:o_id], tag_objects: { name: data[:object] })
|
|
|
|
.order(:id)
|
|
|
|
.pluck('tag_items.name')
|
2012-11-18 11:06:55 +00:00
|
|
|
end
|
2021-08-16 05:57:44 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
Lists references to objects with certain tag. Optionally filter by type.
|
|
|
|
Returns array containing object class name and ID.
|
|
|
|
|
|
|
|
@param tag [String] tag to lookup
|
|
|
|
@param object [String] optional name of the class to search in
|
|
|
|
|
|
|
|
@example
|
|
|
|
|
|
|
|
references = Tag.tag_references(
|
|
|
|
tag: 'Tag',
|
|
|
|
object: 'Ticket'
|
|
|
|
)
|
|
|
|
|
|
|
|
references # [['Ticket', 1], ['Ticket', 4], ...]
|
|
|
|
|
|
|
|
@return [Array<Array<String, Integer>>]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.tag_references(tag:, object: nil)
|
|
|
|
tag_item = Tag::Item.find_by name: tag
|
|
|
|
|
|
|
|
return [] if tag_item.nil?
|
|
|
|
|
|
|
|
output = Tag.where(tag_item: tag_item).joins(:tag_object)
|
|
|
|
|
|
|
|
output = output.where(tag_objects: { name: object }) if object.present?
|
|
|
|
|
|
|
|
output.pluck(:'tag_objects.name', :o_id)
|
|
|
|
end
|
2012-11-18 11:06:55 +00:00
|
|
|
end
|