Merge pull request #2749 from dibdot/wg
[oweals/luci.git] / modules / luci-base / luasrc / cbi.lua
index 4dc1f2abad56e95b158a06b6f0c5c392565770c5..450e413916774669e9e7c4bca75247ab6c25c407 100644 (file)
@@ -388,21 +388,21 @@ function Map.parse(self, readinput, ...)
 
        if self.save then
                self:_run_hooks("on_save", "on_before_save")
+               local i, config
                for i, config in ipairs(self.parsechain) do
                        self.uci:save(config)
                end
                self:_run_hooks("on_after_save")
                if (not self.proceed and self.flow.autoapply) or luci.http.formvalue("cbi.apply") then
                        self:_run_hooks("on_before_commit")
-                       for i, config in ipairs(self.parsechain) do
-                               self.uci:commit(config)
-
-                               -- Refresh data because commit changes section names
-                               self.uci:load(config)
+                       if self.apply_on_parse == false then
+                               for i, config in ipairs(self.parsechain) do
+                                       self.uci:commit(config)
+                               end
                        end
                        self:_run_hooks("on_commit", "on_after_commit", "on_before_apply")
-                       if self.apply_on_parse then
-                               self.uci:apply(self.parsechain)
+                       if self.apply_on_parse == true or self.apply_on_parse == false then
+                               self.uci:apply(self.apply_on_parse)
                                self:_run_hooks("on_apply", "on_after_apply")
                        else
                                -- This is evaluated by the dispatcher and delegated to the
@@ -1199,19 +1199,20 @@ function TypedSection.parse(self, novld)
                        if name then
                                -- Ignore if it already exists
                                if self:cfgvalue(name) then
-                                       name = nil;
-                               end
-
-                               name = self:checkscope(name)
-
-                               if not name then
+                                       name = nil
                                        self.err_invalid = true
-                               end
+                               else
+                                       name = self:checkscope(name)
 
-                               if name and #name > 0 then
-                                       created = self:create(name, origin) and name
-                                       if not created then
-                                               self.invalid_cts = true
+                                       if not name then
+                                               self.err_invalid = true
+                                       end
+
+                                       if name and #name > 0 then
+                                               created = self:create(name, origin) and name
+                                               if not created then
+                                                       self.invalid_cts = true
+                                               end
                                        end
                                end
                        end
@@ -1226,13 +1227,14 @@ function TypedSection.parse(self, novld)
                local stval = RESORT_PREFIX .. self.config .. "." .. self.sectiontype
                local order = self.map:formvalue(stval)
                if order and #order > 0 then
-                       local sid
-                       local num = 0
+                       local sids, sid = { }, nil
                        for sid in util.imatch(order) do
-                               self.map.uci:reorder(self.config, sid, num)
-                               num = num + 1
+                               sids[#sids+1] = sid
+                       end
+                       if #sids > 0 then
+                               self.map.uci:reorder(self.config, sids)
+                               self.changed = true
                        end
-                       self.changed = (num > 0)
                end
        end
 
@@ -1345,6 +1347,18 @@ function AbstractValue.deplist2json(self, section, deplist)
        return util.serialize_json(deps)
 end
 
+-- Serialize choices
+function AbstractValue.choices(self)
+       if type(self.keylist) == "table" and #self.keylist > 0 then
+               local i, k, v = nil, nil, {}
+               for i, k in ipairs(self.keylist) do
+                       v[k] = self.vallist[i] or k
+               end
+               return v
+       end
+       return nil
+end
+
 -- Generates the unique CBID
 function AbstractValue.cbid(self, section)
        return "cbid."..self.map.config.."."..section.."."..self.option
@@ -1416,6 +1430,12 @@ function AbstractValue.parse(self, section, novld)
                        self:add_error(section, "invalid", val_err)
                end
 
+               if self.alias then
+                       self.section.aliased = self.section.aliased or {}
+                       self.section.aliased[section] = self.section.aliased[section] or {}
+                       self.section.aliased[section][self.alias] = true
+               end
+
                if fvalue and (self.forcewrite or not (fvalue == cvalue)) then
                        if self:write(section, fvalue) then
                                -- Push events
@@ -1425,10 +1445,16 @@ function AbstractValue.parse(self, section, novld)
                end
        else                                                    -- Unset the UCI or error
                if self.rmempty or self.optional then
-                       if self:remove(section) then
-                               -- Push events
-                               self.section.changed = true
-                               --luci.util.append(self.map.events, self.events)
+                       if not self.alias or
+                          not self.section.aliased or
+                          not self.section.aliased[section] or
+                          not self.section.aliased[section][self.alias]
+                       then
+                               if self:remove(section) then
+                                       -- Push events
+                                       self.section.changed = true
+                                       --luci.util.append(self.map.events, self.events)
+                               end
                        end
                elseif cvalue ~= fvalue and not novld then
                        -- trigger validator with nil value to get custom user error msg.
@@ -1454,7 +1480,7 @@ function AbstractValue.cfgvalue(self, section)
        if self.tag_error[section] then
                value = self:formvalue(section)
        else
-               value = self.map:get(section, self.option)
+               value = self.map:get(section, self.alias or self.option)
        end
 
        if not value then
@@ -1495,12 +1521,12 @@ AbstractValue.transform = AbstractValue.validate
 
 -- Write to UCI
 function AbstractValue.write(self, section, value)
-       return self.map:set(section, self.option, value)
+       return self.map:set(section, self.alias or self.option, value)
 end
 
 -- Remove from UCI
 function AbstractValue.remove(self, section)
-       return self.map:del(section, self.option)
+       return self.map:del(section, self.alias or self.option)
 end
 
 
@@ -1623,16 +1649,16 @@ function ListValue.__init__(self, ...)
        AbstractValue.__init__(self, ...)
        self.template  = "cbi/lvalue"
 
-       self.keylist = {}
-       self.vallist = {}
-       self.deplist = {}
        self.size   = 1
        self.widget = "select"
+
+       self:reset_values()
 end
 
 function ListValue.reset_values(self)
        self.keylist = {}
        self.vallist = {}
+       self.deplist = {}
 end
 
 function ListValue.value(self, key, val, ...)
@@ -1667,11 +1693,10 @@ function MultiValue.__init__(self, ...)
        AbstractValue.__init__(self, ...)
        self.template = "cbi/mvalue"
 
-       self.keylist = {}
-       self.vallist = {}
-
        self.widget = "checkbox"
        self.delimiter = " "
+
+       self:reset_values()
 end
 
 function MultiValue.render(self, ...)
@@ -1685,6 +1710,7 @@ end
 function MultiValue.reset_values(self)
        self.keylist = {}
        self.vallist = {}
+       self.deplist = {}
 end
 
 function MultiValue.value(self, key, val)
@@ -1759,8 +1785,7 @@ function DynamicList.__init__(self, ...)
        AbstractValue.__init__(self, ...)
        self.template  = "cbi/dynlist"
        self.cast = "table"
-       self.keylist = {}
-       self.vallist = {}
+       self:reset_values()
 end
 
 function DynamicList.reset_values(self)
@@ -1834,6 +1859,15 @@ function DynamicList.formvalue(self, section)
 end
 
 
+DropDown = class(MultiValue)
+
+function DropDown.__init__(self, ...)
+       ListValue.__init__(self, ...)
+       self.template = "cbi/dropdown"
+       self.delimiter = " "
+end
+
+
 --[[
 TextValue - A multi-line value
        rows:   Rows