Moved luci.sys.exec, luci.sys.execl and luci.sys.bigendian to luci.util
[project/luci.git] / libs / cbi / luasrc / cbi.lua
index 6aec2fbbbc9caae17cfd2750fb433d55ec797f2d..d5be48edba96c9133c0584175226b5b19b0701c0 100644 (file)
@@ -144,17 +144,44 @@ function Map.__init__(self, config, ...)
        Node._i18n(self, config, nil, nil, ...)
 
        self.config = config
+       self.parsechain = {self.config}
        self.template = "cbi/map"
        if not uci.load(self.config) then
                error("Unable to read UCI data: " .. self.config)
        end
 end
 
+
+-- Chain foreign config
+function Map.chain(self, config)
+       table.insert(self.parsechain, config)
+end
+
 -- Use optimized UCI writing
 function Map.parse(self, ...)
        Node.parse(self, ...)
-       uci.save(self.config)
-       uci.unload(self.config)
+       for i, config in ipairs(self.parsechain) do
+               uci.save(config)
+       end
+       if luci.http.formvalue("cbi.apply") then
+               for i, config in ipairs(self.parsechain) do
+                       uci.commit(config)
+                       if luci.config.uci_oncommit and luci.config.uci_oncommit[config] then
+                               luci.util.exec(luci.config.uci_oncommit[config])
+                       end
+
+                       -- Refresh data because commit changes section names
+                       uci.unload(config)
+                       uci.load(config)
+               end
+
+               -- Reparse sections
+               Node.parse(self, ...)
+
+       end
+       for i, config in ipairs(self.parsechain) do
+               uci.unload(config)
+       end
 end
 
 -- Creates a child section
@@ -568,7 +595,10 @@ function AbstractValue.render(self, s, scope)
                        if cond then
                                return string.format(
                                        ' %s="%s"', tostring(key),
-                                       tostring( val or scope[key] or self[key] or "" )
+                                       tostring( val
+                                        or scope[key]
+                                        or (type(self[key]) ~= "function" and self[key])
+                                        or "" )
                                )
                        else
                                return ''
@@ -615,17 +645,14 @@ Value = class(AbstractValue)
 function Value.__init__(self, ...)
        AbstractValue.__init__(self, ...)
        self.template  = "cbi/value"
-
-       self.maxlength  = nil
+       self.keylist = {}
+       self.vallist = {}
 end
 
--- This validation is a bit more complex
-function Value.validate(self, val)
-       if self.maxlength and tostring(val):len() > self.maxlength then
-               val = nil
-       end
-
-       return val
+function Value.value(self, key, val)
+       val = val or key
+       table.insert(self.keylist, tostring(key))
+       table.insert(self.vallist, tostring(val))
 end