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

  
  layout "admin"
  
  def lien_anglais
    @text_contents = TextContent.where( "content LIKE ?", "%/en/%")
  end
    
  def anglais
    
    @lang = LangSite.find(2)
    @menu = Menu.find(1)
    @menu_items = @menu.menu_items.unarchived.where(:parent_id => params[:parent_id]).order("position ASC")
    
  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

      menu_item.skip_index = true
      menu_item.save
      
      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
  
    params[:lang] = params[:lang] || "fr"
    
    @lang = LangSite.find_by_slug(params[:lang])

    @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, :title_image => true)
    
    params[:menu_item_content_type] = "Page" if !params[:menu_item_content_type]
    
    @content = params[:menu_item_content_type].constantize.new()
    
    if params[:menu_item_content_type] == "Page"
      LangSite.all.each do |ls|
        @content.lang_pages << LangPage.new(:lang_site_id => ls.id)
      end
    
    end
    @menu_item.menu_content = @content
    
    
    
    
    LangSite.all.each do |ls|
      @menu_item.menu_item_langs << MenuItemLang.new(:lang_site_id => ls.id)
    end
    
    
    
  end

  def create
    params[:lang] = params[:lang] || "fr"
    
    @lang = LangSite.find_by_slug(params[:lang])
  
    params_content = params[:menu_item].delete(:menu_content_attributes)
    content_type = params[:menu_item][:menu_content_type]
    params_langs = params[:menu_item].delete(:menu_item_langs_attributes)

    
    @menu_item= MenuItem.new(params.require(:menu_item).permit!)
    content = content_type.constantize.new(params_content.permit!)
    
    params_langs.each do |lang|
      @menu_item.menu_item_langs << MenuItemLang.new(lang[1].permit!)
    end
    
    @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])
    
    params[:lang] = params[:lang] || "fr"
    
    @lang = LangSite.find_by_slug(params[:lang])
    
    
    @admin = true
  end
  
  def edit_articles

    @menu_item= MenuItem.find(params[:id])
    
    params[:lang] = params[:lang] || "fr"
    
    @lang = LangSite.find_by_slug(params[:lang])
    
    
    @admin = true
  end

  def update
    
    params[:lang] = params[:lang] || "fr"
    
    @lang = LangSite.find_by_slug(params[:lang])
    
    @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!)
        
        
      
        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
          MenuItem.all.each do |mi|
            mi.set_permalink
        
          end
          
          format.js { 
            
            render :action => "update" }
        else
          @menu_item.set_permalink
          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
    
    params[:lang] = params[:lang] || "fr"
    
    @lang = LangSite.find_by_slug(params[:lang])
    
    @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 update_permalinks
    MenuItem.all.each do |mi|
      mi.set_permalink
    
    end
    render :inline => "alert('Mise à jour effectuée');"
  end
  
  
  def clone_as_article
    
    @menu_item= MenuItem.find(params[:id])

		@article = Article.new(:published_at => Time.now, :skip_block => true)
    @article.save
    LangSite.all.each do |ls|
      block = @menu_item.menu_content.blocks.find_by_lang_site_id(ls.id)
      new_b = block.dup
      new_b.blockable = @article
      new_b.save
      mil = @menu_item.select_lang(ls.id)
      lang_page = @menu_item.menu_content.lang_pages.find_by_lang_site_id(ls.id)
      @article.lang_articles << LangArticle.new(:lang_site_id => ls.id, :title => lang_page.title, :description => lang_page.description)
    end
    @article.save
    
    redirect_to admin_articles_path
    
  end
  
  
  
  def clone
    @menu_item= MenuItem.find(params[:id])
    
    @new = @menu_item.dup

    
    @new.id
    @new.menu_content = @menu_item.menu_content.dup
    @new.save
    MenuItem.all.each do |mi|
      mi.set_permalink
    
    end
    
    @menu_item.menu_item_langs.each do |menu_item_lang|
      new_ml = menu_item_lang.dup
      new_ml.name = menu_item_lang.name+" copie"
      slug = menu_item_lang.slug+"-copie"
      new_ml.slug = slug
      
      i = 0
      while MenuItemLang.where(:slug => new_ml.slug).exists?
        i= i+1
        new_ml.slug = slug+"-#{i}"
      end
      new_ml.name = new_ml.name+" (#{i})" if i >0
      new_ml.menu_item = @new
      new_ml.save
      
      
      MenuItem.all.each do |mi|
        mi.set_permalink
      
      end
      
    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
