Working on getting categories setup
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 5 Apr 2017 12:16:41 +0000 (08:16 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 5 Apr 2017 12:16:41 +0000 (08:16 -0400)
Building categories from Brand Manufacture and Class tables.

models/admin/management/import/brewbakersImport.php

index 55e7d16..125348e 100644 (file)
@@ -132,13 +132,10 @@ if ( !$failure ) {
         //echo '<pre>$amenityData: ' . print_r( $amenityData, true ) . '</pre>';
     }
 }
-// 5. Get categories
-// Setup three main level categories
-// Brands, Manufactures, and Classes
-function addNewCategory( $wpdb, $name, $parent )
+function getCategoryId( $wpdb, $name, $parent )
 {
     // check to see if the category already exists
-    $catId = $wpdb->get_var(
+    return $wpdb->get_var(
         $wpdb->prepare(
             "SELECT id
                FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "categories
@@ -148,6 +145,13 @@ function addNewCategory( $wpdb, $name, $parent )
             $parent
         )
     );
+}
+// 5. Get categories
+// Setup three main level categories
+// Brands, Manufactures, and Classes
+function addNewCategory( $wpdb, $name, $parent )
+{
+    $catId = getCategoryId( $wpdb, $name, $parent );
     if ( $catId ) {
         return $catId;
     } else {
@@ -170,6 +174,7 @@ function addNewCategory( $wpdb, $name, $parent )
         return $wpdb->insert_id;
     }
 }
+// Grab the Brands from Brewbakers
 if ( !$failure ) {
     // Add new main level for Brands
     $brandId = addNewCategory( $this->wpdb, 'Brands', 0 );
@@ -182,7 +187,155 @@ if ( !$failure ) {
     $res = pg_query( $db, $sql );
     $rows = pg_num_rows( $res );
     if ( $rows == 0 ) {
-
+        $templateData['genError']  = 'There does not appear to be any brands listed in this database!';
+        $failure = true;
+    } else {
+        $brands = pg_fetch_all( $res );
+        if ( count( $brands ) != $rows ) {
+            $notice = pg_last_notice( $res );
+            $templateData['genError']  = 'While reading base brands data, we did not receive the expected number of brands! ';
+            if ( $notice ) {
+                $templateData['genError'] .= 'Perhaps the following message will help.<br>'.$notice;
+            }
+            $failure = true;
+        }
+        //echo '<pre>$brands: ' . print_r( $brands, true ) . '</pre>';
+        foreach ( $brands as $key => $val ) {
+            echo '<pre>Adding category for ' . $val['name'] . ' parent ' . $brandId . '</pre>';
+            $newCatId = addNewCategory( $this->wpdb, $val['name'], $brandId );
+        }
+    }
+}
+// Grab the Housing Manufacturers
+if ( !$failure ) {
+    // Add new main level for Brands
+    $manufacturerId = addNewCategory( $this->wpdb, 'Housing Manufacturers', 0 );
+    echo '<pre>$manufacturerId: ' . print_r( $manufacturerId, true ) . '</pre>';
+    // Get all Manufactures from housing section and add them as sub categories
+    $sql = "
+    SELECT id,name
+      FROM manufacturer
+     WHERE section = 1
+     ORDER BY id";
+    $res = pg_query( $db, $sql );
+    $rows = pg_num_rows( $res );
+    if ( $rows == 0 ) {
+        $templateData['genError']  = 'There does not appear to be any Housing Manufacturers listed in this database!';
+        $failure = true;
+    } else {
+        $houseMan = pg_fetch_all( $res );
+        if ( count( $houseMan ) != $rows ) {
+            $notice = pg_last_notice( $res );
+            $templateData['genError']  = 'While reading base Housing Manufacturers 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>$houseMan: ' . print_r( $houseMan, true ) . '</pre>';
+        foreach ( $houseMan as $key => $val ) {
+            echo '<pre>Adding category for ' . $val['name'] . ' parent ' . $manufacturerId . '</pre>';
+            $newCatId = addNewCategory( $this->wpdb, $val['name'], $manufacturerId );
+        }
+    }
+}
+// Grab the RV Manufacturers
+if ( !$failure ) {
+    // Add new main level for Brands
+    $manufacturerId = addNewCategory( $this->wpdb, 'RV Manufacturers', 0 );
+    echo '<pre>$manufacturerId: ' . print_r( $manufacturerId, true ) . '</pre>';
+    // Get all Manufactures from rvs section and add them as sub categories
+    $sql = "
+    SELECT id,name
+      FROM manufacturer
+     WHERE section = 2
+     ORDER BY id";
+    $res = pg_query( $db, $sql );
+    $rows = pg_num_rows( $res );
+    if ( $rows == 0 ) {
+        $templateData['genError']  = 'There does not appear to be any RV Manufacturers listed in this database!';
+        $failure = true;
+    } else {
+        $rvMan = pg_fetch_all( $res );
+        if ( count( $rvMan ) != $rows ) {
+            $notice = pg_last_notice( $res );
+            $templateData['genError']  = 'While reading base RV Manufacturers 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>$rvMan: ' . print_r( $rvMan, true ) . '</pre>';
+        foreach ( $rvMan as $key => $val ) {
+            echo '<pre>Adding category for ' . $val['name'] . ' parent ' . $manufacturerId . '</pre>';
+            $newCatId = addNewCategory( $this->wpdb, $val['name'], $manufacturerId );
+        }
+    }
+}
+// Grab the Housing Classes
+if ( !$failure ) {
+    // Add new main level for Brands
+    $classId = addNewCategory( $this->wpdb, 'Housing Classes', 0 );
+    echo '<pre>$classId: ' . print_r( $classId, true ) . '</pre>';
+    // Get all Manufactures from housing section and add them as sub categories
+    $sql = "
+    SELECT id,name
+      FROM classes
+     WHERE section = 1
+     ORDER BY id";
+    $res = pg_query( $db, $sql );
+    $rows = pg_num_rows( $res );
+    if ( $rows == 0 ) {
+        $templateData['genError']  = 'There does not appear to be any Housing Classes listed in this database!';
+        $failure = true;
+    } else {
+        $houseClass = pg_fetch_all( $res );
+        if ( count( $houseClass ) != $rows ) {
+            $notice = pg_last_notice( $res );
+            $templateData['genError']  = 'While reading base Housing Classes 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>$houseClass: ' . print_r( $houseClass, true ) . '</pre>';
+        foreach ( $houseClass as $key => $val ) {
+            echo '<pre>Adding category for ' . $val['name'] . ' parent ' . $classId . '</pre>';
+            $newCatId = addNewCategory( $this->wpdb, $val['name'], $classId );
+        }
+    }
+}
+// Grab the RV Classes
+if ( !$failure ) {
+    // Add new main level for Brands
+    $classId = addNewCategory( $this->wpdb, 'RV Classes', 0 );
+    echo '<pre>$classId: ' . print_r( $classId, true ) . '</pre>';
+    // Get all Manufactures from rvs section and add them as sub categories
+    $sql = "
+    SELECT id,name
+      FROM class
+     WHERE section = 2
+     ORDER BY id";
+    $res = pg_query( $db, $sql );
+    $rows = pg_num_rows( $res );
+    if ( $rows == 0 ) {
+        $templateData['genError']  = 'There does not appear to be any RV Classes listed in this database!';
+        $failure = true;
+    } else {
+        $rvClass = pg_fetch_all( $res );
+        if ( count( $rvClass ) != $rows ) {
+            $notice = pg_last_notice( $res );
+            $templateData['genError']  = 'While reading base RV Classes 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>$rvClass: ' . print_r( $rvClass, true ) . '</pre>';
+        foreach ( $rvClass as $key => $val ) {
+            echo '<pre>Adding category for ' . $val['name'] . ' parent ' . $classId . '</pre>';
+            $newCatId = addNewCategory( $this->wpdb, $val['name'], $classId );
+        }
     }
 }
 // 6. Get category member relation data