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

class NewslettersController < ApplicationController
  layout "admin"
  


  
#  require_permission 'newsletter.show'
#  require_permission 'newsletter.add', :only => [:newsletter_new, :newsletter_create]
#  require_permission 'newsletter.edit', :only => [:newsletter_edit, :newsletter_update]
#  require_permission 'newsletter.delete', :only => [:newsletter_del]
#  require_permission 'newsletter.send', :only => [:send_test, :send_newsletter, :select_recipients]
  
  def index
    redirect_to :action => :newsletter_list
  end
  
  def newsletter_new
    @h2 = "Creer une newsletter"
    @newsletter = Newsletter.new
  end

  def newsletter_create
    @h2 = "Creer une newsletter"
    
    @newsletter = Newsletter.new(params[:newsletter])
  
    if @newsletter.save
      to_my_log("Ajout de la newsletter "+@newsletter.id.to_s)
      flash[:notice]= t('rest.create_message')
      redirect_to :action => :newsletter_list
    else
      render :action => :newsletter_new
    end
  end

  def newsletter_edit
    @h2 = "Editer une newsletter"
    @newsletter = Newsletter.find(params[:id])
  end

  def newsletter_update
    @h2 = "Editer une newsletter"
    @newsletter = Newsletter.find(params[:id])
    if @newsletter.update_attributes(params[:newsletter])
      flash[:notice]= "La newsletter à été mise à jour"
      to_my_log("Modification de la newsletter "+@newsletter.id.to_s)
      redirect_to :action => :newsletter_list
    else
      render :action => :newsletter_edit
    end
  end

  def newsletter_list
     @h2 = "Lister les newsletters éxistantes"
    @newsletter= Newsletter.find(:all, :order => "created_at DESC")
  end
  
  def newsletter_del
    @newsletter = Newsletter.find(params[:id])
    @newsletter.destroy
    to_my_log("Suppression de la newsletter "+@newsletter.id.to_s)
    flash[:notice]= "La newsletter à été supprimé avec succès"
    redirect_to :action => :newsletter_list
    
  end

  
  def send_test
    
    @newsletter = Newsletter.find(params[:id])
    Newsmailer.deliver_newsletter(@newsletter, params[:email]) 
    
    to_my_log("Envoi de la newsletter "+@newsletter.id.to_s+" en test")
    
    
    flash[:notice]= "La newsletter a bien été envoyée à l'email de test"
    redirect_to :action => :newsletter_list
  end
  

  
  def send_newsletter
    
    @newsletter = Newsletter.find(params[:id])
    if params[:confirmed_groups]
      PeopleNewsgroup.all(:conditions => {:newsgroup_id => params[:confirmed_groups]}).each do |pn|
        p =pn.person
        Newsmailer.deliver_newsletter(@newsletter, p.email) if p.email and p.email != "" and p.newsletter
      end
    elsif params[:confirmed_sheets]
      
      

      @sheets = Sheet.find(:all, :conditions => {:id => params[:confirmed_sheets]})
      
      @persons = Person.find(:all, :conditions => {:sheet_id => params[:confirmed_sheets], :newsletter => true})
      
      @persons.each do |p|
        Newsmailer.deliver_newsletter(@newsletter, p.email) if p.email and p.email != ""
      end
      
      @sheets.each do |s|
        Newsmailer.deliver_newsletter(@newsletter, s.other_mail) if s.other_mail and s.other_mail != ""
      end

      
    end
    
    to_my_log("Envoi de la newsletter "+@newsletter.id.to_s+" à une selection d'adresses mail")
    flash[:notice]= "La newsletter a bien été envoyée à toutes les adresses mail séléctionnée"
    redirect_to :action => :newsletter_list
    
  end
  
  def select_recipients
    @newsletter = Newsletter.find(params[:id])
    if params[:sheet_filter]
      @sheets =  Sheet.search_by_params(params[:sheet_filter])
    
    end
    
    respond_to do |format|
      format.html # index.html.erb
      format.js { render(:update){|page| page.replace_html 'sheets_selecteds', :partial => "sheets_selecteds", :object => @sheets} }
    end
    
      
  end
  


end
