luci-0.10: Merge r7739
[project/luci.git] / applications / luci-asterisk / luasrc / model / cbi / asterisk / dialzones.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Jo-Philipp Wich <xm@subsignal.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: trunks.lua 4025 2009-01-11 23:37:21Z jow $
13
14 ]]--
15
16 local ast = require("luci.asterisk")
17 local uci = require("luci.model.uci").cursor()
18
19 --[[
20 Dialzone overview table
21 ]]
22
23 if not arg[1] then
24 zonemap = Map("asterisk", "Dial Zones", [[
25 Dial zones hold patterns of dialed numbers to match.
26 Each zone has one or more trunks assigned. If the first trunk is
27 congested, Asterisk will try to use the next available connection.
28 If all trunks fail, then the following zones in the parent dialplan
29 are tried.
30 ]])
31
32 local zones, znames = ast.dialzone.zones()
33
34 zonetbl = zonemap:section(Table, zones, "Zone Overview")
35 zonetbl.sectionhead = "Zone"
36 zonetbl.addremove = true
37 zonetbl.anonymous = false
38 zonetbl.extedit = luci.dispatcher.build_url(
39 "admin", "asterisk", "dialplans", "zones", "%s"
40 )
41
42 function zonetbl.cfgsections(self)
43 return znames
44 end
45
46 function zonetbl.parse(self)
47 for k, v in pairs(self.map:formvaluetable(
48 luci.cbi.REMOVE_PREFIX .. self.config
49 ) or {}) do
50 if k:sub(-2) == ".x" then k = k:sub(1, #k - 2) end
51 uci:delete("asterisk", k)
52 uci:save("asterisk")
53 self.data[k] = nil
54 for i = 1,#znames do
55 if znames[i] == k then
56 table.remove(znames, i)
57 break
58 end
59 end
60 end
61
62 Table.parse(self)
63 end
64
65 zonetbl:option(DummyValue, "description", "Description")
66 zonetbl:option(DummyValue, "addprefix")
67
68 match = zonetbl:option(DummyValue, "matches")
69 function match.cfgvalue(self, s)
70 return table.concat(zones[s].matches, ", ")
71 end
72
73 trunks = zonetbl:option(DummyValue, "trunk")
74 trunks.template = "asterisk/cbi/cell"
75 function trunks.cfgvalue(self, s)
76 return ast.tools.hyperlinks(zones[s].trunks)
77 end
78
79 return zonemap
80
81 --[[
82 Zone edit form
83 ]]
84
85 else
86 zoneedit = Map("asterisk", "Edit Dialzone")
87
88 entry = zoneedit:section(NamedSection, arg[1])
89 entry.title = "Zone %q" % arg[1];
90
91 back = entry:option(DummyValue, "_overview", "Back to dialzone overview")
92 back.value = ""
93 back.titleref = luci.dispatcher.build_url(
94 "admin", "asterisk", "dialplans", "zones"
95 )
96
97 desc = entry:option(Value, "description", "Description")
98 function desc.cfgvalue(self, s, ...)
99 return Value.cfgvalue(self, s, ...) or s
100 end
101
102 trunks = entry:option(MultiValue, "uses", "Used trunks")
103 trunks.widget = "checkbox"
104 uci:foreach("asterisk", "sip",
105 function(s)
106 if s.provider == "yes" then
107 trunks:value(
108 "SIP/%s" % s['.name'],
109 "SIP/%s (%s)" %{ s['.name'], s.host or 'n/a' }
110 )
111 end
112 end)
113
114
115 match = entry:option(DynamicList, "match", "Number matches")
116
117 intl = entry:option(DynamicList, "international", "Intl. prefix matches (optional)")
118
119 aprefix = entry:option(Value, "addprefix", "Add prefix to dial out (optional)")
120 ccode = entry:option(Value, "countrycode", "Effective countrycode (optional)")
121
122 lzone = entry:option(ListValue, "localzone", "Dialzone for local numbers")
123 lzone:value("", "no special treatment of local numbers")
124 for _, z in ipairs(ast.dialzone.zones()) do
125 lzone:value(z.name, "%q (%s)" %{ z.name, z.description })
126 end
127 --for _, v in ipairs(find_outgoing_contexts(zoneedit.uci)) do
128 -- lzone:value(unpack(v))
129 --end
130
131 lprefix = entry:option(Value, "localprefix", "Prefix for local calls (optional)")
132
133 return zoneedit
134
135 end