File: mod_wsf_edit.spl

package info (click to toggle)
spl 1.0~pre2-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,240 kB
  • ctags: 1,987
  • sloc: ansic: 15,272; yacc: 3,167; sh: 272; makefile: 186; xml: 156
file content (260 lines) | stat: -rw-r--r-- 7,417 bytes parent folder | download | duplicates (5)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 *  SPL - The SPL Programming Language
 *  Copyright (C) 2004, 2005  Clifford Wolf <clifford@clifford.at>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  mod_wsf_edit.spl: A WSF Component for editing data
 */

/**
 * A module which provides a generic WSF component for creating edit frontends
 * for any kind of data tuples. See [[wsf_edit_sql:]] for an implementation.
 */

load "wsf";

/**
 * This is a base object for WSF components which assemble edit frontends for
 * any kind of data tuples. Usually one would not use this object directly but
 * an object which is derived from it, such as [[wsf_edit_sql:WsfEditSql]].
 *
 * A 'data tuple' may be anything that can be represented as list of key-value
 * pairs, such as a tuple from an SQL table.
 *
 * The basic idea is that the methods [[.edit_load()]] and [[.edit_save]] are
 * overloaded so they can load and save the data tuple to be edited.
 *
 * The [[.get_html()]] method should also be overloaded so it evaluates a
 * template which is using the methods [[.edit_input()]], [[.edit_textarea]]
 * and [[.edit_select()]] for creating the form elements.
 *
 * An example for using [[WsfEdit]] can be found in the documentation of
 * [[wsf_edit_sql:WsfEditSql]].
 *
 * This object is derived from [[wsf:WsfComponent]].
 */
object WsfEdit WsfComponent
{
	/**
	 * A hash containing the data tuple. When [[.edit_save()]] is called,
	 * this contains the data sent to the browser, not the new data
	 * returned by the user. This can be used to check if the data has
	 * actually been modified.
	 */
	var edit_data;

	/**
	 * The fields included in the edit form generated. This can be used to
	 * check which fields in the tuple have actually been shown to the user.
	 * The [[.edit_save()]] method should only save the fields defined in
	 * this hash. This is especially important if the template includes some
	 * fields conditionally, e.g. because the user has not the accurate
	 * permissions to edit all fields.
	 */
	var edit_fields;

	/**
	 * This hash can be used to define additional actions to be executed
	 * instead of simply calling [[.edit_save()]].
	 *
	 * If a query string parameter matches a key defined in that hash,
	 * the value of that hash entry will be executed (as function/method
	 * pointer) and [[.edit_save()]]. Won't be called.
	 *
	 * Usually this is used to implement things such as "Back to List"
	 * buttons.
	 */
	var edit_actions;

	/**
	 * The [[.edit_save()]] method should set this variable to '1' when
	 * is saved the new data. This can e.g. be used by the template to 
	 * display a "Database updated" message.
	 */
	var just_updated;
	
	/**
	 * This method must be executed by [[.get_html()]] before evaluating
	 * the template. It resets [[.edit_fields]] and calls [[.edit_load()]].
	 */
	method edit_reset()
	{
		edit_fields = undef;
		edit_load();
	}

	/**
	 * Returns the HTML code for a input box.
	 *
	 * The parameter 'field' is a key in the [[.edit_data]] hash.
	 *
	 * The following named parameters are supported:
	 *
	 *	style	value for the css style attribute
	 *	size	value for the size attribute
	 *
	 * Simply include that function in your template using the ${ .. }
	 * substitution.
	 */
	method edit_input(field, %opt)
	{
		var style = opt["style"];
		var size  = opt["size"];

		edit_fields[field] = field;

		var sizeattr = defined size ? 'size="${xml::size}"' : '';
		return '<input style="${xml::style}" ${sizeattr} ' ~
		       'name="edit_${xml::field}" value="${xml::edit_data.[field]}" />';
	}

	/**
	 * Returns the HTML code for a textarea.
	 *
	 * The following named parameters are supported:
	 *
	 *	style	value for the css style attribute
	 *	cols	value for the cols attribute
	 *	rows	value for the rows attribute
	 *
	 * (see also [[.edit_input()]])
	 */
	method edit_textarea(field, %opt)
	{
		var style = opt["style"];
		var cols  = opt["cols"];
		var rows  = opt["rows"];

		edit_fields[field] = field;

		var colsattr = defined cols ? 'cols="${xml::cols}"' : '';
		var rowsattr = defined rows ? 'rows="${xml::rows}"' : '';
		return '<textarea style="${xml::style}" ${colsattr} ${rowsattr} ' ~
		       'name="edit_${xml::field}">${xml::edit_data.[field]}</textarea>';
	}

	/**
	 * Returns the HTML code for a select box.
	 * 
	 * The list parameter is a hash with the possible values as keys and
	 * the description texts as values.
	 * 
	 * The following named parameters are supported:
	 *
	 *	style	value for the css style attribute
	 *
	 *	emptydesc
	 *		If this parameter is defined, an additional option with
	 *		that description and an empty value is added as first
	 *		option to the list of possible options.
	 *
	 * (see also [[.edit_input()]])
	 */
	method edit_select(field, list, %opt)
	{
		var style = opt["style"];
		var emptydesc = opt["emptydesc"];

		edit_fields[field] = field;
		var html = '<select style="$style" name="edit_${xml::field}">';

		if (defined emptydesc)
			html ~= '<option value="">${xml::emptydesc}</option>';

		foreach i (list)
			if (edit_data.[field] ~== i)
				html ~= '<option selected="1" value="${xml::i}">${xml::list.[i]}</option>';
			else
				html ~= '<option value="${xml::i}">${xml::list.[i]}</option>';

		return html ~ '</select>';
	}

	/**
	 * This method must be overloaded.
	 *
	 * It must fill the [[.edit_data]] hash with values.
	 */
	method edit_load()
	{
		panic "Not overloaded WsfEdit.edit_load() called!";
	}

	/**
	 * This method must be overloaded.
	 *
	 * For all keys defined in [[.edit_fields]], it must update the values
	 * in the backend storage.
	 *
	 * The data must be obtained from 'cgi.param["edit_$keyname"]'.
	 *
	 * It is possible to compare the new values with the old values in
	 * [[.edit_data]] to see if the values have actually changed.
	 *
	 * The variable [[.just_updated]] must be set to '1' if anything has
	 * been updated.
	 */
	method edit_save()
	{
		panic "Not overloaded WsfEdit.edit_save() called!";
	}

	/**
	 * Overloaded [[wsf:WsfComponent.main()]].
	 */
	method main()
	{
		while(1) {
			task_co_return();
			var got_edit_action = 0;

			foreach a (edit_actions) {
				if (declared cgi.param[a]) {
					got_edit_action = 1;
					edit_actions[a]();
				}
			}

			if (!got_edit_action) {
				edit_save();
				dirty = 1;
			}
		}
	}

	/**
	 * Overloaded [[wsf:WsfComponent.get_html()]].
	 *
	 * This method must be overloaded again by object actually implementing
	 * an editor. The method [[.edit_reset()]] must be called in the very
	 * beginning of that method.
	 */
	method get_html()
	{
		edit_reset();
		return '<div id="$id"><!-- Not overloaded WsfSqledit.get_html() --></div>\n';
	}

	/**
	 * Constructor.
	 */
	method init()
	{
		just_updated = 0;
		return *WsfComponent.init();
	}
}