Update brewbakers import script
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 10 Apr 2017 20:56:41 +0000 (16:56 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 10 Apr 2017 20:57:48 +0000 (16:57 -0400)
Update to fix the brands.

models/admin/management/import.php
models/admin/management/import/brewbakersBrands.php [new file with mode: 0644]
models/admin/management/import/brewbakersImport.php
views/admin/management/import.html

index e066a20..1a0682c 100644 (file)
@@ -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 (file)
index 0000000..9b764cf
--- /dev/null
@@ -0,0 +1,121 @@
+<?php
+/**
+ * Import Brewbakers Files
+ * Will have to do these 10 to 20 at a time.
+ */
+
+$failure = false;
+// Setup the database connection to postgres
+$dbServer    = 'ds4';
+$dbName      = 'brewbakers';
+$dbUser      = 'postgres';
+$resetdb     = true;
+$start       = ( isset( $_REQUEST['start'] ) ) ? filter_var( $_REQUEST['start'], FILTER_VALIDATE_INT ): 0;
+$limit       = ( $start === 0 ) ? 20 : 100;
+$totalFiles = 0;
+
+$connString = "host=$dbServer dbname=$dbName user=$dbUser";
+$db = @pg_connect( $connString );
+
+$refType  = $this->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.<br>'.$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 '<pre>$rvs: ' . print_r( $rvs, true ) . '</pre>';
+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';
index 59849a0..f8945e9 100644 (file)
@@ -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,
index d47c4bf..d139ff5 100644 (file)
@@ -27,6 +27,7 @@
                 <td colspan="2">
                     <p><a href="{$thisUrl}?page={$thisPage}&glm_action=import&option=brewbakersImages">Import Images</a></p>
                     <p><a href="{$thisUrl}?page={$thisPage}&glm_action=import&option=brewbakersFiles">Import Files</a></p>
+                    <p><a href="{$thisUrl}?page={$thisPage}&glm_action=import&option=brewbakersBrands">Fix Brands</a></p>
                 </td>
             </tr>
             <tr class="for-member-import all-import-fields">