# -*- encoding : utf-8 -*-

class Admin::TranslationsController < ApplicationController
	before_action :auth_admin

	layout "admin"


	before_action :find_translations


	def index 
    respond_to do |format|
      format.html
    
    
    
      format.csv { 
      
        @file = ""
      
        elements = {
        

          "locale" => "locale",
          "key" => "key",
          "value" => "value",
          
        
        }
      
   
      
        headers = []
        attributes = []
      
        elements.each do |key, value|
          headers << key
          attributes << value
        end
      
        @csv = CSV.generate(:headers => true, :col_sep => ";", :encoding => "UTF-8") do |csv|
          csv << headers
        
          Translation.all.each do |trans|
        
              csv << attributes.map{ |attr| eval("trans."+attr.to_s) }

          end
        
        end
      
    send_data @csv, :filename => "export-translations-#{Date.today.to_s.to_slug}.csv", :type => 'text/csv; header=present' }
	
  end
  end



	def edit
		@translation = Translation.find(params[:id])
	end
  
	def show
		@translation = Translation.find(params[:id])
	end

	

	def update
		@translation = Translation.find(params[:id])


		
	
		
		
		respond_to do |format|
			if @translation.update_attributes(params.require(:translation).permit!)
				

			
				flash[:notice] = "Le menu à été modifié avec succès."
				
				if @reorder
					format.js { 
						
						render :action => "update" }
				else
					format.js { 
					   @translation = Translation.find(@translation.id)
						render :action => "update_row" }
				end
      end
			
		end
    
    
    
    

	end

  def import
    @file = params[:csv_file]
    
    require 'csv'    

    csv_text = @file.tempfile.read.force_encoding('UTF-8')
    @csv = CSV.parse(csv_text, :headers => true, :col_sep => ";")
    
    
  end
	
	protected
	
	def find_translations    
    
    @translations = Translation.all #.order("key ASC")
		

	
  end
  
end
