Skip to content

Commit

Permalink
Merge pull request #3732 from impress-org/issue/3709
Browse files Browse the repository at this point in the history
feat: include custom columns in donor export CSV #3709
  • Loading branch information
ravinderk committed Oct 5, 2018
2 parents 069538e + 4076de8 commit 36c954f
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 124 deletions.
49 changes: 15 additions & 34 deletions includes/admin/tools/export/class-batch-export-donors.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,8 @@ private function cache_donor_ids() {
*/
public function csv_cols() {

$columns = isset( $this->data['give_export_option'] ) ? $this->data['give_export_option'] : array();

// We need columns.
if ( empty( $columns ) ) {
return false;
}

$cols = $this->get_cols( $columns );
$columns = give_export_donors_get_default_columns();
$cols = $this->get_cols( $columns );

return $cols;
}
Expand All @@ -185,12 +179,7 @@ private function get_cols( $columns ) {
foreach ( $columns as $key => $value ) {

switch ( $key ) {
case 'full_name' :
$cols['full_name'] = esc_html__( 'Full Name', 'give' );
break;
case 'email' :
$cols['email'] = esc_html__( 'Email Address', 'give' );
break;

case 'address' :
$cols['address_line1'] = esc_html__( 'Address', 'give' );
$cols['address_line2'] = esc_html__( 'Address 2', 'give' );
Expand All @@ -199,17 +188,9 @@ private function get_cols( $columns ) {
$cols['address_zip'] = esc_html__( 'Zip', 'give' );
$cols['address_country'] = esc_html__( 'Country', 'give' );
break;
case 'userid' :
$cols['userid'] = esc_html__( 'User ID', 'give' );
break;
case 'donor_created_date' :
$cols['donor_created_date'] = esc_html__( 'Donor Created Date', 'give' );
break;
case 'donations' :
$cols['donations'] = esc_html__( 'Number of Donations', 'give' );
break;
case 'donation_sum' :
$cols['donation_sum'] = esc_html__( 'Sum of Donations', 'give' );

default:
$cols[ $key ] = $value;
break;
}
}
Expand All @@ -234,9 +215,9 @@ public function get_data() {

if ( ! empty( $this->form ) ) {

// Export donors for a specific donation form and also within specified timeframe
// Export donors for a specific donation form and also within specified timeframe.
$args = array(
'output' => 'payments', // Use 'posts' to get standard post objects
'output' => 'payments',
'post_type' => array( 'give_payment' ),
'number' => 30,
'paged' => $this->step,
Expand All @@ -245,7 +226,7 @@ public function get_data() {
'meta_value' => absint( $this->form ),
);

// Check for date option filter
// Check for date option filter.
if ( ! empty( $this->data['donor_export_start_date'] ) || ! empty( $this->data['donor_export_end_date'] ) ) {
$args['start_date'] = ! empty( $this->data['donor_export_start_date'] ) ? date( 'Y-n-d 00:00:00', strtotime( $this->data['donor_export_start_date'] ) ) : date( 'Y-n-d 23:59:59', '1970-1-01 00:00:00' );
$args['end_date'] = ! empty( $this->data['donor_export_end_date'] ) ? date( 'Y-n-d 23:59:59', strtotime( $this->data['donor_export_end_date'] ) ) : date( 'Y-n-d 23:59:59', current_time( 'timestamp' ) );
Expand Down Expand Up @@ -304,7 +285,7 @@ public function get_data() {
// Cache donor ids only if admin export donor for specific form.
$this->cache_donor_ids();
}
}
} // End if().
} else {

// Export all donors.
Expand All @@ -315,7 +296,7 @@ public function get_data() {
'offset' => $offset,
);

// Check for date option filter
// Check for date option filter.
if ( ! empty( $this->data['donor_export_start_date'] ) || ! empty( $this->data['donor_export_end_date'] ) ) {
$args['date'] = array(
'start' => ! empty( $this->data['donor_export_start_date'] ) ? date( 'Y-n-d 00:00:00', strtotime( $this->data['donor_export_start_date'] ) ) : date( 'Y-n-d 23:59:59', '1970-1-01 00:00:00' ),
Expand Down Expand Up @@ -387,11 +368,11 @@ private function set_donor_data( $i, $data, $donor ) {

// Set address variable.
$address = '';
if ( isset( $donor->user_id ) && $donor->user_id > 0 ) {
$address = give_get_donor_address( $donor->user_id );
if ( isset( $donor->id ) && $donor->id > 0 ) {
$address = give_get_donor_address( $donor->id );
}

// Set columns
// Set columns.
if ( ! empty( $columns['full_name'] ) ) {
$donor_name = give_get_donor_name_by( $donor->id, 'donor' );
$data[ $i ]['full_name'] = $donor_name;
Expand Down Expand Up @@ -438,4 +419,4 @@ public function unset_properties( $request, $export ) {
Give_Cache::delete( "give_cache_{$this->query_id}" );
}
}
}
}
33 changes: 32 additions & 1 deletion includes/admin/tools/export/export-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function give_do_ajax_export() {
);

} else {

$args = array_merge( $_REQUEST, array(
'step' => $step,
'class' => $class,
Expand All @@ -119,3 +119,34 @@ function give_do_ajax_export() {
}

add_action( 'wp_ajax_give_do_ajax_export', 'give_do_ajax_export' );


/**
* This function is used to define default columns for export.
*
* Note: This function is for internal purposes only.
* Use filter "give_export_donors_get_default_columns" instead.
*
* @since 2.2.6
*
* @return array
*/
function give_export_donors_get_default_columns() {

$default_columns = array(
'full_name' => __( 'Name', 'give' ),
'email' => __( 'Email', 'give' ),
'address' => __( 'Address', 'give' ),
'userid' => __( 'User ID', 'give' ),
'donor_created_date' => __( 'Donor Created Date', 'give' ),
'donations' => __( 'Number of donations', 'give' ),
'donation_sum' => __( 'Total Donated', 'give' ),
);

/**
* This filter will be used to define default columns for export.
*
* @since 2.2.6
*/
return apply_filters( 'give_export_donors_get_default_columns', $default_columns );
}

0 comments on commit 36c954f

Please sign in to comment.