Merge pull request #2157 from kurund/CRM-13896
[civicrm-core.git] / CRM / Core / Page.php
index 95ff5575788cfb1e070f0ae980556e11bebddec3..4ac7d41c09e3ce249aa238c9424e7bf8b2324127 100644 (file)
@@ -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
    *
@@ -123,13 +130,19 @@ class CRM_Core_Page {
       self::$_session = CRM_Core_Session::singleton();
     }
 
-    if (isset($_REQUEST['snippet']) && $_REQUEST['snippet']) {
-      if ($_REQUEST['snippet'] == 3) {
+    // 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;
       }
-      else if ($_REQUEST['snippet'] == 5) {
+      // FIXME - why does this number not match the constant?
+      elseif ($_REQUEST['snippet'] == 5) {
         $this->_print = CRM_Core_Smarty::PRINT_NOFORM;
       }
+      // 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 {
         $this->_print = CRM_Core_Smarty::PRINT_SNIPPET;
       }
@@ -163,7 +176,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 {
@@ -183,6 +196,10 @@ class CRM_Core_Page {
           array('paper_size' => 'a3', 'orientation' => 'landscape')
         );
       }
+      elseif ($this->_print == CRM_Core_Smarty::PRINT_JSON) {
+        $this->ajaxResponse['content'] = $content;
+        CRM_Core_Page_AJAX::returnJsonResponse($this->ajaxResponse);
+      }
       else {
         echo $content;
       }