2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2013-01-04 18:42:20 +00:00
|
|
|
class Link < ApplicationModel
|
2018-05-05 17:03:14 +00:00
|
|
|
|
2018-04-12 14:57:37 +00:00
|
|
|
belongs_to :link_type, class_name: 'Link::Type'
|
|
|
|
belongs_to :link_object, class_name: 'Link::Object'
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2017-05-05 09:16:47 +00:00
|
|
|
after_destroy :touch_link_references
|
|
|
|
|
2012-08-23 22:08:27 +00:00
|
|
|
@map = {
|
|
|
|
'normal' => 'normal',
|
|
|
|
'parent' => 'child',
|
|
|
|
'child' => 'parent',
|
|
|
|
}
|
2012-07-15 22:12:06 +00:00
|
|
|
|
|
|
|
=begin
|
2012-08-20 13:21:15 +00:00
|
|
|
|
2012-11-07 23:47:05 +00:00
|
|
|
links = Link.list(
|
2016-05-19 08:20:38 +00:00
|
|
|
link_object: 'Ticket',
|
|
|
|
link_object_value: 1
|
2012-08-20 13:21:15 +00:00
|
|
|
)
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-07-15 22:12:06 +00:00
|
|
|
=end
|
|
|
|
|
2012-08-21 10:28:41 +00:00
|
|
|
def self.list(data)
|
2015-05-07 12:10:38 +00:00
|
|
|
linkobject = link_object_get( name: data[:link_object] )
|
2012-08-21 10:28:41 +00:00
|
|
|
return if !linkobject
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2012-08-23 22:08:27 +00:00
|
|
|
items = []
|
|
|
|
|
|
|
|
# get links for one site
|
2012-08-21 10:28:41 +00:00
|
|
|
list = Link.where(
|
|
|
|
'link_object_source_id = ? AND link_object_source_value = ?', linkobject.id, data[:link_object_value]
|
|
|
|
)
|
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
list.each do |item|
|
2012-08-21 10:28:41 +00:00
|
|
|
link = {}
|
2012-08-23 22:08:27 +00:00
|
|
|
link['link_type'] = @map[ Link::Type.find( item.link_type_id ).name ]
|
2012-08-21 10:28:41 +00:00
|
|
|
link['link_object'] = Link::Object.find( item.link_object_target_id ).name
|
|
|
|
link['link_object_value'] = item.link_object_target_value
|
|
|
|
items.push link
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2012-08-21 10:28:41 +00:00
|
|
|
|
2012-08-23 22:08:27 +00:00
|
|
|
# get links for the other site
|
2012-08-21 10:28:41 +00:00
|
|
|
list = Link.where(
|
|
|
|
'link_object_target_id = ? AND link_object_target_value = ?', linkobject.id, data[:link_object_value]
|
|
|
|
)
|
2017-10-01 12:25:52 +00:00
|
|
|
list.each do |item|
|
2012-08-21 10:28:41 +00:00
|
|
|
link = {}
|
|
|
|
link['link_type'] = Link::Type.find( item.link_type_id ).name
|
|
|
|
link['link_object'] = Link::Object.find( item.link_object_source_id ).name
|
|
|
|
link['link_object_value'] = item.link_object_source_value
|
|
|
|
items.push link
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2012-08-21 10:28:41 +00:00
|
|
|
|
2015-04-30 17:20:27 +00:00
|
|
|
items
|
2012-08-21 10:28:41 +00:00
|
|
|
end
|
2012-07-15 22:12:06 +00:00
|
|
|
|
2012-08-23 22:08:27 +00:00
|
|
|
=begin
|
2012-07-15 22:12:06 +00:00
|
|
|
|
2012-08-23 22:08:27 +00:00
|
|
|
Link.add(
|
2016-05-19 08:20:38 +00:00
|
|
|
link_type: 'normal',
|
|
|
|
link_object_source: 'Ticket',
|
|
|
|
link_object_source_value: 6,
|
|
|
|
link_object_target: 'Ticket',
|
|
|
|
link_object_target_value: 31
|
2012-08-23 22:08:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
Link.add(
|
2016-05-19 08:20:38 +00:00
|
|
|
link_types_id: 12,
|
|
|
|
link_object_source_id: 1,
|
|
|
|
link_object_source_value: 1,
|
|
|
|
link_object_target_id: 1,
|
|
|
|
link_object_target_value: 1
|
2013-06-12 15:59:58 +00:00
|
|
|
)
|
2012-08-23 22:08:27 +00:00
|
|
|
|
|
|
|
=end
|
2012-08-20 13:21:15 +00:00
|
|
|
|
|
|
|
def self.add(data)
|
2012-07-15 22:12:06 +00:00
|
|
|
|
2015-04-30 17:34:33 +00:00
|
|
|
if data.key?(:link_type)
|
2016-06-07 19:22:08 +00:00
|
|
|
linktype = link_type_get(name: data[:link_type])
|
2012-08-21 10:28:41 +00:00
|
|
|
data[:link_type_id] = linktype.id
|
2016-06-07 19:22:08 +00:00
|
|
|
data.delete(:link_type)
|
2012-08-21 10:28:41 +00:00
|
|
|
end
|
|
|
|
|
2015-04-30 17:34:33 +00:00
|
|
|
if data.key?(:link_object_source)
|
2016-06-07 19:22:08 +00:00
|
|
|
linkobject = link_object_get(name: data[:link_object_source])
|
2012-08-21 10:28:41 +00:00
|
|
|
data[:link_object_source_id] = linkobject.id
|
2016-06-07 19:22:08 +00:00
|
|
|
touch_reference_by_params(
|
|
|
|
object: data[:link_object_source],
|
2018-12-19 17:31:51 +00:00
|
|
|
o_id: data[:link_object_source_value],
|
2016-06-07 19:22:08 +00:00
|
|
|
)
|
|
|
|
data.delete(:link_object_source)
|
2012-07-15 22:12:06 +00:00
|
|
|
end
|
2012-08-21 10:28:41 +00:00
|
|
|
|
2015-04-30 17:34:33 +00:00
|
|
|
if data.key?(:link_object_target)
|
2016-06-07 19:22:08 +00:00
|
|
|
linkobject = link_object_get(name: data[:link_object_target])
|
2012-08-21 10:28:41 +00:00
|
|
|
data[:link_object_target_id] = linkobject.id
|
2016-06-07 19:22:08 +00:00
|
|
|
touch_reference_by_params(
|
|
|
|
object: data[:link_object_target],
|
2018-12-19 17:31:51 +00:00
|
|
|
o_id: data[:link_object_target_value],
|
2016-06-07 19:22:08 +00:00
|
|
|
)
|
|
|
|
data.delete(:link_object_target)
|
2012-08-20 13:21:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Link.create(data)
|
|
|
|
end
|
|
|
|
|
2012-08-23 22:08:27 +00:00
|
|
|
=begin
|
2012-08-20 13:21:15 +00:00
|
|
|
|
2012-08-23 22:08:27 +00:00
|
|
|
Link.remove(
|
2016-05-19 08:20:38 +00:00
|
|
|
link_type: 'normal',
|
|
|
|
link_object_source: 'Ticket',
|
|
|
|
link_object_source_value: 6,
|
|
|
|
link_object_target: 'Ticket',
|
|
|
|
link_object_target_value: 31
|
2013-06-12 15:59:58 +00:00
|
|
|
)
|
2012-08-23 22:08:27 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.remove(data)
|
2015-04-30 17:34:33 +00:00
|
|
|
if data.key?(:link_object_source)
|
2016-06-07 19:22:08 +00:00
|
|
|
linkobject = link_object_get(name: data[:link_object_source])
|
2012-08-23 22:08:27 +00:00
|
|
|
data[:link_object_source_id] = linkobject.id
|
|
|
|
end
|
|
|
|
|
2015-04-30 17:34:33 +00:00
|
|
|
if data.key?(:link_object_target)
|
2016-06-07 19:22:08 +00:00
|
|
|
linkobject = link_object_get(name: data[:link_object_target])
|
2012-08-23 22:08:27 +00:00
|
|
|
data[:link_object_target_id] = linkobject.id
|
|
|
|
end
|
2012-08-20 13:21:15 +00:00
|
|
|
|
2012-08-23 22:08:27 +00:00
|
|
|
# from one site
|
2015-04-30 17:34:33 +00:00
|
|
|
if data.key?(:link_type)
|
2016-06-07 19:22:08 +00:00
|
|
|
linktype = link_type_get(name: data[:link_type])
|
2012-08-23 22:08:27 +00:00
|
|
|
data[:link_type_id] = linktype.id
|
|
|
|
end
|
2017-05-05 09:16:47 +00:00
|
|
|
Link.where(
|
2018-12-19 17:31:51 +00:00
|
|
|
link_type_id: data[:link_type_id],
|
|
|
|
link_object_source_id: data[:link_object_source_id],
|
2015-04-27 13:42:53 +00:00
|
|
|
link_object_source_value: data[:link_object_source_value],
|
2018-12-19 17:31:51 +00:00
|
|
|
link_object_target_id: data[:link_object_target_id],
|
2015-04-27 13:42:53 +00:00
|
|
|
link_object_target_value: data[:link_object_target_value]
|
2017-05-05 09:16:47 +00:00
|
|
|
).destroy_all
|
2012-08-23 22:08:27 +00:00
|
|
|
|
|
|
|
# from the other site
|
2015-04-30 17:34:33 +00:00
|
|
|
if data.key?(:link_type)
|
2016-06-07 19:22:08 +00:00
|
|
|
linktype = link_type_get(name: @map[ data[:link_type] ])
|
2012-08-23 22:08:27 +00:00
|
|
|
data[:link_type_id] = linktype.id
|
|
|
|
end
|
2017-05-05 09:16:47 +00:00
|
|
|
|
|
|
|
Link.where(
|
2018-12-19 17:31:51 +00:00
|
|
|
link_type_id: data[:link_type_id],
|
|
|
|
link_object_target_id: data[:link_object_source_id],
|
2015-04-27 13:42:53 +00:00
|
|
|
link_object_target_value: data[:link_object_source_value],
|
2018-12-19 17:31:51 +00:00
|
|
|
link_object_source_id: data[:link_object_target_id],
|
2015-04-27 13:42:53 +00:00
|
|
|
link_object_source_value: data[:link_object_target_value]
|
2017-05-05 09:16:47 +00:00
|
|
|
).destroy_all
|
|
|
|
end
|
2017-05-05 09:16:26 +00:00
|
|
|
|
2017-05-05 09:16:47 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
Link.remove_all(
|
|
|
|
link_object: 'Ticket',
|
|
|
|
link_object_value: 6,
|
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.remove_all(data)
|
|
|
|
if data.key?(:link_object)
|
|
|
|
linkobject = link_object_get(name: data[:link_object])
|
|
|
|
data[:link_object_id] = linkobject.id
|
|
|
|
end
|
|
|
|
|
|
|
|
Link.where(
|
2018-12-19 17:31:51 +00:00
|
|
|
link_object_target_id: data[:link_object_id],
|
2017-05-05 09:16:47 +00:00
|
|
|
link_object_target_value: data[:link_object_value],
|
|
|
|
).destroy_all
|
|
|
|
Link.where(
|
2018-12-19 17:31:51 +00:00
|
|
|
link_object_source_id: data[:link_object_id],
|
2017-05-05 09:16:47 +00:00
|
|
|
link_object_source_value: data[:link_object_value],
|
|
|
|
).destroy_all
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def touch_link_references
|
|
|
|
Link.touch_reference_by_params(
|
|
|
|
object: Link::Object.lookup(id: link_object_source_id).name,
|
2018-12-19 17:31:51 +00:00
|
|
|
o_id: link_object_source_value,
|
2017-05-05 09:16:47 +00:00
|
|
|
)
|
|
|
|
Link.touch_reference_by_params(
|
|
|
|
object: Link::Object.lookup(id: link_object_target_id).name,
|
2018-12-19 17:31:51 +00:00
|
|
|
o_id: link_object_target_value,
|
2017-05-05 09:16:47 +00:00
|
|
|
)
|
2012-08-20 13:21:15 +00:00
|
|
|
end
|
2012-07-15 22:12:06 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
def self.link_type_get(data)
|
2016-06-07 19:22:08 +00:00
|
|
|
linktype = Link::Type.find_by(name: data[:name])
|
2013-06-12 15:59:58 +00:00
|
|
|
if !linktype
|
2016-06-07 19:22:08 +00:00
|
|
|
linktype = Link::Type.create(name: data[:name])
|
2012-08-21 10:28:41 +00:00
|
|
|
end
|
2015-04-30 17:20:27 +00:00
|
|
|
linktype
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2012-08-21 10:28:41 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
def self.link_object_get(data)
|
2016-06-07 19:22:08 +00:00
|
|
|
linkobject = Link::Object.find_by(name: data[:name])
|
2013-06-12 15:59:58 +00:00
|
|
|
if !linkobject
|
2016-06-07 19:22:08 +00:00
|
|
|
linkobject = Link::Object.create(name: data[:name])
|
2012-08-21 10:28:41 +00:00
|
|
|
end
|
2015-04-30 17:20:27 +00:00
|
|
|
linkobject
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2012-08-21 10:28:41 +00:00
|
|
|
|
2019-06-04 03:40:48 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
Update assets according to given references list
|
|
|
|
|
|
|
|
@param assets [Hash] hash with assets
|
|
|
|
@param link_references [Array<Hash>] @see #list
|
|
|
|
@return [Hash] assets including linked items
|
|
|
|
|
|
|
|
@example Link.reduce_assets(assets, link_references)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.reduce_assets(assets, link_references)
|
|
|
|
link_items = link_references
|
|
|
|
.map { |elem| lookup_linked_object(elem) }
|
|
|
|
.compact
|
|
|
|
|
|
|
|
ApplicationModel::CanAssets.reduce(link_items, assets)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.lookup_linked_object(elem)
|
|
|
|
klass = elem['link_object'].safe_constantize
|
|
|
|
id = elem['link_object_value']
|
|
|
|
|
|
|
|
case klass.to_s
|
|
|
|
when KnowledgeBase::Answer::Translation.to_s
|
|
|
|
Setting.get('kb_active') ? klass.lookup(id: id) : nil
|
|
|
|
else
|
|
|
|
klass&.lookup(id: id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-15 22:12:06 +00:00
|
|
|
end
|
|
|
|
|
2013-01-04 18:42:20 +00:00
|
|
|
class Link::Type < ApplicationModel
|
2015-04-27 13:42:53 +00:00
|
|
|
validates :name, presence: true
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-07-15 22:12:06 +00:00
|
|
|
|
2013-01-04 18:42:20 +00:00
|
|
|
class Link::Object < ApplicationModel
|
2015-04-27 13:42:53 +00:00
|
|
|
validates :name, presence: true
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|