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