Update the edit form so it shows errors
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 4 Dec 2017 17:42:37 +0000 (12:42 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 4 Dec 2017 17:50:11 +0000 (12:50 -0500)
Show update or insert errors or success messages.

models/admin/settings/notificationTypes.php
views/admin/settings/editNotificationType.html

index fa07c07..521ee6f 100644 (file)
@@ -99,14 +99,19 @@ class GlmMembersAdmin_settings_notificationTypes extends GlmDataNotificationType
     public function modelAction ($actionData = false)
     {
 
-        $id                    = 0;
-        $option                = 'list';
-        $view                  = 'notificationTypes';
-        $success               = true;
-        $haveNotificationTypes = false;
-        $notificationTypes     = false;
-        $error                 = false;
-        $enable_members        = $this->config['settings']['enable_members'];
+        $id                      = 0;
+        $option                  = 'list';
+        $view                    = 'notificationTypes';
+        $success                 = true;
+        $notificationUpdated     = false;
+        $notificationUpdateError = false;
+        $notificationAdded       = false;
+        $notificationInsertError = false;
+        $haveNotificationTypes   = false;
+        $notificationTypes       = false;
+        $notification            = false;
+        $error                   = false;
+        $enable_members          = $this->config['settings']['enable_members'];
 
         // Check for region id
         $id = 0;
@@ -139,34 +144,45 @@ class GlmMembersAdmin_settings_notificationTypes extends GlmDataNotificationType
 
         switch($_REQUEST['option']) {
         case 'add':
-            $notificationTypes = $this->newEntry();
+            $notification = $this->newEntry();
             $view = 'editNotificationType';
             break;
 
         case 'insert':
             $view = 'editNotificationType';
-            $test = $this->insertEntry();
-            echo '<pre>$test: ' . print_r($test, true) . '</pre>';
-            $notificationTypes = $this->getList();
+            $notification = $this->insertEntry();
+            if ( $notification['status'] ) {
+                $notificationAdded = true;
+                $id = $notification['fieldData']['id'];
+                $notification = $this->editEntry( $id );
+            } else {
+                $notificationInsertError = true;
+            }
             break;
 
         case 'edit':
-            $notificationTypes = $this->editEntry( $id );
+            $notification = $this->editEntry( $id );
             $view = 'editNotificationType';
             break;
 
         case 'update':
             if ($id > 0) {
-                $this->updateEntry($id);
+                $notification = $this->updateEntry($id);
+                if ( $notification['status'] ) {
+                    $notificationUpdated = true;
+                    $notification = $this->editEntry( $id );
+                } else {
+                    $notificationUpdateError = true;
+                }
+                $view = 'editNotificationType';
             }
-            $notificationTypes = $this->getList();
+
             break;
 
         case 'delete':
             if ($id > 0) {
                 $this->deleteEntry($id, true);
             }
-
         case 'list':
         default:
             // Get a current list of members
@@ -175,8 +191,6 @@ class GlmMembersAdmin_settings_notificationTypes extends GlmDataNotificationType
         }
 
 
-        // echo '<pre>$notificationTypes: ' . print_r( $notificationTypes, true ) . '</pre>';
-
         // If we have list entries - even if it's an empty list
         $success = true;
         $haveNotificationTypes = false;
@@ -203,14 +217,19 @@ class GlmMembersAdmin_settings_notificationTypes extends GlmDataNotificationType
 
         // Compile template data
         $templateData = array(
-            'notification_id'       => $id,
-            'action'                => $_REQUEST['glm_action'],
-            'enable_members'        => $enable_members,
-            'haveNotificationTypes' => $haveNotificationTypes,
-            'notificationTypes'     => $notificationTypes,
-            'send_date_period'      => $this->config['send_date_period'],
-            'send_date_when'        => $this->config['send_date_when'],
-            'send_action'           => $this->config['send_action'],
+            'notification_id'         => $id,
+            'action'                  => $_REQUEST['glm_action'],
+            'enable_members'          => $enable_members,
+            'haveNotificationTypes'   => $haveNotificationTypes,
+            'notificationTypes'       => $notificationTypes,
+            'notification'            => $notification,
+            'notificationUpdated'     => $notificationUpdated,
+            'notificationUpdateError' => $notificationUpdateError,
+            'notificationAdded'       => $notificationAdded,
+            'notificationInsertError' => $notificationInsertError,
+            'send_date_period'        => $this->config['send_date_period'],
+            'send_date_when'          => $this->config['send_date_when'],
+            'send_action'             => $this->config['send_action'],
         );
 
         // Return status, suggested view, and data to controller
index 77ee708..305975a 100644 (file)
@@ -2,7 +2,10 @@
 
 {include file='admin/settings/subHeader.html'}
 
-Show error header here
+{if $notificationUpdated}<span class="glm-notice glm-flash-updated">Notification Updated</span>{/if}
+{if $notificationUpdateError}<span class="glm-notice glm-flash-updated">Notification Update Error</span>{/if}
+{if $notificationInsertError}<span class="glm-notice glm-flash-updated">Notification Insert Error</span>{/if}
+{if $notificationAdded}<span class="glm-notice glm-flash-updated">Notification Added</span>{/if}
 
 <form action="{$thisUrl}?page={$thisPage}&glm_action=notificationTypes" method="post">
     {if $notification_id}
@@ -15,21 +18,23 @@ Show error header here
     <table class="glm-admin-table glm-event-table">
 
         <tr>
-            <th {if $notificationTypes.fieldRequired.name} class="glm-required"}{/if}>Name</th>
-            <td {if $notificationTypes.fieldFail.name}class="glm-form-bad-input" data-tabid="glm-name"{/if}>
-                <input type="text" name="name" value="{$notificationTypes.fieldData.name}" class="glm-form-text-input-medium">
-                {if $notificationTypes.fieldFail.name}<p>{$notificationTypes.fieldFail.name}</p>{/if}<br>
+            <th {if $notification.fieldRequired.name} class="glm-required"}{/if}>Name</th>
+            <td {if $notification.fieldFail.name}class="glm-form-bad-input" data-tabid="glm-name"{/if}>
+                <input type="text" name="name" value="{$notification.fieldData.name}" class="glm-form-text-input-medium">
+                {if $notification.fieldFail.name}<p>{$notification.fieldFail.name}</p>{/if}<br>
             </td>
         </tr>
 
         <tr>
             <th colspan="2" class="glm-required">Send Notification based on</th>
         </tr>
+
         <tr>
             <th colspan="2">Due Date</th>
         </tr>
+
         <tr>
-            <th class="glm-required"><input type="radio" name="send_by" value="date" required{if $notificationTypes.fieldData.send_by_date.value} checked{/if}></th>
+            <th class="glm-required"><input type="radio" name="send_by" value="date" required{if $notification.fieldData.send_by_date.value} checked{/if}></th>
             <td>
                 <input
                     type="number"
@@ -37,24 +42,27 @@ Show error header here
                     min="0"
                     max="100"
                     name="send_date_number"
+                    value="{$notification.fieldData.send_date_number}"
                     class="glm-form-text-input-veryshort">
                 <select name="send_date_period">
                     {foreach $send_date_period as $id => $val}
-                        <option value="{$id}">{$val}</option>
+                        <option value="{$id}"{if $notification.fieldData.send_date_period ==  $id} selected{/if}>{$val}</option>
                     {/foreach}
                 </select>
                 <select name="send_date_when">
                     {foreach $send_date_when as $id => $val}
-                        <option value="{$id}">{$val}</option>
+                        <option value="{$id}"{if $notification.fieldData.send_date_when ==  $id} selected{/if}>{$val}</option>
                     {/foreach}
                 </select>
             </td>
         </tr>
+
         <tr>
             <th colspan="2">Immediate Action</th>
         </tr>
+
         <tr>
-            <th><input type="radio" name="send_by" value="action" required{if $notificationTypes.fieldData.send_by_action.value} checked{/if}></th>
+            <th><input type="radio" name="send_by" value="action" required{if $notification.fieldData.send_by_action.value} checked{/if}></th>
             <td>
                 <select name="send_action">
                     {foreach $send_action as $id => $val}
@@ -65,40 +73,40 @@ Show error header here
         </tr>
 
         <tr>
-            <th {if $notificationTypes.fieldRequired.subject} class="glm-required"}{/if}>Subject</th>
-            <td {if $notificationTypes.fieldFail.subject}class="glm-form-bad-input" data-tabid="glm-subject"{/if}>
-                <input type="text" name="subject" value="{$notificationTypes.fieldData.subject}" class="glm-form-text-input-medium">
-            {if $notificationTypes.fieldFail.subject}<p>{$notificationTypes.fieldFail.subject}</p>{/if}<br>
+            <th {if $notification.fieldRequired.subject} class="glm-required"}{/if}>Subject</th>
+            <td {if $notification.fieldFail.subject}class="glm-form-bad-input" data-tabid="glm-subject"{/if}>
+                <input type="text" name="subject" value="{$notification.fieldData.subject}" class="glm-form-text-input-medium">
+            {if $notification.fieldFail.subject}<p>{$notification.fieldFail.subject}</p>{/if}<br>
             </td>
         </tr>
 
         <tr>
-            <th {if $notificationTypes.fieldRequired.from_header} class="glm-required"}{/if}>From</th>
-            <td {if $notificationTypes.fieldFail.from_header}class="glm-form-bad-input" data-tabid="glm-fromheader"{/if}>
-                <input type="text" name="from_header" value="{$notificationTypes.fieldData.from_header}" class="glm-form-text-input-medium">
-            {if $notificationTypes.fieldFail.from_header}<p>{$notificationTypes.fieldFail.from_header}</p>{/if}<br>
+            <th {if $notification.fieldRequired.from_header} class="glm-required"}{/if}>From</th>
+            <td {if $notification.fieldFail.from_header}class="glm-form-bad-input" data-tabid="glm-fromheader"{/if}>
+                <input type="text" name="from_header" value="{$notification.fieldData.from_header}" class="glm-form-text-input-medium">
+            {if $notification.fieldFail.from_header}<p>{$notification.fieldFail.from_header}</p>{/if}<br>
             </td>
         </tr>
 
         <tr>
-            <th {if $notificationTypes.fieldRequired.replyto} class="glm-required"}{/if}>Reply-To</th>
-            <td {if $notificationTypes.fieldFail.replyto}class="glm-form-bad-input" data-tabid="glm-replyto"{/if}>
-                <input type="text" name="replyto" value="{$notificationTypes.fieldData.replyto}" class="glm-form-text-input-medium">
-            {if $notificationTypes.fieldFail.replyto}<p>{$notificationTypes.fieldFail.replyto}</p>{/if}<br>
+            <th {if $notification.fieldRequired.replyto} class="glm-required"}{/if}>Reply-To</th>
+            <td {if $notification.fieldFail.replyto}class="glm-form-bad-input" data-tabid="glm-replyto"{/if}>
+                <input type="text" name="replyto" value="{$notification.fieldData.replyto}" class="glm-form-text-input-medium">
+            {if $notification.fieldFail.replyto}<p>{$notification.fieldFail.replyto}</p>{/if}<br>
             </td>
         </tr>
 
         <tr>
-            <th {if $notificationTypes.fieldRequired.message}class="glm-required"{/if}>Message:</th>
-            <td {if $notificationTypes.fieldFail.message}class="glm-form-bad-input" data-tabid="glm-message"{/if}>
+            <th {if $notification.fieldRequired.message}class="glm-required"{/if}>Message:</th>
+            <td {if $notification.fieldFail.message}class="glm-form-bad-input" data-tabid="glm-message"{/if}>
                 {php}
-                    wp_editor('{$notificationTypes.fieldData.message|escape:quotes}', 'glm_descr', array(
+                    wp_editor('{$notification.fieldData.message|escape:quotes}', 'glm_descr', array(
                         'media_buttons' => false,
                         'textarea_name' => 'message',
                         'editor_height' => 200,
                     ));
                 {/php}
-                {if $notificationTypes.fieldFail.message}<p>{$notificationTypes.fieldFail.message}</p>{/if}
+                {if $notification.fieldFail.message}<p>{$notification.fieldFail.message}</p>{/if}
             </td>
         </tr>
 
@@ -112,5 +120,12 @@ Show error header here
     </table>
 </form>
 
+<script>
+jQuery(document).ready(function($){
+    // Flash certain elements for a short time after display
+    $(".glm-flash-updated").fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500).fadeOut(500);
+
+});
+</script>
 
 {include file='admin/footer.html'}