/***************************************************
* Application Actions
* *************************************************/
function application_admin_pages() {
add_menu_page(__('Application'), __('Application'), 'manage_options', basename(__FILE__), 'application_landing_function');
}
add_action('admin_menu', 'application_admin_pages');
function application_admin_submenu() {
add_submenu_page( 'functions.php', 'import_export', 'Import & Export', 'manage_options', 'backup-page', 'application_import_export_page');
}
add_action('admin_menu', 'application_admin_submenu');
/* application_landing_function */
function application_landing_function() {?>
<div class="wrap">
<h1>Application</h1>
<table>
<?php
$result = mysql_query("SELECT * FROM _cwcustom_rebate_devices");
while ($table = mysql_fetch_assoc($result)){
$p = $p + 1;
echo '<tr>';
foreach ($table AS $tableName){
echo '<td>'.$tableName.'</td>';
}
echo '</tr>';
} ?>
</table>
</div>
<?php }
/* application_import_export_page */
function application_import_export_page() {?>
<div class="wrap">
<h1>Application</h1>
<h2>Import CSV</h2>
<form method="post" action="" method="post" enctype="multipart/form-data">
<fieldset>
<legend><strong>Choose a CSV (.csv) file to upload, then click Import .</strong></legend>
<input type="file" name="file" size="50" maxlength="100000" /><br/>
</fieldset>
<p class="submit">
<input type="submit" name="import" class="button button-primary button-import" value="Import" />
</p>
</form>
<!-- End Import from HTML-->
<?php echo export_UI(); ?><!-- Export CSV HTML-->
<p></p>
<?php import_form_action();?><!-- Import form action -->
</div>
<script type="text/javascript" charset="utf-8">
$(document).on('click','input.button-import', function(){
if (!confirm("Are You Sure?")) {
return false;
}
})
</script>
<?php }
/* Import Form Action */
function import_form_action() {
define('UPLOAD_PATH', dirname(__FILE__).'/upload/');
global $wpdb;
if ( count($_POST) > 0 && isset($_POST['import']) ) {
$up_info = $_FILES['file'];
if($up_info['error']>0) {
echo "Error: " . $up_info['error']. "<br />";
} else {
$file = fopen($up_info["tmp_name"],"r");
$count = 0;
$table_name = array();
$wpdb->query ('TRUNCATE TABLE _cwcustom_rebate_devices') ;
while(!feof($file) && $data = fgetcsv($file)) {
$column = count($data);
$result = array();
if($count==0 && !empty($data)) {
for($i=0;$i<$column;$i++){
array_push($table_name,$data[$i]);
}
} elseif ($count>0 && !empty($data)) {
for($i=0;$i<$column;$i++) {
array_push($result,$data[$i]);
}
$temp_result = array();
$i=0;
foreach($table_name as $table_name_key => $table_name_val) {
$temp_result[$table_name_val] = $result[$i];
$i++;
}
$res = $wpdb->insert( "_cwcustom_rebate_devices", $temp_result );
echo $res ? $count.' Succeed<br/>' : $count.' False<br/>';
}
$count++;
}
fclose($file);
}
}
}
/* Export */
define('CONST_CSV_SLUG', 'admin.php?page=backup-page&action=');
function export_UI() {
?>
<div id="wrapper">
<h2>Export CSV</h2>
<div id="page">
<table cellspacing="0" class="wp-list-table widefat">
<thead>
<tr>
<th>Sr.</th>
<th>Export Tables to CSV</th>
</tr>
</thead>
<tbody>
<?php
$array_table_name = array('1' => '_cwcustom_rebate_applications','2' => '_cwcustom_rebate_application_devices','3' => '_cwcustom_rebate_devices');
foreach ($array_table_name as $serial_number => $table_name){?>
<tr>
<td><?php echo $serial_number;?></td>
<td><a class="button button-large" href="<?php echo CONST_CSV_SLUG.$table_name;?>">Export <strong><?php echo $table_name;?></strong></a></td>
</tr>
<?php }
?>
</tbody>
</table>
</div>
</div>
<?php
}
/*Prompt to open/save exported data as .CSV file*/
function application_export(){ $getTable = isset($_REQUEST['action']) ? $_REQUEST['action'] : ''; if ($getTable){ echo csv_generate($getTable); exit; } } add_action('init', 'application_export');
/* Convert table data into CSV format */
function csv_generate($getTable){
ob_clean();
global $wpdb;
$field='';
$getField ='';
if($getTable){
$result = $wpdb->get_results("SELECT * FROM $getTable");
$requestedTable = mysql_query("SELECT * FROM ".$getTable);
$fieldsCount = mysql_num_fields($requestedTable);
for($i=0; $i<$fieldsCount; $i++){
$field = mysql_fetch_field($requestedTable);
$field = (object) $field;
$getField .= $field->name.',';
}
$sub = substr_replace($getField, '', -1);
$fields = $sub; # GET FIELDS NAME
$each_field = explode(',', $sub);
$csv_file_name = $getTable.'_'.date('Ymd_His').'.csv'; # CSV FILE NAME WILL BE table_name_yyyymmdd_hhmmss.csv
# GET FIELDS VALUES WITH LAST COMMA EXCLUDED
foreach($result as $row){
for($j = 0; $j < $fieldsCount; $j++){
if($j == 0) $fields .= "\n"; # FORCE NEW LINE IF LOOP COMPLETE
$value = str_replace(array("\n", "\n\r", "\r\n", "\r"), "\t", $row->$each_field[$j]); # REPLACE NEW LINE WITH TAB
$value = str_getcsv ( $value , ",", "\"" , "\\"); # SEQUENCING DATA IN CSV FORMAT, REQUIRED PHP >= 5.3.0
$fields .= $value[0].','; # SEPARATING FIELDS WITH COMMA
}
$fields = substr_replace($fields, '', -1); # REMOVE EXTRA SPACE AT STRING END
}
header("Content-type: text/x-csv"); # DECLARING FILE TYPE
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename=".$csv_file_name); # EXPORT GENERATED CSV FILE
header("Pragma: no-cache");
header("Expires: 0");
return $fields;
}
}
/* End Export*/