Skip to content

Commit

Permalink
fix(admin-tool):update code to check for dry run in CSV impress-org#2875
Browse files Browse the repository at this point in the history
  • Loading branch information
raftaar1191 committed Apr 4, 2018
1 parent 86605bd commit 3986308
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
8 changes: 5 additions & 3 deletions includes/admin/admin-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,11 @@ function give_donation_import_callback() {

parse_str( $fields );

// Processing done here.
$raw_data = give_get_donation_data_from_csv( $csv, $start, $end, $delimiter );
$raw_key = maybe_unserialize( $mapto );

$import_setting['raw_key'] = $raw_key;
$import_setting['create_user'] = $create_user;
$import_setting['mode'] = $mode;
$import_setting['delimiter'] = $delimiter;
Expand All @@ -567,9 +572,6 @@ function give_donation_import_callback() {
$delimiter = ',';
}

// Processing done here.
$raw_data = give_get_donation_data_from_csv( $csv, $start, $end, $delimiter );
$raw_key = maybe_unserialize( $mapto );
$current_key = $start;
if ( empty( $import_setting['dry_run'] ) ) {
$csv_raw_data = give_get_donation_data_from_csv( $csv, 1, $end, $delimiter );
Expand Down
60 changes: 40 additions & 20 deletions includes/import-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,36 +588,56 @@ function give_save_import_donation_to_db( $raw_key, $row_data, $main_key = array
$data = array_combine( $raw_key, $row_data );
$price_id = false;
$donor_id = 0;
$donor_data = array();
$form = array();
$import_setting['create_user'] = isset( $import_setting['create_user'] ) ? $import_setting['create_user'] : 1;
$dry_run = isset( $import_setting['dry_run'] ) ? $import_setting['dry_run'] : false;
$is_duplicate = false;

$data = (array) apply_filters( 'give_save_import_donation_to_db', $data );

$data['amount'] = give_maybe_sanitize_amount( $data['amount'] );

// Here come the login function.
$donor_data = give_import_get_user_from_csv( $data, $import_setting );

if ( empty( $dry_run ) ) {
if ( ! empty( $donor_data->id ) ) {
if ( ! empty( $donor_data->user_id ) ) {
$donor_id = $donor_data->user_id;
} elseif ( ! empty( $data['user_id'] ) ) {
$donor_id = $data['user_id'];
if ( ! empty( $dry_run ) ) {
$csv_raw_data = empty( $import_setting['csv_raw_data'] ) ? array() : $import_setting['csv_raw_data'];
$donation_key = empty( $import_setting['donation_key'] ) ? 1 : $import_setting['donation_key'];
for ( $i = 0; $i < $donation_key; $i ++ ) {
$csv_data = array_combine( $raw_key, $csv_raw_data[ $i ] );
$csv_data['amount'] = give_maybe_sanitize_amount( $csv_data['amount'] );
foreach ( $csv_data as $key => $value ) {
if ( $is_duplicate[ $key ] !== $data[ $key ] ) {
$is_duplicate = true;
$report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 );
$report['duplicate_form'] = ( ! empty( $report['duplicate_form'] ) ? ( absint( $report['duplicate_form'] ) + 1 ) : 1 );
break;
}
}
} else {
return false;
}
}

// get form data or register a form data.
$form = give_import_get_form_data_from_csv( $data, $import_setting );
if ( false == $form && empty( $dry_run ) ) {
return false;
} else {
$price_id = ( ! empty( $form->price_id ) ) ? $form->price_id : false;
}
if ( empty( $is_duplicate ) ) {
// Here come the login function.
$donor_data = give_import_get_user_from_csv( $data, $import_setting );
if ( empty( $dry_run ) ) {
if ( ! empty( $donor_data->id ) ) {
if ( ! empty( $donor_data->user_id ) ) {
$donor_id = $donor_data->user_id;
} elseif ( ! empty( $data['user_id'] ) ) {
$donor_id = $data['user_id'];
}
} else {
return false;
}
}

// get form data or register a form data.
$form = give_import_get_form_data_from_csv( $data, $import_setting );
if ( false == $form && empty( $dry_run ) ) {
return false;
} else {
$price_id = ( ! empty( $form->price_id ) ) ? $form->price_id : false;
}
}

$status = give_import_donation_get_status( $data );
$country = ( ! empty( $data['country'] ) ? ( ( $country_code = array_search( $data['country'], give_get_country_list() ) ) ? $country_code : $data['country'] ) : '' );
Expand Down Expand Up @@ -674,7 +694,7 @@ function give_save_import_donation_to_db( $raw_key, $row_data, $main_key = array

// Check for duplicate code.
$donation_duplicate = give_check_import_donation_duplicate( $payment_data, $data, $form, $donor_data );
if ( false !== $donation_duplicate ) {
if ( false !== $donation_duplicate || ! empty( $is_duplicate ) ) {
$report['donation_details'][ $import_setting['donation_key'] ]['duplicate'] = $donation_duplicate;
$report['duplicate_donation'] = ( ! empty( $report['duplicate_donation'] ) ? ( absint( $report['duplicate_donation'] ) + 1 ) : 1 );
} else {
Expand Down Expand Up @@ -881,7 +901,7 @@ function give_check_import_donation_duplicate( $payment_data, $data, $form, $don
),
array(
'key' => '_give_payment_donor_id',
'value' => $donor_data->id,
'value' => isset( $donor_data->id ) ? $donor_data->id : '',
'compare' => '=',
),
),
Expand Down

0 comments on commit 3986308

Please sign in to comment.