';
//STEP ONE - PASTE DATA OR CHOOSE FILE
if (empty($_POST) || isset($_POST['cancel'])) {
$csvinputerror = '';
amuCSVInputMain($csvinputerror);
//STEP TWO - READ AND CLEAN DATA
} else if ( isset($_POST['readandclean'] ) ) {
amuCSVShowData();
//OPTION - PRINT TO FORM
} else if ( isset($_POST['sendcsvtoform'] ) ) {
amuCSVToForm();
//OPTION - DIRECT REGISTER
} else if ( isset($_POST['sendcsvtoreg'] ) ) {
amuCSVToRegister();
// REGISTER FROM FORM
} else if( isset($_POST['addnewusers'] ) ){
echo '
'.__('Unknown request. Please choose the CSV Import option from the menu.','amulang').'
';
}
//end wrap divs and function
echo '
';
echo '';
}
/*
* CSV IMPORT MAIN INTERFACE
* provides content input and upload methods
*/
function amuCSVInputMain($csvinputerror){
global $wpdb;
echo '
';
echo $csvinputerror;
echo '
'.__('Converts a list of comma-separated values (CSV) into new user registration information.','amulang').'
'.__('Your information will be extracted and available to view and sort in the next step.','amulang').'
';
echo '
';
echo '';
$infotype = 'csvinputpage';
showPluginInfo($infotype);
}
/*
* CSV IMPORT COLUMN ORDERING INTERFACE
* displays uploaded/pasted data and provides column ordering method
*/
function amuCSVShowData(){
global $wpdb;
$proceed = true;
//if both options are empty
if($_FILES['csvuploader']['name'] == '' && $_POST['csvpastebox'] == '') {
$csvinputerror = '
'.__('No information was found. Please choose a file or enter your information in the text box.','amulang').'
';
$proceed = false;
}
//verify file is legit
if($_FILES['csvuploader']['name'] !== '') {
if (is_uploaded_file($_FILES['csvuploader']['tmp_name'])) {
$allowedExtensions = array("txt","TXT","csv","CSV");
if (!in_array(end(explode(".", strtolower($_FILES['csvuploader']['name']))), $allowedExtensions)) {
$csvinputerror = '
'.__('Error: Not a valid file type! Only .csv and .txt files may be uploaded.','amulang').'
';
$proceed = false;
}
}
}
//if all good, show next part
if($proceed) {
//add contents of form if exists
if($_FILES['csvuploader']['name'] !== '') {
//set ini for mac-created files
ini_set('auto_detect_line_endings',true);
$thefiledata = file_get_contents($_FILES['csvuploader']['tmp_name']);
$linecount = count(file($_FILES['csvuploader']['tmp_name']));
//otherwise show pastebox data
} else {
$thefiledata = $_POST['csvpastebox'];
$linecount = count($thefiledata);
}
//begin interface
echo '
';
echo '
'.__('The following user information has been extracted from your data.','amulang').' '.__('Please ensure your column order matches the data order and click one of the registration buttons below to continue.','amulang').'
';
echo '
';
//start form
echo '';
//show info
if(get_option('amu_colorderpref') == 'dynamic') {
$infotype = 'dynamicsort';
showPluginInfo($infotype);
}
$infotype = 'csvinputpage';
showPluginInfo($infotype);
//otherwise, backtrack with errors
} else {
amuCSVInputMain($csvinputerror);
}
}
/*
* CSV IMPORT CONVERT DATA TO FORM
* sends csv data to the form interface
*/
function amuCSVToForm(){
global $wpdb;
$rawCSVdata = $_POST['filecontents'];
$parsedData = parse_csv($rawCSVdata);
$userData = reorder_csv($parsedData);
$source = 'csvlist';
$newForm = new amuFormCreator($source,$userData);
$newForm->amuCreateFormInterface();
//show form field info
$infotype = 'formfields';
showPluginInfo($infotype);
}
/*
* CSV IMPORT DIRECT REGISTER
* sends csv data directly to register users
*/
function amuCSVToRegister(){
global $wpdb;
$rawCSVdata = $_POST['filecontents'];
$parsedData = parse_csv($rawCSVdata);
$reorderedData = reorder_csv($parsedData);
$userData = json_decode($reorderedData);
$confirmationStack = array();
$line = 0;
//pull out each user data array and process individually
foreach($userData as $userRow) {
$line++;
if($userRow->user_login !== '') {
//create new user object
$newUser = new amuUserObject($userRow);
//register initial user info
$newid = $newUser->amuRegisterUser();
if(is_int($newid)) {
//update users data based on input
$newUser->amuUpdateUserData();
//send user a notification email
$newUser->amuSendUserEmail();
//add any additional meta data fields
$newUser->amuUpdateUserMeta($userRow);
//set confirmation message
$confirmUpdate = '
';
}
/*
* READ AND CLEAN CSV DATA
* cleans junk from csv data, establishes lines, makes it all useable
*/
function parse_csv($file,$comma=',',$quote='"',$newline="\n") {
$db_quote = $quote . $quote;
//clean up data
$file = trim($file);
$file = str_replace("\r\n",$newline,$file);
$file = str_replace($db_quote,'"',$file);
$file = str_replace(',",',',,',$file);
$file .= $comma;
$inquotes = false;
$start_point = 0;
$row = 0;
//run for each line
for($i=0; $ikeyname);
}
//or get order from static box
} else if($theColPrefUsed == 'static') {
$postedOptions = trim($_POST['loadorderpref']);
$strippedOptions = str_replace(' ','',$postedOptions);
$definedOptions = explode(',',$strippedOptions);
foreach($definedOptions as $defoption) {
array_push($customorder, $defoption);
}
//or get order from first line of csv data, below...
} else if($theColPrefUsed == 'firstline') {
foreach($pasteddata[0] as $orderpiece) {
array_push($customorder, $orderpiece);
}
unset($pasteddata[0]);
}
//create empty array to store lines
$datastore = array();
$entryCount = 0;
//loop through each line, print to array, add to master
foreach($pasteddata as $thisline) {
//array to store line data
$newarrayline = array();
foreach($customorder as $stdOption) {
if($stdOption !== 'ignore') {
$keyname = $stdOption.'_index';
$$keyname = array_search($stdOption, $customorder);
if($$keyname === false) {
$newarrayline[$stdOption] = '';
} else {
$newarrayline[$stdOption] = $thisline[$$keyname];
}
}
}
//increment counter
$entryCount++;
//add new line to main array
array_push($datastore, $newarrayline);
//kill the array for reuse
unset($newarrayline);
}
//return the encoded data
$data_rev = json_encode($datastore);
return $data_rev;
}
/*
* SHOW SORTING INTERFACE
* Shows preferenced sorting mechanism
*/
function amuAddFileSort() {
global $wpdb;
//show data appropriate to chosen sorting method
$getSortOrderPref = get_option('amu_colorderpref');
//if dynamic, show box to fill with jquery stuff
if($getSortOrderPref == 'dynamic') {
include 'dynamicsorter.php';
//if predefined, show predefined structure
} else if($getSortOrderPref == 'predefined') {
echo '
'.__('You have predefined your CSV data column order as the following','amulang').': ';
$sortpredef = get_option('amu_colorderpredef');
if($sortpredef !== '') {
$sortpredeffields = json_decode($sortpredef);
$newSortPrefArr = array();
foreach($sortpredeffields as $pso) {
$newSortPrefArr[] = $pso->keyname;
}
$printablePSO = implode(',',$newSortPrefArr);
echo $printablePSO;
} else {
_e('No column order has been defined! Please see the Settings page to specify your column order.','amulang');
}
echo ' '.__('Ensure that where standard WordPress values are being used that your column names match the wp_user or wp_usermeta tables.','amulang').'
';
//if static, provide box to type in structure
} else if($getSortOrderPref == 'static') {
echo '
';
echo '
';
echo '';
echo '';
amuColumnNamesHint();
echo '
';
echo '
';
//if using firstline, notify of choice
} else if($getSortOrderPref == 'firstline') {
echo '
'.__('Your Column Order Preference is currently set to read the first line of your CSV data as the column order. This line has been included in your CSV data to modify if necessary.','amulang').'';
amuColumnNamesHint();
}
}
?>