forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotification_factory.rb
96 lines (75 loc) · 1.83 KB
/
notification_factory.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
module NotificationFactory
=begin
result = NotificationFactory.template_read(
template: 'password_reset',
locale: 'en-us',
format: 'html',
type: 'mailer',
)
or
result = NotificationFactory.template_read(
template: 'ticket_update',
locale: 'en-us',
format: 'md',
type: 'slack',
)
returns
{
subject: 'some subject',
body: 'some body',
}
=end
def self.template_read(data)
template_subject = nil
template_body = ''
locale = data[:locale] || Setting.get('locale_default') || 'en-us'
template = data[:template]
format = data[:format]
type = data[:type]
root = Rails.root
location = "#{root}/app/views/#{type}/#{template}/#{locale}.#{format}.erb"
# as fallback, use 2 char locale
if !File.exist?(location)
locale = locale[0, 2]
location = "#{root}/app/views/#{type}/#{template}/#{locale}.#{format}.erb"
end
# as fallback, use en
if !File.exist?(location)
location = "#{root}/app/views/#{type}/#{template}/en.#{format}.erb"
end
File.open(location, 'r:UTF-8').each do |line|
if !template_subject
template_subject = line
next
end
template_body += line
end
{
subject: template_subject,
body: template_body,
}
end
=begin
string = NotificationFactory.application_template_read(
format: 'html',
type: 'mailer',
)
or
string = NotificationFactory.application_template_read(
format: 'md',
type: 'slack',
)
returns
'some template'
=end
def self.application_template_read(data)
format = data[:format]
type = data[:type]
root = Rails.root
application_template = nil
File.open("#{root}/app/views/#{type}/application.#{format}.erb", 'r:UTF-8') do |file|
application_template = file.read
end
application_template
end
end