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