<commit_before>require 'rtf'

class RequirementsTemplatesController < ApplicationController
  before_action :set_requirements_template, only: [:show, :edit, :update, :destroy, :toggle_active]
  before_action :check_DMPTemplate_editor_access, only: [:show, :edit, :update, :destroy, :index]

  # GET /requirements_templates
  # GET /requirements_templates.json
  def index
    case params[:scope]
      when "all"
        @requirements_templates = RequirementsTemplate.all.page(params[:page])
      when "all_limited"
        @requirements_templates = RequirementsTemplate.all.page(params[:page]).per(5)
      when "active"
        @requirements_templates = RequirementsTemplate.active.page(params[:page]).per(5)
      when "inactive"
        @requirements_templates = RequirementsTemplate.inactive.page(params[:page]).per(5)
      when "public"
        @requirements_templates = RequirementsTemplate.public_visibility.page(params[:page]).per(5)
      when "institutional"
        @requirements_templates = RequirementsTemplate.institutional_visibility.page(params[:page]).per(5)
      else
        @requirements_templates = RequirementsTemplate.order(created_at: :asc).page(params[:page]).per(5)
    end
    if !safe_has_role?(Role::DMP_ADMIN)
      @requirements_templates = @requirements_templates.where.
                                any_of(institution_id: [current_user.institution.subtree_ids], visibility: :public)
    end

    template_editors
    count
  end

  # GET /requirements_templates/1
  # GET /requirements_templates/1.json
  def show
  end

  # GET /requirements_templates/1/basic and basic_requirements_template_path
  # shows a basic template (as RTF for now)
  def basic
    @rt = RequirementsTemplate.find(params[:id])

    respond_to do |format|
      format.rtf {
        headers["Content-Disposition"] = "attachment; filename=\"" + sanitize_for_filename(@rt.name) + ".rtf\""
        render :layout => false,
               :content_type=> 'application/rtf'
               #:action => 'basic.rtf.erb',
      }
    end
  end

  # GET /requirements_templates/new
  def new
    @requirements_template = RequirementsTemplate.new
    @requirements_template.tags.build
    @requirements_template.additional_informations.build
    @requirements_template.sample_plans.build
  end

  def template_information
    @requirements_templates = RequirementsTemplate.institutional_visibility.page.per(5)
  end

  # GET /requirements_templates/1/edit
  def edit
    @sample_plans = @requirements_template.sample_plans
    @additional_informations = @requirements_template.additional_informations
    @tags = @requirements_template.tags
  end

  # POST /requirements_templates
  # POST /requirements_templates.json
  def create
    @requirements_template = RequirementsTemplate.new(requirements_template_params)

    respond_to do |format|
      if @requirements_template.save
        format.html { redirect_to edit_requirements_template_path(@requirements_template), notice: 'Requirements template was successfully created.' }
        format.json { render action: 'edit', status: :created, location: @requirements_template }
      else
        format.html { render action: 'new' }
        format.json { render json: @requirements_template.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /requirements_templates/1
  # PATCH/PUT /requirements_templates/1.json
  def update
    respond_to do |format|<commit_msg>Fixed pagination related error in DMP template information page<commit_after>require 'rtf'

class RequirementsTemplatesController < ApplicationController
  before_action :set_requirements_template, only: [:show, :edit, :update, :destroy, :toggle_active]
  before_action :check_DMPTemplate_editor_access, only: [:show, :edit, :update, :destroy, :index]

  # GET /requirements_templates
  # GET /requirements_templates.json
  def index
    case params[:scope]
      when "all"
        @requirements_templates = RequirementsTemplate.all.page(params[:page])
      when "all_limited"
        @requirements_templates = RequirementsTemplate.all.page(params[:page]).per(5)
      when "active"
        @requirements_templates = RequirementsTemplate.active.page(params[:page]).per(5)
      when "inactive"
        @requirements_templates = RequirementsTemplate.inactive.page(params[:page]).per(5)
      when "public"
        @requirements_templates = RequirementsTemplate.public_visibility.page(params[:page]).per(5)
      when "institutional"
        @requirements_templates = RequirementsTemplate.institutional_visibility.page(params[:page]).per(5)
      else
        @requirements_templates = RequirementsTemplate.order(created_at: :asc).page(params[:page]).per(5)
    end
    if !safe_has_role?(Role::DMP_ADMIN)
      @requirements_templates = @requirements_templates.where.
                                any_of(institution_id: [current_user.institution.subtree_ids], visibility: :public)
    end

    template_editors
    count
  end

  # GET /requirements_templates/1
  # GET /requirements_templates/1.json
  def show
  end

  # GET /requirements_templates/1/basic and basic_requirements_template_path
  # shows a basic template (as RTF for now)
  def basic
    @rt = RequirementsTemplate.find(params[:id])

    respond_to do |format|
      format.rtf {
        headers["Content-Disposition"] = "attachment; filename=\"" + sanitize_for_filename(@rt.name) + ".rtf\""
        render :layout => false,
               :content_type=> 'application/rtf'
               #:action => 'basic.rtf.erb',
      }
    end
  end

  # GET /requirements_templates/new
  def new
    @requirements_template = RequirementsTemplate.new
    @requirements_template.tags.build
    @requirements_template.additional_informations.build
    @requirements_template.sample_plans.build
  end

  def template_information
    @requirements_templates = RequirementsTemplate.institutional_visibility.page(params[:page]).per(5)
  end

  # GET /requirements_templates/1/edit
  def edit
    @sample_plans = @requirements_template.sample_plans
    @additional_informations = @requirements_template.additional_informations
    @tags = @requirements_template.tags
  end

  # POST /requirements_templates
  # POST /requirements_templates.json
  def create
    @requirements_template = RequirementsTemplate.new(requirements_template_params)

    respond_to do |format|
      if @requirements_template.save
        format.html { redirect_to edit_requirements_template_path(@requirements_template), notice: 'Requirements template was successfully created.' }
        format.json { render action: 'edit', status: :created, location: @requirements_template }
      else
        format.html { render action: 'new' }
        format.json { render json: @requirements_template.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /requirements_templates/1
  # PATCH/PUT /requirements_templates/1.json
  def update
    respond_to do |format|
{'fraction_non_alphanumeric': 0.09550481114462157, 'fraction_numerical': 0.003734022691368663, 'mean_word_length': 3.8665269042627535, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1365718', 'n_tokens_mistral': 2069, 'n_tokens_neox': 1997, 'n_words': 387}