bsw@1500: function util.add_support(initiative_id, draft_id) bsw@1500: bsw@1500: local initiative = Initiative:new_selector():add_where{ "id = ?", initiative_id }:single_object_mode():exec() bsw@1500: bsw@1500: -- TODO important m1 selectors returning result _SET_! bsw@1500: local issue = initiative:get_reference_selector("issue"):for_share():single_object_mode():exec() bsw@1500: bsw@1529: local member = app.access_token and app.access_token.member or app.session and app.session.member bsw@1529: if not member then bsw@1529: slot.put_into("error", "no valid session") bsw@1529: return false bsw@1529: end bsw@1516: bsw@1516: if not member:has_voting_right_for_unit_id(issue.area.unit_id) then bsw@1500: slot.put_into("error", _"No voting rights.") bsw@1500: return false bsw@1500: end bsw@1500: bsw@1500: if issue.closed then bsw@1500: slot.put_into("error", _"This issue is already closed.") bsw@1500: return false bsw@1500: elseif issue.fully_frozen then bsw@1500: slot.put_into("error", _"Voting for this issue has already begun.") bsw@1500: return false bsw@1500: elseif bsw@1500: (issue.half_frozen and issue.phase_finished) or bsw@1500: (not issue.accepted and issue.phase_finished) bsw@1500: then bsw@1500: slot.put_into("error", _"Current phase is already closed.") bsw@1500: return false bsw@1500: end bsw@1500: bsw@1500: if initiative.revoked then bsw@1500: slot.put_into("error", _"This initiative is revoked") bsw@1500: return false bsw@1500: end bsw@1500: bsw@1500: local member = app.session.member bsw@1500: bsw@1500: local supporter = Supporter:by_pk(initiative.id, member.id) bsw@1500: bsw@1500: local last_draft = Draft:new_selector() bsw@1500: :add_where{ "initiative_id = ?", initiative.id } bsw@1500: :add_order_by("id DESC") bsw@1500: :limit(1) bsw@1500: :single_object_mode() bsw@1500: :exec() bsw@1500: bsw@1500: if draft_id and draft_id ~= last_draft.id then bsw@1500: slot.select("error", function() bsw@1500: ui.tag{ content = _"The initiative draft has been updated again in the meanwhile, support not updated!" } bsw@1500: end) bsw@1500: return false bsw@1500: end bsw@1500: bsw@1500: if not supporter then bsw@1500: supporter = Supporter:new() bsw@1500: supporter.member_id = member.id bsw@1500: supporter.initiative_id = initiative.id bsw@1500: supporter.draft_id = last_draft.id bsw@1500: supporter:save() bsw@1500: elseif supporter.draft_id ~= last_draft.id then bsw@1500: supporter.draft_id = last_draft.id bsw@1500: supporter:save() bsw@1500: slot.put_into("notice", _"Your support has been updated to the latest draft") bsw@1500: else bsw@1500: slot.put_into("notice", _"You are already supporting the latest draft") bsw@1500: end bsw@1500: bsw@1500: return true bsw@1500: bsw@1500: end bsw@1500: