PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 1.07.11
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v1.07.11
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / controllers / FrmFieldsController.php

FrmFieldsController.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 1.07.11, at classes/controllers/FrmFieldsController.php

532 lines 20.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Formidable
4 */
5
6 if(!defined('ABSPATH')) die('You are not allowed to call this page directly.');
7
8 if(class_exists('FrmFieldsController'))
9 return;
10
11 class FrmFieldsController{
12 public static function load_hooks(){
13 add_action('wp_ajax_frm_load_field', 'FrmFieldsController::load_field');
14 add_action('wp_ajax_frm_insert_field', 'FrmFieldsController::create');
15 add_action('wp_ajax_frm_field_name_in_place_edit', 'FrmFieldsController::edit_name');
16 add_action('wp_ajax_frm_field_desc_in_place_edit', 'FrmFieldsController::edit_description');
17 add_action('wp_ajax_frm_update_ajax_option', 'FrmFieldsController::update_ajax_option');
18 add_action('wp_ajax_frm_duplicate_field', 'FrmFieldsController::duplicate');
19 add_action('wp_ajax_frm_delete_field', 'FrmFieldsController::destroy');
20 add_action('wp_ajax_frm_add_field_option', 'FrmFieldsController::add_option');
21 add_action('wp_ajax_frm_field_option_ipe', 'FrmFieldsController::edit_option');
22 add_action('wp_ajax_frm_delete_field_option', 'FrmFieldsController::delete_option');
23 add_action('wp_ajax_frm_import_choices', 'FrmFieldsController::import_choices');
24 add_action('wp_ajax_frm_import_options', 'FrmFieldsController::import_options');
25 add_action('wp_ajax_frm_update_field_order', 'FrmFieldsController::update_order');
26 add_filter('frm_field_type', 'FrmFieldsController::change_type');
27 add_filter('frm_display_field_options', 'FrmFieldsController::display_field_options');
28 add_action('frm_field_input_html', 'FrmFieldsController::input_html');
29 add_filter('frm_field_value_saved', 'FrmFieldsController::check_value', 50, 3);
30 add_filter('frm_field_label_seen', 'FrmFieldsController::check_label', 10, 3);
31 }
32
33 public static function load_field(){
34 $fields = $_POST['field'];
35 if ( empty($fields) ) {
36 die();
37 }
38
39 $_GET['page'] = 'formidable';
40 $fields = stripslashes_deep($fields);
41
42 $ajax = true;
43 $values = array();
44 $path = FrmAppHelper::plugin_path();
45 $field_html = array();
46
47 foreach ( $fields as $field ) {
48 $field = htmlspecialchars_decode(nl2br($field));
49 $field = json_decode($field, true);
50
51 $field_id = $field['id'];
52
53 if ( !isset($field['value']) ) {
54 $field['value'] = '';
55 }
56
57 $field_name = "item_meta[$field_id]";
58
59 ob_start();
60 include($path .'/classes/views/frm-forms/add_field.php');
61 $field_html[$field_id] = ob_get_contents();
62 ob_end_clean();
63 }
64
65 unset($path);
66
67 echo json_encode($field_html);
68
69 die();
70 }
71
72 public static function create(){
73 $field_data = $_POST['field'];
74 $form_id = $_POST['form_id'];
75 $values = array();
76 if(class_exists('FrmProForm'))
77 $values['post_type'] = FrmProFormsHelper::post_type($form_id);
78
79 $field_values = apply_filters('frm_before_field_created', FrmFieldsHelper::setup_new_vars($field_data, $form_id));
80
81 $frm_field = new FrmField();
82 $field_id = $frm_field->create( $field_values );
83
84 if ($field_id){
85 $field = FrmFieldsHelper::setup_edit_vars($frm_field->getOne($field_id));
86 $field_name = "item_meta[$field_id]";
87 $id = $form_id;
88 require(FrmAppHelper::plugin_path() .'/classes/views/frm-forms/add_field.php');
89 }
90 die();
91 }
92
93 public static function edit_name($field = 'name', $id = '') {
94 if ( empty($field) ) {
95 $field = 'name';
96 }
97
98 if ( empty($id) ) {
99 $id = str_replace('field_label_', '', $_POST['element_id']);
100 }
101
102 $value = trim($_POST['update_value']);
103 if ( trim(strip_tags($value)) == '' ) {
104 // set blank value if there is no content
105 $value = '';
106 }
107
108 $frm_field = new FrmField();
109 $form = $frm_field->update($id, array($field => $value));
110 echo stripslashes($value);
111 die();
112 }
113
114
115 public static function edit_description(){
116 $id = str_replace('field_description_', '', $_POST['element_id']);
117 self::edit_name('description', $id);
118 }
119
120 public static function update_ajax_option(){
121 $frm_field = new FrmField();
122 $field = $frm_field->getOne($_POST['field']);
123 foreach ( array('clear_on_focus', 'separate_value', 'default_blank') as $val ) {
124 if ( isset($_POST[$val]) ) {
125 $new_val = $_POST[$val];
126 if ( $val == 'separate_value' ) {
127 $new_val = (isset($field->field_options[$val]) && $field->field_options[$val]) ? 0 : 1;
128 }
129
130 $field->field_options[$val] = $new_val;
131 unset($new_val);
132 }
133 unset($val);
134 }
135
136 $frm_field->update($_POST['field'], array('field_options' => $field->field_options));
137 die();
138 }
139
140 public static function duplicate(){
141 global $wpdb;
142
143 $frm_field = new FrmField();
144 $copy_field = $frm_field->getOne($_POST['field_id']);
145 if (!$copy_field) return;
146
147 $values = array();
148 $values['field_key'] = FrmAppHelper::get_unique_key('', $wpdb->prefix . 'frm_fields', 'field_key');
149 $values['options'] = maybe_serialize($copy_field->options);
150 $values['default_value'] = maybe_serialize($copy_field->default_value);
151 $values['form_id'] = $copy_field->form_id;
152 foreach (array('name', 'description', 'type', 'field_options', 'required') as $col)
153 $values[$col] = $copy_field->{$col};
154 $field_count = FrmAppHelper::getRecordCount(array('form_id' => $copy_field->form_id), $wpdb->prefix . 'frm_fields');
155 $values['field_order'] = $field_count + 1;
156
157 $field_id = $frm_field->create($values);
158
159 if ($field_id){
160 $field = FrmFieldsHelper::setup_edit_vars($frm_field->getOne($field_id));
161 $field_name = "item_meta[$field_id]";
162 $id = $field['form_id'];
163 if($field['type'] == 'html')
164 $field['stop_filter'] = true;
165 require(FrmAppHelper::plugin_path() .'/classes/views/frm-forms/add_field.php');
166 }
167 die();
168 }
169
170 public static function destroy(){
171 $frm_field = new FrmField();
172 $field_id = $frm_field->destroy($_POST['field_id']);
173 die();
174 }
175
176 /* Field Options */
177 public static function add_option(){
178 $frm_field = new FrmField();
179
180 $id = $_POST['field_id'];
181 $field = $frm_field->getOne($id);
182 $options = maybe_unserialize($field->options);
183 if(!empty($options))
184 $last = max(array_keys($options));
185 else
186 $last = 0;
187
188 $opt_key = $last + 1;
189 $first_opt = reset($options);
190 $next_opt = count($options);
191 if($first_opt != '')
192 $next_opt++;
193 $opt = __('Option', 'formidable') .' '. $next_opt;
194 unset($next_opt);
195
196 $field_val = $opt;
197 $options[$opt_key] = $opt;
198 $frm_field->update($id, array('options' => maybe_serialize($options)));
199 $checked = '';
200
201 $field_data = $frm_field->getOne($id);
202 $field_data->field_options = maybe_unserialize($field_data->field_options);
203 $field = array();
204 $field['type'] = $field_data->type;
205 $field['id'] = $id;
206 $field['separate_value'] = isset($field_data->field_options['separate_value']) ? $field_data->field_options['separate_value'] : 0;
207 $field_name = "item_meta[$id]";
208
209 require(FrmAppHelper::plugin_path() .'/classes/views/frm-fields/single-option.php');
210 die();
211 }
212
213 public static function edit_option(){
214 $ids = explode('-', $_POST['element_id']);
215 $id = str_replace('field_', '', $ids[0]);
216 if ( strpos($_POST['element_id'], 'key_') ) {
217 $id = str_replace('key_', '', $id);
218 $new_value = trim($_POST['update_value']);
219 } else {
220 $new_label = trim($_POST['update_value']);
221 }
222
223 $frm_field = new FrmField();
224 $field = $frm_field->getOne($id);
225 $options = maybe_unserialize($field->options);
226 $this_opt = (array) $options[$ids[1]];
227
228 $label = isset($this_opt['label']) ? $this_opt['label'] : reset($this_opt);
229 if ( isset($this_opt['value']) ) {
230 $value = $this_opt['value'];
231 }
232
233 if ( !isset($new_label) ) {
234 $new_label = $label;
235 }
236
237 if ( isset($new_value) || isset($value) ) {
238 $update_value = isset($new_value) ? $new_value : $value;
239 }
240
241 if ( isset($update_value) && $update_value != $new_label ) {
242 $options[$ids[1]] = array('value' => $update_value, 'label' => $new_label);
243 } else {
244 $options[$ids[1]] = trim($_POST['update_value']);
245 }
246
247 $frm_field->update($id, array('options' => maybe_serialize($options)));
248 echo (trim($_POST['update_value']) == '') ? __('(Blank)', 'formidable') : stripslashes($_POST['update_value']);
249 die();
250 }
251
252 public static function delete_option(){
253 $frm_field = new FrmField();
254 $field = $frm_field->getOne($_POST['field_id']);
255 $options = maybe_unserialize($field->options);
256 unset($options[$_POST['opt_key']]);
257 $frm_field->update($_POST['field_id'], array('options' => maybe_serialize($options)));
258 die();
259 }
260
261 public static function import_choices(){
262 if ( !current_user_can('frm_edit_forms') ) {
263 return;
264 }
265
266 $field_id = $_REQUEST['field_id'];
267
268 global $current_screen, $hook_suffix;
269
270 // Catch plugins that include admin-header.php before admin.php completes.
271 if ( empty( $current_screen ) && function_exists('set_current_screen') ) {
272 $hook_suffix = '';
273 set_current_screen();
274 }
275
276 if ( function_exists('register_admin_color_schemes') ) {
277 register_admin_color_schemes();
278 }
279
280 $hook_suffix = $admin_body_class = '';
281
282 if ( get_user_setting('mfold') == 'f' )
283 $admin_body_class .= ' folded';
284
285 if ( function_exists('is_admin_bar_showing') && is_admin_bar_showing() ) {
286 $admin_body_class .= ' admin-bar';
287 }
288
289 if ( is_rtl() )
290 $admin_body_class .= ' rtl';
291
292 $admin_body_class .= ' admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'fresh' );
293 $prepop = array();
294 $prepop[__('Countries', 'formidable')] = FrmAppHelper::get_countries();
295
296 $states = FrmAppHelper::get_us_states();
297 $state_abv = array_keys($states);
298 sort($state_abv);
299 $prepop[__('U.S. State Abbreviations', 'formidable')] = $state_abv;
300 $states = array_values($states);
301 sort($states);
302 $prepop[__('U.S. States', 'formidable')] = $states;
303 unset($state_abv);
304 unset($states);
305
306 $prepop[__('Age', 'formidable')] = array(
307 __('Under 18', 'formidable'), __('18-24', 'formidable'), __('25-34', 'formidable'),
308 __('35-44', 'formidable'), __('45-54', 'formidable'), __('55-64', 'formidable'),
309 __('65 or Above', 'formidable'), __('Prefer Not to Answer', 'formidable')
310 );
311
312 $prepop[__('Satisfaction', 'formidable')] = array(
313 __('Very Satisfied', 'formidable'), __('Satisfied', 'formidable'), __('Neutral', 'formidable'),
314 __('Unsatisfied', 'formidable'), __('Very Unsatisfied', 'formidable'), __('N/A', 'formidable')
315 );
316
317 $prepop[__('Importance', 'formidable')] = array(
318 __('Very Important', 'formidable'), __('Important', 'formidable'), __('Neutral', 'formidable'),
319 __('Somewhat Important', 'formidable'), __('Not at all Important', 'formidable'), __('N/A', 'formidable')
320 );
321
322 $prepop[__('Agreement', 'formidable')] = array(
323 __('Strongly Agree', 'formidable'), __('Agree', 'formidable'), __('Neutral', 'formidable'),
324 __('Disagree', 'formidable'), __('Strongly Disagree', 'formidable'), __('N/A', 'formidable')
325 );
326
327 $prepop = apply_filters('frm_bulk_field_choices', $prepop);
328
329 $frm_field = new FrmField();
330 $field = $frm_field->getOne($field_id);
331
332 include(FrmAppHelper::plugin_path() .'/classes/views/frm-fields/import_choices.php');
333 die();
334 }
335
336 public static function import_options(){
337 if(!is_admin() or !current_user_can('frm_edit_forms'))
338 return;
339
340 extract(stripslashes_deep($_POST));
341
342 $frm_field = new FrmField();
343 $field = $frm_field->getOne($field_id);
344
345 if(!in_array($field->type, array('radio', 'checkbox', 'select')))
346 return;
347
348 $field = FrmFieldsHelper::setup_edit_vars($field);
349 $opts = explode("\n", rtrim($opts, "\n"));
350 if ( $field['separate_value'] ) {
351 foreach ( $opts as $opt_key => $opt ) {
352 if ( strpos($opt, '|') !== false ) {
353 $vals = explode('|', $opt);
354 if ( $vals[0] != $vals[1] ) {
355 $opts[$opt_key] = array('label' => trim($vals[0]), 'value' => trim($vals[1]));
356 }
357 unset($vals);
358 }
359 unset($opt_key);
360 unset($opt);
361 }
362 }
363
364 $frm_field->update($field_id, array('options' => maybe_serialize($opts)));
365
366 $field['options'] = $opts;
367 $field_name = $field['name'];
368
369 if ( $field['type'] == 'radio' || $field['type'] == 'checkbox' ) {
370 require(FrmAppHelper::plugin_path() .'/classes/views/frm-fields/radio.php');
371 } else {
372 foreach ( $field['options'] as $opt_key => $opt ) {
373 $field_val = apply_filters('frm_field_value_saved', $opt, $opt_key, $field);
374 $opt = apply_filters('frm_field_label_seen', $opt, $opt_key, $field);
375 require(FrmAppHelper::plugin_path() .'/classes/views/frm-fields/single-option.php');
376 }
377 }
378
379 die();
380 }
381
382 public static function update_order(){
383 if(isset($_POST) and isset($_POST['frm_field_id'])){
384 $frm_field = new FrmField();
385
386 foreach ($_POST['frm_field_id'] as $position => $item)
387 $frm_field->update($item, array('field_order' => $position));
388 }
389 die();
390 }
391
392 public static function change_type($type){
393 global $frm_vars;
394
395 if ($frm_vars['pro_is_installed']) return $type;
396
397 if($type == 'scale' || $type == '10radio')
398 $type = 'radio';
399 else if($type == 'rte')
400 $type = 'textarea';
401
402 $frm_field_selection = FrmFieldsHelper::field_selection();
403 $types = array_keys($frm_field_selection);
404 if (!in_array($type, $types) && $type != 'captcha')
405 $type = 'text';
406
407 return $type;
408 }
409
410 public static function display_field_options($display){
411 switch($display['type']){
412 case 'captcha':
413 $display['required'] = false;
414 $display['invalid'] = true;
415 $display['default_blank'] = false;
416 break;
417 case 'radio':
418 $display['default_blank'] = false;
419 break;
420 case 'text':
421 case 'textarea':
422 $display['size'] = true;
423 $display['clear_on_focus'] = true;
424 break;
425 case 'select':
426 $display['size'] = true;
427 break;
428 }
429
430 return $display;
431 }
432
433 public static function input_html($field, $echo=true){
434 global $frm_settings, $frm_vars;
435
436 $class = ''; //$field['type'];
437
438 if ( is_admin() && (!isset($frm_vars['preview']) || !$frm_vars['preview']) && !in_array($field['type'], array('scale', 'radio', 'checkbox', 'data')) ) {
439 $class .= 'dyn_default_value';
440 }
441
442 $add_html = '';
443
444 if(isset($field['size']) and $field['size'] > 0){
445 if(!in_array($field['type'], array('textarea', 'select', 'data', 'time', 'hidden')))
446 $add_html .= ' size="'. $field['size'] .'"';
447 $class .= " auto_width";
448 }
449
450 if ( isset($field['max']) && !in_array($field['type'], array('textarea', 'rte', 'hidden')) && !empty($field['max']) && (!is_admin() || !isset($_GET) || !isset($_GET['page']) || $_GET['page'] != 'formidable') ) {
451 $add_html .= ' maxlength="'. $field['max'] .'"';
452 }
453
454 if(!is_admin() or defined('DOING_AJAX') or !isset($_GET) or !isset($_GET['page']) or $_GET['page'] == 'formidable-entries'){
455 /*if(isset($field['required']) and $field['required']){
456 $action = isset($_REQUEST['frm_action']) ? 'frm_action' : 'action';
457 $action = FrmAppHelper::get_param($action);
458
459 //if($field['type'] != 'checkbox')
460 // $add_html .= ' required="required"';
461
462 if($field['type'] == 'file' and $action == 'edit'){
463 //don't add the required class if this is a file upload when editing
464 }else{
465 $class .= " required";
466 }
467 unset($action);
468 }*/
469
470 if(isset($field['clear_on_focus']) and $field['clear_on_focus'] and !empty($field['default_value'])){
471
472 if($frm_settings->use_html and !in_array($field['type'], array('select', 'radio', 'checkbox', 'hidden'))){
473 $add_html .= ' placeholder="'. esc_attr($field['default_value']) .'"';
474 FrmAppHelper::load_scripts('jquery-placeholder');
475 }else if(!$frm_settings->use_html){
476 $val = str_replace(array("\r\n", "\n"), '\r', addslashes(str_replace('&#039;', "'", esc_attr($field['default_value']))));
477 $add_html .= ' onfocus="frmClearDefault('."'". $val ."'". ',this)" onblur="frmReplaceDefault('."'". $val ."'". ',this)"';
478 if($field['value'] == $field['default_value'])
479 $class .= ' frm_default';
480 }
481 }
482 }
483
484 if(isset($field['input_class']) and !empty($field['input_class']))
485 $class .= ' '. $field['input_class'];
486
487 $class = apply_filters('frm_field_classes', $class, $field);
488
489 if(!empty($class))
490 $add_html .= ' class="'. trim($class) .'"';
491
492 if ( isset($field['shortcodes']) && !empty($field['shortcodes']) ) {
493 foreach ( $field['shortcodes'] as $k => $v ) {
494 if ( 'opt' === $k || (!is_numeric($k) && strpos($add_html, " $k=")) ) {
495 continue;
496 }
497
498 if ( is_numeric($k) && strpos($v, '=') ) {
499 $add_html .= ' '. $v;
500 } else {
501 $add_html .= ' '. $k .'="'. $v .'"';
502 }
503
504 unset($k, $v);
505 }
506 }
507
508 if($echo)
509 echo $add_html;
510
511 return $add_html;
512 }
513
514 public static function check_value($opt, $opt_key, $field){
515 if(is_array($opt)){
516 if(isset($field['separate_value']) and $field['separate_value']){
517 $opt = isset($opt['value']) ? $opt['value'] : (isset($opt['label']) ? $opt['label'] : reset($opt));
518 }else{
519 $opt = (isset($opt['label']) ? $opt['label'] : reset($opt));
520 }
521 }
522 return $opt;
523 }
524
525 public static function check_label($opt, $opt_key, $field){
526 if(is_array($opt))
527 $opt = (isset($opt['label']) ? $opt['label'] : reset($opt));
528
529 return $opt;
530 }
531 }
532