5 * Helper functions for the form api
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;
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){
32 if(!isset($data['use_keys'])){
33 $this_value = $option;
39 $button .= '<input type="radio" name="'.$data['name'].'" value="'.$this_value.'" '.($data['value'] == $this_value ? 'checked' : '').'>'.$option.'<br />';
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'] : '').
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>';
75 foreach($data['options'] as $value => $options){
77 if(!isset($data['use_keys'])){
78 $this_value = $options;
84 $selected = ((isset($data['value']) && $this_value == $data['value'] && $data['value'] != '---') ? 'selected' : '');
86 $select .= '<option value="'.$this_value.'"'.$selected.'>'.$options.'</option>';
89 $select .= '</select><br />'.$data['sufix'];
90 $select .= (isset($data['help']) ? $data['help'] . '<br/>' : '');
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" />
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'].' />'
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){
124 return '<fieldset><legend>' .$data['legend'] . '</legend>';
127 return '</fieldset>';
131 function form_set_head($name, $action = NULL, $method = 'post', $extras = NULL){
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
143 function form_render_review(&$the_form, &$orig_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'){
153 $orig_form_name = array_keys($orig_form);
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];
160 //Add values gathered from POST
161 foreach($the_form[$form_name[0]] as $key => $data){
164 if(!isset($the_form[$form_name[0]][$key]['name'])){
165 $the_form[$form_name[0]][$key]['name'] = $key;
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;
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;
183 $the_form[$form_name[0]][$key][$field]['checked'] = '';
184 $the_form[$form_name[0]][$key][$field]['value'] = 0;
187 elseif($the_form[$form_name[0]][$key][$field]['type'] != 'submit'){
188 $the_form[$form_name[0]][$key][$field]['value'] = $_POST[$name];
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;
202 $the_form[$form_name[0]][$key]['checked'] = '';
203 $the_form[$form_name[0]][$key]['value'] = 0;
206 elseif($the_form[$form_name[0]][$key]['type'] != 'submit'){
207 $the_form[$form_name[0]][$key]['value'] = $_POST[$name];
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
219 function form_add_data(&$the_form, &$info){
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;
240 $the_form[$form_name][$key][$field]['checked'] = '';
241 $the_form[$form_name][$key][$field]['value'] = 0;
244 elseif($the_form[$form_name][$key][$field]['type'] != 'submit'){
245 $the_form[$form_name][$key][$field]['value'] = $info[$name];
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;
259 $the_form[$form_name][$key]['checked'] = '';
260 $the_form[$form_name][$key]['value'] = 0;
263 elseif($the_form[$form_name][$key]['type'] != 'submit'){
264 $the_form[$form_name][$key]['value'] = $info[$name];
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)...
276 function form_check_required(&$the_form){
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';
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';