Exemplo n.º 1
0
 private static function matches_current_date($format, $js_timestamp)
 {
     $target_date = $format == "YW" ? $js_timestamp : date($format, $js_timestamp / 1000);
     $current_date = gmdate($format, GFCommon::get_local_timestamp(time()));
     return $target_date == $current_date;
 }
Exemplo n.º 2
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);
 }
 protected function get_sales_summary($form_id)
 {
     global $wpdb;
     $tz_offset = $this->get_mysql_tz_offset();
     $summary = $wpdb->get_results($wpdb->prepare("\n                    SELECT lead.date, lead.orders, lead.subscriptions, transaction.revenue\n                    FROM (\n                       SELECT  date( CONVERT_TZ(date_created, '+00:00', '" . $tz_offset . "') ) as date,\n                               sum( if(transaction_type = 1,1,0) ) as orders,\n                               sum( if(transaction_type = 2,1,0) ) as subscriptions\n                       FROM {$wpdb->prefix}rg_lead\n                       WHERE status='active' AND form_id = %d AND datediff(now(), CONVERT_TZ(date_created, '+00:00', '" . $tz_offset . "') ) <= 30\n                       GROUP BY date\n                     ) AS lead\n\n                     LEFT OUTER JOIN(\n                       SELECT  date( CONVERT_TZ(t.date_created, '+00:00', '" . $tz_offset . "') ) as date,\n                               sum( if(t.transaction_type = 'refund', abs(t.amount) * -1, t.amount) ) as revenue\n                       FROM {$wpdb->prefix}gf_addon_payment_transaction t\n                         INNER JOIN {$wpdb->prefix}rg_lead l ON l.id = t.lead_id\n                       WHERE l.form_id=%d AND l.status='active'\n                       GROUP BY date\n                     ) AS transaction on lead.date = transaction.date\n                    ORDER BY date desc", $form_id, $form_id), ARRAY_A);
     $total_summary = $wpdb->get_results($wpdb->prepare("\n                    SELECT sum( if(transaction_type = 1,1,0) ) as orders,\n                           sum( if(transaction_type = 2,1,0) ) as subscriptions\n                    FROM {$wpdb->prefix}rg_lead\n                    WHERE form_id=%d AND status='active'", $form_id), ARRAY_A);
     $total_revenue = $wpdb->get_var($wpdb->prepare("\n                    SELECT sum( if(t.transaction_type = 'refund', abs(t.amount) * -1, t.amount) ) as revenue\n                    FROM {$wpdb->prefix}gf_addon_payment_transaction t\n                    INNER JOIN {$wpdb->prefix}rg_lead l ON l.id = t.lead_id\n                    WHERE l.form_id=%d AND status='active'", $form_id));
     $result = array('today' => array('revenue' => GFCommon::to_money(0), 'orders' => 0, 'subscriptions' => 0), 'yesterday' => array('revenue' => GFCommon::to_money(0), 'orders' => 0, 'subscriptions' => 0), 'last30' => array('revenue' => 0, 'orders' => 0, 'subscriptions' => 0), 'total' => array('revenue' => GFCommon::to_money($total_revenue), 'orders' => $total_summary[0]['orders'], 'subscriptions' => $total_summary[0]['subscriptions']));
     $local_time = GFCommon::get_local_timestamp();
     $today = gmdate('Y-m-d', $local_time);
     $yesterday = gmdate('Y-m-d', strtotime('-1 day', $local_time));
     foreach ($summary as $day) {
         if ($day['date'] == $today) {
             $result['today']['revenue'] = GFCommon::to_money($day['revenue']);
             $result['today']['orders'] = $day['orders'];
             $result['today']['subscriptions'] = $day['subscriptions'];
         } elseif ($day['date'] == $yesterday) {
             $result['yesterday']['revenue'] = GFCommon::to_money($day['revenue']);
             $result['yesterday']['orders'] = $day['orders'];
             $result['yesterday']['subscriptions'] = $day['subscriptions'];
         }
         $is_within_30_days = strtotime($day['date']) >= strtotime('-30 days', $local_time);
         if ($is_within_30_days) {
             $result['last30']['revenue'] += floatval($day['revenue']);
             $result['last30']['orders'] += floatval($day['orders']);
             $result['last30']['subscriptions'] += floatval($day['subscriptions']);
         }
     }
     $result['last30']['revenue'] = GFCommon::to_money($result['last30']['revenue']);
     return $result;
 }
 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 = '';
     }
 }
Exemplo n.º 5
0
 public static function start_export($form)
 {
     $form_id = $form["id"];
     $fields = $_POST["export_field"];
     $start_date = $_POST["export_date_start"];
     $end_date = $_POST["export_date_end"];
     //adding default fields
     array_push($form["fields"], array("id" => "created_by", "label" => __("Created By (User Id)", "gravityforms")));
     array_push($form["fields"], array("id" => "id", "label" => __("Entry Id", "gravityforms")));
     array_push($form["fields"], array("id" => "date_created", "label" => __("Entry Date", "gravityforms")));
     array_push($form["fields"], array("id" => "source_url", "label" => __("Source Url", "gravityforms")));
     array_push($form["fields"], array("id" => "transaction_id", "label" => __("Transaction Id", "gravityforms")));
     array_push($form["fields"], array("id" => "payment_amount", "label" => __("Payment Amount", "gravityforms")));
     array_push($form["fields"], array("id" => "payment_date", "label" => __("Payment Date", "gravityforms")));
     array_push($form["fields"], array("id" => "payment_status", "label" => __("Payment Status", "gravityforms")));
     array_push($form["fields"], array("id" => "post_id", "label" => __("Post Id", "gravityforms")));
     array_push($form["fields"], array("id" => "user_agent", "label" => __("User Agent", "gravityforms")));
     array_push($form["fields"], array("id" => "ip", "label" => __("User IP", "gravityforms")));
     $entry_count = RGFormsModel::get_lead_count($form_id, "", null, null, $start_date, $end_date);
     $page_size = 200;
     $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
     foreach ($fields as $field_id) {
         $field = RGFormsModel::get_field($form, $field_id);
         $value = str_replace('"', '""', GFCommon::get_label($field, $field_id));
         $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;
             }
         }
     }
     $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);
         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);
                         break;
                     default:
                         $long_text = "";
                         if (strlen($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 : $lead[$field_id];
                         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);
             $lines .= "\n";
         }
         $offset += $page_size;
         $entry_count -= $page_size;
         if (!seems_utf8($lines)) {
             $lines = utf8_encode($lines);
         }
         echo $lines;
         $lines = "";
     }
 }
 protected function get_sales_summary($form_id)
 {
     global $wpdb;
     $tz_offset = $this->get_mysql_tz_offset();
     $summary = $wpdb->get_results($wpdb->prepare("\n                    SELECT lead.date, lead.orders, lead.subscriptions, transaction.revenue\n                    FROM (\n                       SELECT  date( CONVERT_TZ(date_created, '+00:00', '" . $tz_offset . "') ) as date,\n                               sum( if(transaction_type = 1,1,0) ) as orders,\n                               sum( if(transaction_type = 2,1,0) ) as subscriptions\n                       FROM {$wpdb->prefix}rg_lead\n                       WHERE form_id = %d and datediff(now(), CONVERT_TZ(date_created, '+00:00', '" . $tz_offset . "') ) <= 30\n                       GROUP BY date\n                     ) AS lead\n\n                     LEFT OUTER JOIN(\n                       SELECT  date( CONVERT_TZ(t.date_created, '+00:00', '" . $tz_offset . "') ) as date,\n                               sum(t.amount) as revenue\n                       FROM {$wpdb->prefix}gf_addon_payment_transaction t\n                         INNER JOIN {$wpdb->prefix}rg_lead l ON l.id = t.lead_id\n                       WHERE l.form_id=%d\n                       GROUP BY date\n                     ) AS transaction on lead.date = transaction.date\n                    ORDER BY date desc", $form_id, $form_id), ARRAY_A);
     $total_summary = $wpdb->get_results($wpdb->prepare("\n                    SELECT sum( if(transaction_type = 1,1,0) ) as orders,\n                         sum( if(transaction_type = 2,1,0) ) as subscriptions\n                    FROM {$wpdb->prefix}rg_lead\n                    WHERE form_id=%d", $form_id), ARRAY_A);
     $total_revenue = $wpdb->get_var($wpdb->prepare("\n                    SELECT sum(t.amount) as revenue\n                    FROM {$wpdb->prefix}gf_addon_payment_transaction t\n                    INNER JOIN {$wpdb->prefix}rg_lead l ON l.id = t.lead_id\n                    WHERE l.form_id=%d", $form_id));
     $result = array("today" => array("revenue" => GFCommon::to_money(0), "orders" => 0, "subscriptions" => 0), "yesterday" => array("revenue" => GFCommon::to_money(0), "orders" => 0, "subscriptions" => 0), "last30" => array("revenue" => 0, "orders" => 0, "subscriptions" => 0), "total" => array("revenue" => GFCommon::to_money($total_revenue), "orders" => $total_summary[0]["orders"], "subscriptions" => $total_summary[0]["subscriptions"]));
     $local_time = GFCommon::get_local_timestamp();
     $today = gmdate("Y-m-d", $local_time);
     $yesterday = gmdate("Y-m-d", strtotime("-1 day", $local_time));
     foreach ($summary as $day) {
         if ($day["date"] == $today) {
             $result["today"]["revenue"] = GFCommon::to_money($day["revenue"]);
             $result["today"]["orders"] = $day["orders"];
             $result["today"]["subscriptions"] = $day["subscriptions"];
         } else {
             if ($day["date"] == $yesterday) {
                 $result["yesterday"]["revenue"] = GFCommon::to_money($day["revenue"]);
                 $result["yesterday"]["orders"] = $day["orders"];
                 $result["yesterday"]["subscriptions"] = $day["subscriptions"];
             }
         }
         $is_within_30_days = strtotime($day["date"]) >= strtotime($local_time . " -30 days");
         if ($is_within_30_days) {
             $result["last30"]["revenue"] += floatval($day["revenue"]);
             $result["last30"]["orders"] += floatval($day["orders"]);
             $result["last30"]["subscriptions"] += floatval($day["subscriptions"]);
         }
     }
     $result["last30"]["revenue"] = GFCommon::to_money($result["last30"]["revenue"]);
     return $result;
 }
Exemplo n.º 7
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");
     $end_date = empty($_POST["export_date_end"]) ? "" : self::get_gmt_date($_POST["export_date_end"] . " 23:59:59");
     GFCommon::log_debug("start date: {$start_date}");
     GFCommon::log_debug("end date: {$end_date}");
     $form = self::add_default_export_fields($form);
     $entry_count = RGFormsModel::get_lead_count($form_id, "", null, null, $start_date, $end_date);
     $page_size = 200;
     $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
     foreach ($fields as $field_id) {
         $field = RGFormsModel::get_field($form, $field_id);
         $value = str_replace('"', '""', GFCommon::get_label($field, $field_id));
         $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;
             }
         }
     }
     $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);
         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($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 : $lead[$field_id];
                         $value = apply_filters("gform_export_field_value", $value, $form_id, $field_id, $lead);
                         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);
             $lines .= "\n";
         }
         $offset += $page_size;
         $entry_count -= $page_size;
         if (!seems_utf8($lines)) {
             $lines = utf8_encode($lines);
         }
         echo $lines;
         $lines = "";
     }
 }
Exemplo n.º 8
0
 /**
  * Handles the download request from the export entries page.
  *
  * @since 2.0.0
  */
 public static function ajax_download_export()
 {
     check_admin_referer('gform_download_export');
     if (!GFCommon::current_user_can_any('gravityforms_export_entries')) {
         die;
     }
     $form_id = absint(rgget('form-id'));
     if (empty($form_id)) {
         die;
     }
     $form = GFAPI::get_form($form_id);
     if (empty($form)) {
         die;
     }
     $filename = sanitize_title_with_dashes($form['title']) . '-' . gmdate('Y-m-d', GFCommon::get_local_timestamp(time())) . '.csv';
     $charset = get_option('blog_charset');
     header('Content-Description: File Transfer');
     header("Content-Disposition: attachment; filename={$filename}");
     header('Content-Type: text/csv; charset=' . $charset, true);
     $buffer_length = ob_get_length();
     //length or false if no buffer
     if ($buffer_length > 1) {
         ob_clean();
     }
     $export_id = rgget('export-id');
     $export_id = sanitize_key($export_id);
     $export_folder = RGFormsModel::get_upload_root() . 'export/';
     $file = $export_folder . 'export-' . $export_id . '.csv';
     readfile($file);
     @unlink($file);
     exit;
 }
Exemplo n.º 9
0
 /**
  * For some specific field types prepare the filter value before adding it to search criteria
  * @param  array  $filter
  * @return array
  */
 static function parse_advanced_filters($filter = array(), $view_id = NULL)
 {
     if (empty($filter['key']) || !function_exists('gravityview_get_field_type') || !class_exists('GFCommon') || !class_exists('GravityView_API')) {
         return $filter;
     }
     if (!empty($view_id)) {
         $form_id = gravityview_get_form_id($view_id);
         $form = gravityview_get_form($form_id);
     } else {
         global $gravityview_view;
         $form = $gravityview_view->form;
     }
     // replace merge tags
     $filter['value'] = GravityView_API::replace_variables($filter['value'], $form, array());
     $field_type = gravityview_get_field_type($form, $filter['key']);
     switch ($field_type) {
         case 'date':
             $local_timestamp = GFCommon::get_local_timestamp();
             $date = strtotime($filter['value'], $local_timestamp);
             if ($date) {
                 $filter['value'] = date('Y-m-d', $date);
             } else {
                 do_action('gravityview_log_error', __METHOD__ . ' - Date formatting passed to Advanced Filter is invalid', $filter['value']);
             }
             break;
     }
     return $filter;
 }
Exemplo n.º 10
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();
     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 = "";
     }
 }
Exemplo n.º 11
0
 function format_date($gmt_datetime, $is_human = true, $dateformat = 'Y/m/d \\a\\t H:i')
 {
     if (empty($gmt_datetime)) {
         return "";
     }
     //adjusting date to local configured Time Zone
     $lead_gmt_time = mysql2date("G", $gmt_datetime);
     $lead_local_time = GFCommon::get_local_timestamp($lead_gmt_time);
     $date_display = date_i18n($dateformat, $lead_local_time, true);
     return $date_display;
 }
 /**
  * Check if a coupon is active, not expired and not exceeded it's usage limit.
  *
  * @param array $config The coupon to be validated.
  * @param string $invalid_reason The reason the coupon is invalid.
  *
  * @return bool
  */
 public function is_valid($config, &$invalid_reason = '')
 {
     if (!$config['is_active']) {
         $invalid_reason = esc_html__('This coupon is currently inactive.', 'gravityformscoupons');
         return false;
     }
     $start_date = strtotime($config['meta']['startDate']);
     //start of the day
     $end_date = strtotime($config['meta']['endDate'] . ' 23:59:59');
     //end of the day
     $now = GFCommon::get_local_timestamp();
     //validating start date
     if ($config['meta']['startDate'] && $now < $start_date) {
         $invalid_reason = esc_html__('Invalid coupon.', 'gravityformscoupons');
         return false;
     }
     //validating end date
     if ($config['meta']['endDate'] && $now > $end_date) {
         $invalid_reason = esc_html__('This coupon has expired.', 'gravityformscoupons');
         return false;
     }
     //validating usage limit
     $is_under_limit = false;
     $coupon_usage = empty($config['meta']['usageCount']) ? 0 : intval($config['meta']['usageCount']);
     if (empty($config['meta']['usageLimit']) || $coupon_usage < intval($config['meta']['usageLimit'])) {
         $is_under_limit = true;
     }
     if (!$is_under_limit) {
         $invalid_reason = esc_html__('This coupon has reached its usage limit.', 'gravityformscoupons');
         return false;
     }
     //coupon is valid
     return true;
 }
Exemplo n.º 13
0
 /**
  * Allow formatting date and time based on GravityView standards
  *
  * @since 1.16
  *
  * @see GVCommon_Test::test_format_date for examples
  *
  * @param string $date_string The date as stored by Gravity Forms (`Y-m-d h:i:s` GMT)
  * @param string|array $args Array or string of settings for output parsed by `wp_parse_args()`; Can use `raw=1` or `array('raw' => true)` \n
  * - `raw` Un-formatted date string in original `Y-m-d h:i:s` format
  * - `timestamp` Integer timestamp returned by GFCommon::get_local_timestamp()
  * - `diff` "%s ago" format, unless other `format` is defined
  * - `human` Set $is_human parameter to true for `GFCommon::format_date()`. Shows `diff` within 24 hours or date after. Format based on blog setting, unless `format` is defined.
  * - `time` Include time in the `GFCommon::format_date()` output
  * - `format` Define your own date format, or `diff` format
  *
  * @return int|null|string Formatted date based on the original date
  */
 public static function format_date($date_string = '', $args = array())
 {
     $default_atts = array('raw' => false, 'timestamp' => false, 'diff' => false, 'human' => false, 'format' => '', 'time' => false);
     $atts = wp_parse_args($args, $default_atts);
     /**
      * Gravity Forms code to adjust date to locally-configured Time Zone
      * @see GFCommon::format_date() for original code
      */
     $date_gmt_time = mysql2date('G', $date_string);
     $date_local_timestamp = GFCommon::get_local_timestamp($date_gmt_time);
     $format = rgar($atts, 'format');
     $is_human = !empty($atts['human']);
     $is_diff = !empty($atts['diff']);
     $is_raw = !empty($atts['raw']);
     $is_timestamp = !empty($atts['timestamp']);
     $include_time = !empty($atts['time']);
     // If we're using time diff, we want to have a different default format
     if (empty($format)) {
         /* translators: %s: relative time from now, used for generic date comparisons. "1 day ago", or "20 seconds ago" */
         $format = $is_diff ? esc_html__('%s ago', 'gravityview') : get_option('date_format');
     }
     // If raw was specified, don't modify the stored value
     if ($is_raw) {
         $formatted_date = $date_string;
     } elseif ($is_timestamp) {
         $formatted_date = $date_local_timestamp;
     } elseif ($is_diff) {
         $formatted_date = sprintf($format, human_time_diff($date_gmt_time));
     } else {
         $formatted_date = GFCommon::format_date($date_string, $is_human, $format, $include_time);
     }
     unset($format, $is_diff, $is_human, $is_timestamp, $is_raw, $date_gmt_time, $date_local_timestamp, $default_atts);
     return $formatted_date;
 }
Exemplo n.º 14
0
 public static function start_export($form)
 {
     $form_id = $form["id"];
     $fields = $_POST["export_field"];
     $start_date = $_POST["export_date_start"];
     $end_date = $_POST["export_date_end"];
     //adding default fields
     array_push($form["fields"], array("id" => "id", "label" => __("Entry Id", "gravityforms")));
     array_push($form["fields"], array("id" => "date_created", "label" => __("Entry Date", "gravityforms")));
     array_push($form["fields"], array("id" => "ip", "label" => __("User IP", "gravityforms")));
     array_push($form["fields"], array("id" => "source_url", "label" => __("Source Url", "gravityforms")));
     array_push($form["fields"], array("id" => "payment_status", "label" => __("Payment Status", "gravityforms")));
     array_push($form["fields"], array("id" => "payment_date", "label" => __("Payment Date", "gravityforms")));
     array_push($form["fields"], array("id" => "transaction_id", "label" => __("Transaction Id", "gravityforms")));
     $entry_count = RGFormsModel::get_lead_count($form_id, "", null, null, $start_date, $end_date);
     $page_size = 200;
     $offset = 0;
     //Adding BOM marker for UTF-8
     $lines = chr(239) . chr(187) . chr(191);
     //writing header
     foreach ($fields as $field_id) {
         $field = RGFormsModel::get_field($form, $field_id);
         $value = '"' . str_replace('"', '""', GFCommon::get_label($field, $field_id)) . '"';
         $lines .= "{$value},";
     }
     $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);
         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);
                         break;
                     default:
                         $long_text = "";
                         if (strlen($lead[$field_id]) >= GFORMS_MAX_FIELD_LENGTH) {
                             $long_text = RGFormsModel::get_field_value_long($lead["id"], $field_id);
                         }
                         $value = !empty($long_text) ? $long_text : $lead[$field_id];
                         break;
                 }
                 $lines .= '"' . str_replace('"', '""', $value) . '",';
             }
             $lines = substr($lines, 0, strlen($lines) - 1);
             $lines .= "\n";
         }
         $offset += $page_size;
         $entry_count -= $page_size;
         if (!seems_utf8($lines)) {
             $lines = utf8_encode($lines);
         }
         echo $lines;
         $lines = "";
     }
 }
Exemplo n.º 15
0
 /**
  * Handles the download request from the export entries page.
  *
  * @since 2.0.0
  */
 public static function ajax_download_export()
 {
     check_admin_referer('gform_download_export');
     if (!GFCommon::current_user_can_any('gravityforms_export_entries')) {
         die;
     }
     $form_id = absint(rgget('form-id'));
     if (empty($form_id)) {
         die;
     }
     $form = GFAPI::get_form($form_id);
     if (empty($form)) {
         die;
     }
     $filename = sanitize_title_with_dashes($form['title']) . '-' . gmdate('Y-m-d', GFCommon::get_local_timestamp(time())) . '.csv';
     GFCommon::log_debug(__METHOD__ . '(): Starting download of file: ' . $filename);
     $charset = get_option('blog_charset');
     header('Content-Description: File Transfer');
     header("Content-Disposition: attachment; filename={$filename}");
     header('Content-Type: text/csv; charset=' . $charset, true);
     $buffer_length = ob_get_length();
     //length or false if no buffer
     if ($buffer_length > 1) {
         ob_clean();
     }
     $export_folder = RGFormsModel::get_upload_root() . 'export/';
     $export_id = rgget('export-id');
     $file = $export_folder . sanitize_file_name('export-' . $export_id . '.csv');
     $result = readfile($file);
     if ($result === false) {
         GFCommon::log_error(__METHOD__ . '(): An issue occurred whilst reading the file.');
     } else {
         @unlink($file);
         GFCommon::log_debug(__METHOD__ . '(): Number of bytes read from the file: ' . print_r($result, 1));
     }
     exit;
 }