OXYGEN #3: Add "back" button to UCI apply/revert/changes pages
[project/luci.git] / modules / admin-full / 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         local redir = luci.http.formvalue("redir", true) or 
19           luci.dispatcher.build_url(unpack(luci.dispatcher.context.request))
20         
21         entry({"admin", "uci"}, nil, i18n("config"))
22         entry({"admin", "uci", "changes"}, call("action_changes"), i18n("changes"), 40).query = {redir=redir}
23         entry({"admin", "uci", "revert"}, call("action_revert"), i18n("revert"), 30).query = {redir=redir}
24         entry({"admin", "uci", "apply"}, call("action_apply"), i18n("apply"), 20).query = {redir=redir}
25         entry({"admin", "uci", "saveapply"}, call("action_apply"), i18n("saveapply"), 10).query = {redir=redir}
26 end
27
28 function convert_changes(changes)
29         local util = require "luci.util"
30         
31         local ret
32         for r, tbl in pairs(changes) do
33                 for s, os in pairs(tbl) do
34                         for o, v in pairs(os) do
35                                 ret = (ret and ret.."\n" or "") .. "%s%s.%s%s%s" % {
36                                         v == "" and "-" or "",
37                                         r,
38                                         s,
39                                         o ~= ".type" and "."..o or "",
40                                         v ~= "" and "="..util.pcdata(v) or ""
41                                 }
42                         end
43                 end
44         end
45         return ret
46 end
47
48 function action_changes()
49         local changes = convert_changes(luci.model.uci.cursor():changes())
50         luci.template.render("admin_uci/changes", {changes=changes})
51 end
52
53 function action_apply()
54         local path = luci.dispatcher.context.path
55         local uci = luci.model.uci.cursor()
56         local changes = uci:changes()
57         local reload = {}
58
59         -- Collect files to be applied and commit changes
60         for r, tbl in pairs(changes) do
61                 table.insert(reload, r)
62                 if path[#path] ~= "apply" then
63                         uci:load(r)
64                         uci:commit(r)
65                         uci:unload(r)
66                 end
67         end
68         
69         local function _reload()
70                 local cmd = uci:apply(reload, true)
71                 return io.popen(cmd)
72         end
73         
74         luci.template.render("admin_uci/apply", {changes=convert_changes(changes), reload=_reload})
75 end
76
77
78 function action_revert()
79         local uci = luci.model.uci.cursor()
80         local changes = uci:changes()
81
82         -- Collect files to be reverted
83         for r, tbl in pairs(changes) do
84                 uci:load(r)
85                 uci:revert(r)
86                 uci:unload(r)
87         end
88         
89         luci.template.render("admin_uci/revert", {changes=convert_changes(changes)})
90 end