return nil
end
+ ffluci.i18n.loadc("cbi")
+
return map
end
end
end
+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
+
--[[
AbstractSection
]]--
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)
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
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
local sections = {}
for k, v in pairs(self.map.ucidata) do
if v[".type"] == self.sectiontype then
- sections[k] = v
+ if ffluci.util.validate(k, self.scope) then
+ sections[k] = v
+ end
end
end
return sections
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)
self.valid = nil
self.depends = nil
self.default = nil
+ self.size = nil
end
function AbstractValue.formvalue(self)
end
function AbstractValue.parse(self)
- local fvalue = self:validate(self:formvalue())
- if fvalue and not (fvalue == self:ucivalue()) then
- self:write(fvalue)
- end
+ 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.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)
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)
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
end
+-- Wrapper for "uci del"
+function Session.del(self, config, section, option)
+ return self:_uci2("del " .. _path(config, section, option))
+end
+
+function del(...)
+ return default:del(...)
+end
+
+
-- Wrapper for "uci get"
function Session.get(self, config, section, option)
return self:_uci("get " .. _path(config, section, option))
-- Not using ipairs because it is not reliable in case of nil arguments
arg.n = nil
for k,v in pairs(arg) do
- if k == 1 then
- result = "'" .. v:gsub("['.]", "") .. "'"
- elseif k < 4 then
- result = result .. ".'" .. v:gsub("['.]", "") .. "'"
- elseif k == 4 then
- result = result .. "='" .. v:gsub("'", "") .. "'"
+ if v then
+ v = tostring(v)
+ if k == 1 then
+ result = "'" .. v:gsub("['.]", "") .. "'"
+ elseif k < 4 then
+ result = result .. ".'" .. v:gsub("['.]", "") .. "'"
+ elseif k == 4 then
+ result = result .. "='" .. v:gsub("'", "") .. "'"
+ end
end
end
return result