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

class Admin::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
    @h2 = "Lister les newsletters éxistantes"
   @newsletter= Newsletter.find(:all, :order => "created_at DESC")
  end
  
  def new
    @h2 = "Creer une newsletter"
    @newsletter = Newsletter.new
  end

  def create
    @h2 = "Creer une newsletter"
    
    @newsletter = Newsletter.new(params[:newsletter])
  
    if @newsletter.save
      
      redirect_to :action => :index
    else
      render :action => :new
    end
  end

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

  def update
    @h2 = "Editer une newsletter"
    @newsletter = Newsletter.find(params[:id])
    if @newsletter.update_attributes(params[:newsletter])
      flash[:notice]= "La newsletter à été mise à jour"
      redirect_to :action => :index
    else
      render :action => :edit
    end
  end

  def show
    @newsletter = Newsletter.find(params[:id])
    @options = {}
    render :layout => "mail"
  end
  
  def destroy
    @newsletter = Newsletter.find(params[:id])
    @newsletter.destroy
    flash[:notice]= "La newsletter à été supprimé avec succès"
    redirect_to :action => :index
    
  end

  
  def send_test
    
    @newsletter = Newsletter.find(params[:id])
    NewsletterMails.newsletter(@newsletter, params[:email]).deliver
    
    
    
    flash[:notice]= "La newsletter a bien été envoyée à l'email de test"
    redirect_to :action => :index
  end
  
  
  def send_helper(newsletter, email, options = {})
    begin
      NewsletterMails.newsletter(newsletter, email, options).deliver if email and email != "" 
    rescue
      @errors << "<span class'error'>#{p.email}</span>"
    end
  end

  
  def send_newsletter
    @errors = []
    @newsletter = Newsletter.find(params[:id])
    if params[:confirmed_groups]
      PeopleNewsgroup.all(:conditions => {:newsgroup_id => params[:confirmed_groups]}).each do |pn|
        if pn.person
          p =pn.person
          
          send_helper(@newsletter, p.email, :people_newsgroup => pn, :person => p, :sheet => p.sheet) if p.newsletter
        
        end
      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|
        send_helper(@newsletter, p.email, :person => p, :sheet => p.sheet) 
      end
      
      @sheets.each do |s|
       send_helper(@newsletter, s.other_mail, :sheet => s) 
     end

      
    end
    
    
    flash[:notice]= "La newsletter a bien été envoyée à toutes les adresses mail séléctionnée"
    redirect_to :action => :index
    
  end
  
  def select_recipients
    @newsletter = Newsletter.find(params[:id])
    if params[:sheet_filter]
      @sheets =  Sheet.search_by_params(params[:sheet_filter])
    
    end
    
 
    
      
  end
  


end
