| 1 |
<?php |
| 2 |
/** |
| 3 |
* displays the CSV import UI page |
| 4 |
* |
| 5 |
* @version 2.0 |
| 6 |
*/ |
| 7 |
if ( !defined( 'ABSPATH' ) ) |
| 8 |
exit; |
| 9 |
if ( !Participants_Db::current_user_has_plugin_role( 'admin', 'upload csv' ) ) |
| 10 |
exit; |
| 11 |
|
| 12 |
$CSV_import = new PDb_CSV_Import( 'csv_file_upload' ); |
| 13 |
|
| 14 |
$csv_params = PDb_admin_list\csv::csv_options(); |
| 15 |
|
| 16 |
foreach (array_keys( $csv_params ) as $param) { |
| 17 |
$new_value = ''; |
| 18 |
if ( isset( $_POST[$param] ) ) { |
| 19 |
switch ( $param ) { |
| 20 |
case 'enclosure_character': |
| 21 |
$new_value = str_replace( array('"', "'"), array('"', '''), filter_input( INPUT_POST, 'enclosure_character', FILTER_DEFAULT, Participants_Db::string_sanitize() ) ); |
| 22 |
break; |
| 23 |
default: |
| 24 |
$new_value = filter_input( INPUT_POST, $param, FILTER_DEFAULT, Participants_Db::string_sanitize() ); |
| 25 |
} |
| 26 |
$csv_params[$param] = $new_value; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
// update the option |
| 31 |
PDb_admin_list\csv::update_option( $csv_params ); |
| 32 |
|
| 33 |
// make the parameters available as plain variables |
| 34 |
extract( $csv_params ); |
| 35 |
|
| 36 |
// ensure the match field is valid |
| 37 |
if ( !PDb_Form_Field_Def::is_field( $match_field ) ) { |
| 38 |
$match_field = 'id'; |
| 39 |
} |
| 40 |
|
| 41 |
$import_columns = $CSV_import->has_errors() ? $CSV_import->export_columns() : $CSV_import->column_names(); |
| 42 |
$column_count = count( $import_columns ); |
| 43 |
|
| 44 |
?> |
| 45 |
<div class="wrap <?php esc_attr_e( Participants_Db::$prefix ) ?>csv-upload"> |
| 46 |
<?php Participants_Db::admin_page_heading() ?> |
| 47 |
<div id="poststuff"> |
| 48 |
<div id="post-body"> |
| 49 |
<h2><?php esc_html_e( 'Import CSV File', 'participants-database' ) ?></h2> |
| 50 |
|
| 51 |
<?php |
| 52 |
if ( $CSV_import->has_errors() ): |
| 53 |
|
| 54 |
if ( $CSV_import->error_status === 'error' ) |
| 55 |
{ |
| 56 |
Participants_Db::debug_log( 'CSV Import page error: ' . implode( "\n", $CSV_import->get_errors() ), 2 ); |
| 57 |
} |
| 58 |
?> |
| 59 |
|
| 60 |
<div class="<?php echo esc_attr( $CSV_import->error_status ) ?> fade below-h2" id="message"> |
| 61 |
<p><?php echo wp_kses_post( implode( '</p><p>', $CSV_import->get_errors() ) ) ?></p> |
| 62 |
</div> |
| 63 |
|
| 64 |
<?php |
| 65 |
endif; |
| 66 |
?> |
| 67 |
|
| 68 |
<form method="post" action="<?php echo esc_attr( $_SERVER["REQUEST_URI"] ) ?>"> |
| 69 |
<input type="hidden" name="filename" value="blank_record.csv" /> |
| 70 |
<input type="hidden" name="subsource" value="<?php echo esc_attr( Participants_Db::PLUGIN_NAME ) ?>"> |
| 71 |
<input type="hidden" name="action" value="output CSV" /> |
| 72 |
<input type="hidden" name="CSV type" value="blank" /> |
| 73 |
<div class="postbox"> |
| 74 |
<div class="inside"> |
| 75 |
<h3><?php esc_html_e( 'Prepare a spreadsheet file with the correct format:', 'participants-database' ) ?></h3> |
| 76 |
<p><?php esc_html_e( 'To properly import your membership data, the columns in your spreadsheet must match exactly the columns in the database. Currently, the CSV export columns are as follows:', 'participants-database' ) ?></p> |
| 77 |
<div class="spreadsheet-frame"> |
| 78 |
<table class="spreadsheet"> |
| 79 |
<tr> |
| 80 |
<?php |
| 81 |
foreach ( $import_columns as $name ) { |
| 82 |
echo '<th>' . esc_html( $name ) . '</th>'; |
| 83 |
} |
| 84 |
?> |
| 85 |
</tr> |
| 86 |
<tr> |
| 87 |
<?php |
| 88 |
echo str_repeat( '<td> </td>', $column_count ); |
| 89 |
?> |
| 90 |
</tr> |
| 91 |
</table> |
| 92 |
</div> |
| 93 |
<p><?php echo esc_html( sprintf( __( 'This means your spreadsheet needs to have %s columns, and the heading in each of those columns needs to match exactly the names above. If there is no data for a particular column, you can include it and leave it blank, or leave it out entirely. The order of the columns doesn't matter.', 'participants-database' ), $column_count ) ) ?></p> |
| 94 |
<p><?php esc_html_e( 'If the imported CSV file has a different column set, that column set will be imported and used. If a column name does not match a defined column in the database, the import will be aborted and a list of the incorrect column names will be displayed.', 'participants-database' ) ?></p> |
| 95 |
<p><input class="button button-default" type="submit" value="<?php esc_attr_e( 'Get Blank CSV File', 'participants-database' ) ?>" style="float:left;margin:0 5px 5px 0" /><?php esc_html_e( 'You can download this file, then open it in Open Office, Excel or Google Docs.', 'participants-database' ) ?></p> |
| 96 |
</div> |
| 97 |
</div> |
| 98 |
</form> |
| 99 |
<div class="postbox"> |
| 100 |
<div class="inside"> |
| 101 |
<h3><?php esc_html_e( 'Export the .csv file', 'participants-database' ) ?></h3> |
| 102 |
<p><?php esc_html_e( 'When you have your spreadsheet properly set up and filled with data, export it as any of the following: "comma-delimited csv", or just "csv". Save it to your computer then upload it here.', 'participants-database' ) ?></p> |
| 103 |
<h4><?php esc_html_e( 'Exported CSV files should be comma-delimited and enclosed with double-quotes ("). Encoding should be "UTF-8."', 'participants-database' ) ?></h4> |
| 104 |
</div> |
| 105 |
</div> |
| 106 |
<div class="postbox"> |
| 107 |
<div class="inside"> |
| 108 |
<h3><?php esc_html_e( 'Upload the .csv file', 'participants-database' ) ?></h3> |
| 109 |
<form enctype="multipart/form-data" action="<?php esc_attr_e( $_SERVER["REQUEST_URI"] ) ?>" method="POST"> |
| 110 |
<?php wp_nonce_field( PDb_CSV_Import::nonce ) ?> |
| 111 |
<input type="hidden" name="csv_file_upload" id="file_upload" value="true" /> |
| 112 |
<fieldset class="widefat inline-controls settings-row"> |
| 113 |
<p> |
| 114 |
<label> |
| 115 |
<?php _e( 'Enclosure character', 'participants-database' ); ?> |
| 116 |
: |
| 117 |
<?php |
| 118 |
$parameters = array( |
| 119 |
'type' => 'dropdown', |
| 120 |
'name' => 'enclosure_character', |
| 121 |
'value' => $enclosure_character, |
| 122 |
'options' => array( |
| 123 |
PDb_FormElement::null_select_key() => 'false', |
| 124 |
__( 'Auto', 'participants-database' ) => 'auto', |
| 125 |
'"' => '"', |
| 126 |
"'" => "'" |
| 127 |
) |
| 128 |
); |
| 129 |
PDb_FormElement::print_element( $parameters ); |
| 130 |
?> |
| 131 |
</label> |
| 132 |
<label> |
| 133 |
<?php _e( 'Delimiter character', 'participants-database' ); ?> |
| 134 |
: |
| 135 |
<?php $parameters = array( |
| 136 |
'type' => 'dropdown', |
| 137 |
'name' => 'delimiter_character', |
| 138 |
'value' => $delimiter_character, |
| 139 |
'options' => array( |
| 140 |
PDb_FormElement::null_select_key() => 'false', |
| 141 |
__( 'Auto', 'participants-database' ) => 'auto', |
| 142 |
',' => ',', |
| 143 |
';' => ';', |
| 144 |
__( 'tab', 'participants-database' ) => "\t" |
| 145 |
) |
| 146 |
); |
| 147 |
PDb_FormElement::print_element( $parameters ); |
| 148 |
?> |
| 149 |
</label> |
| 150 |
|
| 151 |
|
| 152 |
<label> |
| 153 |
<?php echo __( 'Allow blank value overwrite', 'participants-database' ) . ':'; ?> |
| 154 |
<?php $parameters = array( |
| 155 |
'type' => 'checkbox', |
| 156 |
'name' => 'blank_overwrite', |
| 157 |
'value' => $csv_params['blank_overwrite'], |
| 158 |
'options' => [1,0], |
| 159 |
); |
| 160 |
PDb_FormElement::print_element( $parameters ); |
| 161 |
echo $CSV_import->help_link( 'overwriting' ); |
| 162 |
?> |
| 163 |
</label> |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
</p> |
| 172 |
</fieldset> |
| 173 |
|
| 174 |
<fieldset class="widefat inline-controls"> |
| 175 |
<p> |
| 176 |
<label> |
| 177 |
<?php |
| 178 |
echo __( 'Duplicate Record Preference', 'participants-database' ) . ': '; |
| 179 |
$parameters = array( |
| 180 |
'type' => 'dropdown', |
| 181 |
'name' => 'match_preference', |
| 182 |
'value' => $match_preference, |
| 183 |
'options' => array( |
| 184 |
__( 'Create a new record with the submission', 'participants-database' ) => 'add', |
| 185 |
__( 'Update matching record with new data', 'participants-database' ) => 'update', |
| 186 |
__( "Don't import the record", 'participants-database' ) => 'skip', |
| 187 |
PDb_FormElement::null_select_key() => false, |
| 188 |
) |
| 189 |
); |
| 190 |
PDb_FormElement::print_element( $parameters ); |
| 191 |
?> |
| 192 |
</label> |
| 193 |
</p> |
| 194 |
<p> |
| 195 |
<label> |
| 196 |
<?php |
| 197 |
echo __( 'Duplicate Record Check Field', 'participants-database' ) . ': '; |
| 198 |
$parameters = array( |
| 199 |
'type' => 'dropdown', |
| 200 |
'name' => 'match_field', |
| 201 |
'value' => $match_field, |
| 202 |
'options' => PDb_Settings::_get_identifier_columns( false, array('rich-text', 'multi-checkbox','multi-dropdown','multi-select-other', 'image-upload', 'file-upload', 'password', 'placeholder', 'timestamp','captcha') ), |
| 203 |
); |
| 204 |
PDb_FormElement::print_element( $parameters ); |
| 205 |
?> |
| 206 |
</label> |
| 207 |
</p> |
| 208 |
</fieldset> |
| 209 |
<p><?php _e( '<strong>Note:</strong> Depending on the "Duplicate Record Preference" setting, imported records are checked against existing records by the field set in the "Duplicate Record Check Field" setting. If a record matching an existing record is imported, one of three things can happen, based on the "Duplicate Record Preference" setting:', 'participants-database' ) ?></p> |
| 210 |
<h4 class="inset" id="match-preferences"><?php esc_html_e( 'Current Setting', 'participants-database' ) ?>: |
| 211 |
<?php |
| 212 |
$preferences = array( |
| 213 |
'add' => sprintf( __( '%sCreate New%s adds all imported records as new records without checking for a match.', 'participants-database' ), '<span class="emphasized">', '</span>', '</span>' ), |
| 214 |
'update' => sprintf( __( '%sOverwrite%s an existing record with a matching %s will be updated with the data from the imported record. Blank or missing fields will not overwrite existing data.', 'participants-database' ), '<span class="emphasized">', '</span>', '<em class="match-field">' . Participants_Db::$fields[$match_field]->title() . '</em>' ), |
| 215 |
'skip' => sprintf( __( '%sDon't Import%s does not import the new record if it matches the %s of an existing one.', 'participants-database' ), '<span class="emphasized">', '</span>', '<em class="match-field">' . Participants_Db::$fields[$match_field]->title() . '</em>' ), |
| 216 |
); |
| 217 |
foreach ( $preferences as $i => $preference ) { |
| 218 |
$hide = $i == $match_preference ? '' : 'style="display:none"'; |
| 219 |
printf( '<span class="preference" %s data-index="%s" >%s</span>', $hide, $i, $preference ); |
| 220 |
} |
| 221 |
?></h4> |
| 222 |
|
| 223 |
|
| 224 |
<?php _e( 'Choose .csv file to import:', 'participants-database' ) ?> <input name="<?php esc_attr_e( PDb_CSV_Import::csv_field ) ?>" type="file" /> |
| 225 |
<p><input type="submit" id="csv-upload-submit" class="button button-primary" value="<?php esc_attr_e( 'Upload File', 'participants-database' ) ?>" />  |
| 226 |
<?php if (Participants_Db::plugin_setting_is_true( 'background_import', true ) ) |
| 227 |
{ |
| 228 |
_e('Records will be imported in the background. You may leave this page and come back, the import will continue.','participants-database'); |
| 229 |
} else { |
| 230 |
_e('Records will be imported as the file is uploaded, this may take some time if the file is large.', 'participants-database'); |
| 231 |
echo '<span class="csv-import-spinner">' . Participants_Db::get_loading_spinner() . '</span>'; |
| 232 |
} ?> |
| 233 |
</p> |
| 234 |
</form> |
| 235 |
</div> |
| 236 |
</div> |
| 237 |
</div> |
| 238 |
</div> |
| 239 |
</div> |
| 240 |
<script type="text/javascript"> |
| 241 |
UploadCSV = (function ($) { |
| 242 |
var |
| 243 |
prefs, |
| 244 |
matchfield, |
| 245 |
set_visible_pref = function (i) { |
| 246 |
hide_prefs(); |
| 247 |
show_pref(i); |
| 248 |
}, |
| 249 |
hide_prefs = function () { |
| 250 |
prefs.find('.preference').hide(); |
| 251 |
}, |
| 252 |
show_pref = function (i) { |
| 253 |
prefs.find('.preference[data-index=' + i + ']').show(); |
| 254 |
}, |
| 255 |
set_pref = function () { |
| 256 |
set_visible_pref($(this).val()); |
| 257 |
}, |
| 258 |
set_match_field_text = function (f) { |
| 259 |
matchfield.text(f); |
| 260 |
}, |
| 261 |
set_match_field = function () { |
| 262 |
set_match_field_text($(this).find('option:selected').text()); |
| 263 |
}; |
| 264 |
return { |
| 265 |
run : function () { |
| 266 |
prefs = $('#match-preferences'); |
| 267 |
matchfield = prefs.find('.match-field'); |
| 268 |
$('[id^="pdb-match_preference"]').change(set_pref); |
| 269 |
$('[id^="pdb-match_field"]').change(set_match_field); |
| 270 |
$('#csv-upload-submit').on('click', () => $('.csv-import-spinner .ajax-loading').css('visibility', 'visible')); |
| 271 |
} |
| 272 |
} |
| 273 |
}(jQuery)); |
| 274 |
jQuery(function () { |
| 275 |
"use strict"; |
| 276 |
UploadCSV.run(); |
| 277 |
}); |
| 278 |
</script> |
| 279 |
<style> |
| 280 |
.csv-import-spinner .ajax-loading { |
| 281 |
visibility: hidden; |
| 282 |
display: inline-block; |
| 283 |
vertical-align: middle; |
| 284 |
} |
| 285 |
.csv-import-spinner { |
| 286 |
margin-left: 1em; |
| 287 |
} |
| 288 |
progress { |
| 289 |
-webkit-appearance:none; |
| 290 |
-moz-appearance:none; |
| 291 |
appearance: none; |
| 292 |
border-radius: 3px; |
| 293 |
border: 1px solid #1d232733; |
| 294 |
width: 100%; |
| 295 |
} |
| 296 |
progress::-webkit-progress-value, |
| 297 |
progress::-moz-progress-bar{ |
| 298 |
background-color: #2271b1; |
| 299 |
} |
| 300 |
progress.complete::-webkit-progress-value, |
| 301 |
progress.complete::-moz-progress-bar{ |
| 302 |
background-color: #00a32a; |
| 303 |
} |
| 304 |
progress::-webkit-progress-value, |
| 305 |
progress::-webkit-progress-bar { |
| 306 |
border-radius: 3px; |
| 307 |
} |
| 308 |
#message .progressbar { |
| 309 |
position: relative; |
| 310 |
} |
| 311 |
#message .dashicons.dashicons-flag { |
| 312 |
color: #00a32a; |
| 313 |
} |
| 314 |
</style> |