send email when admin logs in
[shapado:shapado.git] / app / models / notifier.rb
1 class Notifier < ActionMailer::Base
2   helper :application
3   layout "notifications"
4
5   def give_advice(user, group, question, following = false)
6     scope = "mailers.notifications.give_advice"
7     @language = language_for(user)
8     set_locale @language
9     if following
10       subject I18n.t("friend_subject",
11                      :scope => scope,
12                      :question_title => question.title,
13                      :locale => @language)
14     else
15       subject I18n.t("subject",
16                      :scope => scope,
17                      :question_title => question.title,
18                      :locale => @language)
19     end
20
21     @user = user
22     @question = question
23     @group = group
24     @following = following
25
26     mail(:to => user.email, :from => from_email(group),
27          :subject => @subject,
28          :date => Time.now) do |format|
29       format.text
30       format.html
31     end
32   end
33
34   def new_answer(user, group, answer, following = false)
35     scope = "mailers.notifications.new_answer"
36     @language = language_for(user)
37     set_locale @language
38     if user == answer.question.user
39       @subject = I18n.t("subject_owner", :scope => scope,
40                         :title => answer.question.title,
41                         :login => answer.user.login,
42                         :locale => @language)
43     elsif following
44       @subject = I18n.t("subject_friend", :scope => scope,
45                         :title => answer.question.title,
46                         :login => answer.user.login,
47                         :locale => @language)
48     else
49         @subject = I18n.t("subject_other", :scope => scope,
50                           :title => answer.question.title,
51                           :login => answer.user.login,
52                           :locale => @language)
53     end
54     @user = user
55     @answer = answer
56     @question = answer.question
57     @group = group
58     mail(:to => user.email, :from => from_email(group),
59          :subject => @subject, :date => Time.now) do |format|
60       format.text
61       format.html
62     end
63   end
64
65   def new_comment(group, comment, user, question)
66     @user = user
67     @comment = comment
68     @question = question
69     @group = group
70     @language = language_for(user)
71     set_locale @language
72     mail(:to => user.email, :from => from_email(group),
73          :subject => I18n.t("mailers.notifications.new_comment.subject",
74                             :login => comment.user.login,
75                             :group => group.name, :locale => @language),
76          :date => Time.now) do |format|
77       format.text
78       format.html
79     end
80   end
81
82   def new_feedback(user, subject, content, email, ip)
83     @user = user
84     @subject = subject
85     @content = content
86     @email = email
87     @ip = ip
88     @language = language_for(user)
89     set_locale @language
90     mail(:to => AppConfig.exception_notification["exception_recipients"],
91          :from => "Shapado[feedback] <#{AppConfig.notification_email}>",
92          :subject => "feedback: #{subject}",
93          :date => Time.now) do |format|
94       format.text
95     end
96   end
97
98   def admin_login(ip, user_id)
99     @admin = User.find(user_id)
100     @language = language_for
101     set_locale @language
102     @subject =  I18n.t("mailers.notifications.admin_login.subject",
103                        :locale => @language)
104     @ip = ip
105     mail(:to => AppConfig.exception_notification["exception_recipients"],
106          :from => "Shapado[feedback] <#{AppConfig.notification_email}>",
107          :subject => @subject,
108          :date => Time.now) do |format|
109       format.text
110     end
111   end
112
113   def follow(user, followed, group)
114     @user = user
115     @followed = followed
116     @group = group
117     @language = language_for(user)
118     set_locale @language
119     mail(:to => followed.email ,
120          :from => from_email(group),
121          :subject => I18n.t("mailers.notifications.follow.subject",
122                             :login => user.login,
123                             :app => group.name, :locale => @language),
124          :date => Time.now) do |format|
125       format.text
126       format.html
127     end
128   end
129
130   def earned_badge(user, group, badge)
131     @user = user
132     @group = group
133     @badge = badge
134     @language = language_for(user)
135     set_locale @language
136     mail(:to => user.email ,
137          :from => from_email(group),
138          :subject => I18n.t("mailers.notifications.earned_badge.subject",
139                             :group => group.name, :locale => @language),
140          :date => Time.now) do |format|
141       format.text
142       format.html
143     end
144   end
145
146   def created_flag(user, group, reason)
147     @user = user
148     @group = group
149     @@language = language_for(user)
150     set_locale @language
151     @reason = I18n.t(reason, :scope=>"flags.form", :locale => @language)
152     mail(:to => user.email ,
153          :from => from_email(group),
154          :subject => I18n.t("mailers.notifications.created_flag.subject",
155                             :group => group.name, :locale => @language),
156          :date => Time.now) do |format|
157       format.text
158       format.html
159     end
160   end
161
162   def favorited(user, group, answer)
163     @user = user
164     @group = group
165     @question = answer.question
166     @answer = answer
167     @language = language_for(@question.user)
168     set_locale @language
169     mail(:to => @question.user.email,
170          :from => from_email(group),
171          :subject => I18n.t("mailers.notifications.favorited.subject",
172                             :login => user.login, :locale => @language),
173          :date => Time.now) do |format|
174       format.text
175       format.html
176     end
177   end
178
179   def report(user, report)
180     @user = user
181     @report = report
182     @group = report.group
183     @language = language_for(user)
184     set_locale @language
185     mail(:to => user.email,
186          :from => from_email(@group),
187          :subject => I18n.t("mailers.notifications.report.subject",
188                      :group => report.group.name,
189                      :app => AppConfig.application_name, :locale => @language),
190          :date => Time.now) do |format|
191       format.text
192     end
193   end
194
195   private
196   def initialize_defaults(method_name)
197     super
198     @method_name = method_name
199   end
200
201   def from_email(group)
202     "#{group ? group.name : AppConfig.application_name} <notifications@#{ActionMailer::Base.default_url_options[:host]}>"
203   end
204
205   def language_for(user=nil)
206     @language = if user && user.language
207                  @language = user.language
208                else
209                  I18n.locale
210                end
211   end
212 end