*/
public function modelAction ($actionData = false)
{
-
-
$view = 'index.html';
$failure = false;
$option = '';
+ $fileData = array(
+ 'City' => array(
+ 'field' => 'city_file',
+ 'name' => 'cityData.csv',
+ 'exists' => false,
+ 'validate' => function( $row ) {
+ return ( $row === array( 'id', 'name' ) );
+ },
+ ),
+ 'Region' => array(
+ 'field' => 'region_file',
+ 'name' => 'regionData.csv',
+ 'exists' => false,
+ 'validate' => function( $row ) {
+ return ( $row === array( 'id', 'name' ) );
+ },
+ ),
+ 'County' => array(
+ 'field' => 'county_file',
+ 'name' => 'countyData.csv',
+ 'exists' => false,
+ 'validate' => function( $row ) {
+ return ( $row === array( 'id', 'name' ) );
+ },
+ ),
+ 'Member' => array(
+ 'field' => 'member_file',
+ 'name' => 'memberData.csv',
+ 'exists' => false,
+ 'validate' => function( $row ) {
+ return ( $row === array( 'id', 'member_name', 'status', 'descr', 'short_descr', 'addr1', 'addr2', 'city', 'state', 'country', 'zip', 'region', 'county', 'lat', 'lon', 'phone', 'toll_free', 'url', 'reservation_url', 'email', 'logo' ) );
+ },
+ ),
+ );
if (isset($_REQUEST['option']) && $_REQUEST['option'] != '') {
$option = $_REQUEST['option'];
}
- switch($option) {
+ $wpUploadDir = wp_get_upload_dir();
+ $uploadPath = $wpUploadDir['basedir'] . '/' . 'glm-member-import';
+ if ( !is_dir( $uploadPath ) ) {
+ $oldMask = umask(0);
+ mkdir( $uploadPath, 0770 );
+ umask( $oldMask );
+ }
+
+ switch( $option ) {
+
+ case 'import_files';
+ $view = 'validate.html';
+ // Move any files uploaded
+ //echo '<pre>$_FILES: ' . print_r( $_FILES, true ) . '</pre>';
+ if ( isset( $_FILES ) ) {
+ foreach ( $fileData as $fileHeader => $file ) {
+ if ( !$_FILES[$file['field']]['error'] ) {
+ move_uploaded_file( $_FILES[$file['field']]['tmp_name'], $uploadPath . '/'. $file['name'] );
+ }
+ }
+ }
+ // Check that each file exists
+ foreach ( $fileData as $fileHeader => $file ) {
+ if ( is_file( $uploadPath . '/' . $file['name'] ) ) {
+ $fileData[$fileHeader]['exists'] = true;
+ $fData = $this->readCSVFileHeaders( $uploadPath . '/' . $file['name'] );
+ $fileData[$fileHeader]['data'] = $fData;
+ $fileData[$fileHeader]['isValid'] = call_user_func( $file['validate'], $fData );
+ }
+ }
+
+ break;
default:
+ $view = 'index.html';
+ // check upload dir to see if they have any files in yet
+ foreach ( $fileData as $fileHeader => $file ) {
+ if ( is_file( $uploadPath . '/' . $file['name'] ) ) {
+ $fileData[$fileHeader]['exists'] = true;
+ $fileData[$fileHeader]['mtime'] = filemtime( $uploadPath . '/' . $file['name'] );
+ }
+ }
break;
}
$templateData = array(
- 'data' => false,
+ 'data' => false,
+ 'fileData' => $fileData,
+ 'csvData' => '<pre>$fileData: ' . print_r( $fileData, true ) . '</pre>',
);
// Return status, suggested view, and data to controller
return array(
}
+ public function readCSVFileHeaders( $fileName )
+ {
+ $fileHeaders = array();
+ if ( ( $fp = fopen( $fileName, 'r' ) ) !== false ) {
+ // get first line to use as headers
+ $fileHeaders = fgetcsv( $fp, 1000, ',' );
+ fclose( $fp );
+ }
+
+ return $fileHeaders;
+ }
+
+ public function readCSVFile( $fileName )
+ {
+ $fileData = array();
+ $fileHeaders = array();
+ if ( ( $fp = fopen( $fileName, 'r' ) ) !== false ) {
+ // get first line to use as headers
+ $rowNumber = 0;
+ while ( ( $data = fgetcsv( $fp, 1000, ',' ) ) !== false ) {
+ if ( $rowNumber == 0 ) {
+ $fileHeaders = $data;
+ } else {
+ for ( $index = 0; $index < count( $data ); ++$index ) {
+ $fileData[$rowNumber][$fileHeaders[$index]] = $data[$index];
+ }
+ }
+ $rowNumber++;
+ }
+ fclose( $fp );
+ }
+ return $fileData;
+ }
+
}