Merge pull request #2157 from kurund/CRM-13896
[civicrm-core.git] / CRM / Core / Page.php
index 8d69a0a80782e2e00023c25aa86ff20dfe93c422..4ac7d41c09e3ce249aa238c9424e7bf8b2324127 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -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;
       }
@@ -155,7 +168,7 @@ class CRM_Core_Page {
 
     self::$_template->assign('mode', $this->_mode);
 
-    $pageTemplateFile = $this->getTemplateFileName();
+    $pageTemplateFile = $this->getHookedTemplateFileName();
     self::$_template->assign('tplFile', $pageTemplateFile);
 
     // invoke the pagRun hook, CRM-3906
@@ -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;
       }
@@ -195,7 +212,7 @@ class CRM_Core_Page {
     if (empty($_GET['snippet'])) {
       // Version check and intermittent alert to admins
       CRM_Utils_VersionCheck::singleton()->versionAlert();
-  
+
       // Debug msg once per hour
       if ($config->debug && CRM_Core_Permission::check('administer CiviCRM') && CRM_Core_Session::singleton()->timer('debug_alert', 3600)) {
         $msg = ts('Warning: Debug is enabled in <a href="%1">system settings</a>. This should not be enabled on production servers.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/debug', 'reset=1')));
@@ -273,6 +290,28 @@ class CRM_Core_Page {
     self::$_template->assign_by_ref($var, $value);
   }
 
+  /**
+   * appends values to template variables
+   *
+   * @param array|string $tpl_var the template variable name(s)
+   * @param mixed $value the value to append
+   * @param bool $merge
+   */
+  function append($tpl_var, $value=NULL, $merge=FALSE) {
+    self::$_template->append($tpl_var, $value, $merge);
+  }
+
+  /**
+   * Returns an array containing template variables
+   *
+   * @param string $name
+   * @param string $type
+   * @return array
+   */
+  function get_template_vars($name=null) {
+    return self::$_template->get_template_vars($name);
+  }
+
   /**
    * function to destroy all the session state of this page.
    *
@@ -297,6 +336,16 @@ class CRM_Core_Page {
     ) . '.tpl';
   }
 
+  /**
+   * A wrapper for getTemplateFileName that includes calling the hook to
+   * prevent us from having to copy & paste the logic of calling the hook
+   */
+  function getHookedTemplateFileName() {
+    $pageTemplateFile = $this->getTemplateFileName();
+    CRM_Utils_Hook::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile);
+    return $pageTemplateFile;
+  }
+
   /**
    * Default extra tpl file basically just replaces .tpl with .extra.tpl
    * i.e. we dont override