Skip to content

Commit

Permalink
Account contacts are showed and refresh in send email, and now you ca…
Browse files Browse the repository at this point in the history
…n send emails from other users emails
  • Loading branch information
jgordor committed May 17, 2010
1 parent b328017 commit 28e5651
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 18 deletions.
52 changes: 42 additions & 10 deletions app/controllers/mailing_mails_controller.rb
Expand Up @@ -11,7 +11,7 @@ def edit

@mailing_mail.mailable.email.blank? ? @recipient_number = 0 : @recipient_number = 1

@users = User.except(@current_user).all
@users = User.all

if params[:previous] =~ /(\d+)\z/
@previous = MailingMail.find($1)
Expand All @@ -28,6 +28,8 @@ def update
@mailing_mail = MailingMail.find(params[:id])
@mailing = Mailing.find(@mailing_mail.mailing_id)
@mailing_sim = Mailing.find(@mailing_mail.mailing_id)

@users = User.all

# Update subject and body
@mailing_sim.subject = params[:mailing_mail][:subject]
Expand All @@ -48,12 +50,15 @@ def update
if @mailing_mail.status == "ready"
# Sending email
begin
new_email = MailingNotifier.create_simple(@current_user, @mailing_mail, @mailing, params[:mailing_mail][:subject], @template.auto_link(@template.simple_format params[:mailing_mail][:body]), @recipients)
new_email = MailingNotifier.create_simple(@current_user, @mailing_mail, @mailing, params[:mailing_mail][:subject], @template.auto_link(@template.simple_format params[:mailing_mail][:body]), @recipients, params[:sent_by_email])

MailingNotifier.deliver(new_email)

# Now saving with status sent, sent_at date and recipients
params[:mailing_mail][:status] = "sent"
params[:mailing_mail][:sent_at] = Time.now.strftime('%Y-%m-%d %H:%M:%S')
params[:mailing_mail][:sent_by_email] = params[:sent_by_email]
params[:mailing_mail][:sent_by] = @current_user.id

if @recipients.empty?
params[:mailing_mail][:recipients] = @mailing_mail.mailable.email
Expand All @@ -64,8 +69,8 @@ def update
# Making a comment on asset
Comment.create(:title => "mailing_id_#{@mailing.id}", :user => @current_user, :commentable => @mailing_mail.mailable, :comment => "#{t(:mail_to, params[:mailing_mail][:recipients])}\n#{t :subject}: #{params[:mailing_mail][:subject]}\n\n#{params[:mailing_mail][:body]}")

respond_to do |format|
if @mailing_mail.update_attributes(params[:mailing_mail])
respond_to do |format|
if @mailing_mail.update_attributes(params[:mailing_mail])
unless params[:next_mail].blank? || params[:next_mail] == "0"
@mail = MailingMail.find(params[:next_mail].to_i)
# Check and transform Transform data from source
Expand All @@ -86,15 +91,17 @@ def update
# Error sending email
flash[:error] = t :error_sendig_mail
#redirect_to(mailings_path)
render :update do |page|
render :update do |page|
page.redirect_to(@mailing)
end
end
else
# Mail has placeholders, return to edit updated
@users = User.except(@current_user).all
@mailing_mail.mailable.email.blank? ? @recipient_number = 0 : @recipient_number = 1
render :action => "edit"
# Mail has placeholders, return to edit updated
#@mailing_mail.mailable.email.blank? ? @recipient_number = 0 : @recipient_number = 1
#render :action => "edit"
respond_to do |format|
format.js { render :template => "mailing_mails/placeholders_pending" }
end
end
rescue ActiveRecord::RecordNotFound
respond_to_not_found(:js, :xml)
Expand All @@ -110,7 +117,7 @@ def confirm
respond_to_not_found(:js, :xml)
end

# GET /mailing_mails/1/add_recipient AJAX
# GET /mailing_mails/1/add_recipient AJAX
#----------------------------------------------------------------------------
def add_recipient
@mailing_mail = MailingMail.find(params[:id])
Expand All @@ -121,6 +128,31 @@ def add_recipient
respond_to_not_found(:js, :xml)
end

# GET /mailing_mails/1/refresh_with_recipient AJAX
# Called from account contact selection, is allways an Account as source
#----------------------------------------------------------------------------
def refresh_with_recipient
unless params[:email].blank?
@mailing_mail = MailingMail.find(params[:id])
@mailing = Mailing.find(@mailing_mail.mailing_id)
@contact_source = Contact.find(:first, :include => :account, :conditions => { "account_contacts.account_id" => @mailing_mail.mailable.id, :email => params[:email] })

# Change the source to simulate the source as the contact selected
mailable = @mailing_mail.mailable
@mailing_mail.mailable = @contact_source
@new_source = Mailing.check_and_update_mail_placeholders(@mailing_mail, @mailing, true)

# Return the original mailable to the account
@mailing_mail.mailable = mailable
@mailing_mail.save!
else
render :nothing => true
end

rescue ActiveRecord::RecordNotFound
respond_to_not_found(:js, :xml)
end

# DELETE /mailing_mails/1
# DELETE /mailing_mails/1.xml AJAX
#----------------------------------------------------------------------------
Expand Down
6 changes: 4 additions & 2 deletions app/models/mailing_notifier.rb
@@ -1,13 +1,15 @@
class MailingNotifier < ActionMailer::Base

def simple(user, mail, mailing, subject, body, mail_recipients)
def simple(user, mail, mailing, subject, body, mail_recipients, mail_from = nil)
mail_from ||= user.email

if mail_recipients && !mail_recipients.empty?
recipients mail_recipients
else
recipients mail.mailable.email
end

from user.email
from mail_from
bcc user.email
subject subject

Expand Down
11 changes: 9 additions & 2 deletions app/views/mailing_mails/_edit.html.haml
Expand Up @@ -21,10 +21,16 @@
%br
= t(:mail_status_ko_info)
- if @mailing_mail.mailable.class.to_s == "Account"
.label= t(:mail_participants_accounts, :from => @current_user.pref[:google_account])
%br
= t(:mail_from) << ":"
= render :partial => "user_selection"
#recipients= render :partial => "account_recipients"
- else
.label= t(:mail_participants, :to => @mailing_mail.mailable.email, :from => @current_user.pref[:google_account])
%br
= t(:mail_from) << ":"
= render :partial => "user_selection"
= t(:to) << ":"
= @mailing_mail.mailable.email
%tr
%td{ :valign => :top, :colspan => 3 }
.label.req #{t :subject}:
Expand All @@ -40,6 +46,7 @@
- else
.label #{t :without_attachment}

#mailing_mail_info
.buttonbar
= f.submit t(:send)
#{t :or}
Expand Down
2 changes: 2 additions & 0 deletions app/views/mailing_mails/_mail.html.haml
Expand Up @@ -17,6 +17,8 @@
.indent
- if mail.status == "sent"
= t(:mailing_sent, :models => t(type), :link => get_asset_link_name(mail.mailable), :to => auto_link(mail.recipients),:time_ago => time_ago_in_words(mail.sent_at), :user => (mail.user.id == @current_user.id ? t(:me) : h(mail.user.full_name)))
- if mail.user.email != mail.sent_by_email
= t(:mailing_sent_by, mail.sent_by_email)
- else
= t(:mailing_from, :models => t(type), :link => get_asset_link(mail.mailable))
%tt
Expand Down
1 change: 1 addition & 0 deletions app/views/mailing_mails/_user_selection.html.haml
@@ -0,0 +1 @@
= select_tag 'sent_by_email', options_for_select(@users.map { |u| [u.email, u.email] }, @current_user.email), { :style => "width:220px" }
3 changes: 3 additions & 0 deletions app/views/mailing_mails/add_recipient.js.rjs
@@ -1,2 +1,5 @@
page.remove "add_email_recipient"
if @recipient_number == 2
page.insert_html :after, 'recipients_1', :text => "<br> Se utilizarán los datos del primer contacto para personalizar las variables del Mensaje"
end
page.insert_html :bottom, "recipients", :partial => "account_recipients"
3 changes: 3 additions & 0 deletions app/views/mailing_mails/placeholders_pending.js.rjs
@@ -0,0 +1,3 @@
page.replace_html "mailing_mail_info", :text => t(:missing_placeholders)
page << "$('mailing_mail_submit').disabled = false"
page.show "mailing_mail_submit"
2 changes: 2 additions & 0 deletions app/views/mailing_mails/refresh_with_recipient.js.rjs
@@ -0,0 +1,2 @@
page << "$('mailing_mail_subject').value = '#{@new_source.subject}'"
page << "$('mailing_mail_body').value = '#{@new_source.body.gsub("\r\n","\\n").gsub("'", "")}'"
5 changes: 4 additions & 1 deletion app/views/mailings/_account_recipients.html.haml
@@ -1,3 +1,6 @@
= select_tag "recipients[#{@recipient_number}]", options_for_select(@mailing_mail.mailable.contacts.map { |c| ["#{c.name} - #{c.email}", c.email] }.insert(0, '').insert(0, ["#{t(:contact_email, @mailing_mail.mailable.name)} - #{@mailing_mail.mailable.email}", @mailing_mail.mailable.email])), { :style => "width:430px" }
- options = options_for_select(@mailing_mail.mailable.contacts.map { |c| ["#{c.name} - #{c.email}", c.email] }.insert(0, '').insert(0, ["#{t(:contact_email, @mailing_mail.mailable.name)} - #{@mailing_mail.mailable.email}", @mailing_mail.mailable.email]))
- onchange_function = @recipient_number == 1 ? remote_function(:url => refresh_with_recipient_mailing_mail_path(@mailing_mail), :method => :get, :with => "'email='+this.options[this.selectedIndex].value") : ""
= t(:to) << ":"
= select_tag "recipients[#{@recipient_number}]", options, { :style => "width:430px", :onchange => onchange_function }
= link_to_remote(t(:add), {:url => add_recipient_mailing_mail_path(@mailing_mail), :method => :get, :with => "{ cancel: Element.visible('#{:recipients}'), related: '#{@recipient_number}' }"}, {:id => "add_email_recipient"})
%br
6 changes: 5 additions & 1 deletion config/locales/en-US_crm_cats.yml
Expand Up @@ -76,4 +76,8 @@ en-US:
contact_email: "Email contact: {{value}}"
mail_to: Email to {{value}}
find_mails: Find emails
mailing_attachment_intro: You can attach a file to the mail
mailing_attachment_intro: You can attach a file to the mail
missing_placeholders: Missing placeholders to fill!
mailing_sent_by: " using the email {{value}}"
to: To
mail_from: From
6 changes: 5 additions & 1 deletion config/locales/es-ES_crm_cats.yml
Expand Up @@ -76,4 +76,8 @@ es-ES:
contact_email: "Email de contacto: {{value}}"
mail_to: Email a {{value}}
find_mails: Buscar emails
mailing_attachment_intro: Permite adjuntar un fichero adjunto al mail
mailing_attachment_intro: Permite adjuntar un fichero adjunto al mail
missing_placeholders: ¡Faltan placeholders por rellenar!
mailing_sent_by: " usando el email {{value}}"
to: A
mail_from: De
2 changes: 1 addition & 1 deletion config/routes.rb
@@ -1,4 +1,4 @@
ActionController::Routing::Routes.draw do |map|
map.resources :mailings, :has_many => :mailing_mails, :collection => { :search => :get, :auto_complete => :post, :options => :get, :options_mails => :get, :redraw => :post, :redraw_mails => :post }, :member => { :confirm => :get, :addmails => :get, :addtomailing => :get, :check => :get, :start => :get, :attachment => :get, :filter => :any }
map.resources :mailing_mails, :collection => { :search => :get, :auto_complete => :post, :options => :get, :redraw => :post }, :member => { :confirm => :get, :add_recipient => :get }
map.resources :mailing_mails, :collection => { :search => :get, :auto_complete => :post, :options => :get, :redraw => :post }, :member => { :confirm => :get, :add_recipient => :get, :refresh_with_recipient => :get }
end
11 changes: 11 additions & 0 deletions db/migrate/20100517145948_add_sent_by_to_mailing_mails.rb
@@ -0,0 +1,11 @@
class AddSentByToMailingMails < ActiveRecord::Migration
def self.up
add_column :mailing_mails, :sent_by, :integer
add_column :mailing_mails, :sent_by_email, :string
end

def self.down
remove_column :mailing_mails, :sent_by
remove_column :mailing_mails, :sent_by_email
end
end

0 comments on commit 28e5651

Please sign in to comment.