# -*- encoding : utf-8 -*-

class Public::ArticlesController < ApplicationController
	
	layout "public"
	
  def archive
    
  end
  
	def index
    if params[:lang]
      
      @lang = LangSite.find_by_slug(params[:lang])
      
    end
    
		
		@articles = Article.before(Date.today).recents.joins(:lang_articles).where("lang_articles.enabled = 1 and lang_articles.lang_site_id = ?", @lang.id)

		@articles = @articles.page(params[:page]).per(7)
		
		
		@title = "Articles du blog"
	end

	def feed
		# this will be the name of the feed displayed on the feed reader
		@title = ""

		# the news items
		@articles = Article.before(Date.today).recents

		# this will be our Feed's update timestamp
		@updated = @articles.first.updated_at unless @articles.empty?

		
	end


	
  	def tags
  		@tag = Tag.find_by_slug(params[:id])
	
		
	
  		if @tag		
  			@title = "tag : "+@tag.name.to_s
  			@articles =  Kaminari.paginate_array(@tag.recents_articles).page(params[:page]).per(3)
  			
        
  			render :template => "public/articles/index"
  		else
  			redirect_to "/", :notice => "La page que vous demandez n'a pas pu être trouvée.<br /><br />Vous avez donc été redirigé sur notre page d'accueil"
			
  		end
    	end

	

	
	def category
	

		@category_lang = CategoryLang.where(:lang_site_id => @lang.id).find_by_slug(params[:slug])
		@category = @category_lang.category
    
    
		@articles = @category.articles.before(Date.today).recents.joins(:lang_articles).where("lang_articles.enabled = 1 and lang_articles.lang_site_id = ?", @lang.id)
    
    
		@articles = @articles.page(params[:page]).per(7)
		@title = "Articles du blog"
		@index_title = 'Articles de la catégorie "'+@category.name+'"'
		
		
	end
	
	def archives
    if params[:lang]
      
      @lang = LangSite.find_by_slug(params[:lang])
      
    end
    
    
		@month = Time.parse("#{params[:year]}/#{params[:month]}/01 00:00").beginning_of_month 
		@articles = Article.recents.where("articles.published_at >= ? and articles.published_at < ?", @month, (@month + 1.month) ).before(Date.today).joins(:lang_articles).where("lang_articles.enabled = 1 and lang_articles.lang_site_id = ?", @lang.id)
    
		@articles = @articles.page(params[:page]).per(7)
		@title = "Articles du blog"
		if [4,8].include?(@month.month)
			@index_title = 'Articles du mois d\''+l(@month, :format => "%B %Y")+''
		else
			@index_title = 'Articles du mois de '+l(@month, :format => "%B %Y")+''
		end
		
		
	end
	
	
	def show
    if params[:lang]
      
      @lang = LangSite.find_by_slug(params[:lang])
      
    end
    @lang_article = LangArticle.where(:lang_site_id => @lang.id).find_by_slug(params[:slug])
    @article = @lang_article.article
    @facebook_title = @lang_article.fb_title if @lang_article.fb_title?
  	if @article and ((@lang_article.enabled and ( !@article.published_at?  or @article.published_at <= Date.today )) or current_admin)
      
	
  
			
  			@title = @lang_article.title
        @description = @lang_article.description
  			@thumbnail_image = @article.image_file.file.large.medium.small.thumb.url if @article.image_file
			
  		else
  			redirect_to "/", :notice => "La page que vous demandez n'a pas pu être trouvée.<br /><br />Vous avez donc été redirigé sur notre page d'accueil"
			
    
    end
    
  end

	
end
