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

  def ab_group_edit
    assert_privileges("ab_group_edit")
    group_new_edit("edit")
  end

  def ab_group_new
    assert_privileges("ab_group_new")
    group_new_edit("new")
  end

  def ab_group_reorder
    assert_privileges("ab_group_reorder")
    case params[:button]
    when "cancel"
      add_flash(_("%s Group Reorder cancelled") % ui_lookup(:model => "CustomButton"))
      @edit = session[:edit] = nil  # clean out the saved info
      ab_get_node_info(x_node) if x_active_tree == :ab_tree
      replace_right_cell(x_node)
    when "save"
      return unless load_edit("group_reorder", "replace_cell__explorer")
      # save group_index of each custombuttonset in set_data
      if x_active_tree == :sandt_tree
        button_order = []
        st = ServiceTemplate.find_by_id(@sb[:applies_to_id])
      end
      @edit[:new][:fields].each_with_index do |field, i|
        field_nodes = field.last.split('-')
        button_order.push(field.last)   if x_active_tree == :sandt_tree
        if field_nodes.first == "cbg"
          cs = CustomButtonSet.find_by_id(field_nodes.last)
          cs.set_data[:group_index] = i + 1
          cs.save!
        end
      end

      if x_active_tree == :sandt_tree
        st.options[:button_order] = button_order
        st.save
      end
      add_flash(_("%s Group Reorder saved") % ui_lookup(:model => "CustomButton"))
      @edit = session[:edit] = nil  # clean out the saved info
      ab_get_node_info(x_node) if x_active_tree == :ab_tree
      replace_right_cell(x_node, x_active_tree == :ab_tree ? [:ab] : [:sandt])
    else
      if params[:button] == "reset"
        @changed = session[:changed] = false
        add_flash(_("All changes have been reset"), :warning)
      end
      group_reorder_set_form_vars
      @in_a_form = true
      @lastaction = "automate_button"
      @layout = "miq_ae_automate_button"
      replace_right_cell("group_reorder")
    end
  end

  def group_reorder_field_changed
    if params['selected_fields']
      return unless load_edit("group_reorder", "replace_cell__explorer")
      move_cols_up if params[:button] == "up"
      move_cols_down if params[:button] == "down"
      @changed = (@edit[:new] != @edit[:current])
      @refresh_partial = "group_order_form"
      render :update do |page|                    # Use JS to update the display
        page.replace("flash_msg_div", :partial => "layouts/flash_msg") unless @refresh_div && @refresh_div != "column_lists"
        page.replace(@refresh_div, :partial => "shared/buttons/#{@refresh_partial}") if @refresh_div
        page << "miqSparkle(false);"
        page << javascript_for_miq_button_visibility_changed(@changed)
      end
    else
      add_flash("No Button Group was selected!", :error)
      render :update do |page|
        page.replace("flash_msg_div", :partial => "layouts/flash_msg")
      end
    end
  end

  def group_create
    group_create_update("create")
  end

  def group_update
    group_create_update("update")
  end

  def automate_button_field_changed
    if params[:target_class]
    #     @resolve[:target_class] = params[:target_class]
    #     @resolve[:saved_buttons] = Array.new
    #     aset = CustomButtonSet.find_by_name("#{@resolve[:target_class].to_s.downcase}_custom")
    #     if aset
    #       aset.members.sort_by(&:button_id).each do |as|
    #         @resolve[:saved_buttons].push(as)
    #       end
    #     end
    #     uri = CustomButton.all(:conditions => {:applies_to_class=>@resolve[:target_class].to_s, :button_id=>0}).sort_by(&:description)
    #     if uri    # add uri records that dont have button number assigned to them
    #       uri.each do |u|
    #         @resolve[:saved_buttons].push(u)
    #       end
    #     end
    else
      @edit = session[:edit]
      @custom_button = @edit[:custom_button]
      if params[:readonly]
        @edit[:new][:readonly] = (params[:readonly] != "1")
      end
      @edit[:new][:instance_name] = params[:instance_name] if params[:instance_name]
      @edit[:new][:other_name] = params[:other_name] if params[:other_name]
      @edit[:new][:object_message] = params[:object_message] if params[:object_message]
      @edit[:new][:object_request] = params[:object_request] if params[:object_request]
      AE_MAX_RESOLUTION_FIELDS.times do |i|
        f = ("attribute_" + (i + 1).to_s)
        v = ("value_" + (i + 1).to_s)
        @edit[:new][:attrs][i][0] = params[f] if params[f.to_sym]
        @edit[:new][:attrs][i][1] = params[v] if params[v.to_sym]
      end
      @edit[:new][:target_attr_name] = params[:target_attr_name] if params[:target_attr_name]
      @edit[:new][:name] = params[:name] if params[:name]
      @edit[:new][:display] = params[:display] == "1" if params[:display]
      @edit[:new][:description] = params[:description] if params[:description]
      @edit[:new][:button_image] = params[:button_image].to_i if params[:button_image]<commit_msg>remove old automate_button_field_changed code<commit_after>module ApplicationController::Buttons
  extend ActiveSupport::Concern

  def ab_group_edit
    assert_privileges("ab_group_edit")
    group_new_edit("edit")
  end

  def ab_group_new
    assert_privileges("ab_group_new")
    group_new_edit("new")
  end

  def ab_group_reorder
    assert_privileges("ab_group_reorder")
    case params[:button]
    when "cancel"
      add_flash(_("%s Group Reorder cancelled") % ui_lookup(:model => "CustomButton"))
      @edit = session[:edit] = nil  # clean out the saved info
      ab_get_node_info(x_node) if x_active_tree == :ab_tree
      replace_right_cell(x_node)
    when "save"
      return unless load_edit("group_reorder", "replace_cell__explorer")
      # save group_index of each custombuttonset in set_data
      if x_active_tree == :sandt_tree
        button_order = []
        st = ServiceTemplate.find_by_id(@sb[:applies_to_id])
      end
      @edit[:new][:fields].each_with_index do |field, i|
        field_nodes = field.last.split('-')
        button_order.push(field.last)   if x_active_tree == :sandt_tree
        if field_nodes.first == "cbg"
          cs = CustomButtonSet.find_by_id(field_nodes.last)
          cs.set_data[:group_index] = i + 1
          cs.save!
        end
      end

      if x_active_tree == :sandt_tree
        st.options[:button_order] = button_order
        st.save
      end
      add_flash(_("%s Group Reorder saved") % ui_lookup(:model => "CustomButton"))
      @edit = session[:edit] = nil  # clean out the saved info
      ab_get_node_info(x_node) if x_active_tree == :ab_tree
      replace_right_cell(x_node, x_active_tree == :ab_tree ? [:ab] : [:sandt])
    else
      if params[:button] == "reset"
        @changed = session[:changed] = false
        add_flash(_("All changes have been reset"), :warning)
      end
      group_reorder_set_form_vars
      @in_a_form = true
      @lastaction = "automate_button"
      @layout = "miq_ae_automate_button"
      replace_right_cell("group_reorder")
    end
  end

  def group_reorder_field_changed
    if params['selected_fields']
      return unless load_edit("group_reorder", "replace_cell__explorer")
      move_cols_up if params[:button] == "up"
      move_cols_down if params[:button] == "down"
      @changed = (@edit[:new] != @edit[:current])
      @refresh_partial = "group_order_form"
      render :update do |page|                    # Use JS to update the display
        page.replace("flash_msg_div", :partial => "layouts/flash_msg") unless @refresh_div && @refresh_div != "column_lists"
        page.replace(@refresh_div, :partial => "shared/buttons/#{@refresh_partial}") if @refresh_div
        page << "miqSparkle(false);"
        page << javascript_for_miq_button_visibility_changed(@changed)
      end
    else
      add_flash("No Button Group was selected!", :error)
      render :update do |page|
        page.replace("flash_msg_div", :partial => "layouts/flash_msg")
      end
    end
  end

  def group_create
    group_create_update("create")
  end

  def group_update
    group_create_update("update")
  end

  def automate_button_field_changed
    unless params[:target_class]
      @edit = session[:edit]
      @custom_button = @edit[:custom_button]
      if params[:readonly]
        @edit[:new][:readonly] = (params[:readonly] != "1")
      end
      @edit[:new][:instance_name] = params[:instance_name] if params[:instance_name]
      @edit[:new][:other_name] = params[:other_name] if params[:other_name]
      @edit[:new][:object_message] = params[:object_message] if params[:object_message]
      @edit[:new][:object_request] = params[:object_request] if params[:object_request]
      AE_MAX_RESOLUTION_FIELDS.times do |i|
        f = ("attribute_" + (i + 1).to_s)
        v = ("value_" + (i + 1).to_s)
        @edit[:new][:attrs][i][0] = params[f] if params[f.to_sym]
        @edit[:new][:attrs][i][1] = params[v] if params[v.to_sym]
      end
      @edit[:new][:target_attr_name] = params[:target_attr_name] if params[:target_attr_name]
      @edit[:new][:name] = params[:name] if params[:name]
      @edit[:new][:display] = params[:display] == "1" if params[:display]
      @edit[:new][:description] = params[:description] if params[:description]
      @edit[:new][:button_image] = params[:button_image].to_i if params[:button_image]
{'fraction_non_alphanumeric': 0.1300639658848614, 'fraction_numerical': 0.0015991471215351812, 'mean_word_length': 3.3430555555555554, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2488146', 'n_tokens_mistral': 3095, 'n_tokens_neox': 3041, 'n_words': 598}