class Panel::PersonalProject::Plugin::ProjectsController < OrbitBackendController
  include AdminHelper
  include OrbitControllerLib::DivisionForDisable
  
  before_filter :authenticate_user!
  # before_filter :for_app_manager,:except => [:index,:show] 
  before_filter :only => [ :new,:create,:edit,:update,:create] do |controller|
    controller.get_categorys('ProjectCategory')
  end

  def index

  get_categorys("ProjectCategory",params[:project_project_category_id])
  @filter = params[:filter]
  new_filter = params[:new_filter]

  if @filter && params[:clear]
    @filter.delete(params[:type])
  elsif @filter && new_filter
    if @filter.has_key?(new_filter[:type]) && @filter[new_filter[:type]].include?(new_filter[:id].to_s)
      @filter[new_filter[:type]].delete(new_filter[:id].to_s)
    elsif @filter.has_key?(new_filter[:type])
      @filter[new_filter[:type]] << new_filter[:id].to_s
    else
      @filter.merge!({new_filter[:type] => [new_filter[:id].to_s]})
    end
  elsif new_filter
    @filter = {new_filter[:type] => [new_filter[:id].to_s]}
  end
  

    @project_categorys = get_categories_for_index("ProjectCategory")
    @project_category_ids = @project_categories.collect{|t| t.id.to_s} + [nil]
	
	@projects = (params[:sort] || @filter) ? get_sorted_and_filtered("project",:create_user_id => current_user.id) : get_viewable("project", :create_user_id => current_user.id)

  @tags = get_tags

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

  # GET /projects/1
  # GET /projects/1.xml
  def show
    @project = Project.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @project }
    end
  end

  # GET /projects/new
  # GET /projects/new.xml
  def new
  
    @project = Project.new
	
    @project_categorys = ProjectCategory.all
	
    @tags = get_tags

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

  # GET /projects/1/edit
  def edit
    @project = Project.find(params[:id])
	
    @project_categorys = ProjectCategory.all
	
    @tags = get_tags
  end

  # POST /projects
  # POST /projects.xml
  def create
	
    @project_categorys = ProjectCategory.all
	@tags = get_tags
  
    @project = Project.new(params[:project])
	
    @project.create_user_id = current_user.id
    @project.update_user_id = current_user.id

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

  # PUT /projects/1
  # PUT /projects/1.xml
  def update
	
    @project = Project.find(params[:id])
	
    @project.update_user_id = current_user.id
	
	params[:project][:tag_ids] ||=[]

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

  # DELETE /projects/1
  # DELETE /projects/1.xml
  def destroy
    @project = Project.find(params[:id])
    @project.destroy

    respond_to do |format|
      format.html { redirect_to(panel_personal_project_plugin_projects_url) }
      # format.xml  { head :ok }
      format.js
    end
  end

  def delete
    if params[:ids]
      projects = Project.any_in(:_id => params[:ids]).destroy_all
    end
    redirect_to panel_personal_project_plugin_projects_url(:direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options])
  end
  
  protected
 

  # def get_index_categories(id = nil)
  #   @bulletin_categorys = []
  #   if(is_manager? || is_admin?)
  #     @bulletin_categorys = (id ? BulletinCategory.admin_manager_all.find(id).to_a : BulletinCategory.admin_manager_all)
  #   elsif is_sub_manager?
  #     @bulletin_categorys = BulletinCategory.all
  #   end
  #   @bulletin_categorys
  # end


  # def get_categorys(id = nil)
  #   @project_categorys = []
  #   if(is_manager? || is_admin?)
  #     @project_categorys = (id ? Project.admin_manager_all.find(id).to_a : Project.admin_manager_all))
  #   elsif is_sub_manager?
  #     @project_categorys = Project.all.authed_for_user(current_user,'edit')
  #   end
  #   if @project_categorys.empty? && params[:action] != "index"
  #       flash[:alert] = t("announcement.error.no_avilb_cate_for_posting")
  #       redirect_to :action => :index
  #   end
  # end
  
end
