class Admin::TagsController < OrbitBackendController
  before_filter :force_order_for_visitor,:only=>[:index]
  before_filter :force_order_for_user,:except => [:index]
  before_filter :for_app_sub_manager,:except => [:index]
  

  # layout 'new_admin'
  # before_filter :authenticate_user!
  # before_filter :is_admin?
  # before_filter :set_module_app
   
  def index
    get_tags
    @module_app_id = @module_app.id rescue nil
  end

  def new

  end
  
  def edit
    @tag = Tag.find(params[:id])
  end

  def create
    if params[:tag][:module_app_id].blank?
      @tag = Tag.create(params[:tag])
    else
      module_app = ModuleApp.find(params[:tag][:module_app_id])
      @tag = eval("#{module_app.key.camelize}Tag").create(params[:tag])
    end
  end

  def update
    @tag = Tag.find(params[:id])
    @tag.update_attributes(params[:tag])
  end

  def destroy
    @tag = Tag.find(params[:id])
    @tag.destroy
    respond_to do |format|
      format.js { render 'js/remove_element', :locals => {:id => "#{dom_id @tag}"} }
    end
  end
  
  protected
  
  def set_module_app
    @module_app ||= ModuleApp.first(:conditions => {:key => @app_title.underscore}) rescue nil
  end
  
  def get_tags
    @tags = (@module_app ?  @module_app.tags : Tag.all)
  end
  
end
