page_templates_controller.rb 3.16 KB
module Kanjai
  class Admin::PageTemplatesController < AdminController

    before_action :set_s3_direct_post, only: [:new, :update, :edit, :update]

    after_action :unzip_content, only: [:create, :update]

    def index
      @collection = PageTemplate.order(:title)
    end

    def new
      @page_template = PageTemplate.new
    end

    def create
      @page_template = PageTemplate.new(permitted_params[:page_template])
      
      if @page_template.save
        render json: {status: 'ok'}
      end
    end

    def edit
      @page_template = PageTemplate.find(params[:id])
      @url = @page_template.file_expiring_url

      lang = params[:lang]

      if @page_template.unzip
        exist_markers = @page_template.page_content_markers.where(lang: lang).collect(&:marker)

        @page_template.template_statics.where.not(marker: exist_markers).each do |item|
          obj = @page_template.page_content_markers.create(
                      marker: item.marker,
                      marker_type: item.marker_type,
                      marker_name: item.marker_name,
                      lang: lang
          )
        end

        @markers = @page_template.page_content_markers.where(lang: lang)
      end


    end

    def update
      @page_template = PageTemplate.find(params[:id])
      @page_template.unzip = false

      if @page_template.update(permitted_params[:page_template])
        render json: {status: 'ok'}
      end

    end


    def destroy
      @page_template = PageTemplate.find(params[:id])
      @page_template.destroy

      render :json => {:status => 'ok'}.to_json
    end

    def marker
      lang = params[:lang]
      @page_template = PageTemplate.find(params[:id])
      if @page_template.unzip
        exist_markers = @page_template.page_content_markers.where(lang: lang).collect(&:marker)

        @page_template.template_statics.where.not(marker: exist_markers).each do |item|
          @page_template.page_content_markers.create(
                      marker: item.marker,
                      marker_type: item.marker_type,
                      marker_name: item.marker_name,
                      lang: lang
          )
        end



        @markers = @page_template.page_content_markers.where(lang: lang)
      else
        flash[:notice] = "Template must be unzip before edit static element"
        redirect_to action: :index
      end
    end

    def update_marker
      @page_template = PageTemplate.find(params[:id])
      if @page_template.update(permitted_params[:page_template])
        redirect_to :action => :index
      else
        redirect_to :action => :marker
      end
    end

    private

    def permitted_params
      params.permit(:page_template => [:title, :attachment_file_name, :notice,
                                       page_content_markers_attributes: [:id, :page_content_id, :marker, :text_value, :attachment_file_name]
                    ])
    end

    def unzip_content
      if @page_template.unzip == false
        @page_template.unzip_content
      end
    end

    def set_s3_direct_post
      @s3_direct_post = S3_BUCKET.presigned_post(key: "tmp/templates/${filename}", success_action_status: '201', acl: 'private')
    end

  end
end