From b3e8be4d30944a374c4b9116f41ac5671fe0baaf Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Wed, 5 Apr 2017 08:16:41 -0400 Subject: [PATCH] Working on getting categories setup Building categories from Brand Manufacture and Class tables. --- .../management/import/brewbakersImport.php | 165 +++++++++++++++++- 1 file changed, 159 insertions(+), 6 deletions(-) diff --git a/models/admin/management/import/brewbakersImport.php b/models/admin/management/import/brewbakersImport.php index 55e7d164..125348ee 100644 --- a/models/admin/management/import/brewbakersImport.php +++ b/models/admin/management/import/brewbakersImport.php @@ -132,13 +132,10 @@ if ( !$failure ) { //echo '
$amenityData: ' . print_r( $amenityData, true ) . '
'; } } -// 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.
'.$notice; + } + $failure = true; + } + //echo '
$brands: ' . print_r( $brands, true ) . '
'; + foreach ( $brands as $key => $val ) { + echo '
Adding category for ' . $val['name'] . ' parent ' . $brandId . '
'; + $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 '
$manufacturerId: ' . print_r( $manufacturerId, true ) . '
'; + // 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.
'.$notice; + } + $failure = true; + } + //echo '
$houseMan: ' . print_r( $houseMan, true ) . '
'; + foreach ( $houseMan as $key => $val ) { + echo '
Adding category for ' . $val['name'] . ' parent ' . $manufacturerId . '
'; + $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 '
$manufacturerId: ' . print_r( $manufacturerId, true ) . '
'; + // 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.
'.$notice; + } + $failure = true; + } + //echo '
$rvMan: ' . print_r( $rvMan, true ) . '
'; + foreach ( $rvMan as $key => $val ) { + echo '
Adding category for ' . $val['name'] . ' parent ' . $manufacturerId . '
'; + $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 '
$classId: ' . print_r( $classId, true ) . '
'; + // 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.
'.$notice; + } + $failure = true; + } + //echo '
$houseClass: ' . print_r( $houseClass, true ) . '
'; + foreach ( $houseClass as $key => $val ) { + echo '
Adding category for ' . $val['name'] . ' parent ' . $classId . '
'; + $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 '
$classId: ' . print_r( $classId, true ) . '
'; + // 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.
'.$notice; + } + $failure = true; + } + //echo '
$rvClass: ' . print_r( $rvClass, true ) . '
'; + foreach ( $rvClass as $key => $val ) { + echo '
Adding category for ' . $val['name'] . ' parent ' . $classId . '
'; + $newCatId = addNewCategory( $this->wpdb, $val['name'], $classId ); + } } } // 6. Get category member relation data -- 2.17.1