68
|
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 |
70
|
29 |
elseif action == 'add' then |
|
30 |
form:add( args[3], { value = args[4] } ) |
68
|
31 |
elseif action then |
|
32 |
local fname, value |
|
33 |
if action == 'set' then |
|
34 |
fname = args[3] |
|
35 |
value = args[4] |
|
36 |
else |
|
37 |
fname = action |
|
38 |
value = args[3] |
|
39 |
end |
|
40 |
local field = form:field ( fname ) |
|
41 |
if field then |
|
42 |
if value then |
|
43 |
field:value ( value ) |
|
44 |
else |
|
45 |
field:clear () |
|
46 |
end |
|
47 |
else |
|
48 |
-- XXX create? |
|
49 |
print ( 'Field not found: ' .. fname ) |
|
50 |
end |
|
51 |
else |
|
52 |
local desc = 'Form ' .. id .. '\n - Type: ' .. form:type () |
|
53 |
local title, instructions = form:desc () |
|
54 |
if title then |
|
55 |
desc = desc .. '\n - Title: ' .. title |
|
56 |
end |
|
57 |
if instructions then |
|
58 |
desc = desc .. '\n - Instructions: ' .. instructions |
|
59 |
end |
|
60 |
|
|
61 |
local fields = 'Fields:' |
|
62 |
for index, field in form:fields () do |
|
63 |
local ftype = field:type () |
|
64 |
|
|
65 |
fields = fields .. '\n - ' |
|
66 |
|
|
67 |
if field:required () then |
|
68 |
fields = fields .. '* ' |
|
69 |
end |
|
70 |
|
|
71 |
local label, descr = field:desc () |
|
72 |
if label then |
|
73 |
fields = fields .. label .. ' (' .. ( field:name () or '' ) .. ')' |
|
74 |
else |
|
75 |
fields = fields .. ( field:name () or '<no name>' ) |
|
76 |
end |
|
77 |
fields = fields .. ' [' .. ftype .. ']' |
|
78 |
if descr then |
|
79 |
fields = fields .. ': ' .. descr |
|
80 |
end |
|
81 |
|
|
82 |
if ftype == 'list-single' or ftype == 'list-multi' then |
|
83 |
fields = fields .. '\n Options:' |
|
84 |
for option, label in field:options () do |
|
85 |
fields = fields .. '\n * ' .. option .. ' - ' .. label |
|
86 |
end |
|
87 |
end |
|
88 |
|
|
89 |
if ftype == 'list-multi' or ftype == 'text-multi' or ftype == 'jid-multi' then |
|
90 |
fields = fields .. '\n Values: ' |
|
91 |
for vin, value in field:values () do |
|
92 |
fields = fields .. '\n * ' .. value |
|
93 |
end |
|
94 |
else |
|
95 |
fields = fields .. '\n Value: ' .. ( field:value () or '' ) |
|
96 |
end |
|
97 |
end |
|
98 |
print ( desc .. '\n' .. fields ) |
|
99 |
end |
|
100 |
else |
|
101 |
local text = '' |
|
102 |
for id, form in pairs ( forms ) do |
|
103 |
local title = form.form:desc () |
|
104 |
text = text .. '\n - ' .. id .. ' ' .. ( title or 'Untitled' ) .. ' [' .. form.form:type () .. ']' |
|
105 |
end |
|
106 |
if text ~= '' then |
|
107 |
print ( 'Forms list:' .. text ) |
|
108 |
else |
|
109 |
print ( 'No forms' ) |
|
110 |
end |
|
111 |
end |
|
112 |
end, true, form_cid ) |
|
113 |
|
|
114 |
-- vim: se ts=4: -- |