class Panel::PageContent::BackEnd::PageContextsController < OrbitBackendController
  before_filter :force_order_for_visitor,:only=>[:index]
  before_filter :force_order_for_user,:except => [:index]
  
  before_filter :for_app_manager,:except => [:index]
  before_filter :for_app_sub_manager,:except => [:index]
   
  #before_filter :is_admin?

  def index
	@page_contexts =  (params.has_key?:sort)?  get_sorted_and_filtered("page_contexts", {:archived => false}) : PageContext.where(:archived => false).page(params[:page]).per(10)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @page_contexts }
      format.js {}
    end
  end
  
  def view
	
	@page_contexts = PageContext.where("page_id" => params[:page_id]).desc(:updated_at)

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

  # GET /page_contexts/1
  # GET /page_contexts/1.xml
  def show
    @page_context = PageContext.find(params[:id])
	# get_categorys

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

  # GET /page_contexts/new
  # GET /page_contexts/new.xml
  def new
    @page_context = PageContext.new

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

  # GET /page_contexts/1/edit
  def edit
    @page_context = PageContext.find(params[:id])
  end

  # POST /page_contexts
  # POST /page_contexts.xml
  def create
    @page_context = PageContext.new(params[:page_context])
	
    @page_context.create_user_id = current_user.id
    @page_context.update_user_id = current_user.id

    respond_to do |format|
      if @page_context.save
        format.html { redirect_to(panel_page_content_back_end_page_contexts_url, :notice => t('page_content.create_page_content_success')) }
        format.xml  { render :xml => @page_context, :status => :created, :location => @page_context }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @page_context.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /page_contexts/1
  # PUT /page_contexts/1.xml
  def update
    @page_context = PageContext.find(params[:id])
	
	if ( @page_context.version > 0 )
		@create_page_context = PageContext.new(:version => @page_context.version)
		@create_page_context = @page_context.clone
		@create_page_context.context = @page_context.context.clone
		@create_page_context.archived = true
	end
	
    @page_context.update_user_id = current_user.id
    @page_context.version = @page_context.version + 1 

    respond_to do |format|
      if @page_context.update_attributes(params[:page_context])
	  
		if ( @page_context.version > 1 )
			@create_page_context.save		
		end
		
        format.html { redirect_to(panel_page_content_back_end_page_contexts_url, :notice => t('page_content.update_page_content_success')) }
		format.js  { render 'toggle_enable' }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @page_context.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /page_contexts/1
  # DELETE /page_contexts/1.xml
  def destroy
    @page_context = PageContext.find(params[:id])
    @page_context.destroy

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