31e04051b4af87457d653500cf21b566ebadd2f2
[project/luci.git] / modules / admin-mini / luasrc / controller / mini / uci.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15 module("luci.controller.mini.uci", package.seeall)
16
17 function index()
18         local i18n = luci.i18n.translate
19         
20         entry({"mini", "uci"}, nil, i18n("config"))
21         entry({"mini", "uci", "changes"}, call("action_changes"), i18n("changes"), 30)
22         entry({"mini", "uci", "revert"}, call("action_revert"), i18n("revert"), 20)
23         entry({"mini", "uci", "apply"}, call("action_apply"), i18n("saveapply"), 10)
24 end
25
26 function convert_changes(changes)
27         local ret = {}
28         for r, tbl in pairs(changes) do
29                 for s, os in pairs(tbl) do
30                         for o, v in pairs(os) do
31                                 local val, str
32                                 if (v == "") then
33                                         str = "-"
34                                         val = ""
35                                 else
36                                         str = ""
37                                         val = "="..luci.util.pcdata(v)
38                                 end
39                                 str = r.."."..s
40                                 if o ~= ".type" then
41                                         str = str.."."..o
42                                 end
43                                 table.insert(ret, str..val)
44                         end
45                 end
46         end
47         return table.concat(ret, "\n")
48 end
49
50 function action_changes()
51         local changes = convert_changes(luci.model.uci.cursor():changes())
52         luci.template.render("mini/uci_changes", {changes=changes})
53 end
54
55 function action_apply()
56         local uci = luci.model.uci.cursor()
57         local changes = uci:changes()
58         local output  = ""
59         
60         if changes then
61                 local com = {}
62                 local run = {}
63                 
64                 -- Collect files to be applied and commit changes
65                 for r, tbl in pairs(changes) do
66                         if r then
67                                 uci:load(r)
68                                 uci:commit(r)
69                                 uci:unload(r)
70                                 if luci.config.uci_oncommit and luci.config.uci_oncommit[r] then
71                                         run[luci.config.uci_oncommit[r]] = true
72                                 end
73                         end
74                 end
75                 
76                 -- Search for post-commit commands
77                 for cmd, i in pairs(run) do
78                         output = output .. cmd .. ":" .. luci.util.exec(cmd) .. "\n"
79                 end
80         end
81         
82         
83         luci.template.render("mini/uci_apply", {changes=convert_changes(changes), output=output})
84 end
85
86
87 function action_revert()
88         local uci = luci.model.uci.cursor()
89         local changes = uci:changes()
90         if changes then
91                 local revert = {}
92                 
93                 -- Collect files to be reverted
94                 for r, tbl in pairs(changes) do
95                         uci:load(r)
96                         uci:revert(r)
97                         uci:unload(r)
98                 end
99         end
100         
101         luci.template.render("mini/uci_revert", {changes=convert_changes(changes)})
102 end