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