Updates on the saving of the social tab feature/steve
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 11 May 2016 18:24:13 +0000 (14:24 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 11 May 2016 18:24:13 +0000 (14:24 -0400)
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
views/admin/member/social.html

index bed3875..71d0475 100644 (file)
@@ -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 "<hr>Member not found. Created new record in Urls table using id=$memberID with statement: $sql<hr>";
-            }
-        }
 
         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 "</table>";
             echo "<hr />UrlList:<table>";
             echo "<pre>".print_r($urlList[$memberID], true)."</pre>";
-            //echo "<hr />Recordlist:<table>";
-            //echo "<pre>".print_r($socialList, true)."</pre>";
+            echo "<hr />sociallist:<table>";
+            echo "<pre>".print_r($socialList, true)."</pre>";
             echo "</table>";            
         }
         
         $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
         );
     }
 }
index ed983bf..5993e67 100644 (file)
@@ -1,11 +1,10 @@
 {include file='admin/members/header.html'}
 <h2>{if $urlUpdated}<span class="glm-notice glm-flash-updated glm-right">{$statusMessage}</span>{/if}</h2>
     <h3>Member Social Tab</h3>
-    <p>{$displayData}</p>
     
     <div id="editMemberTypeDialog" class="glm-dialog-box" title="Edit Member Type">
         <form action="{$thisUrl}?page={$thisPage}" method="post" enctype="multipart/form-data">
-            <input type="hidden" name="glm_action" value="index">
+            <input type="hidden" name="glm_action" value="social">
             <input type="hidden" name="option" value="update">
             <input id="editMemberTypeID" type="hidden" name="id" value="">
             <table class="glm-admin-table">
     
     <div id="glm-admin-content-container">
         <a href="{$thisUrl}?page={$thisPage}&glm_action=more">Click me to see more!</a>
-        <form action="{$thisUrl}?page={$thisPage}&glm_action=default&member={$memberID}" method="post" enctype="multipart/form-data">
+        <form action="{$thisUrl}?page={$thisPage}&glm_action=social&member={$memberID}" method="post" enctype="multipart/form-data">
             <input type="submit" class="button glm-button submit" value="submit" name='submit'>
             <input type="hidden" name="option" value="submit">
-            <div class="button glm-button right">Update</div>
             <table class="glm-admin-table">
             {foreach from=$socials key=k item=socialItem}
                 {if $socialItem.active}
@@ -92,4 +90,4 @@
     });
 
 </script>
-{include file='admin/footer.html'}
\ No newline at end of file
+{include file='admin/footer.html'}