Adding, editing, and deleting reg_class records now works in admin.
authorChuck Scott <cscott@gaslightmedia.com>
Wed, 20 Sep 2017 17:04:55 +0000 (13:04 -0400)
committerChuck Scott <cscott@gaslightmedia.com>
Wed, 20 Sep 2017 17:04:55 +0000 (13:04 -0400)
js/adminRegApp.js
js/views/admin/regClass.js
models/admin/ajax/regAdmin.php
models/admin/ajax/regAdmin/regClasses.php
views/admin/registrations/eventDashboard.html

index 3a42b32..399e75d 100644 (file)
@@ -156,36 +156,53 @@ app.Views.Admin.RegClass = Backbone.View.extend({
         var parentEvent = this.model.get( 'parent' );
 
         // Try to save the registration class
-        $newID = this.model.save({ option: 'add', parent: parentEvent.id, name: cName, descr: cDescr }); 
-        if (!$newID){
-            console.log('AJAX Save Error');
-        } else {
-            console.log('AJAX Save Successful');
-        }
-        
-        this.model.set( 'id', $newID );
-        
-        this.$('.class-display-template').show();
-        this.$('.class-edit-template').hide();
-        this.$('.class-update').hide();
-        this.$('.class-add').hide();
-        this.$('.class-edit').show();
+        var newID = false;
+        myself = this;
+        this.model.save({ 
+            wait: true,
+            option: 'add', 
+            parent: parentEvent.id, 
+            name: cName, 
+            descr: cDescr 
+        }).success(function(data){
+            if (data > 0) {
+                console.log('AJAX Save Successful: ID = '+data);
+                myself.model.set({id: data});
+                myself.$('.class-display-template').show();
+                myself.$('.class-edit-template').hide();
+                myself.$('.class-update').hide();
+                myself.$('.class-add').hide();
+                myself.$('.class-edit').show();
+                myself.newClass = false;
+            } else {
+                alert('Unable to store data at this time.');
+            }
+        }).error(function(m,e){
+           console.log(e);
+           alert('There was a problem communicating with the AJAX server.');
+        });
+
+        console.log('New ID = '+newID);
 
         // Add this new model to the collection.
         var parentEvent = this.model.get( 'parent' );           // ????????
 //        parentEvent.classes.create( this.model );
         
         glmSubmitRequired -= 1; 
+
     },
 
     deleteLevel: function(){
         console.log('deleteLevel called');
-        if (this.newClass) {
-            alert('removing a new class. May have to avoid doing any ajax call if not updated yet.')
-        }
+        
+        // Remove class here
+        var x = this.model.get('id');
+        this.model.destroy({data: { 
+            id: x,
+            option: 'delete'
+        }, processData: true});
+
         this.remove();
-        alert('Need to check that this is actually removing from database..');
     },
     
     cancel: function(){
@@ -197,8 +214,9 @@ app.Views.Admin.RegClass = Backbone.View.extend({
         console.log('editLevel called');
         this.$('.class-display-template').hide();
         this.$('.class-edit-template').show();
-        this.$('.class-name').focus();
         this.$('.class-update').show();
+        this.$('.class-edit').hide();
+        this.$('.class-name').focus();
         glmSubmitRequired += 1;               
     },
 
@@ -213,6 +231,8 @@ app.Views.Admin.RegClass = Backbone.View.extend({
         }
         this.$('.class-display-template').show();
         this.$('.class-edit-template').hide();
+        this.$('.class-update').hide();
+        this.$('.class-edit').show();
         glmSubmitRequired -= 1;  
     },
 
@@ -224,6 +244,7 @@ app.Views.Admin.RegClass = Backbone.View.extend({
             this.$('.class-display-template').hide();
             this.$('.class-edit-template').show();
             this.$('.class-update').hide();
+            this.$('.class-edit').show();
             this.$('.class-add').show();
             this.$('.class-name').focus();
             this.newClass = true;
index a012745..b57e4ad 100644 (file)
@@ -41,36 +41,53 @@ app.Views.Admin.RegClass = Backbone.View.extend({
         var parentEvent = this.model.get( 'parent' );
 
         // Try to save the registration class
-        $newID = this.model.save({ option: 'add', parent: parentEvent.id, name: cName, descr: cDescr }); 
-        if (!$newID){
-            console.log('AJAX Save Error');
-        } else {
-            console.log('AJAX Save Successful');
-        }
-        
-        this.model.set( 'id', $newID );
-        
-        this.$('.class-display-template').show();
-        this.$('.class-edit-template').hide();
-        this.$('.class-update').hide();
-        this.$('.class-add').hide();
-        this.$('.class-edit').show();
+        var newID = false;
+        myself = this;
+        this.model.save({ 
+            wait: true,
+            option: 'add', 
+            parent: parentEvent.id, 
+            name: cName, 
+            descr: cDescr 
+        }).success(function(data){
+            if (data > 0) {
+                console.log('AJAX Save Successful: ID = '+data);
+                myself.model.set({id: data});
+                myself.$('.class-display-template').show();
+                myself.$('.class-edit-template').hide();
+                myself.$('.class-update').hide();
+                myself.$('.class-add').hide();
+                myself.$('.class-edit').show();
+                myself.newClass = false;
+            } else {
+                alert('Unable to store data at this time.');
+            }
+        }).error(function(m,e){
+           console.log(e);
+           alert('There was a problem communicating with the AJAX server.');
+        });
+
+        console.log('New ID = '+newID);
 
         // Add this new model to the collection.
         var parentEvent = this.model.get( 'parent' );           // ????????
 //        parentEvent.classes.create( this.model );
         
         glmSubmitRequired -= 1; 
+
     },
 
     deleteLevel: function(){
         console.log('deleteLevel called');
-        if (this.newClass) {
-            alert('removing a new class. May have to avoid doing any ajax call if not updated yet.')
-        }
+        
+        // Remove class here
+        var x = this.model.get('id');
+        this.model.destroy({data: { 
+            id: x,
+            option: 'delete'
+        }, processData: true});
+
         this.remove();
-        alert('Need to check that this is actually removing from database..');
     },
     
     cancel: function(){
@@ -82,8 +99,9 @@ app.Views.Admin.RegClass = Backbone.View.extend({
         console.log('editLevel called');
         this.$('.class-display-template').hide();
         this.$('.class-edit-template').show();
-        this.$('.class-name').focus();
         this.$('.class-update').show();
+        this.$('.class-edit').hide();
+        this.$('.class-name').focus();
         glmSubmitRequired += 1;               
     },
 
@@ -98,6 +116,8 @@ app.Views.Admin.RegClass = Backbone.View.extend({
         }
         this.$('.class-display-template').show();
         this.$('.class-edit-template').hide();
+        this.$('.class-update').hide();
+        this.$('.class-edit').show();
         glmSubmitRequired -= 1;  
     },
 
@@ -109,6 +129,7 @@ app.Views.Admin.RegClass = Backbone.View.extend({
             this.$('.class-display-template').hide();
             this.$('.class-edit-template').show();
             this.$('.class-update').hide();
+            this.$('.class-edit').show();
             this.$('.class-add').show();
             this.$('.class-name').focus();
             this.newClass = true;
index 40f797d..460a02f 100644 (file)
@@ -70,17 +70,22 @@ class GlmMembersAdmin_ajax_regAdmin
      */
     public function modelAction( $actionData = false )
     {
-
+        
         // Get Backbone collection we're talking with
         $collection = filter_var( $_REQUEST['collection'], FILTER_SANITIZE_STRING );
    
         // If not a valid collection, die quietly
         if (!in_array($collection, array('regClasses', 'regRates'))) {
-            wp_die();
+            $this->failureResponse('Not a valid desitination (collection?).');
         }
 
-        // Try to decode the JSON
-        $modelData = json_decode(stripslashes($_REQUEST['model']), true);
+        // Try to decode the JSON - if not provided, use REQUEST data instead
+        $modelData = array();
+        if (isset($_REQUEST['model'])) {
+            $modelData = json_decode(stripslashes($_REQUEST['model']), true);
+        } else {
+            $modelData = $_REQUEST;
+        }
         
         // Build model and path and class names
         $modelName = GLM_MEMBERS_REGISTRATIONS_PLUGIN_PATH.'/models/admin/ajax/regAdmin/'.$collection.'.php';
@@ -107,10 +112,10 @@ class GlmMembersAdmin_ajax_regAdmin
         // Instantiate the model and ask it to perform the work
         $model = new $className($this->wpdb, $this->config);
         
-trigger_error(print_r($modelData,1));
-        
         $results = $model->modelAction($modelData);
         
         
     }
+    
+
 }
index f690f2b..90fc203 100644 (file)
@@ -81,6 +81,8 @@ class GlmMembersAdmin_registrations_ajax_regClasses extends GlmDataRegistrations
     public function modelAction($modelData)
     {
 
+//trigger_error(print_r($modelData,1));
+
         // Perform specified action
         switch ($modelData['option']) {
             
@@ -112,7 +114,11 @@ class GlmMembersAdmin_registrations_ajax_regClasses extends GlmDataRegistrations
                     // If there's a new classID, declare success
                     $classID = $this->wpdb->insert_id;
                     if ($classID) {
-                        return $classID;
+                        echo json_encode($classID);
+                        wp_die();
+                    } else {
+                        echo json_encode(false);
+                        wp_die();
                     }
                     
                 }
@@ -148,6 +154,14 @@ class GlmMembersAdmin_registrations_ajax_regClasses extends GlmDataRegistrations
                 break;
                 
             case 'delete':
+                
+                $classID = ($modelData['id'] - 0);
+                if ($classID > 0) {
+                    $this->wpdb->delete( $this->table, array( 'id' => $classID ), array( '%d' ) );
+                }
+                
+                return;
+                
                 break;
                 
             default:
index b7542b6..20f7fc2 100644 (file)
@@ -72,7 +72,7 @@
     <table id="glm-table-calendar" class="glm-admin-table glm-event-table">
         <tr>
             <td>
-                <div id="eventCalendar">(calendar loads here)</div>
+                <div id="eventCalendar" style="width: 50%;"></div>
             </td>
         </tr>
     </table>