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

  
  layout "admin"

  
  
  def import

  

      
    
    @bugs = []
    
    
    
    xml = Nokogiri::XML(File.open(Rails.root.join("db", "data","agendas.xml")))
    
    
    

    
    
    @agendas = xml.search('row').map do |game|
      %w[
        id title description content frontpage start_at finished_at whole_day created_at updated_at
      ].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("db", "data","actualitees.xml")))
    
    
    @actualitees = xml.search('row').map do |game|
      %w[
        id title description content frontpage start_at end_at image_file_name created_at updated_at
      ].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("db", "data","pages.xml")))
    
    
    pages = xml.search('row').map do |game|
      %w[
        id enabled visible archived super_admin parent_id position archived_at created_at updated_at archive_id comments_allowed ins
      ].each_with_object({}) do |n, o|
        o[n] = game.at(n).text if game.at(n)
      end
    end
    
    @pages = []
    pages.each do |p|
      @pages[p["id"].to_i] = p
    end
    
    
    xml = Nokogiri::XML(File.open(Rails.root.join("db", "data","lang_pages.xml")))
    
    
    lang_pages = xml.search('row').map do |game|
      %w[
        id page_id title slug menu_title description keywords
      ].each_with_object({}) do |n, o|
        o[n] = game.at(n).text if game.at(n)
      end
    end
    
    @lang_pages = []
    lang_pages.each do |p|
      @lang_pages[p["id"].to_i] = p
    end
    
    

    
    
    
    xml = Nokogiri::XML(File.open(Rails.root.join("db", "data","blocks.xml")))
    
    
    @blocks = xml.search('row').map do |game|
      %w[
        id lang_page_id content block_name_id
      ].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.unarchived.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.permit!)
    
    @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.unarchived.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
  
  def archives
     @menu_items = MenuItem.archived.order("archived_at DESC")
  end
  
  
  def archive
    @menu_item= MenuItem.find(params[:id])
    
    @new = @menu_item.dup
    @date = Time.now
    @new.name = @new.name+" archive "+ @date.strftime("%m-%d-%Y %H:%M:%S")
    @slug = @new.slug+"-archive-"+ @date.strftime("%m-%d-%Y-%H-%M-%S")
    @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.archived_at = Time.now
    @new.parent_id = nil
    @new.archived = true
    @new.archive_id = @menu_item.id
    
    
    @new.save
    MenuItem.all.each do |mi|
      mi.set_permalink
    
    end
    redirect_to :back
  end
  

end