require_dependency "content_control/application_controller"

module ContentControl
  class LandingsController < ApplicationController
    layout 'content_control/cms'

    # GET /landings
    # GET /landings.json
    def index
      @landings = Landing.all
  
      respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @landings }
      end
    end
  
    # GET /landings/1
    # GET /landings/1.json
    def show
      @landing = ContentControl::Landing.new
      @landing.landing_ip = request.remote_ip
      @landing.landing_url = request.fullpath
      @landing.save

      @page = ContentControl::Page.find_by_page_url_and_page_status_id(request.fullpath, PageStatus::STATUS_PUBLISHED)

      if @page.nil? then
        response.status = 404
        render_404
      else
        render @page.template.template_path
      end
    end

    def render_404
      respond_to do |format|
        format.html { render "404", :status => :not_found }
        format.xml  { head :not_found }
        format.any  { head :not_found }
      end
    end
  
    # GET /landings/new
    # GET /landings/new.json
    def new
      @landing = Landing.new
  
      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @landing }
      end
    end
  
    # GET /landings/1/edit
    def edit
      @landing = Landing.find(params[:id])
    end
  
    # POST /landings
    # POST /landings.json
    def create
      @landing = Landing.new(params[:landing])
  
      respond_to do |format|
        if @landing.save
          format.html { redirect_to @landing, notice: 'Landing was successfully created.' }
          format.json { render json: @landing, status: :created, location: @landing }
        else
          format.html { render action: "new" }
          format.json { render json: @landing.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # PUT /landings/1
    # PUT /landings/1.json
    def update
      @landing = Landing.find(params[:id])
  
      respond_to do |format|
        if @landing.update_attributes(params[:landing])
          format.html { redirect_to @landing, notice: 'Landing was successfully updated.' }
          format.json { head :no_content }
        else
          format.html { render action: "edit" }
          format.json { render json: @landing.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /landings/1
    # DELETE /landings/1.json
    def destroy
      @landing = Landing.find(params[:id])
      @landing.destroy
  
      respond_to do |format|
        format.html { redirect_to landings_url }
        format.json { head :no_content }
      end
    end
  end
end
