From 9cf85194fac7118ca095fc0c774481ad6d337319 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Mon, 10 Apr 2017 16:56:41 -0400 Subject: [PATCH] Update brewbakers import script Update to fix the brands. --- models/admin/management/import.php | 3 + .../management/import/brewbakersBrands.php | 121 ++++++++++++++++++ .../management/import/brewbakersImport.php | 4 +- views/admin/management/import.html | 1 + 4 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 models/admin/management/import/brewbakersBrands.php diff --git a/models/admin/management/import.php b/models/admin/management/import.php index e066a209..1a0682c4 100644 --- a/models/admin/management/import.php +++ b/models/admin/management/import.php @@ -165,6 +165,9 @@ class GlmMembersAdmin_management_import case 'importBrewbakers': require GLM_MEMBERS_PLUGIN_PATH.'/models/admin/management/import/brewbakersImport.php'; break; + case 'brewbakersBrands': + require GLM_MEMBERS_PLUGIN_PATH.'/models/admin/management/import/brewbakersBrands.php'; + break; default: diff --git a/models/admin/management/import/brewbakersBrands.php b/models/admin/management/import/brewbakersBrands.php new file mode 100644 index 00000000..9b764cf6 --- /dev/null +++ b/models/admin/management/import/brewbakersBrands.php @@ -0,0 +1,121 @@ +config['ref_type_numb']['MemberInfo']; +$refTable = $this->config['ref_type_table'][$refType]; +function getCategoryId( $wpdb, $name, $parent ) +{ + // check to see if the category already exists + return $wpdb->get_var( + $wpdb->prepare( + "SELECT id + FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "categories + WHERE name = %s + AND parent = %d", + $name, + $parent + ) + ); +} +$rvBrandId = getCategoryId( $this->wpdb, 'Brands', 0 ); +// Read in all rvs +$file = array(); +$numbFilesFound = 0; +if (!$failure && $start === 0) { + $sql = " + 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) { + $templateData['genError'] = 'There does not appear to be any member files listed in this database!'; + $failure = true; + } else { + $rvs = pg_fetch_all($res); + if (count($rvs) != $rows) { + $notice = pg_last_notice($res); + $templateData['genError'] = 'While reading member photo data, we did not receive the expected number of member files! '; + if ($notice) { + $templateData['genError'] .= 'Perhaps the following message will help.
'.$notice; + } + $failure = true; + } else { + foreach ( $rvs as $rv ) { + $rvCat = array(); + // Set up the brands + // Get the new member info id + $infoID = getMemberInfoIdFromOldMemberId( $this->wpdb, $rv['id'] ); + // get the new brand id + $rvBrand = getCategoryId( $this->wpdb, $rv['brand_name'], $rvBrandId ); + if ( $infoID && $rvBrandId ) { + $rvCat[] = array( + 'category' => $rvBrand, + 'member_info' => $infoID + ); + } + // Add Member Categories + if (isset($rvCat) && count($rvCat) > 0) { + foreach ($rvCat as $catMemData) { + $res = $this->wpdb->insert( + GLM_MEMBERS_PLUGIN_DB_PREFIX.'category_member_info', + $catMemData, + array( + '%d', + '%d' + ) + ); + + } + } + } + } + } +} +echo '
$rvs: ' . print_r( $rvs, true ) . '
'; +exit; + +function getMemberInfoIdFromOldMemberId( $wpdb, $oldMemberId ) +{ + return $wpdb->get_var( + $wpdb->prepare( + "SELECT id + FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "member_info + WHERE member IN ( + SELECT id + FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members + WHERE old_member_id = %d)", + $oldMemberId + ) + ); +} + + +$templateData['newStart'] = $start + $limit; +$templateData['start'] = $start; +$templateData['total'] = $totalFiles; +$templateData['numbFilesFound'] = $numbFilesFound; + +$requestedView = 'import/brewbakersFiles.html'; diff --git a/models/admin/management/import/brewbakersImport.php b/models/admin/management/import/brewbakersImport.php index 59849a03..f8945e98 100644 --- a/models/admin/management/import/brewbakersImport.php +++ b/models/admin/management/import/brewbakersImport.php @@ -873,8 +873,8 @@ if (!$failure) { $rvCat = array(); // Add Member Categories - // Get category id for the model_name - $rvBrand = getCategoryId( $this->wpdb, $val['model_name'], $rvBrandId ); + // Get category id for the brand_name + $rvBrand = getCategoryId( $this->wpdb, $val['brand_name'], $rvBrandId ); if ( $rvBrand ) { $rvCat[] = array( 'category' => $rvBrand, diff --git a/views/admin/management/import.html b/views/admin/management/import.html index d47c4bfe..d139ff5c 100644 --- a/views/admin/management/import.html +++ b/views/admin/management/import.html @@ -27,6 +27,7 @@

Import Images

Import Files

+

Fix Brands

-- 2.17.1