2014-02-03 19:23:00 +00:00
|
|
|
# Copyright (C) 2012-2014 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
|
2015-04-27 13:42:53 +00:00
|
|
|
belongs_to :tag_object, class_name: 'Tag::Object'
|
|
|
|
belongs_to :tag_item, class_name: 'Tag::Item'
|
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,
|
|
|
|
tag_item_id: tag_item_id,
|
|
|
|
o_id: data[:o_id],
|
|
|
|
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],
|
|
|
|
tag_item_id: data[:tag_item_id],
|
2015-04-27 13:42:53 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
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)
|
2016-06-08 11:43:07 +00:00
|
|
|
tag_object_id_requested = Tag::Object.lookup(name: data[:object])
|
|
|
|
return [] if !tag_object_id_requested
|
2012-11-18 11:06:55 +00:00
|
|
|
tag_search = Tag.where(
|
2015-04-27 13:42:53 +00:00
|
|
|
tag_object_id: tag_object_id_requested,
|
|
|
|
o_id: data[:o_id],
|
2012-11-18 11:06:55 +00:00
|
|
|
)
|
|
|
|
tags = []
|
2016-06-30 20:04:48 +00:00
|
|
|
tag_search.each { |tag|
|
2016-06-08 11:43:07 +00:00
|
|
|
tag_item = Tag::Item.lookup(id: tag.tag_item_id)
|
|
|
|
next if !tag_item
|
|
|
|
tags.push tag_item.name
|
2012-11-18 11:06:55 +00:00
|
|
|
}
|
2015-04-30 17:20:27 +00:00
|
|
|
tags
|
2012-11-18 11:06:55 +00:00
|
|
|
end
|
|
|
|
|
2016-06-08 11:43:07 +00:00
|
|
|
class Object < ApplicationModel
|
|
|
|
validates :name, presence: true
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2016-06-08 11:43:07 +00:00
|
|
|
=begin
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2016-06-08 11:43:07 +00:00
|
|
|
lookup by name and create tag item
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2016-06-08 11:43:07 +00:00
|
|
|
tag_object = Tag::Object.lookup_by_name_and_create('some tag')
|
2012-11-18 11:09:37 +00:00
|
|
|
|
2016-06-08 11:43:07 +00:00
|
|
|
=end
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2016-06-08 11:43:07 +00:00
|
|
|
def self.lookup_by_name_and_create(name)
|
|
|
|
name.strip!
|
|
|
|
|
|
|
|
tag_object = Tag::Object.lookup(name: name)
|
|
|
|
return tag_object if tag_object
|
|
|
|
|
|
|
|
Tag::Object.create(name: name)
|
|
|
|
end
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2016-06-08 11:43:07 +00:00
|
|
|
class Item < ApplicationModel
|
|
|
|
validates :name, presence: true
|
|
|
|
before_save :fill_namedowncase
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2016-06-08 11:43:07 +00:00
|
|
|
=begin
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2016-06-08 11:43:07 +00:00
|
|
|
lookup by name and create tag item
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2016-06-08 11:43:07 +00:00
|
|
|
tag_item = Tag::Item.lookup_by_name_and_create('some tag')
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2016-06-08 11:43:07 +00:00
|
|
|
=end
|
|
|
|
|
|
|
|
def self.lookup_by_name_and_create(name)
|
|
|
|
name.strip!
|
|
|
|
|
|
|
|
tag_item = Tag::Item.lookup(name: name)
|
|
|
|
return tag_item if tag_item
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2016-06-08 11:43:07 +00:00
|
|
|
Tag::Item.create(name: name)
|
2012-11-18 11:06:55 +00:00
|
|
|
end
|
|
|
|
|
2016-06-08 11:43:07 +00:00
|
|
|
=begin
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2016-06-08 11:43:07 +00:00
|
|
|
rename tag items
|
2012-11-18 11:06:55 +00:00
|
|
|
|
2016-06-08 11:43:07 +00:00
|
|
|
Tag::Item.rename(
|
|
|
|
id: existing_tag_item_to_rename,
|
|
|
|
name: 'new tag item name',
|
|
|
|
updated_by_id: current_user.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.rename(data)
|
|
|
|
|
|
|
|
new_tag_name = data[:name].strip
|
|
|
|
old_tag_item = Tag::Item.find(data[:id])
|
|
|
|
already_existing_tag = Tag::Item.lookup(name: new_tag_name)
|
|
|
|
|
|
|
|
# merge old with new tag if already existing
|
|
|
|
if already_existing_tag
|
|
|
|
|
|
|
|
# re-assign old tag to already existing tag
|
2016-06-30 20:04:48 +00:00
|
|
|
Tag.where(tag_item_id: old_tag_item.id).each { |tag|
|
2016-06-08 11:43:07 +00:00
|
|
|
|
|
|
|
# check if tag already exists on object
|
|
|
|
if Tag.find_by(tag_object_id: tag.tag_object_id, o_id: tag.o_id, tag_item_id: already_existing_tag.id)
|
|
|
|
Tag.tag_remove(
|
|
|
|
tag_object_id: tag.tag_object_id,
|
|
|
|
o_id: tag.o_id,
|
|
|
|
tag_item_id: old_tag_item.id,
|
|
|
|
)
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
# re-assign
|
|
|
|
tag_object = Tag::Object.lookup(id: tag.tag_object_id)
|
|
|
|
tag.tag_item_id = already_existing_tag.id
|
|
|
|
tag.save
|
|
|
|
|
|
|
|
# touch reference objects
|
|
|
|
Tag.touch_reference_by_params(
|
|
|
|
object: tag_object.name,
|
|
|
|
o_id: tag.o_id,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
# delete not longer used tag
|
|
|
|
old_tag_item.destroy
|
|
|
|
|
|
|
|
# update new tag name
|
|
|
|
else
|
|
|
|
|
|
|
|
old_tag_item.name = new_tag_name
|
|
|
|
old_tag_item.save
|
|
|
|
|
|
|
|
# touch reference objects
|
2016-06-30 20:04:48 +00:00
|
|
|
Tag.where(tag_item_id: old_tag_item.id).each { |tag|
|
2016-06-08 11:43:07 +00:00
|
|
|
tag_object = Tag::Object.lookup(id: tag.tag_object_id)
|
|
|
|
Tag.touch_reference_by_params(
|
|
|
|
object: tag_object.name,
|
|
|
|
o_id: tag.o_id,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
remove tag item (destroy with reverences)
|
|
|
|
|
|
|
|
Tag::Item.remove(id)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.remove(id)
|
|
|
|
|
|
|
|
# search for references, destroy and touch
|
2016-06-30 20:04:48 +00:00
|
|
|
Tag.where(tag_item_id: id).each { |tag|
|
2016-06-08 11:43:07 +00:00
|
|
|
tag_object = Tag::Object.lookup(id: tag.tag_object_id)
|
|
|
|
tag.destroy
|
|
|
|
Tag.touch_reference_by_params(
|
|
|
|
object: tag_object.name,
|
|
|
|
o_id: tag.o_id,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Tag::Item.find(id).destroy
|
|
|
|
true
|
|
|
|
end
|
2016-06-02 06:58:10 +00:00
|
|
|
|
|
|
|
def fill_namedowncase
|
|
|
|
self.name_downcase = name.downcase
|
|
|
|
end
|
|
|
|
|
2012-11-18 11:06:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|