# -*- encoding : utf-8 -*- module KirguduBase module Controllers module ContainerItemsManagementActions def self.included(base) base.class_eval do include InstanceMethods end end module InstanceMethods def management_items_index management_settings = self.class.get_kb_option(:management_settings) filters_4_container = {} self.fill_obligatory_filters(filters_4_container) local_data = {} local_data[:management_settings] = management_settings local_container = management_settings.container_class.get_entry_by_id(params[management_settings.container_id_property], filters_4_container, {logger: string_logger}) local_data[:container] = local_container filters_4_items = {} self.fill_obligatory_filters(filters_4_items) filters_4_items[management_settings.container_id_property] = params[management_settings.container_id_property] local_items = management_settings.item_class.get_entries(nil, filters_4_items, {logger: string_logger}) local_data[:items] = local_items respond_to do |format| format.html { render_html1(self.class.get_method_latest_result(:get_kb_view_path, :index), locals: { data: local_data}) } format.json { render json: local_data } end end def management_items_manage local_data = {} # gettting management settings management_settings = self.class.get_kb_option(:management_settings) local_data[:management_settings] = management_settings #loading container data filters_4_container = {} self.fill_obligatory_filters(filters_4_container) local_container = management_settings.container_class.get_entry_by_id(params[management_settings.container_id_property], filters_4_container, {logger: string_logger}) local_data[:container] = local_container #loading items data filters_4_items = {} self.fill_obligatory_filters(filters_4_items) filters_4_items[management_settings.container_id_property] = params[management_settings.container_id_property] filters_4_items[:per_page] = 999999 filters_4_items[:page] = 1 local_items = management_settings.item_class.get_entries(nil, filters_4_items, {logger: string_logger}) local_data[:items] = local_items #entries_order_by = get_managing_entries_order_by if self.respond_to?(:get_managing_entries_order_by) #entries_order_by = "name asc" unless entries_order_by filters_4_entries = {} self.fill_obligatory_filters(filters_4_entries) filters_4_entries[management_settings.entry_id_property] = params[management_settings.entry_id_property] filters_4_entries[:per_page] = 999999 filters_4_entries[:page] = 1 # query to exclude already added entries local_entries_initial_query = management_settings.entry_class.where { -(id.in management_settings.item_class.get_entries(nil, filters_4_items, {logger: string_logger}).select(management_settings.entry_id_property)) } local_entries = management_settings.entry_class.get_entries(local_entries_initial_query, filters_4_entries, {logger: string_logger}) local_data[:entries] = local_entries # fill parent for data filters_for_parents = {} self.fill_obligatory_filters(filters_for_parents) self.fill_parents_for_local_data(local_data, filters_for_parents) respond_to do |format| format.html { render_html1 self.class.get_method_latest_result(:get_kb_view_path, :management), locals: { data: local_data} } format.json { render json: @entries } end end def management_items_add local_data = {} # getting management settings management_settings = self.class.get_kb_option(:management_settings) local_data[:management_settings] = management_settings #loading container data filters_4_container = {} self.fill_obligatory_filters(filters_4_container) local_container = management_settings.container_class.get_entry_by_id(params[management_settings.container_id_property], filters_4_container, {logger: string_logger}) #loading entry data filters_4_entries = {} self.fill_obligatory_filters(filters_4_entries) local_entry = management_settings.entry_class.get_entry_by_id(params[management_settings.entry_id_property], filters_4_entries, {logger: string_logger}) #checking if data found and valid unless local_container render json: {status: ::ChupakabraTools::SetManagementResult.id_by_tag('set_not_found'), message: I18n.t("#{self.to_i18n}.management.messages.set_not_found", id: params[:id])} return end unless local_entry render json: {status: ::ChupakabraTools::SetManagementResult.id_by_tag('entry_not_found'), message: I18n.t("#{self.to_i18n}.management.messages.entry_not_found", entry_id: params[:entry_id])} return end filters_4_item = {} self.fill_obligatory_filters(filters_4_item) filters_4_item[management_settings.entry_id_property] = params[management_settings.entry_id_property] filters_4_item[management_settings.container_id_property] = params[management_settings.container_id_property] local_item = management_settings.item_class.get_entries(nil, filters_4_item, {logger: string_logger}).first unless local_item local_item = get_managing_item_class.new local_item[management_settings.container_id_property] = container.id local_item[management_settings.entry_id_property] = entry.id if local_item.respond_to?(:sort_position) filters_4_items_max = {} self.fill_obligatory_filters(filters_4_items_max) filters_4_items_max[management_settings.container_id_property] = params[management_settings.container_id_property] max_sort_position = management_settings.item_class.get_entries(nil, filters_4_items_max, {logger: string_logger}).maximum(:sort_position) local_item.sort_position = max_sort_position ? max_sort_position + 1 : 1 end if local_item.save render json: {status: ::ChupakabraTools::SetManagementResult.id_by_tag('management_ok'), message: I18n.t("#{self.to_i18n}.management.messages.add_management_ok")} else render json: {status: ::ChupakabraTools::SetManagementResult.id_by_tag('management_error'), message: I18n.t("#{self.to_i18n}.management.messages.add_management_error")} end else render json: {status: ::ChupakabraTools::SetManagementResult.id_by_tag('entry_already_in_set'), message: I18n.t("#{self.to_i18n}.management.messages.entry_already_in_set")} end end def management_items_remove local_data = {} # getting management settings management_settings = self.class.get_kb_option(:management_settings) local_data[:management_settings] = management_settings #loading container data filters_4_container = {} self.fill_obligatory_filters(filters_4_container) local_container = management_settings.container_class.get_entry_by_id(params[management_settings.container_id_property], filters_4_container, {logger: string_logger}) unless local_container render json: {status: ::ChupakabraTools::SetManagementResult.id_by_tag('set_not_found'), message: I18n.t("#{self.to_i18n}.management.messages.set_not_found", id: params[:id])} return end filters_4_item = {} self.fill_obligatory_filters(filters_4_item) filters_4_item[management_settings.entry_id_property] = params[management_settings.entry_id_property] filters_4_item[management_settings.container_id_property] = params[management_settings.container_id_property] local_item = management_settings.item_class.get_entries(nil, filters_4_item, {logger: string_logger}).first unless local_item render json: {status: ::ChupakabraTools::SetManagementResult.id_by_tag('item_not_found'), message: I18n.t("#{self.to_i18n}.management.messages.item_not_found", entry_id: params[:entry_id])} return end if item.destroy render json: {status: ::ChupakabraTools::SetManagementResult.id_by_tag('management_ok'), message: I18n.t("#{self.to_i18n}.management.messages.remove_management_ok")} else render json: {status: ::ChupakabraTools::SetManagementResult.id_by_tag('management_error'), message: I18n.t("#{self.to_i18n}.management.messages.remove_management_error")} end end end end end end