From: Steve Sutton Date: Wed, 5 Apr 2017 21:03:13 +0000 (-0400) Subject: Worked on member import X-Git-Tag: v2.9.15^2~52 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=ec81e2f6c2779a2bee6e9b87306792610510a44f;p=WP-Plugins%2Fglm-member-db.git Worked on member import Have member categories, amenities and member types setup. Working on member record. --- diff --git a/models/admin/management/import/brewbakersImport.php b/models/admin/management/import/brewbakersImport.php index 125348ee..cec72b95 100644 --- a/models/admin/management/import/brewbakersImport.php +++ b/models/admin/management/import/brewbakersImport.php @@ -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 '
$homes: ' . print_r( $homes, true ) . '
'; + echo '
$homes: ' . print_r( $homes, true ) . '
'; } } // Reset member data tables if needed @@ -201,7 +213,7 @@ if ( !$failure ) { } //echo '
$brands: ' . print_r( $brands, true ) . '
'; foreach ( $brands as $key => $val ) { - echo '
Adding category for ' . $val['name'] . ' parent ' . $brandId . '
'; + //echo '
Adding category for ' . $val['name'] . ' parent ' . $brandId . '
'; $newCatId = addNewCategory( $this->wpdb, $val['name'], $brandId ); } } @@ -234,7 +246,7 @@ if ( !$failure ) { } //echo '
$houseMan: ' . print_r( $houseMan, true ) . '
'; foreach ( $houseMan as $key => $val ) { - echo '
Adding category for ' . $val['name'] . ' parent ' . $manufacturerId . '
'; + //echo '
Adding category for ' . $val['name'] . ' parent ' . $manufacturerId . '
'; $newCatId = addNewCategory( $this->wpdb, $val['name'], $manufacturerId ); } } @@ -267,7 +279,7 @@ if ( !$failure ) { } //echo '
$rvMan: ' . print_r( $rvMan, true ) . '
'; foreach ( $rvMan as $key => $val ) { - echo '
Adding category for ' . $val['name'] . ' parent ' . $manufacturerId . '
'; + //echo '
Adding category for ' . $val['name'] . ' parent ' . $manufacturerId . '
'; $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 '
$houseClass: ' . print_r( $houseClass, true ) . '
'; foreach ( $houseClass as $key => $val ) { - echo '
Adding category for ' . $val['name'] . ' parent ' . $classId . '
'; + //echo '
Adding category for ' . $val['name'] . ' parent ' . $classId . '
'; $newCatId = addNewCategory( $this->wpdb, $val['name'], $classId ); } } @@ -333,10 +345,246 @@ if ( !$failure ) { } //echo '
$rvClass: ' . print_r( $rvClass, true ) . '
'; foreach ( $rvClass as $key => $val ) { - echo '
Adding category for ' . $val['name'] . ' parent ' . $classId . '
'; + //echo '
Adding category for ' . $val['name'] . ' parent ' . $classId . '
'; $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 '
$modelId: ' . print_r( $modelId, true ) . '
'; + // 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.
'.$notice; + } + $failure = true; + } + //echo '
$homeModel: ' . print_r( $homeModel, true ) . '
'; + foreach ( $homeModel as $key => $val ) { + //echo '
Adding category for ' . $val['name'] . ' parent ' . $modelId . '
'; + $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.
'.$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 '
$amenity: ' . print_r( $amenity, true ) . '
'; + } + } +} +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 '
$amenity: ' . print_r( $amenity, true ) . '
'; +} +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.
'.$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 '
$image: ' . print_r( $image, true ) . '
'; + } + } +} +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 .= '
  • Member Duplicate '.$membName.'
  • '; + $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 '
    $member: ' . print_r( $member, true ) . '
    '; +}