root/form/form.php

Revision 302, 13.6 kB (checked in by kevin, 7 months ago)

I had accidentally turned form notification off in the last commit. This fixes that and fixes a problem with some profile choice fields not being displayed properly.

Line 
1 <?php
2
3 /*
4  * Elgg Forms
5  * Kevin Jardine
6  * Radagast Solutions
7  * http://radagast.biz
8  *
9  * The main function for rendering forms
10  * and validating and storing form submissions.
11  *
12  */
13  
14 // Load Elgg framework
15 @require_once("../../includes.php");
16     
17 // Define context
18 define("context","form");
19
20 $form_error_msg = __gettext("Error: this form either does not exist or you do not have permission to view it.");
21 $form_error_title = __gettext("Error");
22
23 $action = optional_param('action','');
24 $preview = optional_param('preview','');
25
26 $form_id = optional_param('id',0,PARAM_INT);
27
28 $form = form_get_form_definition($form_id);
29
30 $form_values = optional_param('form_values','');
31
32 $preview_explanation = '<b>'.__gettext("Preview").':</b> '
33     .__gettext("This is a form preview - submitting the results below will show what would be saved to the database,"
34     ." but will not actually save the values.");
35
36 $fielderror = false;
37
38 if ($form) {
39     if ((isloggedin() && run("users:flags:get", array("admin", $_SESSION['userid']))) || run("users:access_level_check",$form->access)) {
40         $title = $form->title;
41     
42         if ($action == 'submit') {
43             // process the form submission
44             
45             $result = form_validate($form,$form_values);
46                 
47             $fielderror = $result->problem;
48             
49             if ($fielderror) {
50                 $problem_fields = $result->data->problem_fields;
51                 $problem_field_titles = $result->data->problem_field_titles;
52                 $messages[] = __gettext("Error: please correct these missing or invalid fields:")." ".implode(", ",$problem_field_titles);
53             } else {
54                 $body = form_save($result->data,$_SESSION['userid'],$preview);
55                 $body = '<p>'.$form->response_text.'</p>'.$body;
56                 if ($form->notification) {
57                     $msg = "Someone has submitted the form '$title'\n\n"
58                             ."To export the form results or turn form notification off, please login as a site administrator and visit:\n\n"
59                             ."{$CFG->wwwroot}mod/form/index.php";
60                     $subject = "Form submission for '$title'";
61                     notify_user($form->owner, $subject, $msg);
62                 }
63             }
64         }
65         
66         if ($action != 'submit' || $fielderror) {   
67         
68             $body = '<p>'.$form->description.'</p>';
69             if ($preview) {
70                 $body .= '<p>'.$preview_explanation.'</p>';
71             }
72                 
73             $body .= '<form action="'.$CFG->wwwroot.'mod/form/form.php" method="post">';
74             foreach($form->fields as $field) {       
75                  // copy array element with default to ''
76                 $flabel = !empty($field->title) ? $field->title : '';
77                 $fname  = !empty($field->internal_name) ? $field->internal_name : '';
78                 $ftype  = !empty($field->field_type) ? $field->field_type : '';
79                 $fblurb = !empty($field->description) ? $field->description : '';
80                 
81                 $value = new stdClass;
82                 if ($form_values && isset($form_values[$fname])) {
83                     $value->value = $form_values[$fname];
84                 } else {
85                     $value->value = "";
86                 }
87                 
88                 if (isset($problem_fields) && is_array($problem_fields) && in_array($fname,$problem_fields)) {
89                     if ($field->required) {
90                         $name = "<label for=\"$fname\"><b><span style=\"color:red\">* {$flabel}</span></b>";
91                     } else {
92                         $name = "<label for=\"$fname\"><b><span style=\"color:red\">{$flabel}</span></b>";
93                     }
94                 } elseif ($field->required) {                   
95                     $name = "<label for=\"$fname\"><b>* {$flabel}</b>";
96                 } else {
97                     $name = "<label for=\"$fname\"><b>{$flabel}</b>";
98                 }
99                 if (!empty($fblurb)) {
100                     $name .= "<br /><i>" . $fblurb . "</i>";
101                 }
102                 $name .= '</label>';
103                 
104                 if ($ftype == 'choices') {
105                     $options1 = array();
106                     $field_options = get_records_sql("SELECT * FROM {$CFG->prefix}form_fields_choice WHERE field_id = {$field->ident} AND group_id = 1 ORDER BY ident ASC");
107                     if ($field_options) {
108                         foreach($field_options AS $field_option) {
109                             if ($field_option->label) {
110                                 $options1[$field_option->value] = $field_option->label;
111                             } else {
112                                 $options1[] = $field_option->value;
113                             }
114                         }
115                     }
116                     if ($field->choice_type == 'radio_group') {
117                         $options2 = array();
118                         $field_options = get_records_sql("SELECT * FROM {$CFG->prefix}form_fields_choice WHERE field_id = {$field->ident} AND group_id = 2 ORDER BY ident ASC");
119                         if ($field_options) {
120                             foreach($field_options AS $field_option) {
121                                 if ($field_option->label) {
122                                     $options2[$field_option->value] = $field_option->label;
123                                 } else {
124                                     $options2[] = $field_option->value;
125                                 }
126                             }
127                         }
128                     } else if ($field->choice_type == 'select') {
129                         $display_type = 'profile_select';
130                     } else {
131                         if ($field->orientation == 'horizontal') {
132                             $display_type = 'profile_'.$field->choice_type;
133                         } else {
134                             $display_type = 'profile_vertical_'.$field->choice_type;
135                         }
136                     }
137                     
138                     $ftype = 'display_type_'.$fname;
139                     
140                     //prfext_display($ftype,$display_type,$field->is_keyword_tag,$field->default_value,$options);
141                     if ($field->choice_type == 'radio_group') {
142                         prfext_display($ftype,'profile_radio_group',$field->is_keyword_tag,$field->default_value,array($options1,$options2));
143                     } else {
144                         prfext_display($ftype,$display_type,$field->is_keyword_tag,$field->default_value,$options1);
145                     }
146                 }           
147                             
148                 $column1 = display_input_field(array("form_values[" . $fname . "]",$value->value,$ftype,$fname,@$value->ident,$page_owner));
149                 
150                 $body .= templates_draw(array(
151                                                'context' => 'databoxvertical',
152                                                'name'    => $name,
153                                                'contents' => $column1
154                                                ));
155             }
156             #TODO - make the submit button text configurable
157             $buttonValue = __gettext("Submit"); // gettext variable
158         
159             $body .= <<< END
160               <p>
161                   <input type="hidden" name="action" value="submit" />
162                   <input type="hidden" name="id" value="{$form->ident}" />
163                   <input type="hidden" name="preview" value="$preview" />
164                   <input type="submit" value="$buttonValue" />
165               </p>
166             </form>
167 END;
168         }
169     } else {
170         $title = $form_error_title;
171         $body = $form_error_msg;
172     }
173
174 } else {
175         $title = $form_error_title;
176         $body = $form_error_msg;
177 }
178
179 // Output to the screen
180 $body = templates_draw(array(
181                 'context' => 'contentholder',
182                 'title' => $title,
183                 'body' => $body
184             )
185 );
186
187 echo templates_page_draw( array(
188                 $title, $body
189             )
190 );
191             
192 // validate fields
193 // returns a list of problem fields and titles
194 // if there is an error, or the data ready for insertion otherwise
195
196 function form_validate($form,$form_values) {
197
198     global $CFG;
199         
200     $missing_fields = array();
201     $missing_field_titles = array();
202     $invalid_fields = array();
203     $invalid_field_titles = array();
204     $validated_values = array();
205     
206     foreach($form->fields as $field) {
207         $fd = new StdClass;
208         $fname = $field->internal_name;
209         $flabel = $field->title;
210         $frequired = $field->required;
211         $ftype = $field->field_type;
212         if (isset($form_values[$fname])) {
213             $value = trim($form_values[$fname]);
214         }
215       
216         if (!empty($value)) {
217             
218             // non-empty field, so get it ready to save                     
219
220             $fd->name   = $fname;
221             $fd->value  = $value;
222             $fd->profile = $field->profile;
223             $fd->field_id = $field->ident;
224             $fd->map_id = $field->map_id;
225             $fd->form_id = $form->ident;
226             $fd->field_type = $ftype;
227             $fd->default_access = $field->default_access;
228             
229             // give plugins a chance to change or reject the value
230             if ($field->profile) {               
231                 $fd = plugin_hook("profile_data","create",$fd);
232             }
233             
234             if (!empty($fd) && !empty($fd->value)) {
235                 $fd = plugin_hook("form_data","create",$fd);
236             }
237         }
238         if (!empty($fd) && !empty($fd->value)) {
239                           
240             // OK, we have a value, so validate it if there is a validation function
241             $valid = true;
242             
243             if (isset($CFG->display_field_module[$ftype])) {
244                 $callback = $CFG->display_field_module[$ftype] . "_validate_input_field";
245                 if (!$callback($fd)) {
246                     $invalid_fields[] = $fname;
247                     $invalid_field_titles[] = $flabel;
248                     $valid = false;
249                 }
250             }
251             if ($valid) {
252                 $validated_values[] = $fd;
253             }
254             
255         } else {               
256             if ($frequired) {
257                 $missing_fields[] = $fname;
258                 $missing_field_titles[] = $flabel;
259             }
260         }
261     }
262     $problem_fields = array_unique(array_merge($invalid_fields,$missing_fields));
263     $problem_field_titles = array_unique(array_merge($invalid_field_titles,$missing_field_titles));
264     
265     $result = new stdClass;
266     if ($problem_fields) {
267         $result->problem = true;
268         $result->data = new StdClass;
269         $result->data->problem_fields = $problem_fields;
270         $result->data->problem_field_titles = $problem_field_titles;
271     } else {
272         $result->problem = false;
273         $result->data = $validated_values;
274     }       
275     
276     return $result;
277 }
278
279 function form_save($validated_values,$owner,$preview) {
280     global $CFG;
281     
282     $body = '';
283     
284     if (!$preview) {   
285         delete_records('profile_data','owner',$owner);
286         delete_records('tags','owner',$owner);
287     } else {
288         $body .= '<h2>'.__gettext("Form preview results").'</h2>';
289     }
290     
291     $profile_field_text = __gettext("Profile field");
292     $form_field_text = __gettext("Form field");
293     $saved_text = __gettext("would be saved as");
294                 
295     foreach($validated_values as $fd) {
296         $fd->owner = $owner;
297         if ($fd->profile & !$preview) {
298             $pd_current = get_record_sql("SELECT * FROM {$CFG->prefix}profile_data WHERE owner = $owner and name = '{$fd->name}'");
299             if ($pd_current) {
300                 delete_records('profile_data','owner',$owner,'name',$fd->name);
301                 delete_records('tags','owner',$owner,'ref',$pd_current->ident);
302                 $access = $pd_current->access;
303             } else {
304                 $access = $fd->default_access;
305             }
306             $pd = new stdClass;
307             $pd->name = $fd->name;
308             $pd->value = $fd->value;
309             $pd->owner = $owner;
310             $pd->access = $access;
311             $insert_id  = insert_record('profile_data',$pd);
312             $pd->ident = $insert_id;
313             $fd->profile_id = $insert_id;
314             plugin_hook("profile_data","publish",$pd);
315             
316             // insert the keywords
317             
318             if ($fd->field_type == "keywords") {           
319                 insert_tags_from_string ($fd->value, $fd->name, $insert_id, $access, $owner);
320             }
321         } else {
322             $fd->profile_id = 0;
323         }
324         
325         if ($preview) {
326             if ($fd->profile) {
327                 $field_desc = $profile_field_text;
328             } else {
329                 $field_desc = $form_field_text;
330             }
331             $body .= '<hr>'."\n";
332             $body .= '<p>'.$field_desc.' <b>'.$fd->name.'</b> '.$saved_text.':'.'</p>'."\n";
333             $body .= '<p>'.$fd->value.'</p>'."\n";
334         } else {               
335             
336             $fd->created = time();
337             
338             unset($fd->default_access);           
339                 
340             delete_records('form_data','owner',$owner,'field_id',$fd->field_id);
341             $insert_id  = insert_record('form_data',$fd);
342             $fd->data_id = $insert_id;
343             plugin_hook("form_data","publish",$fd);
344         }
345     }
346     return $body;
347 }
348
349 ?>
Note: See TracBrowser for help on using the browser.