editsimple.smarty
9.92 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
{literal}
<style>
.active {
background: transparent;
}
.inactive {
background: #ccc;
}
.inactive .fieldselection {
display: none;
}
.currentmaster .editbutton { display: none; }
.savebutton { display: none; }
.currentmaster .savebutton { display: block; }
.active .activity_message { display: none; }
.inactive .savebutton { display: none; }
</style>
<!-- include the mochikit js -->
<script language="javascript" src="/thirdpartyjs/MochiKit/Base.js"> </script>
<script language="javascript" src="/thirdpartyjs/MochiKit/Iter.js"> </script>
<script language="javascript" src="/thirdpartyjs/MochiKit/Async.js"> </script>
<script language="javascript" src="/thirdpartyjs/MochiKit/DOM.js"> </script>
<!-- custom JS -->
<script language="javascript">
function str_startswith(source, contents) {
var clen = contents.length;
if (source.length < clen) {
return false;
} else {
var matcher = source.substr(0, clen);
if (matcher == contents) {
return true;
} else {
return false;
}
}
}
/*
* Basic workflow on this page is incredibly simple.
* At each stage there is:
* 1. an "active" set of fields
* 2. an "inactive" set of fields.
*
* Essentially, the backend needs to provide a limited set
* of operations:
* 1. Save a selection (active sets)
* 2. Edit a new "master column".
*/
/*
* function saveSimpleEditSelection();
*
* 1. find all "active" columns, and which field is "master".
* 2. create the "save" request (question: how do we build a POST ... the GET is now too long, since IE dies past 1024 chars (or is it 256?)
* 3. submit.
*/
function saveSimpleEditSelection (save_button) {
var formKeys = Array();
var formValues = Array();
// these need to be "pushed" simultanously.
var activeColumns = getElementsByTagAndClassName("td","active");
/* the submitted form has a simple set of variables.
*
* fields[][] - two-dimensional array:
* [field_id] => [array(active values)].
* fieldset_id - integer fieldset id.
*
* master_field - integer master-field id.
*/
formKeys.push('action');
formValues.push('saveSimpleEdit');
for (index in activeColumns) {
var t = DIV(null);
var obj = activeColumns[index];
if (typeof(obj) == typeof(t)) {
var oclass = obj.getAttribute('class');
var oid = obj.getAttribute('id');
// we can extract the field name from this quite simply
// by removing
var cs_len = 'contain_select_'.length;
if (str_startswith(oid, 'contain_select_')) {
var str_end = oid.substr(cs_len, oid.length);
formKeys.push('fields[]');
formValues.push(str_end);
// FIXME handle Trees.
// since we are active, we can also get the select.
var selection = getElementsByTagAndClassName("select","fieldselection", obj);
// MUST be only 1.
if (selection.length != 0) {
var selObj = selection[0];
var key = 'fields['+str_end+'][]';
// there can be multiple matches here.
for (selindex in selObj.options) {
var subobj = selObj.options[selindex];
if (typeof(subobj) == typeof(t)) {
if (subobj.selected) {
formKeys.push(key);
formValues.push(subobj.value);
}
}
}
}
// handle the case where this is a buttongroup - find the "master".
} else {
if (hasElementClass(obj, 'currentmaster')) {
var cs_len = 'contain_buttons_'.length;
var str_end = oid.substr(cs_len, oid.length);
formKeys.push('master_field');
formValues.push(str_end);
}
}
}
}
// finally.
var POSTval = queryString(formKeys, formValues);
var targeturl = 'manageConditionals.php';
var req = getXMLHttpRequest();
req.open("POST", targeturl, true);
req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
var deferred = sendXMLHttpRequest(req, POSTval);
deferred.addCallback(partial(do_saveSimpleRelation, save_button));
deferred.addErrback(partial(do_handleAjaxError, save_button,
'save'));
// indicate to the user that we are saved.
save_button.disabled = true;
save_button.value = "saving...";
}
// intentionally stubbed - we don't actually act here.
// at most we need a "kill saving".
function do_saveSimpleRelation(button, req) {
//alert('saved '+req.responseText);
button.disabled = false;
button.value = "save";
}
function do_handleAjaxError(button, button_val, err) {
alert('failed while trying to '+button_val);
button.disabled = false;
button.value = button_val;
}
function getSubSelect(obj) {
return getElementsByTagAndClassName('select','fieldselection', obj);
}
/*
* function editNewSimpleSelection(int field_id)
*
* 1. initiate an XMLHTTPRequest to get the "controlled fields".
* 2. change all to be either "active" or "inactive"
* 3. add "save" to the appropriate column.
*
*/
function editNewSimpleSelection(field_id) {
var active_fields = Array();
// DEBUG CODE
if (field_id == '8') {
active_fields.push('8');
active_fields.push('9');
} else {
active_fields.push('5');
active_fields.push('8');
}
var edit_columns = getElementsByTagAndClassName('td','editable');
var button_columns = getElementsByTagAndClassName('td','buttons');
for (var i=0; i<edit_columns.length; i++) {
var obj = edit_columns[i];
var cs_len = 'contain_select_'.length; // horrid.
var col_num = obj.id.substr(cs_len, obj.id.length);
setElementClass(obj, 'inactive editable');
// sigh. we want to know if this is "active" ...
for (var j=0; j<active_fields.length; j++)
{
if (col_num == active_fields[j]) {
setElementClass(obj, 'active editable');
// urgh. we now need to set the master column's select to not be multiple.
var slist = getSubSelect(obj);
for (var k=0; k<slist.length; k++) {
if (col_num == field_id) {
slist[k].multiple = false;
} else {
slist[k].multiple = true;
}
}
}
}
}
for (var i=0; i<button_columns.length; i++) {
var obj = button_columns[i];
var cs_len = 'contain_buttons_'.length; // horrid.
var col_num = obj.id.substr(cs_len, obj.id.length);
setElementClass(obj, 'inactive buttons');
// sigh. we want to know if this is "active" ...
for (var j=0; j<active_fields.length; j++)
{
if (col_num == active_fields[j]) {
if (col_num == field_id) {
setElementClass(obj, 'active buttons currentmaster');
} else {
setElementClass(obj, 'active buttons');
}
}
}
}
}
function getMasterField() {
var cols = getElementsByTagAndClassName('td','buttons');
var mf = null;
for (var i=0; i<cols.length; i++) {
var c = cols[i];
if (hasElementClass(c, 'currentmaster')) {
mf = c.id.substr('contain_buttons_'.length,c.id.length);
}
}
return mf;
}
/* function getExistingFieldsForVal
*
* FIXME we need to add XML function here.
*/
function getExistingFieldsForVal(field_id) {
// DEBUG
if ((getMasterField() == field_id) && (field_id == "1")) {
alert('updating selections');
var preoptions = Array();
// 1 get the value for field field_id.
var f = getElement('sel_'+field_id);
var active_val = f.value; // this is the active value of the field.
// 2 fetch the option-array. this should be a list of "<opt num="3" active="{1,0}" />
// DEBUG HACK.
availcols = Array();
availcols.push("2");
availcols.push("3");
preoptions["2"] = Array();
preoptions["3"] = Array();
preoptions["2"]["4"] = true;
preoptions["2"]["5"] = true;
preoptions["2"]["6"] = false;
preoptions["3"]["7"] = true;
preoptions["3"]["8"] = false;
preoptions["3"]["9"] = true;
// 3 get and update from the preoption array
// to do this, grab the "sel" and walk its options.
for (var i=0; i<availcols.length; i++) {
var colid = availcols[i];
var selid = 'sel_'+colid;
var selection = getElement(selid);
for (var k=0; k<selection.options.length; k++) {
selection.options[k].selected = preoptions[colid][selection.options[k].value];
}
}
}
}
</script>
{/literal}
<h2>Editing Fieldset Rules (Simple)</h2>
<!-- by default, we start by editing the _first_ column.
after that, we need to update / get the "active columns" ids. -->
<table>
<tr>
{foreach from=$aFields item=oField}
<th><span title="Field {$oField->getId()}">{$oField->getName()}</span></th>
{/foreach}
</tr>
<tr valign="top">
{foreach from=$aFields item=oField}
{assign var=sMultiple value=""}
{assign var="sActive" value="inactive"}
{if $oField->getParentFieldId()}
{assign var=sMultiple value='multiple="true"'}
{if $oField->getParentFieldId() == $iMasterFieldId}
{assign var="sActive" value="active"}
{/if}
{else}
{assign var="sActive" value="active"}
{/if}
<td class="{$sActive} editable" id="contain_select_{$oField->getId()}">
<p class="activity_message">This field is not controlled by the currently active group.</p>
<select name="column_{$oField->getId()}" {$sMultiple} class="fieldselection" id="sel_{$oField->getId()}" onchange="getExistingFieldsForVal({$oField->getId()});">
{foreach from=$oField->getValues() item=oMetaData}
<option value="{$oMetaData->getId()}">{$oMetaData->getName()|escape}</option>
{/foreach}
</select>
</td>
{/foreach}
</tr>
<tr>
{foreach from=$aFields item=oField}
{assign var="sActive" value="inactive"}
{if $oField->getParentFieldId()}
{assign var=sMultiple value='multiple="true"'}
{if $oField->getParentFieldId() == $iMasterFieldId}
{assign var="sActive" value="active"}
{/if}
{else}
{assign var="sActive" value="active currentmaster"}
{/if}
<td class="{$sActive} buttons" id="contain_buttons_{$oField->getId()}">
{if $oField->hasChildren()}
<input type="button" onclick="editNewSimpleSelection({$oField->getId()});" value="Edit" class="editbutton" />
<input type="button" onclick="saveSimpleEditSelection(this);" value="Save" class="savebutton" />
{/if}
</td>
{/foreach}
</tr>
</table>