Worked on member import
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 5 Apr 2017 21:03:13 +0000 (17:03 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 5 Apr 2017 21:03:13 +0000 (17:03 -0400)
Have member categories, amenities and member types setup.
Working on member record.

models/admin/management/import/brewbakersImport.php

index 125348e..cec72b9 100644 (file)
@@ -24,9 +24,15 @@ if ( !$db ) {
 if ( !$failure ) {
     // Grab the RV data
     $sql = "
-    SELECT *
-      FROM rvs
-     ORDER BY id";
+    SELECT r.*,
+           b.name as brand_name,
+           c.name as class_name,
+           man.name as manufacturer_name
+      FROM rvs r, brands b, manufacturer man, class c
+     WHERE r.brand = b.id
+       AND r.class = c.id
+       AND r.manufacturer = man.id
+     ORDER BY r.id";
     $res = pg_query( $db, $sql );
     $rows = pg_num_rows( $res );
     if ( $rows == 0 ) {
@@ -49,9 +55,15 @@ if ( !$failure ) {
 if ( !$failure ) {
     // Grab the Homes data
     $sql = "
-    SELECT *
-      FROM homes
-     ORDER BY id";
+    SELECT h.*,
+           m.name as model_name,
+           c.name as class_name,
+           man.name as manufacturer_name
+      FROM homes h, model m, manufacturer man, class c
+     WHERE h.model = m.id
+       AND h.class = c.id
+       AND h.manufacturer = man.id
+     ORDER BY h.id";
     $res = pg_query( $db, $sql );
     $rows = pg_num_rows( $res );
     if ( $rows == 0 ) {
@@ -67,7 +79,7 @@ if ( !$failure ) {
             }
             $failure = true;
         }
-        //echo '<pre>$homes: ' . print_r( $homes, true ) . '</pre>';
+        echo '<pre>$homes: ' . print_r( $homes, true ) . '</pre>';
     }
 }
 // Reset member data tables if needed
@@ -201,7 +213,7 @@ if ( !$failure ) {
         }
         //echo '<pre>$brands: ' . print_r( $brands, true ) . '</pre>';
         foreach ( $brands as $key => $val ) {
-            echo '<pre>Adding category for ' . $val['name'] . ' parent ' . $brandId . '</pre>';
+            //echo '<pre>Adding category for ' . $val['name'] . ' parent ' . $brandId . '</pre>';
             $newCatId = addNewCategory( $this->wpdb, $val['name'], $brandId );
         }
     }
@@ -234,7 +246,7 @@ if ( !$failure ) {
         }
         //echo '<pre>$houseMan: ' . print_r( $houseMan, true ) . '</pre>';
         foreach ( $houseMan as $key => $val ) {
-            echo '<pre>Adding category for ' . $val['name'] . ' parent ' . $manufacturerId . '</pre>';
+            //echo '<pre>Adding category for ' . $val['name'] . ' parent ' . $manufacturerId . '</pre>';
             $newCatId = addNewCategory( $this->wpdb, $val['name'], $manufacturerId );
         }
     }
@@ -267,7 +279,7 @@ if ( !$failure ) {
         }
         //echo '<pre>$rvMan: ' . print_r( $rvMan, true ) . '</pre>';
         foreach ( $rvMan as $key => $val ) {
-            echo '<pre>Adding category for ' . $val['name'] . ' parent ' . $manufacturerId . '</pre>';
+            //echo '<pre>Adding category for ' . $val['name'] . ' parent ' . $manufacturerId . '</pre>';
             $newCatId = addNewCategory( $this->wpdb, $val['name'], $manufacturerId );
         }
     }
@@ -280,7 +292,7 @@ if ( !$failure ) {
     // Get all Manufactures from housing section and add them as sub categories
     $sql = "
     SELECT id,name
-      FROM classes
+      FROM class
      WHERE section = 1
      ORDER BY id";
     $res = pg_query( $db, $sql );
@@ -300,7 +312,7 @@ if ( !$failure ) {
         }
         //echo '<pre>$houseClass: ' . print_r( $houseClass, true ) . '</pre>';
         foreach ( $houseClass as $key => $val ) {
-            echo '<pre>Adding category for ' . $val['name'] . ' parent ' . $classId . '</pre>';
+            //echo '<pre>Adding category for ' . $val['name'] . ' parent ' . $classId . '</pre>';
             $newCatId = addNewCategory( $this->wpdb, $val['name'], $classId );
         }
     }
@@ -333,10 +345,246 @@ if ( !$failure ) {
         }
         //echo '<pre>$rvClass: ' . print_r( $rvClass, true ) . '</pre>';
         foreach ( $rvClass as $key => $val ) {
-            echo '<pre>Adding category for ' . $val['name'] . ' parent ' . $classId . '</pre>';
+            //echo '<pre>Adding category for ' . $val['name'] . ' parent ' . $classId . '</pre>';
             $newCatId = addNewCategory( $this->wpdb, $val['name'], $classId );
         }
     }
 }
+// Grab the Homes Models
+if ( !$failure ) {
+    // Add new main level for Models
+    $modelId = addNewCategory( $this->wpdb, 'Home Models', 0 );
+    echo '<pre>$modelId: ' . print_r( $modelId, true ) . '</pre>';
+    // Get all Manufactures from rvs section and add them as sub categories
+    $sql = "
+    SELECT id,name
+      FROM model
+     WHERE section = 1
+     ORDER BY name";
+    $res = pg_query( $db, $sql );
+    $rows = pg_num_rows( $res );
+    if ( $rows == 0 ) {
+        $templateData['genError']  = 'There does not appear to be any RV Models listed in this database!';
+        $failure = true;
+    } else {
+        $homeModel = pg_fetch_all( $res );
+        if ( count( $homeModel ) != $rows ) {
+            $notice = pg_last_notice( $res );
+            $templateData['genError']  = 'While reading base RV Models data, we did not receive the expected number of row! ';
+            if ( $notice ) {
+                $templateData['genError'] .= 'Perhaps the following message will help.<br>'.$notice;
+            }
+            $failure = true;
+        }
+        //echo '<pre>$homeModel: ' . print_r( $homeModel, true ) . '</pre>';
+        foreach ( $homeModel as $key => $val ) {
+            //echo '<pre>Adding category for ' . $val['name'] . ' parent ' . $modelId . '</pre>';
+            $newCatId = addNewCategory( $this->wpdb, $val['name'], $modelId );
+        }
+    }
+}
 // 6. Get category member relation data
+// Read in all amenities
+if (!$failure) {
+    $sql = "
+    SELECT *
+      FROM amenities
+     ORDER BY name
+        ;";
+    $res = pg_query($db, $sql);
+    $rows = pg_num_rows($res);
+    if ($rows == 0) {
+        $templateData['genError']  = 'There does not appear to be any amenities listed in this database!';
+        $failure = true;
+    } else {
+        $tmp = pg_fetch_all($res);
+        if (count($tmp) != $rows) {
+            $notice = pg_last_notice($res);
+            $templateData['genError']  = 'While reading amenity data, we did not receive the expected number of amenities! ';
+            if ($notice) {
+                $templateData['genError'] .= 'Perhaps the following message will help.<br>'.$notice;
+            }
+            $failure = true;
+        } else {
+
+            // Since there was no problem, we'll post-process the cities into an array indexed by city_id
+            $amenity = array();
 
+            // Reprocess into array indexed by amenity_id
+            foreach ($tmp as $x) {
+                $amenity[$x['id']] = $x;
+            }
+            //echo '<pre>$amenity: ' . print_r( $amenity, true ) . '</pre>';
+        }
+    }
+}
+function getAmenityId( $wpdb, $name )
+{
+    return $wpdb->get_var(
+        $wpdb->prepare(
+            "SELECT id
+              FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "amenities
+             WHERE name = %s",
+            $name
+        )
+    );
+}
+function addNewAmenity( $wpdb, $name )
+{
+    $amenityId = getAmenityId( $wpdb, $name );
+    if ( $amenityId ) {
+        return $amenityId;
+    } else {
+        // Add the category
+        $wpdb->insert(
+            GLM_MEMBERS_PLUGIN_DB_PREFIX . 'amenities',
+            array( 'name' => $name ),
+            '%s'
+        );
+        return $wpdb->insert_id;
+    }
+}
+
+if (!$failure) {
+    // Import Amenities
+    while (list ($key, $val) = each ($amenity) ) {
+        $amenityId = addNewAmenity( $this->wpdb, $val['name'] );
+        $amenity[$key]['new_id'] = $this->wpdb->insert_id;
+    }
+    //echo '<pre>$amenity: ' . print_r( $amenity, true ) . '</pre>';
+}
+function getMemberTypeId( $wpdb, $name )
+{
+    return $wpdb->get_var(
+        $wpdb->prepare(
+            "SELECT id
+              FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "member_type
+             WHERE name = %s",
+            $name
+        )
+    );
+}
+function addNewMemberType( $wpdb, $name )
+{
+    $memberTypeId = getMemberTypeId( $wpdb, $name );
+    if ( $memberTypeId ) {
+        return $memberTypeId;
+    } else {
+        // Add the category
+        $wpdb->insert(
+            GLM_MEMBERS_PLUGIN_DB_PREFIX . 'member_type',
+            array(
+                'name' => $name,
+                'descr' => ''
+            ),
+            '%s'
+        );
+        return $wpdb->insert_id;
+    }
+}
+// Member Types
+if (!$failure) {
+    // Add member type for RV's
+    $rvMemberType = addNewMemberType( $this->wpdb, 'RV' );
+    // Add member type for Homes
+    $homeMemberType = addNewMemberType( $this->wpdb, 'Home' );
+}
+// Read in all member photos for Homes and RV's
+$image = array();
+$numbImagesFound = 0;
+if (!$failure) {
+    $sql = "
+    SELECT *
+      FROM homes_photos
+     ORDER BY homes_id,pos";
+    $res = pg_query($db, $sql);
+    $rows = pg_num_rows($res);
+    if ($rows == 0) {
+        $templateData['genError']  = 'There does not appear to be any member photos listed in this database!';
+        $failure = true;
+    } else {
+        $tmp = pg_fetch_all($res);
+        if (count($tmp) != $rows) {
+            $notice = pg_last_notice($res);
+            $templateData['genError']  = 'While reading member photo data, we did not receive the expected number of member photos! ';
+            if ($notice) {
+                $templateData['genError'] .= 'Perhaps the following message will help.<br>'.$notice;
+            }
+            $failure = true;
+        } else {
+
+            // Reprocess into array grouped by member with the main index the member ID
+            foreach ($tmp as $x) {
+
+                // If member entry hasn't been created yet, add it now
+                if (!isset($image[$x['homes_id']])) {
+                    $image[$x['homes_id']] = array(
+                        'homes_id' => $x['homes_id'],
+                        'images' => array()
+                    );
+                }
+
+                $image[$x['homes_id']]['images'][] = $x;
+                $numbImagesFound++;
+            }
+            //echo '<pre>$image: ' . print_r( $image, true ) . '</pre>';
+        }
+    }
+}
+if (!$failure) {
+    // Import Members
+    $numbMembersActive       = 0;
+    $numbMembersInactive     = 0;
+    $namesInserted           = array();
+    $membImportIssues        = '';
+    $haveMembImportIssues    = false;
+    $dupeNames               = 0;
+    while (list ($key, $val) = each ($homes) ) {
+        // Member is active, so set to active-moderated
+        $access = $this->config['access_numb']['Moderated'];
+        $numbMembersActive++;
+
+        // Check for duplicate name
+        // Build out the Home name from year make model class
+        $membName = sprintf(
+            "%s %s %s %s",
+            $val['year'],
+            $val['make'],
+            $val['model_name'],
+            $val['class_name']
+        );
+        if (isset($namesInserted[$membName])) {
+
+            // Bump dupe count and add to name to make this one unique
+            $dupeNames++;
+            $membName .= ' DUPE-'.$dupeNames;
+
+            $membImportIssues .= '<li>Member Duplicate <b>'.$membName.'</b></li>';
+            $haveMembImportIssues = true;
+
+        }
+        // Add main member record
+        $res = $this->wpdb->insert(
+                GLM_MEMBERS_PLUGIN_DB_PREFIX.'members',
+                array(
+                    'access'        => $access,
+                    'member_type'   => $homeMemberType,
+                    'created'       => date('Y-m-d'),
+                    'name'          => $membName,
+                    'member_slug'   => sanitize_title($membName),
+                    'old_member_id' => $val['id']
+                ),
+                array(
+                    '%d',
+                    '%d',
+                    '%s',
+                    '%s',
+                    '%s',
+                    '%d'
+                )
+        );
+        $membID = $this->wpdb->insert_id;
+        $member[$key]['new_id'] = $membID;
+    }
+    echo '<pre>$member: ' . print_r( $member, true ) . '</pre>';
+}