diff options
Diffstat (limited to 'lbu-controller.lua')
-rw-r--r-- | lbu-controller.lua | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/lbu-controller.lua b/lbu-controller.lua new file mode 100644 index 0000000..51a0538 --- /dev/null +++ b/lbu-controller.lua @@ -0,0 +1,99 @@ +module(..., package.seeall) + +local list_redir = function(self) + self.conf.action = "read" + self.conf.type = "redir" + error(self.conf) +end + +local cfgfile + +mvc={} +mvc.on_load = function(self, parent) + cfgfile = self:new("cfgfile") + if (self.worker[self.conf.action] == nil) or (self.conf.action == "init") then + self.worker[self.conf.action] = list_redir(self) + end +end + +-- Public methods +-- <prefix>/hostname/get + +local function getNotes(self) + ret = {} + for k,v in pairs(cfgfile.model:list(nil)) do + if v.status then + ret[#ret + 1] = { + content = "There are some configuration changes. Please do not forget to save." + } + break + end + end + return ret +end + +read = function(self) + return { + list=cfgfile.model:list(function(x) return x.app == "lbu" end), + script=ENV["SCRIPT_NAME"], + prefix=self.conf.prefix, + controller=self.conf.controller, + action="update", + note=getNotes(self), + } +end + +update = function(self) + local id = tonumber(self.clientdata.id) or -1 + local result + local data + + result, data = cfgfile.model:get(id) + if not result then return list_redir(self) end + + if self.clientdata.cmd then + for k,v in pairs (data) do + if self.clientdata[k] then + data[k].value = self.clientdata[k] + end + end + result, data = cfgfile.model:set(id, data) + if result then return list_redir(self) end + end + + data.cmd = cfe { type="action", value="save", label="action" } + return cfe{ type="form", + option={ script=ENV["SCRIPT_NAME"], + prefix=self.conf.prefix, + controller = self.conf.controller, + action = "update", + extra = ""}, + value = data} +end + +function commit(self) + local ret = { + script=ENV["SCRIPT_NAME"], + prefix=self.conf.prefix, + controller = self.conf.controller, + action="commit", + data={}, + title="LBU", + text={}, + } + if self.clientdata.commit then + local result, report = self.model:commit() + local label = result and "Report" or "Error" + ret.text[#ret.text + 1] = { label=label, content=report } + else + for i,v in ipairs(self.model:list(nil)) do + ret.data[#ret.data + 1] = { + status = v.status, + name = v.name, + } + end + end + ret.note=getNotes(self) + return ret +end + |