}
/**
- * 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);
'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 '<textarea>';
- }
- echo json_encode($response);
- if (!$xhr) {
- echo '</textarea>';
- }
- CRM_Utils_System::civiExit();
+ // Note: Post hooks will be called by CRM_Core_Form::mainProcess
}
}
$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();
}
}
*/
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
if (!isset(self::$_template)) {
self::$_template = CRM_Core_Smarty::singleton();
}
+
+ $this->assign('snippet', (int) CRM_Utils_Array::value('snippet', $_REQUEST));
}
static function generateID() {
*/
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);
+ }
}
/**
}
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;
}
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 '<textarea>';
+ }
+ echo json_encode($response);
+ if (!$xhr) {
+ echo '</textarea>';
+ }
+ CRM_Utils_System::civiExit();
+ }
}
$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();
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 '<textarea>';
- }
- echo json_encode($response);
- if (!$xhr) {
- echo '</textarea>';
+ if (!empty($form['errors'])) {
+ $response['status'] = 'form_error';
+ $response['errors'] = $form['errors'];
}
- CRM_Utils_System::civiExit();
+ CRM_Core_Page_AJAX::returnJsonResponse($response);
}
if ($print) {
'text' => $text,
'title' => $title,
'type' => $type,
- 'options' => $options ? json_encode($options) : NULL,
+ 'options' => $options ? $options : NULL,
);
}
}
});
}
+ // 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
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');
<div>{$form.hidden}</div>
{/if}
-{if ! $suppressForm and count($form.errors) gt 0}
+{if $snippet neq 6 and !$suppressForm and count($form.errors) gt 0}
<div class="messages crm-error">
<div class="icon red-icon alert-icon"></div>
{ts}Please correct the following errors in the form fields below:{/ts}
{else}
<div class="crm-container-snippet" bgColor="white">
- {* 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}
<!-- .tpl file invoked: {$tplFile}. Call via form.tpl if we have a form in the page. -->
{if !empty($isForm)}
{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}