|
1 |
|
2 local form_cid = main.add_category { 'del', 'send' } |
|
3 local forms = { } |
|
4 |
|
5 -- public |
|
6 function insert_form ( form, submit, reject ) |
|
7 table.insert ( forms, { |
|
8 form = form, |
|
9 submit = submit, |
|
10 reject = reject, |
|
11 } ) |
|
12 main.add_completion ( form_cid, tostring(#forms) ) |
|
13 print ( "You have new form. Use /form " .. #forms .. " to fill and submit or cancel it." ) |
|
14 return #forms |
|
15 end |
|
16 |
|
17 main.command ( 'form', |
|
18 function ( args ) |
|
19 local id, action = tonumber (args[1]), args[2] |
|
20 if forms[id] then |
|
21 local form = forms[id].form |
|
22 if action == 'send' then |
|
23 forms[id].submit ( form ) |
|
24 elseif action == 'reject' then |
|
25 forms[id].reject ( form ) |
|
26 elseif action == 'del' then |
|
27 main.del_completion ( form_cid, id ) |
|
28 forms[id] = nil |
|
29 elseif action then |
|
30 local fname, value |
|
31 if action == 'set' then |
|
32 fname = args[3] |
|
33 value = args[4] |
|
34 else |
|
35 fname = action |
|
36 value = args[3] |
|
37 end |
|
38 local field = form:field ( fname ) |
|
39 if field then |
|
40 if value then |
|
41 field:value ( value ) |
|
42 else |
|
43 field:clear () |
|
44 end |
|
45 else |
|
46 -- XXX create? |
|
47 print ( 'Field not found: ' .. fname ) |
|
48 end |
|
49 else |
|
50 local desc = 'Form ' .. id .. '\n - Type: ' .. form:type () |
|
51 local title, instructions = form:desc () |
|
52 if title then |
|
53 desc = desc .. '\n - Title: ' .. title |
|
54 end |
|
55 if instructions then |
|
56 desc = desc .. '\n - Instructions: ' .. instructions |
|
57 end |
|
58 |
|
59 local fields = 'Fields:' |
|
60 for index, field in form:fields () do |
|
61 local ftype = field:type () |
|
62 |
|
63 fields = fields .. '\n - ' |
|
64 |
|
65 if field:required () then |
|
66 fields = fields .. '* ' |
|
67 end |
|
68 |
|
69 local label, descr = field:desc () |
|
70 if label then |
|
71 fields = fields .. label .. ' (' .. ( field.var or '' ) .. ')' |
|
72 else |
|
73 fields = fields .. ( field:name () or '<no name>' ) |
|
74 end |
|
75 fields = fields .. ' [' .. ftype .. ']' |
|
76 if descr then |
|
77 fields = fields .. ': ' .. descr |
|
78 end |
|
79 |
|
80 if ftype == 'list-single' or ftype == 'list-multi' then |
|
81 fields = fields .. '\n Options:' |
|
82 for option, label in field:options () do |
|
83 fields = fields .. '\n * ' .. option .. ' - ' .. label |
|
84 end |
|
85 end |
|
86 |
|
87 if ftype == 'list-multi' or ftype == 'text-multi' or ftype == 'jid-multi' then |
|
88 fields = fields .. '\n Values: ' |
|
89 for vin, value in field:values () do |
|
90 fields = fields .. '\n * ' .. value |
|
91 end |
|
92 else |
|
93 fields = fields .. '\n Value: ' .. ( field:value () or '' ) |
|
94 end |
|
95 end |
|
96 print ( desc .. '\n' .. fields ) |
|
97 end |
|
98 else |
|
99 local text = '' |
|
100 for id, form in pairs ( forms ) do |
|
101 local title = form.form:desc () |
|
102 text = text .. '\n - ' .. id .. ' ' .. ( title or 'Untitled' ) .. ' [' .. form.form:type () .. ']' |
|
103 end |
|
104 if text ~= '' then |
|
105 print ( 'Forms list:' .. text ) |
|
106 else |
|
107 print ( 'No forms' ) |
|
108 end |
|
109 end |
|
110 end, true, form_cid ) |
|
111 |
|
112 commands_help['form'] = "[form_id [send | reject | [set] fieldname [value]]]\n\nWithout arguments prints form list.\nWith bare form id prints info on that form.\nWhen setting multivalue field, new values are added, not replacing previous.\nWithout value unsets field, multivalue fields lose all their values." |
|
113 |
|
114 -- vim: se ts=4: -- |