| 1 |
<?php // phpcs:ignore Standard.Category.SniffName.ErrorCode |
| 2 |
|
| 3 |
namespace WPDataAccess\CSV_Files { |
| 4 |
|
| 5 |
use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Lists; |
| 6 |
use WPDataAccess\Plugin_Table_Models\WPDA_CSV_Uploads_Model; |
| 7 |
use WPDataAccess\WPDA; |
| 8 |
|
| 9 |
/** |
| 10 |
* CSV mapping class |
| 11 |
*/ |
| 12 |
class WPDA_CSV_Mapping { |
| 13 |
|
| 14 |
|
| 15 |
/** |
| 16 |
* Delimiters |
| 17 |
*/ |
| 18 |
const DELIMITER = array( |
| 19 |
'comma' => ',', |
| 20 |
'semicolon' => ';', |
| 21 |
'tab' => "\t", |
| 22 |
'pipe' => '|', |
| 23 |
'colon' => ':', |
| 24 |
); |
| 25 |
/** |
| 26 |
* Mapping |
| 27 |
* |
| 28 |
* @var mixed|null |
| 29 |
*/ |
| 30 |
protected $mapping = null; |
| 31 |
|
| 32 |
/** |
| 33 |
* Actual delimiter |
| 34 |
* |
| 35 |
* @var mixed|string |
| 36 |
*/ |
| 37 |
protected $delimiter = ','; |
| 38 |
/** |
| 39 |
* Data format |
| 40 |
* |
| 41 |
* @var mixed|string |
| 42 |
*/ |
| 43 |
protected $date_format = '%Y-%m-%d'; |
| 44 |
/** |
| 45 |
* Indicates if header columns are available |
| 46 |
* |
| 47 |
* @var bool |
| 48 |
*/ |
| 49 |
protected $has_header_columns = true; |
| 50 |
|
| 51 |
/** |
| 52 |
* CSV ID |
| 53 |
* |
| 54 |
* @var string|null |
| 55 |
*/ |
| 56 |
protected $csv_id = null; |
| 57 |
/** |
| 58 |
* CSV name |
| 59 |
* |
| 60 |
* @var string |
| 61 |
*/ |
| 62 |
protected $csv_name = ''; |
| 63 |
/** |
| 64 |
* CSV upload |
| 65 |
* |
| 66 |
* @var null |
| 67 |
*/ |
| 68 |
protected $csv_upload = null; |
| 69 |
|
| 70 |
/** |
| 71 |
* Nonce |
| 72 |
* |
| 73 |
* @var null |
| 74 |
*/ |
| 75 |
protected $wpnonce = null; |
| 76 |
/** |
| 77 |
* Preview nonce |
| 78 |
* |
| 79 |
* @var null |
| 80 |
*/ |
| 81 |
protected $wpnonce_preview = null; |
| 82 |
|
| 83 |
/** |
| 84 |
* Database schema name |
| 85 |
* |
| 86 |
* @var array |
| 87 |
*/ |
| 88 |
protected $db_schema_name = array(); |
| 89 |
/** |
| 90 |
* Schema name mapping |
| 91 |
* |
| 92 |
* @var mixed|null |
| 93 |
*/ |
| 94 |
protected $schema_name_mapping = null; |
| 95 |
|
| 96 |
/** |
| 97 |
* Constructor |
| 98 |
*/ |
| 99 |
public function __construct() { |
| 100 |
$this->csv_id = |
| 101 |
isset( $_REQUEST['csv_id'] ) ? |
| 102 |
sanitize_text_field( wp_unslash( $_REQUEST['csv_id'] ) ) : ''; // input var okay. |
| 103 |
|
| 104 |
if ( null === $this->csv_id ) { |
| 105 |
wp_die( __( 'ERROR: Missing argument', 'wp-data-access' ) ); // phpcs:ignore WordPress.Security.EscapeOutput |
| 106 |
} |
| 107 |
|
| 108 |
$wp_nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : ''; // input var okay. |
| 109 |
if ( ! wp_verify_nonce( $wp_nonce, "wpda-mapping-{$this->csv_id}" ) ) { |
| 110 |
wp_die( __( 'ERROR: Not authorized', 'wp-data-access' ) ); // phpcs:ignore WordPress.Security.EscapeOutput |
| 111 |
} |
| 112 |
|
| 113 |
$this->csv_upload = WPDA_CSV_Uploads_Model::query( $this->csv_id ); |
| 114 |
if ( isset( $this->csv_upload[0]->csv_name ) ) { |
| 115 |
$this->csv_name = $this->csv_upload[0]->csv_name; |
| 116 |
} |
| 117 |
if ( isset( $this->csv_upload[0]->csv_mapping ) ) { |
| 118 |
$this->mapping = json_decode( $this->csv_upload[0]->csv_mapping, true ); |
| 119 |
if ( isset( $this->mapping['settings']['delimiter'] ) ) { |
| 120 |
$this->delimiter = $this->mapping['settings']['delimiter']; |
| 121 |
if ( '\\t' === $this->delimiter ) { |
| 122 |
$this->delimiter = "\t"; |
| 123 |
} |
| 124 |
} |
| 125 |
if ( isset( $this->mapping['settings']['date_format'] ) ) { |
| 126 |
$this->date_format = $this->mapping['settings']['date_format']; |
| 127 |
} |
| 128 |
if ( |
| 129 |
isset( $this->mapping['settings']['has_header_columns'] ) && |
| 130 |
'false' === $this->mapping['settings']['has_header_columns'] |
| 131 |
) { |
| 132 |
$this->has_header_columns = false; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
$this->db_schema_name = WPDA_Dictionary_Lists::get_db_schemas(); |
| 137 |
|
| 138 |
if ( isset( $this->mapping['database']['wpdaschema_name'] ) ) { |
| 139 |
$this->schema_name_mapping = $this->mapping['database']['wpdaschema_name']; |
| 140 |
} else { |
| 141 |
$this->schema_name_mapping = WPDA::get_user_default_scheme(); |
| 142 |
} |
| 143 |
|
| 144 |
$this->wpnonce = wp_create_nonce( "wpda-csv-mapping-{$this->csv_id}" ); |
| 145 |
$this->wpnonce_preview = wp_create_nonce( "wpda-csv-preview-mapping-{$this->csv_id}" ); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Show mapping page |
| 150 |
* |
| 151 |
* @return void |
| 152 |
*/ |
| 153 |
public function show() { |
| 154 |
$upload_dir = WPDA::get_plugin_upload_dir(); |
| 155 |
if ( ! isset( $this->csv_upload[0]->csv_real_file_name ) ) { |
| 156 |
wp_die( __( 'ERROR: CSV file not found', 'wp-data-access' ) ); // phpcs:ignore WordPress.Security.EscapeOutput |
| 157 |
} |
| 158 |
$file_name = $upload_dir . $this->csv_upload[0]->csv_real_file_name; |
| 159 |
|
| 160 |
$table_list = WPDA_Dictionary_Lists::get_tables( false, $this->schema_name_mapping ); |
| 161 |
|
| 162 |
$wp_nonce = esc_attr( wp_create_nonce( "wpda-import-csv-{$this->csv_id}" ) ); |
| 163 |
$wp_nonce_mapping = esc_attr( wp_create_nonce( "wpda-mapping-{$this->csv_id}" ) ); |
| 164 |
?> |
| 165 |
<style type="text/css"> |
| 166 |
#csv_table_columns, .csv_column { |
| 167 |
display: inline-block; |
| 168 |
min-width: 200px; |
| 169 |
margin: 0; |
| 170 |
} |
| 171 |
#csv_table_columns { |
| 172 |
min-height: 100px; |
| 173 |
} |
| 174 |
.csv_column { |
| 175 |
height: 28px; |
| 176 |
} |
| 177 |
#csv_table_columns li, .csv_column li { |
| 178 |
padding: 0 8px; |
| 179 |
line-height: 2; |
| 180 |
min-height: 28px; |
| 181 |
box-shadow: 0 0 0 transparent; |
| 182 |
border-radius: 4px; |
| 183 |
border: 1px solid #7e8993; |
| 184 |
background-color: #fff; |
| 185 |
color: #32373c; |
| 186 |
margin-bottom: 2px; |
| 187 |
} |
| 188 |
#wpda_csv_mapping th { |
| 189 |
text-align: right; |
| 190 |
} |
| 191 |
#wpda_csv_mapping th, #wpda_csv_mapping td { |
| 192 |
padding: 2px; |
| 193 |
} |
| 194 |
.wpda_highlight { |
| 195 |
background-color: yellow !important; |
| 196 |
} |
| 197 |
.wpda_receive { |
| 198 |
background-color: lightblue !important; |
| 199 |
} |
| 200 |
.wpda_csv_column_init { |
| 201 |
border: none !important; |
| 202 |
} |
| 203 |
</style> |
| 204 |
<script type="text/javascript"> |
| 205 |
var mapping = []; |
| 206 |
<?php |
| 207 |
if ( null !== $this->mapping ) { |
| 208 |
$mapping_to_js = wp_json_encode( $this->mapping ); |
| 209 |
echo 'var mapping = ' . $mapping_to_js . ";\n"; // phpcs:ignore WordPress.Security.EscapeOutput |
| 210 |
} |
| 211 |
?> |
| 212 |
|
| 213 |
function wpda_get_tables() { |
| 214 |
var schema_name = jQuery('#csv_schema_name').val(); |
| 215 |
|
| 216 |
var url = location.pathname + '?action=wpda_get_tables&hideviews=TRUE'; |
| 217 |
var data = { |
| 218 |
wpdaschema_name: schema_name, |
| 219 |
wpda_wpnonce: '<?php echo esc_attr( wp_create_nonce( 'wpda-getdata-access-' . WPDA::get_current_user_login() ) ); ?>' |
| 220 |
}; |
| 221 |
jQuery.post( |
| 222 |
url, |
| 223 |
data, |
| 224 |
function (data) { |
| 225 |
jQuery('#csv_table_name').find('option').remove(); |
| 226 |
var jsonData = JSON.parse(data); |
| 227 |
for (i = 0; i < jsonData.length; i++) { |
| 228 |
jQuery('#csv_table_name').append( |
| 229 |
jQuery("<option></option>") |
| 230 |
.attr("value", jsonData[i]['table_name']) |
| 231 |
.text(jsonData[i]['table_name']) |
| 232 |
); |
| 233 |
} |
| 234 |
jQuery('#csv_table_name').trigger("change"); |
| 235 |
} |
| 236 |
); |
| 237 |
} |
| 238 |
|
| 239 |
function wpda_get_columns(init=false) { |
| 240 |
var schema_name = jQuery('#csv_schema_name').val(); |
| 241 |
var table_name = jQuery('#csv_table_name').val(); |
| 242 |
|
| 243 |
var url = location.pathname + '?action=wpda_get_columns'; |
| 244 |
var data = { |
| 245 |
wpdaschema_name: schema_name, |
| 246 |
table_name: table_name, |
| 247 |
wpda_wpnonce: '<?php echo esc_attr( wp_create_nonce( 'wpda-getdata-access-' . WPDA::get_current_user_login() ) ); ?>' |
| 248 |
}; |
| 249 |
jQuery.post( |
| 250 |
url, |
| 251 |
data, |
| 252 |
function (data) { |
| 253 |
var jsonData = JSON.parse(data); |
| 254 |
jQuery('#csv_table_columns').empty(); |
| 255 |
if (!init) { |
| 256 |
jQuery('.csv_column').empty().append( |
| 257 |
'<li class="wpda_csv_column_init"><?php echo __( 'Drag column here...', 'wp-data-access' ); // phpcs:ignore WordPress.Security.EscapeOutput ?></li>' |
| 258 |
); |
| 259 |
} |
| 260 |
for (i = 0; i < jsonData.length; i++) { |
| 261 |
add_column = true; |
| 262 |
if (init) { |
| 263 |
for (var column in mapping.columns) { |
| 264 |
if (mapping.columns[column]===jsonData[i]['column_name']) { |
| 265 |
add_column = false; |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
if (add_column) { |
| 270 |
jQuery('#csv_table_columns').append( |
| 271 |
jQuery("<li></li>") |
| 272 |
.attr("value", jsonData[i]['column_name']) |
| 273 |
.text(jsonData[i]['column_name']) |
| 274 |
); |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
); |
| 279 |
} |
| 280 |
|
| 281 |
function save_mapping(refresh=false, exclude_mapping=false) { |
| 282 |
var dbs_schema_name = jQuery('#csv_schema_name').val(); |
| 283 |
var dbs_table_name = jQuery('#csv_table_name').val(); |
| 284 |
|
| 285 |
let delimiter = jQuery('#csv_delimiter').val(); |
| 286 |
if (delimiter==="\t") { |
| 287 |
delimiter = "\\t"; |
| 288 |
} |
| 289 |
var settings = { |
| 290 |
delimiter: delimiter, |
| 291 |
date_format: jQuery('#csv_date_format').val(), |
| 292 |
has_header_columns: jQuery('#csv_has_header_columns').is(':checked') |
| 293 |
}; |
| 294 |
var database = { |
| 295 |
wpdaschema_name: dbs_schema_name, |
| 296 |
table_name: dbs_table_name |
| 297 |
}; |
| 298 |
var columns = {}; |
| 299 |
|
| 300 |
i = 0; |
| 301 |
jQuery('ul.csv_column_mapped').each(function() { |
| 302 |
csv_column_name = jQuery(this).attr('data-csv-column-name'); |
| 303 |
if (csv_column_name!==undefined) { |
| 304 |
dbs_column_name = jQuery(this).find('li').first().text().trim(); |
| 305 |
if (dbs_column_name!=='' && |
| 306 |
dbs_column_name!=='<?php echo __( 'Drag column here...', 'wp-data-access' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>' |
| 307 |
) { |
| 308 |
columns[i] = dbs_column_name; |
| 309 |
} |
| 310 |
} |
| 311 |
i++; |
| 312 |
}); |
| 313 |
|
| 314 |
if (exclude_mapping) { |
| 315 |
var new_mapping = { |
| 316 |
settings: settings, |
| 317 |
database: mapping.database, |
| 318 |
columns: mapping.columns |
| 319 |
}; |
| 320 |
} else { |
| 321 |
var new_mapping = { |
| 322 |
settings: settings, |
| 323 |
database: database, |
| 324 |
columns: columns |
| 325 |
}; |
| 326 |
} |
| 327 |
|
| 328 |
jQuery.ajax({ |
| 329 |
method: 'POST', |
| 330 |
url: location.pathname + "?action=wpda_save_csv_mapping", |
| 331 |
data: { |
| 332 |
wpnonce: '<?php echo esc_attr( $this->wpnonce ); ?>', |
| 333 |
csv_id: '<?php echo esc_attr( $this->csv_id ); ?>', |
| 334 |
csv_mapping: new_mapping |
| 335 |
} |
| 336 |
}).done( |
| 337 |
function(msg) { |
| 338 |
if (msg==='UPD-0' || msg==='UPD-1') { |
| 339 |
alert('Mapping successfully updated'); |
| 340 |
if (refresh) { |
| 341 |
jQuery('#change_delimiter').submit(); |
| 342 |
} |
| 343 |
} else { |
| 344 |
alert('ERROR: ' + msg); |
| 345 |
} |
| 346 |
} |
| 347 |
); |
| 348 |
} |
| 349 |
|
| 350 |
function change_delimiter() { |
| 351 |
save_mapping(true, true); |
| 352 |
} |
| 353 |
|
| 354 |
function preview(page_number, page_length) { |
| 355 |
if (page_number<1) { |
| 356 |
page_number = 1; |
| 357 |
} |
| 358 |
jQuery.ajax({ |
| 359 |
type: 'POST', |
| 360 |
url: location.pathname + '?action=wpda_csv_preview_mapping', |
| 361 |
data: { |
| 362 |
csv_id: '<?php echo esc_attr( $this->csv_id ); ?>', |
| 363 |
wpnonce: '<?php echo esc_attr( $this->wpnonce_preview ); ?>', |
| 364 |
page_number: page_number, |
| 365 |
page_length: page_length |
| 366 |
} |
| 367 |
}).done( |
| 368 |
function(msg) { |
| 369 |
jQuery('#wpda_csv_preview_table').empty().append(msg); |
| 370 |
} |
| 371 |
); |
| 372 |
} |
| 373 |
|
| 374 |
function toggle_preview() { |
| 375 |
jQuery('#wpda_csv_preview').toggle(); |
| 376 |
if ( jQuery('#wpda_csv_preview').is(":visible") ) { |
| 377 |
jQuery('#wpda_preview_visible').hide(); |
| 378 |
jQuery('#wpda_preview_hidden').show(); |
| 379 |
} else { |
| 380 |
jQuery('#wpda_preview_visible').show(); |
| 381 |
jQuery('#wpda_preview_hidden').hide(); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
var wpda_moving_item = ''; |
| 386 |
|
| 387 |
jQuery(function() { |
| 388 |
wpda_get_columns(true); |
| 389 |
|
| 390 |
jQuery('#csv_table_columns').sortable({ |
| 391 |
connectWith: 'ul', |
| 392 |
start: function( event, ui ) { |
| 393 |
jQuery(ui.item).addClass("wpda_highlight"); |
| 394 |
}, |
| 395 |
stop: function( event, ui ) { |
| 396 |
jQuery(ui.item).removeClass("wpda_highlight"); |
| 397 |
}, |
| 398 |
receive: function( event, ui ) { |
| 399 |
if (wpda_moving_item.childElementCount===0) { |
| 400 |
jQuery(wpda_moving_item).empty().append( |
| 401 |
'<li class="wpda_csv_column_init"><?php echo __( 'Drag column here...', 'wp-data-access' ); // phpcs:ignore WordPress.Security.EscapeOutput ?></li>' |
| 402 |
); |
| 403 |
} |
| 404 |
} |
| 405 |
}); |
| 406 |
|
| 407 |
jQuery('.csv_column').sortable({ |
| 408 |
connectWith: 'ul', |
| 409 |
start: function( event, ui ) { |
| 410 |
wpda_moving_item = event.target; |
| 411 |
}, |
| 412 |
stop: function( event, ui ) { |
| 413 |
wpda_moving_item = ''; |
| 414 |
}, |
| 415 |
over: function( event, ui ) { |
| 416 |
jQuery(event.target).closest('.csv_column_selected').find('.csv_column_selected_cell').addClass("wpda_receive"); |
| 417 |
}, |
| 418 |
out: function( event, ui ) { |
| 419 |
jQuery(event.target).closest('.csv_column_selected').find('.csv_column_selected_cell').removeClass("wpda_receive"); |
| 420 |
}, |
| 421 |
receive: function( event, ui ) { |
| 422 |
jQuery(event.target).find('.wpda_csv_column_init').remove(); |
| 423 |
if (jQuery(event.target)[0].children.length===2) { |
| 424 |
if (wpda_moving_item!=='') { |
| 425 |
jQuery(wpda_moving_item).append(ui.item[0]); |
| 426 |
} else { |
| 427 |
if (jQuery(event.target)[0].firstChild===ui.item[0]) { |
| 428 |
jQuery('#csv_table_columns').append(jQuery(event.target)[0].lastChild); |
| 429 |
} else { |
| 430 |
jQuery('#csv_table_columns').append(jQuery(event.target)[0].firstChild); |
| 431 |
} |
| 432 |
} |
| 433 |
} |
| 434 |
if (wpda_moving_item.childElementCount===0) { |
| 435 |
jQuery(wpda_moving_item).append( |
| 436 |
'<li class="wpda_csv_column_init"><?php echo __( 'Drag column here...', 'wp-data-access' ); // phpcs:ignore WordPress.Security.EscapeOutput ?></li>' |
| 437 |
); |
| 438 |
} |
| 439 |
} |
| 440 |
}); |
| 441 |
|
| 442 |
preview(1, 5); |
| 443 |
}); |
| 444 |
</script> |
| 445 |
<form method="post" action="?page=wpda&page_action=wpda_import_csv"> |
| 446 |
<br/> |
| 447 |
<fieldset class="wpda_fieldset"> |
| 448 |
<legend> |
| 449 |
<?php echo __( 'Settings', 'wp-data-access' ); // phpcs:ignore WordPress.Security.EscapeOutput ?> |
| 450 |
</legend> |
| 451 |
<div id="wpda_csv_settings"> |
| 452 |
<label for="csv_delimiter" style="line-height: 2; vertical-align: text-top;"> |
| 453 |
<span>Field separator</span> |
| 454 |
<select id="csv_delimiter" name="csv_table_name" style="margin-left: 0; vertical-align: top;" onchange="wpda_get_columns()"> |
| 455 |
<?php |
| 456 |
foreach ( self::DELIMITER as $key => $val ) { |
| 457 |
$selected = $this->delimiter === $val ? ' selected' : ''; |
| 458 |
echo '<option value="' . esc_attr( $val ) . '" ' . esc_attr( $selected ) . '>' . esc_attr( $key ) . '</option>'; |
| 459 |
} |
| 460 |
?> |
| 461 |
</select> |
| 462 |
</label> |
| 463 |
|
| 464 |
<label for="csv_has_header_columns" style="line-height: 2; vertical-align: text-top;"> |
| 465 |
<span>Date format</span> |
| 466 |
<select id="csv_date_format" name="csv_date_format"> |
| 467 |
<option value="%Y-%m-%d" <?php echo '%Y-%m-%d' === $this->date_format ? 'selected' : ''; ?>>yyyy-mm-dd</option> |
| 468 |
<option value="%Y/%m/%d" <?php echo '%Y/%m/%d' === $this->date_format ? 'selected' : ''; ?>>yyyy/mm/dd</option> |
| 469 |
<option value="%d-%m-%Y" <?php echo '%d-%m-%Y' === $this->date_format ? 'selected' : ''; ?>>dd-mm-yyyy</option> |
| 470 |
<option value="%d/%m/%Y" <?php echo '%d/%m/%Y' === $this->date_format ? 'selected' : ''; ?>>dd/mm/yyyy</option> |
| 471 |
<option value="%m-%d-%Y" <?php echo '%m-%d-%Y' === $this->date_format ? 'selected' : ''; ?>>mm-dd-yyyy</option> |
| 472 |
<option value="%m/%d/%Y" <?php echo '%m/%d/%Y' === $this->date_format ? 'selected' : ''; ?>>mm/dd/yyyy</option> |
| 473 |
</select> |
| 474 |
</label> |
| 475 |
|
| 476 |
<label for="csv_has_header_columns" style="line-height: 2; vertical-align: text-top; padding-right:15px;"> |
| 477 |
<input id="csv_has_header_columns" |
| 478 |
name="csv_has_header_columns" |
| 479 |
type="checkbox" |
| 480 |
<?php echo $this->has_header_columns ? 'checked' : ''; ?> |
| 481 |
/> |
| 482 |
<span>Has header columns</span> |
| 483 |
</label> |
| 484 |
|
| 485 |
<button type="button" class="button" onclick="change_delimiter()"> |
| 486 |
<i class="fas fa-check wpda_icon_on_button"></i> |
| 487 |
<?php echo __( 'Apply settings' ); // phpcs:ignore WordPress.Security.EscapeOutput ?> |
| 488 |
</button> |
| 489 |
</div> |
| 490 |
</fieldset> |
| 491 |
<br/> |
| 492 |
<fieldset class="wpda_fieldset"> |
| 493 |
<legend> |
| 494 |
<?php echo __( 'Destination database and table', 'wp-data-access' ); // phpcs:ignore WordPress.Security.EscapeOutput ?> |
| 495 |
</legend> |
| 496 |
<div> |
| 497 |
<select id="csv_schema_name" name="csv_schema_name" style="margin-left: 0; vertical-align: top;" onchange="wpda_get_tables()"> |
| 498 |
<?php |
| 499 |
foreach ( $this->db_schema_name as $db_schema_name ) { |
| 500 |
$selected = $this->schema_name_mapping === $db_schema_name['schema_name'] ? 'selected' : ''; |
| 501 |
echo '<option value="' . esc_attr( $db_schema_name['schema_name'] ) . '" ' . esc_attr( $selected ) . '>' . |
| 502 |
esc_attr( $db_schema_name['schema_name'] ) . |
| 503 |
'</option>'; |
| 504 |
} |
| 505 |
?> |
| 506 |
</select> |
| 507 |
<br/> |
| 508 |
<select id="csv_table_name" name="csv_table_name" style="margin-left: 0; vertical-align: top;" onchange="wpda_get_columns()"> |
| 509 |
<?php |
| 510 |
foreach ( $table_list as $table ) { |
| 511 |
$selected = ''; |
| 512 |
if ( isset( $this->mapping['database']['table_name'] ) ) { |
| 513 |
$selected = $this->mapping['database']['table_name'] === $table['table_name'] ? ' selected' : ''; |
| 514 |
} |
| 515 |
echo '<option value="' . esc_attr( $table['table_name'] ) . '" ' . esc_attr( $selected ) . '>' . |
| 516 |
esc_attr( $table['table_name'] ) . |
| 517 |
'</option>'; |
| 518 |
} |
| 519 |
?> |
| 520 |
</select> |
| 521 |
</div> |
| 522 |
</fieldset> |
| 523 |
<?php |
| 524 |
$csv_columns = array(); |
| 525 |
@ini_set( 'auto_detect_line_endings', true ); // phpcs:ignore |
| 526 |
if ( false !== ( $fp = fopen( $file_name, 'r' ) ) ) { // phpcs:ignore |
| 527 |
$index = 0; |
| 528 |
while ( false !== ( $data = fgetcsv( $fp, 0, $this->delimiter ) ) ) { // phpcs:ignore |
| 529 |
for ( $column = 0; $column < count( $data ); $column ++ ) { // phpcs:ignore - 8.1 proof |
| 530 |
if ( $this->has_header_columns ) { |
| 531 |
array_push( $csv_columns, $data[ $column ] );//phpcs:ignore - 8.1 proof |
| 532 |
} else { |
| 533 |
array_push( $csv_columns, "column_{$index}" );//phpcs:ignore - 8.1 proof |
| 534 |
$index++; |
| 535 |
} |
| 536 |
} |
| 537 |
break; |
| 538 |
} |
| 539 |
fclose( $fp ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose |
| 540 |
} |
| 541 |
?> |
| 542 |
<br/> |
| 543 |
<fieldset class="wpda_fieldset"> |
| 544 |
<legend> |
| 545 |
<?php echo __( 'Column mapping', 'wp-data-access' ); // phpcs:ignore WordPress.Security.EscapeOutput ?> |
| 546 |
<a href="javascript:void(0)"> |
| 547 |
<span class="dashicons dashicons-editor-help wpda_tooltip" title="Drag columns from Table to Mapped" style="cursor:pointer;vertical-align:bottom;text-decoration:none;"></span> |
| 548 |
</a> |
| 549 |
</legend> |
| 550 |
<div> |
| 551 |
<table id="wpda_csv_mapping" cellpadding="0" cellspacing="0"> |
| 552 |
<tr> |
| 553 |
<th></th> |
| 554 |
<th> </th> |
| 555 |
<th style="text-align:left;">Mapped</th> |
| 556 |
<th> </th> |
| 557 |
<th style="text-align:left;">Table</th> |
| 558 |
</tr> |
| 559 |
<?php |
| 560 |
$first_row = true; |
| 561 |
foreach ( $csv_columns as $key => $csv_column_ ) { |
| 562 |
$csv_column = trim( $csv_column_ ); |
| 563 |
?> |
| 564 |
<tr class="csv_column_selected" style="height: 30px !important;"> |
| 565 |
<th class="csv_column_selected_cell"> |
| 566 |
<?php echo esc_attr( $csv_column ); ?> |
| 567 |
</th> |
| 568 |
<td></td> |
| 569 |
<td class="csv_column_selected_cell"> |
| 570 |
<ul class="csv_column csv_column_mapped" |
| 571 |
data-csv-column-name="<?php echo esc_attr( $csv_column ); ?>" |
| 572 |
> |
| 573 |
<?php |
| 574 |
if ( isset( $this->mapping['columns'][ $key ] ) ) { |
| 575 |
?> |
| 576 |
<li value="<?php echo esc_attr( $this->mapping['columns'][ $key ] ); ?>"> |
| 577 |
<?php echo esc_attr( $this->mapping['columns'][ $key ] ); ?> |
| 578 |
</li> |
| 579 |
<?php |
| 580 |
} else { |
| 581 |
?> |
| 582 |
<li class="wpda_csv_column_init"> |
| 583 |
<?php echo __( 'Drag column here...', 'wp-data-access' ); // phpcs:ignore WordPress.Security.EscapeOutput ?> |
| 584 |
</li> |
| 585 |
<?php |
| 586 |
} |
| 587 |
?> |
| 588 |
</ul> |
| 589 |
</td> |
| 590 |
<td></td> |
| 591 |
<?php |
| 592 |
if ( $first_row ) { |
| 593 |
?> |
| 594 |
<td rowspan="<?php echo esc_attr( count( $csv_columns ) + 1 ); //phpcs:ignore - 8.1 proof ?>" style="vertical-align: top;"> |
| 595 |
<ul id="csv_table_columns"> |
| 596 |
</ul> |
| 597 |
</td> |
| 598 |
<?php |
| 599 |
} |
| 600 |
?> |
| 601 |
</tr> |
| 602 |
<?php |
| 603 |
$first_row = false; |
| 604 |
} |
| 605 |
?> |
| 606 |
<tr> |
| 607 |
<td></td> |
| 608 |
<td></td> |
| 609 |
<td></td> |
| 610 |
<td></td> |
| 611 |
</tr> |
| 612 |
</table> |
| 613 |
</div> |
| 614 |
</fieldset> |
| 615 |
</form> |
| 616 |
<form id="change_delimiter" method="post" |
| 617 |
action="?page=wpda&page_action=wpda_import_csv" style="display: none;"> |
| 618 |
<input type="hidden" name="csv_id" value="<?php echo esc_attr( $this->csv_id ); ?>"/> |
| 619 |
<input type="hidden" name="action" value="mapping"/> |
| 620 |
<input type="hidden" name="_wpnonce" value="<?php echo esc_attr( $wp_nonce_mapping ); ?>" /> |
| 621 |
</form> |
| 622 |
<form id="import_form" action="?page=wpda&page_action=wpda_import_csv" |
| 623 |
method="post" style="display: none;"> |
| 624 |
<input type="hidden" name="action" value="import_start" /> |
| 625 |
<input type="hidden" name="csv_id" value="<?php echo esc_attr( $this->csv_id ); ?>" /> |
| 626 |
<input type="hidden" name="csv_name" value="<?php echo esc_attr( $this->csv_name ); ?>" /> |
| 627 |
<input type="hidden" name="_wpnonce" value="<?php echo esc_attr( $wp_nonce ); ?>" /> |
| 628 |
</form> |
| 629 |
<br/> |
| 630 |
<a href="javascript:void(0)" onclick="save_mapping()" class="button"> |
| 631 |
<span class="dashicons dashicons-yes-alt" style="padding-top: 4px; padding-right: 4px;"></span> |
| 632 |
Save mapping |
| 633 |
</a> |
| 634 |
<a href="javascript:void(0)" onclick="jQuery('#import_form').submit()" class="button"> |
| 635 |
<span class="dashicons dashicons-upload" style="padding-top: 4px; padding-right: 4px;"></span> |
| 636 |
Import CSV file |
| 637 |
</a> |
| 638 |
<a id="wpda_preview_visible" href="javascript:void(0)" onclick="toggle_preview()" class="button"> |
| 639 |
<span class="dashicons dashicons-visibility" style="padding-top: 4px; padding-right: 4px;"></span> |
| 640 |
Preview CSV file |
| 641 |
</a> |
| 642 |
<a id="wpda_preview_hidden" href="javascript:void(0)" onclick="toggle_preview()" class="button" style="display: none;"> |
| 643 |
<span class="dashicons dashicons-hidden" style="padding-top: 4px; padding-right: 4px;"></span> |
| 644 |
Hide CSV file |
| 645 |
</a> |
| 646 |
<?php |
| 647 |
$this->preview(); |
| 648 |
} |
| 649 |
|
| 650 |
/** |
| 651 |
* Preview container |
| 652 |
* |
| 653 |
* @return void |
| 654 |
*/ |
| 655 |
protected function preview() { |
| 656 |
?> |
| 657 |
<div id="wpda_csv_preview" style="display: none;"> |
| 658 |
<br/> |
| 659 |
<div id="wpda_csv_preview_table"></div> |
| 660 |
<?php |
| 661 |
} |
| 662 |
|
| 663 |
} |
| 664 |
|
| 665 |
} |
| 666 |
|