<commit_before>module ApplicationController::Tags
  extend ActiveSupport::Concern

  # Edit user, group or tenant tags
  def tagging_edit(db = nil, assert = true)
    assert_privileges("#{controller_for_common_methods}_tag") if assert
    @explorer = true if request.xml_http_request? # Ajax request means in explorer
    case params[:button]
    when "cancel"
      tagging_edit_tags_cancel
    when "save", "add"
      tagging_edit_tags_save
    when "reset", nil # Reset or first time in
      @tagging = session[:tag_db] = params[:db] ? params[:db] : db if params[:db] || db
      tagging_edit_tags_reset
    end
  end

  def service_tag
    tagging_edit('Service')
  end

  def container_tag
    tagging_edit('Container')
  end

  alias_method :image_tag, :tagging_edit
  alias_method :instance_tag, :tagging_edit
  alias_method :vm_tag, :tagging_edit
  alias_method :miq_template_tag, :tagging_edit
  alias_method :storage_tag, :tagging_edit
  alias_method :infra_networking_tag, :tagging_edit

  # New classification category chosen on the classify screen
  def classify_new_cat
    session[:cat] = Classification.find_by_name(params["classification"]["name"])
    classify_build_entries_pulldown

    render :update do |page|
      page << javascript_prologue
      page.replace("value_div", :partial => "layouts/classify_value")
    end
  end

  # Handle tag edit field changes
  def tag_edit_form_field_changed
    id = params[:id]
    return unless load_edit("#{session[:tag_db]}_edit_tags__#{id}", "replace_cell__explorer")

    if params[:tag_cat]
      @edit[:cat] = Classification.find_by_id(params[:tag_cat])
      tag_edit_build_entries_pulldown
    elsif params[:tag_add]
      @edit[:new][:assignments].push(params[:tag_add].to_i)
      @assignments ||= Classification.find(@edit.fetch_path(:new, :assignments))
      @assignments.each_with_index do |a, a_idx|
        if a.parent.name == @edit[:cat].name && # If same category
           a.parent.single_value && #    single value category
           a.id != params[:tag_add].to_i           #    different tag
          @edit[:new][:assignments].delete(a.id)  # Remove prev tag from new
          @assignments.delete_at(a_idx)           # Remove prev tag from display
          break
        end
      end
    elsif params[:tag_remove]
      @edit[:new][:assignments].delete(params[:tag_remove].to_i)
    end
    @edit[:new][:assignments].sort!
    @assignments ||= Classification.find(@edit.fetch_path(:new, :assignments))

    tag_edit_build_entries_pulldown
    render :update do |page|
      page << javascript_prologue
      changed = (@edit[:new] != @edit[:current])
      if changed != session[:changed]
        session[:changed] = changed
        page << javascript_for_miq_button_visibility(changed)
      end
      page.replace("cat_tags_div", :partial => "layouts/tag_edit_cat_tags")
      page.replace("assignments_div", :partial => "layouts/tag_edit_assignments") unless params[:tag_cat]
      if params[:tag_add]
        page << jquery_pulsate_element("#{j_str(params[:tag_add])}_tr")
      end
    end
  end

  # Assign a classification entry to a set of objects
  def classify_assign
    entry = Classification.find_by_id(params["entry"]["id"])
    session[:tag_items].each do |item|
      entry.assign_entry_to(session[:tag_db].find(item))
    end
    classify_build_screen
    render :update do |page|
      page << javascript_prologue
      page.replace("value_div", :partial => "layouts/classify_value")
      page.replace("table_div", :partial => "layouts/classify_table")
      page << jquery_pulsate_element("#{entry.id}_tr")
    end
  end

  # Remove a classification entry from a set of objects
  def classify_remove
    entry = Classification.find_by_id(params["id"])
    session[:tag_items].each do |item|
      entry.remove_entry_from(session[:tag_db].find(item))
    end
    classify_build_screen
    render :update do |page|
      page << javascript_prologue
      page.replace("value_div", :partial => "layouts/classify_value")
      page.replace("table_div", :partial => "layouts/classify_table")
    end
  end

  def filters
    @in_a_form = true
    session[:filter_object] = Host.find(1)
    session[:cat] = nil
    classify_build_screen
  end

  private ############################

  def get_tag_items
    recs = []
    if !session[:checked_items].nil? && @lastaction == "set_checked_items"
      recs = session[:checked_items]
    else
      recs = find_checked_items
    end
    if recs.blank?
      recs = [params[:id]]
    end
    if recs.length < 1
      add_flash(_("One or more %{model} must be selected to Smart Tagging") %
        {:model => Dictionary.gettext(db.to_s, :type => :model, :notfound => :titleize, :plural => true)}, :error)
      @refresh_div = "flash_msg_div"
      @refresh_partial = "layouts/flash_msg"
      return
    else
      session[:tag_items] = recs    # Set the array of tag items
      session[:assigned_filters] = assigned_filters
    end
  end

  def tagging_edit_tags_reset
    get_tag_items if @explorer
    @object_ids = session[:tag_items]
    @sb[:rec_id] = params[:id] ? params[:id] : session[:tag_items][0]
    if params[:button] == "reset"
      @tagging = session[:tag_db].to_s
      id = params[:id] if params[:id]
      return unless load_edit("#{session[:tag_db]}_edit_tags__#{id}")
      @object_ids = @edit[:object_ids]
    else
      # @object_ids[0] = params[:id] if @object_ids.blank? && params[:id]
      @tagging = session[:tag_db].to_s
    end
    tagging_tags_set_form_vars
    @display   = nil
    @in_a_form = true
    session[:changed] = false
    add_flash(_("All changes have been reset"), :warning) if params[:button] == "reset"
    if tagging_explorer_controller?
      @refresh_partial = "layouts/tagging"
      replace_right_cell(@sb[:action]) if params[:button]
    else
      render "shared/views/tagging_edit"
    end
  end

  # Set form vars for tag editor
  def tagging_tags_set_form_vars
    @edit = {}<commit_msg>Refactor Changes- Removed duplicate code in tagging_edit_tags_reset method<commit_after>module ApplicationController::Tags
  extend ActiveSupport::Concern

  # Edit user, group or tenant tags
  def tagging_edit(db = nil, assert = true)
    assert_privileges("#{controller_for_common_methods}_tag") if assert
    @explorer = true if request.xml_http_request? # Ajax request means in explorer
    case params[:button]
    when "cancel"
      tagging_edit_tags_cancel
    when "save", "add"
      tagging_edit_tags_save
    when "reset", nil # Reset or first time in
      @tagging = session[:tag_db] = params[:db] ? params[:db] : db if params[:db] || db
      tagging_edit_tags_reset
    end
  end

  def service_tag
    tagging_edit('Service')
  end

  def container_tag
    tagging_edit('Container')
  end

  alias_method :image_tag, :tagging_edit
  alias_method :instance_tag, :tagging_edit
  alias_method :vm_tag, :tagging_edit
  alias_method :miq_template_tag, :tagging_edit
  alias_method :storage_tag, :tagging_edit
  alias_method :infra_networking_tag, :tagging_edit

  # New classification category chosen on the classify screen
  def classify_new_cat
    session[:cat] = Classification.find_by_name(params["classification"]["name"])
    classify_build_entries_pulldown

    render :update do |page|
      page << javascript_prologue
      page.replace("value_div", :partial => "layouts/classify_value")
    end
  end

  # Handle tag edit field changes
  def tag_edit_form_field_changed
    id = params[:id]
    return unless load_edit("#{session[:tag_db]}_edit_tags__#{id}", "replace_cell__explorer")

    if params[:tag_cat]
      @edit[:cat] = Classification.find_by_id(params[:tag_cat])
      tag_edit_build_entries_pulldown
    elsif params[:tag_add]
      @edit[:new][:assignments].push(params[:tag_add].to_i)
      @assignments ||= Classification.find(@edit.fetch_path(:new, :assignments))
      @assignments.each_with_index do |a, a_idx|
        if a.parent.name == @edit[:cat].name && # If same category
           a.parent.single_value && #    single value category
           a.id != params[:tag_add].to_i           #    different tag
          @edit[:new][:assignments].delete(a.id)  # Remove prev tag from new
          @assignments.delete_at(a_idx)           # Remove prev tag from display
          break
        end
      end
    elsif params[:tag_remove]
      @edit[:new][:assignments].delete(params[:tag_remove].to_i)
    end
    @edit[:new][:assignments].sort!
    @assignments ||= Classification.find(@edit.fetch_path(:new, :assignments))

    tag_edit_build_entries_pulldown
    render :update do |page|
      page << javascript_prologue
      changed = (@edit[:new] != @edit[:current])
      if changed != session[:changed]
        session[:changed] = changed
        page << javascript_for_miq_button_visibility(changed)
      end
      page.replace("cat_tags_div", :partial => "layouts/tag_edit_cat_tags")
      page.replace("assignments_div", :partial => "layouts/tag_edit_assignments") unless params[:tag_cat]
      if params[:tag_add]
        page << jquery_pulsate_element("#{j_str(params[:tag_add])}_tr")
      end
    end
  end

  # Assign a classification entry to a set of objects
  def classify_assign
    entry = Classification.find_by_id(params["entry"]["id"])
    session[:tag_items].each do |item|
      entry.assign_entry_to(session[:tag_db].find(item))
    end
    classify_build_screen
    render :update do |page|
      page << javascript_prologue
      page.replace("value_div", :partial => "layouts/classify_value")
      page.replace("table_div", :partial => "layouts/classify_table")
      page << jquery_pulsate_element("#{entry.id}_tr")
    end
  end

  # Remove a classification entry from a set of objects
  def classify_remove
    entry = Classification.find_by_id(params["id"])
    session[:tag_items].each do |item|
      entry.remove_entry_from(session[:tag_db].find(item))
    end
    classify_build_screen
    render :update do |page|
      page << javascript_prologue
      page.replace("value_div", :partial => "layouts/classify_value")
      page.replace("table_div", :partial => "layouts/classify_table")
    end
  end

  def filters
    @in_a_form = true
    session[:filter_object] = Host.find(1)
    session[:cat] = nil
    classify_build_screen
  end

  private ############################

  def get_tag_items
    recs = []
    if !session[:checked_items].nil? && @lastaction == "set_checked_items"
      recs = session[:checked_items]
    else
      recs = find_checked_items
    end
    if recs.blank?
      recs = [params[:id]]
    end
    if recs.length < 1
      add_flash(_("One or more %{model} must be selected to Smart Tagging") %
        {:model => Dictionary.gettext(db.to_s, :type => :model, :notfound => :titleize, :plural => true)}, :error)
      @refresh_div = "flash_msg_div"
      @refresh_partial = "layouts/flash_msg"
      return
    else
      session[:tag_items] = recs    # Set the array of tag items
      session[:assigned_filters] = assigned_filters
    end
  end

  def tagging_edit_tags_reset
    get_tag_items if @explorer
    @object_ids = session[:tag_items]
    @sb[:rec_id] = params[:id] ? params[:id] : session[:tag_items][0]
    @tagging = session[:tag_db].to_s
    if params[:button] == "reset"
      id = params[:id] if params[:id]
      return unless load_edit("#{session[:tag_db]}_edit_tags__#{id}")
      @object_ids = @edit[:object_ids]
    end
    tagging_tags_set_form_vars
    @display   = nil
    @in_a_form = true
    session[:changed] = false
    add_flash(_("All changes have been reset"), :warning) if params[:button] == "reset"
    if tagging_explorer_controller?
      @refresh_partial = "layouts/tagging"
      replace_right_cell(@sb[:action]) if params[:button]
    else
      render "shared/views/tagging_edit"
    end
  end

  # Set form vars for tag editor
  def tagging_tags_set_form_vars
    @edit = {}
{'fraction_non_alphanumeric': 0.11945678598373712, 'fraction_numerical': 0.0005868052644815156, 'mean_word_length': 3.551697825257535, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4618102', 'n_tokens_mistral': 3880, 'n_tokens_neox': 3798, 'n_words': 821}