cirandas.net

ref: master

plugins/work_assignment/lib/work_assignment_plugin/email_contact.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class WorkAssignmentPlugin::EmailContact

  include ActiveModel::Validations

  def initialize(attributes = nil)
    if attributes
      attributes.each do |attr,value|
        self.send("#{attr}=", value)
      end
    end
  end

  attr_accessor :name
  attr_accessor :subject
  attr_accessor :message
  attr_accessor :email
  attr_accessor :receive_a_copy
  attr_accessor :sender
  attr_accessor :receiver

  N_('Subject'); N_('Message'); N_('e-Mail'); N_('Name')

  validates_presence_of :receiver, :subject, :message, :sender
  validates_format_of :receiver, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|o| !o.email.blank?})

  def deliver
    return false unless self.valid?
    WorkAssignmentPlugin::EmailContact::EmailSender.notification(self).deliver
  end

  class EmailSender < ApplicationMailer

    def notification(email_contact)
      self.environment = email_contact.sender.environment

      name = email_contact.sender.name
      email = email_contact.sender.email
      message = email_contact.message
      target = email_contact.receiver

      options = {
        content_type: 'text/html',
        to: target,
        reply_to: email,
        subject: email_contact.subject,
        body: message,
        from: "#{email_contact.sender.environment.name} <#{email_contact.sender.environment.contact_email}>",
      }

      mail(options)
    end

    def self.build_mail_message(email_contact, uploaded_files)
      message = ""
      if uploaded_files && uploaded_files.first && uploaded_files.first.parent && uploaded_files.first.parent.parent
        article = uploaded_files.first.parent.parent
        message = article.default_email + "<br>"
        uploaded_files.each do |file|
          url = Rails.application.routes.url_helpers.url_for(file.url)
          message += "<br><a href='#{url}'>#{url}</a>"
        end
      end
      email_contact.message = message
    end
  end
end