Photos and Files for members now going in groups.
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 26 Jun 2017 19:40:57 +0000 (15:40 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 26 Jun 2017 19:40:57 +0000 (15:40 -0400)
Processing the member files and photos into groups now.

models/admin/ajax/fileUpload.php
models/admin/import/index.php
views/admin/import/filesProcess.html [new file with mode: 0644]
views/admin/import/filesValidate.html
views/admin/import/header.html
views/admin/import/photosProcess.html
views/admin/import/process.html
views/admin/import/processMembers.html

index a91553c..7e871f2 100644 (file)
@@ -204,7 +204,17 @@ class GlmMembersAdmin_ajax_fileUpload extends GlmDataImages
         if ( isset( $_FILES['file'] ) ) {
             $fileUploaded = move_uploaded_file( $_FILES['file']['tmp_name'], GLM_MEMBERS_PLUGIN_FILES_PATH . '/'. $newFilename );
         } else {
-            $fileUploaded = copy( $file['tmp_name'], GLM_MEMBERS_PLUGIN_FILES_PATH . '/'. $newFilename );
+            // Use wordpress remote get method
+            $wp_response = wp_remote_get( $file['tmp_name'] );
+            if ( is_array( $wp_response ) ) {
+                $fileContents = $wp_response['body'];
+                $fp = fopen( GLM_MEMBERS_PLUGIN_FILES_PATH . '/'. $newFilename, 'w' );
+                fwrite( $fp, $fileContents );
+                fclose( $fp );
+                $fileUploaded = true;
+            } else {
+                $fileUploaded = false;
+            }
         }
 
         // If we have a good image
index 443560d..edbfe47 100644 (file)
@@ -102,6 +102,21 @@ class GlmMembersAdmin_import_index
      */
     public $memberProcessCountPerRound = 20;
 
+    /**
+     * fileProcessCountPerRound
+     *
+     * @var float
+     * @access public
+     */
+    public $fileProcessCountPerRound = 10;
+    /**
+     * photoProcessCountPerRound
+     *
+     * @var float
+     * @access public
+     */
+    public $photoProcessCountPerRound = 10;
+
     /**
      * numberProcessed
      *
@@ -142,6 +157,14 @@ class GlmMembersAdmin_import_index
      */
     public $photosProcessed = 0;
 
+    /**
+     * filesProcessed
+     *
+     * @var float
+     * @access public
+     */
+    public $filesProcessed = 0;
+
     /**
      * processingComplete
      *
@@ -222,6 +245,12 @@ class GlmMembersAdmin_import_index
         if ( isset( $_REQUEST['numberProcessed'] ) ) {
             $this->numberProcessed = filter_var( $_REQUEST['numberProcessed'], FILTER_VALIDATE_INT );
         }
+        if ( isset( $_REQUEST['photosProcessed'] ) ) {
+            $this->photosProcessed = filter_var( $_REQUEST['photosProcessed'], FILTER_VALIDATE_INT );
+        }
+        if ( isset( $_REQUEST['filesProcessed'] ) ) {
+            $this->filesProcessed = filter_var( $_REQUEST['filesProcessed'], FILTER_VALIDATE_INT );
+        }
         // $fileData - The main files needed for the member import
         // - field:    input field name
         // - name:     file name
@@ -367,6 +396,8 @@ class GlmMembersAdmin_import_index
                     $success = $this->processFile( $uploadPath . '/' . $file['name'], $file['type'] );
                     if ( $success ) {
                         $fileData[$fileHeader]['results'] = "<p>$fileHeader file processed successfully.</p>";
+                    } else {
+                        $fileData[$fileHeader]['results'] = "<p>$fileHeader failed.</p>";
                     }
 
                     // Store the first files into wp options
@@ -444,7 +475,7 @@ class GlmMembersAdmin_import_index
             // If the file validates then we're ready to process
             $readyToProcess = $isValid;
             // Set the view file
-            $view= 'photosValidate.html';
+            $view = 'photosValidate.html';
             $fileData = '<pre>$_FILES: ' . print_r( $_FILES, true ) . '</pre>';
 
             break;
@@ -463,7 +494,7 @@ class GlmMembersAdmin_import_index
             // If the file validates then we're ready to process
             $readyToProcess = $isValid;
             // Set the view file
-            $view= 'filesValidate.html';
+            $view = 'filesValidate.html';
             $fileData = '<pre>$_FILES: ' . print_r( $_FILES, true ) . '</pre>';
 
             break;
@@ -477,22 +508,31 @@ class GlmMembersAdmin_import_index
             require_once GLM_MEMBERS_PLUGIN_PATH .  '/classes/data/dataMemberInfo.php';
             $memberInfoObj = new GlmDataMemberInfo( $this->wpdb, $this->config );
 
+            $start = 1;
+            if ( isset( $_REQUEST['start'] ) ) {
+                $start = filter_var( $_REQUEST['start'], FILTER_VALIDATE_INT );
+            }
+
             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>';
+                $photoData         = $this->readCSVFile( $uploadPath . '/photoData.csv' );
                 $this->totalPhotos = count( $photoData );
-                $this->photosProcessed = count( $photoData );
 
-                $refType = $this->config['ref_type_numb']['MemberInfo'];
+                $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>';
+                // Find out how many are left to process.
+                $testEnd = $start + $this->photoProcessCountPerRound;
+                $ending  = ( $testEnd <= $this->totalPhotos ) ? $testEnd : $this->totalPhotos;
+
+                // We have to add one to $ending (array starts at 1 not 0).
+                $ending++;
+                for ( $index = $start; $index < $ending; $index++ ) {
+                //foreach ( $photoData as $photo ) {
+                    $photo = $photoData[$index];
                     // 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(
@@ -506,14 +546,12 @@ class GlmMembersAdmin_import_index
                         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,
@@ -522,42 +560,56 @@ class GlmMembersAdmin_import_index
                             $photo['caption']
                         );
                     }
+                    $this->photosProcessed++;
 
                 }
 
                 // Set the view file
-                $view= 'photosProcess.html';
+                $view = 'photosProcess.html';
             } else {
                 // Set the view file
-                $view= 'photosValidate.html';
+                $view = 'photosValidate.html';
+            }
+            // 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->photosProcessed == $this->totalPhotos ) {
+                $this->processingComplete = true;
             }
             break;
 
         case 'filesProcess':
             // Setup the file processing
             require_once GLM_MEMBERS_PLUGIN_PATH . '/models/admin/ajax/fileUpload.php';
-            $FileUpload = new GlmMembersAdmin_ajax_imageUpload($this->wpdb, $this->config);
+            $FileUpload = new GlmMembersAdmin_ajax_fileUpload($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 );
 
+            $start = 1;
+            if ( isset( $_REQUEST['start'] ) ) {
+                $start = filter_var( $_REQUEST['start'], FILTER_VALIDATE_INT );
+            }
+
             if ( is_file( $uploadPath . '/filesData.csv' ) ) {
                 // Get the entire files file data
-                $filesData = $this->readCSVFile( $uploadPath . '/filesData.csv' );
-                //echo '<pre>$filesData: ' . print_r( $filesData, true ) . '</pre>';
-                $this->totalFiles     = count( $filesData );
-                $this->filesProcessed = count( $filesData );
+                $filesData        = $this->readCSVFile( $uploadPath . '/filesData.csv' );
+                $this->totalFiles = count( $filesData );
 
-                $refType = $this->config['ref_type_numb']['MemberInfo'];
+                $refType  = $this->config['ref_type_numb']['MemberInfo'];
                 $refTable = $this->config['ref_type_table'][$refType];
 
                 // Loop through the filesData array
-                foreach ( $filesData as $file ) {
-                    echo '<pre>$file: ' . print_r( $file, true ) . '</pre>';
+                // Find out how many are left to process.
+                $testEnd = $start + $this->fileProcessCountPerRound;
+                $ending  = ( $testEnd <= $this->totalFiles ) ? $testEnd : $this->totalFiles;
+
+                // We have to add one to $ending (array starts at 1 not 0).
+                $ending++;
+                for ( $index = $start; $index < $ending; $index++ ) {
+                    $file = $filesData[$index];
                     // If there's no url or the url is not valid then skip it.
-                    if ( $fileUrl = filter_var( $file['image'], FILTER_VALIDATE_URL ) ) {
-                        echo '<pre>$fileUrl: ' . print_r( $fileUrl, true ) . '</pre>';
+                    if ( $fileUrl = filter_var( $file['file_url'], FILTER_VALIDATE_URL ) ) {
                         // Need to first get the member id from the database
                         // It will match from the old_member_id field
                         $memberId = $this->wpdb->get_var(
@@ -571,37 +623,40 @@ class GlmMembersAdmin_import_index
                         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 = $FileUpload->storeFile(
                             $fileUrl,
                             $refType,
                             $refTable,
                             $memberInfoId,
-                            $file['caption']
+                            $file['file_name']
                         );
                     }
+                    $this->filesProcessed++;
 
                 }
 
                 // Set the view file
-                $view= 'filesProcess.html';
+                $view = 'filesProcess.html';
             } else {
                 // Set the view file
-                $view= 'filesValidate.html';
+                $view = 'filesValidate.html';
+            }
+            // 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->filesProcessed == $this->totalFiles ) {
+                $this->processingComplete = true;
             }
-            $view= 'filesValidate.html';
             break;
 
         case 'photos':
             // Set the view file
-            $view= 'photos.html';
+            $view = 'photos.html';
             unset( $fileData );
             if ( is_file( $uploadPath . '/photoData.csv' ) ) {
                 $fileExists = true;
@@ -667,6 +722,7 @@ class GlmMembersAdmin_import_index
             'totalPhotos'     => $this->totalPhotos,
             'totalFiles'      => $this->totalFiles,
             'photosProcessed' => $this->photosProcessed,
+            'filesProcessed'  => $this->filesProcessed,
             'completed'       => $this->processingComplete,
             'data'            => false,
             'fileData'        => $fileData,
@@ -1028,7 +1084,7 @@ class GlmMembersAdmin_import_index
 
                 // create truncated short_descr from descriptions - less tags, html encoded characters, newlines, tabs, etc.
                 $stripped = str_replace(
-                    php_eol,
+                    PHP_EOL,
                     '',
                     preg_replace( '/\t+/', '', preg_replace( "/&#?[a-z0-9]{2,8};/i", "", strip_tags( $data['descr'] ) ) )
                 );
diff --git a/views/admin/import/filesProcess.html b/views/admin/import/filesProcess.html
new file mode 100644 (file)
index 0000000..fc1d22d
--- /dev/null
@@ -0,0 +1,40 @@
+{include file='admin/import/header.html'}
+
+    <h2>Data Import Step 3: Process Member Files</h2>
+
+    <table class="glm-admin-table">
+        <tr>
+            <th>Total Files</th>
+            <td>{$totalFiles}</td>
+        </tr>
+        <tr>
+            <th>Processed Files</th>
+            <td>{$filesProcessed}</td>
+        </tr>
+        {if !$completed}
+            {$newStart = 1 + $filesProcessed}
+            <tr>
+                <td colspan="2">
+                    Please wait - Loading ...
+                </td>
+            </tr>
+        {/if}
+
+
+        {if $errors}
+            {foreach $errors as $error}
+                {$error}
+            {/foreach}
+        {/if}
+
+    </table>
+
+    {if !$completed}
+            {$newStart = 1 + $filesProcessed}
+    <script>
+    var test = '{$thisUrl}?page={$thisPage}&glm_action=index&option=filesProcess&start={$newStart}&filesProcessed={$filesProcessed}';
+    window.location.href = test;
+    </script>
+    {/if}
+
+{include file='admin/footer.html'}
index 1e2f502..a340f3e 100644 (file)
@@ -1,6 +1,6 @@
 {include file='admin/import/header.html'}
 
-<h2>Data Import: Photos Validation</h2>
+<h2>Data Import: Files Validation</h2>
 
 <table class="glm-admin-table">
 
index db8fc9e..4e38c07 100644 (file)
@@ -3,10 +3,15 @@
     <h2>{$glmPluginName} CSV Import</h2>
 
     <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>
+        <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 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>
+        <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 or $option===filesValidate} nav-tab-active{/if}">Import Files</a>
         {/if}
     </h2>
     <div id="glm-admin-content-container">
index 26a6936..4127b08 100644 (file)
@@ -1,6 +1,6 @@
 {include file='admin/import/header.html'}
 
-    <h2>Data Import Step 3: Process Members</h2>
+    <h2>Data Import Step 3: Process Member Photos</h2>
 
     <table class="glm-admin-table">
         <tr>
             <th>Processed Photos</th>
             <td>{$photosProcessed}</td>
         </tr>
+        {if !$completed}
+            {$newStart = 1 + $photosProcessed}
+            <tr>
+                <td colspan="2">
+                    Please wait - Loading ...
+                </td>
+            </tr>
+        {/if}
 
         {if $errors}
             {foreach $errors as $error}
 
     </table>
 
+    {if !$completed}
+            {$newStart = 1 + $photosProcessed}
+    <script>
+    var test = '{$thisUrl}?page={$thisPage}&glm_action=index&option=photosProcess&start={$newStart}&photosProcessed={$photosProcessed}';
+    window.location.href = test;
+    </script>
+    {/if}
+
 {include file='admin/footer.html'}
index 1237fd0..9a4f369 100644 (file)
@@ -1,14 +1,15 @@
 {include file='admin/import/header.html'}
 
     <h2>Data Import Step 3: Process Files</h2>
-
     <table class="glm-admin-table">
         {foreach $fileData as $fileHeader => $file}
-        <tr>
-            <td>
-                {$file.results}
-            </td>
-        </tr>
+            {if isset($file.results)}
+                <tr>
+                    <td>
+                        {$file.results}
+                    </td>
+                </tr>
+            {/if}
         {/foreach}
 
         {if $errors}
index 63052c7..477d2da 100644 (file)
         </tr>
         {if !$completed}
             {$newStart = 1 + $numberProcessed}
-        <tr>
-            <td colspan="2">
-                <a href="{$thisUrl}?page={$thisPage}&glm_action=index&option=processMembers&start={$newStart}&numberProcessed={$numberProcessed}">Process Next Count Members</a>
-            </td>
-        </tr>
+            <tr>
+                <td colspan="2">
+                    Please wait - Loading ...
+                </td>
+            </tr>
         {/if}
 
         {if $errors}