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

class Public::ArticlesController < ApplicationController
	
	layout :get_public_layout
	
  def archive
    
  end
  
	def index
    if params[:lang]
      
      @lang = LangSite.find_by_slug(params[:lang])
      
    end
    ids_exclude = [0]
		@article_1_id = SpecificPreference.find_by_key("blog_article_1_id").value if SpecificPreference.find_by_key("blog_article_1_id") and Article.where(:id => SpecificPreference.find_by_key("blog_article_1_id").value).first
    if @article_1_id and @article_1_id.to_i > 0
		  @article_1 = Article.where(:id => SpecificPreference.find_by_key("blog_article_1_id").value).first
      ids_exclude << @article_1.id 
    end
    
 
		@article_2_id = SpecificPreference.find_by_key("blog_article_2_id").value if SpecificPreference.find_by_key("blog_article_2_id") and Article.where(:id => SpecificPreference.find_by_key("blog_article_2_id").value).first
    if @article_2_id and @article_2_id.to_i > 0
		  @article_2 = Article.where(:id => SpecificPreference.find_by_key("blog_article_2_id").value).first
      ids_exclude << @article_2.id 
    end
   
    if @new_site
      params[:category_id] = nil if params[:category_id].to_i == 0
      @category = Category.find(params[:category_id]) if params[:category_id]
  		
      if @category
        @articles_ok = Article.joins(:category_categoryables).where(:category_categoryables => {:category_id => @category.id})
      else
        @articles_ok = Article
    
      end
      @articles_ok = @articles_ok.before(Time.now).recents.joins(:lang_articles).where("lang_articles.enabled = 1 and lang_articles.lang_site_id = ?", @lang.id)
		  
      @articles_ok = @articles_ok.uniq
      @articles_slider = @articles_ok.where("articles.id not in(?)", ids_exclude).limit(3)
      @articles_une = @articles_ok.where("articles.id not in(?)", ids_exclude)
      @articles_une = @articles_une.offset(3).limit(2)
      
      @articles_une[0] = @article_1 if @article_1
      @articles_une[1] = @article_2 if @article_2
      @articles = @articles_ok
      @articles = @articles.page(params[:page]).per(4)
      
      
    else
  		@articles = Article.before(Time.now).recents.joins(:lang_articles).where("lang_articles.enabled = 1 and lang_articles.lang_site_id = ?", @lang.id)
    
      @articles = @articles.page(params[:page]).per(7)
    end
		
		@blog_index = true
		@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(Time.now).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[:tag_id])
	
		
	
  		if @tag		
  			@title = "tag : "+@tag.name.to_s
  			@articles =  @tag.recents_articles
  			
        
  			
  		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(Time.now).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+'"'
		
		redirect_to articles_path(:category_id => @category.id, :anchor => "categories") if @new_site
	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(Time.now).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 <= Time.now )) 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
			
        @comments = @article.comments.where(:enabled => true, :parent_id => nil).order("created_at DESC").page(params[:page]).per(4)
      
      
  		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
