/**
  * Set the value for each field
  * This function is used when the form is first loaded and on all page turns *for a new entry*
  *
  * @since 2.0.13
  *
  * @param object $field - this is passed by reference since it is an object
  * @param boolean $reset
  * @param array $args
  * @return string|array $new_value
  */
 private static function get_field_value_for_new_entry($field, $reset, $args)
 {
     //If checkbox, multi-select dropdown, or checkbox data from entries field, the value should be an array
     $return_array = FrmField::is_field_with_multiple_values($field);
     // Do any shortcodes in default value and allow customization of default value
     $field->default_value = apply_filters('frm_get_default_value', $field->default_value, $field, true, $return_array);
     // Calls FrmProFieldsHelper::get_default_value
     $new_value = $field->default_value;
     if (!$reset && self::value_is_posted($field, $args)) {
         self::get_posted_value($field, $new_value, $args);
     } else {
         if (FrmField::is_option_true($field, 'clear_on_focus')) {
             // If clear on focus is selected, the value should be blank (unless it was posted, of course)
             // TODO: move to Pro
             if ('address' == $field->type && isset($new_value['country'])) {
                 $new_value = array('country' => $new_value['country']);
             } else {
                 $new_value = '';
             }
         }
     }
     if (!is_array($new_value)) {
         $new_value = str_replace('"', '"', $new_value);
     }
     return $new_value;
 }
 /**
  * Set the value for each field
  * This function is used when the form is first loaded and on all page turns *for a new entry*
  *
  * @since 2.0.13
  *
  * @param object $field - this is passed by reference since it is an object
  * @param boolean $reset
  * @return string|array $new_value
  */
 private static function get_field_value_for_new_entry($field, $reset)
 {
     //If checkbox, multi-select dropdown, or checkbox data from entries field, the value should be an array
     $return_array = FrmField::is_field_with_multiple_values($field);
     // Do any shortcodes in default value and allow customization of default value
     $field->default_value = apply_filters('frm_get_default_value', $field->default_value, $field, true, $return_array);
     // Calls FrmProFieldsHelper::get_default_value
     $new_value = $field->default_value;
     if (!$reset && $_POST && isset($_POST['item_meta'][$field->id])) {
         // If value was posted, get it
         $new_value = stripslashes_deep($_POST['item_meta'][$field->id]);
     } else {
         if (FrmField::is_option_true($field, 'clear_on_focus')) {
             // If clear on focus is selected, the value should be blank (unless it was posted, of course)
             $new_value = '';
         }
     }
     if (!is_array($new_value)) {
         $new_value = str_replace('"', '"', $new_value);
     }
     return $new_value;
 }
 public static function is_field_with_multiple_values($field)
 {
     _deprecated_function(__FUNCTION__, '2.0.9', 'FrmField::is_field_with_multiple_values');
     return FrmField::is_field_with_multiple_values($field);
 }
 /**
  * Called by the filter_where function
  */
 private static function prepare_where_args(&$args, $where_field, $entry_ids)
 {
     if ($args['where_val'] == 'NOW') {
         $date_format = 'Y-m-d';
         if ($where_field->type == 'time') {
             $time_format = isset($where_field->field_options['clock']) ? $where_field->field_options['clock'] : 12;
             $date_format = $time_format == 12 ? 'h:i A' : 'H:i';
         }
         $args['where_val'] = self::get_date($date_format);
         unset($date_format);
     }
     if ($where_field->type == 'date' && !empty($args['where_val'])) {
         $args['where_val'] = date('Y-m-d', strtotime($args['where_val']));
     } else {
         if ($args['where_is'] == '=' && $args['where_val'] != '' && FrmField::is_field_with_multiple_values($where_field)) {
             if ($where_field->type != 'data' || $where_field->field_options['data_type'] != 'checkbox' || is_numeric($args['where_val'])) {
                 // leave $args['where_is'] the same if this is a data from entries checkbox with a numeric value
                 $args['where_is'] = 'LIKE';
             }
         }
     }
     $args['temp_where_is'] = str_replace(array('!', 'not '), '', $args['where_is']);
     //get values that aren't blank and then remove them from entry list
     if ($args['where_val'] == '' && $args['temp_where_is'] == '=') {
         $args['temp_where_is'] = '!=';
     }
     if (in_array($args['where_is'], array('LIKE', 'not LIKE'))) {
         //add extra slashes to match values that are escaped in the database
         $args['where_val_esc'] = addslashes($args['where_val']);
     } else {
         if (!strpos($args['where_is'], 'in') && !is_numeric($args['where_val'])) {
             $args['where_val_esc'] = $args['where_val'];
         }
     }
     $filter_args = $args;
     $filter_args['entry_ids'] = $entry_ids;
     $args['where_val'] = apply_filters('frm_filter_where_val', $args['where_val'], $filter_args);
     self::prepare_dfe_text($args, $where_field);
 }
 public static function get_field_stats($id, $type = 'total', $user_id = false, $value = false, $round = 100, $limit = '', $atts = array(), $drafts = false)
 {
     global $wpdb, $frm_post_ids;
     $field = FrmField::getOne($id);
     if (!$field) {
         return 0;
     }
     $id = $field->id;
     if (isset($atts['thousands_sep']) && $atts['thousands_sep']) {
         $thousands_sep = $atts['thousands_sep'];
         unset($atts['thousands_sep']);
         $round = $round == 100 ? 2 : $round;
     }
     $where = array();
     if ($value) {
         $slash_val = strpos($value, '\\') === false ? addslashes($value) : $value;
         if (FrmField::is_field_with_multiple_values($field)) {
             $where[] = array('or' => 1, 'meta_value like' => $value, 'meta_value like ' => $slash_val);
             //add extra slashes to match values that are escaped in the database
         } else {
             //$where_value = $wpdb->prepare(" meta_value = %s", addcslashes( $slash_val, '_%' ) );
             $where[] = array('or' => 1, 'meta_value' => $value, 'meta_value ' => addcslashes($slash_val, '_%'));
         }
         unset($slash_val);
     }
     //if(!$frm_post_ids)
     $frm_post_ids = array();
     $post_ids = array();
     if (isset($frm_post_ids[$id])) {
         $form_posts = $frm_post_ids[$id];
     } else {
         $where_post = array('form_id' => $field->form_id, 'post_id >' => 1);
         if ($drafts != 'both') {
             $where_post['is_draft'] = $drafts;
         }
         if ($user_id) {
             $where_post['user_id'] = $user_id;
         }
         $form_posts = FrmDb::get_results('frm_items', $where_post, 'id,post_id');
         $frm_post_ids[$id] = $form_posts;
     }
     foreach ((array) $form_posts as $form_post) {
         $post_ids[$form_post->id] = $form_post->post_id;
     }
     if ($value) {
         $atts[$id] = $value;
     }
     if (!empty($atts)) {
         $entry_ids = array();
         if (isset($atts['entry_id']) && $atts['entry_id'] && is_numeric($atts['entry_id'])) {
             $entry_ids[] = $atts['entry_id'];
         }
         $after_where = false;
         foreach ($atts as $orig_f => $val) {
             // Accommodate for times when users are in Visual tab
             $val = str_replace(array('&gt;', '&lt;'), array('>', '<'), $val);
             // If first character is a quote, but the last character is not a quote
             if (strpos($val, '"') === 0 && substr($val, -1) != '"' || strpos($val, "'") === 0 && substr($val, -1) != "'") {
                 //parse atts back together if they were broken at spaces
                 $next_val = array('char' => substr($val, 0, 1), 'val' => $val);
                 continue;
                 // If we don't have a previous value that needs to be parsed back together
             } else {
                 if (!isset($next_val)) {
                     $temp = FrmAppHelper::replace_quotes($val);
                     foreach (array('"', "'") as $q) {
                         // Check if <" or >" exists in string and string does not end with ".
                         if (substr($temp, -1) != $q && (strpos($temp, '<' . $q) || strpos($temp, '>' . $q))) {
                             $next_val = array('char' => $q, 'val' => $val);
                             $cont = true;
                         }
                         unset($q);
                     }
                     unset($temp);
                     if (isset($cont)) {
                         unset($cont);
                         continue;
                     }
                 }
             }
             // If we have a previous value saved that needs to be parsed back together (due to WordPress pullling it apart)
             if (isset($next_val)) {
                 if (substr(FrmAppHelper::replace_quotes($val), -1) == $next_val['char']) {
                     $val = $next_val['val'] . ' ' . $val;
                     unset($next_val);
                 } else {
                     $next_val['val'] .= ' ' . $val;
                     continue;
                 }
             }
             $entry_ids = self::get_field_matches(compact('entry_ids', 'orig_f', 'val', 'id', 'atts', 'field', 'form_posts', 'after_where', 'drafts'));
             $after_where = true;
         }
         if (empty($entry_ids)) {
             if ($type == 'star') {
                 $stat = '';
                 ob_start();
                 include FrmAppHelper::plugin_path() . '/pro/classes/views/frmpro-fields/star_disabled.php';
                 $contents = ob_get_contents();
                 ob_end_clean();
                 return $contents;
             } else {
                 return 0;
             }
         }
         foreach ($post_ids as $entry_id => $post_id) {
             if (!in_array($entry_id, $entry_ids)) {
                 unset($post_ids[$entry_id]);
             }
         }
         $where['it.item_id'] = $entry_ids;
     }
     $join = '';
     if (is_numeric($id)) {
         $where['field_id'] = $id;
     } else {
         $join .= ' LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id';
         $where['fi.field_key'] = $id;
     }
     if ($user_id) {
         $where['en.user_id'] = $user_id;
     }
     $join .= ' LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_items en ON en.id=it.item_id';
     if ($drafts != 'both') {
         $where['en.is_draft'] = $drafts;
     }
     $field_metas = FrmDb::get_col($wpdb->prefix . 'frm_item_metas it ' . $join, $where, 'meta_value', array('order_by' => 'it.created_at DESC', 'limit' => $limit));
     if (!empty($post_ids)) {
         if (FrmField::is_option_true($field, 'post_field')) {
             if ($field->field_options['post_field'] == 'post_custom') {
                 //get custom post field value
                 $post_values = FrmDb::get_col($wpdb->postmeta, array('meta_key' => $field->field_options['custom_field'], 'post_id' => $post_ids), 'meta_value');
             } else {
                 if ($field->field_options['post_field'] == 'post_category') {
                     $post_query = array('tt.taxonomy' => $field->field_options['taxonomy'], 'tr.object_id' => $post_ids);
                     if ($value) {
                         $post_query[] = array('or' => 1, 't.term_id' => $value, 't.slug' => $value, 't.name' => $value);
                     }
                     $post_values = FrmDb::get_col($wpdb->terms . ' AS t INNER JOIN ' . $wpdb->term_taxonomy . ' AS tt ON tt.term_id = t.term_id INNER JOIN ' . $wpdb->term_relationships . ' AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id', $post_query, 'tr.object_id');
                     $post_values = array_unique($post_values);
                 } else {
                     $post_values = FrmDb::get_results($wpdb->posts, array('ID' => $post_ids), $field->field_options['post_field']);
                 }
             }
             $field_metas = array_merge($post_values, $field_metas);
         }
     }
     if ($type != 'star') {
         unset($field);
     }
     if (empty($field_metas)) {
         if ($type == 'star') {
             $stat = '';
             ob_start();
             include FrmAppHelper::plugin_path() . '/pro/classes/views/frmpro-fields/star_disabled.php';
             $contents = ob_get_contents();
             ob_end_clean();
             return $contents;
         } else {
             return 0;
         }
     }
     $count = count($field_metas);
     $total = array_sum($field_metas);
     switch ($type) {
         case 'average':
         case 'mean':
         case 'star':
             $stat = $total / $count;
             break;
         case 'median':
             rsort($field_metas);
             $n = ceil($count / 2);
             // Middle of the array
             if ($count % 2) {
                 $stat = $field_metas[$n - 1];
                 // If number is odd
             } else {
                 $n2 = floor($count / 2);
                 // Other middle of the array
                 $stat = ($field_metas[$n - 1] + $field_metas[$n2 - 1]) / 2;
             }
             $stat = maybe_unserialize($stat);
             if (is_array($stat)) {
                 $stat = 0;
             }
             break;
         case 'deviation':
             $mean = $total / $count;
             $stat = 0.0;
             foreach ($field_metas as $i) {
                 $stat += pow($i - $mean, 2);
             }
             if ($count > 1) {
                 $stat /= $count - 1;
                 $stat = sqrt($stat);
             } else {
                 $stat = 0;
             }
             break;
         case 'minimum':
             $stat = min($field_metas);
             break;
         case 'maximum':
             $stat = max($field_metas);
             break;
         case 'count':
             $stat = $count;
             break;
         case 'unique':
             $stat = array_unique($field_metas);
             $stat = count($stat);
             break;
         case 'total':
         default:
             $stat = $total;
     }
     $stat = round($stat, $round);
     if ($type == 'star') {
         ob_start();
         include FrmAppHelper::plugin_path() . '/pro/classes/views/frmpro-fields/star_disabled.php';
         $contents = ob_get_contents();
         ob_end_clean();
         return $contents;
     }
     if ($round && $round < 5 || isset($thousands_sep)) {
         $thousands_sep = isset($thousands_sep) ? $thousands_sep : ',';
         $stat = number_format($stat, $round, '.', $thousands_sep);
     }
     return $stat;
 }
 /**
  * Strip slashes and get rid of multi-dimensional arrays in inputs
  *
  * @since 2.0
  *
  * @param array $inputs
  * @param object $field
  * @param array $args
  * @return array $inputs - cleaned inputs array
  */
 public static function clean_inputs(&$inputs, $field, $args, $x_entries = array())
 {
     if (!$inputs) {
         return false;
     }
     //Break out any inner arrays (for checkbox or multi-select fields) and add them to the end of the $inputs array
     if (!$args['x_axis'] && FrmField::is_field_with_multiple_values($field)) {
         $count = 0;
         foreach ($inputs as $k => $i) {
             $i = maybe_unserialize($i);
             if (!is_array($i)) {
                 unset($k, $i);
                 continue;
             }
             unset($inputs[$k]);
             $count++;
             foreach ($i as $i_key => $item) {
                 // If this is an "other" option, keep key
                 if (strpos($i_key, 'other') !== false) {
                     $inputs[] = $i_key;
                 } else {
                     $inputs[] = $item;
                 }
                 unset($item, $i_key);
             }
             unset($k, $i);
         }
         unset($count);
     }
     if ($x_entries) {
         // Get rid of inputs if there is no match in x_inputs
         foreach ($inputs as $key => $input) {
             if (!in_array($input['item_id'], $x_entries)) {
                 unset($inputs[$key]);
             }
             unset($key, $input);
         }
     }
     //Strip slashes from inputs
     $inputs = stripslashes_deep($inputs);
     return $inputs;
 }
 public static function setup_new_vars($fields, $form = '', $reset = false)
 {
     global $frm_vars;
     $values = array();
     foreach (array('name' => '', 'description' => '', 'item_key' => '') as $var => $default) {
         $values[$var] = FrmAppHelper::get_post_param($var, $default);
     }
     $values['fields'] = array();
     if (empty($fields)) {
         return apply_filters('frm_setup_new_entry', $values);
     }
     foreach ((array) $fields as $field) {
         $default = $field->default_value;
         $posted_val = false;
         $new_value = $default;
         if (!$reset && $_POST && isset($_POST['item_meta'][$field->id]) && $_POST['item_meta'][$field->id] != '') {
             $new_value = stripslashes_deep($_POST['item_meta'][$field->id]);
             $posted_val = true;
         } else {
             if (FrmField::is_option_true($field, 'clear_on_focus')) {
                 $new_value = '';
             }
         }
         $is_default = $new_value == $default ? true : false;
         //If checkbox, multi-select dropdown, or checkbox data from entries field, set return array to true
         $return_array = FrmField::is_field_with_multiple_values($field);
         $field->default_value = apply_filters('frm_get_default_value', $field->default_value, $field, true, $return_array);
         if (!is_array($new_value)) {
             if ($is_default) {
                 $new_value = $field->default_value;
             } else {
                 if (!$posted_val) {
                     $new_value = apply_filters('frm_filter_default_value', $new_value, $field);
                 }
             }
             $new_value = str_replace('"', '&quot;', $new_value);
         }
         unset($is_default, $posted_val);
         $field_array = array('id' => $field->id, 'value' => $new_value, 'default_value' => $field->default_value, 'name' => $field->name, 'description' => $field->description, 'type' => apply_filters('frm_field_type', $field->type, $field, $new_value), 'options' => $field->options, 'required' => $field->required, 'field_key' => $field->field_key, 'field_order' => $field->field_order, 'form_id' => $field->form_id);
         $opt_defaults = FrmFieldsHelper::get_default_field_opts($field_array['type'], $field, true);
         $opt_defaults['required_indicator'] = '';
         $opt_defaults['original_type'] = $field->type;
         foreach ($opt_defaults as $opt => $default_opt) {
             $field_array[$opt] = isset($field->field_options[$opt]) && $field->field_options[$opt] != '' ? $field->field_options[$opt] : $default_opt;
             unset($opt, $default_opt);
         }
         unset($opt_defaults);
         if ($field_array['custom_html'] == '') {
             $field_array['custom_html'] = FrmFieldsHelper::get_default_html($field->type);
         }
         $field_array = apply_filters('frm_setup_new_fields_vars', $field_array, $field);
         $field_array = array_merge($field->field_options, $field_array);
         $values['fields'][] = $field_array;
         if (!$form || !isset($form->id)) {
             $form = FrmForm::getOne($field->form_id);
         }
     }
     $form->options = maybe_unserialize($form->options);
     if (is_array($form->options)) {
         foreach ($form->options as $opt => $value) {
             $values[$opt] = FrmAppHelper::get_post_param($opt, $value);
             unset($opt, $value);
         }
     }
     $form_defaults = FrmFormsHelper::get_default_opts();
     $frm_settings = FrmAppHelper::get_settings();
     $form_defaults['custom_style'] = $frm_settings->load_style != 'none';
     $values = array_merge($form_defaults, $values);
     return apply_filters('frm_setup_new_entry', $values);
 }