From: Steve Sutton Date: Mon, 5 Jun 2017 20:23:59 +0000 (-0400) Subject: WIP file upload and validation X-Git-Tag: v2.10.0^2~11^2~30 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=525bf5fe8b69d897846776765a8f147e775580ea;p=WP-Plugins%2Fglm-member-db.git WIP file upload and validation Upload test files. Validating them from their headers. --- diff --git a/models/admin/import/index.php b/models/admin/import/index.php index 0eda98e7..c0e2a4ec 100644 --- a/models/admin/import/index.php +++ b/models/admin/import/index.php @@ -92,26 +92,99 @@ class GlmMembersAdmin_import_index */ 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 '
$_FILES: ' . print_r( $_FILES, true ) . '
'; + 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' => '
$fileData: ' . print_r( $fileData, true ) . '
', ); // Return status, suggested view, and data to controller return array( @@ -124,4 +197,38 @@ class GlmMembersAdmin_import_index } + 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; + } + } diff --git a/views/admin/import/index.html b/views/admin/import/index.html index 2d9bb763..4ce1eea2 100644 --- a/views/admin/import/index.html +++ b/views/admin/import/index.html @@ -4,20 +4,27 @@
+ + {foreach $fileData as $fileHeader => $file} - - - - - - - - - - + + + + + {/foreach}
City Data
State Data
Member Profile Data{$fileHeader} + + + {if $file.exists} + {$fileHeader} File + {/if} + + {if $file.exists} + {$file.mtime|date_format:"%D %I:%M %p"} + {/if} +
diff --git a/views/admin/import/validate.html b/views/admin/import/validate.html new file mode 100644 index 00000000..bd228829 --- /dev/null +++ b/views/admin/import/validate.html @@ -0,0 +1,26 @@ +{include file='admin/import/header.html'} + +

Data Import Step 2: Validate files

+ + + {foreach $fileData as $fileHeader => $file} + + + + + {/foreach} + + Process Files +
+ {if $file.exists} + {$fileHeader} File + {/if} + + {if $file.isValid} + Is Valid + {else} + Not Valid + {/if} +
+ +{include file='admin/footer.html'}