examples/mc_forms.lua
changeset 67 d33ca5572e91
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/mc_forms.lua	Sat Mar 28 19:43:12 2009 +0200
@@ -0,0 +1,114 @@
+
+local form_cid = main.add_category { 'del', 'send' }
+local forms = { }
+
+-- public
+function insert_form ( form, submit, reject )
+	table.insert ( forms, {
+			form   = form,
+			submit = submit,
+			reject = reject,
+		} )
+	main.add_completion ( form_cid, tostring(#forms) )
+	print ( "You have new form. Use /form " .. #forms .. " to fill and submit or cancel it." )
+	return #forms
+end
+
+main.command ( 'form',
+	function ( args )
+		local id, action = tonumber (args[1]), args[2]
+		if forms[id] then
+			local form = forms[id].form
+			if action == 'send' then
+				forms[id].submit ( form )
+			elseif action == 'reject' then
+				forms[id].reject ( form )
+			elseif action == 'del' then
+				main.del_completion ( form_cid, id )
+				forms[id] = nil
+			elseif action then
+				local fname, value
+				if action == 'set' then
+					fname = args[3]
+					value = args[4]
+				else
+					fname = action
+					value = args[3]
+				end
+				local field = form:field ( fname )
+				if field then
+					if value then
+						field:value ( value )
+					else
+						field:clear ()
+					end
+				else
+					-- XXX create?
+					print ( 'Field not found: ' .. fname )
+				end
+			else
+				local desc = 'Form ' .. id .. '\n - Type: ' .. form:type ()
+				local title, instructions = form:desc ()
+				if title then
+					desc = desc .. '\n - Title: ' .. title
+				end
+				if instructions then
+					desc = desc .. '\n - Instructions: ' .. instructions
+				end
+
+				local fields = 'Fields:'
+				for index, field in form:fields () do
+					local ftype = field:type ()
+
+					fields = fields .. '\n - '
+					
+					if field:required () then
+						fields = fields .. '* '
+					end
+
+					local label, descr = field:desc ()
+					if label then
+						fields = fields .. label .. ' (' .. ( field.var or '' ) .. ')'
+					else
+						fields = fields .. ( field:name () or '<no name>' )
+					end
+					fields = fields .. ' [' .. ftype .. ']'
+					if descr then
+						fields = fields .. ': ' .. descr
+					end
+
+					if ftype == 'list-single' or ftype == 'list-multi' then
+						fields = fields .. '\n   Options:'
+						for option, label in field:options () do
+							fields = fields .. '\n    * ' .. option .. ' - ' .. label
+						end
+					end
+					
+					if ftype == 'list-multi' or ftype == 'text-multi' or ftype == 'jid-multi' then
+						fields = fields .. '\n   Values: '
+						for vin, value in field:values () do
+							fields = fields .. '\n    * ' .. value
+						end
+					else
+						fields = fields .. '\n   Value: ' .. ( field:value () or '' )
+					end
+				end
+				print ( desc .. '\n' .. fields )
+			end
+		else
+			local text = ''
+			for id, form in pairs ( forms ) do
+				local title = form.form:desc ()
+				text = text .. '\n - ' .. id .. ' ' .. ( title or 'Untitled' ) .. ' [' .. form.form:type () .. ']'
+			end
+			if text ~= '' then
+				print ( 'Forms list:' .. text )
+			else
+				print ( 'No forms' )
+			end
+		end
+	end, true, form_cid )
+
+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."
+
+-- vim: se ts=4: --