Now avatars!!! both on full node view and teaser.
[estigi.git] / core / form / form.api
blobe4baac73ce460e2b8d22647a52b4de64d5875891
1 <?php
2 /**
3  * @file form.api
4  * Part of the form api
5  * Helper functions for the form api
6  *
7  * @ingroup Form
8  */
10 function form_input_render_skin(&$data){
12         $data['type'] = (!isset($data['type']) ? 'text' : $data['type']);
13         if(!isset($data['inline'])){
14                 //$data['inline'] = 1;
15         }
17         return $data['prefix'].
18                         '<br>'.$data['text'] . ($data['required'] == TRUE ? '<span class="form-required" title="This field is required.">*</span>' : '') .
19                         ($data['inline'] == 1 ? '' : '<br>').
20                         ' <input type="'.$data['type'].'" class="'.$data['class'].'" value="'.$data['value'].'" name="'.$data['name'].'" id="'.$data['id'].'" size="'.$data['size'].'" maxlength="'.$data['maxlength'].'" '.$data['attributes'].' />'.
21                         ($data['help'] != '' ? '<div class="description">'.$data['help'].'</div>' : '')
22                         .($data['sufix'] != '' ? $data['sufix'] : '')
23                         .($data['end_break'] == FALSE ? '' : '<br />');
26 function form_radio_render_skin(&$data){
28         $button = $data['text'] . ($data['required'] == TRUE ? '<span class="form-required" title="This field is required.">*</span>' : '') . '<br />';
30         foreach($data['options'] as $value => $option){
31                 //keys
32                 if(!isset($data['use_keys'])){
33                         $this_value = $option;
34                 }
35                 else{
36                         $this_value = $value;
37                 }
39                 $button .= '<input type="radio" name="'.$data['name'].'" value="'.$this_value.'" '.($data['value'] == $this_value ? 'checked' : '').'>'.$option.'<br />';
40         }
42         return $button;
45 function form_checkbox_render_skin(&$data){
47         return $data['prefix'].
48                          ' <input type="checkbox" class="checkbox '.$data['class'].'" value="'.$data['value'].'" name="'.$data['name'].'" id="'.$data['id'].'"'.$data['attributes'].' ' . (($data['checked'] != '') ? 'checked="checked"' : '') . '"/>'.
49                         ($data['inline'] == 1 ? '' : '<br>').$data['text']. ($data['required'] == TRUE ? '<span class="form-required" title="This field is required.">*</span>' : '') . 
50                          ($data['help'] != '' ? '<br /><div class="description">'.$data['help'].'</div>' : '')
51                          .($data['sufix'] != '' ? $data['sufix'] : '').
52                         '<br>';
55 function form_textarea_render_skin(&$data){
57         $data['cols'] = (isset($data['cols']) ? $data['cols'] : 80);
59         return $data['prefix'].
60                          $data['text'].($data['required'] == TRUE ? '<span class="form-required" title="This field is required.">*</span>' : '').($data['inline'] == 1 ? '' : '<br>').
61                          ' <textarea class="'.$data['class'].'" name="'.$data['name'].'" id="'.$data['id'].'" cols="'.$data['cols'].'" rows="'.$data['rows'].'" '.$data['attributes'].' />'.$data['value'].'</textarea>'.
62                          ($data['help'] != '' ? '<br /><div class="description">'.$data['help'].'</div>' : '').
63                          $data['sufix'].'<br>';
67 function form_select_render_skin(&$data){
69         $select = $data['prefix'].$data['text'].($data['required'] == TRUE ? '<span class="form-required" title="This field is required.">*</span>' : '').($data['inline'] == 1 ? '' : '<br />').' <select name="'.$data['name'].'" id="'.$data['id'].'" class="'.$data['class'].'" '.$data['attributes'].'>';
71         if(!isset($data['required'])){
72                 $select .= '<option value="---">----</option>';
73         }
75         foreach($data['options'] as $value => $options){
76                 //keys
77                 if(!isset($data['use_keys'])){
78                         $this_value = $options;
79                 }
80                 else{
81                         $this_value = $value;
82                 }
84                 $selected = ((isset($data['value']) && $this_value == $data['value'] && $data['value'] != '---') ? 'selected' : '');
86                 $select .= '<option value="'.$this_value.'"'.$selected.'>'.$options.'</option>';
87         }
89         $select .= '</select><br />'.$data['sufix'];
90         $select .= (isset($data['help']) ? $data['help'] . '<br/>' : '');
92         return $select;
96 function form_file_render_skin(&$data){
98 $file = '<!-- The data encoding type, enctype, MUST be specified as below -->
99 <form enctype="multipart/form-data" action="__URL__" method="POST">
100     <!-- MAX_FILE_SIZE must precede the file input field -->
101     <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
102     <!-- Name of input element determines name in $_FILES array -->
103     Send this file: <input name="userfile" type="file" />
104     <input type="submit" value="Send File" />
105 </form>';
108 function form_submit_render_skin(&$data){
110         return $data['prefix'].
111                          '<br> <input type="submit" class="'.$data['class'].'" value="'.$data['value'].'" name="'.$data['name'].'" id="'.$data['id'].'" '.$data['attributes'].' />'
112                          .$data['sufix'];
115 function form_hidden_render_skin(&$data){
117         return ' <input type="hidden" value="'.$data['value'].'" name="'.$data['name'].'" id="'.$data['id'].'" />';
121 function form_fieldset_render_skin(&$data, $open = TRUE){
123         if($open){
124                 return '<fieldset><legend>' .$data['legend'] . '</legend>';
125         }
126         else
127                 return '</fieldset>';
131 function form_set_head($name, $action = NULL, $method = 'post', $extras = NULL){
133         global $x;
135         return '<form name="'.$name.'" enctype="multipart/form-data" id="'.$name.'" method="'.$method.'" action="'.path_set_url(isset($action) ? $action : $x).'&form=confirm" class="formular" '.$extras.'>';
140  * Render a form for review
141  * Renders a form with values from $_POST before reviewing it
142  */
143 function form_render_review(&$the_form, &$orig_form){
145         global $form_status;
147         //Cached Form
148         $form_name = array_keys($the_form);
150         //Replace the cached form with the new original form and its modifications. This will replace the values from POST
151         if($form_status == 'with_errors'){
152                 //Original Form
153                 $orig_form_name = array_keys($orig_form);
154         
155                 foreach($the_form[$form_name[0]] as $new_key => $new_data){
156                         $the_form[$form_name[0]][$new_key] = $orig_form[$orig_form_name[0]][$new_key];
157                 }
158         }
160         //Add values gathered from POST
161         foreach($the_form[$form_name[0]] as $key => $data){
163                 //add names
164                 if(!isset($the_form[$form_name[0]][$key]['name'])){
165                         $the_form[$form_name[0]][$key]['name'] = $key;
166                 }
168                 //I am not happy with this, but the alternative may imply to much memory use
169                 if($the_form[$form_name[0]][$key]['type'] == 'fieldset'){
170                         foreach($the_form[$form_name[0]][$key] as $field => $field_item){
171                                 if(is_array($field_item)){
172                                         if(!isset($field_item['name'])){
173                                                 $the_form[$form_name[0]][$key][$field]['name'] = $field;
174                                         }
175                                         $name = $the_form[$form_name[0]][$key][$field]['name'];
177                                         if($the_form[$form_name[0]][$key][$field]['type'] == 'checkbox'){
178                                                 if(isset($_POST[$name])){
179                                                         $the_form[$form_name[0]][$key][$field]['checked'] = 'checked';
180                                                         $the_form[$form_name[0]][$key][$field]['value'] = 1;
181                                                 }
182                                                 else{
183                                                         $the_form[$form_name[0]][$key][$field]['checked'] = '';
184                                                         $the_form[$form_name[0]][$key][$field]['value'] = 0;
185                                                 }
186                                         }
187                                         elseif($the_form[$form_name[0]][$key][$field]['type'] != 'submit'){
188                                                 $the_form[$form_name[0]][$key][$field]['value'] = $_POST[$name];
189                                         }
190                                 }
191                         }
192                 }
194                 $name = (isset($data['name']) ? $data['name'] : $key);
196                 if($the_form[$form_name[0]][$key]['type'] == 'checkbox'){
197                         if(isset($_POST[$name])){
198                                 $the_form[$form_name[0]][$key]['checked'] = 'checked';
199                                 $the_form[$form_name[0]][$key]['value'] = 1;
200                         }
201                         else{
202                                 $the_form[$form_name[0]][$key]['checked'] = '';
203                                 $the_form[$form_name[0]][$key]['value'] = 0;
204                         }
205                 }
206                 elseif($the_form[$form_name[0]][$key]['type'] != 'submit'){
207                         $the_form[$form_name[0]][$key]['value'] = $_POST[$name];
208                 }
210         }
215  * Render a form with values
216  * Renders a form with values given by user, this does basically the same as form_render_review
217  * and they may become one only function in the feature
218  */
219 function form_add_data(&$the_form, &$info){
221         //Cached Form
222         $form_name = array_keys($the_form);
223         $form_name = $form_name[0];
225         //Add values from $data
226         foreach($the_form[$form_name] as $key => $data){
228                 //I am not happy with this, but he option may imply to much memory use
229                 if($the_form[$form_name][$key]['type'] == 'fieldset'){
230                         foreach($the_form[$form_name][$key] as $field => $field_item){
231                                 if(is_array($field_item)){
232                                         $name = (isset($field_item['name']) ? $field_item['name'] : $field);
234                                         if($the_form[$form_name][$key][$field]['type'] == 'checkbox'){
235                                                 if(isset($info[$name]) && $info[$name] == 1){
236                                                         $the_form[$form_name][$key][$field]['checked'] = 'checked';
237                                                         $the_form[$form_name][$key][$field]['value'] = 1;
238                                                 }
239                                                 else{
240                                                         $the_form[$form_name][$key][$field]['checked'] = '';
241                                                         $the_form[$form_name][$key][$field]['value'] = 0;
242                                                 }
243                                         }
244                                         elseif($the_form[$form_name][$key][$field]['type'] != 'submit'){
245                                                 $the_form[$form_name][$key][$field]['value'] = $info[$name];
246                                         }
247                                 }
248                         }
249                 }
251                 $name = (isset($data['name']) ? $data['name'] : $key);
253                 if($the_form[$form_name][$key]['type'] == 'checkbox'){
254                         if(isset($info[$name]) && $info[$name] == 1){
255                                 $the_form[$form_name][$key]['checked'] = 'checked';
256                                 $the_form[$form_name][$key]['value'] = 1;
257                         }
258                         else{
259                                 $the_form[$form_name][$key]['checked'] = '';
260                                 $the_form[$form_name][$key]['value'] = 0;
261                         }
262                 }
263                 elseif($the_form[$form_name][$key]['type'] != 'submit'){
264                         $the_form[$form_name][$key]['value'] = $info[$name];
265                 }
267         }
272  * Verifies settings in forms
273  * This will verify certain settings used in the form such as required
274  * fields, max-lenght, data-type, format (some)...
275  */
276 function form_check_required(&$the_form){
278         global $form_status;
280         $form_name = array_keys($the_form);
281         $items     = array_keys($the_form[$form_name[0]]);
283         $keys = array_keys($the_form[$form_name[0]]);
285         foreach($keys as $k){
287                 //I am not happy with this, but he option may imply to much memory use
288                 if($the_form[$form_name[0]][$k]['type'] == 'fieldset'){
289                         foreach($the_form[$form_name[0]][$k] as $field => $field_item){
290                                 if(is_array($field_item)){
291                                         if($field_item['required'] == 1 && (!isset($field_item['value']) || $field_item['value'] == '')){
292                                                 $the_form[$form_name[0]][$k][$field]['class'] .= ' required error';
293                                                 system_warnings('Field required: ' . $the_form[$form_name[0]][$k][$field]['text'], 'error');
294                                                 $form_status = 'with_errors';
295                                         }
296                                 }
297                         }
298                 }
300                 //required
301                 if($the_form[$form_name[0]][$k]['required'] == 1 && (!isset($the_form[$form_name[0]][$k]['value']) || $the_form[$form_name[0]][$k]['value'] == '')){
302                         $the_form[$form_name[0]][$k]['class'] .= ' required error';
303                         system_warnings('Field required: ' . $the_form[$form_name[0]][$k]['text'], 'error');
304                         $form_status = 'with_errors';
305                         }
306         }