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

  def index

  get_categorys("BookPaperType",params[:book_paper_type_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
  @paper_types =  BookPaperType.all
  @paper_type_ids = @paper_types.collect{|t| t.id.to_s} + [nil]
  

	# @writing_books = WritingBook.search(params[:category_id])
	#@writing_books = (params[:sort] || @filter) ? get_sorted_and_filtered_writing_books : WritingBook.all.page(params[:page]).per(10)
  @writing_books = (params[:sort] || @filter) ? get_sorted_and_filtered("writing_book",:create_user_id => current_user.id) : get_viewable("writing_book", :create_user_id => current_user.id)

  get_tags

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

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

  # GET /writing_books/new
  # GET /writing_books/new.xml
  def new
  
    @writing_book = WritingBook.new
    # @writing_book_files = WritingBookFile.all
    # @level_types = BookLevelType.all
    @author_types = BookAuthorType.all
    @paper_types = BookPaperType.all
	
    get_tags

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

  # GET /writing_books/1/edit
  def edit
    @writing_book = WritingBook.find(params[:id])
	
    # @level_types = BookLevelType.all
    @author_types = BookAuthorType.all
    @paper_types = BookPaperType.all
	
    get_tags
  end

  # POST /writing_books
  # POST /writing_books.xml
  def create
	
    # @level_types = BookLevelType.all
    @author_types = BookAuthorType.all
    @paper_types = BookPaperType.all
	get_tags
  
    @writing_book = WritingBook.new(params[:writing_book])
	
    @writing_book.create_user_id = current_user.id
    @writing_book.update_user_id = current_user.id

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

  # PUT /writing_books/1
  # PUT /writing_books/1.xml
  def update
    @writing_book = WritingBook.find(params[:id])
	
    @writing_book.update_user_id = current_user.id
	
	params[:writing_book][:tag_ids] ||=[]

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

  # DELETE /writing_books/1
  # DELETE /writing_books/1.xml
  def destroy
    @writing_book = WritingBook.find(params[:id])
    @writing_book.destroy

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

  def delete
    if params[:ids]
      writing_books = WritingBook.any_in(:_id => params[:ids]).delete_all
    end
    redirect_to panel_personal_book_plugin_writing_books_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)
  #   @writing_book_categorys = []
  #   if(is_manager? || is_admin?)
  #     @writing_book_categorys = (id ? WritingBookCategory.admin_manager_all.find(id).to_a : WritingBookCategory.admin_manager_all))
  #   elsif is_sub_manager?
  #     @writing_book_categorys = WritingBookCategory.all.authed_for_user(current_user,'edit')
  #   end
  #   if @writing_book_categorys.empty? && params[:action] != "index"
  #       flash[:alert] = t("announcement.error.no_avilb_cate_for_posting")
  #       redirect_to :action => :index
  #   end
  # end
  
  def get_tags
  	module_app = ModuleApp.first(:conditions => {:key => 'personal_book'})
  	@tags = Tag.all(:conditions => {:module_app_id => module_app.id})
  end
  
end
