root/form/field_form_definition.php

Revision 277, 22.5 kB (checked in by kevin, 9 months ago)

First commit of form builder plugin

Line 
1 <?php
2
3 $page_return_type = optional_param('type','');
4 $prfext_types = array('choices','profile_date_dmy','profile_date_mdy','profile_shorttext','profile_smallbox');
5
6 // set up the field values
7
8 if ($field) {
9     $field_id = $field->ident;
10     
11     if ($field_id) {
12         $action = 'change';
13     } else {
14         // this is duplicating an existing field
15         $action = 'add';
16     }   
17     
18     $internal_name = $field->internal_name;
19     $form_title = $field->title;
20     $description = $field->description;
21     $field_type = $field->field_type;
22     $field_access = $field->default_access;
23     $default_value = $field->default_value;
24     $is_keyword_tag = $field->is_keyword_tag;
25     if (in_array($field_type,array('web','email','aim','msn','skype','icq'))) {
26         $contact_type = $field_type;
27         $field_type = 'contact';
28         $contact_class = 'visible';
29     } else {
30         $contact_class = 'invisible';
31         $contact_type = '';
32     }
33     
34     if (isset($field->options1)) {
35         $number_of_options1 = count($field->options1);
36     } else {
37         $number_of_options1 = 0;
38     }
39     
40     if (isset($field->options2)) {
41         $number_of_options2 = count($field->options2);
42     } else {
43         $number_of_options2 = 0;
44     }
45     
46     if ($field->profile && in_array($field_type,$prfext_types)) {
47         $is_keyword_tag_class = 'visible';
48     } else {
49         $is_keyword_tag_class = 'invisible';
50     }
51     
52     if ($field_type == 'choices') {
53         $choices_class1 = 'visible';
54         if ($number_of_options2 > 0) {
55             $choices_class2 = 'visible';
56         }
57             
58     } else {
59         $choices_class1 = 'invisible';
60         $choices_class2 = 'invisible';
61     }
62     
63     $required_no_checked = '';
64     $required_yes_checked = '';
65     if ($field->required) {
66         $required_yes_checked = 'checked';
67     } else {
68         $required_no_checked = 'checked';
69     }
70     
71     $profile_no_checked = '';
72     $profile_yes_checked = '';
73     if ($field->profile) {
74         $profile_yes_checked = 'checked';
75         $profile_class = 'visible';
76     } else {
77         $profile_no_checked = 'checked';
78         $profile_class = 'invisible';
79     }
80     
81     $category = $field->category;
82     
83     $invisible_no_checked = '';
84     $invisible_yes_checked = '';
85     if ($field->invisible) {
86         $invisible_yes_checked = 'checked';
87     } else {
88         $invisible_no_checked = 'checked';
89     }
90     
91     $is_keyword_tag_no_checked = '';
92     $is_keyword_tag_yes_checked = '';
93     if ($is_keyword_tag) {
94         $is_keyword_tag_yes_checked = 'checked';
95     } else {
96         $is_keyword_tag_no_checked = 'checked';
97     }
98     
99     $column_1_checked = '';
100     $column_2_checked = '';
101     $column_no_checked = '';
102     if ($field->col == 1) {
103         $column_1_checked = 'checked';
104     } else if ($field->col == 2) {
105         $column_2_checked = 'checked';
106     } else {
107         $column_no_checked = 'checked';
108     }
109     
110     $choice_type_select_checked = '';
111     $choice_type_radio_checked = '';
112     $choice_type_checkbox_checked = '';
113     $choice_type_radio_group_checked = '';
114     if ($field->choice_type == 'select') {
115         $choice_type_select_checked = 'checked';
116         $orientation_class = 'invisible';
117     } else if ($field->choice_type == 'radio') {
118         $choice_type_radio_checked = 'checked';
119         $orientation_class = 'visible';
120     } else if ($field->choice_type == 'radio_group') {
121         $choice_type_radio_group_checked = 'checked';
122         $orientation_class = 'invisible';
123     } else {
124         $choice_type_checkbox_checked = 'checked';
125         $orientation_class = 'visible';
126     }
127     
128     $orientation_horizontal_checked = '';
129     $orientation_vertical_checked = '';
130     if ($field->orientation == 'horizontal') {
131         $orientation_horizontal_checked = 'checked';
132     } else {
133         $orientation_vertical_checked = 'checked';
134     }
135     
136 } else {
137     $action = 'add';
138     
139     $number_of_options1 = 0;
140     $number_of_options2 = 0;
141     
142     $orientation_class = 'invisible';
143     $profile_class = 'invisible';
144     $choices_class1 = 'invisible';
145     $choices_class2 = 'invisible';
146     $contact_class = 'invisible';
147     $is_keyword_tag_class = 'invisible';
148     
149     $field_id = 0;
150     
151     $internal_name = $field_name;
152     $form_title = '';
153     $description = '';
154     $field_type = 'profile_shorttext';
155     $field_access = '';
156     $contact_type = 'web';
157     
158     $default_value = '';
159     $is_keyword_tag_no_checked = 'checked';
160     $is_keyword_tag_yes_checked = '';   
161     
162     $required_no_checked = 'checked';
163     $required_yes_checked = '';
164             
165     $profile_no_checked = 'checked';
166     $profile_yes_checked = '';
167     
168     $category = '';
169     
170     $invisible_no_checked = 'checked';
171     $invisible_yes_checked = '';
172     
173     $registration_no_checked = 'checked';
174     $registration_yes_checked = '';
175             
176     $column_1_checked = '';
177     $column_2_checked = '';
178     $column_no_checked = 'checked';
179     
180     $choice_type_select_checked = 'checked';
181     $choice_type_radio_checked = '';
182     $choice_type_checkbox_checked = '';
183     $choice_type_radio_group_checked = '';
184     
185     $orientation_horizontal_checked = 'checked';
186     $orientation_vertical_checked = '';
187 }
188
189 // define the Javascript and CSS
190     
191 $metatags .= <<<END
192 <script>
193 number_of_options1 = $number_of_options1;
194 number_of_options2 = $number_of_options2;
195
196 function vis() {
197     var x,v;
198     // show profile config if relevant
199     v = document.field_form.profile;
200     x = document.getElementById('profile_config');
201     if (v[1].checked) {
202         x.className = 'visible';
203     } else {
204         x.className = 'invisible';
205     }
206     
207     // show choice config if relevant
208     u = document.field_form.profile;
209     v = document.field_form.field_type;
210     w1 = document.getElementById('option_outer_container1');
211     w2 = document.getElementById('option_outer_container2');
212     x = document.getElementById('choices_config');
213     y = document.getElementById('contact_config');
214     z = document.getElementById('is_keyword_tag_config');
215     z.className = 'invisible';
216     if (v.options[9].selected) {
217         x.className = 'visible';
218         w1.className = 'visible';
219         y.className = 'invisible';
220         if (u[1].checked) {
221             z.className = 'visible';
222         }
223     } else if (v.options[8].selected) {
224         x.className = 'invisible';
225         y.className = 'visible';       
226     } else {
227         x.className = 'invisible';
228         y.className = 'invisible';
229     }   
230     
231     // show orientation and second choice config if relevant
232     v = document.field_form.choice_type;
233     x = document.getElementById('orientation_config');
234     if (v[1].checked || v[3].checked) {
235         x.className = 'visible';
236     } else {
237         x.className = 'invisible';
238     }
239     
240     if (v[2].checked) {
241         w2.className = 'visible';
242     } else {
243         w2.className = 'invisible';
244     }         
245 }
246
247 function add_option(group) {
248     var o,el;
249     o = document.getElementById('option_container'+String(group));
250     el = document.createElement('input');
251     el.type = 'text';
252     el.className = "option_value_input";
253     if (group == 1) {
254         el.name = "option_"+String(group)+"_"+String(number_of_options1)+"_value";
255     } else {
256         el.name = "option_"+String(group)+"_"+String(number_of_options2)+"_value";
257     }
258     el.value = "";
259     o.appendChild(el);
260     el = document.createElement('input');
261     el.type = 'text';
262     el.className = "option_label_input";
263     if (group == 1) {
264         el.name = "option_"+String(group)+"_"+String(number_of_options1)+"_label";
265     } else {
266         el.name = "option_"+String(group)+"_"+String(number_of_options2)+"_label";
267     }
268     el.value = "";
269     o.appendChild(el);
270     el = document.createElement('br');
271     o.appendChild(el);
272     if (group == 1) {
273         number_of_options1++;
274         document.field_form.number_of_options1.value = number_of_options1;
275     } else {
276         number_of_options2++;
277         document.field_form.number_of_options2.value = number_of_options2;
278     }
279 }    
280 </script>
281 <style>
282 div.invisible {
283     display: none;
284 }
285 div.visibleright {
286     float:right;
287     width: 100px;
288 }
289
290 label.labelclass {
291     display:block;
292     font-weight:bold;
293 }
294
295 p.description {
296     font-size: 0.9em;
297     line-height: 0.9em;
298     margin-top: 3px;
299 }
300
301 span.option_value_header {
302     font-weight: bold;
303     width: 150px;
304     display: block;
305     float: left;
306 }
307
308 span.option_label_header {
309     font-weight: bold;
310     width: 300px;
311     margin-left: 10px;
312 }
313
314 input.option_value_input {
315     width:150px;
316 }
317
318 input.option_label_input {
319     margin-left:10px;
320     width:300px;
321 }
322
323 input.standard {  
324     widrh: 150px;
325 }
326
327 input.longer {
328     width:300px;
329 } 
330 </style>
331 END;
332
333 // define a whole lot of text
334
335 $yes = __gettext("yes");
336 $no = __gettext("no");
337
338 $horizontal = __gettext("horizontal");
339 $vertical = __gettext("vertical");
340
341 if ($action == 'add') {
342     $title = __gettext("Add Field");
343     $form_description = __gettext('Please complete the form below and click on the "Add field" at the bottom.');
344 } else {
345     $title = __gettext("Edit Field");
346     $form_description = __gettext('Please complete the form below and click on the "Change field" at the bottom.');
347 }   
348
349 $internal_name_label = __gettext("Internal name");
350 $internal_name_description = __gettext("The internal name for this field - must be one word (underscores allowed, but no blanks).");
351
352 $title_label = __gettext("Title");
353 $title_description = __gettext("The title (very short description) for this field");
354
355 $description_label = __gettext("Description");
356 $description_description = __gettext("The full description that should be displayed for this field");
357
358 $required_label = __gettext("Required");
359 $required_description = __gettext("Is this field required?");
360
361 $profile_label = __gettext("Profile field");
362 $profile_description = __gettext("Should the user be able to edit this field as part of their profile?");
363
364 $field_type_label = __gettext("Field type");
365
366 $profile_shorttext_label = __gettext("Short text (250 px long)");
367
368 $profile_text_label = __gettext("Long text (100% width)");
369
370 $profile_smallbox_label = __gettext("Small text box (250 px long)");
371
372 $profile_mediumtext_label = __gettext("Large text box (100% width)");
373
374 $profile_date_dmy_label = __gettext("Date (day/month/year)");
375
376 $profile_date_mdy_label = __gettext("Date (month/day/year)");
377
378 $profile_keywords_label = __gettext("Keyword box");
379
380 $profile_profile_photo_label = __gettext("Profile photo");
381
382 $profile_contact_label = __gettext("Contact field (Email, Skype, MSN etc.)");
383
384 $profile_choices_label = __gettext("Dropdown, radio or checkboxes");
385
386 $profile_config_title = __gettext("Profile field configuraton");
387
388 $category_label = __gettext("Category");
389 $category_description = __gettext("The category determines the section of the profile that this field appears in.");
390
391 $invisible_label = __gettext("Invisible");
392
393 $invisible_description = __gettext("Invisible fields can be edited in the profile edit form but never appear on the profile itself.");
394
395 $registration_label = __gettext("Registration");
396
397 $registration_description = __gettext("Should this field appear on the registration form?");
398
399 $column_label = __gettext("Primary profile column");
400
401 $column_description = __gettext("The column in which this field should appear in the primary profile region at the top.");
402
403 $choice_header = __gettext("Choice configuration");
404
405 $choice_field_type_label = __gettext("Field type");
406 $choice_field_type_description = __gettext("Choose the type of field.");
407
408 $dropdown = __gettext("Dropdown (select) box");
409 $radio = __gettext("Radio buttons");
410 $radio_group = __gettext("Radio group (2D grid)");
411 $checkbox = __gettext("Checkbox group");
412
413 $default_value_label = __gettext("Default value");
414 $default_value_description = __gettext("Enter the default value for this field. If the field is a radio or checkbox group, then you can precheck"
415 ." several values by entering a comma-separated list.");
416
417 $is_keyword_tag_label = __gettext("Keyword tag");
418 $is_keyword_tag_description = __gettext("If yes, then this field will be treated as a searchable keyword tag and will be linked in profiles.");
419
420 $orientation_label = __gettext("Orientation");
421 $orientation_description = __gettext("If this field is made up of radio buttons or a checkbox group, then you can specify the orientation.");
422
423 $access_label = __gettext("Default access");
424 $access_description = __gettext("Set the default access value for this profile field.");
425
426 $options_header1 = __gettext("Options");
427 $options_header2 = __gettext("Options (Columns)");
428 $options_value_header = __gettext("Value");
429 $options_label_header = __gettext("Label (optional)");
430 $options_description1 = __gettext("For ordinary choices or radio group rows. The label is optional. If you specify a label for one option, then you need to specify one for all options.");
431 $options_description2 = __gettext("For radio group columns. The label is optional. If you specify a label for one option, then you need to specify one for all options.");
432
433 $options_add_button = __gettext("Add option");
434
435 // set up some form components
436
437 if ($action == "add") {
438     $manage_field_button = __gettext("Add field");
439 } else {
440     $manage_field_button = __gettext("Change field");
441 }
442
443 $this_form_link_text = __gettext("Return to form page");
444 $form_link_text = __gettext("List all forms");
445 $field_link_text = __gettext("List all fields");
446
447 $access_options = array(
448     '' => __gettext('Use system default access level'),
449     'PRIVATE' => __gettext('Private'),
450     'PUBLIC' => __gettext('Public'),
451     'LOGGED_IN' => __gettext("Logged in users")
452 );
453
454 $access = '<select name="default_access">'."\n";
455 foreach ($access_options AS $value => $label) {
456     if ($value == $field_access) {
457         $selected = 'selected';
458     } else {
459         $selected = '';
460     }
461     $access .= sprintf('<option value="%s" %s>%s</option>'."\n", $value, $selected, $label);
462 }
463     
464 $access .= '</select>'."\n";
465
466 //$access = run("display:access_level_select",array("default_access",$field_access));
467
468 $field_type_options = array(
469     'profile_shorttext' => $profile_shorttext_label,
470     'text' => $profile_text_label,
471     'profile_smallbox' => $profile_smallbox_label,
472     'mediumtext' => $profile_mediumtext_label,
473     'profile_date_dmy' => $profile_date_dmy_label,
474     'profile_date_mdy' => $profile_date_mdy_label,
475     'keywords' => $profile_keywords_label,
476     'profile_photo' => $profile_profile_photo_label,
477     'contact' => $profile_contact_label,
478     'choices' => $profile_choices_label
479 );
480
481 $field_type_select = '<select name="field_type" onChange="vis();">'."\n";
482 foreach ($field_type_options AS $value => $label) {
483     if ($value == $field_type) {
484         $selected = 'selected';
485     } else {
486         $selected = '';
487     }
488     $field_type_select .= sprintf('<option value="%s" %s>%s</option>'."\n", $value, $selected, $label);
489 }
490     
491 $field_type_select .= '</select>'."\n";
492
493 $contact_options = array(
494     'web' => __gettext("web"),
495     'email' => __gettext("email"),
496     'aim' => __gettext("aim"),
497     'msn' => __gettext("msn"),
498     'skype' => __gettext("skype"),
499     'icq' => __gettext("icq"),
500 );
501
502 $contact_radio = '';
503
504 foreach ($contact_options AS $value => $label) {
505     if ($value == $contact_type) {
506         $checked = 'checked';
507     } else {
508         $checked = '';
509     }
510     $contact_radio .= sprintf('<input type="radio" name="contact_type" value="%s" %s /> %s'."\n", $value, $checked, $label);
511 }
512
513 $option_template = '<input class="option_value_input" type="text" name="option_%s_value" value="%s">';
514 $option_template .= '<input class="option_label_input" type="text" name="option_%s_label" value="%s">';
515 $option_template .= "<br />\n";
516
517 $options_bit1 = '';
518
519 if ($number_of_options1 > 0) {
520     $count = 0;
521     foreach($field->options1 AS $option) {
522         $options_bit1 .= sprintf($option_template,'1_'.$count,$option->value,'1_'.$count,$option->label);
523         $count ++;
524     }
525 }
526
527 $options_bit2 = '';
528
529 if ($number_of_options2 > 0) {
530     $count = 0;
531     foreach($field->options2 AS $option) {
532         $options_bit2 .= sprintf($option_template,'2_'.$count,$option->value,'2_'.$count,$option->label);
533         $count ++;
534     }
535 }     
536
537 // define the form
538
539 if ($form_id) {
540
541     $form_link = <<<END
542     <a href="{$CFG->wwwroot}mod/form/manage_form.php?id=$form_id">$this_form_link_text</a>
543              |
544 END;
545 } else {
546     $form_link = '';
547 }
548
549 $body = <<< END
550 <p>$form_link <a href="{$CFG->wwwroot}mod/form/index.php">$form_link_text</a>
551          | <a href="{$CFG->wwwroot}mod/form/list_fields.php">$field_link_text</a>
552 </p>
553 <p>$form_description</p>
554 <form name="field_form" action="{$CFG->wwwroot}mod/form/manage_field.php" method="post">
555 <input type="hidden" name="action" value="$action">
556 <input type="hidden" name="form_id" value="$form_id">
557 <input type="hidden" name="field_id" value="$field_id">
558 <input type="hidden" name="number_of_options1" value="$number_of_options1">
559 <input type="hidden" name="number_of_options2" value="$number_of_options2">
560 <input type="hidden" name="type" value="$page_return_type">
561
562 <label class="labelclass" for="internal_name">$internal_name_label</label>
563 <input type="text" class="standard" name="internal_name" value="$internal_name">
564 <p class="description">$internal_name_description</p>
565 <label class="labelclass" for="title">$title_label</label>
566 <input type="text" class="standard" name="title" value="$form_title">
567 <p class="description">$title_description</p>
568 <label class="labelclass" for="description">$description_label</label>
569 <input type="text" class="longer" name="description" value="$description">
570 <p class="description">$description_description</p>
571 <label class="labelclass" for="required">$required_label</label>
572 <input type="radio" name="required" value="0" $required_no_checked> $no
573 <input type="radio" name="required" value="1" $required_yes_checked> $yes
574 <p class="description">$required_description</p>
575 <label class="labelclass" for="profile">$profile_label</label>
576 <input type="radio" name="profile" value="0" onChange="vis();" $profile_no_checked> $no
577 <input type="radio" name="profile" value="1" onChange="vis();" $profile_yes_checked> $yes
578 <p class="description">$profile_description</p>
579 <label class="labelclass" for="field_type">$field_type_label</label>
580 $field_type_select
581
582 <div id="profile_config" class="$profile_class">
583     <br />
584     <h1>$profile_config_title</h1>
585     <br />
586     <label class="labelclass" for="category">$category_label</label>
587     <input type="text" class="standard" name="category" value="$category">
588     <p class="description">$category_description</p>
589     <label class="labelclass" for="invisible">$invisible_label</label>
590     <input type="radio" name="invisible" value="0" $invisible_no_checked> $no
591     <input type="radio" name="invisible" value="1" $invisible_yes_checked> $yes
592     <p class="description">$invisible_description</p>
593     <label class="labelclass" for="column">$column_label</label>
594     <input type="radio" name="column" value="1" $column_1_checked> 1
595     <input type="radio" name="column" value="2" $column_2_checked> 2
596     <input type="radio" name="column" value="0" $column_no_checked> $no
597     <p class="description">$column_description</p>
598     <div id="is_keyword_tag_config" class="$is_keyword_tag_class">
599         <label class="labelclass" for="is_keyword_tag">$is_keyword_tag_label</label>
600         <input type="radio" name="is_keyword_tag" value="0" $is_keyword_tag_no_checked> $no
601         <input type="radio" name="is_keyword_tag" value="1" $is_keyword_tag_yes_checked> $yes
602         <p class="description">$is_keyword_tag_description</p>
603     </div>
604     <label class="labelclass" for="default_access">$access_label</label>
605