From fc05b8dace3316f960581dd40466405c67ca10e1 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 29 Nov 2013 09:54:08 -0800 Subject: [PATCH] CRM-10693 - change PRINT_JSON constant from '6' to 'json' for readability --- CRM/Core/Controller.php | 5 +++-- CRM/Core/Form.php | 6 +++--- CRM/Core/Page.php | 13 +++++++++++-- CRM/Core/Smarty.php | 6 ++++-- js/Common.js | 2 +- templates/CRM/Form/body.tpl | 2 +- templates/CRM/common/snippet.tpl | 2 +- 7 files changed, 24 insertions(+), 12 deletions(-) diff --git a/CRM/Core/Controller.php b/CRM/Core/Controller.php index 21a4ac461b..1c9b09ddd2 100644 --- a/CRM/Core/Controller.php +++ b/CRM/Core/Controller.php @@ -240,8 +240,9 @@ class CRM_Core_Controller extends HTML_QuickForm_Controller { // also used for the various tabs via TabHeader $this->_print = CRM_Core_Smarty::PRINT_NOFORM; } - elseif ($snippet == 6) { - $this->_print = CRM_Core_Smarty::PRINT_NOFORM; + // Respond with JSON if in AJAX context (also support legacy value '6') + elseif (in_array($snippet, array(CRM_Core_Smarty::PRINT_JSON, 6))) { + $this->_print = CRM_Core_Smarty::PRINT_JSON; $this->_QFResponseType = 'json'; } else { diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index a4a38ff557..5c84c5027e 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -99,7 +99,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page { static protected $_template; /** - * What to return to the client if in ajax mode (snippet=6) + * What to return to the client if in ajax mode (snippet=json) * * @var array */ @@ -270,8 +270,8 @@ class CRM_Core_Form extends HTML_QuickForm_Page { $this->postProcess(); $this->postProcessHook(); - // Respond with JSON if in AJAX context - if (!empty($_REQUEST['snippet']) && $_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_JSON) { + // Respond with JSON if in AJAX context (also support legacy value '6') + if (!empty($_REQUEST['snippet']) && in_array($_REQUEST['snippet'], array(CRM_Core_Smarty::PRINT_JSON, 6))) { $this->ajaxResponse['buttonName'] = str_replace('_qf_' . $this->getAttribute('id') . '_', '', $this->controller->getButtonName()); $this->ajaxResponse['action'] = $this->_action; if (isset($this->_id) || isset($this->id)) { diff --git a/CRM/Core/Page.php b/CRM/Core/Page.php index a30dcb3c00..4ac7d41c09 100644 --- a/CRM/Core/Page.php +++ b/CRM/Core/Page.php @@ -104,6 +104,13 @@ class CRM_Core_Page { */ static protected $_session; + /** + * What to return to the client if in ajax mode (snippet=json) + * + * @var array + */ + public $ajaxResponse = array(); + /** * class constructor * @@ -132,7 +139,8 @@ class CRM_Core_Page { elseif ($_REQUEST['snippet'] == 5) { $this->_print = CRM_Core_Smarty::PRINT_NOFORM; } - elseif ($_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_JSON) { + // Support 'json' as well as legacy value '6' + elseif (in_array($_REQUEST['snippet'], array(CRM_Core_Smarty::PRINT_JSON, 6))) { $this->_print = CRM_Core_Smarty::PRINT_JSON; } else { @@ -189,7 +197,8 @@ class CRM_Core_Page { ); } elseif ($this->_print == CRM_Core_Smarty::PRINT_JSON) { - CRM_Core_Page_AJAX::returnJsonResponse($content); + $this->ajaxResponse['content'] = $content; + CRM_Core_Page_AJAX::returnJsonResponse($this->ajaxResponse); } else { echo $content; diff --git a/CRM/Core/Smarty.php b/CRM/Core/Smarty.php index c88b450ee9..20f1512032 100644 --- a/CRM/Core/Smarty.php +++ b/CRM/Core/Smarty.php @@ -59,8 +59,10 @@ class CRM_Core_Smarty extends Smarty { // this prints a complete form and also generates a qfKey, can we replace this with // snippet = 2?? Does the constant _NOFFORM do anything? PRINT_QFKEY = 5, - // this sends the output back in json - PRINT_JSON = 6; + // Note: added in v 4.3 with the value '6' + // Value changed in 4.5 to 'json' for better readability + // @see CRM_Core_Page_AJAX::returnJsonResponse + PRINT_JSON = 'json'; /** * We only need one instance of this object. So we use the singleton diff --git a/js/Common.js b/js/Common.js index 68fdb87bdb..073064e55c 100644 --- a/js/Common.js +++ b/js/Common.js @@ -841,7 +841,7 @@ CRM.validate = CRM.validate || { _formatUrl: function(url) { // Add snippet argument to url if (url.search(/[&?]snippet=/) < 0) { - url += (url.indexOf('?') < 0 ? '?' : '&') + 'snippet=6'; + url += (url.indexOf('?') < 0 ? '?' : '&') + 'snippet=json'; } return url; }, diff --git a/templates/CRM/Form/body.tpl b/templates/CRM/Form/body.tpl index 01ef1b845d..927feb22d8 100644 --- a/templates/CRM/Form/body.tpl +++ b/templates/CRM/Form/body.tpl @@ -33,7 +33,7 @@
{$form.hidden}
{/if} -{if $snippet neq 6 and !$suppressForm and count($form.errors) gt 0} +{if $snippet neq 'json' 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 5ec59a3807..657b845d3c 100644 --- a/templates/CRM/common/snippet.tpl +++ b/templates/CRM/common/snippet.tpl @@ -40,7 +40,7 @@
{* Display Status messages unless we are outputting json. *} - {if $smarty.get.snippet neq 6} + {if $smarty.get.snippet neq 'json'} {include file="CRM/common/status.tpl"} {/if} -- 2.25.1