11 end |
14 end |
12 |
15 |
13 local form_x_attr = { xmlns = xmlns_forms }; |
16 local form_x_attr = { xmlns = xmlns_forms }; |
14 |
17 |
15 function form_t.form(layout, data) |
18 function form_t.form(layout, data) |
16 local form = st.tag("x", form_x_attr); |
19 local form = st.stanza("x", form_x_attr); |
|
20 if layout.title then |
|
21 form:tag("title"):text(layout.title):up(); |
|
22 end |
|
23 if layout.instructions then |
|
24 form:tag("instructions"):text(layout.instructions):up(); |
|
25 end |
17 for n, field in ipairs(layout) do |
26 for n, field in ipairs(layout) do |
18 local field_type = field.type; |
27 local field_type = field.type or "text-single"; |
19 -- Add field tag |
28 -- Add field tag |
20 form:tag("field", { type = field_type, var = field.name }); |
29 form:tag("field", { type = field_type, var = field.name, label = field.label }); |
21 |
30 |
22 local value = data[field.name]; |
31 local value = data[field.name]; |
23 |
32 |
24 -- Add value, depending on type |
33 -- Add value, depending on type |
25 if field_type == "hidden" then |
34 if field_type == "hidden" then |
28 form:add_child(value); |
37 form:add_child(value); |
29 elseif value then |
38 elseif value then |
30 form:text(tostring(value)); |
39 form:text(tostring(value)); |
31 end |
40 end |
32 elseif field_type == "boolean" then |
41 elseif field_type == "boolean" then |
33 form:text((value and "1") or "0"); |
42 form:tag("value"):text((value and "1") or "0"); |
34 elseif field_type == "fixed" then |
43 elseif field_type == "fixed" then |
35 |
44 |
36 elseif field_type == "jid-multi" then |
45 elseif field_type == "jid-multi" then |
37 for _, jid in ipairs(value) do |
46 for _, jid in ipairs(value) do |
38 form:tag("value"):text(jid):up(); |
47 form:tag("value"):text(jid):up(); |
39 end |
48 end |
40 elseif field_type == "jid-single" then |
49 elseif field_type == "jid-single" then |
41 form:tag("value"):text(value):up(); |
50 form:tag("value"):text(value):up(); |
42 |
51 elseif field_type == "text-single" or field_type == "text-private" then |
|
52 form:tag("value"):text(value):up(); |
|
53 elseif field_type == "text-multi" then |
|
54 -- Split into multiple <value> tags, one for each line |
|
55 for line in value:gmatch("([^\r\n]+)\r?\n*") do |
|
56 form:tag("value"):text(line):up(); |
|
57 end |
43 end |
58 end |
44 |
59 |
45 -- Jump back up to list of fields |
60 -- Jump back up to list of fields |
46 form:up(); |
61 form:up(); |
47 end |
62 end |