| rev | line source | 
| bsw@286 | 1 Event = mondelefant.new_class() | 
| bsw@286 | 2 Event.table = 'event' | 
| bsw@286 | 3 | 
| bsw@286 | 4 Event:add_reference{ | 
| bsw@286 | 5   mode          = 'm1', | 
| bsw@286 | 6   to            = "Issue", | 
| bsw@286 | 7   this_key      = 'issue_id', | 
| bsw@286 | 8   that_key      = 'id', | 
| bsw@286 | 9   ref           = 'issue', | 
| bsw@287 | 10 } | 
| bsw@287 | 11 | 
| bsw@287 | 12 function Event.object:send_notification() | 
| bsw@287 | 13 | 
| bsw@287 | 14   local url | 
| bsw@287 | 15 | 
| bsw@287 | 16   local body = "" | 
| bsw@287 | 17 | 
| bsw@287 | 18   body = body .. _("      Unit: #{name}\n", { name = self.issue.area.unit.name }) | 
| bsw@287 | 19   body = body .. _("      Area: #{name}\n", { name = self.issue.area.name }) | 
| bsw@287 | 20   body = body .. _("     Issue: ##{id}\n", { id = self.issue_id }) | 
| bsw@287 | 21   body = body .. _("    Policy: #{phase}\n", { phase = self.issue.policy.name }) | 
| bsw@287 | 22   body = body .. _("     Phase: #{phase}\n\n", { phase = self.state }) | 
| bsw@287 | 23   body = body .. _("     Event: #{event}\n\n", { event = self.event }) | 
| bsw@287 | 24 | 
| bsw@287 | 25   if self.initiative_id then | 
| bsw@287 | 26     url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html" | 
| bsw@287 | 27   elseif self.suggestion_id then | 
| bsw@287 | 28     url = request.get_absolute_baseurl() .. "suggestion/show/" .. self.suggestion_id .. ".html" | 
| bsw@287 | 29   else | 
| bsw@287 | 30     url = request.get_absolute_baseurl() .. "issue/show/" .. self.issue_id .. ".html" | 
| bsw@287 | 31   end | 
| bsw@287 | 32 | 
| bsw@287 | 33   body = body .. _("       URL: #{url}\n\n", { url = url }) | 
| bsw@287 | 34 | 
| bsw@287 | 35   if self.initiative_id then | 
| bsw@287 | 36     local initiative = Initiative:by_id(self.initiative_id) | 
| bsw@287 | 37     body = body .. _("i#{id}: #{name}\n\n", { id = initiative.id, name = initiative.name }) | 
| bsw@287 | 38   else | 
| bsw@287 | 39     local initiative_count = Initiative:new_selector() | 
| bsw@287 | 40       :add_where{ "initiative.issue_id = ?", self.issue_id } | 
| bsw@287 | 41       :count() | 
| bsw@287 | 42     local initiatives = Initiative:new_selector() | 
| bsw@287 | 43       :add_where{ "initiative.issue_id = ?", self.issue_id } | 
| bsw@287 | 44       :add_order_by("initiative.supporter_count DESC") | 
| bsw@287 | 45       :limit(3) | 
| bsw@287 | 46       :exec() | 
| bsw@287 | 47     for i, initiative in ipairs(initiatives) do | 
| bsw@287 | 48       body = body .. _("i#{id}: #{name}\n", { id = initiative.id, name = initiative.name }) | 
| bsw@287 | 49     end | 
| bsw@287 | 50     if initiative_count - 3 > 0 then | 
| bsw@287 | 51       body = body .. _("and #{count} more initiatives\n", { count = initiative_count }) | 
| bsw@287 | 52     end | 
| bsw@287 | 53     body = body .. "\n" | 
| bsw@287 | 54   end | 
| bsw@287 | 55 | 
| bsw@287 | 56   if self.suggestion_id then | 
| bsw@287 | 57     local suggestion = Suggestion:by_id(self.suggestion_id) | 
| bsw@287 | 58     body = body .. _("#{name}\n\n", { name = suggestion.name }) | 
| bsw@287 | 59   end | 
| bsw@287 | 60 | 
| bsw@287 | 61   slot.put("<pre>", encode.html_newlines(body), "</pre>") | 
| bsw@287 | 62   slot.put("<hr />") | 
| bsw@287 | 63 end | 
| bsw@287 | 64 | 
| bsw@287 | 65 function Event:send_next_notification(last_event_id) | 
| bsw@287 | 66 | 
| bsw@287 | 67   local event = Event:new_selector() | 
| bsw@287 | 68     :add_where{ "event.id > ?", last_id } | 
| bsw@287 | 69     :add_order_by("event.id") | 
| bsw@287 | 70     :limit(1) | 
| bsw@287 | 71     :optional_object_mode() | 
| bsw@287 | 72     :exec() | 
| bsw@287 | 73 | 
| bsw@287 | 74   last_id = nil | 
| bsw@287 | 75   if event then | 
| bsw@287 | 76     last_id = event.id | 
| bsw@287 | 77     local members_to_notify = Member:new_selector() | 
| bsw@287 | 78       :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 = ?", event.id } ) | 
| bsw@287 | 79       :add_where("member.activated NOTNULL AND member.notify_email NOTNULL") | 
| bsw@287 | 80       :exec() | 
| bsw@287 | 81 | 
| bsw@287 | 82     ui.container{ content = _("Event #{id} -> #{num} members", { id = event.id, num = #members_to_notify }) } | 
| bsw@287 | 83 | 
| bsw@287 | 84     event:send_notification() | 
| bsw@287 | 85 | 
| bsw@287 | 86     return event.id | 
| bsw@287 | 87 | 
| bsw@287 | 88   else | 
| bsw@287 | 89     return last_event_id | 
| bsw@287 | 90 | 
| bsw@287 | 91   end | 
| bsw@287 | 92 | 
| bsw@287 | 93 end |