From 2f0fe8e609119130532bf2f7441dfcc582265385 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Tue, 17 May 2016 15:43:35 -0400 Subject: [PATCH] Updating the management section for import of old member social urls Pull the member data from current site to bring in tho old memeber social urls --- models/admin/management/social.php | 250 +++++++++++++++++++++++++++-- views/admin/management/social.html | 65 +++++++- 2 files changed, 302 insertions(+), 13 deletions(-) diff --git a/models/admin/management/social.php b/models/admin/management/social.php index 494a23f..6f76543 100644 --- a/models/admin/management/social.php +++ b/models/admin/management/social.php @@ -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 = '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; + $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 '
$oldMemberData: ' . print_r($oldMemberData, true) . '
'; + $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 '
$e: ' . print_r($e, true) . '
'; + wp_die(); + } + } } diff --git a/views/admin/management/social.html b/views/admin/management/social.html index bafa806..9bc2ed4 100644 --- a/views/admin/management/social.html +++ b/views/admin/management/social.html @@ -1,7 +1,12 @@ {include file='admin/management/header.html'}

{if $socialUpdated}{$statusMessage}{/if}

+ +
Add a Social Media Type
@@ -117,11 +122,69 @@
+
+ + {if $importResult} + + + + {else} + + + + {/if} +
+ {$importResult} +
+
+ + + + +
+
+ Database to import from: + + + + + + + + +
+
+
+
+ Settings: + + +
+ + +
+
+