Photos upload now working
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 22 Jun 2017 20:54:35 +0000 (16:54 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 22 Jun 2017 20:54:35 +0000 (16:54 -0400)
Need to break up like members for really huge files.

models/admin/import/index.php
views/admin/import/header.html
views/admin/import/photosProcess.html [new file with mode: 0644]
views/admin/import/photosValidate.html [new file with mode: 0644]

index 5ec84a2..357fad3 100644 (file)
@@ -118,6 +118,22 @@ class GlmMembersAdmin_import_index
      */
     public $totalMembers = 0;
 
+    /**
+     * totalPhotos
+     *
+     * @var float
+     * @access public
+     */
+    public $totalPhotos = 0;
+
+    /**
+     * photosProcessed
+     *
+     * @var float
+     * @access public
+     */
+    public $photosProcessed = 0;
+
     /**
      * processingComplete
      *
@@ -187,6 +203,7 @@ class GlmMembersAdmin_import_index
         $option      = 'default';
         $clearData   = false;
         $haveMembers = false;
+        $fileExists  = false;
         // Check to see if they have members
         $haveMembers = $this->wpdb->get_var(
             "SELECT count(id)
@@ -386,16 +403,83 @@ class GlmMembersAdmin_import_index
             if ( isset( $_FILES ) ) {
                 move_uploaded_file( $_FILES['photos_file']['tmp_name'], $uploadPath . '/photoData.csv' );
             }
+            if ( is_file( $uploadPath . '/photoData.csv' ) ) {
+                $fileExists = true;
+            }
             // validate the header line
-            $fData   = $this->readCSVFileHeaders( $uploadPath . '/photoData.csv' );
-            $isValid = ( $fData == array( 'member_id', 'image', 'caption' ) );
+            $fData    = $this->readCSVFileHeaders( $uploadPath . '/photoData.csv' );
+            $validate = array( 'member_id', 'image', 'caption' );
+            $isValid  = ( $fData == $validate );
             // If the file validates then we're ready to process
             $readyToProcess = $isValid;
-            $view= 'photos.html';
+            $view= 'photosValidate.html';
             $fileData = '<pre>$_FILES: ' . print_r( $_FILES, true ) . '</pre>';
 
             break;
 
+        case 'photosProcess':
+            // Setup the image processing
+            require_once GLM_MEMBERS_PLUGIN_PATH . '/models/admin/ajax/imageUpload.php';
+            $ImageUpload = new GlmMembersAdmin_ajax_imageUpload($this->wpdb, $this->config);
+
+            // Setup the member_info class
+            require_once GLM_MEMBERS_PLUGIN_PATH .  '/classes/data/dataMemberInfo.php';
+            $memberInfoObj = new GlmDataMemberInfo( $this->wpdb, $this->config );
+
+            if ( is_file( $uploadPath . '/photoData.csv' ) ) {
+                // Get the entire photo file data
+                $photoData = $this->readCSVFile( $uploadPath . '/photoData.csv' );
+                //echo '<pre>$photoData: ' . print_r( $photoData, true ) . '</pre>';
+                $this->totalPhotos = count( $photoData );
+                $this->photosProcessed = count( $photoData );
+
+                $refType = $this->config['ref_type_numb']['MemberInfo'];
+                $refTable = $this->config['ref_type_table'][$refType];
+
+                // Loop through the photoData array
+                foreach ( $photoData as $photo ) {
+                    echo '<pre>$photo: ' . print_r( $photo, 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 ) ) {
+                        echo '<pre>$photoUrl: ' . print_r( $photoUrl, true ) . '</pre>';
+                        // 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",
+                                $photo['member_id']
+                            )
+                        );
+                        if ( !$memberId ) {
+                            continue;
+                        }
+                        echo '<pre>$memberId: ' . print_r( $memberId, true ) . '</pre>';
+                        // Now that we have the member id we need to get the
+                        // id for the active member info record
+                        $memberInfoId = $memberInfoObj->getActiveInfoIdForMember( $memberId );
+                        if ( !$memberInfoId ) {
+                            continue;
+                        }
+                        echo '<pre>$memberInfoId: ' . print_r( $memberInfoId, true ) . '</pre>';
+                        $res = $ImageUpload->storeImage(
+                            $photoUrl,
+                            $refType,
+                            $refTable,
+                            $memberInfoId,
+                            $photo['caption']
+                        );
+                    }
+
+                }
+
+                $view= 'photosProcess.html';
+            } else {
+                $view= 'photosValidate.html';
+            }
+            break;
+
         case 'photos':
             $view= 'photos.html';
             break;
@@ -421,10 +505,13 @@ class GlmMembersAdmin_import_index
 
         // Setup the template data array
         $templateData = array(
+            'fileExists'      => $fileExists,
             'option'          => $option,
             'errors'          => $this->errors,
             'numberProcessed' => $this->numberProcessed,
             'totalMembers'    => $this->totalMembers,
+            'totalPhotos'     => $this->totalPhotos,
+            'photosProcessed' => $this->photosProcessed,
             'completed'       => $this->processingComplete,
             'data'            => false,
             'fileData'        => $fileData,
@@ -432,6 +519,7 @@ class GlmMembersAdmin_import_index
             'csvData'         => '<pre>$fileData: ' . print_r( $fileData, true ) . '</pre>',
             'readyToProcess'  => $readyToProcess,
             'haveMembers'     => $haveMembers,
+            'isValid'         => $isValid,
         );
         // Return status, suggested view, and data to controller
         return array(
index 64b0995..db8fc9e 100644 (file)
@@ -5,7 +5,7 @@
     <h2 class="nav-tab-wrapper">
         <a href="{$thisUrl}?page={$thisPage}&glm_action=index" class="nav-tab{if $option==default} nav-tab-active{/if}">Import CSV</a>
         {if $haveMembers}
-        <a href="{$thisUrl}?page={$thisPage}&glm_action=index&option=photos" class="nav-tab{if $option==photos} nav-tab-active{/if}">Import Photos</a>
+        <a href="{$thisUrl}?page={$thisPage}&glm_action=index&option=photos" class="nav-tab{if $option==photos or $option==photosValidate} nav-tab-active{/if}">Import Photos</a>
         <a href="{$thisUrl}?page={$thisPage}&glm_action=index&option=files" class="nav-tab{if $option==files} nav-tab-active{/if}">Import Files</a>
         {/if}
     </h2>
diff --git a/views/admin/import/photosProcess.html b/views/admin/import/photosProcess.html
new file mode 100644 (file)
index 0000000..26a6936
--- /dev/null
@@ -0,0 +1,23 @@
+{include file='admin/import/header.html'}
+
+    <h2>Data Import Step 3: Process Members</h2>
+
+    <table class="glm-admin-table">
+        <tr>
+            <th>Total Photos</th>
+            <td>{$totalPhotos}</td>
+        </tr>
+        <tr>
+            <th>Processed Photos</th>
+            <td>{$photosProcessed}</td>
+        </tr>
+
+        {if $errors}
+            {foreach $errors as $error}
+                {$error}
+            {/foreach}
+        {/if}
+
+    </table>
+
+{include file='admin/footer.html'}
diff --git a/views/admin/import/photosValidate.html b/views/admin/import/photosValidate.html
new file mode 100644 (file)
index 0000000..e8c03e4
--- /dev/null
@@ -0,0 +1,31 @@
+{include file='admin/import/header.html'}
+
+<h2>Data Import: Photos Validation</h2>
+
+<table class="glm-admin-table">
+
+    <tr>
+        <td>
+            {if $fileExists}
+                photoData.csv
+            {/if}
+        </td>
+        <td>
+            {if $isValid}
+                Is Valid
+            {else}
+                Not Valid
+            {/if}
+        </td>
+    </tr>
+        {if $readyToProcess}
+        <tr>
+            <td colspan="2">
+                <a href="{$thisUrl}?page={$thisPage}&glm_action=index&option=photosProcess" class="button">Process Photos</a>
+            </td>
+        </tr>
+        {/if}
+
+</table>
+
+{include file='admin/footer.html'}