| rev | line source | 
| bsw@1500 | 1 function util.add_support(initiative_id, draft_id) | 
| bsw@1500 | 2 | 
| bsw@1500 | 3   local initiative = Initiative:new_selector():add_where{ "id = ?", initiative_id }:single_object_mode():exec() | 
| bsw@1500 | 4 | 
| bsw@1500 | 5   -- TODO important m1 selectors returning result _SET_! | 
| bsw@1500 | 6   local issue = initiative:get_reference_selector("issue"):for_share():single_object_mode():exec() | 
| bsw@1500 | 7 | 
| bsw@1529 | 8   local member = app.access_token and app.access_token.member or app.session and app.session.member | 
| bsw@1529 | 9   if not member then | 
| bsw@1529 | 10     slot.put_into("error", "no valid session") | 
| bsw@1529 | 11     return false | 
| bsw@1529 | 12   end | 
| bsw@1516 | 13 | 
| bsw@1516 | 14   if not member:has_voting_right_for_unit_id(issue.area.unit_id) then | 
| bsw@1500 | 15     slot.put_into("error", _"No voting rights.") | 
| bsw@1500 | 16     return false | 
| bsw@1500 | 17   end | 
| bsw@1500 | 18 | 
| bsw@1500 | 19   if issue.closed then | 
| bsw@1500 | 20     slot.put_into("error", _"This issue is already closed.") | 
| bsw@1500 | 21     return false | 
| bsw@1500 | 22   elseif issue.fully_frozen then | 
| bsw@1500 | 23     slot.put_into("error", _"Voting for this issue has already begun.") | 
| bsw@1500 | 24     return false | 
| bsw@1500 | 25   elseif | 
| bsw@1500 | 26     (issue.half_frozen and issue.phase_finished) or | 
| bsw@1500 | 27     (not issue.accepted and issue.phase_finished) | 
| bsw@1500 | 28   then | 
| bsw@1500 | 29     slot.put_into("error", _"Current phase is already closed.") | 
| bsw@1500 | 30     return false | 
| bsw@1500 | 31   end | 
| bsw@1500 | 32 | 
| bsw@1500 | 33   if initiative.revoked then | 
| bsw@1500 | 34     slot.put_into("error", _"This initiative is revoked") | 
| bsw@1500 | 35     return false | 
| bsw@1500 | 36   end | 
| bsw@1500 | 37 | 
| bsw@1500 | 38   local member = app.session.member | 
| bsw@1500 | 39 | 
| bsw@1500 | 40   local supporter = Supporter:by_pk(initiative.id, member.id) | 
| bsw@1500 | 41 | 
| bsw@1500 | 42   local last_draft = Draft:new_selector() | 
| bsw@1500 | 43     :add_where{ "initiative_id = ?", initiative.id } | 
| bsw@1500 | 44     :add_order_by("id DESC") | 
| bsw@1500 | 45     :limit(1) | 
| bsw@1500 | 46     :single_object_mode() | 
| bsw@1500 | 47     :exec() | 
| bsw@1500 | 48 | 
| bsw@1500 | 49   if draft_id and draft_id ~= last_draft.id then | 
| bsw@1500 | 50     slot.select("error", function() | 
| bsw@1500 | 51       ui.tag{ content = _"The initiative draft has been updated again in the meanwhile, support not updated!" } | 
| bsw@1500 | 52     end) | 
| bsw@1500 | 53     return false | 
| bsw@1500 | 54   end | 
| bsw@1500 | 55 | 
| bsw@1500 | 56   if not supporter then | 
| bsw@1500 | 57     supporter = Supporter:new() | 
| bsw@1500 | 58     supporter.member_id = member.id | 
| bsw@1500 | 59     supporter.initiative_id = initiative.id | 
| bsw@1500 | 60     supporter.draft_id = last_draft.id | 
| bsw@1500 | 61     supporter:save() | 
| bsw@1500 | 62   elseif supporter.draft_id ~= last_draft.id then | 
| bsw@1500 | 63     supporter.draft_id = last_draft.id | 
| bsw@1500 | 64     supporter:save() | 
| bsw@1500 | 65     slot.put_into("notice", _"Your support has been updated to the latest draft") | 
| bsw@1500 | 66   else | 
| bsw@1500 | 67     slot.put_into("notice", _"You are already supporting the latest draft") | 
| bsw@1500 | 68   end | 
| bsw@1500 | 69 | 
| bsw@1500 | 70   return true | 
| bsw@1500 | 71 | 
| bsw@1500 | 72 end | 
| bsw@1500 | 73 |