function gravitywp_count_func($atts, $content = null)
{
    extract(shortcode_atts(array('formid' => 'formid', 'field' => 'field'), $atts));
    $search_criteria = null;
    $sorting = null;
    $paging = array('offset' => 0, 'page_size' => 200);
    $entries = GFAPI::get_entries($formid, $search_criteria, $sorting, $paging);
    $countentries = GFAPI::count_entries($formid);
    $gwp_count = 0;
    for ($row = 0; $row < $countentries; $row++) {
        $gwp_count += $entries[$row][$field];
    }
    return $gwp_count;
}
 /**
  * @since 1.15
  * @covers GravityView_Uninstall::fire_everything()
  */
 function test_fire_everything()
 {
     $create_count = 10;
     $form = $this->factory->form->create_and_get();
     $all_forms = GFAPI::get_forms();
     $views = $this->factory->view->create_many($create_count, array('form_id' => $form['id']));
     $entry_ids = $this->factory->entry->create_many($create_count, array('form_id' => $form['id']));
     $connected = gravityview_get_connected_views($form['id']);
     $entry_count = GFAPI::count_entries($form['id']);
     // Make sure the objects were created and connected
     $this->assertEquals($create_count, count(array_filter($views)));
     $this->assertEquals($create_count, count(array_filter($connected)));
     $this->assertEquals($create_count, count(array_filter($entry_ids)));
     $this->_set_up_expected_options();
     ### DO NOT DELETE WHEN THE USER DOESN'T HAVE THE CAPABILITY
     $user = $this->factory->user->create_and_set(array('user_login' => 'administrator', 'user_pass' => 'administrator', 'role' => 'administrator'));
     $this->assertTrue(GVCommon::has_cap('gravityview_uninstall'));
     ### DO NOT DELETE WHEN IT IS NOT SET OR SET TO FALSE
     // TRY deleting when the settings aren't configured.
     $this->_set_up_gravityview_settings(NULL);
     $this->uninstall();
     $this->_check_deleted_options(false);
     // TRY deleting when the Delete setting is set to No
     $this->_set_up_gravityview_settings('0');
     $this->uninstall();
     $this->_check_deleted_options(false);
     ### REALLY DELETE NOW
     // Create the items
     $this->_set_up_gravityview_settings('delete');
     $this->_set_up_notes($entry_ids);
     $this->_set_up_entry_meta($entry_ids, $form);
     $this->uninstall();
     // No Forms should be deleted
     $this->assertEquals($all_forms, GFAPI::get_forms());
     $this->_check_posts();
     $this->_check_entries($form, $entry_count);
     $this->_check_deleted_options();
     $this->_check_deleted_entry_notes($entry_ids);
     $this->_check_deleted_entry_meta($entry_ids);
 }
예제 #3
0
 public function get_results_data($form, $fields, $search_criteria = array(), $state_array = array(), $max_execution_time = 15)
 {
     // todo: add hooks to modify $max_execution_time and $page_size?
     $page_size = 150;
     $time_start = microtime(true);
     $form_id = $form["id"];
     $data = array();
     $offset = 0;
     $entry_count = 0;
     $field_data = array();
     if ($state_array) {
         //get counts from state
         $data = $state_array;
         $offset = (int) rgar($data, "offset");
         unset($data["offset"]);
         $entry_count = $offset;
         $field_data = rgar($data, "field_data");
     } else {
         //initialize counts
         foreach ($fields as $field) {
             $field_type = GFFormsModel::get_input_type($field);
             if (false === isset($field["choices"])) {
                 $field_data[$field["id"]] = 0;
                 continue;
             }
             $choices = $field["choices"];
             if ($field_type == "likert" && rgar($field, "gsurveyLikertEnableMultipleRows")) {
                 foreach ($field["gsurveyLikertRows"] as $row) {
                     foreach ($choices as $choice) {
                         $field_data[$field["id"]][$row["value"]][$choice['value']] = 0;
                     }
                 }
             } else {
                 foreach ($choices as $choice) {
                     $field_data[$field["id"]][$choice['value']] = 0;
                 }
             }
             if ($field_type == "likert" && rgar($field, "gsurveyLikertEnableScoring")) {
                 $field_data[$field["id"]]["sum_of_scores"] = 0;
             }
         }
     }
     $count_search_leads = GFAPI::count_entries($form_id, $search_criteria);
     $data["entry_count"] = $count_search_leads;
     $entries_left = $count_search_leads - $offset;
     while ($entries_left >= 0) {
         $paging = array('offset' => $offset, 'page_size' => $page_size);
         $search_leads_time_start = microtime(true);
         $leads = GFFormsModel::search_leads($form_id, $search_criteria, null, $paging);
         $search_leads_time_end = microtime(true);
         $search_leads_time = $search_leads_time_end - $search_leads_time_start;
         $leads_in_search = count($leads);
         $entry_count += $leads_in_search;
         foreach ($leads as $lead) {
             foreach ($fields as $field) {
                 $field_type = GFFormsModel::get_input_type($field);
                 $field_id = $field["id"];
                 $value = RGFormsModel::get_lead_field_value($lead, $field);
                 if ($field_type == "likert" && rgar($field, "gsurveyLikertEnableMultipleRows")) {
                     if (empty($value)) {
                         continue;
                     }
                     foreach ($value as $value_vector) {
                         if (empty($value_vector)) {
                             continue;
                         }
                         list($row_val, $col_val) = explode(":", $value_vector, 2);
                         if (isset($field_data[$field["id"]][$row_val]) && isset($field_data[$field["id"]][$row_val][$col_val])) {
                             $field_data[$field["id"]][$row_val][$col_val]++;
                         }
                     }
                 } elseif ($field_type == "rank") {
                     $score = count(rgar($field, "choices"));
                     $values = explode(",", $value);
                     foreach ($values as $ranked_value) {
                         $field_data[$field["id"]][$ranked_value] += $score;
                         $score--;
                     }
                 } else {
                     if (false === isset($field["choices"])) {
                         if (false === empty($value)) {
                             $field_data[$field_id]++;
                         }
                         continue;
                     }
                     $choices = $field["choices"];
                     foreach ($choices as $choice) {
                         $choice_is_selected = false;
                         if (is_array($value)) {
                             $choice_value = rgar($choice, "value");
                             if (in_array($choice_value, $value)) {
                                 $choice_is_selected = true;
                             }
                         } else {
                             if (RGFormsModel::choice_value_match($field, $choice, $value)) {
                                 $choice_is_selected = true;
                             }
                         }
                         if ($choice_is_selected) {
                             $field_data[$field_id][$choice['value']]++;
                         }
                     }
                 }
                 if ($field_type == "likert" && rgar($field, "gsurveyLikertEnableScoring")) {
                     $field_data[$field["id"]]["sum_of_scores"] += $this->get_likert_score($field, $lead);
                 }
             }
         }
         $data["field_data"] = $field_data;
         if (isset($this->_callbacks["calculation"])) {
             $data = call_user_func($this->_callbacks["calculation"], $data, $form, $fields, $leads);
             $field_data = $data["field_data"];
         }
         $offset += $page_size;
         $entries_left -= $page_size;
         $time_end = microtime(true);
         $execution_time = $time_end - $time_start;
         if ($entries_left > 0 && $execution_time + $search_leads_time > $max_execution_time) {
             $data["status"] = "incomplete";
             $data["offset"] = $offset;
             $progress = $data["entry_count"] > 0 ? round($data["offset"] / $data["entry_count"] * 100) : 0;
             $data["progress"] = $progress;
             break;
         }
         if ($entries_left <= 0) {
             $data["status"] = "complete";
         }
     }
     $data["timestamp"] = time();
     return $data;
 }
예제 #4
0
파일: webapi.php 프로젝트: tlandn/akvo-web
 public function get_entries($entry_ids, $form_ids = null, $schema = "", $field_ids = array())
 {
     $this->authorize("gravityforms_view_entries");
     $status = 200;
     $response = array();
     $result = array();
     if ($entry_ids) {
         if (is_array($entry_ids)) {
             foreach ($entry_ids as $entry_id) {
                 $result = GFAPI::get_entry($entry_id);
                 if (!is_wp_error($result)) {
                     $response[$entry_id] = $result;
                     if (!empty($field_ids) && !empty($response[$entry_id])) {
                         $response[$entry_id] = $this->filter_entry_object($response[$entry_id], $field_ids);
                     }
                 }
             }
         } else {
             $result = GFAPI::get_entry($entry_ids);
             if (!is_wp_error($result)) {
                 $response = $result;
                 if (!empty($field_ids) && !empty($response)) {
                     $response = $this->filter_entry_object($response, $field_ids);
                 }
             }
         }
         if ($schema == "mtd") {
             $response = self::mtd_transform_entry_data($response);
         }
     } else {
         //sorting parameters
         $sort_key = isset($_GET["sorting"]["key"]) && !empty($_GET["sorting"]["key"]) ? $_GET["sorting"]["key"] : "id";
         $sort_dir = isset($_GET["sorting"]["direction"]) && !empty($_GET["sorting"]["direction"]) ? $_GET["sorting"]["direction"] : "DESC";
         $sorting = array('key' => $sort_key, 'direction' => $sort_dir);
         //paging parameters
         $page_size = isset($_GET["paging"]["page_size"]) ? intval($_GET["paging"]["page_size"]) : 10;
         if (isset($_GET["paging"]["current_page"])) {
             $current_page = intval($_GET["paging"]["current_page"]);
             $offset = $page_size * ($current_page - 1);
         } else {
             $offset = isset($_GET["paging"]["offset"]) ? intval($_GET["paging"]["offset"]) : 0;
         }
         $paging = array('offset' => $offset, 'page_size' => $page_size);
         $search = isset($_GET["search"]) ? $_GET["search"] : array();
         if (empty($form_ids)) {
             $form_ids = 0;
         }
         // all forms
         $entry_count = GFAPI::count_entries($form_ids, $search);
         $result = $entry_count > 0 ? GFAPI::get_entries($form_ids, $search, $sorting, $paging) : array();
         if (!is_wp_error($result)) {
             $response = array("total_count" => $entry_count, "entries" => $result);
             if ($schema == "mtd") {
                 $response = $this->mtd_transform_entries_data($response, $form_ids);
             }
         }
     }
     if (is_wp_error($result)) {
         $response = $this->get_error_response($result);
         $status = $this->get_error_status($result);
     }
     $this->end($status, $response);
 }
 public static function start_export($form)
 {
     $form_id = $form['id'];
     $fields = $_POST['export_field'];
     $start_date = empty($_POST['export_date_start']) ? '' : self::get_gmt_date($_POST['export_date_start'] . ' 00:00:00');
     $end_date = empty($_POST['export_date_end']) ? '' : self::get_gmt_date($_POST['export_date_end'] . ' 23:59:59');
     $search_criteria['status'] = 'active';
     $search_criteria['field_filters'] = GFCommon::get_field_filters_from_post($form);
     if (!empty($start_date)) {
         $search_criteria['start_date'] = $start_date;
     }
     if (!empty($end_date)) {
         $search_criteria['end_date'] = $end_date;
     }
     $sorting = array('key' => 'date_created', 'direction' => 'DESC', 'type' => 'info');
     GFCommon::log_debug("GFExport::start_export(): Start date: {$start_date}");
     GFCommon::log_debug("GFExport::start_export(): End date: {$end_date}");
     $form = self::add_default_export_fields($form);
     $entry_count = GFAPI::count_entries($form_id, $search_criteria);
     $page_size = 100;
     $offset = 0;
     //Adding BOM marker for UTF-8
     $lines = chr(239) . chr(187) . chr(191);
     // set the separater
     $separator = apply_filters('gform_export_separator_' . $form_id, apply_filters('gform_export_separator', ',', $form_id), $form_id);
     $field_rows = self::get_field_row_count($form, $fields, $entry_count);
     //writing header
     $headers = array();
     foreach ($fields as $field_id) {
         $field = RGFormsModel::get_field($form, $field_id);
         $value = str_replace('"', '""', GFCommon::get_label($field, $field_id));
         GFCommon::log_debug("GFExport::start_export(): Header for field ID {$field_id}: {$value}");
         $headers[$field_id] = $str = preg_replace('/[^a-z\\d ]/i', '', $value);
         $subrow_count = isset($field_rows[$field_id]) ? intval($field_rows[$field_id]) : 0;
         if ($subrow_count == 0) {
             $lines .= '"' . $value . '"' . $separator;
         } else {
             for ($i = 1; $i <= $subrow_count; $i++) {
                 $lines .= '"' . $value . ' ' . $i . '"' . $separator;
             }
         }
         GFCommon::log_debug("GFExport::start_export(): Lines: {$lines}");
     }
     $lines = substr($lines, 0, strlen($lines) - 1) . "\n";
     //paging through results for memory issues
     while ($entry_count > 0) {
         $paging = array('offset' => $offset, 'page_size' => $page_size);
         $leads = GFAPI::get_entries($form_id, $search_criteria, $sorting, $paging);
         $leads = apply_filters("gform_leads_before_export_{$form_id}", apply_filters('gform_leads_before_export', $leads, $form, $paging), $form, $paging);
         foreach ($leads as $lead) {
             foreach ($fields as $field_id) {
                 switch ($field_id) {
                     case 'date_created':
                         $lead_gmt_time = mysql2date('G', $lead['date_created']);
                         $lead_local_time = GFCommon::get_local_timestamp($lead_gmt_time);
                         $value = date_i18n('Y-m-d H:i:s', $lead_local_time, true);
                         break;
                     default:
                         $long_text = '';
                         if (strlen(rgar($lead, $field_id)) >= GFORMS_MAX_FIELD_LENGTH - 10) {
                             $long_text = RGFormsModel::get_field_value_long($lead, $field_id, $form);
                         }
                         $value = !empty($long_text) ? $long_text : rgar($lead, $field_id);
                         $field = RGFormsModel::get_field($form, $field_id);
                         $input_type = RGFormsModel::get_input_type($field);
                         if ($input_type == 'checkbox') {
                             //pass in label value that has not had quotes escaped so the is_checkbox_checked function compares the unchanged label value with the lead value
                             $header_label_not_escaped = GFCommon::get_label($field, $field_id);
                             $value = GFFormsModel::is_checkbox_checked($field_id, $header_label_not_escaped, $lead, $form);
                             if ($value === false) {
                                 $value = '';
                             }
                         } else {
                             if ($input_type == 'fileupload' && $field->multipleFiles) {
                                 $value = !empty($value) ? implode(' , ', json_decode($value, true)) : '';
                             }
                         }
                         $value = preg_replace('/[^a-z\\d ]/i', '', $value);
                         $value = apply_filters('gform_export_field_value', $value, $form_id, $field_id, $lead);
                         GFCommon::log_debug("GFExport::start_export(): Value for field ID {$field_id}: {$value}");
                         break;
                 }
                 if (isset($field_rows[$field_id])) {
                     $list = empty($value) ? array() : unserialize($value);
                     foreach ($list as $row) {
                         $row_values = array_values($row);
                         $row_str = implode('|', $row_values);
                         $lines .= '"' . str_replace('"', '""', $row_str) . '"' . $separator;
                     }
                     //filling missing subrow columns (if any)
                     $missing_count = intval($field_rows[$field_id]) - count($list);
                     for ($i = 0; $i < $missing_count; $i++) {
                         $lines .= '""' . $separator;
                     }
                 } else {
                     $value = maybe_unserialize($value);
                     if (is_array($value)) {
                         $value = implode('|', $value);
                     }
                     $lines .= '"' . str_replace('"', '""', $value) . '"' . $separator;
                 }
             }
             $lines = substr($lines, 0, strlen($lines) - 1);
             GFCommon::log_debug("GFExport::start_export(): Lines: {$lines}");
             $lines .= "\n";
         }
         $offset += $page_size;
         $entry_count -= $page_size;
         if (!seems_utf8($lines)) {
             $lines = utf8_encode($lines);
         }
         if (function_exists('mb_convert_encoding')) {
             // Convert the contents to UTF-16LE which has wider support than UTF-8.
             // This fixes an issue with special characters in Excel for Mac.
             $lines = mb_convert_encoding($lines, 'UTF-16LE', 'UTF-8');
         }
         echo $lines;
         $lines = '';
     }
 }
예제 #6
0
 /**
  * Retrieve entries given search, sort, paging criteria
  *
  * @see  GFAPI::get_entries()
  * @see GFFormsModel::get_field_filters_where()
  * @access public
  * @param int|array $form_ids The ID of the form or an array IDs of the Forms. Zero for all forms.
  * @param mixed $passed_criteria (default: null)
  * @param mixed &$total Optional. An output parameter containing the total number of entries. Pass a non-null value to generate the total count. (default: null)
  * @return mixed False: Error fetching entries. Array: Multi-dimensional array of Gravity Forms entry arrays
  */
 public static function get_entries($form_ids = null, $passed_criteria = null, &$total = null)
 {
     // Filter the criteria before query (includes Adv Filter)
     $criteria = self::calculate_get_entries_criteria($passed_criteria, $form_ids);
     do_action('gravityview_log_debug', '[gravityview_get_entries] Final Parameters', $criteria);
     // Return value
     $return = null;
     if (!empty($criteria['cache'])) {
         $Cache = new GravityView_Cache($form_ids, $criteria);
         if ($entries = $Cache->get()) {
             // Still update the total count when using cached results
             if (!is_null($total)) {
                 $total = GFAPI::count_entries($form_ids, $criteria['search_criteria']);
             }
             $return = $entries;
         }
     }
     if (is_null($return) && class_exists('GFAPI') && (is_numeric($form_ids) || is_array($form_ids))) {
         $entries = GFAPI::get_entries($form_ids, $criteria['search_criteria'], $criteria['sorting'], $criteria['paging'], $total);
         if (is_wp_error($entries)) {
             do_action('gravityview_log_error', $entries->get_error_message(), $entries);
             return false;
         }
         if (!empty($criteria['cache']) && isset($Cache)) {
             // Cache results
             $Cache->set($entries, 'entries');
         }
         $return = $entries;
     }
     /**
      * @filter `gravityview_entries` Modify the array of entries returned to GravityView after it has been fetched from the cache or from `GFAPI::get_entries()`.
      * @param  array|null $entries Array of entries as returned by the cache or by `GFAPI::get_entries()`
      * @param  array $criteria The final search criteria used to generate the request to `GFAPI::get_entries()`
      * @param array $passed_criteria The original search criteria passed to `GVCommon::get_entries()`
      * @param  int|null $total Optional. An output parameter containing the total number of entries. Pass a non-null value to generate
      */
     $return = apply_filters('gravityview_entries', $return, $criteria, $passed_criteria, $total);
     return $return;
 }
예제 #7
0
 public function get_entries($entry_ids, $form_ids = null, $schema = '', $field_ids = array())
 {
     $this->log_debug(__METHOD__ . '(): Running.');
     $capability = apply_filters('gform_web_api_capability_get_entries', 'gravityforms_view_entries');
     $this->authorize($capability);
     $status = 200;
     $response = array();
     $result = array();
     if ($entry_ids) {
         if (is_array($entry_ids)) {
             foreach ($entry_ids as $entry_id) {
                 $result = GFAPI::get_entry($entry_id);
                 if (!is_wp_error($result)) {
                     $result = $this->maybe_json_encode_list_fields($result);
                     $response[$entry_id] = $result;
                     if (!empty($field_ids) && !empty($response[$entry_id])) {
                         $response[$entry_id] = $this->filter_entry_object($response[$entry_id], $field_ids);
                     }
                 }
             }
         } else {
             $result = GFAPI::get_entry($entry_ids);
             if (!is_wp_error($result)) {
                 $result = $this->maybe_json_encode_list_fields($result);
                 $response = $result;
                 if (!empty($field_ids) && !empty($response)) {
                     $response = $this->filter_entry_object($response, $field_ids);
                 }
             }
         }
         if ($schema == 'mtd') {
             $response = self::mtd_transform_entry_data($response);
         }
     } else {
         //sorting parameters
         $sort_key = isset($_GET['sorting']['key']) && !empty($_GET['sorting']['key']) ? $_GET['sorting']['key'] : 'id';
         $sort_dir = isset($_GET['sorting']['direction']) && !empty($_GET['sorting']['direction']) ? $_GET['sorting']['direction'] : 'DESC';
         $sorting = array('key' => $sort_key, 'direction' => $sort_dir);
         if (isset($_GET['sorting']['is_numeric'])) {
             $sorting['is_numeric'] = $_GET['sorting']['is_numeric'];
         }
         //paging parameters
         $page_size = isset($_GET['paging']['page_size']) ? intval($_GET['paging']['page_size']) : 10;
         if (isset($_GET['paging']['current_page'])) {
             $current_page = intval($_GET['paging']['current_page']);
             $offset = $page_size * ($current_page - 1);
         } else {
             $offset = isset($_GET['paging']['offset']) ? intval($_GET['paging']['offset']) : 0;
         }
         $paging = array('offset' => $offset, 'page_size' => $page_size);
         if (isset($_GET['search'])) {
             $search = $_GET['search'];
             if (!is_array($search)) {
                 $search = urldecode(stripslashes($search));
                 $search = json_decode($search, true);
             }
         } else {
             $search = array();
         }
         if (empty($form_ids)) {
             $form_ids = 0;
         }
         // all forms
         $entry_count = GFAPI::count_entries($form_ids, $search);
         $result = $entry_count > 0 ? GFAPI::get_entries($form_ids, $search, $sorting, $paging) : array();
         if (!is_wp_error($result)) {
             foreach ($result as &$entry) {
                 $entry = $this->maybe_json_encode_list_fields($entry);
             }
             $response = array('total_count' => $entry_count, 'entries' => $result);
             if ($schema == 'mtd') {
                 $response = $this->mtd_transform_entries_data($response, $form_ids);
             }
         }
     }
     if (is_wp_error($result)) {
         $response = $this->get_error_response($result);
         $status = $this->get_error_status($result);
     }
     $this->end($status, $response);
 }
예제 #8
0
function wpcampus_get_vote_on_new_name()
{
    // Set the form ID
    $form_id = 6;
    // Build response
    $response = array();
    // Get form
    if (($form = GFAPI::get_form($form_id)) && ($fields = $form['fields'])) {
        foreach ($fields as $field) {
            // Get our specific field
            if ('Vote on New Name' == $field->adminLabel) {
                foreach ($field->choices as $choice) {
                    // Get the count
                    $search_criteria = array('status' => 'active');
                    $search_criteria['field_filters'][] = array('key' => $field->id, 'operator' => '=', 'value' => $choice['value']);
                    $choice_count = GFAPI::count_entries($form_id, $search_criteria);
                    // Add to response
                    $response[] = array('text' => $choice['text'], 'count' => $choice_count);
                }
                break;
            }
        }
    }
    return $response;
}
 public static function start_export($form)
 {
     $form_id = $form["id"];
     $fields = $_POST["export_field"];
     $start_date = empty($_POST["export_date_start"]) ? "" : self::get_gmt_date($_POST["export_date_start"] . " 00:00:00");
     $end_date = empty($_POST["export_date_end"]) ? "" : self::get_gmt_date($_POST["export_date_end"] . " 23:59:59");
     $search_criteria["status"] = "active";
     $search_criteria["field_filters"] = GFCommon::get_field_filters_from_post();
     if (!empty($start_date)) {
         $search_criteria["start_date"] = $start_date;
     }
     if (!empty($end_date)) {
         $search_criteria["end_date"] = $end_date;
     }
     $sorting = array('key' => "date_created", 'direction' => "DESC", "type" => "info");
     GFCommon::log_debug("start date: {$start_date}");
     GFCommon::log_debug("end date: {$end_date}");
     $form = self::add_default_export_fields($form);
     $entry_count = GFAPI::count_entries($form_id, $search_criteria);
     $page_size = 100;
     $offset = 0;
     //Adding BOM marker for UTF-8
     $lines = chr(239) . chr(187) . chr(191);
     // set the separater
     $separator = apply_filters('gform_export_separator_' . $form_id, apply_filters('gform_export_separator', ',', $form_id), $form_id);
     $field_rows = self::get_field_row_count($form, $fields, $entry_count);
     //writing header
     $headers = array();
     foreach ($fields as $field_id) {
         $field = RGFormsModel::get_field($form, $field_id);
         $value = str_replace('"', '""', GFCommon::get_label($field, $field_id));
         GFCommon::log_debug("Header for field ID {$field_id}: {$value}");
         $headers[$field_id] = $value;
         $subrow_count = isset($field_rows[$field_id]) ? intval($field_rows[$field_id]) : 0;
         if ($subrow_count == 0) {
             $lines .= '"' . $value . '"' . $separator;
         } else {
             for ($i = 1; $i <= $subrow_count; $i++) {
                 $lines .= '"' . $value . " " . $i . '"' . $separator;
             }
         }
         GFCommon::log_debug("Lines: {$lines}");
     }
     $lines = substr($lines, 0, strlen($lines) - 1) . "\n";
     //paging through results for memory issues
     while ($entry_count > 0) {
         //$leads = RGFormsModel::get_leads($form_id,"date_created", "DESC", "", $offset, $page_size, null, null, false, $start_date, $end_date);
         $paging = array('offset' => $offset, 'page_size' => $page_size);
         $leads = GFAPI::get_entries($form_id, $search_criteria, $sorting, $paging);
         $leads = apply_filters("gform_leads_before_export_{$form_id}", apply_filters("gform_leads_before_export", $leads, $form, $paging), $form, $paging);
         foreach ($leads as $lead) {
             foreach ($fields as $field_id) {
                 switch ($field_id) {
                     case "date_created":
                         $lead_gmt_time = mysql2date("G", $lead["date_created"]);
                         $lead_local_time = GFCommon::get_local_timestamp($lead_gmt_time);
                         $value = date_i18n("Y-m-d H:i:s", $lead_local_time, true);
                         break;
                     default:
                         $long_text = "";
                         if (strlen(rgar($lead, $field_id)) >= GFORMS_MAX_FIELD_LENGTH - 10) {
                             $long_text = RGFormsModel::get_field_value_long($lead, $field_id, $form);
                         }
                         $value = !empty($long_text) ? $long_text : rgar($lead, $field_id);
                         $field = RGFormsModel::get_field($form, $field_id);
                         $input_type = RGFormsModel::get_input_type($field);
                         if ($input_type == "checkbox") {
                             $value = GFFormsModel::is_checkbox_checked($field_id, $headers[$field_id], $lead, $form);
                             if ($value === false) {
                                 $value = "";
                             }
                         } else {
                             if ($input_type == "fileupload" && rgar($field, "multipleFiles")) {
                                 $value = !empty($value) ? implode(" , ", json_decode($value, true)) : "";
                             }
                         }
                         $value = apply_filters("gform_export_field_value", $value, $form_id, $field_id, $lead);
                         GFCommon::log_debug("Value for field ID {$field_id}: {$value}");
                         break;
                 }
                 if (isset($field_rows[$field_id])) {
                     $list = empty($value) ? array() : unserialize($value);
                     foreach ($list as $row) {
                         $row_values = array_values($row);
                         $row_str = implode("|", $row_values);
                         $lines .= '"' . str_replace('"', '""', $row_str) . '"' . $separator;
                     }
                     //filling missing subrow columns (if any)
                     $missing_count = intval($field_rows[$field_id]) - count($list);
                     for ($i = 0; $i < $missing_count; $i++) {
                         $lines .= '""' . $separator;
                     }
                 } else {
                     $value = maybe_unserialize($value);
                     if (is_array($value)) {
                         $value = implode("|", $value);
                     }
                     $lines .= '"' . str_replace('"', '""', $value) . '"' . $separator;
                 }
             }
             $lines = substr($lines, 0, strlen($lines) - 1);
             GFCommon::log_debug("Lines: {$lines}");
             $lines .= "\n";
         }
         $offset += $page_size;
         $entry_count -= $page_size;
         if (!seems_utf8($lines)) {
             $lines = utf8_encode($lines);
         }
         echo $lines;
         $lines = "";
     }
 }
 public function get_entries($entry_ids, $form_ids = null, $schema = '', $field_ids = array())
 {
     $this->authorize('gravityforms_view_entries');
     $status = 200;
     $response = array();
     $result = array();
     if ($entry_ids) {
         if (is_array($entry_ids)) {
             foreach ($entry_ids as $entry_id) {
                 $result = GFAPI::get_entry($entry_id);
                 if (!is_wp_error($result)) {
                     $response[$entry_id] = $result;
                     if (!empty($field_ids) && !empty($response[$entry_id])) {
                         $response[$entry_id] = $this->filter_entry_object($response[$entry_id], $field_ids);
                     }
                 }
             }
         } else {
             $result = GFAPI::get_entry($entry_ids);
             if (!is_wp_error($result)) {
                 $response = $result;
                 if (!empty($field_ids) && !empty($response)) {
                     $response = $this->filter_entry_object($response, $field_ids);
                 }
             }
         }
         if ($schema == 'mtd') {
             $response = self::mtd_transform_entry_data($response);
         }
     } else {
         //sorting parameters
         $sort_key = isset($_GET['sorting']['key']) && !empty($_GET['sorting']['key']) ? $_GET['sorting']['key'] : 'id';
         $sort_dir = isset($_GET['sorting']['direction']) && !empty($_GET['sorting']['direction']) ? $_GET['sorting']['direction'] : 'DESC';
         $sorting = array('key' => $sort_key, 'direction' => $sort_dir);
         if (isset($_GET['sorting']['is_numeric'])) {
             $sorting['is_numeric'] = $_GET['sorting']['is_numeric'];
         }
         //paging parameters
         $page_size = isset($_GET['paging']['page_size']) ? intval($_GET['paging']['page_size']) : 10;
         if (isset($_GET['paging']['current_page'])) {
             $current_page = intval($_GET['paging']['current_page']);
             $offset = $page_size * ($current_page - 1);
         } else {
             $offset = isset($_GET['paging']['offset']) ? intval($_GET['paging']['offset']) : 0;
         }
         $paging = array('offset' => $offset, 'page_size' => $page_size);
         $search = isset($_GET['search']) ? $_GET['search'] : array();
         if (empty($form_ids)) {
             $form_ids = 0;
         }
         // all forms
         $entry_count = GFAPI::count_entries($form_ids, $search);
         $result = $entry_count > 0 ? GFAPI::get_entries($form_ids, $search, $sorting, $paging) : array();
         if (!is_wp_error($result)) {
             $response = array('total_count' => $entry_count, 'entries' => $result);
             if ($schema == 'mtd') {
                 $response = $this->mtd_transform_entries_data($response, $form_ids);
             }
         }
     }
     if (is_wp_error($result)) {
         $response = $this->get_error_response($result);
         $status = $this->get_error_status($result);
     }
     $this->end($status, $response);
 }
예제 #11
0
 function get_counts($args)
 {
     if (!empty($args['field_filters'])) {
         if (isset($args['form-id'])) {
             $form_ids = absint($args['form-id']);
         } else {
             $form_ids = $this->get_workflow_form_ids();
         }
         $results = new stdClass();
         $results->total = 0;
         $results->pending = 0;
         $results->complete = 0;
         $results->cancelled = 0;
         if (empty($form_ids)) {
             $this->items = array();
             return $results;
         }
         $base_search_criteria = $this->get_search_criteria();
         $pending_search_criteria = $base_search_criteria;
         $pending_search_criteria['field_filters'][] = array('key' => 'workflow_final_status', 'value' => 'pending');
         $complete_search_criteria = $base_search_criteria;
         $complete_search_criteria['field_filters'][] = array('key' => 'workflow_final_status', 'operator' => 'not in', 'value' => array('pending', 'cancelled'));
         $cancelled_search_criteria = $base_search_criteria;
         $cancelled_search_criteria['field_filters'][] = array('key' => 'workflow_final_status', 'value' => 'cancelled');
         $results->total = GFAPI::count_entries($form_ids, $base_search_criteria);
         $results->pending = GFAPI::count_entries($form_ids, $pending_search_criteria);
         $results->complete = GFAPI::count_entries($form_ids, $complete_search_criteria);
         $results->cancelled = GFAPI::count_entries($form_ids, $cancelled_search_criteria);
         return $results;
     }
     global $wpdb;
     if (!empty($args['form-id'])) {
         $form_clause = ' AND l.form_id=' . absint($args['form-id']);
     } else {
         $form_ids = $this->get_workflow_form_ids();
         if (empty($form_ids)) {
             $results = new stdClass();
             $results->pending = 0;
             $results->complete = 0;
             $results->cancelled = 0;
             return $results;
         }
         $form_clause = ' AND l.form_id IN(' . join(',', $form_ids) . ')';
     }
     $start_clause = '';
     if (!empty($args['start-date'])) {
         $start_clause = $wpdb->prepare(' AND l.date_created >= %s', $args['start-date']);
     }
     $end_clause = '';
     if (!empty($args['end-date'])) {
         $end_clause = $wpdb->prepare(' AND l.date_created <= %s', $args['end-date']);
     }
     $user_id_clause = '';
     if (!$this->display_all) {
         $user = wp_get_current_user();
         $user_id_clause = $wpdb->prepare(' AND created_by=%d', $user->ID);
     }
     $lead_table = GFFormsModel::get_lead_table_name();
     $meta_table = GFFormsModel::get_lead_meta_table_name();
     $sql = "SELECT\n\t\t(SELECT count(distinct(l.id)) FROM {$lead_table} l WHERE l.status='active' {$form_clause} {$start_clause} {$end_clause} {$user_id_clause}) as total,\n\t\t(SELECT count(distinct(l.id)) FROM {$lead_table} l INNER JOIN  {$meta_table} m ON l.id = m.lead_id WHERE l.status='active' AND meta_key='workflow_final_status' AND meta_value='pending' {$form_clause} {$start_clause} {$end_clause} {$user_id_clause}) as pending,\n\t\t(SELECT count(distinct(l.id)) FROM {$lead_table} l INNER JOIN  {$meta_table} m ON l.id = m.lead_id WHERE l.status='active' AND meta_key='workflow_final_status' AND meta_value NOT IN('pending', 'cancelled') {$form_clause} {$start_clause} {$end_clause} {$user_id_clause}) as complete,\n\t\t(SELECT count(distinct(l.id)) FROM {$lead_table} l INNER JOIN  {$meta_table} m ON l.id = m.lead_id WHERE l.status='active' AND meta_key='workflow_final_status' AND meta_value='cancelled' {$form_clause} {$start_clause} {$end_clause} {$user_id_clause}) as cancelled\n\t\t";
     $results = $wpdb->get_results($sql);
     return $results[0];
 }
예제 #12
0
 function get_form_entries_json($gf_form_id, $post_id)
 {
     $gf_fields = array();
     $gf_headers = array();
     $gf_field_ids = array();
     $i = 0;
     while (have_rows('field_column_display_repeater', $post_id)) {
         the_row();
         $gf_headers[$i] = get_sub_field('form_field_column_header');
         $gf_field_ids[$i] = get_sub_field('form_field_column_selector');
         $i++;
     }
     $search_criteria = array();
     $sorting = array('key' => $gf_fields[1], 'direction' => 'ASC');
     $gf_entries = GFAPI::get_entries($gf_form_id, $search_criteria, $sorting);
     $output = array('sColumns' => implode(',', $gf_headers), 'sEcho' => isset($_GET['sEcho']) ? intval($_GET['sEcho']) : null, 'iTotalRecords' => isset($gf_form_id) ? GFAPI::count_entries($gf_form_id) : 0, 'iTotalDisplayRecords' => 10, 'aaData' => array());
     foreach ($gf_entries as $gf_entry) {
         $columnvalue = $this->get_columnvalues($gf_entry, $gf_field_ids);
         $row = array();
         for ($i = 0; $i < count($gf_field_ids); $i++) {
             $row[$i] = $columnvalue[$i];
         }
         $output['aaData'][] = $row;
     }
     return $output;
 }
예제 #13
0
 /**
  * Returns the number of entries on this step.
  *
  * @return int|mixed
  */
 public function entry_count()
 {
     if (isset($this->_entry_count)) {
         return $this->_entry_count;
     }
     $form_id = $this->get_form_id();
     $search_criteria = array('status' => 'active', 'field_filters' => array(array('key' => 'workflow_step', 'value' => $this->get_id())));
     $this->_entry_count = GFAPI::count_entries($form_id, $search_criteria);
     return $this->_entry_count;
 }
예제 #14
0
 public static function start_export($form, $offset = 0, $export_id = '')
 {
     $time_start = microtime(true);
     /***
      * Allows the export max execution time to be changed.
      *
      * When the max execution time is reached, the export routine stop briefly and submit another AJAX request to continue exporting entries from the point it stopped.
      *
      * @since 2.0.3.10
      *
      * @param int   20    The amount of time, in seconds, that each request should run for.  Defaults to 20 seconds.
      * @param array $form The Form Object
      */
     $max_execution_time = apply_filters('gform_export_max_execution_time', 20, $form);
     // seconds
     $page_size = 20;
     $form_id = $form['id'];
     $fields = $_POST['export_field'];
     $start_date = empty($_POST['export_date_start']) ? '' : self::get_gmt_date($_POST['export_date_start'] . ' 00:00:00');
     $end_date = empty($_POST['export_date_end']) ? '' : self::get_gmt_date($_POST['export_date_end'] . ' 23:59:59');
     $search_criteria['status'] = 'active';
     $search_criteria['field_filters'] = GFCommon::get_field_filters_from_post($form);
     if (!empty($start_date)) {
         $search_criteria['start_date'] = $start_date;
     }
     if (!empty($end_date)) {
         $search_criteria['end_date'] = $end_date;
     }
     //$sorting = array( 'key' => 'date_created', 'direction' => 'DESC', 'type' => 'info' );
     $sorting = array('key' => 'id', 'direction' => 'DESC', 'type' => 'info');
     $form = self::add_default_export_fields($form);
     $total_entry_count = GFAPI::count_entries($form_id, $search_criteria);
     $remaining_entry_count = $offset == 0 ? $total_entry_count : $total_entry_count - $offset;
     // Adding BOM marker for UTF-8
     $lines = '';
     // Set the separator
     $separator = gf_apply_filters(array('gform_export_separator', $form_id), ',', $form_id);
     $field_rows = self::get_field_row_count($form, $fields, $remaining_entry_count);
     if ($offset == 0) {
         //Adding BOM marker for UTF-8
         $lines = chr(239) . chr(187) . chr(191);
         //writing header
         $headers = array();
         foreach ($fields as $field_id) {
             $field = RGFormsModel::get_field($form, $field_id);
             $label = gf_apply_filters(array('gform_entries_field_header_pre_export', $form_id, $field_id), GFCommon::get_label($field, $field_id), $form, $field);
             $value = str_replace('"', '""', $label);
             GFCommon::log_debug("GFExport::start_export(): Header for field ID {$field_id}: {$value}");
             if (strpos($value, '=') === 0) {
                 // Prevent Excel formulas
                 $value = "'" . $value;
             }
             $headers[$field_id] = $value;
             $subrow_count = isset($field_rows[$field_id]) ? intval($field_rows[$field_id]) : 0;
             if ($subrow_count == 0) {
                 $lines .= '"' . $value . '"' . $separator;
             } else {
                 for ($i = 1; $i <= $subrow_count; $i++) {
                     $lines .= '"' . $value . ' ' . $i . '"' . $separator;
                 }
             }
             //GFCommon::log_debug( "GFExport::start_export(): Lines: {$lines}" );
         }
         $lines = substr($lines, 0, strlen($lines) - 1) . "\n";
         if ($remaining_entry_count == 0) {
             self::write_file($lines, $export_id);
         }
     }
     // Paging through results for memory issues
     while ($remaining_entry_count > 0) {
         $paging = array('offset' => $offset, 'page_size' => $page_size);
         $leads = GFAPI::get_entries($form_id, $search_criteria, $sorting, $paging);
         $leads = gf_apply_filters(array('gform_leads_before_export', $form_id), $leads, $form, $paging);
         GFCommon::log_debug(__METHOD__ . '(): search criteria: ' . print_r($search_criteria, true));
         GFCommon::log_debug(__METHOD__ . '(): sorting: ' . print_r($sorting, true));
         GFCommon::log_debug(__METHOD__ . '(): paging: ' . print_r($paging, true));
         foreach ($leads as $lead) {
             GFCommon::log_debug(__METHOD__ . '(): Processing entry #' . $lead['id']);
             foreach ($fields as $field_id) {
                 switch ($field_id) {
                     case 'date_created':
                         $lead_gmt_time = mysql2date('G', $lead['date_created']);
                         $lead_local_time = GFCommon::get_local_timestamp($lead_gmt_time);
                         $value = date_i18n('Y-m-d H:i:s', $lead_local_time, true);
                         break;
                     default:
                         $field = RGFormsModel::get_field($form, $field_id);
                         $value = is_object($field) ? $field->get_value_export($lead, $field_id, false, true) : rgar($lead, $field_id);
                         $value = apply_filters('gform_export_field_value', $value, $form_id, $field_id, $lead);
                         //GFCommon::log_debug( "GFExport::start_export(): Value for field ID {$field_id}: {$value}" );
                         break;
                 }
                 if (isset($field_rows[$field_id])) {
                     $list = empty($value) ? array() : unserialize($value);
                     foreach ($list as $row) {
                         $row_values = array_values($row);
                         $row_str = implode('|', $row_values);
                         if (strpos($row_str, '=') === 0) {
                             // Prevent Excel formulas
                             $row_str = "'" . $row_str;
                         }
                         $lines .= '"' . str_replace('"', '""', $row_str) . '"' . $separator;
                     }
                     //filling missing subrow columns (if any)
                     $missing_count = intval($field_rows[$field_id]) - count($list);
                     for ($i = 0; $i < $missing_count; $i++) {
                         $lines .= '""' . $separator;
                     }
                 } else {
                     $value = maybe_unserialize($value);
                     if (is_array($value)) {
                         $value = implode('|', $value);
                     }
                     if (strpos($value, '=') === 0) {
                         // Prevent Excel formulas
                         $value = "'" . $value;
                     }
                     $lines .= '"' . str_replace('"', '""', $value) . '"' . $separator;
                 }
             }
             $lines = substr($lines, 0, strlen($lines) - 1);
             //GFCommon::log_debug( "GFExport::start_export(): Lines: {$lines}" );
             $lines .= "\n";
         }
         $offset += $page_size;
         $remaining_entry_count -= $page_size;
         if (!seems_utf8($lines)) {
             $lines = utf8_encode($lines);
         }
         $lines = apply_filters('gform_export_lines', $lines);
         self::write_file($lines, $export_id);
         $time_end = microtime(true);
         $execution_time = $time_end - $time_start;
         if ($execution_time >= $max_execution_time) {
             break;
         }
         $lines = '';
     }
     $complete = $remaining_entry_count <= 0;
     if ($complete) {
         /**
          * Fires after exporting all the entries in form
          *
          * @param array  $form       The Form object to get the entries from
          * @param string $start_date The start date for when the export of entries should take place
          * @param string $end_date   The end date for when the export of entries should stop
          * @param array  $fields     The specified fields where the entries should be exported from
          */
         do_action('gform_post_export_entries', $form, $start_date, $end_date, $fields);
     }
     $offset = $complete ? 0 : $offset;
     $status = array('status' => $complete ? 'complete' : 'in_progress', 'offset' => $offset, 'exportId' => $export_id, 'progress' => $remaining_entry_count > 0 ? intval(100 - $remaining_entry_count / $total_entry_count * 100) . '%' : '');
     GFCommon::log_debug(__METHOD__ . '(): Status: ' . print_r($status, 1));
     return $status;
 }
예제 #15
0
 public static function start_export($form)
 {
     $form_id = $form['id'];
     $fields = $_POST['export_field'];
     $start_date = empty($_POST['export_date_start']) ? '' : self::get_gmt_date($_POST['export_date_start'] . ' 00:00:00');
     $end_date = empty($_POST['export_date_end']) ? '' : self::get_gmt_date($_POST['export_date_end'] . ' 23:59:59');
     $search_criteria['status'] = 'active';
     $search_criteria['field_filters'] = GFCommon::get_field_filters_from_post($form);
     if (!empty($start_date)) {
         $search_criteria['start_date'] = $start_date;
     }
     if (!empty($end_date)) {
         $search_criteria['end_date'] = $end_date;
     }
     $sorting = array('key' => 'date_created', 'direction' => 'DESC', 'type' => 'info');
     GFCommon::log_debug("GFExport::start_export(): Start date: {$start_date}");
     GFCommon::log_debug("GFExport::start_export(): End date: {$end_date}");
     $form = self::add_default_export_fields($form);
     $entry_count = GFAPI::count_entries($form_id, $search_criteria);
     $page_size = 100;
     $offset = 0;
     //Adding BOM marker for UTF-8
     $lines = chr(239) . chr(187) . chr(191);
     // set the separater
     $separator = gf_apply_filters('gform_export_separator', $form_id, ',', $form_id);
     $field_rows = self::get_field_row_count($form, $fields, $entry_count);
     //writing header
     $headers = array();
     foreach ($fields as $field_id) {
         $field = RGFormsModel::get_field($form, $field_id);
         $label = gf_apply_filters('gform_entries_field_header_pre_export', array($form_id, $field_id), GFCommon::get_label($field, $field_id), $form, $field);
         $value = str_replace('"', '""', $label);
         GFCommon::log_debug("GFExport::start_export(): Header for field ID {$field_id}: {$value}");
         $headers[$field_id] = $value;
         $subrow_count = isset($field_rows[$field_id]) ? intval($field_rows[$field_id]) : 0;
         if ($subrow_count == 0) {
             $lines .= '"' . $value . '"' . $separator;
         } else {
             for ($i = 1; $i <= $subrow_count; $i++) {
                 $lines .= '"' . $value . ' ' . $i . '"' . $separator;
             }
         }
         GFCommon::log_debug("GFExport::start_export(): Lines: {$lines}");
     }
     $lines = substr($lines, 0, strlen($lines) - 1) . "\n";
     //paging through results for memory issues
     while ($entry_count > 0) {
         $paging = array('offset' => $offset, 'page_size' => $page_size);
         $leads = GFAPI::get_entries($form_id, $search_criteria, $sorting, $paging);
         $leads = gf_apply_filters('gform_leads_before_export', $form_id, $leads, $form, $paging);
         foreach ($leads as $lead) {
             foreach ($fields as $field_id) {
                 switch ($field_id) {
                     case 'date_created':
                         $lead_gmt_time = mysql2date('G', $lead['date_created']);
                         $lead_local_time = GFCommon::get_local_timestamp($lead_gmt_time);
                         $value = date_i18n('Y-m-d H:i:s', $lead_local_time, true);
                         break;
                     default:
                         $field = RGFormsModel::get_field($form, $field_id);
                         $value = is_object($field) ? $field->get_value_export($lead, $field_id, false, true) : rgar($lead, $field_id);
                         $value = apply_filters('gform_export_field_value', $value, $form_id, $field_id, $lead);
                         GFCommon::log_debug("GFExport::start_export(): Value for field ID {$field_id}: {$value}");
                         break;
                 }
                 if (isset($field_rows[$field_id])) {
                     $list = empty($value) ? array() : unserialize($value);
                     foreach ($list as $row) {
                         $row_values = array_values($row);
                         $row_str = implode('|', $row_values);
                         $lines .= '"' . str_replace('"', '""', $row_str) . '"' . $separator;
                     }
                     //filling missing subrow columns (if any)
                     $missing_count = intval($field_rows[$field_id]) - count($list);
                     for ($i = 0; $i < $missing_count; $i++) {
                         $lines .= '""' . $separator;
                     }
                 } else {
                     $value = maybe_unserialize($value);
                     if (is_array($value)) {
                         $value = implode('|', $value);
                     }
                     $lines .= '"' . str_replace('"', '""', $value) . '"' . $separator;
                 }
             }
             $lines = substr($lines, 0, strlen($lines) - 1);
             GFCommon::log_debug("GFExport::start_export(): Lines: {$lines}");
             $lines .= "\n";
         }
         $offset += $page_size;
         $entry_count -= $page_size;
         if (!seems_utf8($lines)) {
             $lines = utf8_encode($lines);
         }
         $lines = apply_filters('gform_export_lines', $lines);
         echo $lines;
         $lines = '';
     }
     /**
      * Fires after exporting all the entries in form
      *
      * @param array $form The Form object to get the entries from
      * @param string $start_date The start date for when the export of entries should take place
      * @param string $end_date The end date for when the export of entries should stop
      * @param array $fields The specified fields where the entries should be exported from
      */
     do_action('gform_post_export_entries', $form, $start_date, $end_date, $fields);
 }
예제 #16
0
/**
 * This function will remove all of the music from the NNMTA music database.
 *
 * This function was created to support the scenario when the festival chariman needs
 * to update the music in the NNMTA music database. In order to do this, all of the existing
 * data is removed from the database prior to adding all of the new data. This ensures
 * that the new data is added appropriately without accidentally adding old, possibly
 * unwanted music data.
 *
 * @since 2.0.0
 * @author KREW
 */
function aria_remove_all_music_from_nnmta_database()
{
    $nnmta_music_database_form_id = aria_get_nnmta_database_form_id();
    $index = 0;
    $total_count = GFAPI::count_entries($nnmta_music_database_form_id);
    while ($index < $total_count) {
        $twenty_songs = GFAPI::get_entries($nnmta_music_database_form_id);
        for ($song_on_page = 0; $song_on_page < 20; $song_on_page++) {
            $deleted_song = GFAPI::delete_entry($twenty_songs[$song_on_page]['id']);
            if (is_wp_error($deleted_song)) {
                wp_die($deleted_song->get_error_message());
            }
        }
        $index += 20;
    }
    /*
    	foreach ($all_songs as $song) {
    		$deleted_song = GFAPI::delete_entry($song['id']);
    		if (is_wp_error($deleted_song)) {
    			wp_die($deleted_song->get_error_message());
    		}
    	}
    */
}
예제 #17
0
 public function get_results_data($form, $fields, $search_criteria = array(), $state_array = array(), $max_execution_time = 15)
 {
     // todo: add hooks to modify $max_execution_time and $page_size?
     $page_size = 150;
     $time_start = microtime(true);
     $form_id = $form['id'];
     $data = array();
     $offset = 0;
     $entry_count = 0;
     $field_data = array();
     if ($state_array) {
         //get counts from state
         $data = $state_array;
         $offset = (int) rgar($data, 'offset');
         unset($data['offset']);
         $entry_count = $offset;
         $field_data = rgar($data, 'field_data');
     } else {
         //initialize counts
         foreach ($fields as $field) {
             $field_type = GFFormsModel::get_input_type($field);
             if (false === isset($field->choices)) {
                 $field_data[$field->id] = 0;
                 continue;
             }
             $choices = $field->choices;
             if ($field_type == 'likert' && rgar($field, 'gsurveyLikertEnableMultipleRows')) {
                 foreach ($field->gsurveyLikertRows as $row) {
                     foreach ($choices as $choice) {
                         $field_data[$field->id][$row['value']][$choice['value']] = 0;
                     }
                     if (rgar($field, 'gsurveyLikertEnableScoring')) {
                         $field_data[$field->id][$row['value']]['row_score_sum'] = 0;
                     }
                 }
             } else {
                 if (!empty($choices) && is_array($choices)) {
                     foreach ($choices as $choice) {
                         $field_data[$field->id][$choice['value']] = 0;
                     }
                 } else {
                     $field_data[$field->id] = 0;
                 }
             }
             if ($field_type == 'likert' && rgar($field, 'gsurveyLikertEnableScoring')) {
                 $field_data[$field->id]['sum_of_scores'] = 0;
             }
         }
     }
     $count_search_leads = GFAPI::count_entries($form_id, $search_criteria);
     $data['entry_count'] = $count_search_leads;
     $entries_left = $count_search_leads - $offset;
     while ($entries_left > 0) {
         $paging = array('offset' => $offset, 'page_size' => $page_size);
         $search_leads_time_start = microtime(true);
         $leads = GFFormsModel::search_leads($form_id, $search_criteria, null, $paging);
         $search_leads_time_end = microtime(true);
         $search_leads_time = $search_leads_time_end - $search_leads_time_start;
         $leads_in_search = count($leads);
         $entry_count += $leads_in_search;
         $leads_processed = 0;
         foreach ($leads as $lead) {
             $lead_time_start = microtime(true);
             foreach ($fields as $field) {
                 $field_type = GFFormsModel::get_input_type($field);
                 $field_id = $field->id;
                 $value = RGFormsModel::get_lead_field_value($lead, $field);
                 if ($field_type == 'likert' && rgar($field, 'gsurveyLikertEnableMultipleRows')) {
                     if (empty($value)) {
                         continue;
                     }
                     foreach ($value as $value_vector) {
                         if (empty($value_vector)) {
                             continue;
                         }
                         list($row_val, $col_val) = explode(':', $value_vector, 2);
                         if (isset($field_data[$field->id][$row_val]) && isset($field_data[$field->id][$row_val][$col_val])) {
                             $field_data[$field->id][$row_val][$col_val]++;
                             if ($field->gsurveyLikertEnableScoring) {
                                 $field_data[$field->id][$row_val]['row_score_sum'] += $this->get_likert_row_score($row_val, $field, $lead);
                             }
                         }
                     }
                 } elseif ($field_type == 'rank') {
                     $score = count(rgar($field, 'choices'));
                     $values = explode(',', $value);
                     foreach ($values as $ranked_value) {
                         $field_data[$field->id][$ranked_value] += $score;
                         $score--;
                     }
                 } else {
                     if (empty($field->choices)) {
                         if (false === empty($value)) {
                             $field_data[$field_id]++;
                         }
                         continue;
                     }
                     $choices = $field->choices;
                     foreach ($choices as $choice) {
                         $choice_is_selected = false;
                         if (is_array($value)) {
                             $choice_value = rgar($choice, 'value');
                             if (in_array($choice_value, $value)) {
                                 $choice_is_selected = true;
                             }
                         } else {
                             if (RGFormsModel::choice_value_match($field, $choice, $value)) {
                                 $choice_is_selected = true;
                             }
                         }
                         if ($choice_is_selected) {
                             $field_data[$field_id][$choice['value']]++;
                         }
                     }
                 }
                 if ($field_type == 'likert' && rgar($field, 'gsurveyLikertEnableScoring')) {
                     $field_data[$field->id]['sum_of_scores'] += $this->get_likert_score($field, $lead);
                 }
             }
             $leads_processed++;
             $lead_time_end = microtime(true);
             $total_execution_time = $lead_time_end - $search_leads_time_start;
             $lead_execution_time = $lead_time_end - $lead_time_start;
             if ($total_execution_time + $lead_execution_time > $max_execution_time) {
                 break;
             }
         }
         $data['field_data'] = $field_data;
         if (isset($this->_callbacks['calculation'])) {
             $data = call_user_func($this->_callbacks['calculation'], $data, $form, $fields, $leads);
             $field_data = $data['field_data'];
         }
         $offset += $leads_processed;
         $entries_left -= $leads_processed;
         $time_end = microtime(true);
         $execution_time = $time_end - $time_start;
         if ($entries_left > 0 && $execution_time + $search_leads_time > $max_execution_time) {
             $data['status'] = 'incomplete';
             $data['offset'] = $offset;
             $progress = $data['entry_count'] > 0 ? round($data['offset'] / $data['entry_count'] * 100) : 0;
             $data['progress'] = $progress;
             break;
         }
         if ($entries_left <= 0) {
             $data['status'] = 'complete';
         }
     }
     $data['timestamp'] = time();
     return $data;
 }
 public function wp_ajax_gfe_util()
 {
     $criteria = $_POST['criteria'];
     //: Check if criteria exists and is in the correct format ://
     if (!isset($criteria) || empty($criteria) || !is_array($criteria)) {
         die;
     }
     //: Check for non-null method parameter exist ://
     if (!isset($criteria['method']) || empty($criteria['method'])) {
         die;
     }
     //: Check for criteria and set defaults ://
     $start_date = !empty($criteria['start_date']) ? $criteria['start_date'] : date('Y-m-d', 0);
     $end_date = !empty($criteria['end_date']) ? $criteria['end_date'] : current_time('Y-m-d');
     $search_criteria = array('start_date' => sanitize_text_field($start_date), 'end_date' => sanitize_text_field($end_date));
     //: Get form ids if not empty or set to all forms ://
     $forms = !empty($criteria['form_id']) ? array(GFAPI::get_form(sanitize_key($criteria['form_id']))) : GFAPI::get_forms();
     $response = array('criteria' => array('method' => sanitize_key($criteria['method']), 'start_date' => sanitize_text_field($start_date), 'end_date' => sanitize_text_field($end_date)), 'results' => array());
     switch (sanitize_key($criteria['method'])) {
         //: Count entries ://
         case 'count':
             $total_count = 0;
             foreach ($forms as $form) {
                 $entry_count = GFAPI::count_entries($form['id'], $search_criteria);
                 $response['results'][] = array('id' => $form['id'], 'title' => $form['title'], 'entry_count' => $entry_count);
                 $total_count += $entry_count;
             }
             break;
             //: Export Entries ://
         //: Export Entries ://
         case 'export':
             //: Todo ://
             die;
             break;
     }
     wp_send_json($response);
     die;
 }