CRM-10693 - LoadPage/Form improvements
authorColeman Watts <coleman@civicrm.org>
Fri, 22 Nov 2013 23:05:51 +0000 (15:05 -0800)
committerColeman Watts <coleman@civicrm.org>
Fri, 22 Nov 2013 23:05:51 +0000 (15:05 -0800)
CRM/Core/Page.php
CRM/Core/Page/AJAX.php
CRM/Core/QuickForm/Action/Display.php
js/Common.js
js/view/crm.designer.js

index 1ba8b8f326946877227300f02694d79f03928907..a30dcb3c00401eb8b7d65abb796a15908843e393 100644 (file)
@@ -123,14 +123,18 @@ class CRM_Core_Page {
       self::$_session = CRM_Core_Session::singleton();
     }
 
-    if (isset($_REQUEST['snippet']) && $_REQUEST['snippet']) {
+    // FIXME - why are we messing with 'snippet'? Why not just pass it directly into $this->_print?
+    if (!empty($_REQUEST['snippet'])) {
       if ($_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_PDF) {
         $this->_print = CRM_Core_Smarty::PRINT_PDF;
       }
       // FIXME - why does this number not match the constant?
-      else if ($_REQUEST['snippet'] == 5) {
+      elseif ($_REQUEST['snippet'] == 5) {
         $this->_print = CRM_Core_Smarty::PRINT_NOFORM;
       }
+      elseif ($_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_JSON) {
+        $this->_print = CRM_Core_Smarty::PRINT_JSON;
+      }
       else {
         $this->_print = CRM_Core_Smarty::PRINT_SNIPPET;
       }
@@ -164,7 +168,7 @@ class CRM_Core_Page {
 
     if ($this->_print) {
       if (in_array( $this->_print, array( CRM_Core_Smarty::PRINT_SNIPPET,
-        CRM_Core_Smarty::PRINT_PDF, CRM_Core_Smarty::PRINT_NOFORM ))) {
+        CRM_Core_Smarty::PRINT_PDF, CRM_Core_Smarty::PRINT_NOFORM, CRM_Core_Smarty::PRINT_JSON ))) {
         $content = self::$_template->fetch('CRM/common/snippet.tpl');
       }
       else {
@@ -184,6 +188,9 @@ class CRM_Core_Page {
           array('paper_size' => 'a3', 'orientation' => 'landscape')
         );
       }
+      elseif ($this->_print == CRM_Core_Smarty::PRINT_JSON) {
+        CRM_Core_Page_AJAX::returnJsonResponse($content);
+      }
       else {
         echo $content;
       }
index fa720eb8699d12227f77536254767ee13c430a0f..e39ac960eb949f9dff820035a2f78478bc9c26ba 100644 (file)
@@ -172,12 +172,15 @@ class CRM_Core_Page_AJAX {
     if (is_string($response)) {
       $response = array('content' => $response);
     }
+    // Add session variables to response
     $session = CRM_Core_Session::singleton();
     $response += array(
       'status' => 'success',
       'userContext' => htmlspecialchars_decode($session->readUserContext()),
+      'title' => CRM_Utils_System::$title,
     );
-    // crmMessages will be automatically handled by our client-side ajax preprocessor @see Common.js
+    // crmMessages will be automatically handled by our ajax preprocessor
+    // @see js/Common.js
     if ($session->getStatus(FALSE)) {
       $response['crmMessages'] = $session->getStatus(TRUE);
     }
index ad9cc918733c179fb936365fbd14e4580f6ae7dd..832b617d7977db4d1e8dc4c3de290d0b092571d5 100644 (file)
@@ -159,7 +159,6 @@ class CRM_Core_QuickForm_Action_Display extends CRM_Core_QuickForm_Action {
     if ($controller->_QFResponseType == 'json') {
       $response = array(
         'content' => $html,
-        'title' => CRM_Utils_System::$title,
       );
       if (!empty($form['errors'])) {
         $response['status'] = 'form_error';
index 60738a3489cb16677e10b8126a89fe22bbd74636..7387bb37248c72dc3b01931acd628e09e1836df3 100644 (file)
@@ -820,11 +820,11 @@ CRM.validate = CRM.validate || {
     });
   }
 
+  var dialogCount = 0;
   CRM.loadPage = function(url, options) {
     options = options || {};
-    url += (url.indexOf('?') < 0 ? '?' : '&') + 'snippet=6';
     var settings = {
-      target: '#crm-ajax-dialog',
+      target: '#crm-ajax-dialog-' + (dialogCount++),
       dialog: {
         modal: true,
         width: '65%',
@@ -834,74 +834,91 @@ CRM.validate = CRM.validate || {
           $(this).remove();
         }
       },
-      onLoad: null,
-      type: 'Page',
-      url: url
+      autoClose: true,
+      onFailure: function(data, settings) {
+        if (settings.dialog !== false && settings.autoClose) {
+          $(this).dialog('close');
+        }
+        CRM.alert(ts('Unable to reach the server. Please refresh this page in your browser and try again.'), ts('Network Error'), 'error');
+      },
+      type: 'Page'
     };
     $.extend(true, settings, options);
+    // Add snippet argument to url
+    if (url.search(/[&?]snippet=/) < 0) {
+      url += (url.indexOf('?') < 0 ? '?' : '&') + 'snippet=6';
+    }
+    settings.url = url;
     // Create new dialog
-    if (settings.dialog && settings.target[0] == '#') {
+    if (settings.dialog !== false && settings.target[0] == '#') {
       $('<div id="'+ settings.target.substring(1) +'" class="crm-container"><div class="crm-loading-element">' + ts('Loading') + '...</div></div>').dialog(settings.dialog);
     }
     $.getJSON(url, function(data) {
+      if (typeof(data) != 'object' || typeof(data.content) != 'string') {
+        settings.onFailure.call($(settings.target), data, settings);
+        return;
+      }
       if (settings.dialog && !settings.dialog.title && data.title) {
         $(settings.target).dialog('option', 'title', data.title);
       }
       settings.content = data.content;
       $(settings.target).html(data.content).trigger('crm' + settings.type + 'Load', data);
       if (typeof(settings.onLoad) == 'function') {
-        settings.onLoad(data, settings);
+        settings.onLoad.call($(settings.target),data, settings);
       }
     });
+    return $(settings.target);
   };
 
   CRM.loadForm = function(url, options) {
     options = options || {};
     var settings = {
-      target: '#crm-ajax-dialog',
       validate: true,
-      onLoad: null,
-      onCancel: function(e) {
-        if (settings.dialog !== false) {
-          $(settings.target).dialog('close');
-        }
+      onCancel: function(event, settings) {
         return false;
       },
       onError: function(data, settings) {
-        $(settings.target).html(data.content).trigger('crmFormReload', data);
+        $(this).html(data.content).trigger('crmFormReload', data);
         if (typeof(data.errors) == 'object') {
-          $.each(data.errors, function(target, msg) {
-            $('[name="'+target+'"]').crmError(msg);
+          $.each(data.errors, function(formElement, msg) {
+            $('[name="'+formElement+'"]').crmError(msg);
           });
         }
-        settings.onLoad(data, settings);
-      },
-      onSuccess: function(data, settings) {
-        if (settings.dialog !== false) {
-          $(settings.target).dialog('close');
-        }
+        settings.onLoad.call($(settings.target), data, settings);
       }
     };
     $.extend(settings, options, {type: 'Form'});
     settings.onLoad = function(data, settings) {
-      $(".cancel.form-submit", settings.target).click(settings.onCancel);
+      $(".cancel.form-submit", this).click(function(event) {
+        var returnVal = settings.onCancel.call($(settings.target), event, settings);
+        if (settings.dialog !== false && settings.autoClose && returnVal !== true) {
+          $(settings.target).dialog('close');
+        }
+        return returnVal === true ? returnVal : false;
+      });
       if (settings.validate) {
-        $("form", settings.target).validate(typeof(settings.validate) == 'object' ? settings.validate : CRM.validate.params);
+        $("form", this).validate(typeof(settings.validate) == 'object' ? settings.validate : CRM.validate.params);
       }
-      $("form", settings.target).ajaxForm({
+      $("form", this).ajaxForm({
         url: settings.url,
         dataType: 'json',
         success: function(response) {
           if (response.status == 'success') {
-            settings.onSuccess(response, settings);
+            if (typeof(settings.onSuccess) === 'function') {
+              settings.onSuccess.call($(settings.target), response, settings);
+            }
             $(settings.target).unblock().trigger('crmFormSuccess', data);
+            // Reset form for e.g. "save and new"
             if (settings.resetButton && response.buttonName == settings.resetButton) {
               $(settings.target).html(settings.content).trigger('crmFormLoad', data);
-              settings.onLoad(data, settings);
+              settings.onLoad.call($(settings.target), data, settings);
+            }
+            else if (settings.dialog !== false && settings.autoClose) {
+              $(settings.target).dialog('close');
             }
           }
           else {
-            settings.onError(response, settings);
+            settings.onError.call($(settings.target), response, settings);
           }
         },
         beforeSubmit: function() {
@@ -910,10 +927,10 @@ CRM.validate = CRM.validate || {
       });
       // Call original onLoad fn
       if (typeof(options.onLoad) == 'function') {
-        options.onLoad(data, settings);
+        options.onLoad.call($(settings.target), data, settings);
       }
     };
-    CRM.loadPage(url, settings);
+    return CRM.loadPage(url, settings);
   };
 
   // Preprocess all cj ajax calls to display messages
index 51e1ec739b422fee171e4f53d65c1aa6cdd8e684..f35d60b0b69f37d009acc2efef3b664e721f0f74 100644 (file)
       CRM.loadForm(url, {
         resetButton: 'next_new',
         onSuccess: function(data, settings) {
-          paletteView.doRefresh('custom_' + data.customField.id);
-          if (data.buttonName != 'next_new') {
-            $(settings.target).dialog('close');
-          }
+          paletteView.doRefresh('custom_' + data.id);
         }
       });
       return false;