From: Coleman Watts Date: Sun, 17 Nov 2013 05:03:12 +0000 (-0800) Subject: CRM-10693 - AJAX - Standardize protocol for page, form, messages X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=03a7ec8f9b74a0d9ca59afd436ef87d4a0dc2630;p=civicrm-core.git CRM-10693 - AJAX - Standardize protocol for page, form, messages --- diff --git a/CRM/Contact/Form/Inline.php b/CRM/Contact/Form/Inline.php index 6a88ac5912..01289571a6 100644 --- a/CRM/Contact/Form/Inline.php +++ b/CRM/Contact/Form/Inline.php @@ -140,14 +140,13 @@ abstract class CRM_Contact_Form_Inline extends CRM_Core_Form { } /** - * Final response from successful form submit - * - * @param response: array - data to send to the client + * Common function for all inline contact edit forms + * Prepares ajaxResponse * * @return void * @protected */ - protected function response($response = array()) { + protected function response() { // Load changelog footer from template $smarty = CRM_Core_Smarty::singleton(); $smarty->assign('contactId', $this->_contactId); @@ -157,27 +156,16 @@ abstract class CRM_Contact_Form_Inline extends CRM_Core_Form { 'contact_view_options', TRUE ); $smarty->assign('changeLog', $viewOptions['log']); - $response = array_merge( + $this->ajaxResponse = array_merge( array( - 'status' => 'save', 'changeLog' => array( 'count' => CRM_Contact_BAO_Contact::getCountComponent('log', $this->_contactId), 'markup' => $smarty->fetch('CRM/common/contactFooter.tpl'), ), ), - $response, + $this->ajaxResponse, CRM_Contact_Form_Inline_Lock::getResponse($this->_contactId) ); - $this->postProcessHook(); - // CRM-11831 @see http://www.malsup.com/jquery/form/#file-upload - $xhr = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'; - if (!$xhr) { - echo ''; - } - CRM_Utils_System::civiExit(); + // Note: Post hooks will be called by CRM_Core_Form::mainProcess } } diff --git a/CRM/Contact/Form/Inline/Address.php b/CRM/Contact/Form/Inline/Address.php index 2b6bb6fb8e..9e74ab31e2 100644 --- a/CRM/Contact/Form/Inline/Address.php +++ b/CRM/Contact/Form/Inline/Address.php @@ -204,6 +204,7 @@ class CRM_Contact_Form_Inline_Address extends CRM_Contact_Form_Inline { $address = CRM_Core_BAO_Address::create($params, TRUE); $this->log(); - $this->response(array('addressId' => $address[0]->id)); + $this->ajaxResponse['addressId'] = $address[0]->id; + $this->response(); } } diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 5e6f2c78d3..46f69b9d2c 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -98,6 +98,13 @@ class CRM_Core_Form extends HTML_QuickForm_Page { */ static protected $_template; + /** + * What to return to the client if in ajax mode (snippet=6) + * + * @var array + */ + public $ajaxResponse = array(); + /** * constants for attributes for various form elements * attempt to standardize on the number of variations that we @@ -159,6 +166,8 @@ class CRM_Core_Form extends HTML_QuickForm_Page { if (!isset(self::$_template)) { self::$_template = CRM_Core_Smarty::singleton(); } + + $this->assign('snippet', (int) CRM_Utils_Array::value('snippet', $_REQUEST)); } static function generateID() { @@ -259,8 +268,14 @@ class CRM_Core_Form extends HTML_QuickForm_Page { */ function mainProcess() { $this->postProcess(); - $this->postProcessHook(); + + // Respond with JSON if in AJAX context + if (!empty($_REQUEST['snippet']) && $_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_JSON) { + $this->ajaxResponse['buttonName'] = str_replace('_qf_' . $this->getAttribute('id') . '_', '', $this->controller->getButtonName()); + $this->ajaxResponse['action'] = $this->_action; + CRM_Core_Page_AJAX::returnJsonResponse($this->ajaxResponse); + } } /** diff --git a/CRM/Core/Page.php b/CRM/Core/Page.php index 95ff557578..1ba8b8f326 100644 --- a/CRM/Core/Page.php +++ b/CRM/Core/Page.php @@ -124,9 +124,10 @@ class CRM_Core_Page { } if (isset($_REQUEST['snippet']) && $_REQUEST['snippet']) { - if ($_REQUEST['snippet'] == 3) { + 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) { $this->_print = CRM_Core_Smarty::PRINT_NOFORM; } diff --git a/CRM/Core/Page/AJAX.php b/CRM/Core/Page/AJAX.php index 8c7eff678c..fa720eb869 100644 --- a/CRM/Core/Page/AJAX.php +++ b/CRM/Core/Page/AJAX.php @@ -162,5 +162,36 @@ class CRM_Core_Page_AJAX { return FALSE; } } + + /** + * Outputs the CiviCRM standard json-formatted page/form response + * @param array|string $response + */ + static function returnJsonResponse($response) { + // Allow lazy callers to not wrap content in an array + if (is_string($response)) { + $response = array('content' => $response); + } + $session = CRM_Core_Session::singleton(); + $response += array( + 'status' => 'success', + 'userContext' => htmlspecialchars_decode($session->readUserContext()), + ); + // crmMessages will be automatically handled by our client-side ajax preprocessor @see Common.js + if ($session->getStatus(FALSE)) { + $response['crmMessages'] = $session->getStatus(TRUE); + } + + // CRM-11831 @see http://www.malsup.com/jquery/form/#file-upload + $xhr = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'; + if (!$xhr) { + echo ''; + } + CRM_Utils_System::civiExit(); + } } diff --git a/CRM/Core/QuickForm/Action/Display.php b/CRM/Core/QuickForm/Action/Display.php index b02bd4f32b..14c76669c6 100644 --- a/CRM/Core/QuickForm/Action/Display.php +++ b/CRM/Core/QuickForm/Action/Display.php @@ -115,8 +115,8 @@ class CRM_Core_QuickForm_Action_Display extends CRM_Core_QuickForm_Action { $template = CRM_Core_Smarty::singleton(); $form = $page->toSmarty(); + // Deprecated - use snippet=6 instead of json=1 $json = CRM_Utils_Request::retrieve('json', 'Boolean', CRM_Core_DAO::$_nullObject); - if ($json) { echo json_encode($form); CRM_Utils_System::civiExit(); @@ -158,16 +158,11 @@ class CRM_Core_QuickForm_Action_Display extends CRM_Core_QuickForm_Action { if ($controller->_QFResponseType == 'json') { $response = array('content' => $html); - // CRM-11831 @see http://www.malsup.com/jquery/form/#file-upload - $xhr = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'; - if (!$xhr) { - echo ''; + if (!empty($form['errors'])) { + $response['status'] = 'form_error'; + $response['errors'] = $form['errors']; } - CRM_Utils_System::civiExit(); + CRM_Core_Page_AJAX::returnJsonResponse($response); } if ($print) { diff --git a/CRM/Core/Session.php b/CRM/Core/Session.php index d667b83725..7eb7b71629 100644 --- a/CRM/Core/Session.php +++ b/CRM/Core/Session.php @@ -509,7 +509,7 @@ class CRM_Core_Session { 'text' => $text, 'title' => $title, 'type' => $type, - 'options' => $options ? json_encode($options) : NULL, + 'options' => $options ? $options : NULL, ); } } diff --git a/js/Common.js b/js/Common.js index e5d14fef7f..21dca3559e 100644 --- a/js/Common.js +++ b/js/Common.js @@ -820,6 +820,22 @@ CRM.validate = CRM.validate || { }); } + // Preprocess all cj ajax calls to display messages + $(document).ajaxSuccess(function(event, xhr, settings) { + try { + if ((!settings.dataType || settings.dataType == 'json') && xhr.responseText) { + var response = $.parseJSON(xhr.responseText); + if (typeof(response.crmMessages) == 'object') { + $.each(response.crmMessages, function(n, msg) { + CRM.alert(msg.text, msg.title, msg.type, msg.options); + }) + } + } + } + // Suppress errors + catch (e) {} + }); + $(function () { if ($('#crm-notification-container').length) { // Initialize notifications diff --git a/templates/CRM/Contact/Page/View/Summary.js b/templates/CRM/Contact/Page/View/Summary.js index 82a46c4ce6..dc91dc6649 100644 --- a/templates/CRM/Contact/Page/View/Summary.js +++ b/templates/CRM/Contact/Page/View/Summary.js @@ -44,7 +44,7 @@ function requestHandler(response) { var o = $('div.crm-inline-edit.form'); - if (response.status == 'save' || response.status == 'cancel') { + if (response.status == 'success' || response.status == 'cancel') { o.trigger('crmFormSuccess', [response]); $('.crm-inline-edit-container').addClass('crm-edit-ready'); var data = o.data('edit-params'); diff --git a/templates/CRM/Form/body.tpl b/templates/CRM/Form/body.tpl index 2221e69457..01ef1b845d 100644 --- a/templates/CRM/Form/body.tpl +++ b/templates/CRM/Form/body.tpl @@ -33,7 +33,7 @@
{$form.hidden}
{/if} -{if ! $suppressForm and count($form.errors) gt 0} +{if $snippet neq 6 and !$suppressForm and count($form.errors) gt 0}
{ts}Please correct the following errors in the form fields below:{/ts} diff --git a/templates/CRM/common/snippet.tpl b/templates/CRM/common/snippet.tpl index cb71658916..5ec59a3807 100644 --- a/templates/CRM/common/snippet.tpl +++ b/templates/CRM/common/snippet.tpl @@ -39,8 +39,10 @@ {else}
- {* Check for Status message for the page (stored in session->getStatus). Status is cleared on retrieval. *} - {include file="CRM/common/status.tpl"} + {* Display Status messages unless we are outputting json. *} + {if $smarty.get.snippet neq 6} + {include file="CRM/common/status.tpl"} + {/if} {if !empty($isForm)} diff --git a/templates/CRM/common/status.tpl b/templates/CRM/common/status.tpl index bae44e517c..f42dd5b7f7 100644 --- a/templates/CRM/common/status.tpl +++ b/templates/CRM/common/status.tpl @@ -33,6 +33,6 @@ {else} {assign var="infoType" value=$statItem.type} {/if} - {include file="CRM/common/info.tpl" infoTitle=$statItem.title infoMessage=$statItem.text infoOptions=$statItem.options} + {include file="CRM/common/info.tpl" infoTitle=$statItem.title infoMessage=$statItem.text infoOptions=$statItem.options|@json_encode} {/foreach} {/if}