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

class Public::NewslettersController < ApplicationController
	
	layout "public"
	
	def index
		
		@newsletters = Newsletter.before(Date.today).recents

		#@newsletters = @newsletters.page(params[:page]).per(10)
		
		
		@title = "Newsletters"
	end

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

		# the news items
		@newsletters = Newsletter.before(Date.today).recents

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

		
	end


	
	def category
	
		
	
		
		@category = Category.find_by_slug(params[:slug])
		@newsletters = @category.newsletters.before(Date.today).recents
		@newsletters = @newsletters.page(params[:page]).per(10)
		@title = "Newsletters du blog"
		@index_title = 'Newsletters de la catégorie "'+@category.name+'"'
		render :action => :index
		
	end
	
	def archives
		@month = Date.parse("#{params[:year]}/#{params[:month]}").beginning_of_month 
		@newsletters = Newsletter.where("enabled = ? and published_at > ? and published_at < ?", true, @month, @month.end_of_month ).before(Date.today).recents
		@newsletters = @newsletters.page(params[:page]).per(10000)
		@title = "Newsletters du blog"
		if [4,8].include?(@month.month)
			@index_title = 'Newsletters du mois d\''+l(@month, :format => "%B %Y")+''
		else
			@index_title = 'Newsletters du mois de '+l(@month, :format => "%B %Y")+''
		end
		
		render :action => :index
	end
	
	
	def show
		@newsletter = Newsletter.find_by_slug(params[:id])
	
		if @newsletter	and @newsletter.enabled	
			
			@title = @newsletter.title
	
		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
