From: Steve Sutton ' . __( 'Follow the Sample File provided in creating the file for custom field import.
+ Each record must have the following fields. $fields: ' . print_r( $fields, true ) . '
';
+ $this->totalFields = count( $fields );
+ // Clear the custom field data table
+ $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_field_data" );
+ foreach ( $fields as $customRow ) {
+ // Need to first get the member id from the database
+ // It will match from the old_member_id field
+ $memberId = $this->wpdb->get_var(
+ $this->wpdb->prepare(
+ "SELECT id
+ FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members
+ WHERE old_member_id = %d",
+ $customRow[0]
+ )
+ );
+ // get the active member info id
+ $memberInfoId =
+ $memberInfoData->getActiveInfoIdForMember( $memberId );
+ $customFieldId = $this->wpdb->get_var(
+ $this->wpdb->prepare(
+ "SELECT id
+ FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_fields
+ WHERE field_name = %s",
+ $customRow[1]
+ )
+ );
+ if ( $customFieldId ) {
+ $newId = $this->wpdb->insert(
+ GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . 'custom_field_data',
+ array(
+ 'ref_dest' => $memberInfoId,
+ 'field_id' => $customFieldId,
+ 'field_data' => $customRow[2],
+ ),
+ array(
+ '%d',
+ '%d',
+ '%s'
+ )
+ );
+ $this->numberProcessed++;
+ }
+ }
+ $importRet = array();
+ }
+ }
+ if ( count( $this->errors ) == 0 ) {
+ $readyToProcess = true;
+ }
+ // Here we need to check to see if we processed all members.
+ // Also the counter has to increment the total processed so far.
+ if ( $this->numberProcessed == $this->totalFields ) {
+ $this->processingComplete = true;
+ }
+ // Set the view file:<
+ $view = 'fieldsProcess.html';
+ break;
+
+ case 'fields':
+ default:
+ // Set the view file
+ $view = 'fields.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;
+
+ }
+
+ // Setup the template data array
+ $templateData = array(
+ 'fileExists' => $fileExists,
+ 'option' => $option,
+ 'errors' => $this->errors,
+ 'numberProcessed' => $this->numberProcessed,
+ 'totalFields' => $this->totalFields,
+ 'completed' => $this->processingComplete,
+ 'data' => false,
+ 'fileData' => $fileData,
+ 'clearData' => $clearData,
+ 'importRet' => $importRet,
+ 'csvData' => '$fileData: ' . print_r( $fileData, true ) . '
',
+ 'readyToProcess' => $readyToProcess,
+ 'haveMembers' => $haveMembers,
+ 'isValid' => $isValid,
+ 'sampleFileUrl' => GLM_MEMBERS_PLUGIN_BASE_URL . '/sample-files/',
+ );
+
+ // Return status, suggested view, and data to controller
+ return array(
+ 'status' => true,
+ 'menuItemRedirect' => false,
+ 'modelRedirect' => false,
+ 'view' => 'admin/import/' . $view,
+ 'data' => $templateData,
+ );
+
+ }
+
+ /**
+ * Read in fields from a csv file
+ *
+ * Header line as follows. Data follows immediately below this line. this
+ * line and all above it are ignored.
+ *
+ * 'member_id','member_name','member_login','member_passwd','contact_email'
+ *
+ * @param string $csv Temporary file name of csv file upload
+ *
+ * @return array Array of data from CSV file or an error message
+ *
+ */
+ public function readCSVFields($csv)
+ {
+
+ $fields = array();
+ $startImport = false;
+
+ // Try to open file
+ if (($handle = fopen($csv, "r")) !== false) {
+
+ // For each line in the file
+ while (($c = fgetcsv($handle, 1000, ",")) !== false) {
+
+ // If we're past the header, the first item is numeric, and we have at least 5 fields
+ if($startImport && ($c[0]-0) > 0 && count($c) >= 3) {
+
+ // Add this line of data to Contacts
+ $fields[] = $c;
+
+ }
+
+ // If we find the header, assume all data is below that
+ if ($c[0] == 'member_id') {
+ $startImport = true;
+ }
+
+ }
+
+ fclose($handle);
+
+ } else {
+ return "No file submitted.";
+ }
+
+ // If we never found the header
+ if (!$startImport) {
+ return "Required header not found in file.";
+ }
+
+ // If we found no data below the header
+ if (count($fields) == 0) {
+ return "Header found but no data followed";
+ }
+
+ return $fields;
+
+ }
+
+ /**
+ * readCSVFileHeaders
+ *
+ * Read the cvs file. Just the first line is read.
+ *
+ * @param mixed $fileName Name of the file (path)
+
+ * @access public
+ * @return void
+ */
+ public function readCSVFileHeaders( $fileName )
+ {
+ $fileHeaders = array();
+ if ( ( $fp = fopen( $fileName, 'r' ) ) !== false ) {
+ // get first line to use as headers
+ $fileHeaders = fgetcsv( $fp, SELF::CSV_CHARS_PER_LINE, ',' );
+ fclose( $fp );
+ }
+
+ return $fileHeaders;
+ }
+}
diff --git a/setup/adminTabs.php b/setup/adminTabs.php
index 0860651..1a1447e 100644
--- a/setup/adminTabs.php
+++ b/setup/adminTabs.php
@@ -50,3 +50,30 @@ if (current_user_can('glm_members_members')) {
}
);
}
+if (current_user_can('glm_members_management')) {
+ add_filter('glm-member-db-add-tab-for-import',
+ function($addOnTabs) {
+ $newTabs = array(
+ array(
+ 'text' => 'Custom Fields',
+ 'menu' => 'import',
+ 'action' => 'fields',
+ )
+ );
+ $addOnTabs = array_merge($addOnTabs, $newTabs);
+ return $addOnTabs;
+ }
+ );
+ add_filter( 'glm-member-db-add-help-for-import',
+ function( $helpTabs ){
+ $helpTabs[] = array(
+ 'id' => 'glm-member-db-import-help-fields',
+ 'title' => __( 'Custom Fields' ),
+ 'content' => 'Required Fields
' ) . '
Total Records | +{$totalFields} | +
---|---|
Processed Records | +{$numberProcessed} | +
+ {if $file.exists} + {$fileHeader} File + {/if} + | ++ {if $file.isValid} + Is Valid + {else} + Not Valid + {/if} + | +
+ Process Files + | +|
+ One or more of your files are not the correct csv format. Please go back and try again. + Go Back + |
+