Updating the management section for import of old member social urls
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 17 May 2016 19:43:35 +0000 (15:43 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 17 May 2016 19:43:35 +0000 (15:43 -0400)
Pull the member data from current site to bring in tho old memeber
social urls

models/admin/management/social.php
views/admin/management/social.html

index 494a23f..6f76543 100644 (file)
@@ -51,6 +51,13 @@ class GlmMembersAdmin_management_social extends GlmDataSocials
      * @access public
      */
     public $memberID = false;
+    /**
+     * dbh Postgres database connection
+     *
+     * @var mixed
+     * @access public
+     */
+    public $dbh;
     /*
      * Constructor
      *
@@ -104,10 +111,12 @@ class GlmMembersAdmin_management_social extends GlmDataSocials
             case "addNew";
                 $this->insertEntry();
 // **** NEED TO ADD TO ALL MEMBERS ALSO
+                $option = 'settings';
                 break;
 
             case "update":
                 $this->updateEntry($socialID);
+                $option = 'settings';
                 break;
 
             case "delete";
@@ -117,21 +126,34 @@ class GlmMembersAdmin_management_social extends GlmDataSocials
                     DELETE FROM ".GLM_MEMBERS_SOCIAL_PLUGIN_DB_PREFIX . "social_urls
                      WHERE social = $socialID;
                 ");
+                $option = 'settings';
+                break;
+
+            case 'socialimport':
+                $importResult = '<pre>$_REQUEST: ' . print_r( $_REQUEST, true ) . '</pre>';
+                $db_host      = filter_var( $_REQUEST['db_host'], FILTER_SANITIZE_STRING );
+                $db_name      = filter_var( $_REQUEST['db_name'], FILTER_SANITIZE_STRING );
+                $db_user      = filter_var( $_REQUEST['db_user'], FILTER_SANITIZE_STRING );
+                $db_password  = filter_var( $_REQUEST['db_password'], FILTER_SANITIZE_STRING );
+                $this->connectPostgresDb($db_host, $db_name, $db_user, $db_password);
+                $this->importSocial();
                 break;
 
             default:
+                // Make sure option is set if default
+                $option = 'settings';
                 break;
         }
 
         // If we had a fatal error, redirect to the error page
         if ($socialError) {
             return array(
-                'status' => $success,
-                'option' => $option,
+                'status'           => $success,
+                'option'           => $option,
                 'menuItemRedirect' => 'error',
-                'modelRedirect' => 'index',
-                'view' => 'admin/error/index.html',
-                'data' => false
+                'modelRedirect'    => 'index',
+                'view'             => 'admin/error/index.html',
+                'data'             => false
             );
         }
 
@@ -140,20 +162,224 @@ class GlmMembersAdmin_management_social extends GlmDataSocials
 
         // Compile template data
         $templateData = array(
-            'assetsUrl' => GLM_MEMBERS_SOCIAL_PLUGIN_ASSETS_URL,
+            'assetsUrl'     => GLM_MEMBERS_SOCIAL_PLUGIN_ASSETS_URL,
             'socialUpdated' => $socialUpdated,
-            'socialError' => $socialError,
-            'option' => $option,
-            'socials' => $socialList,
+            'socialError'   => $socialError,
+            'option'        => $option,
+            'importResult'     => $importResult,
+            'socials'       => $socialList,
             'statusMessage' => $statusMessage
         );
 
         // Return status, any suggested view, and any data to controller
         return array(
-            'status' => true,
+            'status'        => true,
             'modelRedirect' => false,
-            'view' => 'admin/management/social.html',
-            'data' => $templateData
+            'view'          => 'admin/management/social.html',
+            'data'          => $templateData
         );
     }
+    /**
+     * importSocial
+     */
+    public function importSocial()
+    {
+        $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_SOCIAL_PLUGIN_DB_PREFIX . "social_urls" );
+        require_once(GLM_MEMBERS_SOCIAL_PLUGIN_CLASS_PATH.'/data/dataSocialUrls.php');
+        $socialUrl = new GlmDataSocialUrls($this->wpdb, $this->config);
+
+        $oldMemberDataSql = "
+          SELECT m.member_id,m.facebook, m.twitter, m.linkedin, m.blog, m.youtube, m.pinterest,
+                 m.instagram, m.google_plus
+            FROM members.member m
+        ORDER BY m.member_id";
+        $oldMemberData = $this->dbh->query( $oldMemberDataSql )->fetchAll(PDO::FETCH_ASSOC);
+        //echo '<pre>$oldMemberData: ' . print_r($oldMemberData, true) . '</pre>';
+        $dataFormat = array( '%s', '%d', '%d', '%d' );
+        $dataTable  = GLM_MEMBERS_SOCIAL_PLUGIN_DB_PREFIX . 'social_urls';
+        foreach ($oldMemberData as $member) {
+            // get the correct member id from old one
+            $memberID = $this->wpdb->get_var(
+                "SELECT id 
+                   FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members
+                  WHERE old_member_id = {$member['member_id']}"
+            );
+            $socialArray = array();
+            if ($member['facebook']) {
+                // add for facebook
+                $url = $member['facebook'];
+                $newId = $this->wpdb->insert(
+                    $dataTable,
+                    array(
+                        'url'      => $member['facebook'],
+                        'ref_type' => $this->config['ref_type_numb']['MemberInfo'],
+                        'ref_dest' => $memberID,
+                        'social'   => 1
+                    ),
+                    $dataFormat
+                );
+                if (!$newId) {
+                    die('fialed to add url');
+                }
+            }
+            if ($member['twitter']) {
+                // add for twitter
+                $newId = $this->wpdb->insert(
+                    $dataTable,
+                    array(
+                        'url'      => $member['twitter'],
+                        'ref_type' => $this->config['ref_type_numb']['MemberInfo'],
+                        'ref_dest' => $memberID,
+                        'social'   => 2
+                    ),
+                    $dataFormat
+                );
+                if (!$newId) {
+                    die('fialed to add url');
+                }
+            }
+            if ($member['pinterest']) {
+                // add for pinterest
+                $newId = $this->wpdb->insert(
+                    $dataTable,
+                    array(
+                        'url'      => $member['pinterest'],
+                        'ref_type' => $this->config['ref_type_numb']['MemberInfo'],
+                        'ref_dest' => $memberID,
+                        'social'   => 3
+                    ),
+                    $dataFormat
+                );
+                if (!$newId) {
+                    die('fialed to add url');
+                }
+            }
+            if ($member['google_plus']) {
+                // add for google_plus
+                $newId = $this->wpdb->insert(
+                    $dataTable,
+                    array(
+                        'url'      => $member['google_plus'],
+                        'ref_type' => $this->config['ref_type_numb']['MemberInfo'],
+                        'ref_dest' => $memberID,
+                        'social'   => 4
+                    ),
+                    $dataFormat
+                );
+                if (!$newId) {
+                    die('fialed to add url');
+                }
+            }
+            if ($member['linkedin']) {
+                // add for linkedin
+                $newId = $this->wpdb->insert(
+                    $dataTable,
+                    array(
+                        'url'      => $member['linkedin'],
+                        'ref_type' => $this->config['ref_type_numb']['MemberInfo'],
+                        'ref_dest' => $memberID,
+                        'social'   => 6
+                    ),
+                    $dataFormat
+                );
+                if (!$newId) {
+                    die('fialed to add url');
+                }
+            }
+            if ($member['instagram']) {
+                // add for instagram
+                $newId = $this->wpdb->insert(
+                    $dataTable,
+                    array(
+                        'url'      => $member['instagram'],
+                        'ref_type' => $this->config['ref_type_numb']['MemberInfo'],
+                        'ref_dest' => $memberID,
+                        'social'   => 7
+                    ),
+                    $dataFormat
+                );
+                if (!$newId) {
+                    die('fialed to add url');
+                }
+            }
+            if ($member['youtube']) {
+                // add for youtube
+                $newId = $this->wpdb->insert(
+                    $dataTable,
+                    array(
+                        'url'      => $member['youtube'],
+                        'ref_type' => $this->config['ref_type_numb']['MemberInfo'],
+                        'ref_dest' => $memberID,
+                        'social'   => 8
+                    ),
+                    $dataFormat
+                );
+                if (!$newId) {
+                    die('fialed to add url');
+                }
+            }
+            if ($member['blog']) {
+                // add for blog
+                $newId = $this->wpdb->insert(
+                    $dataTable,
+                    array(
+                        'url'      => $member['blog'],
+                        'ref_type' => $this->config['ref_type_numb']['MemberInfo'],
+                        'ref_dest' => $memberID,
+                        'social'   => 10
+                    ),
+                    $dataFormat
+                );
+                if (!$newId) {
+                    die('fialed to add url');
+                }
+            }
+
+        }
+    }
+    /**
+     * connectPostgresDb
+     *
+     * Make a connection to the given database for the site. (postgres)
+     * Sets the $this->dbh with the postgers database connection
+     *
+     * @param mixed $db_host
+     * @param mixed $db_name
+     * @param mixed $db_user
+     * @param mixed $db_password
+     * @access public
+     * @return void
+     */
+    public function connectPostgresDb($db_host, $db_name, $db_user, $db_password)
+    {
+        $connStr      = "pgsql:";
+        if ( $db_host ) {
+            $connPart[] = "host={$db_host}";
+        }
+        if ( $db_name ) {
+            $connPart[] = "dbname={$db_name}";
+        }
+        if ( $db_user ) {
+            $connPart[] = "user={$db_user}";
+        }
+        if ( $db_password ) {
+            $connPart[] = "password={$db_password}";
+        }
+        if ( !empty($connPart) ) {
+             $connStr .= implode( " ", $connPart );
+        }
+        $driverOptions = array(
+            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_BOTH,
+        );
+        try {
+            $this->dbh = new PDO($connStr, null, null, $driverOptions);
+            $this->dbh->setAttribute(
+                PDO::ATTR_ERRMODE,
+                PDO::ERRMODE_EXCEPTION
+            );
+        } catch(PDOException $e) {
+            echo '<pre>$e: ' . print_r($e, true) . '</pre>';
+            wp_die();
+        }
+    }
 }
index bafa806..9bc2ed4 100644 (file)
@@ -1,7 +1,12 @@
 {include file='admin/management/header.html'}
 
     <h2>{if $socialUpdated}<span class="glm-notice glm-flash-updated glm-right">{$statusMessage}</span>{/if}</h2>
+    <h2 class="nav-tab-wrapper" style="margin-bottom: 1em;">
+        <a id="glm-settings" data-show-table="glm-table-settings" class="glm-settings-tab nav-tab{if $option=='settings'} nav-tab-active{/if}">Settings</a>
+        <a id="glm-socialimport" data-show-table="glm-table-socialimport" class="glm-settings-tab nav-tab{if $option=='socialimport'} nav-tab-active{/if}">Import</a>
+    </h2>
 
+<div id="glm-table-settings" class="glm-admin-table glm-settings-table{if $option!='settings'} glm-hidden{/if}">
     <!-- Add Social Media Button and Dialog Box -->
     <div id="newSocialButton" class="button button-primary glm-right">Add a Social Media Type</div>    
     <div id="newSocialDialog" class="glm-dialog-box" title="Enter New Social Media Type">
             </table>
         </form>
     </div>
+</div>
+<table id="glm-table-socialimport" class="glm-admin-table glm-settings-table{if $option!='socialimport'} glm-hidden{/if}">
+  {if $importResult}
+  <tr>
+      <td>
+        {$importResult}
+      </td>
+  </tr>
+  {else}
+    <tr>
+        <td>
+            <form action="{$thisUrl}">
+                <input type="hidden" name="page" value="{$thisPage}">
+                <input type="hidden" name="glm_action" value="social">
+                <input type="hidden" name="option" value="socialimport">
+                <input type="hidden" name="import" value="true">
+                <div style="float:left;width: 200px;">
+                    <fieldset>
+                        <legend>Database to import from:</legend>
+                        <label for="db_host">DB Host</label>
+                        <input id="db_host" name="db_host" />
+                        <label for="db_name">DB Name</label>
+                        <input id="db_name" name="db_name" />
+                        <label for="db_user">DB User</label>
+                        <input id="db_user" name="db_user" />
+                        <label for="db_password">DB Password</label>
+                        <input id="db_password" name="db_password" />
+                    <fieldset>
+                </div>
+                <div style="float:left;width: 200px;">
+                    <fieldset>
+                        <legend>Settings:</legend>
+                        <label for="schema">Schema</label>
+                        <input id="schema" name="schema" />
+                    </fieldset>
+
+                    <input type="submit" value="Import Members Social Url's" />
+                </div>
+            </form>
+        </td>
+    </tr>
+  {/if}
+</table>
     
 
 <script type="text/javascript">
     jQuery(document).ready(function($) {
-        
+        /*
+         * Edit area tabs
+         */
+        $('.glm-settings-tab').click( function() {
+
+            // Clear highlights and hide all tables
+            $('.glm-settings-tab').removeClass('nav-tab-active');
+            $('.glm-settings-table').addClass('glm-hidden');
+            
+            // Highlight selected tab
+            $(this).addClass('nav-tab-active');
+            
+            // Show selected table
+            var table = $(this).attr('data-show-table');
+            $('#' + table).removeClass('glm-hidden');
+        });
         
         $("#newSocialDialog").dialog({
             autoOpen: false,