'; echo '
'; echo '

'.__('Add Multiple Users - Import CSV Data','amulang').'

'; //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 '
'; echo '

'.__('New User Accounts Processed.','amulang').'

'; echo '
'; amuRegisterFromForm(); //OTHERWISE, UNKNOWN REQUEST HANDLER } else { 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 '
'; echo '

'.__('Choose a CSV/TXT file to upload','amulang').'

'; echo '
'; echo ''; echo '
'; echo '

'.__('Information in the text box below will be ignored if a file is found in the File browser above.','amulang').'

'; echo '

'.__('Or paste your CSV data in the box below','amulang').'

'; echo '
'; echo ' '; echo '
'; echo '

'; $csvinfo = new amuSettingsObject(); echo $csvinfo->get_amu_setallroles(); echo $csvinfo->get_amu_dispnamedef(); echo $csvinfo->get_amu_colorderpref(); echo $csvinfo->get_updatenow(); echo '

'; echo '
'; echo '

'.__('Please ensure you have read the information on CSV importing functions and updated your Settings before proceeding.','amulang').'

'; echo ''; echo ''; 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 '
'; echo '

'.__('Data Column Ordering','amulang').'

'; amuAddFileSort(); echo '

'.__('Your CSV Data','amulang').'

'; echo '
'; echo ''; echo '

'.__('Total lines found in this document','amulang').': '.$linecount.'

'; echo '
'; echo '
'; echo '

'.__('Click the Create User Information Form button to convert this user information into a Form, allowing you to review and customise specific information.','amulang').'

'; echo ' '; echo '

'.__('Alternatively, choose the Skip Form and Add Users option if you want to immediately add all users.','amulang').' '.__('Important','amulang').': '.__('If you are adding more than 100 users in one pass, it is recommended you use this option. Please see the information at the bottom of the screen for more info.','amulang').'

'; echo ' '; echo '
'; 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 = '

'.$line.'. '.__('New user successfully registered.','amulang').' '.__('Login','amulang').': '.$newUser->user_login.' '.__('Password','amulang').': '.$newUser->user_pass.' '.__('Email','amulang').': '.$newUser->user_email.'

'; } else { //return failure message $confirmUpdate = '

'.$line.'. '.$newid.'

'; } //kill reusable object unset($newUser); } else { //return failure message $confirmUpdate = '

'.$line.'. '.__('No user_login was found. This line was skipped.','amulang').'

'; } //add success or failure message to stack $confirmationStack[] = $confirmUpdate; } echo '

'.__('Results of your new user registrations','amulang').'

'; //admin notifications $adminUser = new amuAdminObject(); $sendRegResults = $adminUser->amuAdminConfirmation($confirmationStack); $stackDisplay = $adminUser->amuShowStack($confirmationStack); //print notifications to screen echo $sendRegResults; echo '
'.$stackDisplay.'
'; } /* * 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(); } } ?>