liquid_feedback_frontend
view model/event.lua @ 631:a9fd951b67bb
Updated englisch, esperanto and french translation file for new strings
author | bsw |
---|---|
date | Mon Jun 25 16:50:30 2012 +0200 (2012-06-25) |
parents | d22125d054aa |
children | 0daedb7c1c11 |
line source
1 Event = mondelefant.new_class()
2 Event.table = 'event'
4 Event:add_reference{
5 mode = 'm1',
6 to = "Issue",
7 this_key = 'issue_id',
8 that_key = 'id',
9 ref = 'issue',
10 }
12 Event:add_reference{
13 mode = 'm1',
14 to = "Suggestion",
15 this_key = 'suggestion_id',
16 that_key = 'id',
17 ref = 'suggestion',
18 }
20 Event:add_reference{
21 mode = 'm1',
22 to = "Member",
23 this_key = 'member_id',
24 that_key = 'id',
25 ref = 'member',
26 }
28 function Event.object_get:event_name()
29 return ({
30 issue_state_changed = _"Issue reached next phase",
31 initiative_created_in_new_issue = _"New issue",
32 initiative_created_in_existing_issue = _"New initiative",
33 initiative_revoked = _"Initiative revoked",
34 new_draft_created = _"New initiative draft",
35 suggestion_created = _"New suggestion"
36 })[self.event]
37 end
39 function Event.object_get:state_name()
40 return ({
41 admission = _"New",
42 discussion = _"Discussion",
43 verification = _"Frozen",
44 voting = _"Voting",
45 canceled_revoked_before_accepted = _"Cancelled (before accepted due to revocation)",
46 canceled_issue_not_accepted = _"Cancelled (issue not accepted)",
47 canceled_after_revocation_during_discussion = _"Cancelled (during discussion due to revocation)",
48 canceled_after_revocation_during_verification = _"Cancelled (during verification due to revocation)",
49 calculation = _"Calculation",
50 canceled_no_initiative_admitted = _"Cancelled (no initiative admitted)",
51 finished_without_winner = _"Finished (without winner)",
52 finished_with_winner = _"Finished (with winner)"
53 })[self.state]
54 end
56 function Event.object:send_notification()
58 local members_to_notify = Member:new_selector()
59 :join("event_seen_by_member", nil, { "event_seen_by_member.seen_by_member_id = member.id AND event_seen_by_member.notify_level <= member.notify_level AND event_seen_by_member.id = ?", self.id } )
60 :add_where("member.activated NOTNULL AND member.notify_email NOTNULL")
61 -- SAFETY FIRST, NEVER send notifications for events more then 7 days in past or future
62 :add_where("now() - event_seen_by_member.occurrence BETWEEN '-7 days'::interval AND '7 days'::interval")
63 :exec()
65 print (_("Event #{id} -> #{num} members", { id = self.id, num = #members_to_notify }))
68 local url
70 for i, member in ipairs(members_to_notify) do
71 local subject
72 local body = ""
74 locale.do_with(
75 { lang = member.lang or config.default_lang or 'en' },
76 function()
77 subject = config.mail_subject_prefix .. " " .. self.event_name
78 body = body .. _("[event mail] Unit: #{name}", { name = self.issue.area.unit.name }) .. "\n"
79 body = body .. _("[event mail] Area: #{name}", { name = self.issue.area.name }) .. "\n"
80 body = body .. _("[event mail] Issue: ##{id}", { id = self.issue_id }) .. "\n\n"
81 body = body .. _("[event mail] Policy: #{policy}", { policy = self.issue.policy.name }) .. "\n\n"
82 body = body .. _("[event mail] Event: #{event}", { event = self.event_name }) .. "\n\n"
83 body = body .. _("[event mail] Phase: #{phase}", { phase = self.state_name }) .. "\n\n"
85 if self.initiative_id then
86 url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html"
87 elseif self.suggestion_id then
88 url = request.get_absolute_baseurl() .. "suggestion/show/" .. self.suggestion_id .. ".html"
89 else
90 url = request.get_absolute_baseurl() .. "issue/show/" .. self.issue_id .. ".html"
91 end
93 body = body .. _("[event mail] URL: #{url}", { url = url }) .. "\n\n"
95 if self.initiative_id then
96 local initiative = Initiative:by_id(self.initiative_id)
97 body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n\n"
98 else
99 local initiative_count = Initiative:new_selector()
100 :add_where{ "initiative.issue_id = ?", self.issue_id }
101 :count()
102 local initiatives = Initiative:new_selector()
103 :add_where{ "initiative.issue_id = ?", self.issue_id }
104 :add_order_by("initiative.supporter_count DESC")
105 :limit(3)
106 :exec()
107 for i, initiative in ipairs(initiatives) do
108 body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n"
109 end
110 if initiative_count - 3 > 0 then
111 body = body .. _("and #{count} more initiatives", { count = initiative_count }) .. "\n"
112 end
113 body = body .. "\n"
114 end
116 if self.suggestion_id then
117 local suggestion = Suggestion:by_id(self.suggestion_id)
118 body = body .. _("#{name}\n\n", { name = suggestion.name })
119 end
121 local success = net.send_mail{
122 envelope_from = config.mail_envelope_from,
123 from = config.mail_from,
124 reply_to = config.mail_reply_to,
125 to = member.notify_email,
126 subject = subject,
127 content_type = "text/plain; charset=UTF-8",
128 content = body
129 }
131 end
132 )
133 end
135 end
137 function Event:send_next_notification()
139 local notification_sent = NotificationSent:new_selector()
140 :optional_object_mode()
141 :for_update()
142 :exec()
144 local last_event_id = 0
145 if notification_sent then
146 last_event_id = notification_sent.event_id
147 end
149 local event = Event:new_selector()
150 :add_where{ "event.id > ?", last_event_id }
151 :add_order_by("event.id")
152 :limit(1)
153 :optional_object_mode()
154 :exec()
156 if event then
157 if last_event_id == 0 then
158 db:query{ "INSERT INTO notification_sent (event_id) VALUES (?)", event.id }
159 else
160 db:query{ "UPDATE notification_sent SET event_id = ?", event.id }
161 end
163 event:send_notification()
165 return true
167 end
169 end
171 function Event:send_notifications_loop()
173 while true do
174 local did_work = Event:send_next_notification()
175 if not did_work then
176 print "Sleeping 1 second"
177 os.execute("sleep 1")
178 end
179 end
181 end