# -*- encoding : utf-8 -*-
class Public::MenuItemsController < ApplicationController
  
  layout "public"
  
  def redirect
    redirect_to menu_item_path(:url => params[:url]), status: 301
  end
  
  def archive
    @menu_item= MenuItem.archived.find(params[:id])
  end
  
  
  def show
    
    
    if params[:url]
      
      url = params[:url].split("/")
      url = url.map do |u|
        u.to_slug
      end
      url = url.join("/")
        
      @menu_item = MenuItem.find_by_permalink(url)
    else
    
      @menu_item = MenuItem.find_by_slug("index")
    
    
    end
    
    
    
    if @menu_item
    
      if !@menu_item.enabled and !@menu_item.visible and !current_admin
        render :file => Rails.root+"public/404.html", :layout => false, :status => 404
    
      elsif @menu_item.menu_content_type == "MenuAlias"
        if @menu_item.menu_content.menu_item
          redirect_to menu_item_path(:url => @menu_item.menu_content.menu_item_alias.permalink)
        else
          redirect_to "/404.html"
        end
  
    
      elsif @menu_item.menu_content_type == "Page"
        
        
          @page = @menu_item.menu_content
      
          @title = @page.title
          @description = @page.description
          @keywords = @page.keywords
          
        
      
          
      
      
      end
      
      if !@menu_item.password?
        @auth = true
      else
        if params[:pass] and params[:pass] == @menu_item.password
          session[:auth] = session[:auth] || {}
          session[:auth][@menu_item.id.to_s] = true
          @auth = true
        elsif session[:auth] and session[:auth][@menu_item.id.to_s] == true
          @auth = true
        end
      
  
    
      end
      
      
      
    else
      slug = params[:url].split("/")[-1] if params[:url]
      if slug and Article.exists?(:slug => slug)
        
        redirect_to article_path(:id => slug), status: 301
      else
      redirect_to "/404.html"
      end
    end
    end

end
