liquid_feedback_frontend

annotate app/main/initiative/_action/create.lua @ 1045:701a5cf6b067

Imported LiquidFeedback Frontend 3.0 branch
author bsw
date Thu Jul 10 01:19:48 2014 +0200 (2014-07-10)
parents c23a25f38c44
children 5b65ea5c24f5
rev   line source
bsw/jbe@0 1 local issue
bsw/jbe@0 2 local area
bsw/jbe@0 3
bsw/jbe@0 4 local issue_id = param.get("issue_id", atom.integer)
bsw/jbe@0 5 if issue_id then
bsw@969 6 issue = Issue:new_selector():add_where{"id=?",issue_id}:for_share():single_object_mode():exec()
bsw/jbe@5 7 if issue.closed then
bsw/jbe@5 8 slot.put_into("error", _"This issue is already closed.")
bsw/jbe@5 9 return false
bsw/jbe@5 10 elseif issue.fully_frozen then
bsw/jbe@5 11 slot.put_into("error", _"Voting for this issue has already begun.")
bsw/jbe@5 12 return false
bsw@967 13 elseif issue.phase_finished then
bsw@967 14 slot.put_into("error", _"Current phase is already closed.")
bsw@967 15 return false
bsw/jbe@5 16 end
bsw/jbe@0 17 area = issue.area
bsw/jbe@0 18 else
bsw/jbe@0 19 local area_id = param.get("area_id", atom.integer)
bsw/jbe@0 20 area = Area:new_selector():add_where{"id=?",area_id}:single_object_mode():exec()
bsw/jbe@52 21 if not area.active then
bsw/jbe@52 22 slot.put_into("error", "Invalid area.")
bsw/jbe@52 23 return false
bsw/jbe@52 24 end
bsw/jbe@0 25 end
bsw/jbe@0 26
bsw@281 27 if not app.session.member:has_voting_right_for_unit_id(area.unit_id) then
bsw@281 28 error("access denied")
bsw@281 29 end
bsw@281 30
bsw@904 31 local policy_id = param.get("policy_id", atom.integer)
bsw@904 32 local policy
bsw@904 33 if policy_id then
bsw@904 34 policy = Policy:by_id(policy_id)
bsw@904 35 end
bsw@904 36
bsw@904 37 if not issue then
bsw@904 38 if policy_id == -1 then
bsw@904 39 slot.put_into("error", _"Please choose a policy")
bsw@904 40 return false
bsw@904 41 end
bsw@904 42 if not policy.active then
bsw@904 43 slot.put_into("error", "Invalid policy.")
bsw@904 44 return false
bsw@904 45 end
bsw@904 46 if policy.polling and not app.session.member:has_polling_right_for_unit_id(area.unit_id) then
bsw@904 47 error("no polling right for this unit")
bsw@904 48 end
bsw@904 49
bsw@904 50 if not area:get_reference_selector("allowed_policies")
bsw@904 51 :add_where{ "policy.id = ?", policy_id }
bsw@904 52 :optional_object_mode()
bsw@904 53 :exec()
bsw@904 54 then
bsw@904 55 error("policy not allowed")
bsw@904 56 end
bsw@904 57 end
bsw@904 58
bsw@904 59 local is_polling = (issue and param.get("polling", atom.boolean)) or (policy and policy.polling) or false
bsw@904 60
bsw@904 61 local tmp = db:query({ "SELECT text_entries_left, initiatives_left FROM member_contingent_left WHERE member_id = ? AND polling = ?", app.session.member.id, is_polling }, "opt_object")
bsw@904 62 if not tmp or tmp.initiatives_left < 1 then
bsw@904 63 slot.put_into("error", _"Sorry, your contingent for creating initiatives has been used up. Please try again later.")
bsw@904 64 return false
bsw@904 65 end
bsw@904 66 if tmp and tmp.text_entries_left < 1 then
bsw@904 67 slot.put_into("error", _"Sorry, you have reached your personal flood limit. Please be slower...")
bsw@904 68 return false
bsw@904 69 end
bsw@904 70
bsw/jbe@6 71 local name = param.get("name")
bsw/jbe@6 72
bsw/jbe@6 73 local name = util.trim(name)
bsw/jbe@6 74
bsw/jbe@6 75 if #name < 3 then
bsw@1045 76 slot.put_into("error", _"Please enter a meaningful title for your initiative!")
bsw@1045 77 return false
bsw@1045 78 end
bsw@1045 79
bsw@1045 80 if #name > 140 then
bsw@1045 81 slot.put_into("error", _"This title is too long!")
bsw/jbe@6 82 return false
bsw/jbe@6 83 end
bsw/jbe@6 84
bsw@1045 85 local formatting_engine
bsw@1045 86 if config.enforce_formatting_engine then
bsw@1045 87 formatting_engine = config.enforce_formatting_engine
bsw@1045 88 else
bsw@1045 89 formatting_engine = param.get("formatting_engine")
bsw@1045 90 local formatting_engine_valid = false
bsw@1045 91 for i, fe in ipairs(config.formatting_engines) do
bsw@1045 92 if formatting_engine == fe.id then
bsw@1045 93 formatting_engine_valid = true
bsw@1045 94 end
bsw@1045 95 end
bsw@1045 96 if not formatting_engine_valid then
bsw@1045 97 error("invalid formatting engine!")
bsw@81 98 end
bsw@81 99 end
bsw@81 100
bsw@1045 101 if param.get("preview") or param.get("edit") then
bsw@95 102 return
bsw@95 103 end
bsw@81 104
bsw@81 105
bsw/jbe@0 106 local initiative = Initiative:new()
bsw/jbe@0 107
bsw/jbe@0 108 if not issue then
bsw/jbe@0 109 issue = Issue:new()
bsw/jbe@0 110 issue.area_id = area.id
bsw@7 111 issue.policy_id = policy_id
bsw@898 112
bsw@898 113 if policy.polling then
bsw@898 114 issue.accepted = 'now'
bsw@898 115 issue.state = 'discussion'
bsw@898 116 initiative.polling = true
bsw@901 117
bsw@901 118 if policy.free_timeable then
bsw@901 119 local free_timing_string = util.trim(param.get("free_timing"))
bsw@901 120 local available_timings
bsw@901 121 if config.free_timing and config.free_timing.available_func then
bsw@901 122 available_timings = config.free_timing.available_func(policy)
bsw@901 123 if available_timings == false then
bsw@901 124 error("error in free timing config")
bsw@901 125 end
bsw@901 126 end
bsw@901 127 if available_timings then
bsw@901 128 local timing_available = false
bsw@901 129 for i, available_timing in ipairs(available_timings) do
bsw@901 130 if available_timing.id == free_timing_string then
bsw@901 131 timing_available = true
bsw@901 132 end
bsw@901 133 end
bsw@901 134 if not timing_available then
bsw@901 135 error('Invalid timing')
bsw@901 136 end
bsw@901 137 end
bsw@901 138 local timing = config.free_timing.calculate_func(policy, free_timing_string)
bsw@901 139 if not timing then
bsw@901 140 error("error in free timing config")
bsw@901 141 end
bsw@901 142 issue.discussion_time = timing.discussion
bsw@901 143 issue.verification_time = timing.verification
bsw@901 144 issue.voting_time = timing.voting
bsw@901 145 end
bsw@901 146
bsw@898 147 end
bsw@898 148
bsw/jbe@0 149 issue:save()
bsw@286 150
bsw@286 151 if config.etherpad then
bsw@286 152 local result = net.curl(
bsw@286 153 config.etherpad.api_base
bsw@286 154 .. "api/1/createGroupPad?apikey=" .. config.etherpad.api_key
bsw@286 155 .. "&groupID=" .. config.etherpad.group_id
bsw@286 156 .. "&padName=Issue" .. tostring(issue.id)
jbe@326 157 .. "&text=" .. request.get_absolute_baseurl() .. "issue/show/" .. tostring(issue.id) .. ".html"
bsw@286 158 )
bsw@286 159 end
bsw/jbe@0 160 end
bsw/jbe@0 161
bsw@898 162 if param.get("polling", atom.boolean) and app.session.member:has_polling_right_for_unit_id(area.unit_id) then
bsw@898 163 initiative.polling = true
bsw@898 164 end
bsw/jbe@0 165 initiative.issue_id = issue.id
bsw/jbe@6 166 initiative.name = name
bsw/jbe@0 167 initiative:save()
bsw/jbe@0 168
bsw/jbe@0 169 local draft = Draft:new()
bsw/jbe@0 170 draft.initiative_id = initiative.id
bsw/jbe@4 171 draft.formatting_engine = formatting_engine
bsw/jbe@0 172 draft.content = param.get("draft")
bsw/jbe@0 173 draft.author_id = app.session.member.id
bsw/jbe@0 174 draft:save()
bsw/jbe@0 175
bsw/jbe@0 176 local initiator = Initiator:new()
bsw/jbe@0 177 initiator.initiative_id = initiative.id
bsw/jbe@0 178 initiator.member_id = app.session.member.id
bsw@10 179 initiator.accepted = true
bsw/jbe@0 180 initiator:save()
bsw/jbe@0 181
bsw@944 182 if not is_polling then
bsw@944 183 local supporter = Supporter:new()
bsw@944 184 supporter.initiative_id = initiative.id
bsw@944 185 supporter.member_id = app.session.member.id
bsw@944 186 supporter.draft_id = draft.id
bsw@944 187 supporter:save()
bsw@944 188 end
bsw/jbe@0 189
bsw/jbe@0 190 slot.put_into("notice", _"Initiative successfully created")
bsw/jbe@0 191
bsw/jbe@0 192 request.redirect{
bsw/jbe@0 193 module = "initiative",
bsw/jbe@0 194 view = "show",
bsw/jbe@0 195 id = initiative.id
jbe@326 196 }

Impressum / About Us