Software /
code /
verse
Comparison
util/dataforms.lua @ 335:9e69ee8542d4
util.dataforms: Add method for converting XML dataforms to tables
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 15 Feb 2013 20:45:31 +0100 (2013-02-15) |
parent | 334:34c52f3b21c4 |
child | 441:e4c0b1d7fd6b |
comparison
equal
deleted
inserted
replaced
334:34c52f3b21c4 | 335:9e69ee8542d4 |
---|---|
20 local form_t = {}; | 20 local form_t = {}; |
21 local form_mt = { __index = form_t }; | 21 local form_mt = { __index = form_t }; |
22 | 22 |
23 function new(layout) | 23 function new(layout) |
24 return setmetatable(layout, form_mt); | 24 return setmetatable(layout, form_mt); |
25 end | |
26 | |
27 function from_stanza(stanza) | |
28 local layout = { | |
29 title = stanza:get_child_text("title"); | |
30 instructions = stanza:get_child_text("instructions"); | |
31 }; | |
32 for tag in stanza:childtags("field") do | |
33 local field = { | |
34 name = tag.attr.var; | |
35 label = tag.attr.label; | |
36 type = tag.attr.type; | |
37 required = tag:get_child("required") and true or nil; | |
38 value = tag:get_child_text("value"); | |
39 }; | |
40 layout[#layout+1] = field; | |
41 if field.type then | |
42 local value = {}; | |
43 if field.type:match"list%-" then | |
44 for tag in tag:childtags("option") do | |
45 value[#value+1] = { label = tag.attr.label, value = tag:get_child_text("value") }; | |
46 end | |
47 for tag in tag:childtags("value") do | |
48 value[#value+1] = { label = tag.attr.label, value = tag:get_text(), default = true }; | |
49 end | |
50 elseif field.type:match"%-multi" then | |
51 for tag in tag:childtags("value") do | |
52 value[#value+1] = tag.attr.label and { label = tag.attr.label, value = tag:get_text() } or tag:get_text(); | |
53 end | |
54 if field.type == "text-multi" then | |
55 field.value = t_concat(value, "\n"); | |
56 else | |
57 field.value = value; | |
58 end | |
59 end | |
60 end | |
61 end | |
62 return new(layout); | |
25 end | 63 end |
26 | 64 |
27 function form_t.form(layout, data, formtype) | 65 function form_t.form(layout, data, formtype) |
28 local form = st.stanza("x", { xmlns = xmlns_forms, type = formtype or "form" }); | 66 local form = st.stanza("x", { xmlns = xmlns_forms, type = formtype or "form" }); |
29 if layout.title then | 67 if layout.title then |