From e44dfb414897953c292f3def8d421f3e62b90f92 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Wed, 11 May 2016 14:24:13 -0400 Subject: [PATCH] Updates on the saving of the social tab Still having an issue with the other tabs and social saving the member id. The urls are now inserting when you add an entry Updating when changing them Deleting them when you empty the field. --- models/admin/member/social.php | 166 +++++++++++++++++++++------------ views/admin/member/social.html | 8 +- 2 files changed, 108 insertions(+), 66 deletions(-) diff --git a/models/admin/member/social.php b/models/admin/member/social.php index bed3875..71d0475 100644 --- a/models/admin/member/social.php +++ b/models/admin/member/social.php @@ -57,26 +57,42 @@ class GlmMembersAdmin_member_social //extends GlmDataUrls public function modelAction($actionData = false) { - $displayData = 'Hello, World! This is the Social Add-On "social" model talking to you from inside WordPress.'; - $success = true; - $urlUpdated = false; - $haveMember = false; - $socialError = false; - $option = false; - $debug = true; + // Get the Member ID + $memberID = '0'; + if (isset($_REQUEST['member']) && ($_REQUEST['member']-0) > 0) { + $memberID = $_REQUEST['member'] - 0; + } + if ($memberID == 0) { + // There should have been a member ID - So failure + return array( + 'status' => false, + 'menuItemRedirect' => 'error', + 'modelRedirect' => 'index', + 'view' => 'admin/error/index.html', + 'data' => false + ); + } + $displayData = 'Hello, World! This is the Social Add-On "social" model talking to you from inside WordPress.'; + $success = true; + $urlUpdated = false; + $haveMember = true; + $socialError = false; + $option = false; + $debug = false; $statusMessage = "Update complete"; require_once(GLM_MEMBERS_SOCIAL_PLUGIN_CLASS_PATH.'/data/dataUrls.php'); - $Urls = new GlmDataUrls($this->wpdb, $this->config); + $Urls = new GlmDataUrls($this->wpdb, $this->config); $urlStats = $Urls->getStats(); $haveUrls = ($urlStats > 0); if ($urlStats && $urlStats > 0) { $haveUrls = true; } - $urlList = $Urls->getList(); + $urlList = $Urls->getList("member_id = " . $memberID); + //$urlList = $Urls->getList(); require_once(GLM_MEMBERS_SOCIAL_PLUGIN_CLASS_PATH.'/data/dataSocials.php'); - $Socials = new GlmDataSocials($this->wpdb, $this->config); + $Socials = new GlmDataSocials($this->wpdb, $this->config); $socialList = $Socials->getList(); // Get the option or set to default @@ -86,40 +102,70 @@ class GlmMembersAdmin_member_social //extends GlmDataUrls $option = "default"; } - // Get the Member ID - $memberID = '0'; - if (isset($_REQUEST['member']) && ($_REQUEST['member']-0) > 0) { - $memberID = $_REQUEST['member'] - 0; - } - if ($memberID == 0) { - // There should have been a member ID - So failure - return array( - 'status' => false, - 'menuItemRedirect' => 'error', - 'modelRedirect' => 'index', - 'view' => 'admin/error/index.html', - 'data' => false - ); - } - - if (!$Urls->getEntry($memberID)){ - $sql = " - INSERT INTO ".$Urls->table." - (id) - VALUES ($memberID) - ;"; - $this->wpdb->query($sql); - if ($debug) { - echo "
Member not found. Created new record in Urls table using id=$memberID with statement: $sql
"; - } - } switch ($option) { case "submit": - $Urls->updateEntry($memberID); - $urlUpdated = true; + // loop thru the socialList to see if there's a new value for that media + // if there no value then see if there's a record and delete it + // if there's no record then add it + // if there's a record then update it + if ($socialList) { + // get list of current member social urls + $memberSocialUrlList = array(); + $convertedNameList = array(); + if (!empty($urlList)) { + foreach ($urlList as $url) { + $mediumName = strtolower($url['medium']); + $convertedNameList[$mediumName] = $url['medium']; + if (!isset($_REQUEST[$mediumName]) || $_REQUEST[$mediumName] == '') { + // delete record here + $this->wpdb->delete( + GLM_MEMBERS_SOCIAL_PLUGIN_DB_PREFIX . 'urls', + array( 'id' => $url['id']), + array( '%d' ) + ); + } else { + $memberSocialUrlList[] = $mediumName; + } + } + } + foreach ($socialList as $socialData) { + $socialName = strtolower($socialData['name']); + if (isset($_REQUEST[$socialName]) && $_REQUEST[$socialName]) { + // see if there's a record for it + if (in_array($socialName, $memberSocialUrlList)) { + // update record here + $urlData = array('url' => $_REQUEST[$socialName]); + $urlDataFormat = array('%s'); + $this->wpdb->update( + GLM_MEMBERS_SOCIAL_PLUGIN_DB_PREFIX . 'urls', + $urlData, + array( 'member_id' => $memberID, 'medium' => $socialData['name']), + $urlDataFormat, + array('%d', '%s' ) + ); + } else { + // add record here + $urlData = array( + 'url' => $_REQUEST[$socialName], + 'member_id' => $memberID, + 'medium' => $socialData['name'] + ); + $urlDataFormat = array( '%s', '%d', '%s' ); + $newId = $this->wpdb->insert( + GLM_MEMBERS_SOCIAL_PLUGIN_DB_PREFIX . 'urls', + $urlData, + $urlDataFormat + ); + } + } + } + } + + //$Urls->updateEntry($memberID); + //$urlUpdated = true; $statusMessage = "Social Media URLs Updated"; - + $urlList = $Urls->getList("member_id = " . $memberID); break; case "social"; default: @@ -129,21 +175,19 @@ class GlmMembersAdmin_member_social //extends GlmDataUrls // 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 ); } // Refresh the Url list before returning it $memberUrlList = array(); - $urlList = $Urls->getList("member_id = " . $memberID); if (!empty($urlList)) { foreach ($urlList as $url) { - $memberUrlList[strtolower($url['medium'])] = $url['url']; } } @@ -156,30 +200,30 @@ class GlmMembersAdmin_member_social //extends GlmDataUrls echo ""; echo "
UrlList:"; echo "
".print_r($urlList[$memberID], true)."
"; - //echo "
Recordlist:
"; - //echo "
".print_r($socialList, true)."
"; + echo "
sociallist:
"; + echo "
".print_r($socialList, true)."
"; echo "
"; } $templateData = array( - 'displayData' => $displayData, - 'socials' => $socialList, - 'urlList' => $memberUrlList, - 'assetsUrl' => GLM_MEMBERS_SOCIAL_PLUGIN_ASSETS_URL, - 'urlUpdated' => $urlUpdated, - 'socialError' => $socialError, - 'haveMember' => $haveMember, - 'memberID' => $memberID, - 'option' => $option, + 'displayData' => $displayData, + 'socials' => $socialList, + 'urlList' => $memberUrlList, + 'assetsUrl' => GLM_MEMBERS_SOCIAL_PLUGIN_ASSETS_URL, + 'urlUpdated' => $urlUpdated, + 'socialError' => $socialError, + 'haveMember' => $haveMember, + 'memberID' => $memberID, + 'option' => $option, 'statusMessage' => $statusMessage ); // Return status, any suggested view, and any data to controller return array( - 'status' => true, + 'status' => true, 'modelRedirect' => false, - 'view' => 'admin/member/social.html', - 'data' => $templateData + 'view' => 'admin/member/social.html', + 'data' => $templateData ); } } diff --git a/views/admin/member/social.html b/views/admin/member/social.html index ed983bf..5993e67 100644 --- a/views/admin/member/social.html +++ b/views/admin/member/social.html @@ -1,11 +1,10 @@ {include file='admin/members/header.html'}

{if $urlUpdated}{$statusMessage}{/if}

Member Social Tab

-

{$displayData}

- + @@ -38,10 +37,9 @@
Click me to see more! - + -
Update
{foreach from=$socials key=k item=socialItem} {if $socialItem.active} @@ -92,4 +90,4 @@ }); -{include file='admin/footer.html'} \ No newline at end of file +{include file='admin/footer.html'} -- 2.17.1