2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2019-06-04 03:40:48 +00:00
|
|
|
|
|
|
|
class KnowledgeBase::CategoriesController < KnowledgeBase::BaseController
|
|
|
|
before_action :load_knowledge_base, only: %i[reorder_root_categories reorder_categories reorder_answers]
|
|
|
|
|
|
|
|
def reorder_root_categories
|
2021-07-20 13:31:46 +00:00
|
|
|
reorder_records @knowledge_base.categories.root, params[:ordered_ids], KnowledgeBase::Category
|
2019-06-04 03:40:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def reorder_categories
|
2021-07-20 13:31:46 +00:00
|
|
|
reorder_records @category.children, params[:ordered_ids], KnowledgeBase::Category
|
2019-06-04 03:40:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def reorder_answers
|
2021-07-20 13:31:46 +00:00
|
|
|
reorder_records @category.answers, params[:ordered_ids], KnowledgeBase::Answer
|
2019-06-04 03:40:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-07-20 13:31:46 +00:00
|
|
|
def reorder_records(collection, ids, klass)
|
2019-06-04 03:40:48 +00:00
|
|
|
# Check if ids for models in collection are present
|
|
|
|
all_ids_present = collection.map(&:id).sort == ids.sort
|
2021-11-15 15:58:19 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, __('Provide position of all items in scope') if !all_ids_present
|
2019-06-04 03:40:48 +00:00
|
|
|
|
|
|
|
klass.notify_kb_clients_suspend = true
|
|
|
|
|
|
|
|
klass.acts_as_list_no_update do
|
|
|
|
ids.each_with_index do |id, index|
|
|
|
|
collection
|
|
|
|
.find { |item| item.id == id }
|
|
|
|
.update!(position: index)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
klass.notify_kb_clients_suspend = false
|
|
|
|
|
|
|
|
# it's enough to notify about one updated item
|
|
|
|
collection.first.touch # rubocop:disable Rails/SkipsModelValidations
|
|
|
|
|
|
|
|
assets = ApplicationModel::CanAssets.reduce(collection, {})
|
|
|
|
render json: assets
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_knowledge_base
|
|
|
|
@knowledge_base = KnowledgeBase.find params[:knowledge_base_id]
|
|
|
|
@category = @knowledge_base.categories.find params[:id] if params.key? :id
|
|
|
|
end
|
|
|
|
end
|