luci-base: introduce luci.dispatcher.lookup()
[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
9           luci.dispatcher.build_url(unpack(luci.dispatcher.context.request))
10
11         entry({"admin", "uci"}, nil, _("Configuration"))
12         entry({"admin", "uci", "changes"}, call("action_changes"), _("Changes"), 40).query = {redir=redir}
13         entry({"admin", "uci", "revert"}, post("action_revert"), _("Revert"), 30).query = {redir=redir}
14         entry({"admin", "uci", "apply"}, post("action_apply"), _("Apply"), 20).query = {redir=redir}
15         entry({"admin", "uci", "saveapply"}, post("action_apply"), _("Save &#38; Apply"), 10).query = {redir=redir}
16 end
17
18 function action_changes()
19         local uci = luci.model.uci.cursor()
20         local changes = uci:changes()
21
22         luci.template.render("admin_uci/changes", {
23                 changes = next(changes) and changes
24         })
25 end
26
27 function action_apply()
28         local path = luci.dispatcher.context.path
29         local uci = luci.model.uci.cursor()
30         local changes = uci:changes()
31         local reload = {}
32
33         -- Collect files to be applied and commit changes
34         for r, tbl in pairs(changes) do
35                 table.insert(reload, r)
36                 if path[#path] ~= "apply" then
37                         uci:load(r)
38                         uci:commit(r)
39                         uci:unload(r)
40                 end
41         end
42
43         luci.template.render("admin_uci/apply", {
44                 changes = next(changes) and changes,
45                 configs = reload
46         })
47 end
48
49
50 function action_revert()
51         local uci = luci.model.uci.cursor()
52         local changes = uci:changes()
53
54         -- Collect files to be reverted
55         for r, tbl in pairs(changes) do
56                 uci:load(r)
57                 uci:revert(r)
58                 uci:unload(r)
59         end
60
61         luci.template.render("admin_uci/revert", {
62                 changes = next(changes) and changes
63         })
64 end