* CBI: updates
[project/luci.git] / src / ffluci / cbi.lua
index 5a20aad..ac5c1dd 100644 (file)
@@ -41,10 +41,10 @@ function load(cbimap)
        require("ffluci.i18n")
        
        local cbidir = ffluci.fs.dirname(ffluci.util.__file__()) .. "model/cbi/"
-       local func = loadfile(cbidir..cbimap..".lua")
+       local func, err = loadfile(cbidir..cbimap..".lua")
        
        if not func then
-               error("Unable to load CBI map: " .. cbimap)
+               error(err)
                return nil
        end
        
@@ -59,6 +59,8 @@ function load(cbimap)
                return nil
        end
        
+       ffluci.i18n.loadc("cbi")
+       
        return map
 end
 
@@ -86,6 +88,12 @@ function Node.render(self)
        ffluci.template.render(self.template, {self=self})
 end
 
+function Node.render_children(self)
+       for k, node in ipairs(self.children) do
+               node:render()
+       end
+end
+
 
 --[[
 Map - A map describing a configuration file 
@@ -96,6 +104,27 @@ function Map.__init__(self, config, ...)
        Node.__init__(self, ...)
        self.config = config
        self.template = "cbi/map"
+       self.uci = ffluci.model.uci.Session()
+end
+
+function Map.parse(self)
+       self.ucidata = self.uci:show(self.config)
+       if not self.ucidata then
+               error("Unable to read UCI data: " .. self.config)
+       else
+               self.ucidata = self.ucidata[self.config]
+       end
+       Node.parse(self)
+end
+
+function Map.render(self)
+       self.ucidata = self.uci:show(self.config)
+       if not self.ucidata then
+               error("Unable to read UCI data: " .. self.config)
+       else
+               self.ucidata = self.ucidata[self.config]
+       end
+       Node.render(self)       
 end
 
 function Map.section(self, class, ...)
@@ -110,9 +139,16 @@ function Map.section(self, class, ...)
        end
 end
 
-function Map.read(self)
-       self.ucidata = self.ucidata or ffluci.model.uci.show(self.config)[self.config]
-       return self.ucidata
+function Map.add(self, sectiontype)
+       return self.uci:add(self.config, sectiontype)
+end
+
+function Map.set(self, section, option, value)
+       return self.uci:set(self.config, section, option, value)
+end
+
+function Map.remove(self, section, option)
+       return self.uci:del(self.config, section, option)
 end
 
 --[[
@@ -162,7 +198,8 @@ end
 TypedSection - A (set of) configuration section(s) defined by the type
        addremove:      Defines whether the user can add/remove sections of this type
        anonymous:  Allow creating anonymous sections
-       valid:          a table with valid names or a function returning nil if invalid
+       valid:          a list of names or a validation function for creating sections 
+       scope:          a list of names or a validation function for editing sections
 ]]--
 TypedSection = class(AbstractSection)
 
@@ -170,9 +207,87 @@ function TypedSection.__init__(self, ...)
        AbstractSection.__init__(self, ...)
        self.template  = "cbi/tsection"
        
-       self.addremove = true
-       self.anonymous = false
-       self.valid     = nil
+       self.addremove   = true
+       self.anonymous   = false
+       self.valid       = nil
+       self.scope               = nil
+end
+
+function TypedSection.create(self, name)
+       if name then    
+               self.map:set(name, nil, self.sectiontype)
+       else
+               name = self.map:add(self.sectiontype)
+       end
+       
+       for k,v in pairs(self.children) do
+               if v.default then
+                       self.map:set(name, v.option, v.default)
+               end
+       end
+end
+
+function TypedSection.parse(self)
+       if self.addremove then
+               local crval = "cbi.cts." .. self.config .. "." .. self.sectiontype
+               local name  = ffluci.http.formvalue(crval)
+               if self.anonymous then
+                       if name then
+                               self:create()
+                       end
+               else            
+                       if name then
+                               name = ffluci.util.validate(name, self.valid)
+                               if not name then
+                                       self.err_invalid = true
+                               end             
+                               if name and name:len() > 0 then
+                                       self:create(name)
+                               end
+                       end
+               end
+               
+               
+               crval = "cbi.rts." .. self.config
+               name = ffluci.http.formvalue(crval)
+               if type(name) == "table" then
+                       for k,v in pairs(name) do
+                               if ffluci.util.validate(k, self.valid) then
+                                       self:remove(k)
+                               end
+                       end
+               end             
+       end
+       
+       for k, v in pairs(self:ucisections()) do
+               for i, node in ipairs(self.children) do
+                       node.section = k
+                       node:parse()
+               end 
+       end
+end
+
+function TypedSection.remove(self, name)
+       return self.map:remove(name)
+end
+
+function TypedSection.render_children(self, section)
+       for k, node in ipairs(self.children) do
+               node.section = section
+               node:render()
+       end
+end
+
+function TypedSection.ucisections(self)
+       local sections = {}
+       for k, v in pairs(self.map.ucidata) do
+               if v[".type"] == self.sectiontype then
+                       if ffluci.util.validate(k, self.scope) then
+                               sections[k] = v
+                       end
+               end
+       end
+       return sections 
 end
 
 
@@ -182,6 +297,7 @@ AbstractValue - An abstract Value Type
        valid:          A function returning the value if it is valid otherwise nil 
        depends:        A table of option => value pairs of which one must be true
        default:        The default value
+       size:           The size of the input fields
 ]]--
 AbstractValue = class(Node)
 
@@ -192,32 +308,48 @@ function AbstractValue.__init__(self, option, ...)
        self.valid   = nil
        self.depends = nil
        self.default = nil
+       self.size    = nil
 end
 
-
 function AbstractValue.formvalue(self)
-       local key = "uci."..self.map.config.."."..self.section.."."..self.option
+       local key = "cbid."..self.map.config.."."..self.section.."."..self.option
        return ffluci.http.formvalue(key)
 end
 
+function AbstractValue.parse(self)
+       local fvalue = self:formvalue()
+       if fvalue then
+               fvalue = self:validate(fvalue)
+               if not fvalue then
+                       self.err_invalid = true
+               end
+               if fvalue and not (fvalue == self:ucivalue()) then
+                       self:write(fvalue)
+               end 
+       end
+end
+
 function AbstractValue.ucivalue(self)
-       return self.map:read()[self.section][self.option]
+       return self.map.ucidata[self.section][self.option]
 end
 
-function AbstractValue.validate(self, value)
-       return ffluci.util.validate(value, nil, nil, self.valid)
+function AbstractValue.validate(self, val)
+       return ffluci.util.validate(val, self.valid)
 end
 
 function AbstractValue.write(self, value)
-       ffluci.model.uci.set(self.config, self.section, self.option, value)
+       return self.map:set(self.section, self.option, value)
 end
 
 
+
+
 --[[
 Value - A one-line value
        maxlength:      The maximum length
        isnumber:       The value must be a valid (floating point) number
        isinteger:  The value must be a valid integer
+       ispositive: The value must be positive (and a number)
 ]]--
 Value = class(AbstractValue)
 
@@ -225,9 +357,17 @@ function Value.__init__(self, ...)
        AbstractValue.__init__(self, ...)
        self.template  = "cbi/value"
        
-       self.maxlength = nil
-       self.isnumber  = false
-       self.isinteger = false
+       self.maxlength  = nil
+       self.isnumber   = false
+       self.isinteger  = false
+end
+
+function Value.validate(self, val)
+       if self.maxlength and tostring(val):len() > self.maxlength then
+               val = nil
+       end
+       
+       return ffluci.util.validate(val, self.valid, self.isnumber, self.isinteger)
 end
 
 
@@ -243,7 +383,7 @@ function ListValue.__init__(self, ...)
        self.list = {}
 end
 
-function ListValue.addValue(self, key, val)
+function ListValue.add_value(self, key, val)
        val = val or key
        self.list[key] = val
 end
\ No newline at end of file