WIP working on imports
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 6 Jun 2017 19:11:56 +0000 (15:11 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 6 Jun 2017 19:11:56 +0000 (15:11 -0400)
city import
county import
region import
working on member and profile import.

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

index c0e2a4e..2ec0279 100644 (file)
@@ -103,6 +103,7 @@ class GlmMembersAdmin_import_index
                 'validate' => function( $row ) {
                     return ( $row === array( 'id', 'name' ) );
                 },
+                'type'     => 'city',
             ),
             'Region' => array(
                 'field'    => 'region_file',
@@ -111,6 +112,7 @@ class GlmMembersAdmin_import_index
                 'validate' => function( $row ) {
                     return ( $row === array( 'id', 'name' ) );
                 },
+                'type'     => 'region',
             ),
             'County' => array(
                 'field'    => 'county_file',
@@ -119,6 +121,7 @@ class GlmMembersAdmin_import_index
                 'validate' => function( $row ) {
                     return ( $row === array( 'id', 'name' ) );
                 },
+                'type'     => 'county',
             ),
             'Member' => array(
                 'field'    => 'member_file',
@@ -127,6 +130,7 @@ class GlmMembersAdmin_import_index
                 'validate' => function( $row ) {
                     return ( $row === array( 'id', 'member_name', 'status', 'descr', 'short_descr', 'addr1', 'addr2', 'city', 'state', 'country', 'zip', 'region', 'county', 'lat', 'lon', 'phone', 'toll_free', 'url', 'reservation_url', 'email', 'logo' ) );
                 },
+                'type'     => 'member',
             ),
         );
 
@@ -144,40 +148,49 @@ class GlmMembersAdmin_import_index
 
         switch( $option ) {
 
-            case 'import_files';
-                $view = 'validate.html';
-                // Move any files uploaded
-                //echo '<pre>$_FILES: ' . print_r( $_FILES, true ) . '</pre>';
-                if ( isset( $_FILES ) ) {
-                    foreach ( $fileData as $fileHeader => $file ) {
-                        if ( !$_FILES[$file['field']]['error'] ) {
-                            move_uploaded_file( $_FILES[$file['field']]['tmp_name'], $uploadPath . '/'. $file['name'] );
-                        }
-                    }
-                }
-                // Check that each file exists
+        case 'import_files';
+            $view = 'validate.html';
+            // Move any files uploaded
+            //echo '<pre>$_FILES: ' . print_r( $_FILES, true ) . '</pre>';
+            if ( isset( $_FILES ) ) {
                 foreach ( $fileData as $fileHeader => $file ) {
-                    if ( is_file( $uploadPath . '/' . $file['name'] ) ) {
-                        $fileData[$fileHeader]['exists']  = true;
-                        $fData = $this->readCSVFileHeaders( $uploadPath . '/' . $file['name'] );
-                        $fileData[$fileHeader]['data']    = $fData;
-                        $fileData[$fileHeader]['isValid'] = call_user_func( $file['validate'], $fData );
+                    if ( !$_FILES[$file['field']]['error'] ) {
+                        move_uploaded_file( $_FILES[$file['field']]['tmp_name'], $uploadPath . '/'. $file['name'] );
                     }
                 }
+            }
+            // Check that each file exists
+            foreach ( $fileData as $fileHeader => $file ) {
+                if ( is_file( $uploadPath . '/' . $file['name'] ) ) {
+                    $fileData[$fileHeader]['exists']  = true;
+                    $fData = $this->readCSVFileHeaders( $uploadPath . '/' . $file['name'] );
+                    $fileData[$fileHeader]['data']    = $fData;
+                    $fileData[$fileHeader]['isValid'] = call_user_func( $file['validate'], $fData );
+                }
+            }
 
-                break;
+            break;
 
-            default:
-                $view = 'index.html';
-                // check upload dir to see if they have any files in yet
-                foreach ( $fileData as $fileHeader => $file ) {
-                    if ( is_file( $uploadPath . '/' . $file['name'] ) ) {
-                        $fileData[$fileHeader]['exists'] = true;
-                        $fileData[$fileHeader]['mtime']  = filemtime( $uploadPath . '/' . $file['name'] );
-                    }
+        case 'process':
+            foreach ( $fileData as $fileHeader => $file ) {
+                if ( is_file( $uploadPath . '/' . $file['name'] ) ) {
+                    $fileData[$fileHeader]['results'] = $this->processFile( $uploadPath . '/' . $file['name'], $file['type'] );
                 }
+            }
+            $view = 'process.html';
+            break;
 
-                break;
+        default:
+            $view = 'index.html';
+            // check upload dir to see if they have any files in yet
+            foreach ( $fileData as $fileHeader => $file ) {
+                if ( is_file( $uploadPath . '/' . $file['name'] ) ) {
+                    $fileData[$fileHeader]['exists'] = true;
+                    $fileData[$fileHeader]['mtime']  = filemtime( $uploadPath . '/' . $file['name'] );
+                }
+            }
+
+            break;
 
         }
 
@@ -231,4 +244,178 @@ class GlmMembersAdmin_import_index
         return $fileData;
     }
 
+    public function processFile( $fileName, $type )
+    {
+        $city = $region = $county = $member = array();
+        $ret = '';
+        switch ( $type ) {
+        case 'city':
+            $cityData = $this->readCSVFile( $fileName );
+            $ret .= '<p>Processing City File</p>';
+            $ret .= '<pre>$cityData: ' . print_r( $cityData, true ) . '</pre>';
+            foreach ( $cityData as $data ) {
+                // import city names if not found
+                $cityId = $this->wpdb->get_var(
+                    $this->wpdb->prepare(
+                        "SELECT id
+                           FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "cities
+                          WHERE name = %s",
+                        $data['name']
+                    )
+                );
+                if ( $cityId ) {
+                    $city[$data['id']]['new_id'] = $cityId;
+                } else {
+                    $this->wpdb->insert(
+                        GLM_MEMBERS_PLUGIN_DB_PREFIX . 'cities',
+                        array(
+                            'name' => $data['name']
+                        ),
+                        '%s'
+                    );
+                    $city[$data['id']]['new_id'] = $this->wpdb->insert_id;
+                }
+            }
+            $ret .= '<pre>$city: ' . print_r( $city, true ) . '</pre>';
+            break;
+        case 'region':
+            $regionData = $this->readCSVFile( $fileName );
+            $ret .= '<p>Processing Region File</p>';
+            $ret .= '<pre>$regionData: ' . print_r( $regionData, true ) . '</pre>';
+            foreach ( $regionData as $data ) {
+                // import region names if not found
+                $regionId = $this->wpdb->get_var(
+                    $this->wpdb->prepare(
+                        "SELECT id
+                           FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "regions
+                          WHERE name = %s",
+                        $data['name']
+
+                    )
+                );
+                if ( $regionId ) {
+                    $region[$data['id']]['new_id'] = $regionId;
+                } else {
+                    $this->wpdb->insert(
+                        GLM_MEMBERS_PLUGIN_DB_PREFIX . 'regions',
+                        array(
+                            'name'        => $data['name'],
+                            'descr'       => '',
+                            'short_descr' => ''
+                        ),
+                        '%s'
+                    );
+                    $region[$data['id']]['new_id'] = $this->wpdb->insert_id;
+                }
+            }
+            $ret .= '<pre>$region: ' . print_r( $region, true ) . '</pre>';
+            break;
+        case 'county':
+            $countyData = $this->readCSVFile( $fileName );
+            $ret = '<pre>$countyData: ' . print_r( $countyData, true ) . '</pre>';
+            foreach ( $countyData as $data ) {
+                // import county names if not found
+                $countyId = $this->wpdb->get_var(
+                    $this->wpdb->prepare(
+                        "SELECT id
+                           FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "counties
+                          WHERE name = %s",
+                        $data['name']
+
+                    )
+                );
+                if ( $countyId ) {
+                    $county[$data['id']]['new_id'] = $countyId;
+                } else {
+                    $this->wpdb->insert(
+                        GLM_MEMBERS_PLUGIN_DB_PREFIX . 'counties',
+                        array(
+                            'name'        => $data['name'],
+                            'descr'       => '',
+                            'short_descr' => ''
+                        ),
+                        '%s'
+                    );
+                    $county[$data['id']]['new_id'] = $this->wpdb->insert_id;
+                }
+            }
+            $ret .= '<pre>$county: ' . print_r( $county, true ) . '</pre>';
+            break;
+        case 'member':
+            $memberData = $this->readCSVFile( $fileName );
+            $ret = '<pre>$memberData: ' . print_r( $memberData, true ) . '</pre>';
+            foreach ( $memberData as $data ) {
+                // Check for duplicate member
+                $memberId = $this->wpdb->get_var(
+                    $this->wpdb->prepare(
+                        "SELECT id
+                           FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members
+                          WHERE name = %s",
+                        $data['member_name']
+                    )
+                );
+                if ( $memberId ) {
+                    $member[$data['id']]['new_id'] = $memberId;
+                } else {
+                    $this->wpdb->insert(
+                        GLM_MEMBERS_PLUGIN_DB_PREFIX . 'members',
+                        array(
+                            'access'        => $access,
+                            'member_type'   => $defaultMemberType,
+                            'created'       => date( 'Y-m-d' ),
+                            'name'          => $data['member_name'],
+                            'member_slug'   => sanitize_title( $data['member_name'] ),
+                            'old_member_id' => $data['id']
+                        ),
+                        array(
+                            '%d',
+                            '%d',
+                            '%s',
+                            '%s',
+                            '%s',
+                            '%d'
+                        )
+                    );
+                    $memberId = $this->wpdb->insert_id;
+                    $member[$data['id']]['new_id'] = $this->wpdb->insert_id;
+                }
+            }
+            // Add the member info record
+            $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'           => $data['city'],
+                    'state'          => $data['state'],
+                    'country'        => $data['country'],
+                    'region'         => $data['region'],
+                    'county'         => $data['county'],
+                    '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' ),
+                ),
+                array()
+            );
+            break;
+        default:
+            break;
+        }
+        return $ret;
+    }
+
 }
diff --git a/views/admin/import/process.html b/views/admin/import/process.html
new file mode 100644 (file)
index 0000000..9abc56b
--- /dev/null
@@ -0,0 +1,17 @@
+{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>
+        {/foreach}
+
+        <a href="{$thisUrl}?page={$thisPage}&glm_action=index&option=process" class="button">Process Files</a>
+    </table>
+
+{include file='admin/footer.html'}
index bd22882..84610f7 100644 (file)
@@ -20,7 +20,7 @@
         </tr>
         {/foreach}
 
-        <a href="#" class="button">Process Files</a>
+        <a href="{$thisUrl}?page={$thisPage}&glm_action=index&option=process" class="button">Process Files</a>
     </table>
 
 {include file='admin/footer.html'}