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

class Admin::ArticlesController < ApplicationController

	before_filter :auth_admin, :except => [:rss, :html]
	
	layout "admin"
	


	before_filter :find_articles


  def rss
    if params[:breve]
      @articles = Article.before(Time.now).recents.where("category_id != ?", params[:cat_id]).joins(:lang_articles).where("lang_articles.enabled = 1 and lang_articles.lang_site_id = ?", @lang.id)
    else
    
      @articles = Article.before(Time.now).recents.where("category_id = ?", params[:cat_id]).joins(:lang_articles).where("lang_articles.enabled = 1 and lang_articles.lang_site_id = ?", @lang.id)
    end
    
    return_json_a = []
    
    @articles.each do |article|
      return_json = {}
    
   
    
      return_json[:category_id] = article.category_id
      return_json[:title] = article.lang(@lang.slug).title
      return_json[:description] = article.lang(@lang.slug).description
      return_json[:slug] = article.lang(@lang.slug).slug
      return_json[:id] = article.id
      return_json[:date] = article.created_at.to_s(:rfc822)
      return_json[:html] = article.lang(@lang.slug).html
      return_json[:url] = article_path(:slug => article.lang(@lang.slug).slug, :lang => @lang.slug)
      if article.image_file
        return_json[:image_url] = article.image_file.file.large.medium.url
      end
      return_json_a << return_json
    end
    render :inline => return_json_a.to_json
    
  end
  def html
    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
    
    
    
    article = @article
    return_json = {}
  
 
  
    return_json[:category_id] = article.category_id
    return_json[:title] = article.lang(@lang.slug).title
    return_json[:description] = article.lang(@lang.slug).description
    return_json[:slug] = article.lang(@lang.slug).slug
    return_json[:id] = article.id
    return_json[:date] = article.created_at.to_s(:rfc822)
    return_json[:html] = article.lang(@lang.slug).html
    return_json[:url] = article_path(:slug => article.lang(@lang.slug).slug, :lang => @lang.slug)
    if article.image_file
      return_json[:image_url] = article.image_file.file.large.medium.url
    end
    
    
    render :inline => return_json.to_json
  end

	def index 

	end


	def cible
		
		render :layout => false
		
	end
	
	
	def new
	
		@article = Article.new(:published_at => Time.now)
    
    
    LangSite.all.each do |ls|
      @article.lang_articles << LangArticle.new(:lang_site_id => ls.id)
    end

	end


	def edit
	
    params[:lang] = params[:lang] || "fr"
    
    @lang = LangSite.find_by_slug(params[:lang])
  
		@article = Article.find(params[:id])
    
		
	end
  
	def edit_menu_items
	
    params[:lang] = params[:lang] || "fr"
    
    @lang = LangSite.find_by_slug(params[:lang])
  
		@article = Article.find(params[:id])
    
		
	end


	def create
		
		@article = Article.new(article_params)

		
		if @article.save
			flash[:notice] = "L'article à été ajouté avec succès."
			self.find_articles
		
		else
			render :action => "new" 

		end

	end


	def update
	
		@article = Article.find(params[:id])
	
  
    if params[:article]
  		if @article.update_attributes(article_params)
  			flash[:notice] = "L'article à été modifié avec succès."
  		else
  			render :action => "edit"
  		end
      
      
    elsif params[:tag_id]
      @article.tag_by_tag_ids(params[:tag_id])
    end
    
  
  
		
	end


	def destroy
	
		@article = Article.find(params[:id])
		@article.destroy

	end
	
	protected
	
	def find_articles
		date_regex = /^(0[1-9]|[12][0-9]|3[01])[\/](0[1-9]|1[012])[\/](19|20)\d\d$/i 
		if params[:start] and params[:start] =~ date_regex
			start = Date.parse(params[:start]).beginning_of_day 
			params[:start]= start.strftime('%d/%m/%Y')
		else
			params[:start] = ""
		end
		if params[:stop] and params[:stop] =~ date_regex
			stop = Date.parse(params[:stop]).end_of_day 
			params[:stop]= stop.strftime('%d/%m/%Y')
		 else
		 	params[:stop] = ""
		end
		
		@articles = Article.order('published_at DESC')
		@articles = @articles.after(start) if start
		@articles = @articles.before(stop) if stop
		per_page = (params[:per_page] and params[:per_page] != "") ? params[:per_page] : 5000
		page = (params[:page] and params[:page] != "") ? params[:page] : 1 
		@articles = @articles.page(page).per(per_page)

		#@articles = Event.order('start_at, stop_at').after(start).before(stop)
	end
	
  private
    def article_params
      params.require(:article).permit!
      
  
      
      
    end

  
end
