c3bf668521b3c131c148c1d696e092c2bc71c0bf
[project/luci.git] / modules / luci-mod-admin-full / luasrc / controller / admin / uci.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2010-2015 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
4
5 module("luci.controller.admin.uci", package.seeall)
6
7 function index()
8         local redir = luci.http.formvalue("redir", true) or table.concat(disp.context.request, "/")
9
10         entry({"admin", "uci"}, nil, _("Configuration"))
11         entry({"admin", "uci", "changes"}, call("action_changes"), _("Changes"), 40).query = {redir=redir}
12         entry({"admin", "uci", "revert"}, post("action_revert"), _("Revert"), 30).query = {redir=redir}
13         entry({"admin", "uci", "apply"}, post("action_apply"), _("Apply"), 20).query = {redir=redir}
14         entry({"admin", "uci", "saveapply"}, post("action_apply"), _("Save &#38; Apply"), 10).query = {redir=redir}
15 end
16
17 function action_changes()
18         local uci = luci.model.uci.cursor()
19         local changes = uci:changes()
20
21         luci.template.render("admin_uci/changes", {
22                 changes = next(changes) and changes
23         })
24 end
25
26 function action_apply()
27         local path = luci.dispatcher.context.path
28         local uci = luci.model.uci.cursor()
29         local changes = uci:changes()
30         local reload = {}
31
32         -- Collect files to be applied and commit changes
33         for r, tbl in pairs(changes) do
34                 table.insert(reload, r)
35                 if path[#path] ~= "apply" then
36                         uci:load(r)
37                         uci:commit(r)
38                         uci:unload(r)
39                 end
40         end
41
42         luci.template.render("admin_uci/apply", {
43                 changes = next(changes) and changes,
44                 configs = reload
45         })
46 end
47
48
49 function action_revert()
50         local uci = luci.model.uci.cursor()
51         local changes = uci:changes()
52
53         -- Collect files to be reverted
54         for r, tbl in pairs(changes) do
55                 uci:load(r)
56                 uci:revert(r)
57                 uci:unload(r)
58         end
59
60         luci.template.render("admin_uci/revert", {
61                 changes = next(changes) and changes
62         })
63 end