Work on the file upload for photos
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 22 Jun 2017 16:27:05 +0000 (12:27 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 22 Jun 2017 16:27:05 +0000 (12:27 -0400)
Validate the header line.

models/admin/import/index.php
views/admin/import/header.html
views/admin/import/index.html
views/admin/import/photos.html
views/admin/import/validate.html

index 902053c..5ec84a2 100644 (file)
@@ -182,10 +182,16 @@ class GlmMembersAdmin_import_index
      */
     public function modelAction ($actionData = false)
     {
-        $view      = 'index.html';
-        $failure   = false;
-        $option    = 'default';
-        $clearData = false;
+        $view        = 'index.html';
+        $failure     = false;
+        $option      = 'default';
+        $clearData   = false;
+        $haveMembers = false;
+        // Check to see if they have members
+        $haveMembers = $this->wpdb->get_var(
+            "SELECT count(id)
+               FROM " .  GLM_MEMBERS_PLUGIN_DB_PREFIX . "members"
+        );
         if ( isset( $_REQUEST['numberProcessed'] ) ) {
             $this->numberProcessed = filter_var( $_REQUEST['numberProcessed'], FILTER_VALIDATE_INT );
         }
@@ -378,16 +384,20 @@ class GlmMembersAdmin_import_index
 
         case 'photosValidate':
             if ( isset( $_FILES ) ) {
-                move_uploaded_file( $_FILES['photos_file']['tmp_name'], $uploadPath . '/photoData.csv'  );
+                move_uploaded_file( $_FILES['photos_file']['tmp_name'], $uploadPath . '/photoData.csv' );
             }
             // validate the header line
-            $fData = $this->readCSVFileHeaders( $uploadPath . '/photoData.csv' );
+            $fData   = $this->readCSVFileHeaders( $uploadPath . '/photoData.csv' );
+            $isValid = ( $fData == array( 'member_id', 'image', 'caption' ) );
+            // If the file validates then we're ready to process
+            $readyToProcess = $isValid;
+            $view= 'photos.html';
+            $fileData = '<pre>$_FILES: ' . print_r( $_FILES, true ) . '</pre>';
 
             break;
 
         case 'photos':
             $view= 'photos.html';
-            $fileData = '<pre>$_FILES: ' . print_r( $_FILES, true ) . '</pre>';
             break;
 
         case 'files':
@@ -409,6 +419,7 @@ class GlmMembersAdmin_import_index
 
         }
 
+        // Setup the template data array
         $templateData = array(
             'option'          => $option,
             'errors'          => $this->errors,
@@ -419,7 +430,8 @@ class GlmMembersAdmin_import_index
             'fileData'        => $fileData,
             'clearData'       => $clearData,
             'csvData'         => '<pre>$fileData: ' . print_r( $fileData, true ) . '</pre>',
-            'readyToProcess'  => $readyToProcess
+            'readyToProcess'  => $readyToProcess,
+            'haveMembers'     => $haveMembers,
         );
         // Return status, suggested view, and data to controller
         return array(
@@ -427,7 +439,7 @@ class GlmMembersAdmin_import_index
             'menuItemRedirect' => false,
             'modelRedirect'    => false,
             'view'             => 'admin/import/' . $view,
-            'data'             => $templateData
+            'data'             => $templateData,
         );
 
     }
index 69c87e1..64b0995 100644 (file)
@@ -4,8 +4,10 @@
 
     <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=files" class="nav-tab{if $option==files} nav-tab-active{/if}">Import Files</a>
+        {/if}
     </h2>
     <div id="glm-admin-content-container">
 
index 521c339..0c6e860 100644 (file)
@@ -29,7 +29,7 @@
                 <td>Clear Data</td>
                 <td>
                     <input type="hidden" name="clear_data" value="0">
-                    <input type="checkbox" name="clear_data" value="1">
+                    <input type="checkbox" name="clear_data" value="1" checked>
                 </td>
             </tr>
         </table>
index cbbb14e..a2af2a2 100644 (file)
@@ -3,7 +3,7 @@
     <h2>Data Import: Photos</h2>
     <form action="{$thisUrl}?page={$thisPage}" method="post" enctype="multipart/form-data">
         <input type="hidden" name="glm_action" value="index" />
-        <input type="hidden" name="option" value="photos" />
+        <input type="hidden" name="option" value="photosValidate" />
 
         <table class="glm-admin-table">
             <tr>
@@ -16,8 +16,4 @@
         <input type="submit" value="Continue" class="button button-primary submit-import">
     </form>
 
-    {if $fileData}
-        {$fileData}
-    {/if}
-
 {include file='admin/footer.html'}
index bf1f100..8672dd6 100644 (file)
                 All Member Data will be cleared before importing any files.
             </td>
         </tr>
+        {else}
+        <tr>
+            <td colspan="2">
+                No Member Data will be cleared before importing any files. No attempts will be made to prevent duplication of members.
+            </td>
+        </tr>
         {/if}
 
         {if $readyToProcess}
                 <a href="{$thisUrl}?page={$thisPage}&glm_action=index&option=process{if $clearData}&clear_data=1{/if}" class="button">Process Files</a>
             </td>
         </tr>
+        {else}
+        <tr>
+            <td colspan="2">
+                <p>One or more of your files are not the correct csv format. Please go back and try again.</p>
+                <a href="{$thisUrl}?page={$thisPage}&glm_action=index" class="button">Go Back</a>
+            </td>
+        </tr>
         {/if}
 
     </table>