Break up file processing.
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 14 Jun 2017 14:32:59 +0000 (10:32 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 14 Jun 2017 14:32:59 +0000 (10:32 -0400)
First process the amenity,category,region,county,city files.
Then process the members file.
Still have to break up the processing of members into smaller groups.
This will be so there's no timeout issues.

models/admin/import/index.php
views/admin/import/process.html
views/admin/import/processMembers.html [new file with mode: 0644]
views/admin/import/validate.html

index 9ff02e3..9b4ac94 100644 (file)
@@ -36,12 +36,62 @@ class GlmMembersAdmin_import_index
      */
     public $config;
 
-    public $regions = array();
-    public $counties = array();
+    /**
+     * amenities
+     *
+     * @var $amenities
+     * @access public
+     */
     public $amenities = array();
+    /**
+     * categories
+     *
+     * @var $categories
+     * @access public
+     */
     public $categories = array();
+    /**
+     * cities
+     *
+     * @var $cities
+     * @access public
+     */
     public $cities = array();
+    /**
+     * regions
+     *
+     * @var $regions
+     * @access public
+     */
+    public $regions = array();
+    /**
+     * counties
+     *
+     * @var $counties
+     * @access public
+     */
+    public $counties = array();
+    /**
+     * members
+     *
+     * @var $members
+     * @access public
+     */
     public $members = array();
+    /**
+     * errors
+     *
+     * @var $errors
+     * @access public
+     */
+    public $errors = array();
+    /**
+     * wpOptionPrefix
+     *
+     * @var $wpOptionPrefix
+     * @access public
+     */
+    public $wpOptionPrefix = 'glm_members_import_';
 
     /**
      * Constructor
@@ -146,6 +196,7 @@ class GlmMembersAdmin_import_index
                 'validate' => array(
                     'id', 'member_name', 'status', 'descr', 'short_descr',
                     'addr1', 'addr2', 'city', 'state', 'country', 'zip',
+                    'mailing_addr1', 'mailing_addr2', 'mailing_city', 'mailing_state', 'mailing_zip',
                     'region', 'county', 'lat', 'lon', 'phone', 'toll_free',
                     'url', 'reservation_url', 'email', 'logo', 'categories', 'amenities' ),
                 'type'     => 'member',
@@ -218,13 +269,67 @@ class GlmMembersAdmin_import_index
             }
             //exit;
             foreach ( $fileData as $fileHeader => $file ) {
-                if ( is_file( $uploadPath . '/' . $file['name'] ) ) {
-                    $fileData[$fileHeader]['results'] = $this->processFile( $uploadPath . '/' . $file['name'], $file['type'] );
+                if ( is_file( $uploadPath . '/' . $file['name'] ) && $fileHeader != 'Member' ) {
+                    //$fileData[$fileHeader]['results']
+                    $success = $this->processFile( $uploadPath . '/' . $file['name'], $file['type'] );
+                    if ( $success ) {
+                        $fileData[$fileHeader]['results'] = "<p>$fileHeader file processed successfully.</p>";
+                    }
+
+                    // Store the first files into wp options
+                    switch ( $file['type'] ) {
+                    case 'amenity':
+                        update_option( $this->wpOptionPrefix . $file['type'], $this->amenities );
+                        break;
+                    case 'category':
+                        update_option( $this->wpOptionPrefix . $file['type'], $this->categories );
+                        break;
+                    case 'city':
+                        update_option( $this->wpOptionPrefix . $file['type'], $this->cities );
+                        break;
+                    case 'region':
+                        update_option( $this->wpOptionPrefix . $file['type'], $this->regions );
+                        break;
+                    case 'county':
+                        update_option( $this->wpOptionPrefix . $file['type'], $this->counties );
+                        break;
+                    }
                 }
             }
+            if ( count( $this->errors ) == 0 ) {
+                $readyToProcess = true;
+            }
             $view = 'process.html';
             break;
 
+        case 'processMembers':
+            foreach ( $fileData as $fileHeader => $file ) {
+                if ( is_file( $uploadPath . '/' . $file['name'] ) && $fileHeader != 'Member' ) {
+                    // Retrieve the first files from wp options
+                    switch ( $file['type'] ) {
+                    case 'amenity':
+                        $this->amenities = get_option( $this->wpOptionPrefix . $file['type'], array() );
+                        break;
+                    case 'category':
+                        $this->categories = get_option( $this->wpOptionPrefix . $file['type'], array() );
+                        break;
+                    case 'city':
+                        $this->cities = get_option( $this->wpOptionPrefix . $file['type'], array() );
+                        break;
+                    case 'region':
+                        $this->regions = get_option( $this->wpOptionPrefix . $file['type'], array() );
+                        break;
+                    case 'county':
+                        $this->counties = get_option( $this->wpOptionPrefix . $file['type'], array() );
+                        break;
+                    }
+                } else if ( is_file( $uploadPath . '/' . $file['name'] ) && $fileHeader == 'Member' ) {
+                    $fileData[$fileHeader]['results'] = $this->processFile( $uploadPath . '/' . $file['name'], $file['type'] );
+                }
+            }
+            $view = 'processMembers.html';
+            break;
+
         default:
             $view = 'index.html';
             // check upload dir to see if they have any files in yet
@@ -240,6 +345,7 @@ class GlmMembersAdmin_import_index
         }
 
         $templateData = array(
+            'errors'         => $this->errors,
             'data'           => false,
             'fileData'       => $fileData,
             'clearData'      => $clearData,
@@ -291,9 +397,22 @@ class GlmMembersAdmin_import_index
         return $fileData;
     }
 
+    /**
+     * processFile
+     *
+     * Processes a file for the member import. Based on the type of file.
+     * Report errors in the $this->errors array.
+     *
+     * @param mixed $fileName File name for the csv file.
+     * @param mixed $type     Type (category,amenity,city)
+
+     * @access public
+     * @return void
+     */
     public function processFile( $fileName, $type )
     {
         $ret = '';
+        $errorCount = 0;
         switch ( $type ) {
         case 'amenity':
             $amenityData = $this->readCSVFile( $fileName );
@@ -311,17 +430,23 @@ class GlmMembersAdmin_import_index
                 if ( $amenityId ) {
                     $this->amenities[$data['id']]['new_id'] = $amenityId;
                 } else {
-                    $this->wpdb->insert(
+                    $success = $this->wpdb->insert(
                         GLM_MEMBERS_PLUGIN_DB_PREFIX . 'amenities',
                         array(
                             'name' => $data['name']
                         ),
                         '%s'
                     );
-                    $this->amenities[$data['id']]['new_id'] = $this->wpdb->insert_id;
+                    if ( $success ) {
+                        $this->amenities[$data['id']]['new_id'] = $this->wpdb->insert_id;
+                    } else {
+                        $errorCount++;
+                        $this->errors[] = '<p>There was an error adding amenity</p>';
+                    }
                 }
             }
             $ret .= '<p>...</p>';
+            return ( $errorCount == 0 );
             break;
         case 'category':
             $categoryData = $this->readCSVFile( $fileName );
@@ -339,17 +464,23 @@ class GlmMembersAdmin_import_index
                 if ( $categoryId ) {
                     $this->categories[$data['id']]['new_id'] = $categoryId;
                 } else {
-                    $this->wpdb->insert(
+                    $success = $this->wpdb->insert(
                         GLM_MEMBERS_PLUGIN_DB_PREFIX . 'categories',
                         array(
                             'name' => $data['name']
                         ),
                         '%s'
                     );
-                    $this->categories[$data['id']]['new_id'] = $this->wpdb->insert_id;
+                    if ( $success ) {
+                        $this->categories[$data['id']]['new_id'] = $this->wpdb->insert_id;
+                    } else {
+                        $errorCount++;
+                        $this->errors[] = '<p>There was an error adding category</p>';
+                    }
                 }
             }
             $ret .= '<p>...</p>';
+            return ( $errorCount == 0 );
             break;
         case 'city':
             $cityData = $this->readCSVFile( $fileName );
@@ -367,17 +498,23 @@ class GlmMembersAdmin_import_index
                 if ( $cityId ) {
                     $this->cities[$data['id']]['new_id'] = $cityId;
                 } else {
-                    $this->wpdb->insert(
+                    $success = $this->wpdb->insert(
                         GLM_MEMBERS_PLUGIN_DB_PREFIX . 'cities',
                         array(
                             'name' => $data['name']
                         ),
                         '%s'
                     );
-                    $this->cities[$data['id']]['new_id'] = $this->wpdb->insert_id;
+                    if ( $success ) {
+                        $this->cities[$data['id']]['new_id'] = $this->wpdb->insert_id;
+                    } else {
+                        $errorCount++;
+                        $this->errors[] = '<p>There was an error adding city</p>';
+                    }
                 }
             }
             $ret .= '<p>...</p>';
+            return ( $errorCount == 0 );
             break;
         case 'region':
             $regionData = $this->readCSVFile( $fileName );
@@ -396,7 +533,7 @@ class GlmMembersAdmin_import_index
                 if ( $regionId ) {
                     $this->regions[$data['id']]['new_id'] = $regionId;
                 } else {
-                    $this->wpdb->insert(
+                    $success = $this->wpdb->insert(
                         GLM_MEMBERS_PLUGIN_DB_PREFIX . 'regions',
                         array(
                             'name'        => $data['name'],
@@ -405,10 +542,16 @@ class GlmMembersAdmin_import_index
                         ),
                         '%s'
                     );
-                    $this->regions[$data['id']]['new_id'] = $this->wpdb->insert_id;
+                    if ( $success ) {
+                        $this->regions[$data['id']]['new_id'] = $this->wpdb->insert_id;
+                    } else {
+                        $errorCount++;
+                        $this->errors[] = '<p>There was an error adding region</p>';
+                    }
                 }
             }
             $ret .= '<p>...</p>';
+            return ( $errorCount == 0 );
             break;
         case 'county':
             $countyData = $this->readCSVFile( $fileName );
@@ -427,7 +570,7 @@ class GlmMembersAdmin_import_index
                 if ( $countyId ) {
                     $this->counties[$data['id']]['new_id'] = $countyId;
                 } else {
-                    $this->wpdb->insert(
+                    $success = $this->wpdb->insert(
                         GLM_MEMBERS_PLUGIN_DB_PREFIX . 'counties',
                         array(
                             'name'        => $data['name'],
@@ -436,10 +579,16 @@ class GlmMembersAdmin_import_index
                         ),
                         '%s'
                     );
-                    $this->counties[$data['id']]['new_id'] = $this->wpdb->insert_id;
+                    if ( $success ) {
+                        $this->counties[$data['id']]['new_id'] = $this->wpdb->insert_id;
+                    } else {
+                        $errorCount++;
+                        $this->errors[] = '<p>There was an error adding county</p>';
+                    }
                 }
             }
             $ret .= '<p>...</p>';
+            return ( $errorCount == 0 );
             break;
         case 'member':
             $memberData = $this->readCSVFile( $fileName );
@@ -502,62 +651,80 @@ class GlmMembersAdmin_import_index
                 );
                 $memberId = $this->wpdb->insert_id;
                 $this->members[$data['id']]['new_id'] = $this->wpdb->insert_id;
+
+                // Logo needs to be a complete url.
+
+
                 // Add the member info record
                 $insert = $this->wpdb->insert(
                     GLM_MEMBERS_PLUGIN_DB_PREFIX . 'member_info',
                     array(
-                        'member'         => $memberId,
-                        'member_name'    => $data['member_name'],
-                        'status'         => $this->config['status_numb']['Active'],
-                        'reference_name' => 'Imported Member Information',
-                        'descr'          => $data['descr'],
-                        'short_descr'    => $data['short_descr'],
-                        'addr1'          => $data['addr1'],
-                        'addr2'          => $data['addr2'],
-                        'city'           => ((isset($data['city']) && isset($this->cities[$data['city']]['new_id'])) ? $this->cities[$data['city']]['new_id']: 0),
-                        'state'          => $data['state'],
-                        'country'        => $data['country'],
-                        'region'         => ((isset($data['region']) && isset($this->regions[$data['region']]['new_id'])) ? $this->regions[$data['region']]['new_id']: 0),
-                        'county'         => ((isset($data['county']) && isset($this->counties[$data['county']]['new_id'])) ? $this->counties[$data['county']]['new_id']: 0),
-                        'zip'            => $data['zip'],
-                        'lat'            => $data['lat'],
-                        'lon'            => $data['lon'],
-                        'phone'          => $data['phone'],
-                        'toll_free'      => $data['toll_free'],
-                        'url'            => $data['url'],
-                        'email'          => $data['email'],
-                        'logo'           => $data['logo'],
-                        'notes'          => '',
-                        'create_time'    => date( 'Y-m-d' ),
-                        'modify_time'    => date( 'Y-m-d' ),
-                        'cc_type'        => 0,
+                        'member'          => $memberId,
+                        'member_name'     => $data['member_name'],
+                        'status'          => $this->config['status_numb']['Active'],
+                        'reference_name'  => 'Imported Member Information',
+                        'descr'           => $data['descr'],
+                        'short_descr'     => $data['short_descr'],
+                        'addr1'           => $data['addr1'],
+                        'addr2'           => $data['addr2'],
+                        'city'            => ((isset($data['city']) && isset($this->cities[$data['city']]['new_id'])) ? $this->cities[$data['city']]['new_id']: 0),
+                        'state'           => $data['state'],
+                        'country'         => $data['country'],
+                        'region'          => ((isset($data['region']) && isset($this->regions[$data['region']]['new_id'])) ? $this->regions[$data['region']]['new_id']: 0),
+                        'county'          => ((isset($data['county']) && isset($this->counties[$data['county']]['new_id'])) ? $this->counties[$data['county']]['new_id']: 0),
+                        'zip'             => $data['zip'],
+
+                        'mailing_addr1'   => $data['mailing_addr1'],
+                        'mailing_addr2'   => $data['mailing_addr2'],
+                        'mailing_city'    => ((isset($data['mailing_city']) && isset($this->cities[$data['mailing_city']]['new_id'])) ? $this->cities[$data['mailing_city']]['new_id']: 0),
+                        'mailing_state'   => $data['mailing_state'],
+                        'mailing_zip'     => $data['mailing_zip'],
+
+                        'lat'             => $data['lat'],
+                        'lon'             => $data['lon'],
+                        'phone'           => $data['phone'],
+                        'toll_free'       => $data['toll_free'],
+                        'url'             => $data['url'],
+                        'reservation_url' => $data['reservation_url'],
+                        'email'           => $data['email'],
+                        'logo'            => $data['logo'],
+                        'notes'           => '',
+                        'create_time'     => date( 'Y-m-d' ),
+                        'modify_time'     => date( 'Y-m-d' ),
+                        'cc_type'         => 0,
                     ),
                     array(
-                        '%d',
-                        '%s',
-                        '%d',
-                        '%s',
-                        '%s',
-                        '%s',
-                        '%s',
-                        '%s',
-                        '%d',
-                        '%s',
-                        '%s',
-                        '%d',
-                        '%d',
-                        '%s',
-                        '%s',
-                        '%s',
-                        '%s',
-                        '%s',
-                        '%s',
-                        '%s',
-                        '%s',
-                        '%s',
-                        '%s',
-                        '%s',
-                        '%d',
+                        '%d', // member
+                        '%s', // member_name
+                        '%d', // status
+                        '%s', // reference_name
+                        '%s', // descr
+                        '%s', // short_descr
+                        '%s', // addr1
+                        '%s', // addr2
+                        '%d', // city
+                        '%s', // state
+                        '%d', // country
+                        '%d', // region
+                        '%s', // county
+                        '%s', // zip
+                        '%s', // mailing_addr1
+                        '%s', // mailing_addr2
+                        '%d', // mailing_city
+                        '%s', // mailing_state
+                        '%s', // mailing_zip
+                        '%s', // lat
+                        '%s', // lon
+                        '%s', // phone
+                        '%s', // toll_free
+                        '%s', // url
+                        '%s', // reservation_url
+                        '%s', // email
+                        '%s', // logo
+                        '%s', // notes
+                        '%s', // create_time
+                        '%s', // modify_time
+                        '%d', // cc_type
                     )
                 );
                 if ( !$insert ) {
index 98676f5..1237fd0 100644 (file)
@@ -1,6 +1,6 @@
 {include file='admin/import/header.html'}
 
-    <h2>Data Import Step 3: Process files</h2>
+    <h2>Data Import Step 3: Process Files</h2>
 
     <table class="glm-admin-table">
         {foreach $fileData as $fileHeader => $file}
         </tr>
         {/foreach}
 
+        {if $errors}
+            {foreach $errors as $error}
+                {$error}
+            {/foreach}
+        {/if}
+
+        {if $readyToProcess}
+        <tr>
+            <td colspan="2">
+                <a href="{$thisUrl}?page={$thisPage}&glm_action=index&option=processMembers" class="button">Process Member File</a>
+            </td>
+        </tr>
+        {/if}
+
     </table>
 
 {include file='admin/footer.html'}
diff --git a/views/admin/import/processMembers.html b/views/admin/import/processMembers.html
new file mode 100644 (file)
index 0000000..ef702dc
--- /dev/null
@@ -0,0 +1,22 @@
+{include file='admin/import/header.html'}
+
+    <h2>Data Import Step 3: Process Members</h2>
+
+    <table class="glm-admin-table">
+        {foreach $fileData as $fileHeader => $file}
+        <tr>
+            <td>
+                {$file.results}
+            </td>
+        </tr>
+        {/foreach}
+
+        {if $errors}
+            {foreach $errors as $error}
+                {$error}
+            {/foreach}
+        {/if}
+
+    </table>
+
+{include file='admin/footer.html'}
index b4c1b55..bf1f100 100644 (file)
@@ -35,6 +35,7 @@
             </td>
         </tr>
         {/if}
+
     </table>
 
 {include file='admin/footer.html'}