liquid_feedback_frontend

view app/main/draft/new.lua @ 1824:de3770da915e

Better handling of new draft in not correctly configured area
author bsw
date Thu Feb 03 11:37:06 2022 +0100 (2022-02-03)
parents 89f524f1d185
children 7b04f14c6d0a
line source
1 local issue
2 local area
3 local area_id
5 local issue_id = param.get("issue_id", atom.integer)
6 if issue_id then
7 issue = Issue:new_selector():add_where{"id=?",issue_id}:single_object_mode():exec()
8 issue:load_everything_for_member_id(app.session.member_id)
9 area = issue.area
10 else
11 area_id = param.get("area_id", atom.integer)
12 if area_id then
13 area = Area:new_selector():add_where{"id=?",area_id}:single_object_mode():exec()
14 area:load_delegation_info_once_for_member_id(app.session.member_id)
15 else
16 local firstlife_id = param.get("firstlife_id")
17 if firstlife_id then
18 area = Area:new_selector():join("unit", nil, "unit.id = area.unit_id"):add_where{"attr->>'firstlife_id'=?",firstlife_id}:single_object_mode():exec()
19 area:load_delegation_info_once_for_member_id(app.session.member_id)
20 area_id = area.id
21 end
22 end
23 end
25 if #(area.allowed_policies) < 1 then
26 slot.put_into("error", _"Subject area configuration invalid. Please contact the administrator.")
27 return false
28 end
30 local polling = param.get("polling", atom.boolean)
32 local policy_id = param.get("policy_id", atom.integer)
33 local policy
35 local preview = param.get("preview")
37 if #(slot.get_content("error")) > 0 then
38 preview = false
39 end
41 if policy_id then
42 policy = Policy:by_id(policy_id)
43 end
45 local callback = param.get("callback")
48 local initiative_id = param.get("initiative_id")
49 local initiative = Initiative:by_id(initiative_id)
50 local draft
51 if initiative then
52 initiative:load_everything_for_member_id(app.session.member_id)
53 initiative.issue:load_everything_for_member_id(app.session.member_id)
55 if initiative.issue.closed then
56 slot.put_into("error", _"This issue is already closed.")
57 return
58 elseif initiative.issue.half_frozen then
59 slot.put_into("error", _"This issue is already frozen.")
60 return
61 elseif initiative.issue.phase_finished then
62 slot.put_into("error", _"Current phase is already closed.")
63 return
64 end
66 draft = initiative.current_draft
67 if config.initiative_abstract then
68 draft.abstract = string.match(draft.content, "(.+)<!%--END_OF_ABSTRACT%-->")
69 if draft.abstract then
70 draft.content = string.match(draft.content, "<!%--END_OF_ABSTRACT%-->(.*)")
71 end
72 end
73 end
75 if not initiative and not issue and not area then
76 ui.heading{ content = _"Missing parameter" }
77 return false
78 end
80 ui.form{
81 record = draft,
82 attr = { class = "vertical section", enctype = 'multipart/form-data' },
83 module = "draft",
84 action = "add",
85 params = {
86 area_id = area and area.id,
87 issue_id = issue and issue.id or nil,
88 initiative_id = initiative_id,
89 callback = callback
90 },
91 routing = {
92 ok = {
93 mode = "redirect",
94 module = "initiative",
95 view = "show",
96 id = initiative_id
97 }
98 },
99 content = function()
101 if issue or initiative then
102 execute.view {
103 module = "issue", view = "_head", params = {
104 issue = issue or initiative.issue,
105 member = app.session.member
106 }
107 }
108 else
109 execute.view {
110 module = "area", view = "_head", params = {
111 area = area,
112 member = app.session.member
113 }
114 }
115 end
117 ui.grid{ content = function()
118 ui.cell_main{ content = function()
119 ui.container{ attr = { class = "mdl-card mdl-shadow--2dp mdl-card__fullwidth" }, content = function()
120 ui.container{ attr = { class = "mdl-card__title mdl-card--border" }, content = function()
121 if initiative then
122 ui.heading { attr = { class = "mdl-card__title-text" }, level = 2, content = initiative.display_name }
123 elseif param.get("name") then
124 ui.heading { attr = { class = "mdl-card__title-text" }, level = 2, content = param.get("name") }
125 elseif issue then
126 ui.heading { attr = { class = "mdl-card__title-text" }, level = 2, content = _("New competing initiative in issue '#{issue}'", { issue = issue.name }) }
127 elseif area then
128 ui.heading { attr = { class = "mdl-card__title-text" }, level = 2, content = _"New issue" }
129 end
130 end }
131 ui.container{ attr = { class = "mdl-card__content mdl-card--border" }, content = function()
133 -- -------- PREVIEW
134 if param.get("preview") and slot.get_content("error") == "" then
135 ui.sectionRow( function()
136 if not issue and not initiative and #area.allowed_policies > 1 then
137 ui.container { content = policy and policy.name or "" }
138 slot.put("<br />")
139 end
140 if param.get("free_timing") then
141 ui.container { content = param.get("free_timing") }
142 slot.put("<br />")
143 end
144 ui.field.hidden{ name = "policy_id", value = param.get("policy_id") }
145 ui.field.hidden{ name = "name", value = param.get("name") }
146 if config.initiative_abstract then
147 ui.field.hidden{ name = "abstract", value = param.get("abstract") }
148 ui.container{
149 attr = { class = "abstract" },
150 content = param.get("abstract")
151 }
152 slot.put("<br />")
153 end
154 local draft_text = param.get("content")
155 local draft_text = util.wysihtml_preproc(draft_text)
156 ui.field.hidden{ name = "content", value = draft_text }
157 ui.container{
158 attr = { class = "draft" },
159 content = function()
160 slot.put(draft_text)
161 end
162 }
163 slot.put("<br />")
165 if config.attachments then
166 local file_upload_session = param.get("file_upload_session") or multirand.string(
167 32,
168 '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
169 )
170 file_upload_session = string.gsub(file_upload_session, "[^A-Za-z0-9]", "")
171 ui.field.hidden{ name = "file_upload_session", value = file_upload_session }
172 if initiative then
173 local files = File:new_selector()
174 :left_join("draft_attachment", nil, "draft_attachment.file_id = file.id")
175 :add_where{ "draft_attachment.draft_id = ?", initiative.current_draft.id }
176 :reset_fields()
177 :add_field("file.id")
178 :add_field("draft_attachment.title")
179 :add_field("draft_attachment.description")
180 :add_order_by("draft_attachment.id")
181 :exec()
183 if #files > 0 then
184 ui.container {
185 content = function()
186 for i, file in ipairs(files) do
187 if param.get("file_delete_" .. file.id, atom.boolean) then
188 ui.field.hidden{ name = "file_delete_" .. file.id, value = "1" }
189 else
190 ui.image{ module = "file", view = "show.jpg", id = file.id, params = { preview = true } }
191 ui.container{ content = function()
192 ui.tag{ tag = "strong", content = file.title or "" }
193 end }
194 ui.container{ content = file.description or "" }
195 slot.put("<br /><br />")
196 end
197 end
198 end
199 }
200 end
201 end
202 local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. ".json")
203 local fh = io.open(filename, "r")
204 if fh then
205 local file_uploads = json.import(fh:read("*a"))
206 for i, file_upload in ipairs(file_uploads) do
207 ui.image{ module = "draft", view = "show_file_upload", params = {
208 file_upload_session = file_upload_session, file_id = file_upload.id, preview = true
209 } }
210 ui.container{ content = function()
211 ui.tag{ tag = "strong", content = file_upload.title or "" }
212 end }
213 ui.container{ content = file_upload.description or "" }
214 slot.put("<br />")
215 end
216 end
217 end
219 ui.tag{
220 tag = "input",
221 attr = {
222 type = "submit",
223 class = "mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored",
224 value = _'Publish now'
225 },
226 content = ""
227 }
228 slot.put(" &nbsp; ")
230 ui.tag{
231 tag = "input",
232 attr = {
233 type = "submit",
234 name = "edit",
235 class = "mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect",
236 value = _'Edit again'
237 },
238 content = ""
239 }
240 slot.put(" &nbsp; ")
242 ui.link{
243 attr = { class = "mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect" },
244 content = _"Cancel",
245 module = initiative and "initiative" or "index",
246 view = initiative and "show" or "index",
247 id = initiative_id,
248 params = { unit = area and area.unit_id or nil, area = area_id }
249 }
250 end )
252 -- -------- EDIT
253 else
255 if not issue_id and not initiative_id then
256 local tmp = { { id = -1, name = "" } }
257 for i, allowed_policy in ipairs(area.allowed_policies) do
258 if not allowed_policy.polling or app.session.member:has_polling_right_for_unit_id(area.unit_id) then
259 tmp[#tmp+1] = allowed_policy
260 end
261 end
262 if #area.allowed_policies > 1 then
263 ui.container{ content = _"Please choose a policy for the new issue:" }
264 ui.field.select{
265 name = "policy_id",
266 foreign_records = tmp,
267 foreign_id = "id",
268 foreign_name = "name",
269 value = param.get("policy_id", atom.integer) or area.default_policy and area.default_policy.id
270 }
271 else
272 ui.field.hidden{
273 name = "policy_id",
274 value = area.allowed_policies[1].id
275 }
276 end
277 if policy and policy.free_timeable then
278 local available_timings
279 if config.free_timing and config.free_timing.available_func then
280 available_timings = config.free_timing.available_func(policy)
281 if available_timings == false then
282 slot.put_into("error", "error in free timing config")
283 return false
284 end
285 end
286 ui.heading{ level = 4, content = _"Free timing:" }
287 if available_timings then
288 ui.field.select{
289 name = "free_timing",
290 foreign_records = available_timings,
291 foreign_id = "id",
292 foreign_name = "name",
293 value = param.get("free_timing")
294 }
295 else
296 ui.field.text{
297 name = "free_timing",
298 value = param.get("free_timing")
299 }
300 end
301 end
302 end
304 if issue and issue.policy.polling and app.session.member:has_polling_right_for_unit_id(area.unit_id) then
305 slot.put("<br />")
306 ui.field.boolean{ name = "polling", label = _"No admission needed", value = polling }
307 end
309 if not initiative then
310 ui.container{ attr = { class = "mdl-textfield mdl-js-textfield mdl-textfield--floating-label mdl-card__fullwidth" }, content = function ()
311 ui.field.text{
312 attr = { id = "lf-initiative__name", class = "mdl-textfield__input" },
313 label_attr = { class = "mdl-textfield__label", ["for"] = "lf-initiative__name" },
314 label = _"Title",
315 name = "name",
316 value = param.get("name")
317 }
318 end }
319 end
321 if config.initiative_abstract then
322 ui.container { content = _"Enter abstract:" }
323 ui.container{ attr = { class = "mdl-textfield mdl-js-textfield mdl-textfield--expandable mdl-textfield__fullwidth" }, content = function()
324 ui.field.text{
325 name = "abstract",
326 multiline = true,
327 attr = { id = "abstract", style = "height: 20ex; width: 100%;" },
328 value = param.get("abstract")
329 }
330 end }
331 end
333 ui.container { content = _"Enter your proposal and/or reasons:" }
334 ui.field.wysihtml{
335 name = "content",
336 multiline = true,
337 attr = { id = "draft", style = "height: 50ex; width: 100%;" },
338 value = param.get("content")
339 }
340 if not issue or issue.state == "admission" or issue.state == "discussion" then
341 ui.container { content = _"You can change your text again anytime during admission and discussion phase" }
342 else
343 ui.container { content = _"You cannot change your text again later, because this issue is already in verfication phase!" }
344 end
346 slot.put("<br />")
347 if config.attachments then
348 local file_upload_session = param.get("file_upload_session") or multirand.string(
349 32,
350 '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
351 )
352 file_upload_session = string.gsub(file_upload_session, "[^A-Za-z0-9]", "")
353 ui.field.hidden{ name = "file_upload_session", value = file_upload_session }
354 if initiative then
355 local files = File:new_selector()
356 :left_join("draft_attachment", nil, "draft_attachment.file_id = file.id")
357 :add_where{ "draft_attachment.draft_id = ?", initiative.current_draft.id }
358 :reset_fields()
359 :add_field("file.id")
360 :add_field("draft_attachment.title")
361 :add_field("draft_attachment.description")
362 :add_order_by("draft_attachment.id")
363 :exec()
365 if #files > 0 then
366 ui.container {
367 content = function()
368 for i, file in ipairs(files) do
369 ui.image{ module = "file", view = "show.jpg", id = file.id, params = { preview = true } }
370 ui.container{ content = function()
371 ui.tag{ tag = "strong", content = file.title or "" }
372 end }
373 ui.container{ content = file.description or "" }
374 ui.field.boolean{ label = _"delete", name = "file_delete_" .. file.id, value = param.get("file_delete_" .. file.id) and true or false }
375 slot.put("<br /><br />")
376 end
377 end
378 }
379 end
380 end
381 local filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "file_upload-" .. file_upload_session .. ".json")
382 local fh = io.open(filename, "r")
383 if fh then
384 local file_uploads = json.import(fh:read("*a"))
385 for i, file_upload in ipairs(file_uploads) do
386 ui.image{ module = "draft", view = "show_file_upload", params = {
387 file_upload_session = file_upload_session, file_id = file_upload.id, preview = true
388 } }
389 ui.container{ content = function()
390 ui.tag{ tag = "strong", content = file_upload.title or "" }
391 end }
392 ui.container{ content = file_upload.description or "" }
393 ui.field.boolean{ label = _"delete", name = "file_upload_delete_" .. file_upload.id }
394 slot.put("<br />")
395 end
396 end
397 ui.container{ attr = { id = "file_upload_template", style = "display: none;" }, content = function()
398 ui.field.text{ label = _"Title", name = "__ID_title__" }
399 ui.field.text{ label = _"Description", name = "__ID_description__" }
400 ui.field.image{ field_name = "__ID_file__" }
401 end }
402 ui.container{ attr = { id = "file_upload" }, content = function()
403 end }
404 ui.field.hidden{ attr = { id = "file_upload_last_id" }, name = "file_upload_last_id" }
405 ui.script{ script = [[ var file_upload_id = 1; ]] }
406 ui.tag{ tag = "a", content = _"Attach image", attr = {
407 href = "#",
408 onclick = "var html = document.getElementById('file_upload_template').innerHTML; html = html.replace('__ID_file__', 'file_' + file_upload_id); html = html.replace('__ID_title__', 'title_' + file_upload_id); html = html.replace('__ID_description__', 'description_' + file_upload_id); var el = document.createElement('div'); el.innerHTML = html; document.getElementById('file_upload').appendChild(el); document.getElementById('file_upload_last_id').value = file_upload_id; file_upload_id++; return false;"
409 } }
410 slot.put("<br />")
412 slot.put("<br />")
414 end
416 ui.tag{
417 tag = "input",
418 attr = {
419 type = "submit",
420 name = "preview",
421 class = "mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored",
422 value = _'Preview'
423 },
424 content = ""
425 }
426 slot.put(" &nbsp; ")
428 ui.link{
429 content = _"Cancel",
430 module = initiative and "initiative" or issue and "issue" or "index",
431 view = area and not issue and "index" or "show",
432 id = initiative_id or issue_id,
433 params = { area = area_id, unit = area and area.unit_id or nil },
434 attr = { class = "mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect" }
435 }
437 end
438 end }
439 end }
440 end }
442 if config.map or config.firstlife then
443 ui.cell_sidebar{ content = function()
444 ui.container{ attr = { class = "mdl-special-card map mdl-shadow--2dp" }, content = function()
445 local location = param.get("location")
446 local lat = param.get("lat", atom.number)
447 local lon = param.get("lon", atom.number)
448 if lat and lon then
449 location = json.export(json.object{
450 type = "Point",
451 coordinates = json.array{ lon, lat }
452 })
453 end
454 ui.field.location{ name = "location", value = location }
455 end }
456 end }
457 end
459 if config.firstlife then
460 ui.field.hidden{ name = "external_reference", value = param.get("external_reference") }
461 end
463 end }
464 end
465 }

Impressum / About Us