2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2019-06-04 03:40:48 +00:00
|
|
|
class KnowledgeBase
|
|
|
|
class MenuItemUpdateAction
|
2020-05-13 17:01:42 +00:00
|
|
|
def initialize(kb_locale, location, menu_items_data)
|
2019-06-04 03:40:48 +00:00
|
|
|
@kb_locale = kb_locale
|
2020-05-13 17:01:42 +00:00
|
|
|
@location = location
|
2019-06-04 03:40:48 +00:00
|
|
|
@menu_items_data = menu_items_data
|
|
|
|
end
|
|
|
|
|
2020-05-13 17:01:42 +00:00
|
|
|
def scope
|
|
|
|
@kb_locale.menu_items.location(@location)
|
|
|
|
end
|
|
|
|
|
2019-06-04 03:40:48 +00:00
|
|
|
def perform!
|
2020-08-18 13:01:18 +00:00
|
|
|
raise_unprocessable if !all_ids_present?
|
2019-06-04 03:40:48 +00:00
|
|
|
|
|
|
|
KnowledgeBase::MenuItem.transaction do
|
|
|
|
KnowledgeBase::MenuItem.acts_as_list_no_update do
|
|
|
|
remove_deleted
|
|
|
|
update_order
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-13 17:01:42 +00:00
|
|
|
# Mass-update KB menu items
|
|
|
|
#
|
|
|
|
# @param [KnowledgeBase] knowledge_base
|
|
|
|
# @param [[<Hash>]] params @see .update_location_params!
|
|
|
|
#
|
|
|
|
# @return [<KnowledgeBase::MenuItem>]
|
|
|
|
def self.update_using_params!(knowledge_base, params)
|
|
|
|
return if params.blank?
|
|
|
|
|
|
|
|
params
|
2020-11-24 16:20:57 +00:00
|
|
|
.map { |location_params| update_location_using_params! knowledge_base, location_params }.sum(&:reload)
|
2020-05-13 17:01:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Mass-update KB menu items in a given location
|
|
|
|
#
|
|
|
|
# @param [KnowledgeBase] knowledge_base
|
|
|
|
# @param [Hash] location_params
|
|
|
|
#
|
|
|
|
# @option location_params [Integer] :kb_locale_id
|
|
|
|
# @option location_params [String] :location header or footer
|
|
|
|
# @option location_params [[<Hash>]] :menu_items @see #update_order
|
|
|
|
def self.update_location_using_params!(knowledge_base, location_params)
|
|
|
|
action = new(
|
|
|
|
knowledge_base.kb_locales.find(location_params[:kb_locale_id]),
|
|
|
|
location_params[:location],
|
|
|
|
location_params[:menu_items]
|
|
|
|
)
|
|
|
|
|
|
|
|
action.perform!
|
|
|
|
action.scope
|
|
|
|
end
|
|
|
|
|
2019-06-04 03:40:48 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def update_order
|
2020-05-13 17:01:42 +00:00
|
|
|
old_items = scope.to_a
|
2019-06-04 03:40:48 +00:00
|
|
|
|
|
|
|
@menu_items_data
|
|
|
|
.reject { |elem| elem[:_destroy] }
|
|
|
|
.each_with_index do |data_elem, index|
|
2020-05-13 17:01:42 +00:00
|
|
|
item = old_items.find { |record| record.id == data_elem[:id] } || scope.build
|
2019-06-04 03:40:48 +00:00
|
|
|
|
|
|
|
item.position = index
|
|
|
|
item.title = data_elem[:title]
|
|
|
|
item.url = data_elem[:url]
|
|
|
|
item.new_tab = data_elem[:new_tab]
|
|
|
|
|
|
|
|
item.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_deleted
|
|
|
|
@menu_items_data
|
|
|
|
.select { |elem| elem[:_destroy] }
|
2020-09-30 09:07:01 +00:00
|
|
|
.pluck(:id)
|
|
|
|
.tap { |array| @kb_locale.menu_items.where(id: array).destroy_all }
|
2019-06-04 03:40:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def all_ids_present?
|
2020-05-13 17:01:42 +00:00
|
|
|
old_ids = scope.pluck(:id)
|
2021-06-24 07:05:39 +00:00
|
|
|
new_ids = @menu_items_data.filter_map { |elem| elem[:id]&.to_i }
|
2019-06-04 03:40:48 +00:00
|
|
|
|
|
|
|
old_ids.sort == new_ids.sort
|
|
|
|
end
|
|
|
|
|
|
|
|
def raise_unprocessable
|
2021-11-15 15:58:19 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, __('Provide position of all items in scope')
|
2019-06-04 03:40:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|