Final testing of Connections import
authorChuck Scott <cscott@gaslightmedia.com>
Mon, 11 May 2015 18:39:35 +0000 (14:39 -0400)
committerChuck Scott <cscott@gaslightmedia.com>
Mon, 11 May 2015 18:39:35 +0000 (14:39 -0400)
classes/data/dataCategoryMemberInfo.php
classes/glmMemberImportFromConnections.php
models/admin/configure/development.php
models/admin/member/memberInfo.php

index 821a576..b2d2547 100644 (file)
@@ -269,19 +269,21 @@ class GlmDataCategoryMemberInfo extends GlmDataAbstract
         }
 
         // For any existing categories listed that aren't marked as selected, remove them
-        foreach ($current as $key => $val) {
+        if (is_array($current) && count($current) > 0) {
+            foreach ($current as $key => $val) {
 
-            if (!$val['selected']) {
+                if (!$val['selected']) {
 
-                // Delete the entry
-                // Add the category
-                $sql = "
-                    DELETE FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."category_member_info
-                     WHERE member_info = $memberInfoID
-                       AND id = ".$val['id']."
-                ;";
-                $this->wpdb->query($sql);
+                    // Delete the entry
+                    // Add the category
+                    $sql = "
+                        DELETE FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."category_member_info
+                         WHERE member_info = $memberInfoID
+                           AND id = ".$val['id']."
+                    ;";
+                    $this->wpdb->query($sql);
 
+                }
             }
         }
 
index 411f025..047b664 100644 (file)
@@ -179,6 +179,7 @@ class GlmMemberImportFromConnections
         $queryError = $this->wpdb->last_error;
         if ($queryError) {
             glmMembersAdmin::addNotice('<b>Error when adding the default member type: Check following message.</b><br><pre>'.$queryError.'</pre>', 'AdminError');
+            return false;
         }
 
         /*
@@ -204,6 +205,115 @@ class GlmMemberImportFromConnections
         $queryError = $this->wpdb->last_error;
         if ($queryError) {
             glmMembersAdmin::addNotice('<b>Error when adding the default region: Check following message.</b><br><pre>'.$queryError.'</pre>', 'AdminError');
+            return false;
+        }
+
+        /*
+         * Get category names from Connections
+         */
+
+        // Get the category items from the term taxonomy table in Connections
+        $sql = "
+            SELECT *
+              FROM ".$this->wpdb->prefix."connections_term_taxonomy;
+        ;";
+        $rawCats = $this->wpdb->get_results($sql, ARRAY_A);
+        $queryError = $this->wpdb->last_error;
+        if ($queryError) {
+            glmMembersAdmin::addNotice('<b>Error when reading cateory data from Connections.</b><br><pre>'.$queryError.'</pre>', 'AdminError');
+            return false;
+        }
+
+        // Start building a clean category table
+        $cats = array();
+        foreach ($rawCats as $c) {
+            if ($c['taxonomy'] == 'category') {
+                $cats[$c['term_id']] = array(
+                    'id' => $c['term_taxonomy_id'],
+                    'term_id' => $c['term_id'],
+                    'descr' => $c['description'],
+                    'parent' => $c['parent']
+                );
+            }
+        }
+
+        // get the category terms (names) from Connections
+        // Get the category items from the term taxonomy table in Connections
+        $sql = "
+            SELECT *
+              FROM ".$this->wpdb->prefix."connections_terms;
+        ;";
+        $rawTerms = $this->wpdb->get_results($sql, ARRAY_A);
+        $queryError = $this->wpdb->last_error;
+        if ($queryError) {
+            glmMembersAdmin::addNotice('<b>Error when reading cateory terms from Connections.</b><br><pre>'.$queryError.'</pre>', 'AdminError');
+            return false;
+        }
+
+        // Add terms to category table
+        foreach ($rawTerms as $t) {
+            $cats[$t['term_id']]['name'] = $t['name'];
+        }
+
+        // Store all categories and collect the new IDs
+        reset($cats);
+        while (list($k, $c) = each($cats)) {
+            $sql = "
+                INSERT INTO ".GLM_MEMBERS_PLUGIN_DB_PREFIX."categories
+                    (
+                    name,
+                    descr,
+                    short_descr,
+                    parent
+                    )
+                VALUES
+                    (
+                    '".$c['name']."',
+                    '".$c['descr']."',
+                    '',
+                    0
+                    )
+            ;";
+            $this->wpdb->query($sql);
+            $catID = $this->wpdb->insert_id;
+            $queryError = $this->wpdb->last_error;
+            if ($queryError) {
+                glmMembersAdmin::addNotice('<b>Error when creating member: Check following message.</b><br><pre>'.$queryError.'</pre>', 'AdminError');
+            }
+            $cats[$k]['new_id'] = $catID;
+        }
+
+        // Re-scan categories and populate parents with updated id
+        reset($cats);
+        foreach ($cats as $c) {
+
+            // If the category has a parent ID
+            if ($c['parent'] > 0) {
+
+                $newParent = $cats[$c['parent']]['new_id'];
+                $sql = "
+                    UPDATE ".GLM_MEMBERS_PLUGIN_DB_PREFIX."categories
+                       SET parent = $newParent
+                     WHERE id = ".$c['new_id']."
+                ;";
+                $this->wpdb->query($sql);
+                $queryError = $this->wpdb->last_error;
+                if ($queryError) {
+                    glmMembersAdmin::addNotice('<b>Error when creating member: Check following message.</b><br><pre>'.$queryError.'</pre>', 'AdminError');
+                }
+            }
+        }
+
+        // Get member/category mapping
+        $sql = "
+            SELECT *
+              FROM ".$this->wpdb->prefix."connections_term_relationships
+        ;";
+        $memberCats = $this->wpdb->get_results($sql, ARRAY_A);
+        $queryError = $this->wpdb->last_error;
+        if ($queryError) {
+            glmMembersAdmin::addNotice('<b>Error when reading term relationships from Connections.</b><br><pre>'.$queryError.'</pre>', 'AdminError');
+            return false;
         }
 
         /*
@@ -216,9 +326,6 @@ class GlmMemberImportFromConnections
             // If we're not skipping this entry
             if (!isset($_REQUEST['skip'][$m['id']])) {
 
-                echo "123  ".$m['id'];
-
-
                 /*
                  * Create base Member Record
                  */
@@ -378,6 +485,34 @@ class GlmMemberImportFromConnections
                     glmMembersAdmin::addNotice('<b>Error when creating member: Check following message.</b><br><pre>'.$queryError.'</pre>', 'AdminError');
                 }
 
+                // Scan member/category mapping array for any for this member
+                reset ($memberCats);
+                foreach ($memberCats as $mc) {
+
+                    // If there's a mapping for this member
+                    if ($mc['entry_id'] == $m['id']) {
+
+                        // Add to category_member_info table
+                        $sql = "
+                            INSERT INTO ".GLM_MEMBERS_PLUGIN_DB_PREFIX."category_member_info
+                                   (
+                                   category,
+                                   member_info
+                                   )
+                            VALUES
+                                   (
+                                   ".$cats[$mc['term_taxonomy_id']]['new_id'].",
+                                   $membID
+                                   )
+                        ;";
+                        $this->wpdb->query($sql);
+                        $queryError = $this->wpdb->last_error;
+                        if ($queryError) {
+                            glmMembersAdmin::addNotice('<b>Error when creating member/category entry: Check following message.</b><br><pre>'.$queryError.'</pre>', 'AdminError');
+                        }
+                    }
+                }
+
                 // Check for additional image
 /*
                 $image = '';
index edf68dc..1dd42a3 100644 (file)
@@ -136,6 +136,9 @@ class GlmMembersAdmin_configure_development
                     }
                     glmMembersAdmin::addNotice('<b>Database tables have been reset in preparation importing members.</b><br>', 'AdminNotice');
 
+                    // Delete Images
+//                    unlink(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/*');
+
                     $templateData['import'] = true;
                     $templateData['membersImported'] = $Connections->doImport();
 
index 3361d89..48c7dd9 100644 (file)
@@ -424,7 +424,7 @@ class GlmMembersAdmin_member_memberInfo extends GlmDataMemberInfo
 
                         $newCategory = true;
 
-                        // Check if a parent is specied
+                        // Check if a parent is specified
                         $parent = 0;
                         if ($_REQUEST['newCatParent'][$key] != '') {
                             $parent = $_REQUEST['newCatParent'][$key] -0;
@@ -449,6 +449,7 @@ class GlmMembersAdmin_member_memberInfo extends GlmDataMemberInfo
                         $selectedCategories[$key] = $key;
 
                     }
+
                     // A zero index should never happen, but we ignore them anyway
 
                     // If there's selected categories
@@ -469,8 +470,8 @@ class GlmMembersAdmin_member_memberInfo extends GlmDataMemberInfo
 
                 } // For each category being submitted
 
-            // Otherwise there's no categories submitted, so make sure there's none stored
-            } else {
+            // Otherwise if this is a submission and there's no categories submitted, so make sure there's none stored
+            } elseif (isset($_REQUEST['glm_action'])) {
                 $CategoryMemberInfo->clearMemberInfoCategories($memberInfoID);
             }