liquid_feedback_frontend
view app/main/initiative/_action/create.lua @ 1489:3ab878ba277e
Accessibility improvements part 1
author | bsw |
---|---|
date | Mon Feb 18 19:46:36 2019 +0100 (2019-02-18) |
parents | 32cc544d5a5b |
children |
line source
1 local issue
2 local area
4 local issue_id = param.get("issue_id", atom.integer)
5 if issue_id then
6 issue = Issue:new_selector():add_where{"id=?",issue_id}:for_share():single_object_mode():exec()
7 if issue.closed then
8 slot.put_into("error", _"This issue is already closed.")
9 return false
10 elseif issue.fully_frozen then
11 slot.put_into("error", _"Voting for this issue has already begun.")
12 return false
13 elseif issue.phase_finished then
14 slot.put_into("error", _"Current phase is already closed.")
15 return false
16 end
17 area = issue.area
18 else
19 local area_id = param.get("area_id", atom.integer)
20 area = Area:new_selector():add_where{"id=?",area_id}:single_object_mode():exec()
21 if not area.active then
22 slot.put_into("error", "Invalid area.")
23 return false
24 end
25 end
27 if not app.session.member:has_voting_right_for_unit_id(area.unit_id) then
28 return execute.view { module = "index", view = "403" }
29 end
31 local policy_id = param.get("policy_id", atom.integer)
32 local policy
33 if policy_id then
34 policy = Policy:by_id(policy_id)
35 end
37 if not issue then
38 if policy_id == -1 then
39 slot.put_into("error", _"Please choose a policy")
40 return false
41 end
42 if not policy.active then
43 slot.put_into("error", "Invalid policy.")
44 return false
45 end
46 if policy.polling and not app.session.member:has_polling_right_for_unit_id(area.unit_id) then
47 return execute.view { module = "index", view = "403" }
48 end
49 if not area:get_reference_selector("allowed_policies")
50 :add_where{ "policy.id = ?", policy_id }
51 :optional_object_mode()
52 :exec()
53 then
54 slot.put_into("error", "policy not allowed")
55 return false
56 end
57 end
59 local is_polling = (issue and param.get("polling", atom.boolean)) or (policy and policy.polling) or false
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")
62 if not tmp or tmp.initiatives_left < 1 then
63 slot.put_into("error", _"Sorry, your contingent for creating initiatives has been used up. Please try again later.")
64 return false
65 end
66 if tmp and tmp.text_entries_left < 1 then
67 slot.put_into("error", _"Sorry, you have reached your personal flood limit. Please be slower...")
68 return false
69 end
71 local name = param.get("name")
73 local name = util.trim(name)
75 if #name < 3 then
76 slot.put_into("error", _"Please enter a meaningful title for your initiative!")
77 return false
78 end
80 if #name > 140 then
81 slot.put_into("error", _"This title is too long!")
82 return false
83 end
85 local timing
86 if not issue and policy.free_timeable then
87 local free_timing_string = util.trim(param.get("free_timing"))
88 if not free_timing_string or #free_timing_string < 1 then
89 slot.put_into("error", _"Choose timing")
90 return false
91 end
92 local available_timings
93 if config.free_timing and config.free_timing.available_func then
94 available_timings = config.free_timing.available_func(policy)
95 if available_timings == false then
96 slot.put_into("error", "error in free timing config")
97 return false
98 end
99 end
100 if available_timings then
101 local timing_available = false
102 for i, available_timing in ipairs(available_timings) do
103 if available_timing.id == free_timing_string then
104 timing_available = true
105 end
106 end
107 if not timing_available then
108 slot.put_into("error", _"Invalid timing")
109 return false
110 end
111 end
112 timing = config.free_timing.calculate_func(policy, free_timing_string)
113 if not timing then
114 slot.put_into("error", "error in free timing config")
115 return false
116 end
117 end
119 local draft_text = param.get("draft")
121 if not draft_text then
122 return false
123 end
125 local draft_text = util.wysihtml_preproc(draft_text)
127 local valid_html, error_message = util.html_is_safe(draft_text)
128 if not valid_html then
129 slot.put_into("error", _("Draft contains invalid formatting or character sequence: #{error_message}", { error_message = error_message }) )
130 return false
131 end
133 if config.initiative_abstract then
134 local abstract = param.get("abstract")
135 if not abstract then
136 return false
137 end
138 abstract = encode.html(abstract)
139 draft_text = abstract .. "<!--END_OF_ABSTRACT-->" .. draft_text
140 end
142 local location = param.get("location")
143 if location == "" then
144 location = nil
145 end
147 if param.get("preview") or param.get("edit") then
148 return
149 end
151 local initiative = Initiative:new()
153 if not issue then
154 issue = Issue:new()
155 issue.area_id = area.id
156 issue.policy_id = policy_id
158 if policy.polling then
159 issue.accepted = 'now'
160 issue.state = 'discussion'
161 initiative.polling = true
163 if policy.free_timeable then
164 issue.discussion_time = timing.discussion
165 issue.verification_time = timing.verification
166 issue.voting_time = timing.voting
167 end
169 end
171 issue:save()
173 if config.etherpad then
174 local result = net.curl(
175 config.etherpad.api_base
176 .. "api/1/createGroupPad?apikey=" .. config.etherpad.api_key
177 .. "&groupID=" .. config.etherpad.group_id
178 .. "&padName=Issue" .. tostring(issue.id)
179 .. "&text=" .. request.get_absolute_baseurl() .. "issue/show/" .. tostring(issue.id) .. ".html"
180 )
181 end
182 end
184 if param.get("polling", atom.boolean) and app.session.member:has_polling_right_for_unit_id(area.unit_id) then
185 initiative.polling = true
186 end
187 initiative.issue_id = issue.id
188 initiative.name = name
189 initiative:save()
191 local draft = Draft:new()
192 draft.initiative_id = initiative.id
193 draft.formatting_engine = formatting_engine
194 draft.content = draft_text
195 draft.location = location
196 draft.author_id = app.session.member.id
197 draft:save()
199 local initiator = Initiator:new()
200 initiator.initiative_id = initiative.id
201 initiator.member_id = app.session.member.id
202 initiator.accepted = true
203 initiator:save()
205 if not is_polling then
206 local supporter = Supporter:new()
207 supporter.initiative_id = initiative.id
208 supporter.member_id = app.session.member.id
209 supporter.draft_id = draft.id
210 supporter:save()
211 end
213 slot.put_into("notice", _"Initiative successfully created")
215 request.redirect{
216 module = "initiative",
217 view = "show",
218 id = initiative.id
219 }