summaryrefslogtreecommitdiffstats
path: root/app/acf_cli-controller.lua
blob: c8714550d26ef1e6a76147d21f659857e571bbc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
local mymodule = {}

posix = require("posix")
session = require("session")

local parent_exception_handler

mymodule.mvc = {}
mymodule.mvc.on_load = function (self, parent)
	-- Make sure we have some kind of sane defaults for libdir
	self.conf.libdir = self.conf.libdir or ( string.match(self.conf.appdir, "[^,]+/") .. "/lib/" )
	self.conf.script = ""
	self.conf.viewtype = "serialized"

	parent_exception_handler = parent.exception_handler

	-- this sets the package path for us and our children
	for p in string.gmatch(self.conf.libdir, "[^,]+") do
		package.path=  p .. "?.lua;" .. package.path
	end

	self.session = {}
end

mymodule.exception_handler = function (self, message )
	print(session.serialize("exception", message))
	parent_exception_handler(self, message)
end

mymodule.handle_clientdata = function(form, clientdata, group)
	clientdata = clientdata or {}
	form.errtxt = nil
	for n,value in pairs(form.value) do
		value.errtxt = nil
		local name = n
		if group then name = group.."."..name end
		if value.type == "group" then
			mymodule.handle_clientdata(value, clientdata, name)
		elseif value.readonly then
			-- Don't update readonly values
		-- Don't update from the default unless a value exists
		elseif value.type == "boolean" and clientdata[name] then
			value.value = (clientdata[name] == "true")
		elseif value.type == "multi" or value.type == "list" then
			-- for cli we use name[num] as the name
			local temp = {}
			for n,val in pairs(clientdata) do
				if string.find(n, "^"..name.."%[%d+%]$") then
					temp[tonumber(string.match(n, "%[(%d+)%]$"))] = val
				end
			end
			-- Use clientdata[name] to specify empty list
			if #temp > 0 or clientdata[name] then
				value.value = temp
			end
		else
			value.value = clientdata[name] or value.value
		end
	end
end

return mymodule