Rework for ajax calls
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 23 May 2018 20:49:04 +0000 (16:49 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 23 May 2018 20:49:04 +0000 (16:49 -0400)
This is so the add notes to work if they refresh and it won't add dupe
note.

models/admin/ajax/travelNotes.php
models/admin/travel/index.php
views/admin/ajax/notes/list.html

index db556ec..24ac5c0 100644 (file)
@@ -82,6 +82,50 @@ class GlmMembersAdmin_ajax_travelNotes extends GlmDataTravelNotes
         trigger_error( print_r( $_REQUEST, E_USER_NOTICE ) );
 
         switch ( $option ) {
+        case 'add':
+            if ( isset( $_REQUEST['id'] )
+                && isset( $_REQUEST['new_log'] )
+                && $lead_id = filter_var( $_REQUEST['id'], FILTER_VALIDATE_INT )
+                && $new_note = filter_var( $_REQUEST['new_log'], FILTER_SANITIZE_STRING )
+            ) {
+
+                $this->wpdb->insert(
+                    GLM_MEMBERS_TRAVEL_PLUGIN_DB_PREFIX . 'lead_notes',
+                    array(
+                        'lead'    => $lead_id,
+                        'created' => date( 'Y-m-d H:i:s' ),
+                        'updated' => date( 'Y-m-d H:i:s' ),
+                        'notes'   => $new_note
+                    ),
+                    array(
+                        '%d',
+                        '%s',
+                        '%s',
+                        '%s'
+                    )
+                );
+
+            }
+            // Redirect back to the edit page
+            wp_redirect( admin_url() . 'admin.php?page=glm-members-admin-menu-glm-travel&glm_action=index&option=edit&id=' . $lead_id . '#ui-tabs-3' );
+            exit;
+            break;
+        case 'delete':
+            if ( isset( $_REQUEST['id'] )
+                && isset( $_REQUEST['note_id'] )
+                && $lead_id = filter_var( $_REQUEST['id'], FILTER_VALIDATE_INT )
+                && $note_id = filter_var( $_REQUEST['note_id'], FILTER_VALIDATE_INT )
+            ) {
+                $this->wpdb->delete(
+                    GLM_MEMBERS_TRAVEL_PLUGIN_DB_PREFIX . 'lead_notes',
+                    array( 'lead' => $lead_id, 'id'   => $note_id ),
+                    array( '%d', '%d' )
+                );
+            }
+            // Redirect back to the edit page
+            wp_redirect( admin_url() . 'admin.php?page=glm-members-admin-menu-glm-travel&glm_action=index&option=edit&id=' . $lead_id . '#ui-tabs-3' );
+            exit;
+            break;
         case 'list':
         default:
             $lead_id = filter_var( $_REQUEST['lead_id'], FILTER_VALIDATE_INT );
index fb3e0b0..fc0f2f7 100644 (file)
@@ -379,16 +379,6 @@ class GlmMembersAdmin_travel_index extends GlmDataTravelLeads
                 )
             );
 
-            // return array(
-            //     'status'           => true,
-            //     'modelRedirect'    => 'index',
-            //     // 'menuItemRedirect' => 'index',
-            //     'data'             => array(
-            //         'option' => 'edit',
-            //         'id'     => $this->entryId,
-            //     )
-            // );
-
             $entry = $this->editEntry( $this->entryId );
             $view = 'edit.html';
             break;
index 210fc8d..15a56e2 100644 (file)
@@ -1,16 +1,18 @@
-<form id="addLog" action="{$adminUrl}?page={$editPage}">
-    <input type="hidden" name="glm_action" value="index" />
-    <input type="hidden" name="id" value="{$lead_id}" />
-    <input type="hidden" name="page" value="{$editPage}" />
-    <input type="hidden" name="option" value="addLog" />
-    <textarea name="new_log" cols="30" rows="5"></textarea>
-    <input type="submit" class="button" value="New Note Entry" />
-</form>
+<div id="ui-tabs-3">
+    <form id="addLog" action="{$ajaxUrl}">
+        <input type="hidden" name="action" value="glm_members_admin_ajax" />
+        <input type="hidden" name="glm_action" value="travelNotes" />
+        <input type="hidden" name="id" value="{$lead_id}" />
+        <input type="hidden" name="option" value="add" />
+        <textarea name="new_log" cols="30" rows="5"></textarea>
+        <input type="submit" class="button" value="New Note Entry" />
+    </form>
 
-{foreach $notes as $n}
-    <div class="tsLog">
-        <h2>{$n.updated.datetime}</h2>
-        <p>{$n.notes|nl2br}</p>
-        <a class="tlDelete" href="{$adminUrl}?page={$editPage}&option=deleteLog&id={$lead_id}&note_id={$n.id}#ui-id-6" title="Delete this entry">Delete</a>
-    </div>
-{/foreach}
+    {foreach $notes as $n}
+        <div class="tsLog">
+            <h2>{$n.updated.datetime}</h2>
+            <p>{$n.notes|nl2br}</p>
+            <a class="tlDelete" href="{$ajaxUrl}?action=glm_members_admin_ajax&glm_action=travelNotes&option=delete&id={$lead_id}&note_id={$n.id}" title="Delete this entry">Delete</a>
+        </div>
+    {/foreach}
+</div>