# -*- 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
        redirect_to "/404.html"
    
      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"
        
        
        if @menu_item.password and !session[:auth]
          session[:prev_page] = request.url
          redirect_to new_public_entreprise_path
        else
          @page = @menu_item.menu_content
      
          @title = @page.title
          @description = @page.description
          @keywords = @page.keywords
          
        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
