<commit_before># frozen_string_literal: true

module GobiertoAdmin
  module GobiertoPlans
    class PlansController < GobiertoAdmin::GobiertoPlans::BaseController
      def index
        @plans = current_site.plans.sort_by_updated_at
        @archived_plans = current_site.plans.only_archived.sort_by_updated_at
      end

      def new
        @plan_form = PlanForm.new(site_id: current_site.id)
        @plan_visibility_levels = plan_visibility_levels
        @plan_types = find_plan_types
        @vocabularies = current_site.vocabularies
      end

      def edit
        @plan = find_plan
        @plan_types = find_plan_types
        @plan_visibility_levels = plan_visibility_levels
        @vocabularies = current_site.vocabularies

        @plan_form = PlanForm.new(
          @plan.attributes.except(*ignored_plan_attributes).merge(site_id: current_site.id)
        )
      end

      def data
        @plan = find_plan
        if @plan.categories.exists?
          @categories_list = (0..@plan.categories.maximum(:level)).map do |level|
            @plan.categories.where(level: level).map { |cat| { id: cat.id.to_s, name: cat.name } }.unshift(id: nil, name: nil)
          end
          @categories_vocabulary = ActiveModelSerializers::SerializableResource.new(@plan.categories, each_serializer: GobiertoAdmin::GobiertoPlans::CategorySerializer).serializable_hash
        end
      end

      def create
        @plan_form = PlanForm.new(plan_params.merge(site_id: current_site.id))

        if @plan_form.save
          track_create_activity

          redirect_to(
            edit_admin_plans_plan_path(@plan_form.plan),
            notice: t(".success_html", link: gobierto_plans_plan_type_preview_url(@plan_form.plan, host: current_site.domain))
          )
        else
          @plan_types = find_plan_types
          @plan_visibility_levels = plan_visibility_levels
          @vocabularies = current_site.vocabularies

          render :new
        end
      end

      def update
        @plan = find_plan

        @plan_form = PlanForm.new(
          plan_params.merge(id: params[:id], site_id: current_site.id)
        )

        if @plan_form.save
          track_update_activity

          redirect_to(
            edit_admin_plans_plan_path(@plan),
            notice: t(".success_html", link: gobierto_plans_plan_type_preview_url(@plan_form.plan, host: current_site.domain))
          )
        else
          @plan_types = find_plan_types
          @plan_visibility_levels = plan_visibility_levels
          @vocabularies = current_site.vocabularies

          render :edit
        end
      end

      def destroy
        @plan = find_plan

        @plan.destroy

        redirect_to admin_plans_plans_path, notice: t(".success")
      end

      def recover
        @plan = find_archived_plan
        @plan.restore

        redirect_to admin_plans_plans_path, notice: t(".success")
      end

      def import_csv
        @plan = find_plan
        @plan_data_form = PlanDataForm.new(plan: @plan)
      end

      def export_csv
        @plan = find_plan
        respond_to do |format|
          format.csv { render csv: ::GobiertoPlans::PlanDataDecorator.new(@plan).csv, filename: @plan.title }
        end
      end

      def import_data<commit_msg>Parameterize and dasherize plan title for exported file name<commit_after># frozen_string_literal: true

module GobiertoAdmin
  module GobiertoPlans
    class PlansController < GobiertoAdmin::GobiertoPlans::BaseController
      def index
        @plans = current_site.plans.sort_by_updated_at
        @archived_plans = current_site.plans.only_archived.sort_by_updated_at
      end

      def new
        @plan_form = PlanForm.new(site_id: current_site.id)
        @plan_visibility_levels = plan_visibility_levels
        @plan_types = find_plan_types
        @vocabularies = current_site.vocabularies
      end

      def edit
        @plan = find_plan
        @plan_types = find_plan_types
        @plan_visibility_levels = plan_visibility_levels
        @vocabularies = current_site.vocabularies

        @plan_form = PlanForm.new(
          @plan.attributes.except(*ignored_plan_attributes).merge(site_id: current_site.id)
        )
      end

      def data
        @plan = find_plan
        if @plan.categories.exists?
          @categories_list = (0..@plan.categories.maximum(:level)).map do |level|
            @plan.categories.where(level: level).map { |cat| { id: cat.id.to_s, name: cat.name } }.unshift(id: nil, name: nil)
          end
          @categories_vocabulary = ActiveModelSerializers::SerializableResource.new(@plan.categories, each_serializer: GobiertoAdmin::GobiertoPlans::CategorySerializer).serializable_hash
        end
      end

      def create
        @plan_form = PlanForm.new(plan_params.merge(site_id: current_site.id))

        if @plan_form.save
          track_create_activity

          redirect_to(
            edit_admin_plans_plan_path(@plan_form.plan),
            notice: t(".success_html", link: gobierto_plans_plan_type_preview_url(@plan_form.plan, host: current_site.domain))
          )
        else
          @plan_types = find_plan_types
          @plan_visibility_levels = plan_visibility_levels
          @vocabularies = current_site.vocabularies

          render :new
        end
      end

      def update
        @plan = find_plan

        @plan_form = PlanForm.new(
          plan_params.merge(id: params[:id], site_id: current_site.id)
        )

        if @plan_form.save
          track_update_activity

          redirect_to(
            edit_admin_plans_plan_path(@plan),
            notice: t(".success_html", link: gobierto_plans_plan_type_preview_url(@plan_form.plan, host: current_site.domain))
          )
        else
          @plan_types = find_plan_types
          @plan_visibility_levels = plan_visibility_levels
          @vocabularies = current_site.vocabularies

          render :edit
        end
      end

      def destroy
        @plan = find_plan

        @plan.destroy

        redirect_to admin_plans_plans_path, notice: t(".success")
      end

      def recover
        @plan = find_archived_plan
        @plan.restore

        redirect_to admin_plans_plans_path, notice: t(".success")
      end

      def import_csv
        @plan = find_plan
        @plan_data_form = PlanDataForm.new(plan: @plan)
      end

      def export_csv
        @plan = find_plan
        respond_to do |format|
          format.csv { render csv: ::GobiertoPlans::PlanDataDecorator.new(@plan).csv, filename: @plan.title.parameterize.dasherize }
        end
      end

      def import_data
{'fraction_non_alphanumeric': 0.0793124246079614, 'fraction_numerical': 0.00030156815440289503, 'mean_word_length': 2.392838874680307, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6439709', 'n_tokens_mistral': 2074, 'n_tokens_neox': 1998, 'n_words': 359}