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

class Public::ArticlesController < ApplicationController
	
	layout "public"
	
  def archive
    
  end
  
	def index

      @lang = LangSite.first
      

    
		
		@articles = Article.before(Date.today).recents.joins(:lang_articles).where("lang_articles.enabled = 1 and lang_articles.lang_site_id = 1")

		@articles = @articles.page(params[:page]).per(11)
		
		
		@title = "Actualités"
	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 = Category.find_by_slug(params[:slug])
		@articles = @category.articles.before(Date.today).recents
		@articles = @articles.page(params[:page]).per(3)
		@title = "Articles du blog"
		@index_title = 'Articles de la catégorie "'+@category.name+'"'
		render :action => :index
		
	end
	
	def archives
		@month = Date.parse("#{params[:year]}/#{params[:month]}").beginning_of_month 
		@articles = Article.recents.where("enabled = ? and published_at > ? and published_at < ?", true, @month, @month.end_of_month ).before(Date.today)
		@articles = @articles.page(params[:page]).per(3)
		@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
		
		render :action => :index
	end
	
	
	def show

      
      @lang = LangSite.find_by_slug("fr")
      
      
    @lang_article = LangArticle.where(:lang_site_id => @lang.id).find_by_slug(params[:id])
		@article = @lang_article.article
	
		if @article
			
			@title = @lang_article.title
			@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
