| rev | 
   line source | 
| 
bsw/jbe@0
 | 
     1 local initiative = Initiative:new_selector():add_where{ "id = ?", param.get_id()}:single_object_mode():exec()
 | 
| 
poelzi@148
 | 
     2 local auto_support = param.get("auto_support", atom.boolean)
 | 
| 
bsw/jbe@0
 | 
     3 
 | 
| 
bsw/jbe@5
 | 
     4 -- TODO important m1 selectors returning result _SET_!
 | 
| 
bsw/jbe@5
 | 
     5 local issue = initiative:get_reference_selector("issue"):for_share():single_object_mode():exec()
 | 
| 
bsw/jbe@5
 | 
     6 
 | 
| 
bsw@281
 | 
     7 if not app.session.member:has_voting_right_for_unit_id(issue.area.unit_id) then
 | 
| 
bsw@281
 | 
     8   error("access denied")
 | 
| 
bsw@281
 | 
     9 end
 | 
| 
bsw@281
 | 
    10 
 | 
| 
bsw/jbe@5
 | 
    11 if issue.closed then
 | 
| 
bsw/jbe@5
 | 
    12   slot.put_into("error", _"This issue is already closed.")
 | 
| 
bsw/jbe@5
 | 
    13   return false
 | 
| 
bsw/jbe@5
 | 
    14 elseif issue.fully_frozen then 
 | 
| 
bsw/jbe@5
 | 
    15   slot.put_into("error", _"Voting for this issue has already begun.")
 | 
| 
bsw/jbe@5
 | 
    16   return false
 | 
| 
bsw/jbe@5
 | 
    17 end
 | 
| 
bsw/jbe@5
 | 
    18 
 | 
| 
bsw@10
 | 
    19 if initiative.revoked then
 | 
| 
bsw@10
 | 
    20   slot.put_into("error", _"This initiative is revoked")
 | 
| 
bsw@10
 | 
    21   return false
 | 
| 
bsw@10
 | 
    22 end
 | 
| 
bsw@10
 | 
    23 
 | 
| 
bsw/jbe@0
 | 
    24 local member = app.session.member
 | 
| 
bsw/jbe@0
 | 
    25 
 | 
| 
bsw/jbe@0
 | 
    26 local supporter = Supporter:by_pk(initiative.id, member.id)
 | 
| 
bsw/jbe@0
 | 
    27 
 | 
| 
bsw/jbe@0
 | 
    28 local last_draft = Draft:new_selector()
 | 
| 
bsw/jbe@0
 | 
    29   :add_where{ "initiative_id = ?", initiative.id }
 | 
| 
bsw/jbe@0
 | 
    30   :add_order_by("id DESC")
 | 
| 
bsw/jbe@0
 | 
    31   :limit(1)
 | 
| 
bsw/jbe@0
 | 
    32   :single_object_mode()
 | 
| 
bsw/jbe@0
 | 
    33   :exec()
 | 
| 
bsw/jbe@0
 | 
    34 
 | 
| 
bsw/jbe@0
 | 
    35 if not supporter then
 | 
| 
bsw/jbe@0
 | 
    36   supporter = Supporter:new()
 | 
| 
bsw/jbe@0
 | 
    37   supporter.member_id = member.id
 | 
| 
bsw/jbe@0
 | 
    38   supporter.initiative_id = initiative.id
 | 
| 
bsw/jbe@0
 | 
    39   supporter.draft_id = last_draft.id
 | 
| 
poelzi@165
 | 
    40   if config.auto_support and auto_support ~= nil then
 | 
| 
poelzi@165
 | 
    41     supporter.auto_support = auto_support
 | 
| 
poelzi@165
 | 
    42   end
 | 
| 
bsw/jbe@0
 | 
    43   supporter:save()
 | 
| 
bsw@277
 | 
    44 --  slot.put_into("notice", _"Your support has been added to this initiative")
 | 
| 
bsw/jbe@0
 | 
    45 elseif supporter.draft_id ~= last_draft.id then
 | 
| 
bsw/jbe@0
 | 
    46   supporter.draft_id = last_draft.id
 | 
| 
bsw/jbe@0
 | 
    47   supporter:save()
 | 
| 
bsw@277
 | 
    48 --  slot.put_into("notice", _"Your support has been updated to the latest draft")
 | 
| 
bsw/jbe@0
 | 
    49 else
 | 
| 
bsw@277
 | 
    50 --  slot.put_into("notice", _"You are already supporting the latest draft")
 | 
| 
poelzi@148
 | 
    51 end
 | 
| 
poelzi@148
 | 
    52 
 |