function get_formatted_time($date, $date_format = false, $time_format = false)
 {
     if (empty($date)) {
         return $date;
     }
     if (!$date_format) {
         $date_format = get_option('date_format');
     }
     if (preg_match('/^\\d{1-2}\\/\\d{1-2}\\/\\d{4}$/', $date)) {
         global $frmpro_settings;
         $date = FrmProAppHelper::convert_date($date, $frmpro_settings->date_format, 'Y-m-d');
     }
     $do_time = date('H:i:s', strtotime($date)) == '00:00:00' ? false : true;
     $date = get_date_from_gmt($date);
     $formatted = date_i18n($date_format, strtotime($date));
     if ($do_time) {
         if (!$time_format) {
             $time_format = get_option('time_format');
         }
         $trimmed_format = trim($time_format);
         if ($time_format and !empty($trimmed_format)) {
             $formatted .= ' ' . __('at', 'formidable') . ' ' . date_i18n($time_format, strtotime($date));
         }
     }
     return $formatted;
 }
Example #2
0
 public static function get_formatted_time($date, $date_format = '', $time_format = '')
 {
     if (empty($date)) {
         return $date;
     }
     if (empty($date_format)) {
         $date_format = get_option('date_format');
     }
     if (preg_match('/^\\d{1-2}\\/\\d{1-2}\\/\\d{4}$/', $date) && self::pro_is_installed()) {
         $frmpro_settings = new FrmProSettings();
         $date = FrmProAppHelper::convert_date($date, $frmpro_settings->date_format, 'Y-m-d');
     }
     $formatted = self::get_localized_date($date_format, $date);
     $do_time = date('H:i:s', strtotime($date)) != '00:00:00';
     if ($do_time) {
         $formatted .= self::add_time_to_date($time_format, $date);
     }
     return $formatted;
 }
Example #3
0
 function get_date($date, $date_format = false)
 {
     if (empty($date)) {
         return $date;
     }
     if (!$date_format) {
         $date_format = get_option('date_format');
     }
     if (preg_match('/^\\d{1-2}\\/\\d{1-2}\\/\\d{4}$/', $date)) {
         global $frmpro_settings;
         $date = FrmProAppHelper::convert_date($date, $frmpro_settings->date_format, 'Y-m-d');
     }
     return date_i18n($date_format, strtotime($date));
 }
 public static function set_post_fields($field, $value, $errors = null)
 {
     $field->field_options = maybe_unserialize($field->field_options);
     if (!isset($field->field_options['post_field']) || $field->field_options['post_field'] == '') {
         if (isset($errors)) {
             return $errors;
         }
         return;
     }
     if ($field->type == 'file') {
         global $frm_vars;
         if (!isset($frm_vars['media_id'])) {
             $frm_vars['media_id'] = array();
         }
         $frm_vars['media_id'][$field->id] = $value;
     }
     global $frmpro_settings;
     if ($value && !empty($value) && isset($field->field_options['unique']) && $field->field_options['unique']) {
         global $frmdb;
         $entry_id = isset($_POST) && isset($_POST['id']) ? $_POST['id'] : false;
         $post_id = $entry_id ? $frmdb->get_var($frmdb->entries, array('id' => $entry_id), 'post_id') : false;
         if (isset($errors) && FrmProEntryMetaHelper::post_value_exists($field->field_options['post_field'], $value, $post_id, $field->field_options['custom_field'])) {
             $errors['field' . $field->id] = FrmProFieldsHelper::get_error_msg($field, 'unique_msg');
         }
         unset($entry_id);
         unset($post_id);
     }
     if ($field->field_options['post_field'] == 'post_custom') {
         if ($field->type == 'date' and !preg_match('/^\\d{4}-\\d{2}-\\d{2}/', trim($value))) {
             $value = FrmProAppHelper::convert_date($value, $frmpro_settings->date_format, 'Y-m-d');
         }
         $_POST['frm_wp_post_custom'][$field->id . '=' . $field->field_options['custom_field']] = $value;
         if (isset($errors)) {
             return $errors;
         }
         return;
     }
     if ($field->field_options['post_field'] == 'post_date') {
         if (!preg_match('/^\\d{4}-\\d{2}-\\d{2}/', trim($value))) {
             $value = FrmProAppHelper::convert_date($value, $frmpro_settings->date_format, 'Y-m-d H:i:s');
         }
     } else {
         if ($field->type != 'tag' && $field->field_options['post_field'] == 'post_category') {
             $value = (array) $value;
             // change text to numeric ids
             if (defined('WP_IMPORTING')) {
                 foreach ($value as $k => $val) {
                     if (empty($val)) {
                         continue;
                     }
                     $term = term_exists($val, $field->field_options['taxonomy']);
                     if ($term) {
                         $value[$k] = is_array($term) ? $term['term_id'] : $term;
                     }
                     unset($k, $val, $term);
                 }
             }
             if (isset($field->field_options['taxonomy']) && $field->field_options['taxonomy'] != 'category') {
                 $new_value = array();
                 foreach ($value as $val) {
                     if ($val == 0) {
                         continue;
                     }
                     $term = get_term($val, $field->field_options['taxonomy']);
                     if (!isset($term->errors)) {
                         $new_value[$val] = $term->name;
                     } else {
                         $new_value[$val] = $val;
                     }
                     unset($term);
                 }
                 if (!isset($_POST['frm_tax_input'])) {
                     $_POST['frm_tax_input'] = array();
                 }
                 if (isset($_POST['frm_tax_input'][$field->field_options['taxonomy']])) {
                     foreach ($new_value as $new_key => $new_name) {
                         $_POST['frm_tax_input'][$field->field_options['taxonomy']][$new_key] = $new_name;
                     }
                 } else {
                     $_POST['frm_tax_input'][$field->field_options['taxonomy']] = $new_value;
                 }
             } else {
                 $_POST['frm_wp_post'][$field->id . '=' . $field->field_options['post_field']] = $value;
             }
         } else {
             if ($field->type == 'tag' && $field->field_options['post_field'] == 'post_category') {
                 $value = trim($value);
                 $value = array_map('trim', explode(',', $value));
                 $tax_type = isset($field->field_options['taxonomy']) && !empty($field->field_options['taxonomy']) ? $field->field_options['taxonomy'] : 'frm_tag';
                 if (!isset($_POST['frm_tax_input'])) {
                     $_POST['frm_tax_input'] = array();
                 }
                 if (is_taxonomy_hierarchical($tax_type)) {
                     //create the term or check to see if it exists
                     $terms = array();
                     foreach ($value as $v) {
                         $term_id = term_exists($v, $tax_type);
                         if (!$term_id) {
                             $term_id = wp_insert_term($v, $tax_type);
                         }
                         if ($term_id && is_array($term_id)) {
                             $term_id = $term_id['term_id'];
                         }
                         if (is_numeric($term_id)) {
                             $terms[$term_id] = $v;
                         }
                         unset($term_id);
                         unset($v);
                     }
                     $value = $terms;
                     unset($terms);
                 }
                 if (!isset($_POST['frm_tax_input'][$tax_type])) {
                     $_POST['frm_tax_input'][$tax_type] = (array) $value;
                 } else {
                     $_POST['frm_tax_input'][$tax_type] += (array) $value;
                 }
             }
         }
     }
     if ($field->field_options['post_field'] != 'post_category') {
         $_POST['frm_wp_post'][$field->id . '=' . $field->field_options['post_field']] = $value;
     }
     if (isset($errors)) {
         return $errors;
     }
 }
Example #5
0
 function validate($errors, $field)
 {
     if ($field->type == 'user_id') {
         // make sure we have a user ID
         if (!is_numeric($_POST['item_meta'][$field->id])) {
             $_POST['item_meta'][$field->id] = FrmProAppHelper::get_user_id_param($_POST['item_meta'][$field->id]);
         }
         //add user id to post variables to be saved with entry
         $_POST['frm_user_id'] = $_POST['item_meta'][$field->id];
     } else {
         if ($field->type == 'time' and is_array($_POST['item_meta'][$field->id])) {
             $_POST['item_meta'][$field->id] = $value = $_POST['item_meta'][$field->id]['H'] . ':' . $_POST['item_meta'][$field->id]['m'] . (isset($_POST['item_meta'][$field->id]['A']) ? ' ' . $_POST['item_meta'][$field->id]['A'] : '');
         }
     }
     // don't validate if going backwards
     if (FrmProFormsHelper::going_to_prev($field->form_id)) {
         return array();
     }
     // clear any existing errors if draft
     if (FrmProFormsHelper::saving_draft($field->form_id) && isset($errors['field' . $field->id])) {
         unset($errors['field' . $field->id]);
     }
     //if the field is a file upload, check for a file
     if ($field->type == 'file' && isset($_FILES['file' . $field->id]) && !empty($_FILES['file' . $field->id]['name'])) {
         $filled = true;
         if (is_array($_FILES['file' . $field->id]['name'])) {
             $filled = false;
             foreach ($_FILES['file' . $field->id]['name'] as $n) {
                 if (!empty($n)) {
                     $filled = true;
                 }
             }
         }
         if ($filled) {
             if (isset($errors['field' . $field->id])) {
                 unset($errors['field' . $field->id]);
             }
             if (isset($field->field_options['restrict']) && $field->field_options['restrict'] && isset($field->field_options['ftypes']) && !empty($field->field_options['ftypes'])) {
                 $mimes = $field->field_options['ftypes'];
             } else {
                 $mimes = null;
             }
             //check allowed mime types for this field
             if (is_array($_FILES['file' . $field->id]['name'])) {
                 foreach ($_FILES['file' . $field->id]['name'] as $name) {
                     if (empty($name)) {
                         continue;
                     }
                     $file_type = wp_check_filetype($name, $mimes);
                     unset($name);
                     if (!$file_type['ext']) {
                         break;
                     }
                 }
             } else {
                 $file_type = wp_check_filetype($_FILES['file' . $field->id]['name'], $mimes);
             }
             if (isset($file_type) && !$file_type['ext']) {
                 $errors['field' . $field->id] = $field->field_options['invalid'] == __('This field is invalid', 'formidable') || $field->field_options['invalid'] == '' || $field->field_options['invalid'] == $field->name . ' ' . __('is invalid', 'formidable') ? __('Sorry, this file type is not permitted for security reasons.', 'formidable') : $field->field_options['invalid'];
             }
             unset($file_type);
         }
         unset($filled);
     }
     // if saving draft, only check file type since it won't be checked later
     if (FrmProFormsHelper::saving_draft($field->form_id)) {
         return $errors;
     }
     if (in_array($field->type, array('break', 'html', 'divider'))) {
         $hidden = FrmProFieldsHelper::is_field_hidden($field, stripslashes_deep($_POST));
         global $frm_hidden_break, $frm_hidden_divider;
         if ($field->type == 'break') {
             $frm_hidden_break = array('field_order' => $field->field_order, 'hidden' => $hidden);
         } else {
             if ($field->type == 'divider') {
                 $frm_hidden_divider = array('field_order' => $field->field_order, 'hidden' => $hidden);
             }
         }
         if (isset($errors['field' . $field->id])) {
             unset($errors['field' . $field->id]);
         }
     }
     $value = $_POST['item_meta'][$field->id];
     if (($field->type != 'tag' and $value == 0 or $field->type == 'tag' and $value == '') and isset($field->field_options['post_field']) and $field->field_options['post_field'] == 'post_category' and $field->required == '1') {
         global $frm_settings;
         $errors['field' . $field->id] = (!isset($field->field_options['blank']) or $field->field_options['blank'] == '' or $field->field_options['blank'] == 'Untitled cannot be blank') ? $frm_settings->blank_msg : $field->field_options['blank'];
     }
     //Don't require fields hidden with shortcode fields="25,26,27"
     global $frm_vars;
     if (isset($frm_vars['show_fields']) and !empty($frm_vars['show_fields']) and is_array($frm_vars['show_fields']) and $field->required == '1' and isset($errors['field' . $field->id]) and !in_array($field->id, $frm_vars['show_fields']) and !in_array($field->field_key, $frm_vars['show_fields'])) {
         unset($errors['field' . $field->id]);
         $_POST['item_meta'][$field->id] = $value = '';
     }
     //Don't require a conditionally hidden field
     if (isset($field->field_options['hide_field']) and !empty($field->field_options['hide_field'])) {
         if (FrmProFieldsHelper::is_field_hidden($field, stripslashes_deep($_POST))) {
             if (isset($errors['field' . $field->id])) {
                 unset($errors['field' . $field->id]);
             }
             $_POST['item_meta'][$field->id] = $value = '';
         }
     }
     //Don't require a field hidden in a conditional page or section heading
     if (isset($errors['field' . $field->id]) or $_POST['item_meta'][$field->id] != '') {
         global $frm_hidden_break, $frm_hidden_divider;
         if ($frm_hidden_break and $frm_hidden_break['hidden'] or $frm_hidden_divider and $frm_hidden_divider['hidden'] and (!$frm_hidden_break or $frm_hidden_break['field_order'] < $frm_hidden_divider['field_order'])) {
             if (isset($errors['field' . $field->id])) {
                 unset($errors['field' . $field->id]);
             }
             $_POST['item_meta'][$field->id] = $value = '';
         }
     }
     //make sure the [auto_id] is still unique
     if (!empty($field->default_value) and !is_array($field->default_value) and !empty($value) and is_numeric($value) and strpos($field->default_value, '[auto_id') !== false) {
         //make sure we are not editing
         if (isset($_POST) and !isset($_POST['id']) or !is_numeric($_POST['id'])) {
             $_POST['item_meta'][$field->id] = $value = FrmProFieldsHelper::get_default_value($field->default_value, $field);
         }
     }
     //check uniqueness
     if ($value and !empty($value) and isset($field->field_options['unique']) and $field->field_options['unique']) {
         $entry_id = (isset($_POST) and isset($_POST['id'])) ? $_POST['id'] : false;
         if ($field->type == 'time') {
             //TODO: add server-side validation for unique date-time
         } else {
             if ($field->type == 'date') {
                 global $frmpro_settings;
                 $old_value = $value;
                 if (!preg_match('/^\\d{4}-\\d{2}-\\d{2}$/', trim($value))) {
                     $value = FrmProAppHelper::convert_date($value, $frmpro_settings->date_format, 'Y-m-d');
                 }
                 if (FrmProEntryMetaHelper::value_exists($field->id, $value, $entry_id)) {
                     $errors['field' . $field->id] = FrmProFieldsHelper::get_error_msg($field, 'unique_msg');
                 }
                 $value = $old_value;
             } else {
                 if (FrmProEntryMetaHelper::value_exists($field->id, $value, $entry_id)) {
                     $errors['field' . $field->id] = FrmProFieldsHelper::get_error_msg($field, 'unique_msg');
                 }
             }
         }
         unset($entry_id);
     }
     // validate number settings
     if ('number' == $field->type && $value != '') {
         global $frm_settings;
         // only check if options are available in settings
         if ($frm_settings->use_html && isset($field->field_options['minnum']) && isset($field->field_options['maxnum'])) {
             //minnum maxnum
             if ((double) $value < $field->field_options['minnum']) {
                 $errors['field' . $field->id] = __('Please select a higher number', 'formidable');
             } else {
                 if ((double) $value > $field->field_options['maxnum']) {
                     $errors['field' . $field->id] = __('Please select a lower number', 'formidable');
                 }
             }
         }
     }
     if (!empty($value) and ($field->type == 'website' or $field->type == 'url' or $field->type == 'image')) {
         if (trim($value) == 'http://') {
             $_POST['item_meta'][$field->id] = $value = '';
         } else {
             $value = esc_url_raw($value);
             $_POST['item_meta'][$field->id] = $value = preg_match('/^(https?|ftps?|mailto|news|feed|telnet):/is', $value) ? $value : 'http://' . $value;
         }
     }
     $errors = FrmProEntryMetaHelper::set_post_fields($field, $value, $errors);
     if (!FrmProFieldsHelper::is_field_visible_to_user($field)) {
         //don't validate admin only fields that can't be seen
         unset($errors['field' . $field->id]);
         return $errors;
     }
     if (false and isset($field->field_options['use_calc']) and !empty($field->field_options['use_calc']) and !empty($field->field_options['calc'])) {
         $field->field_options['calc'] = trim($field->field_options['calc']);
         preg_match_all("/\\[(.*?)\\]/s", $field->field_options['calc'], $calc_matches, PREG_PATTERN_ORDER);
         if (isset($calc_matches[1])) {
             foreach ($calc_matches[1] as $c) {
                 if (is_numeric($c)) {
                     $c_id = $c;
                 } else {
                     global $frm_field;
                     $c_field = $frm_field->getOne($c);
                     if (!$c_field) {
                         $field->field_options['calc'] = str_replace('[' . $c . ']', 0, $field->field_options['calc']);
                         continue;
                     }
                     $c_id = $c_field->id;
                     unset($c_field);
                 }
                 $c_val = trim($_POST['item_meta'][$c_id]);
                 if (!is_numeric($c_val)) {
                     preg_match_all('/[0-9,]*\\.?[0-9]+/', $c_val, $c_matches);
                     $c_val = $c_matches ? end($c_matches[0]) : 0;
                     unset($c_matches);
                 }
                 if ($c_val == '') {
                     $c_val = 0;
                 }
                 $field->field_options['calc'] = str_replace('[' . $c . ']', $c_val, $field->field_options['calc']);
                 unset($c);
                 unset($c_id);
             }
             include FrmAppHelper::plugin_path() . '/pro/classes/helpers/FrmProMathHelper.php';
             $m = new EvalMath();
             if (strpos($field->field_options['calc'], ').toFixed(')) {
                 $field->field_options['calc'] = str_replace(').toFixed(2', '', $field->field_options['calc']);
                 $round = 2;
             }
             $result = $m->evaluate(str_replace('Math.', '', '(' . $field->field_options['calc'] . ')'));
             if (isset($round) and $round) {
                 $result = sprintf('%.' . $round . 'f', $result);
             }
             unset($m);
             $_POST['item_meta'][$field->id] = $value = $result;
             unset($result);
         }
         unset($calc_matches);
     }
     //Don't validate the format if field is blank
     if ($value == '' or is_array($value)) {
         return $errors;
     }
     $value = trim($value);
     //validate the format
     if ($field->type == 'number' and !is_numeric($value) or $field->type == 'email' and !is_email($value) or ($field->type == 'website' or $field->type == 'url' or $field->type == 'image') and !preg_match('/^http(s)?:\\/\\/([\\da-z\\.-]+)\\.([\\da-z\\.-]+)/i', $value)) {
         $errors['field' . $field->id] = FrmProFieldsHelper::get_error_msg($field, 'invalid');
     }
     if ($field->type == 'phone') {
         $pattern = (isset($field->field_options['format']) and !empty($field->field_options['format'])) ? $field->field_options['format'] : '^((\\+\\d{1,3}(-|.| )?\\(?\\d\\)?(-| |.)?\\d{1,5})|(\\(?\\d{2,6}\\)?))(-|.| )?(\\d{3,4})(-|.| )?(\\d{4})(( x| ext)\\d{1,5}){0,1}$';
         $pattern = apply_filters('frm_phone_pattern', $pattern, $field);
         //check if format is already a regular expression
         if (strpos($pattern, '^') !== 0) {
             //if not, create a regular expression
             $pattern = preg_replace('/\\d/', '\\d', preg_quote($pattern));
             $pattern = '/^' . $pattern . '$/';
         } else {
             $pattern = '/' . $pattern . '/';
         }
         if (!preg_match($pattern, $value)) {
             $errors['field' . $field->id] = FrmProFieldsHelper::get_error_msg($field, 'invalid');
         }
         unset($pattern);
     }
     if ($field->type == 'date') {
         if (!preg_match('/^\\d{4}-\\d{2}-\\d{2}$/', $value)) {
             global $frmpro_settings;
             $formated_date = FrmProAppHelper::convert_date($value, $frmpro_settings->date_format, 'Y-m-d');
             //check format before converting
             if ($value != date($frmpro_settings->date_format, strtotime($formated_date))) {
                 $errors['field' . $field->id] = FrmProFieldsHelper::get_error_msg($field, 'invalid');
             }
             $value = $formated_date;
             unset($formated_date);
         }
         $date = explode('-', $value);
         if (count($date) != 3 or !checkdate((int) $date[1], (int) $date[2], (int) $date[0])) {
             $errors['field' . $field->id] = FrmProFieldsHelper::get_error_msg($field, 'invalid');
         }
     }
     return $errors;
 }
 public static function get_single_date($date, $date_format)
 {
     if (preg_match('/^\\d{1-2}\\/\\d{1-2}\\/\\d{4}$/', $date)) {
         $frmpro_settings = new FrmProSettings();
         $date = FrmProAppHelper::convert_date($date, $frmpro_settings->date_format, 'Y-m-d');
     }
     return date_i18n($date_format, strtotime($date));
 }
Example #7
0
 public static function validate_date_field(&$errors, $field, $value)
 {
     if ($field->type != 'date') {
         return;
     }
     if (!preg_match('/^\\d{4}-\\d{2}-\\d{2}$/', $value)) {
         $frmpro_settings = new FrmProSettings();
         $formated_date = FrmProAppHelper::convert_date($value, $frmpro_settings->date_format, 'Y-m-d');
         //check format before converting
         if ($value != date($frmpro_settings->date_format, strtotime($formated_date))) {
             $errors['field' . $field->temp_id] = FrmFieldsHelper::get_error_msg($field, 'invalid');
         }
         $value = $formated_date;
         unset($formated_date);
     }
     $date = explode('-', $value);
     if (count($date) != 3 || !checkdate((int) $date[1], (int) $date[2], (int) $date[0])) {
         $errors['field' . $field->temp_id] = FrmFieldsHelper::get_error_msg($field, 'invalid');
     }
 }
 public static function ajax_time_options()
 {
     global $frmpro_settings, $frmdb, $wpdb, $frm_entry_meta;
     //posted vars = $time_field, $date_field, $date
     extract($_POST);
     $time_key = str_replace('field_', '', $time_field);
     $date_key = str_replace('field_', '', $date_field);
     if (!preg_match('/^\\d{4}-\\d{2}-\\d{2}$/', trim($date))) {
         $date = FrmProAppHelper::convert_date($date, $frmpro_settings->date_format, 'Y-m-d');
     }
     $date_entries = $frm_entry_meta->getEntryIds(array('fi.field_key' => $date_key, 'meta_value' => $date));
     $remove = array();
     if ($date_entries and !empty($date_entries)) {
         $query = $wpdb->prepare("SELECT meta_value FROM {$frmdb->entry_metas} it LEFT JOIN {$frmdb->fields} fi ON (it.field_id = fi.id) WHERE fi.field_key=%s", $time_key);
         if (is_numeric($entry_id)) {
             $query = $wpdb->prepare(' and it.item_id != %d', $entry_id);
         }
         $used_times = $wpdb->get_col("{$query} and it.item_id in (" . implode(',', $date_entries) . ")");
         if ($used_times and !empty($used_times)) {
             $number_allowed = apply_filters('frm_allowed_time_count', 1, $time_key, $date_key);
             $count = array();
             foreach ($used_times as $used) {
                 if (isset($remove[$used])) {
                     continue;
                 }
                 if (!isset($count[$used])) {
                     $count[$used] = 0;
                 }
                 $count[$used]++;
                 if ((int) $count[$used] >= $number_allowed) {
                     $remove[$used] = $used;
                 }
             }
             unset($count);
         }
     }
     echo json_encode($remove);
     die;
 }
 /**
  * Combine dates when using created-at, updated-at, or date field on x-axis
  *
  * @since 2.0
  *
  * @param boolean $combine_dates - will be true if combining dates
  * @param array $values
  * @param array $labels
  * @param array $tooltips
  * @param array $f_values - additional field values
  * @param array $args - arguments
  */
 public static function combine_dates(&$combine_dates, &$values, &$labels, &$tooltips, &$f_values, $args)
 {
     if (isset($args['x_field']) && $args['x_field'] && $args['x_field']->type == 'date' || in_array($args['x_axis'], array('created_at', 'updated_at'))) {
         $combine_dates = apply_filters('frm_combine_dates', true, $args['x_field']);
     }
     if ($combine_dates === false) {
         return;
     }
     if ($args['include_zero']) {
         $start_timestamp = empty($args['start_date']) ? strtotime('-1 month') : strtotime($args['start_date']);
         $end_timestamp = empty($args['end_date']) ? time() : strtotime($args['end_date']);
         $dates_array = array();
         // Get the dates array
         for ($e = $start_timestamp; $e <= $end_timestamp; $e += 60 * 60 * 24) {
             $dates_array[] = date('Y-m-d', $e);
         }
         unset($e);
         // Add the zero count days
         foreach ($dates_array as $date_str) {
             if (!in_array($date_str, $labels)) {
                 $labels[$date_str] = $date_str;
                 $values[$date_str] = 0;
                 foreach ($args['ids'] as $f_id) {
                     if (!isset($f_values[$f_id][$date_str])) {
                         $f_values[$f_id][$date_str] = 0;
                     }
                 }
             }
         }
         unset($dates_array, $start_timestamp, $end_timestamp);
     }
     asort($labels);
     foreach ($labels as $l_key => $l) {
         if ((isset($args['x_field']) && $args['x_field'] && $args['x_field']->type == 'date' || in_array($args['x_axis'], array('created_at', 'updated_at'))) && !$args['group_by']) {
             if ($args['type'] != 'pie' && preg_match('/^\\d{4}-\\d{2}-\\d{2}$/', $l)) {
                 $frmpro_settings = new FrmProSettings();
                 $labels[$l_key] = FrmProAppHelper::convert_date($l, 'Y-m-d', $frmpro_settings->date_format);
             }
         }
         unset($l_key, $l);
     }
     $values = FrmProAppHelper::sort_by_array($values, array_keys($labels));
     $tooltips = FrmProAppHelper::sort_by_array($tooltips, array_keys($labels));
     foreach ($args['ids'] as $f_id) {
         $f_values[$f_id] = FrmProAppHelper::sort_by_array($f_values[$f_id], array_keys($labels));
         $f_values[$f_id] = FrmProAppHelper::reset_keys($f_values[$f_id]);
         ksort($f_values[$f_id]);
         unset($f_id);
     }
 }
Example #10
0
 /**
  * Convert timestamps to the database format
  */
 private static function convert_timestamps(&$values)
 {
     $offset = get_option('gmt_offset') * 60 * 60;
     $frmpro_settings = new FrmProSettings();
     foreach (array('created_at', 'updated_at') as $stamp) {
         if (!isset($values[$stamp])) {
             continue;
         }
         // adjust the date format if it starts with the day
         if (!preg_match('/^\\d{4}-\\d{2}-\\d{2}/', trim($values[$stamp])) && substr($frmpro_settings->date_format, 0, 1) == 'd') {
             $reg_ex = str_replace(array('/', '.', '-', 'd', 'j', 'm', 'y', 'Y'), array('\\/', '\\.', '\\-', '\\d{2}', '\\d', '\\d{2}', '\\d{2}', '\\d{4}'), $frmpro_settings->date_format);
             if (preg_match('/^' . $reg_ex . '/', trim($values[$stamp]))) {
                 $values[$stamp] = FrmProAppHelper::convert_date($values[$stamp], $frmpro_settings->date_format, 'Y-m-d H:i:s');
             }
         }
         $values[$stamp] = date('Y-m-d H:i:s', strtotime($values[$stamp]) - $offset);
         unset($stamp);
     }
 }
 function get_graph_values($field, $args)
 {
     global $frm_entry_meta, $frm_field, $frmdb, $wpdb;
     $values = $labels = $f_values = $f_labels = $rows = $cols = array();
     $pie = false;
     extract($args);
     $show_key = (int) $show_key;
     if ($show_key and $show_key < 5) {
         $show_key = 10;
     }
     $options = array('width' => $width, 'height' => $height, 'legend' => 'none');
     if (!empty($colors)) {
         $options['colors'] = $colors;
     }
     $options['title'] = preg_replace("/&#?[a-z0-9]{2,8};/i", "", FrmAppHelper::truncate($field->name, $truncate, 0));
     if ($show_key) {
         $options['legend'] = array('position' => 'right', 'textStyle' => array('fontSize' => $show_key));
     }
     $fields = $f_inputs = array();
     $fields[$field->id] = $field;
     if ($ids) {
         $ids = explode(',', $ids);
         foreach ($ids as $id_key => $f) {
             $ids[$id_key] = $f = trim($f);
             if (!$f or empty($f)) {
                 unset($ids[$id_key]);
                 continue;
             }
             if ($add_field = $frm_field->getOne($f)) {
                 $fields[$add_field->id] = $add_field;
                 $ids[$id_key] = $add_field->id;
             }
             unset($f);
             unset($id_key);
         }
     } else {
         $ids = array();
     }
     $cols['xaxis'] = array('type' => 'string');
     if ($x_axis) {
         $x_field = $frm_field->getOne($x_axis);
         $query = $x_query = "SELECT meta_value, item_id FROM {$frmdb->entry_metas} em";
         if (!$x_field) {
             $x_query = "SELECT id, {$x_axis} FROM {$frmdb->entries} e";
         }
         if ($user_id) {
             $query .= " LEFT JOIN {$frmdb->entries} e ON (e.id=em.item_id)";
             if ($x_field) {
                 $x_query .= " LEFT JOIN {$frmdb->entries} e ON (e.id=em.item_id)";
             }
         }
         if ($x_field) {
             if (isset($allowed_col_types)) {
                 $cols['xaxis'] = array('type' => in_array($x_field->type, $allowed_col_types) ? $x_field->type : 'string', 'id' => $x_field->id);
             }
             $options['hAxis'] = array('title' => stripslashes($x_field->name));
             $x_query .= " WHERE em.field_id='{$x_field->id}'";
             if (!empty($x_start)) {
                 if ($x_field->type == 'date') {
                     $x_start = date('Y-m-d', strtotime($x_start));
                 }
                 $x_query .= " and meta_value >= '{$x_start}'";
             }
             if (!empty($x_end)) {
                 if ($x_field->type == 'date') {
                     $x_end = date('Y-m-d', strtotime($x_end));
                 }
                 $x_query .= " and meta_value <= '{$x_end}'";
             }
         } else {
             $cols['xaxis'] = array('type' => 'string');
             $x_query .= " WHERE form_id=" . $field->form_id;
             if (!empty($x_start)) {
                 if (in_array($x_axis, array('created_at', 'updated_at'))) {
                     $x_start = date('Y-m-d', strtotime($x_start));
                 }
                 $x_query .= " and e.{$x_axis} >= '{$x_start}'";
             }
             if (!empty($x_end)) {
                 if (in_array($x_axis, array('created_at', 'updated_at'))) {
                     $x_end = date('Y-m-d', strtotime($x_end)) . ' 23:59:59';
                 }
                 $x_query .= " and e.{$x_axis} <= '{$x_end}'";
             }
         }
         $q = array();
         foreach ($fields as $f_id => $f) {
             if ($f_id != $field->id) {
                 $q[$f_id] = $query . " WHERE em.field_id='{$f_id}'" . ($user_id ? " AND user_id='{$user_id}'" : '');
             }
             unset($f);
             unset($f_id);
         }
         $query .= " WHERE em.field_id='{$field->id}'";
         if ($user_id) {
             $query .= " AND user_id='{$user_id}'";
             $x_query .= " AND user_id='{$user_id}'";
         }
         $inputs = $wpdb->get_results($query, ARRAY_A);
         $x_inputs = $wpdb->get_results($x_query, ARRAY_A);
         if (!$x_inputs) {
             $x_inputs = array('id' => '0');
         }
         unset($query);
         unset($x_query);
         foreach ($q as $f_id => $query) {
             $f_inputs[$f_id] = $wpdb->get_results($query, ARRAY_A);
             unset($query);
         }
         unset($q);
     } else {
         if ($user_id) {
             $inputs = $wpdb->get_col("SELECT meta_value FROM {$frmdb->entry_metas} em LEFT JOIN {$frmdb->entries} e ON (e.id=em.item_id) WHERE em.field_id='{$field->id}' AND user_id='{$user_id}'");
         } else {
             $inputs = $frm_entry_meta->get_entry_metas_for_field($field->id);
         }
         foreach ($fields as $f_id => $f) {
             if ($f_id != $field->id) {
                 $f_inputs[$f_id] = $wpdb->get_col("SELECT meta_value FROM {$frmdb->entry_metas} em LEFT JOIN {$frmdb->entries} e ON (e.id=em.item_id) WHERE em.field_id='{$f_id}'" . ($user_id ? " AND user_id='{$user_id}'" : ''));
             }
             unset($f_id);
             unset($f);
         }
     }
     $inputs = array_map('maybe_unserialize', $inputs);
     $inputs = stripslashes_deep($inputs);
     foreach ($f_inputs as $f_id => $f) {
         $f = array_map('maybe_unserialize', $f);
         $f_inputs[$f_id] = stripslashes_deep($f);
         unset($f_id);
         unset($f);
     }
     if (isset($allowed_col_types)) {
         //add columns for each field
         foreach ($fields as $f_id => $f) {
             $cols[stripslashes($f->name)] = array('type' => in_array($f->type, $allowed_col_types) ? $f->type : 'number', 'id' => $f->id);
             unset($f);
             unset($f_id);
         }
         unset($allowed_col_types);
     }
     $field_options = maybe_unserialize($field->options);
     $field->field_options = maybe_unserialize($field->field_options);
     global $frm_posts;
     if ($frm_posts and isset($frm_posts[$field->form_id])) {
         $form_posts = $frm_posts[$field->form_id];
     } else {
         $form_posts = $frmdb->get_records($frmdb->entries, array('form_id' => $field->form_id, 'post_id >' => 1), '', '', 'id,post_id');
         if (!$frm_posts) {
             $frm_posts = array();
         }
         $frm_posts[$field->form_id] = $form_posts;
     }
     if (!empty($form_posts)) {
         if (isset($field->field_options['post_field']) and $field->field_options['post_field'] != '') {
             if ($field->field_options['post_field'] == 'post_category') {
                 $field_options = FrmProFieldsHelper::get_category_options($field);
             } else {
                 if ($field->field_options['post_field'] == 'post_custom' and $field->field_options['custom_field'] != '') {
                     //check custom fields
                     foreach ($form_posts as $form_post) {
                         $meta_value = get_post_meta($form_post->post_id, $field->field_options['custom_field'], true);
                         if ($meta_value) {
                             if ($x_axis) {
                                 $inputs[] = array('meta_value' => $meta_value, 'item_id' => $form_post->id);
                             } else {
                                 $inputs[] = $meta_value;
                             }
                         }
                     }
                 } else {
                     //if field is post field
                     if ($field->field_options['post_field'] == 'post_status') {
                         $field_options = FrmProFieldsHelper::get_status_options($field);
                     }
                     foreach ($form_posts as $form_post) {
                         $post_value = $wpdb->get_var("SELECT " . $field->field_options['post_field'] . " FROM {$wpdb->posts} WHERE ID=" . $form_post->post_id);
                         if ($post_value) {
                             if ($x_axis) {
                                 $inputs[] = array('meta_value' => $post_value, 'item_id' => $form_post->id);
                             } else {
                                 $inputs[] = $post_value;
                             }
                         }
                     }
                 }
             }
         }
     }
     if ($field->type == 'data') {
         foreach ($inputs as $k => $i) {
             if (is_numeric($i)) {
                 if (is_array($inputs[$k]) and isset($inputs[$k]['meta_value'])) {
                     $inputs[$k]['meta_value'] = FrmProFieldsHelper::get_data_value($inputs[$k]['meta_value'], $field, array('truncate' => 'truncate_label'));
                 } else {
                     $inputs[$k] = FrmProFieldsHelper::get_data_value($inputs[$k], $field, array('truncate' => 'truncate_label'));
                 }
             }
             unset($k);
             unset($i);
         }
     }
     if (isset($x_inputs) and $x_inputs) {
         $x_temp = array();
         foreach ($x_inputs as $x_input) {
             if ($x_field) {
                 $x_temp[$x_input['item_id']] = $x_input['meta_value'];
             } else {
                 $x_temp[$x_input['id']] = $x_input[$x_axis];
             }
         }
         $x_inputs = apply_filters('frm_graph_value', $x_temp, $x_field ? $x_field : $x_axis, $args);
         unset($x_temp);
         unset($x_input);
     }
     if ($x_axis and $inputs) {
         $y_temp = array();
         foreach ($inputs as $input) {
             $y_temp[$input['item_id']] = $input['meta_value'];
         }
         foreach ($ids as $f_id) {
             if (!isset($f_values[$f_id])) {
                 $f_values[$f_id] = array();
             }
             $f_values[$f_id][key($y_temp)] = 0;
             unset($f_id);
         }
         $inputs = $y_temp;
         unset($y_temp);
         unset($input);
     }
     $inputs = apply_filters('frm_graph_value', $inputs, $field, $args);
     foreach ($f_inputs as $f_id => $f) {
         $temp = array();
         foreach ($f as $input) {
             if (is_array($input)) {
                 $temp[$input['item_id']] = $input['meta_value'];
                 foreach ($ids as $d) {
                     if (!isset($f_values[$d][$input['item_id']])) {
                         $f_values[$d][$input['item_id']] = 0;
                     }
                     unset($d);
                 }
             } else {
                 $temp[] = $input;
             }
             unset($input);
         }
         $f_inputs[$f_id] = apply_filters('frm_graph_value', $temp, $fields[$f_id], $args);
         unset($temp);
         unset($input);
         unset($f);
     }
     if (in_array($field->type, array('select', 'checkbox', 'radio', '10radio', 'scale')) and (!isset($x_inputs) or !$x_inputs)) {
         if ($limit == '') {
             $limit = 10;
         }
         $field_opt_count = count($field_options);
         if ($field_options) {
             foreach ($field_options as $opt_key => $opt) {
                 $field_val = apply_filters('frm_field_value_saved', $opt, $opt_key, $field->field_options);
                 $opt = apply_filters('frm_field_label_seen', $opt, $opt_key, $field);
                 $count = 0;
                 if (empty($opt)) {
                     continue;
                 }
                 $opt = stripslashes_deep($opt);
                 foreach ($inputs as $in) {
                     if (FrmAppHelper::check_selected($in, $field_val)) {
                         if ($data_type == 'total') {
                             $count += $field_val;
                         } else {
                             $count++;
                         }
                     }
                 }
                 $new_val = FrmAppHelper::truncate($opt, $truncate_label, 2);
                 if ($count > 0 or $field_opt_count < $limit or !$count and $include_zero) {
                     $labels[$new_val] = $new_val;
                     $values[$new_val] = $count;
                 }
                 unset($count);
                 foreach ($f_inputs as $f_id => $f) {
                     foreach ($f as $in) {
                         if (!isset($f_values[$f_id])) {
                             $f_values[$f_id] = array();
                         }
                         if (!isset($f_values[$f_id][$new_val])) {
                             $f_values[$f_id][$new_val] = 0;
                         }
                         if (FrmAppHelper::check_selected($in, $field_val)) {
                             if ($data_type == 'total') {
                                 $f_values[$f_id][$new_val] += $field_val;
                             } else {
                                 $f_values[$f_id][$new_val]++;
                             }
                         }
                         unset($in);
                     }
                     unset($f_id);
                     unset($f);
                 }
             }
             if ($limit != 10 and count($values) > $limit) {
                 $ordered_vals = $values;
                 arsort($ordered_vals);
                 $l_count = 0;
                 foreach ($ordered_vals as $vkey => $v) {
                     $l_count++;
                     if ($l_count > $limit) {
                         unset($values[$vkey]);
                         unset($labels[$vkey]);
                     }
                     unset($vkey);
                     unset($v);
                 }
                 unset($l_count);
                 unset($ordered_vals);
             }
         }
         if (!in_array($field->type, array('checkbox', '10radio', 'scale'))) {
             //and count($field_options) == 2
             $pie = true;
         }
     } else {
         if ($field->type == 'user_id') {
             $form = $frmdb->get_one_record($frmdb->forms, array('id' => $field->form_id));
             $form_options = maybe_unserialize($form->options);
             $id_count = array_count_values($inputs);
             if ($form->editable and (isset($form_options['single_entry']) and isset($form_options['single_entry_type']) and $form_options['single_entry_type'] == 'user')) {
                 //if only one response per user, do a pie chart of users who have submitted the form
                 $users_of_blog = function_exists('get_users') ? get_users() : get_users_of_blog();
                 $total_users = count($users_of_blog);
                 unset($users_of_blog);
                 $id_count = count($id_count);
                 $not_completed = (int) $total_users - (int) $id_count;
                 $labels = array(__('Completed', 'formidable'), __('Not Completed', 'formidable'));
                 $values = array($id_count, $not_completed);
                 $pie = true;
             } else {
                 //arsort($id_count);
                 foreach ($id_count as $val => $count) {
                     $user_info = get_userdata($val);
                     $labels[] = $user_info ? $user_info->display_name : __('Deleted User', 'formidable');
                     $values[] = $count;
                 }
                 if (count($labels) < 10) {
                     $pie = true;
                 }
             }
         } else {
             if (isset($x_inputs) and $x_inputs) {
                 $calc_array = array();
                 foreach ($inputs as $entry_id => $in) {
                     $entry_id = (int) $entry_id;
                     if (!isset($values[$entry_id])) {
                         $values[$entry_id] = 0;
                     }
                     $labels[$entry_id] = isset($x_inputs[$entry_id]) ? $x_inputs[$entry_id] : '';
                     if (!isset($calc_array[$entry_id])) {
                         $calc_array[$entry_id] = array('count' => 0);
                     }
                     if ($data_type == 'total' or $data_type == 'average') {
                         $values[$entry_id] += (double) $in;
                         $calc_array[$entry_id]['total'] = $values[$entry_id];
                         $calc_array[$entry_id]['count']++;
                     } else {
                         $values[$entry_id]++;
                     }
                     unset($entry_id);
                     unset($in);
                 }
                 if ($data_type == 'average') {
                     foreach ($calc_array as $entry_id => $calc) {
                         $values[$entry_id] = $calc['total'] / $calc['count'];
                         unset($entry_id);
                         unset($calc);
                     }
                 }
                 $calc_array = array();
                 foreach ($f_inputs as $f_id => $f) {
                     if (!isset($calc_array[$f_id])) {
                         $calc_array[$f_id] = array();
                     }
                     foreach ($f as $entry_id => $in) {
                         $entry_id = (int) $entry_id;
                         if (!isset($labels[$entry_id])) {
                             $labels[$entry_id] = isset($x_inputs[$entry_id]) ? $x_inputs[$entry_id] : '';
                             $values[$entry_id] = 0;
                         }
                         if (!isset($calc_array[$f_id][$entry_id])) {
                             $calc_array[$f_id][$entry_id] = array('count' => 0);
                         }
                         if (!isset($f_values[$f_id][$entry_id])) {
                             $f_values[$f_id][$entry_id] = 0;
                         }
                         if ($data_type == 'total' or $data_type == 'average') {
                             $f_values[$f_id][$entry_id] += (double) $in;
                             $calc_array[$f_id][$entry_id]['total'] = $f_values[$f_id][$entry_id];
                             $calc_array[$f_id][$entry_id]['count']++;
                         } else {
                             $f_values[$f_id][$entry_id]++;
                         }
                         unset($entry_id);
                         unset($in);
                     }
                     unset($f_id);
                     unset($f);
                 }
                 if ($data_type == 'average') {
                     foreach ($calc_array as $f_id => $calc) {
                         foreach ($calc as $entry_id => $c) {
                             $f_values[$f_id][$entry_id] = $c['total'] / $c['count'];
                             unset($entry_id);
                             unset($c);
                         }
                         unset($calc);
                         unset($f_id);
                     }
                 }
                 unset($calc_array);
             } else {
                 if (is_array(reset($inputs))) {
                     $id_count = array_map('implode', $inputs);
                     $id_count = array_count_values(array_map('strtolower', $id_count));
                 } else {
                     $id_count = array_count_values(array_map('strtolower', $inputs));
                     arsort($id_count);
                 }
                 $i = 0;
                 foreach ($id_count as $val => $count) {
                     if ($i < $response_count) {
                         if ($field->type == 'user_id') {
                             $user_info = get_userdata($val);
                             $new_val = $user_info->display_name;
                         } else {
                             $new_val = ucwords($val);
                         }
                         $labels[$new_val] = $new_val;
                         $values[$new_val] = $count;
                     }
                     $i++;
                 }
                 foreach ($f_inputs as $f_id => $f) {
                     $id_count = array_count_values(array_map('strtolower', $f));
                     arsort($id_count);
                     $i = 0;
                     foreach ($id_count as $val => $count) {
                         if ($i < $response_count) {
                             if ($field->type == 'user_id') {
                                 $user_info = get_userdata($val);
                                 $new_val = $user_info->display_name;
                             } else {
                                 $new_val = ucwords($val);
                             }
                             $position = array_search($new_val, $labels);
                             if (!$position) {
                                 end($labels);
                                 $position = key($labels);
                                 $labels[$new_val] = $new_val;
                                 $values[$new_val] = 0;
                             }
                             $f_values[$f_id][$new_val] = $count;
                         }
                         $i++;
                     }
                     unset($f_id);
                     unset($f);
                 }
             }
         }
     }
     if (isset($x_inputs) and $x_inputs) {
         $used_vals = $calc_array = array();
         foreach ($labels as $l_key => $label) {
             if (empty($label) and (!empty($x_start) or !empty($x_end))) {
                 unset($values[$l_key]);
                 unset($labels[$l_key]);
                 continue;
             }
             if (in_array($x_axis, array('created_at', 'updated_at'))) {
                 if ($type == 'pie') {
                     $labels[$l_key] = $label = $inputs[$l_key];
                 } else {
                     $labels[$l_key] = $label = date('Y-m-d', strtotime($label));
                 }
             }
             if (isset($used_vals[$label])) {
                 $values[$l_key] += $values[$used_vals[$label]];
                 unset($values[$used_vals[$label]]);
                 foreach ($ids as $f_id) {
                     if (!isset($f_values[$f_id][$l_key])) {
                         $f_values[$f_id][$l_key] = 0;
                     }
                     if (!isset($f_values[$f_id][$used_vals[$label]])) {
                         $f_values[$f_id][$used_vals[$label]] = 0;
                     }
                     $f_values[$f_id][$l_key] += $f_values[$f_id][$used_vals[$label]];
                     unset($f_values[$f_id][$used_vals[$label]]);
                     unset($f_id);
                 }
                 unset($labels[$used_vals[$label]]);
             }
             $used_vals[$label] = $l_key;
             if ($data_type == 'average') {
                 if (!isset($calc_array[$label])) {
                     $calc_array[$label] = 0;
                 }
                 $calc_array[$label]++;
             }
             unset($label);
             unset($l_key);
         }
         if (!empty($calc_array)) {
             foreach ($calc_array as $label => $calc) {
                 if (isset($used_vals[$label])) {
                     $values[$used_vals[$label]] = $values[$used_vals[$label]] / $calc;
                     foreach ($ids as $f_id) {
                         $f_values[$f_id][$used_vals[$label]] = $f_values[$f_id][$used_vals[$label]] / $calc;
                         unset($f_id);
                     }
                 }
                 unset($label);
                 unset($calc);
             }
         }
         unset($used_vals);
     }
     $combine_dates = false;
     if (isset($x_field) and $x_field and $x_field->type == 'date' or in_array($x_axis, array('created_at', 'updated_at'))) {
         $combine_dates = apply_filters('frm_combine_dates', true, $x_field);
     }
     if ($combine_dates) {
         if ($include_zero) {
             $start_timestamp = empty($x_start) ? time() : strtotime($x_start);
             $end_timestamp = empty($x_end) ? time() : strtotime($x_end);
             $dates_array = array();
             // Get the dates array
             for ($e = $start_timestamp; $e <= $end_timestamp; $e += 60 * 60 * 24) {
                 $dates_array[] = date('Y-m-d', $e);
             }
             unset($e);
             // Add the zero count days
             foreach ($dates_array as $date_str) {
                 if (!in_array($date_str, $labels)) {
                     $labels[$date_str] = $date_str;
                     $values[$date_str] = 0;
                     foreach ($ids as $f_id) {
                         if (!isset($f_values[$f_id][$date_str])) {
                             $f_values[$f_id][$date_str] = 0;
                         }
                     }
                 }
             }
             unset($dates_array);
             unset($start_timestamp);
             unset($end_timestamp);
         }
         asort($labels);
         global $frmpro_settings;
         foreach ($labels as $l_key => $l) {
             if (isset($x_field) and $x_field and $x_field->type == 'date' or in_array($x_axis, array('created_at', 'updated_at'))) {
                 if ($type != 'pie' and preg_match('/^\\d{4}-\\d{2}-\\d{2}$/', $l)) {
                     global $frmpro_settings;
                     $labels[$l_key] = FrmProAppHelper::convert_date($l, 'Y-m-d', $frmpro_settings->date_format);
                 }
             }
             unset($l_key);
             unset($l);
         }
         $values = FrmProAppHelper::sort_by_array($values, array_keys($labels));
         foreach ($ids as $f_id) {
             $f_values[$f_id] = FrmProAppHelper::sort_by_array($f_values[$f_id], array_keys($labels));
             $f_values[$f_id] = FrmProAppHelper::reset_keys($f_values[$f_id]);
             ksort($f_values[$f_id]);
             unset($f_id);
         }
     } else {
         if (isset($x_inputs) and $x_inputs) {
             foreach ($labels as $l_key => $l) {
                 foreach ($ids as $f_id) {
                     //do a last check to make sure all bars/lines have a value for each label
                     if (!isset($f_values[$f_id][$l_key])) {
                         $f_values[$f_id][$l_key] = 0;
                     }
                     unset($fid);
                 }
                 unset($l_key);
                 unset($l);
             }
         }
         foreach ($ids as $f_id) {
             $f_values[$f_id] = FrmProAppHelper::reset_keys($f_values[$f_id]);
             ksort($f_values[$f_id]);
             unset($f_id);
         }
         ksort($labels);
         ksort($values);
     }
     $labels = FrmProAppHelper::reset_keys($labels);
     $values = FrmProAppHelper::reset_keys($values);
     $return = array('total_count' => count($inputs), 'f_values' => $f_values, 'labels' => $labels, 'values' => $values, 'pie' => $pie, 'combine_dates' => $combine_dates, 'ids' => $ids, 'cols' => $cols, 'rows' => $rows, 'options' => $options, 'fields' => $fields);
     if (isset($x_inputs)) {
         $return['x_inputs'] = $x_inputs;
     }
     return $return;
 }
 function ajax_time_options()
 {
     global $frmpro_settings, $frmdb, $wpdb;
     //posted vars = $time_field, $date_field, $step, $start, $end, $date, $clock
     extract($_POST);
     $time_key = str_replace('field_', '', $time_field);
     $date_key = str_replace('field_', '', $date_field);
     if (!preg_match('/^\\d{4}-\\d{2}-\\d{2}$/', trim($date))) {
         $date = FrmProAppHelper::convert_date($date, $frmpro_settings->date_format, 'Y-m-d');
     }
     $date_entries = FrmEntryMeta::getEntryIds("fi.field_key='{$date_key}' and meta_value='{$date}'");
     $opts = array('' => '');
     $time = strtotime($start);
     $end = strtotime($end);
     $step = explode(':', $step);
     $step = isset($step[1]) ? $step[0] * 3600 + $step[1] * 60 : $step[0] * 60;
     $format = $clock ? 'H:i' : 'h:i A';
     while ($time <= $end) {
         $opts[date($format, $time)] = date($format, $time);
         $time += $step;
     }
     if ($date_entries and !empty($date_entries)) {
         $used_times = $wpdb->get_col("SELECT meta_value FROM {$frmdb->entry_metas} it LEFT JOIN {$frmdb->fields} fi ON (it.field_id = fi.id) WHERE fi.field_key='{$time_key}' and it.item_id in (" . implode(',', $date_entries) . ")");
         if ($used_times and !empty($used_times)) {
             $number_allowed = apply_filters('frm_allowed_time_count', 1, $time_key, $date_key);
             $count = array();
             foreach ($used_times as $used) {
                 if (!isset($opts[$used])) {
                     continue;
                 }
                 if (!isset($count[$used])) {
                     $count[$used] = 0;
                 }
                 $count[$used]++;
                 if ((int) $count[$used] >= $number_allowed) {
                     unset($opts[$used]);
                 }
             }
             unset($count);
         }
     }
     echo json_encode($opts);
     die;
 }
 function set_post_fields($field, $value, $errors = false)
 {
     $field->field_options = maybe_unserialize($field->field_options);
     if (isset($field->field_options['post_field']) and $field->field_options['post_field'] != '') {
         global $frmpro_settings;
         if ($value and !empty($value) and isset($field->field_options['unique']) and $field->field_options['unique']) {
             global $frmdb;
             $entry_id = (isset($_POST) and isset($_POST['id'])) ? $_POST['id'] : false;
             if ($entry_id) {
                 $post_id = $frmdb->get_var($frmdb->entries, array('id' => $entry_id), 'post_id');
             } else {
                 $post_id = false;
             }
             if ($errors and $this->post_value_exists($field->field_options['post_field'], $value, $post_id, $field->field_options['custom_field'])) {
                 $errors['field' . $field->id] = $field->name . ' ' . __('must be unique', 'formidable');
             }
             unset($entry_id);
             unset($post_id);
         }
         if ($field->field_options['post_field'] == 'post_custom') {
             if ($field->type == 'date' and !preg_match('/^\\d{4}-\\d{2}-\\d{2}/', trim($value))) {
                 $value = FrmProAppHelper::convert_date($value, $frmpro_settings->date_format, 'Y-m-d');
             } else {
                 if ($field->type == 'file') {
                     global $frm_media_id;
                     $frm_media_id[$field->id] = $value;
                 }
             }
             $_POST['frm_wp_post_custom'][$field->id . '=' . $field->field_options['custom_field']] = $value;
         } else {
             if ($field->field_options['post_field'] == 'post_date') {
                 if (!preg_match('/^\\d{4}-\\d{2}-\\d{2}/', trim($value))) {
                     $value = FrmProAppHelper::convert_date($value, $frmpro_settings->date_format, 'Y-m-d H:i:s');
                 }
             } else {
                 if ($field->type != 'tag' and $field->field_options['post_field'] == 'post_category') {
                     $value = (array) $value;
                     if (isset($field->field_options['taxonomy']) and $field->field_options['taxonomy'] != 'category') {
                         $new_value = array();
                         foreach ($value as $val) {
                             $term = get_term($val, $field->field_options['taxonomy']);
                             if (!isset($term->errors)) {
                                 $new_value[$val] = $term->name;
                             } else {
                                 $new_value[$val] = $val;
                             }
                         }
                         if (!isset($_POST['frm_tax_input'])) {
                             $_POST['frm_tax_input'] = array();
                         }
                         if (isset($_POST['frm_tax_input'][$field->field_options['taxonomy']])) {
                             foreach ($new_value as $new_key => $new_name) {
                                 $_POST['frm_tax_input'][$field->field_options['taxonomy']][$new_key] = $new_name;
                             }
                         } else {
                             $_POST['frm_tax_input'][$field->field_options['taxonomy']] = $new_value;
                         }
                     } else {
                         $_POST['frm_wp_post'][$field->id . '=' . $field->field_options['post_field']] = $value;
                     }
                 } else {
                     if ($field->type == 'tag' and $field->field_options['post_field'] == 'post_category') {
                         //$tags = explode(',', $value);
                         $tax_type = (isset($field->field_options['taxonomy']) and !empty($field->field_options['taxonomy'])) ? $field->field_options['taxonomy'] : 'frm_tag';
                         if (!isset($_POST['frm_tax_input'])) {
                             $_POST['frm_tax_input'] = array();
                         }
                         $_POST['frm_tax_input'][$tax_type] = $value;
                         //unset($tags);
                     }
                 }
             }
             if ($field->field_options['post_field'] != 'post_category') {
                 $_POST['frm_wp_post'][$field->id . '=' . $field->field_options['post_field']] = $value;
             }
         }
     }
     if ($errors) {
         return $errors;
     }
 }
Example #14
0
 public static function import_csv($path, $form_id, $field_ids, $entry_key = 0, $start_row = 2, $del = ',', $max = 250)
 {
     global $importing_fields, $wpdb, $frmpro_settings;
     if (!defined('WP_IMPORTING')) {
         define('WP_IMPORTING', true);
     }
     $form_id = (int) $form_id;
     if (!$form_id) {
         return $start_row;
     }
     if (!$importing_fields) {
         $importing_fields = array();
     }
     if (!ini_get('safe_mode')) {
         set_time_limit(0);
     }
     //Remove time limit to execute this function
     if ($f = fopen($path, "r")) {
         unset($path);
         global $frm_entry, $frmdb, $frm_field;
         $row = 0;
         //setlocale(LC_ALL, get_locale());
         while (($data = fgetcsv($f, 100000, $del)) !== FALSE) {
             $row++;
             if ($start_row > $row) {
                 continue;
             }
             $values = array('form_id' => $form_id);
             $values['item_meta'] = array();
             foreach ($field_ids as $key => $field_id) {
                 $data[$key] = isset($data[$key]) ? $data[$key] : '';
                 if (is_numeric($field_id)) {
                     if (isset($importing_fields[$field_id])) {
                         $field = $importing_fields[$field_id];
                     } else {
                         $field = $frm_field->getOne($field_id);
                         $importing_fields[$field_id] = $field;
                     }
                     $values['item_meta'][$field_id] = apply_filters('frm_import_val', $data[$key], $field);
                     switch ($field->type) {
                         case 'user_id':
                             $values['item_meta'][$field_id] = FrmProAppHelper::get_user_id_param(trim($values['item_meta'][$field_id]));
                             $_POST['frm_user_id'] = $values['frm_user_id'] = $values['item_meta'][$field_id];
                             break;
                         case 'checkbox':
                         case 'select':
                             $values['item_meta'][$field_id] = self::get_multi_opts($values['item_meta'][$field_id], $field);
                             break;
                         case 'data':
                             $values['item_meta'][$field_id] = self::get_dfe_id($values['item_meta'][$field_id], $field, array(), $values);
                             break;
                         case 'file':
                             $values['item_meta'][$field_id] = self::get_file_id($values['item_meta'][$field_id]);
                             break;
                         case 'date':
                             $values['item_meta'][$field_id] = self::get_date($values['item_meta'][$field_id]);
                             break;
                     }
                     if (isset($_POST['item_meta'][$field_id]) and ($field->type == 'checkbox' or $field->type == 'data' and $field->field_options['data_type'] != 'checkbox')) {
                         if (empty($values['item_meta'][$field_id])) {
                             $values['item_meta'][$field_id] = $_POST['item_meta'][$field_id];
                         } else {
                             if (!empty($_POST['item_meta'][$field_id])) {
                                 $values['item_meta'][$field_id] = array_merge((array) $_POST['item_meta'][$field_id], (array) $values['item_meta'][$field_id]);
                             }
                         }
                     }
                     $_POST['item_meta'][$field_id] = $values['item_meta'][$field_id];
                     FrmProEntryMetaHelper::set_post_fields($field, $values['item_meta'][$field_id]);
                     unset($field);
                 } else {
                     if (is_array($field_id)) {
                         $field_type = isset($field_id['type']) ? $field_id['type'] : false;
                         $linked = isset($field_id['linked']) ? $field_id['linked'] : false;
                         $field_id = $field_id['field_id'];
                         if ($field_type == 'data') {
                             if ($linked) {
                                 $entry_id = $frmdb->get_var($frmdb->entry_metas, array('meta_value' => $data[$key], 'field_id' => $linked), 'item_id');
                             } else {
                                 //get entry id of entry with item_key == $data[$key]
                                 $entry_id = $frmdb->get_var($frmdb->entries, array('item_key' => $data[$key]));
                             }
                             if ($entry_id) {
                                 $values['item_meta'][$field_id] = $entry_id;
                             }
                         }
                         unset($field_type);
                         unset($linked);
                     } else {
                         $values[$field_id] = $data[$key];
                     }
                 }
             }
             if (!isset($values['item_key']) or empty($values['item_key'])) {
                 $values['item_key'] = $data[$entry_key];
             }
             $offset = get_option('gmt_offset') * 60 * 60;
             foreach (array('created_at', 'updated_at') as $stamp) {
                 if (!isset($values[$stamp])) {
                     continue;
                 }
                 // adjust the date format if it starts with the day
                 if (!preg_match('/^\\d{4}-\\d{2}-\\d{2}/', trim($values[$stamp])) && substr($frmpro_settings->date_format, 0, 1) == 'd') {
                     $reg_ex = str_replace(array('/', '.', '-', 'd', 'j', 'm', 'y', 'Y'), array('\\/', '\\.', '\\-', '\\d{2}', '\\d', '\\d{2}', '\\d{2}', '\\d{4}'), $frmpro_settings->date_format);
                     if (preg_match('/^' . $reg_ex . '/', trim($values[$stamp]))) {
                         $values[$stamp] = FrmProAppHelper::convert_date($values[$stamp], $frmpro_settings->date_format, 'Y-m-d H:i:s');
                     }
                 }
                 $values[$stamp] = date('Y-m-d H:i:s', strtotime($values[$stamp]) - $offset);
                 unset($stamp);
             }
             unset($offset);
             if (isset($values['user_id'])) {
                 $values['user_id'] = FrmProAppHelper::get_user_id_param($values['user_id']);
             }
             if (isset($values['updated_by'])) {
                 $values['updated_by'] = FrmProAppHelper::get_user_id_param($values['updated_by']);
             }
             if (isset($values['is_draft'])) {
                 $values['is_draft'] = (int) $values['is_draft'];
             }
             $editing = false;
             if (isset($values['id']) && $values['item_key']) {
                 //check for updating by entry ID
                 $editing = $wpdb->get_var($wpdb->prepare("SELECT id FROM {$wpdb->prefix}frm_items WHERE form_id=%d AND id=%d", $values['form_id'], $values['id']));
             }
             if ($editing) {
                 $created = $frm_entry->update($values['id'], $values);
             } else {
                 $created = $frm_entry->create($values);
             }
             unset($_POST);
             unset($values);
             unset($created);
             if ($row - $start_row >= $max) {
                 fclose($f);
                 return $row;
             }
         }
         fclose($f);
         return $row;
     }
 }
 static function get_graph_values($field, $args)
 {
     global $frm_entry_meta, $frm_field, $frmdb, $wpdb;
     $values = $labels = $f_values = $f_labels = $rows = $cols = array();
     $pie = false;
     extract($args);
     $user_id = (int) $user_id;
     $show_key = (int) $show_key;
     if ($show_key and $show_key < 5) {
         $show_key = 10;
     }
     $options = array('width' => $width, 'height' => $height, 'legend' => 'none', 'title' => '', 'titleTextStyle' => '');
     if (!empty($colors)) {
         $options['colors'] = $colors;
     }
     if (!empty($title)) {
         $options['title'] = $title;
     } else {
         $options['title'] = preg_replace("/&#?[a-z0-9]{2,8};/i", "", FrmAppHelper::truncate($field->name, $truncate, 0));
     }
     if (!empty($title_size) or !empty($title_font)) {
         $options['titleTextStyle'] = array('fontSize' => $title_size, 'fontName' => $title_font);
     }
     if ($show_key) {
         $options['legend'] = array('position' => 'right', 'textStyle' => array('fontSize' => $show_key));
     }
     $fields = $f_inputs = array();
     $fields[$field->id] = $field;
     if ($ids) {
         $ids = explode(',', $ids);
         foreach ($ids as $id_key => $f) {
             $ids[$id_key] = $f = trim($f);
             if (!$f or empty($f)) {
                 unset($ids[$id_key]);
                 continue;
             }
             if ($add_field = $frm_field->getOne($f)) {
                 $fields[$add_field->id] = $add_field;
                 $ids[$id_key] = $add_field->id;
             }
             unset($f);
             unset($id_key);
         }
     } else {
         $ids = array();
     }
     $cols['xaxis'] = array('type' => 'string');
     if ($atts) {
         //Sets up variables for get_field_matches function
         global $frm_post_ids;
         $entry_ids = array();
         $after_where = '';
         if (isset($frm_post_ids[$field->id])) {
             $form_posts = $frm_post_ids[$field->id];
         } else {
             $where_post = array('form_id' => $field->form_id, 'post_id >' => 1, 'is_draft' => 0);
             if ($user_id) {
                 $where_post['user_id'] = $user_id;
             }
             $form_posts = $frmdb->get_records($frmdb->entries, $where_post, '', '', 'id,post_id');
         }
         //Returns the entry IDs for fields being used to filter graph data
         foreach ($atts as $orig_f => $val) {
             $entry_ids = FrmProFieldsHelper::get_field_matches(array('entry_ids' => $entry_ids, 'orig_f' => $orig_f, 'val' => $val, 'id' => $field->id, 'atts' => $atts, 'field' => $field, 'form_posts' => $form_posts, 'after_where' => $after_where, 'drafts' => false));
             $after_where = true;
         }
     }
     //If start date is set, prepare the start date
     if ($start_date) {
         $start_date = $wpdb->prepare('%s', date('Y-m-d', strtotime($start_date)));
     }
     //If end date is set, prepare the end date
     if ($end_date) {
         $end_date = $wpdb->prepare('%s', date('Y-m-d', strtotime($end_date)));
     }
     if ($x_axis) {
         if (is_numeric($x_axis)) {
             $x_field = $frm_field->getOne($x_axis);
         } else {
             $x_field = false;
         }
         $query = $x_query = "SELECT em.meta_value, em.item_id FROM {$frmdb->entry_metas} em";
         if (!$x_field) {
             $x_query = "SELECT id, {$x_axis} FROM {$frmdb->entries} e";
         } else {
             $x_query .= " LEFT JOIN {$frmdb->entries} e ON (e.id=em.item_id)";
         }
         $query .= " LEFT JOIN {$frmdb->entries} e ON (e.id=em.item_id)";
         if ($x_field) {
             if (isset($allowed_col_types)) {
                 $cols['xaxis'] = array('type' => in_array($x_field->type, $allowed_col_types) ? $x_field->type : 'string', 'id' => $x_field->id);
             }
             $options['hAxis'] = array('title' => $x_field->name);
             $x_query .= " WHERE em.field_id='{$x_field->id}'";
             if (!empty($x_start)) {
                 if ($x_field->type == 'date') {
                     $x_start = date('Y-m-d', strtotime($x_start));
                 }
                 $x_query .= " and meta_value >= '{$x_start}'";
             }
             if (!empty($x_end)) {
                 if ($x_field->type == 'date') {
                     $x_end = date('Y-m-d', strtotime($x_end));
                 }
                 $x_query .= " and meta_value <= '{$x_end}'";
             }
         } else {
             $cols['xaxis'] = array('type' => 'string');
             $x_query .= " WHERE form_id=" . $field->form_id;
             if (!empty($x_start)) {
                 if (in_array($x_axis, array('created_at', 'updated_at'))) {
                     $x_start = date('Y-m-d', strtotime($x_start));
                 }
                 $x_query .= " and e.{$x_axis} >= '{$x_start}'";
             }
             if (!empty($x_end)) {
                 if (in_array($x_axis, array('created_at', 'updated_at'))) {
                     $x_end = date('Y-m-d', strtotime($x_end)) . ' 23:59:59';
                 }
                 $x_query .= " and e.{$x_axis} <= '{$x_end}'";
             }
         }
         $q = array();
         foreach ($fields as $f_id => $f) {
             if ($f_id != $field->id) {
                 $q[$f_id] = $query . " WHERE em.field_id=" . (int) $f_id . ($user_id ? " AND user_id='{$user_id}'" : '');
             }
             unset($f);
             unset($f_id);
         }
         $query .= " WHERE em.field_id=" . (int) $field->id;
         if ($user_id) {
             //If user_id parameter is set
             $query .= $wpdb->prepare(" AND user_id=%d", $user_id);
             $x_query .= $wpdb->prepare(" AND user_id=%d", $user_id);
         }
         if ($entry) {
             //If entry_id parameter is set
             $query .= " AND em.item_id in ({$entry})";
             $x_query .= " AND e.id in ({$entry})";
         }
         if ($start_date) {
             //If start_date is set
             $query .= " AND e.created_at >= {$start_date}";
             $x_query .= " AND e.created_at >= {$start_date}";
         }
         if ($end_date) {
             //If end_date is set
             $query .= " AND e.created_at <= {$end_date}";
             $x_query .= " AND e.created_at <= {$end_date}";
         }
         $query .= " AND is_draft=0";
         $x_query .= " AND is_draft=0";
         //Set up $x_query for data from entries fields.
         if ($x_field && $x_field->type == 'data') {
             $linked_field = $x_field->field_options['form_select'];
             $x_query = str_replace('SELECT em.meta_value, em.item_id', 'SELECT dfe.meta_value, em.item_id', $x_query);
             $x_query = str_replace($wpdb->prefix . 'frm_item_metas em', $wpdb->prefix . 'frm_item_metas dfe, ' . $wpdb->prefix . 'frm_item_metas em', $x_query);
             $x_query = str_replace('WHERE', 'WHERE dfe.item_id=em.meta_value AND dfe.field_id=' . $linked_field . ' AND', $x_query);
         }
         //If filtering by another field ID
         if ($atts) {
             //If there are matching entry IDs, add entry IDs onto query
             if (!empty($entry_ids)) {
                 //if field is a post field, retrieve inputs differently
                 if (isset($field->field_options['post_field']) and $field->field_options['post_field'] != '') {
                     foreach ($form_posts as $form_post) {
                         if (in_array($form_post->id, $entry_ids)) {
                             $query = $wpdb->get_var($wpdb->prepare("SELECT " . $field->field_options['post_field'] . " FROM {$wpdb->posts} WHERE ID = %d", $form_post->post_id));
                             $temp_inputs[] = array('meta_value' => $query, 'item_id' => $form_post->id);
                         }
                         unset($form_post);
                     }
                     unset($form_posts);
                     $skip_posts_code = true;
                 } else {
                     $query .= " AND em.item_id in (" . implode(',', $entry_ids) . ")";
                 }
                 $x_query .= " AND e.id in (" . implode(',', $entry_ids) . ")";
             } else {
                 //If entry ids is blank
                 $query = false;
                 $x_query = false;
             }
         }
         if ($query) {
             $query = apply_filters('frm_graph_query', $query, $field, $args);
         }
         if ($x_query) {
             $x_query = apply_filters('frm_graph_xquery', $x_query, $field, $args);
         }
         if (isset($temp_inputs)) {
             $inputs = $temp_inputs;
         } else {
             if ($query) {
                 $inputs = $wpdb->get_results($query, ARRAY_A);
             } else {
                 $inputs = false;
             }
         }
         if ($x_query) {
             $x_inputs = $wpdb->get_results($x_query, ARRAY_A);
         } else {
             $x_inputs = false;
         }
         if (!$x_inputs) {
             return;
         }
         unset($query);
         unset($x_query);
         unset($temp_inputs);
         //If there are multiple fields being graphed
         foreach ($q as $f_id => $query) {
             if (!empty($entry_ids)) {
                 //If a filter is set, only get filtered data from additional fields
                 $query .= " AND item_id in (" . implode(',', $entry_ids) . ")";
             } else {
                 if ($entry) {
                     //If entry_id parameter is set, only get data for this entry
                     $query .= " AND item_id in ({$entry})";
                 }
             }
             if ($start_date) {
                 $query .= " AND e.created_at >= {$start_date}";
             }
             if ($end_date) {
                 $query .= " AND e.created_at <= {$end_date}";
             }
             $f_inputs[$f_id] = $wpdb->get_results($query, ARRAY_A);
             unset($query);
         }
         unset($q);
         //If filtering by another field ID
     } else {
         if ($atts) {
             //If there are matching entry IDs, declare inputs
             if (!empty($entry_ids)) {
                 //If field is a post field, retrieve inputs in a different way - TODO: change database call for post fields so filters like entry ID, start_date, user ID, etc. are included
                 if (isset($field->field_options['post_field']) and $field->field_options['post_field'] != '') {
                     foreach ($form_posts as $form_post) {
                         if (in_array($form_post->id, $entry_ids)) {
                             $post_ids[$form_post->id] = $form_post->post_id;
                         }
                         unset($form_post);
                     }
                     unset($form_posts);
                     if (!empty($post_ids)) {
                         $inputs = $wpdb->get_col("SELECT {$field->field_options['post_field']} FROM {$wpdb->posts} WHERE ID in (" . implode(',', $post_ids) . ")");
                     }
                     $skip_posts_code = true;
                 } else {
                     $inputs = $wpdb->get_col("SELECT meta_value FROM {$frmdb->entry_metas} em LEFT JOIN {$frmdb->entries} e ON (e.id=em.item_id) WHERE is_draft=0 AND em.field_id='" . (int) $field->id . "' AND item_id in (" . implode(',', $entry_ids) . ")" . ($user_id ? " AND user_id=" . (int) $user_id : '') . ($start_date ? " AND e.created_at >= {$start_date}" : '') . ($end_date ? " AND e.created_at <= {$end_date}" : ''));
                 }
                 //If there are multiple fields being graphed
                 foreach ($fields as $f_id => $f) {
                     if ($f_id != $field->id) {
                         $f_inputs[$f_id] = $wpdb->get_col("SELECT meta_value FROM {$frmdb->entry_metas} em LEFT JOIN {$frmdb->entries} e ON (e.id=em.item_id) WHERE is_draft=0 AND em.field_id='{$f_id}' AND item_id in (" . implode(',', $entry_ids) . ")" . ($user_id ? " AND user_id=" . (int) $user_id : '') . ($start_date ? " AND e.created_at >= {$start_date}" : '') . ($end_date ? " AND e.created_at <= {$end_date}" : ''));
                     }
                     unset($f_id);
                     unset($f);
                 }
             }
         } else {
             if ($user_id) {
                 //If UserID is the only filtering parameter defined
                 $inputs = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM {$wpdb->prefix}frm_item_metas em INNER JOIN {$wpdb->prefix}frm_items e ON (e.id=em.item_id) WHERE is_draft=%d AND em.field_id=%d AND user_id=%d" . ($start_date ? " AND e.created_at >= {$start_date}" : '') . ($end_date ? " AND e.created_at <= {$end_date}" : ''), 0, $field->id, $user_id));
             } else {
                 if ($entry) {
                     //If entry ID is the only filtering parameter defined
                     $inputs = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM {$wpdb->prefix}frm_item_metas em INNER JOIN {$wpdb->prefix}frm_items e ON (e.id=em.item_id) WHERE is_draft=%d AND em.field_id=%d AND em.item_id in ({$entry})", 0, $field->id));
                 } else {
                     //If no user ID, atts, or x_axis defined
                     $inputs = $wpdb->get_col($wpdb->prepare("SELECT em.meta_value FROM {$wpdb->prefix}frm_item_metas em INNER JOIN {$wpdb->prefix}frm_items e ON (e.id=em.item_id) WHERE em.field_id=%d AND e.is_draft=%d" . ($start_date ? " AND e.created_at >= {$start_date}" : '') . ($end_date ? " AND e.created_at <= {$end_date}" : ''), $field->id, 0));
                 }
             }
             foreach ($fields as $f_id => $f) {
                 //Get the meta_values for additional fields being graphed
                 if ($f_id != $field->id) {
                     $f_where = array(0, $f_id);
                     if ($user_id) {
                         $f_where[] = $user_id;
                     }
                     $f_inputs[$f_id] = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM {$wpdb->prefix}frm_item_metas em LEFT JOIN {$wpdb->prefix}frm_items e ON (e.id=em.item_id) WHERE is_draft=%d AND em.field_id=%d" . ($user_id ? " AND user_id=%d" : '') . ($entry ? " AND item_id in ({$entry})" : ''), $f_where));
                     unset($f_where);
                 }
                 unset($f_id);
                 unset($f);
             }
         }
     }
     if (isset($inputs) && !empty($inputs)) {
         //Break out any inner arrays (for checkbox or multi-select fields) and add them to the end of the $inputs array
         if (!$x_axis && ($field->type == 'data' && ($field->field_options['data_type'] == 'checkbox' || $field->field_options['data_type'] == 'select' && $field->field_options['multiple'] == 1) || $field->type == 'checkbox' || $field->type == 'select' && $field->field_options['multiple'] == 1)) {
             foreach ($inputs as $k => $i) {
                 $i = maybe_unserialize($i);
                 if (!is_array($i)) {
                     unset($k, $i);
                     continue;
                 }
                 unset($inputs[$k]);
                 foreach ($i as $item) {
                     $inputs[] = $item;
                     unset($item);
                 }
                 unset($k, $i);
             }
             $inputs = array_values($inputs);
         }
         //Strip slashes from inputs
         $inputs = stripslashes_deep($inputs);
     }
     if (isset($form_posts)) {
         unset($form_posts);
     }
     foreach ($f_inputs as $f_id => $f) {
         $f = array_map('maybe_unserialize', $f);
         $f_inputs[$f_id] = stripslashes_deep($f);
         unset($f_id);
         unset($f);
     }
     if (isset($allowed_col_types)) {
         //add columns for each field
         $count = 0;
         foreach ($fields as $f_id => $f) {
             if (isset($tooltip_label[$count]) && !empty($tooltip_label[$count])) {
                 //If tooltip label is set, change the tooltip label
                 $cols[$tooltip_label[$count]] = array('type' => in_array($f->type, $allowed_col_types) ? $f->type : 'number', 'id' => $f->id);
                 $count++;
             } else {
                 //if tooltip label is not set, use the field name
                 $cols[$f->name] = array('type' => in_array($f->type, $allowed_col_types) ? $f->type : 'number', 'id' => $f->id);
             }
             unset($f);
             unset($f_id);
         }
         unset($allowed_col_types);
         unset($count);
     }
     //Declare $field_options variable.
     $field_options = $field->options;
     //Declare $form_posts. This will return empty if the form does not create posts.
     $query = "SELECT id, post_id FROM {$wpdb->prefix}frm_items WHERE form_id = %d AND post_id >= %d" . ($user_id ? " AND user_id=%d" : '') . ($entry ? " AND id in ({$entry})" : '');
     $temp_values = array_filter(array($field->form_id, 1, $user_id));
     $form_posts = $wpdb->get_results($wpdb->prepare($query, $temp_values));
     unset($query);
     unset($temp_values);
     //If form creates posts and graph field is a post field or custom field
     if (!empty($form_posts)) {
         if (isset($field->field_options['post_field']) and $field->field_options['post_field'] != '') {
             if ($field->field_options['post_field'] == 'post_category') {
                 $field_options = FrmProFieldsHelper::get_category_options($field);
             } else {
                 if ($field->field_options['post_field'] == 'post_custom' and $field->field_options['custom_field'] != '') {
                     //check custom fields
                     foreach ($form_posts as $form_post) {
                         $meta_value = get_post_meta($form_post->post_id, $field->field_options['custom_field'], true);
                         if ($meta_value) {
                             if ($x_axis) {
                                 $inputs[] = array('meta_value' => $meta_value, 'item_id' => $form_post->id);
                             } else {
                                 $inputs[] = $meta_value;
                             }
                         }
                     }
                 } else {
                     //if field is post field
                     if ($field->field_options['post_field'] == 'post_status') {
                         $field_options = FrmProFieldsHelper::get_status_options($field);
                     }
                     if (!isset($skip_posts_code)) {
                         foreach ($form_posts as $form_post) {
                             $post_value = $wpdb->get_var("SELECT " . $field->field_options['post_field'] . " FROM {$wpdb->posts} WHERE ID=" . $form_post->post_id);
                             if ($post_value) {
                                 if ($x_axis) {
                                     $inputs[] = array('meta_value' => $post_value, 'item_id' => $form_post->id);
                                 } else {
                                     $inputs[] = $post_value;
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     if ($field->type == 'data') {
         foreach ($inputs as $k => $i) {
             if (is_numeric($i)) {
                 if (is_array($inputs[$k]) and isset($inputs[$k]['meta_value'])) {
                     $inputs[$k]['meta_value'] = FrmProFieldsHelper::get_data_value($inputs[$k]['meta_value'], $field, array('truncate' => 'truncate_label'));
                 } else {
                     $inputs[$k] = FrmProFieldsHelper::get_data_value($inputs[$k], $field, array('truncate' => 'truncate_label'));
                 }
             }
             unset($k);
             unset($i);
         }
     }
     if (isset($x_inputs) and $x_inputs) {
         $x_temp = array();
         foreach ($x_inputs as $x_input) {
             if (!$x_input) {
                 continue;
             }
             if ($x_field) {
                 $x_temp[$x_input['item_id']] = $x_input['meta_value'];
             } else {
                 $x_temp[$x_input['id']] = $x_input[$x_axis];
             }
         }
         $x_inputs = apply_filters('frm_graph_value', $x_temp, $x_field ? $x_field : $x_axis, $args);
         unset($x_temp);
         unset($x_input);
     }
     if ($x_axis and $inputs) {
         $y_temp = array();
         foreach ($inputs as $input) {
             $y_temp[$input['item_id']] = $input['meta_value'];
         }
         foreach ($ids as $f_id) {
             if (!isset($f_values[$f_id])) {
                 $f_values[$f_id] = array();
             }
             $f_values[$f_id][key($y_temp)] = 0;
             unset($f_id);
         }
         $inputs = $y_temp;
         unset($y_temp);
         unset($input);
     }
     //there is no data, so don't graph
     if (!isset($inputs) || empty($inputs)) {
         //TODO: consolidate?
         return array();
     }
     $inputs = apply_filters('frm_graph_value', $inputs, $field, $args);
     foreach ($f_inputs as $f_id => $f) {
         $temp = array();
         foreach ($f as $input) {
             if (is_array($input)) {
                 $temp[$input['item_id']] = $input['meta_value'];
                 foreach ($ids as $d) {
                     if (!isset($f_values[$d][$input['item_id']])) {
                         $f_values[$d][$input['item_id']] = 0;
                     }
                     unset($d);
                 }
             } else {
                 $temp[] = $input;
             }
             unset($input);
         }
         $f_inputs[$f_id] = apply_filters('frm_graph_value', $temp, $fields[$f_id], $args);
         unset($temp);
         unset($input);
         unset($f);
     }
     $tooltips = array();
     if (in_array($field->type, array('select', 'checkbox', 'radio', 'scale')) and (!isset($x_inputs) or !$x_inputs)) {
         if ($limit == '') {
             $limit = 10;
         }
         if (!in_array($field->type, array('checkbox', 'scale'))) {
             //and count($field_options) == 2
             $pie = true;
         }
         //Set default type to pie if field is radio or select
         $field_opt_count = count($field_options);
         if ($field_options) {
             foreach ($field_options as $opt_key => $opt) {
                 $field_val = apply_filters('frm_field_value_saved', $opt, $opt_key, $field->field_options);
                 $opt = apply_filters('frm_field_label_seen', $opt, $opt_key, $field);
                 $count = 0;
                 if (empty($opt)) {
                     continue;
                 }
                 foreach ($inputs as $in) {
                     if (FrmAppHelper::check_selected($in, $field_val)) {
                         if ($data_type == 'total') {
                             $count += $field_val;
                         } else {
                             $count++;
                         }
                     }
                 }
                 $new_val = FrmAppHelper::truncate($opt, $pie && ($type == 'default' || $type == 'pie') ? 50 : $truncate_label, 2);
                 //Prevent label truncation if type is pie
                 if ($count > 0 or $field_opt_count < $limit or !$count and $include_zero) {
                     $labels[$opt] = $new_val;
                     $values[$opt] = $count;
                     $tooltips[$opt] = $opt;
                 }
                 unset($count);
                 foreach ($f_inputs as $f_id => $f) {
                     foreach ($f as $in) {
                         if (!isset($f_values[$f_id])) {
                             $f_values[$f_id] = array();
                         }
                         if (!isset($f_values[$f_id][$new_val])) {
                             $f_values[$f_id][$new_val] = 0;
                         }
                         if (FrmAppHelper::check_selected($in, $field_val)) {
                             if ($data_type == 'total') {
                                 $f_values[$f_id][$new_val] += $field_val;
                             } else {
                                 $f_values[$f_id][$new_val]++;
                             }
                         }
                         unset($in);
                     }
                     unset($f_id);
                     unset($f);
                 }
             }
             if ($limit != 10 and count($values) > $limit) {
                 $ordered_vals = $values;
                 arsort($ordered_vals);
                 $l_count = 0;
                 foreach ($ordered_vals as $vkey => $v) {
                     $l_count++;
                     if ($l_count > $limit) {
                         unset($values[$vkey]);
                         unset($labels[$vkey]);
                         if (isset($tooltips[$vkey])) {
                             unset($tooltips[$vkey]);
                         }
                     }
                     unset($vkey);
                     unset($v);
                 }
                 unset($l_count);
                 unset($ordered_vals);
             }
         }
     } else {
         if ($field->type == 'user_id') {
             $form = $frmdb->get_one_record($wpdb->prefix . 'frm_forms', array('id' => $field->form_id));
             $form_options = maybe_unserialize($form->options);
             $id_count = array_count_values($inputs);
             if ($form->editable and (isset($form_options['single_entry']) and $form_options['single_entry'] and isset($form_options['single_entry_type']) and $form_options['single_entry_type'] == 'user')) {
                 //if only one response per user, do a pie chart of users who have submitted the form
                 $users_of_blog = get_users();
                 $total_users = count($users_of_blog);
                 unset($users_of_blog);
                 $id_count = count($id_count);
                 $not_completed = (int) $total_users - (int) $id_count;
                 $labels = array(__('Completed', 'formidable'), __('Not Completed', 'formidable'));
                 $values = array($id_count, $not_completed);
                 $pie = true;
             } else {
                 //arsort($id_count);
                 foreach ($id_count as $val => $count) {
                     $user_info = get_userdata($val);
                     $labels[] = $user_info ? $user_info->display_name : __('Deleted User', 'formidable');
                     $values[] = $count;
                 }
                 if (count($labels) < 10) {
                     $pie = true;
                 }
             }
         } else {
             if (isset($x_inputs) and $x_inputs) {
                 $calc_array = array();
                 foreach ($inputs as $entry_id => $in) {
                     $entry_id = (int) $entry_id;
                     if (!isset($values[$entry_id])) {
                         $values[$entry_id] = 0;
                     }
                     $labels[$entry_id] = isset($x_inputs[$entry_id]) ? $x_inputs[$entry_id] : '';
                     if (!isset($calc_array[$entry_id])) {
                         $calc_array[$entry_id] = array('count' => 0);
                     }
                     if ($data_type == 'total' or $data_type == 'average') {
                         $values[$entry_id] += (double) $in;
                         $calc_array[$entry_id]['total'] = $values[$entry_id];
                         $calc_array[$entry_id]['count']++;
                     } else {
                         $values[$entry_id]++;
                     }
                     unset($entry_id);
                     unset($in);
                 }
                 if ($data_type == 'average') {
                     foreach ($calc_array as $entry_id => $calc) {
                         $values[$entry_id] = $calc['total'] / $calc['count'];
                         unset($entry_id);
                         unset($calc);
                     }
                 }
                 $calc_array = array();
                 foreach ($f_inputs as $f_id => $f) {
                     if (!isset($calc_array[$f_id])) {
                         $calc_array[$f_id] = array();
                     }
                     foreach ($f as $entry_id => $in) {
                         $entry_id = (int) $entry_id;
                         if (!isset($labels[$entry_id])) {
                             $labels[$entry_id] = isset($x_inputs[$entry_id]) ? $x_inputs[$entry_id] : '';
                             $values[$entry_id] = 0;
                         }
                         if (!isset($calc_array[$f_id][$entry_id])) {
                             $calc_array[$f_id][$entry_id] = array('count' => 0);
                         }
                         if (!isset($f_values[$f_id][$entry_id])) {
                             $f_values[$f_id][$entry_id] = 0;
                         }
                         if ($data_type == 'total' or $data_type == 'average') {
                             $f_values[$f_id][$entry_id] += (double) $in;
                             $calc_array[$f_id][$entry_id]['total'] = $f_values[$f_id][$entry_id];
                             $calc_array[$f_id][$entry_id]['count']++;
                         } else {
                             $f_values[$f_id][$entry_id]++;
                         }
                         unset($entry_id);
                         unset($in);
                     }
                     unset($f_id);
                     unset($f);
                 }
                 if ($data_type == 'average') {
                     foreach ($calc_array as $f_id => $calc) {
                         foreach ($calc as $entry_id => $c) {
                             $f_values[$f_id][$entry_id] = $c['total'] / $c['count'];
                             unset($entry_id);
                             unset($c);
                         }
                         unset($calc);
                         unset($f_id);
                     }
                 }
                 unset($calc_array);
             } else {
                 $id_count = array_count_values(array_map('strtolower', $inputs));
                 arsort($id_count);
                 $i = 0;
                 foreach ($id_count as $val => $count) {
                     if ($i < $response_count) {
                         if ($field->type == 'user_id') {
                             $user_info = get_userdata($val);
                             $new_val = $user_info->display_name;
                         } else {
                             $new_val = ucwords($val);
                         }
                         $labels[$new_val] = $new_val;
                         $values[$new_val] = $count;
                     }
                     $i++;
                 }
                 foreach ($f_inputs as $f_id => $f) {
                     $id_count = array_count_values(array_map('strtolower', $f));
                     arsort($id_count);
                     $i = 0;
                     foreach ($id_count as $val => $count) {
                         if ($i < $response_count) {
                             if ($field->type == 'user_id') {
                                 $user_info = get_userdata($val);
                                 $new_val = $user_info->display_name;
                             } else {
                                 $new_val = ucwords($val);
                             }
                             $position = array_search($new_val, $labels);
                             if (!$position) {
                                 end($labels);
                                 $position = key($labels);
                                 $labels[$new_val] = $new_val;
                                 $values[$new_val] = 0;
                             }
                             $f_values[$f_id][$new_val] = $count;
                         }
                         $i++;
                     }
                     unset($f_id);
                     unset($f);
                 }
             }
         }
     }
     if (isset($x_inputs) and $x_inputs) {
         $used_vals = $calc_array = array();
         foreach ($labels as $l_key => $label) {
             if (empty($label) and (!empty($x_start) or !empty($x_end))) {
                 unset($values[$l_key]);
                 unset($labels[$l_key]);
                 if (isset($tooltips[$l_key])) {
                     unset($tooltips[$l_key]);
                 }
                 continue;
             }
             if (in_array($x_axis, array('created_at', 'updated_at'))) {
                 if ($type == 'pie') {
                     $labels[$l_key] = $label = $inputs[$l_key];
                 } else {
                     $labels[$l_key] = $label = date('Y-m-d', strtotime($label));
                 }
             }
             if (isset($used_vals[$label])) {
                 $values[$l_key] += $values[$used_vals[$label]];
                 unset($values[$used_vals[$label]]);
                 foreach ($ids as $f_id) {
                     if (!isset($f_values[$f_id][$l_key])) {
                         $f_values[$f_id][$l_key] = 0;
                     }
                     if (!isset($f_values[$f_id][$used_vals[$label]])) {
                         $f_values[$f_id][$used_vals[$label]] = 0;
                     }
                     $f_values[$f_id][$l_key] += $f_values[$f_id][$used_vals[$label]];
                     unset($f_values[$f_id][$used_vals[$label]]);
                     unset($f_id);
                 }
                 unset($labels[$used_vals[$label]]);
             }
             $used_vals[$label] = $l_key;
             if ($data_type == 'average') {
                 if (!isset($calc_array[$label])) {
                     $calc_array[$label] = 0;
                 }
                 $calc_array[$label]++;
             }
             unset($label);
             unset($l_key);
         }
         if (!empty($calc_array)) {
             foreach ($calc_array as $label => $calc) {
                 if (isset($used_vals[$label])) {
                     $values[$used_vals[$label]] = $values[$used_vals[$label]] / $calc;
                     foreach ($ids as $f_id) {
                         $f_values[$f_id][$used_vals[$label]] = $f_values[$f_id][$used_vals[$label]] / $calc;
                         unset($f_id);
                     }
                 }
                 unset($label);
                 unset($calc);
             }
         }
         unset($used_vals);
     }
     $combine_dates = false;
     if (isset($x_field) and $x_field and $x_field->type == 'date' or in_array($x_axis, array('created_at', 'updated_at'))) {
         $combine_dates = apply_filters('frm_combine_dates', true, $x_field);
     }
     if ($combine_dates) {
         if ($include_zero) {
             $start_timestamp = empty($x_start) ? time() : strtotime($x_start);
             $end_timestamp = empty($x_end) ? time() : strtotime($x_end);
             $dates_array = array();
             // Get the dates array
             for ($e = $start_timestamp; $e <= $end_timestamp; $e += 60 * 60 * 24) {
                 $dates_array[] = date('Y-m-d', $e);
             }
             unset($e);
             // Add the zero count days
             foreach ($dates_array as $date_str) {
                 if (!in_array($date_str, $labels)) {
                     $labels[$date_str] = $date_str;
                     $values[$date_str] = 0;
                     foreach ($ids as $f_id) {
                         if (!isset($f_values[$f_id][$date_str])) {
                             $f_values[$f_id][$date_str] = 0;
                         }
                     }
                 }
             }
             unset($dates_array);
             unset($start_timestamp);
             unset($end_timestamp);
         }
         asort($labels);
         global $frmpro_settings;
         foreach ($labels as $l_key => $l) {
             if (isset($x_field) and $x_field and $x_field->type == 'date' or in_array($x_axis, array('created_at', 'updated_at'))) {
                 if ($type != 'pie' and preg_match('/^\\d{4}-\\d{2}-\\d{2}$/', $l)) {
                     global $frmpro_settings;
                     $labels[$l_key] = FrmProAppHelper::convert_date($l, 'Y-m-d', $frmpro_settings->date_format);
                 }
             }
             unset($l_key);
             unset($l);
         }
         $values = FrmProAppHelper::sort_by_array($values, array_keys($labels));
         $tooltips = FrmProAppHelper::sort_by_array($tooltips, array_keys($labels));
         foreach ($ids as $f_id) {
             $f_values[$f_id] = FrmProAppHelper::sort_by_array($f_values[$f_id], array_keys($labels));
             $f_values[$f_id] = FrmProAppHelper::reset_keys($f_values[$f_id]);
             ksort($f_values[$f_id]);
             unset($f_id);
         }
     } else {
         if (isset($x_inputs) and $x_inputs) {
             foreach ($labels as $l_key => $l) {
                 foreach ($ids as $f_id) {
                     //do a last check to make sure all bars/lines have a value for each label
                     if (!isset($f_values[$f_id][$l_key])) {
                         $f_values[$f_id][$l_key] = 0;
                     }
                     unset($fid);
                 }
                 unset($l_key);
                 unset($l);
             }
         }
         foreach ($ids as $f_id) {
             $f_values[$f_id] = FrmProAppHelper::reset_keys($f_values[$f_id]);
             ksort($f_values[$f_id]);
             unset($f_id);
         }
         if ($x_order == '1') {
             ksort($labels);
             ksort($values);
             ksort($tooltips);
         }
     }
     $labels = FrmProAppHelper::reset_keys($labels);
     $values = FrmProAppHelper::reset_keys($values);
     $tooltips = FrmProAppHelper::reset_keys($tooltips);
     //Graph by month or quarter
     if (isset($group_by) && in_array($group_by, array('month', 'quarter'))) {
         foreach ($labels as $key => $label) {
             if ($group_by == 'month') {
                 $labels[$key] = date('F Y', strtotime($label));
             } else {
                 if ($group_by == 'quarter') {
                     $label = date('Y-m-d', strtotime($label));
                     //Convert date to Y-m-d format
                     if (preg_match('/-(01|02|03)-/', $label)) {
                         $labels[$key] = 'Q1 ' . date('Y', strtotime($label));
                     } else {
                         if (preg_match('/-(04|05|06)-/', $label)) {
                             $labels[$key] = 'Q2 ' . date('Y', strtotime($label));
                         } else {
                             if (preg_match('/-(07|08|09)-/', $label)) {
                                 $labels[$key] = 'Q3 ' . date('Y', strtotime($label));
                             } else {
                                 if (preg_match('/-(10|11|12)-/', $label)) {
                                     $labels[$key] = 'Q4 ' . date('Y', strtotime($label));
                                 }
                             }
                         }
                     }
                 }
             }
         }
         $count = count($labels) - 1;
         for ($i = 0; $i < $count; $i++) {
             if ($labels[$i] == $labels[$i + 1]) {
                 unset($labels[$i]);
                 $values[$i + 1] = $values[$i] + $values[$i + 1];
                 unset($values[$i]);
             }
         }
         $labels = FrmProAppHelper::reset_keys($labels);
         $values = FrmProAppHelper::reset_keys($values);
     }
     $labels = apply_filters('frm_graph_labels', $labels, $args, $field);
     $values = apply_filters('frm_final_graph_values', $values, $args, $field);
     $return = array('total_count' => count($inputs), 'f_values' => $f_values, 'labels' => $labels, 'values' => $values, 'pie' => $pie, 'combine_dates' => $combine_dates, 'ids' => $ids, 'cols' => $cols, 'rows' => $rows, 'options' => $options, 'fields' => $fields, 'tooltips' => $tooltips);
     if (isset($x_inputs)) {
         $return['x_inputs'] = $x_inputs;
     }
     return $return;
 }