summaryrefslogtreecommitdiffstats
path: root/did-editdefinition-html.lsp
blob: a78a618b553f138fa2e1a486569ee445cdfd247c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<% local form, viewlibrary, page_info = ... 
require("viewfunctions")
require("format")
require("json")
%>

<style type="text/css">
	.alt {
		background: #ecf6fc;
	}
	.error {
		font-weight:bold;
		font-style:italic;
		color:#f33;
	}
</style>

<script type="text/javascript" src="<%= html.html_escape(page_info.wwwprefix) %>/js/jquery-latest.js"></script>
<script type="text/javascript">
	function deleteLine(){
		$(this).parent().parent().replaceWith();
	}
	function addLine(rule){
		var form = '<TABLE><TR><TD>Extension<TD><input type="text" value="' + rule.extension + '"></TR>';
		form = form + '<TR><TD>Start Time<TD><input type="text" value="' + rule.starttime + '"></TD>';
		form = form + '<TD>End Time<TD><input type="text" value="' + rule.endtime + '"></TD></TR>';
		form = form + '<TR><TD>Start Date<TD><input type="text" value="' + rule.startdate + '"></TD>';
		form = form + '<TD>End Date<TD><input type="text" value="' + rule.enddate + '"></TD></TR>';
		form = form + '<TR><TD colspan="4">Days of the Week: Mon<input type="checkbox"';
		if (rule.dayofweek.charAt(0) == '1'){
			form = form + " checked";
		};
		form = form + '> Tue<input type="checkbox"';
		if (rule.dayofweek.charAt(1) == '1'){
			form = form + " checked";
		};
		form = form + '> Wed<input type="checkbox"';
		if (rule.dayofweek.charAt(2) == '1'){
			form = form + " checked";
		};
		form = form + '> Thu<input type="checkbox"';
		if (rule.dayofweek.charAt(3) == '1'){
			form = form + " checked";
		};
		form = form + '> Fri<input type="checkbox"';
		if (rule.dayofweek.charAt(4) == '1'){
			form = form + " checked";
		};
		form = form + '> Sat<input type="checkbox"';
		if (rule.dayofweek.charAt(5) == '1'){
			form = form + " checked";
		};
		form = form + '> Sun<input type="checkbox"';
		if (rule.dayofweek.charAt(6) == '1'){
			form = form + " checked";
		};
		form = form + '></TD></TR></TABLE>'
		$("#rules").append(form);
	}
	function submitFile(){
		var file = "";
		var lines = $("#rules").find("table");
		lines.each(function(){
			var elem = jQuery(this);
			elem.find("input:text").each(function(){
				file = file + jQuery(this).val() + ", ";
			});
			elem.find("input:checkbox").each(function(){
				if (jQuery(this).attr("checked")){
					file = file + "1";
				} else {
					file = file + "0";
				};
			});
			file = file + "\n";
		});
		$("input[name='rules']").val(file);
	}
	function goBack(){
		history.back();
		return false;
	}

	var emptyrule = {"starttime":"","enddate":"","extension":"","startdate":"","dayofweek":"","endtime":""}
	var rules = <% -- Generate the data structure in Lua and then convert to json
			local rules = {}
			form.value.rules.value = format.dostounix(form.value.rules.value)
			for line in string.gmatch(form.value.rules.value, "([^\n]+)") do
				local tabs = format.string_to_table(line, "%s*,%s*")
				if #tabs > 0 then
					rules[#rules+1] = {extension=tabs[1], starttime=tabs[2], endtime=tabs[3], startdate=tabs[4], enddate=tabs[5], dayofweek=tabs[6]}
				end
			end
			io.write( json.encode(rules) ) %>;

	$(function(){
		jQuery.each(rules, function(i,rule){
 			addLine(rule);
		});
		addLine(emptyrule);
		$("#rules table:even").addClass("alt");
		$("input.submit").click(submitFile);
		$("#cancel").click(goBack);
	});
</script>

<% if form.descr or form.errtxt then
	session.didresult = form.descr
	form.descr = nil
end %>
<% if session.didresult or form.errtxt then %>
<H1>Last Result</H1><DL>
<% if session.didresult then %>
<P CLASS="descr"><%= string.gsub(html.html_escape(session.didresult), "\n", "<BR>") %></P>
<% else
	io.write('<P CLASS="error">' .. string.gsub(html.html_escape(form.errtxt), "\n", "<BR>") .. " " .. html.html_escape(form.value.did.value) .. "<BR>\n")
	for name,val in pairs(form.value) do
		if val.errtxt then
			io.write(string.gsub(html.html_escape(val.errtxt), "\n", "<BR>").."<BR>\n")
		end
	end
	io.write("</P>\n")
	form.errtxt = nil
end %>
</DL></H1>
<% end %>

<H1><%= html.html_escape(form.label) %></H1>
<% displayformstart(form, page_info) %>
<%
	local order = {"did", "description", "identification", "department", "rules"}
	if page_info.action == "editdefinition" then
		form.value.did.readonly = true
	end
	form.value.rules.type = "hidden"
	for i,name in ipairs(order) do
		displayformitem(form.value[name], name)
	end
%>
</DL>
<H2>Rules</H2>
<DL><div id="rules">
</div>
<% if form.value.rules.errtxt then %><P CLASS='error'><%= string.gsub(html.html_escape(form.value.rules.errtxt), "\n", "<BR>") %></P><% end %>
</DL>

<H2>Save Changes</H2>
<DL>
<DT></DT><DD><input class="submit" type="submit" name="Cancel" value="Cancel" id="cancel"></DD>
<% displayformend(form) %>