Update csv importer
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 5 Mar 2019 15:51:56 +0000 (10:51 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 5 Mar 2019 15:51:56 +0000 (10:51 -0500)
Update to not insert photos that are already there.

models/admin/import/index.php

index 061db1a..37695dc 100644 (file)
@@ -651,7 +651,6 @@ class GlmMembersAdmin_import_index
                 for ( $index = $start; $index < $ending; $index++ ) {
                     $photo = $photoData[$index];
                     $this->photosProcessed++;
-                    // echo '<pre>$photoUrl: ' . print_r( $photoUrl, true ) . '</pre>';
                     // If there's no url or the url is not valid then skip it.
                     if ( $photoUrl = filter_var( $photo['image'], FILTER_VALIDATE_URL ) ) {
                         // Need to first get the member id from the database
@@ -664,24 +663,46 @@ class GlmMembersAdmin_import_index
                                 $photo['member_id']
                             )
                         );
-                        // echo '<pre>$memberId: ' . print_r( $memberId, true ) . '</pre>';
                         if ( !$memberId ) {
-                            // var_dump( 'no member id' );
                             continue;
                         }
                         // Now that we have the member id we need to get the
                         // id for the active member info record
                         $memberInfoId = $memberInfoObj->getActiveInfoIdForMember( $memberId );
-                        // echo '<pre>$memberInfoId: ' . print_r( $memberInfoId, true ) . '</pre>';
                         if ( !$memberInfoId ) {
-                            // var_dump( 'no member info id' );
                             continue;
                         }
+                        // Get any current photos for this member info record
+                        $memberInfoPhotos = $this->wpdb->get_results(
+                            $this->wpdb->prepare(
+                                "SELECT *
+                                   FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "images
+                                  WHERE ref_dest = %d",
+                                $memberInfoId
+                            ),
+                            ARRAY_A
+                        );
+
+                        $photoExists = false;
+
+                        $fileBaseName = basename( $photoUrl );
+
+                        foreach ( $memberInfoPhotos as $memberPhotos ) {
+                            $checkFileName = preg_replace( '%/tmp/%', '', $memberPhotos['name'] );
+                            if ( $checkFileName == $fileBaseName ) {
+                                $photoExists = true;
+                            }
+                        }
+
+                        // Test if the member info image is already stored
+                        if ( $photoExists ) {
+                            continue;
+                        }
+
                         // There's a character limit on the photo caption field
                         // of 255 (mysql TINYTEXT)
                         $caption = substr( $photo['caption'], 0, 250 );
                         $newPhotoUrl = $this->fetchRemoteImage( $photoUrl, '/tmp' );
-                        // echo '<pre>$newPhotoUrl: ' . print_r( $newPhotoUrl, true ) . '</pre>';
                         $res = $ImageUpload->storeImage(
                             '/tmp/' . $newPhotoUrl,
                             $refType,
@@ -689,7 +710,6 @@ class GlmMembersAdmin_import_index
                             $memberInfoId,
                             $caption
                         );
-                        // echo '<pre>$res: ' . print_r( $res, true ) . '</pre>';
                         if ( !$res ) {
                             $this->errors = true;
                         }
@@ -871,6 +891,27 @@ class GlmMembersAdmin_import_index
 
         }
 
+        if ( $this->numberProcessed ) {
+            $current_stats = get_option( 'glm-import-stats', false );
+            if ( $current_stats ) {
+                $glm_import_stats = unserialize( $current_stats );
+                $addedMembers   = $glm_import_stats['addedMembers'] + $this->addedMembers;
+                $updatedMembers = $glm_import_stats['updatedMembers'] + $this->updatedMembers;
+                $totalMembers   = $this->totalMembers;
+            } else {
+                $addedMembers   = $this->addedMembers;
+                $updatedMembers = $this->updatedMembers;
+                $totalMembers   = $this->totalMembers;
+            }
+            $glm_import_stats = serialize( array(
+                'numberProcessed' => $this->numberProcessed,
+                'addedMembers'    => $addedMembers,
+                'updatedMembers'  => $updatedMembers,
+                'totalMembers'    => $totalMembers,
+            ) );
+            update_option( 'glm-import-stats', $glm_import_stats );
+        }
+
         // Setup the template data array
         $templateData = array(
             //'addOnTabs'       => $addOnTabs,
@@ -971,6 +1012,7 @@ class GlmMembersAdmin_import_index
         $ch = curl_init($file);
         curl_setopt($ch, CURLOPT_TIMEOUT, 50);
         curl_setopt($ch, CURLOPT_FILE, $fp);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
         $imgData = curl_exec($ch);
         $httpCode = curl_getinfo($ch);
@@ -1326,10 +1368,12 @@ class GlmMembersAdmin_import_index
                 // Logo needs to be a complete url.
                 if ( isset( $data['logo'] ) ) {
                     $logo = filter_var( $data['logo'], FILTER_VALIDATE_URL );
+                    // echo '<pre>$logo: ' . print_r( $logo, true ) . '</pre>';
                     if ( $logo ) {
                         // Transfer the image over to temp directory.
                         $logoImage = $this->fetchRemoteImage( $logo, '/tmp' );
                         $res = $ImageUpload->storeImage( '/tmp/'. $logoImage );
+                        // echo '<pre>$res: ' . print_r( $res, true ) . '</pre>';
                         if ($res['newFileName']) {
                             $data['logo'] = $res['newFileName'];
                         }