# -*- encoding : utf-8 -*-
class Admin::MenuItemsController < ApplicationController
	 before_filter :auth_admin

	
	layout "admin"

  
  
  def import
    @repertoires = []
    
    @repertoires[33] = "coaching"
    @repertoires[3] = "technique"
    @repertoires[4] = "temoignages"
    @repertoires[36] = "livres"
    @repertoires[37] = "archives"
    @repertoires[34] = "traductions"
    @repertoires[35] = "temoignages"
    @repertoires[38] = "genevieve-bally"
    @repertoires[39] = "veronique-waumans"
    @repertoires[40] = "eft-loire"
  
  
    @repertoires.each do |repertoire|
      puts "TEST"
      puts repertoire
      
      repertoire_new= MenuItem.new(:enabled => true, :visible => true,:menu_id => 1, :menu_content_type => "Page",:name => repertoire, :slug => repertoire.to_s)
  		content = Page.new(:title => repertoire)

  		repertoire_new.menu_content = content
  
  
      repertoire_new.save
      
    end
      
    
    @bugs = []
    xml = Nokogiri::XML(File.open(Rails.root.join("article2.xml")))
    
    
    @articles = xml.search('articles').map do |game|
      %w[
        id section repertoire title name description keywords
      ].each_with_object({}) do |n, o|
        o[n] = game.at(n).text if game.at(n)
      end
    end
    
    
    
    xml = Nokogiri::XML(File.open(Rails.root.join("archives.xml")))
    
    
    @dates_brutes = xml.search('article').map do |game|
      %w[
        repertoire name created_at
      ].each_with_object({}) do |n, o|
        o[n] = game.at(n).text if game.at(n)
      end
    end
    
    @dates = {}
    
    @dates_brutes.each do |date|
      reper = date["repertoire"] 
      reper += "/" if reper
      @dates[reper.to_s+date["name"]] = date["created_at"]
      
    end
    
    
    
 
    
  end
	
  def find_menu_items

    
    params[:parent_id] = nil if !params[:parent_id] or params[:parent_id] == ""
			
		@menu_parent = MenuItem.find(params[:parent_id]) if params[:parent_id]
    
    
    @menu_items = @menu.menu_items.where(:parent_id => params[:parent_id]).order("position ASC")
		

	
  end
  
  
  def reorder

    i = 1
    
    
    params[:menu_items_ids].each do |id|
      

      menu_item = MenuItem.find(id)
      menu_item.position = i
      puts i
      menu_item.update_attributes :position => i
      
      i = i + 1
      
      
    end
    
		if params[:menu_id] and params[:menu_id] != "" and @menu = Menu.find(params[:menu_id])
			
      find_menu_items
    end
    
    flash[:success] = "Menus réorganisés avec succès."
    
   
    
  end
  
	def index
	


		@title = "Eléments de menu"
    
		if params[:menu_id] and params[:menu_id] != "" and @menu = Menu.find(params[:menu_id])
			
      find_menu_items
			
			
		
	
       

		
		
		else
			redirect_to admin_root_path, :alert => "Un menu doit être séléctionné."
		end
    
    
    
	end

	def new

		
		
		@menu_item = MenuItem.new(:menu_id => params[:menu_id],:parent_id => params[:parent_id], :visible => true)
		
		params[:menu_item_content_type] = "Page" if !params[:menu_item_content_type]
		
		@content = params[:menu_item_content_type].constantize.new()
		@menu_item.menu_content = @content
		
		
		
		
	end

	def create
	
		params_content = params[:menu_item].delete(:menu_content_attributes)
		content_type = params[:menu_item][:menu_content_type]

    
		@menu_item= MenuItem.new(params.require(:menu_item).permit(:parent_id, :menu_id, :enabled, :visible, :slug, :name, :menu_content_type))
		content = content_type.constantize.new(params_content)
		
		@menu_item.menu_content = content
	

		
		respond_to do |format|
			if @menu_item.save
				MenuItem.all.each do |mi|
					mi.set_permalink
				end
				
        @menu = Menu.find( @menu_item.menu_id)
        params[:parent_id] = @menu_item.parent_id
        find_menu_items
        
        
				flash[:notice] = "Le menu à été ajouté avec succès."
				format.html { 
					redirect_to(admin_menu_items_path(:menu_id => @menu_item.menu_id, :parent_id => @menu_item.parent_id)) 
					}
        format.js
				
			else
        
				format.html { render :action => "new" }
        format.js { render :action => "new" }
			end
		end
	
	end

	def edit

		@menu_item= MenuItem.find(params[:id])

	end

	def update

		@menu_item= MenuItem.find(params[:id])
		
		@menu = @menu_item.menu
		
		if request.xhr?
			@menu_parent = @menu_item.parent if @menu_item.parent
			@menu_items = MenuItem.where(:parent_id => @menu_item.parent_id, :menu_id => @menu_item.menu_id).order(:position)
		end
	
		if params[:menu_item][:parent_id] and params[:menu_item][:parent_id] == "no-menu-selected"
			@menu_item.parent_id = nil
			@menu_item.save
			params[:menu_item].delete(:parent_id)
			
		end
		
		@reorder = true if params[:reorder]
		
		
		
		
	
		
		
		respond_to do |format|
			if @menu_item.update_attributes(params.require(:menu_item).permit!)
				
				MenuItem.all.each do |mi|
					mi.set_permalink
				
				end
			
				flash[:notice] = "Le menu à été modifié avec succès."
				format.html { redirect_to(admin_menu_items_path(:parent_id => @menu_item.parent_id, :menu_id => @menu_item.menu_id)) }
				if @reorder
					format.js { 
						
						render :action => "update" }
				else
					format.js { 
					   @menu_item = MenuItem.find(@menu_item.id)
						render :action => "show" }
				end
			else
				
				flash[:error] = "Cet élément de menu n'a pas pu être déplacé. Vérifiez que son lien permanent n'éxiste pas déjà dans l'élément cible."
				if @reorder
					format.js { render :action => "update_reorder_failled" }
				else
					format.html { render :action => "edit" }
					format.js { render :action => "edit" }
				end
				
				
			end
		end
		
	end

	def destroy
		@menu_item = MenuItem.find(params[:id])
		@menu_item.destroy
		flash[:notice] = "L'élément de menu a été supprimé avec succès."
		respond_to do |format|
		
				
				format.html { redirect_to(admin_menu_items_path(:menu_id => @menu_item.menu_id, :parent_id => @menu_item.parent_id)) }
				format.js
		
	
		end
	end

	def show
		@menu_item= MenuItem.find(params[:id])
	end
	
	def cible
		
		params[:menu_id] = params[:menu_id] || 1

			if params[:menu_id] and params[:menu_id] != "" and @menu = Menu.find(params[:menu_id])


				params[:parent_id] = nil if !params[:parent_id]

				@menu_parent = MenuItem.find(params[:parent_id]) if params[:parent_id]



				@order = "position ASC"

				@menu_items = @menu.menu_items.where(:parent_id => params[:parent_id]).order(@order)



			else
				redirect_to admin_root_path, :alert => "Un menu doit être séléctionné."
			end
		
		render :layout => false
	
	end
	
	def clone
		@menu_item= MenuItem.find(params[:id])
		
		@new = @menu_item.dup
		@new.name = @new.name+" copie"
		@slug = @new.slug+"-copie"
		@new.slug = @slug
		i = 0
		while MenuItem.where(:slug => @new.slug, :menu_id => @new.menu_id, :parent_id => @new.parent_id).exists?
			i= i+1
			@new.slug = @slug+"-#{i}"
		end
		@new.name = @new.name+" (#{i})" if i >0
		@new.id
		@new.menu_content = @menu_item.menu_content.dup
		@new.save
		MenuItem.all.each do |mi|
			mi.set_permalink
		
		end
		redirect_to :back
	end

end