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

  
  layout "admin"
  
  def group_exec
    @tag = Tag.find(params[:id])
    @tag_cible = Tag.find(params[:cible_id])
    if @tag != @tag_cible and !@tag_cible.ancestors.include?(@tag)
      TagTaggable.where(:tag_id => @tag.id).update_all(:tag_id => @tag_cible.id )
      Tag.where(:parent_id => @tag.id).update_all(:parent_id => @tag_cible.id )
      
      @tag.destroy
    end
    
  end
  
  def group
    @tag = Tag.find(params[:id])
    if params[:q]
      @tags = Tag.where("name LIKE ? or slug LIKE ? ", "%"+params[:q]+"%","%"+params[:q]+"%").order(:name)
    end
  end
  
  
  def images_select
    if params[:q]
      @tags = Tag.where("name LIKE ? or slug LIKE ? ", "%"+params[:q]+"%","%"+params[:q]+"%").order(:name)
    end
  end
  
  
  def search_form
    if params[:q] and params[:q].size > 2
      @tags = Tag.where("name LIKE ? or slug LIKE ? ", "%"+params[:q]+"%","%"+params[:q]+"%").order(:name)
    end
  end
  
  
  def find_tags

    
    params[:parent_id] = nil if !params[:parent_id] or params[:parent_id] == ""
      
    @tag_parent = Tag.find(params[:parent_id]) if params[:parent_id]
    
    
    @tags = Tag.where(:parent_id => params[:parent_id])
    

  
  end
  
  
  def reorder

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

      tag = Tag.find(id)
      tag.position = i

      tag.skip_index = true
      tag.save
      
      i = i + 1
      
      
    end
    
    if params[:menu_id] and params[:menu_id] != "" and @menu = Menu.find(params[:menu_id])
      
      find_tags
    end
    
    flash[:success] = ""
    
   
    
  end
  
  def index
  

     
      find_tags
      
      
  		per_page = (params[:per_page] and params[:per_page] != "") ? params[:per_page] : 1500
  		page = (params[:page] and params[:page] != "") ? params[:page] : 1 
      @tags = @tags.page(page).per(per_page)
    
  


   
    
    
  end

  def new

    
    
    @tag = Tag.new(:parent_id => params[:parent_id])
    
   
    
    
  end

  def create
    
    @tag= Tag.new(params.require(:tag).permit!)
   
    
    respond_to do |format|
      if @tag.save
        
        params[:parent_id] = @tag.parent_id
        find_tags
    		per_page = (params[:per_page] and params[:per_page] != "") ? params[:per_page] : 400
    		page = (params[:page] and params[:page] != "") ? params[:page] : 1 
        @tags = @tags.page(page).per(per_page)
    
        
        flash[:notice] = "Le tag a été ajouté"
        format.html { 
          redirect_to(admin_tags_path(:menu_id => @tag.menu_id, :parent_id => @tag.parent_id)) 
          }
        format.js
        
      else
        
        format.html { render :action => "new" }
        format.js { render :action => "new" }
      end
    end
  
  end

  def edit

    @tag= Tag.find(params[:id])
    
    params[:lang] = params[:lang] || "fr"
    
    @lang = LangSite.find_by_slug(params[:lang])
    
    
    @admin = true
  end
  
  def edit_articles

    @tag= Tag.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])
    
    @tag= Tag.find(params[:id])
  
    
    if request.xhr?
      @tag_parent = @tag.parent if @tag.parent
      @tags = Tag.where(:parent_id => @tag.parent_id)
    end
  
    if params[:tag][:parent_id] and params[:tag][:parent_id] == "no-menu-selected"
      @tag.parent_id = nil
      @tag.save
      params[:tag].delete(:parent_id)
      
    end
    
    @reorder = true if params[:reorder]
    
    
    
    
  
    
    
    respond_to do |format|
      if @tag.update_attributes(params.require(:tag).permit!)
        
        
      
        flash[:notice] = "Le tag à été modifié avec succès."
        format.html { redirect_to(admin_tags_path(:parent_id => @tag.parent_id)) }
        if @reorder
          
          
          format.js { 
            
            render :action => "update" }
        else
         
          format.js { 
             @tag = Tag.find(@tag.id)
            }
        end
      else
        
        flash[:error] = "Ce tag n'a pas pu être déplacé."
        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])
    
    @tag = Tag.find(params[:id])
    @tag.destroy
    flash[:notice] = "Tag supprimé"
    respond_to do |format|
    
        
        format.html { redirect_to(admin_tags_path()) }
        format.js
    
  
    end
  end

  def show
    @tag= Tag.find(params[:id])
  end
  
  def reorder_by_group
    @tag = Tag.find(params[:id]) if params[:id].to_i != 0
    ids = []
    params[:tag_ids].each do |tag_id|
      ids << tag_id.to_i
    end
    
    past_parent_tag = Tag.find(ids[0]).parent if Tag.find(ids[0])
    Tag.where(:id => ids).update_all(:parent_id => (@tag ? @tag.id : nil))
    
    @tag.save if @tag
    past_parent_tag.save if past_parent_tag
  end
  
  
  

end
