class Panel::Archive::BackEnd::ArchiveFilesController < 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]

  def index
  
	get_categorys(params[:archive_file_category_id])
	
	@archive_files = ArchiveFile.all.page(params[:page]).per(10)
	
	get_tags

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @archive_files }
    end
  end

  # GET /archive_files/1
  # GET /archive_files/1.xml
  def show
    @archive_file = ArchiveFile.find(params[:id])
	# get_categorys

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @archive_file }
    end
  end

  # GET /archive_files/new
  # GET /archive_files/new.xml
  def new
    @archive_file = ArchiveFile.new
	
	get_categorys
	
	get_tags

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @archive_file }
    end
  end

  # GET /archive_files/1/edit
  def edit
    @archive_file = ArchiveFile.find(params[:id])
	get_categorys
	get_tags
  end

  # POST /archive_files
  # POST /archive_files.xml
  def create
    @archive_file = ArchiveFile.new(params[:archive_file])
    @archive_file.create_user_id = current_user.id
    @archive_file.update_user_id = current_user.id

    respond_to do |format|
      if @archive_file.save
        format.html { redirect_to(panel_archive_back_end_archive_files_url) }
        format.xml  { render :xml => @archive_file, :status => :created, :location => @archive_file }
      else
        get_categorys
        get_tags
        format.html { render :action => "new" }
        format.xml  { render :xml => @archive_file.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /archive_files/1
  # PUT /archive_files/1.xml
  def update
    @archive_file = ArchiveFile.find(params[:id])
	
    @archive_file.update_user_id = current_user.id
	
	params[:archive_file][:tag_ids] ||=[]

    respond_to do |format|
      if @archive_file.update_attributes(params[:archive_file])
        format.html { redirect_to(panel_archive_back_end_archive_files_url) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @archive_file.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /archive_files/1
  # DELETE /archive_files/1.xml
  def destroy
    @archive_file = ArchiveFile.find(params[:id])
    @archive_file.destroy

    respond_to do |format|
      format.html { redirect_to(panel_archive_back_end_archive_files_url) }
      # format.xml  { head :ok }
      format.js
    end
  end
  
  protected
  
  def get_categorys(id = nil)
    @archive_file_categorys = (id ? ArchiveFileCategory.find(id).to_a : ArchiveFileCategory.excludes('disabled' => true))
  end
  
  def get_tags
  	module_app = ModuleApp.first(:conditions => {:key => 'archive'})
  	@tags = Tag.all(:conditions => {:module_app_id => module_app.id})
  end
  
end
