liquid_feedback_frontend
view model/event.lua @ 410:7efe31566c11
Added missing translation to event messages
| author | bsw | 
|---|---|
| date | Thu Mar 08 16:30:24 2012 +0100 (2012-03-08) | 
| parents | dac54851083d | 
| children | 699b9fa7bc36 | 
 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 function Event.object_get:event_name()
    13   return ({
    14     issue_state_changed = _"Issue reached next phase",
    15     initiative_created_in_new_issue = _"New issue",
    16     initiative_created_in_existing_issue = _"New initiative in existing issue",
    17     initiative_revoked = _"Initiative revoked",
    18     new_draft_created = _"New initiative draft",
    19     suggestion_created = _"New suggestion"
    20   })[self.event]
    21 end
    23 function Event.object_get:state_name()
    24   return ({
    25     admission = _"New",
    26     discussion = _"Discussion",
    27     verification = _"Frozen",
    28     voting = _"Voting",
    29     canceled_revoked_before_accepted = _"Cancelled (before accepted due to revocation)",
    30     canceled_issue_not_accepted = _"Cancelled (issue not accepted)",
    31     canceled_after_revocation_during_discussion = _"Cancelled (during discussion due to revocation)",
    32     canceled_after_revocation_during_verification = _"Cancelled (during verification due to revocation)",
    33     calculation = _"Calculation",
    34     canceled_no_initiative_admitted = _"Cancelled (no initiative admitted)",
    35     finished_without_winner = _"Finished (without winner)",
    36     finished_with_winner = _"Finished (with winner)"
    37   })[self.state]
    38 end
    40 function Event.object:send_notification() 
    42   local members_to_notify = Member:new_selector()
    43     :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 } )
    44     :add_where("member.activated NOTNULL AND member.notify_email NOTNULL")
    45     :exec()
    47   print (_("Event #{id} -> #{num} members", { id = self.id, num = #members_to_notify }))
    50   local url
    52   for i, member in ipairs(members_to_notify) do
    53     local subject
    54     local body = ""
    56     locale.do_with(
    57       { lang = member.lang or config.default_lang or 'en' },
    58       function()
    59         subject = config.mail_subject_prefix .. " " .. self.event_name
    60         body = body .. _("[event mail]      Unit: #{name}", { name = self.issue.area.unit.name }) .. "\n"
    61         body = body .. _("[event mail]      Area: #{name}", { name = self.issue.area.name }) .. "\n"
    62         body = body .. _("[event mail]     Issue: ##{id}", { id = self.issue_id }) .. "\n\n"
    63         body = body .. _("[event mail]    Policy: #{policy}", { policy = self.issue.policy.name }) .. "\n\n"
    64         body = body .. _("[event mail]     Event: #{event}", { event = self.event_name }) .. "\n\n"
    65         body = body .. _("[event mail]     Phase: #{phase}", { phase = self.state_name }) .. "\n\n"
    67         if self.initiative_id then
    68           url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html"
    69         elseif self.suggestion_id then
    70           url = request.get_absolute_baseurl() .. "suggestion/show/" .. self.suggestion_id .. ".html"
    71         else
    72           url = request.get_absolute_baseurl() .. "issue/show/" .. self.issue_id .. ".html"
    73         end
    75         body = body .. _("[event mail]       URL: #{url}", { url = url }) .. "\n\n"
    77         if self.initiative_id then
    78           local initiative = Initiative:by_id(self.initiative_id)
    79           body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n\n"
    80         else
    81           local initiative_count = Initiative:new_selector()
    82             :add_where{ "initiative.issue_id = ?", self.issue_id }
    83             :count()
    84           local initiatives = Initiative:new_selector()
    85             :add_where{ "initiative.issue_id = ?", self.issue_id }
    86             :add_order_by("initiative.supporter_count DESC")
    87             :limit(3)
    88             :exec()
    89           for i, initiative in ipairs(initiatives) do
    90             body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n"
    91           end
    92           if initiative_count - 3 > 0 then
    93             body = body .. _("and #{count} more initiatives", { count = initiative_count }) .. "\n"
    94           end
    95           body = body .. "\n"
    96         end
    98         if self.suggestion_id then
    99           local suggestion = Suggestion:by_id(self.suggestion_id)
   100           body = body .. _("#{name}\n\n", { name = suggestion.name })
   101         end
   103         local success = net.send_mail{
   104           envelope_from = config.mail_envelope_from,
   105           from          = config.mail_from,
   106           reply_to      = config.mail_reply_to,
   107           to            = member.notify_email,
   108           subject       = subject,
   109           content_type  = "text/plain; charset=UTF-8",
   110           content       = body
   111         }
   113       end
   114     )
   115   end
   117 end
   119 function Event:send_next_notification()
   121   local notification_sent = NotificationSent:new_selector()
   122     :optional_object_mode()
   123     :for_update()
   124     :exec()
   126   local last_event_id = 0
   127   if notification_sent then
   128     last_event_id = notification_sent.event_id
   129   end
   131   local event = Event:new_selector()
   132     :add_where{ "event.id > ?", last_event_id }
   133     :add_order_by("event.id")
   134     :limit(1)
   135     :optional_object_mode()
   136     :exec()
   138   if event then
   139     if last_event_id == 0 then
   140       db:query{ "INSERT INTO notification_sent (event_id) VALUES (?)", event.id }
   141     else
   142       db:query{ "UPDATE notification_sent SET event_id = ?", event.id }
   143     end
   145     event:send_notification()
   147     return true
   149   end
   151 end
   153 function Event:send_notifications_loop()
   155   while true do
   156     local did_work = Event:send_next_notification()
   157     if not did_work then
   158       print "Sleeping 1 second"
   159       os.execute("sleep 1")
   160     end
   161   end
   163 end
